You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

segment_runner_test.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <algorithm>
  17. #include "common/common_test.h"
  18. #include "common/py_func_graph_fetcher.h"
  19. #include "ir/manager.h"
  20. #include "utils/log_adapter.h"
  21. #include "ir/func_graph_cloner.h"
  22. #include "pipeline/jit/parse/parse.h"
  23. #include "ir/graph_utils.h"
  24. #include "pipeline/jit/resource.h"
  25. #include "debug/draw.h"
  26. #include "frontend/operator/ops.h"
  27. #include "vm/segment_runner.h"
  28. #include "vm/transform.h"
  29. #include "ir/tensor.h"
  30. #include "utils/convert_utils.h"
  31. #include "utils/convert_utils_py.h"
  32. #include "utils/log_adapter.h"
  33. #include "base/core_ops.h"
  34. namespace mindspore {
  35. namespace compile {
  36. using Tensor = tensor::Tensor;
  37. class TestCompileSegmentRunner : public UT::Common {
  38. public:
  39. TestCompileSegmentRunner() : get_py_fun_("gtest_input.vm", true) { UT::InitPythonPath(); }
  40. protected:
  41. UT::PyFuncGraphFetcher get_py_fun_;
  42. VM vm_;
  43. };
  44. TEST_F(TestCompileSegmentRunner, test_MsVmConvert1) {
  45. FuncGraphPtr g = get_py_fun_(prim::kScalarAdd);
  46. // g was managed by local variable manager in get_py_fun_ and that manager will be freed as no reference.
  47. // so a new manager should be declared to make get_outputs() in segment_runner.cc happy.
  48. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  49. BackendPtr b = std::make_shared<Backend>("vm");
  50. auto graph_partition = std::make_shared<GraphPartition>(nonlinear_ops, b->name());
  51. auto segments = graph_partition->Partition(g);
  52. VectorRef args({1.0, 2.0});
  53. auto convertResult = MsVmConvert(segments[0], "");
  54. auto runResult = (*(convertResult.run))(args);
  55. ASSERT_TRUE(runResult.size() == 1 && py::cast<double>(BaseRefToPyData(runResult[0])) == 3.0);
  56. }
  57. TEST_F(TestCompileSegmentRunner, test_MsVmConvert2) {
  58. FuncGraphPtr g = get_py_fun_(prim::kScalarMul);
  59. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  60. BackendPtr b = std::make_shared<Backend>("vm");
  61. auto graph_partition = std::make_shared<GraphPartition>(nonlinear_ops, b->name());
  62. auto segments = graph_partition->Partition(g);
  63. VectorRef args({1.0, 2.0});
  64. auto convertResult = MsVmConvert(segments[0], "");
  65. auto runResult = (*(convertResult.run))(args);
  66. ASSERT_TRUE(runResult.size() == 1 && py::cast<double>(BaseRefToPyData(runResult[0])) == 2.0);
  67. }
  68. TEST_F(TestCompileSegmentRunner, test_if) {
  69. FuncGraphPtr g = get_py_fun_("test_if");
  70. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  71. BackendPtr b = std::make_shared<Backend>("vm");
  72. auto graph_partition = std::make_shared<GraphPartition>(nonlinear_ops, b->name());
  73. auto segments = graph_partition->Partition(g);
  74. VectorRef args({1.0, 2.0});
  75. auto convertResult = MsVmConvert(segments[0], "");
  76. auto runResult = (*(convertResult.run))(args);
  77. auto result = py::cast<bool>(BaseRefToPyData(runResult[0]));
  78. ASSERT_TRUE(runResult.size() == 1 && result == false);
  79. }
  80. TEST_F(TestCompileSegmentRunner, test_RunOperation1) {
  81. VectorRef args({1});
  82. auto res = RunOperation(std::make_shared<PrimitivePy>(py::str(prim::kPrimIdentity->name()), py::none()), args);
  83. ASSERT_EQ(py::cast<int>(BaseRefToPyData(res)), 1);
  84. }
  85. TEST_F(TestCompileSegmentRunner, test_RunOperation2) {
  86. VectorRef args({1, 2});
  87. auto res = RunOperation(std::make_shared<PrimitivePy>(py::str(prim::kPrimScalarGt->name()), py::none()), args);
  88. ASSERT_EQ(py::cast<bool>(BaseRefToPyData(res)), false);
  89. }
  90. } // namespace compile
  91. } // namespace mindspore