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 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. namespace mindspore {
  34. namespace compile {
  35. using Tensor = tensor::Tensor;
  36. class TestCompileSegmentRunner : public UT::Common {
  37. public:
  38. TestCompileSegmentRunner() : get_py_fun_("gtest_input.vm", true) { UT::InitPythonPath(); }
  39. protected:
  40. UT::PyFuncGraphFetcher get_py_fun_;
  41. VM vm_;
  42. };
  43. TEST_F(TestCompileSegmentRunner, test_MsVmConvert1) {
  44. FuncGraphPtr g = get_py_fun_("scalar_add");
  45. // g was managed by local variable manager in get_py_fun_ and that manager will be freed as no reference.
  46. // so a new manager should be declared to make get_outputs() in segment_runner.cc happy.
  47. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  48. BackendPtr b = std::make_shared<Backend>("vm");
  49. CompileGraph transform_(b);
  50. auto splits = transform_.SplitNodes(g);
  51. VectorRef args({1.0, 2.0});
  52. std::vector<BaseRef> todos(splits.size());
  53. auto it = std::copy_if(std::begin(splits), std::end(splits), std::begin(todos),
  54. [](const BaseRef &seg) -> bool { return utils::isa<VectorRef>(seg); });
  55. todos.resize(std::distance(todos.begin(), it));
  56. ASSERT_EQ(todos.size(), 1);
  57. AnfNodePtrList anf_list;
  58. for (auto &item : utils::cast<VectorRef>(todos[0])) {
  59. anf_list.push_back(utils::cast<AnfNodePtr>(item));
  60. }
  61. auto convertResult = MsVmConvert(anf_list, "");
  62. auto runResult = (*(convertResult.run))(args);
  63. ASSERT_TRUE(runResult.size() == 1 && py::cast<double>(BaseRefToPyData(runResult[0])) == 3.0);
  64. }
  65. TEST_F(TestCompileSegmentRunner, test_MsVmConvert2) {
  66. FuncGraphPtr g = get_py_fun_("scalar_mul");
  67. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  68. BackendPtr b = std::make_shared<Backend>("vm");
  69. CompileGraph transform_(b);
  70. auto splits = transform_.SplitNodes(g);
  71. VectorRef args({1.0, 2.0});
  72. std::vector<BaseRef> todos(splits.size());
  73. auto it = std::copy_if(std::begin(splits), std::end(splits), std::begin(todos),
  74. [](const BaseRef &seg) -> bool { return utils::isa<VectorRef>(seg); });
  75. todos.resize(std::distance(todos.begin(), it));
  76. ASSERT_EQ(todos.size(), 1);
  77. AnfNodePtrList anf_list;
  78. for (auto &item : utils::cast<VectorRef>(todos[0])) {
  79. anf_list.push_back(utils::cast<AnfNodePtr>(item));
  80. }
  81. auto convertResult = MsVmConvert(anf_list, "");
  82. auto runResult = (*(convertResult.run))(args);
  83. ASSERT_TRUE(runResult.size() == 1 && py::cast<double>(BaseRefToPyData(runResult[0])) == 2.0);
  84. }
  85. TEST_F(TestCompileSegmentRunner, test_if) {
  86. FuncGraphPtr g = get_py_fun_("test_if");
  87. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  88. BackendPtr b = std::make_shared<Backend>("vm");
  89. CompileGraph transform_(b);
  90. auto splits = transform_.SplitNodes(g);
  91. VectorRef args({1.0, 2.0});
  92. std::vector<BaseRef> todos(splits.size());
  93. auto it = std::copy_if(std::begin(splits), std::end(splits), std::begin(todos),
  94. [](const BaseRef &seg) -> bool { return utils::isa<VectorRef>(seg); });
  95. todos.resize(std::distance(todos.begin(), it));
  96. ASSERT_EQ(todos.size(), 1);
  97. AnfNodePtrList anf_list;
  98. for (auto &item : utils::cast<VectorRef>(todos[0])) {
  99. anf_list.push_back(utils::cast<AnfNodePtr>(item));
  100. }
  101. auto convertResult = MsVmConvert(anf_list, "");
  102. auto runResult = (*(convertResult.run))(args);
  103. auto result = py::cast<bool>(BaseRefToPyData(runResult[0]));
  104. ASSERT_TRUE(runResult.size() == 1 && result == false);
  105. }
  106. TEST_F(TestCompileSegmentRunner, test_RunOperation1) {
  107. VectorRef args({1});
  108. auto res = RunOperation(std::make_shared<PrimitivePy>(py::str(prim::kPrimIdentity->name()), py::none()), args);
  109. ASSERT_EQ(py::cast<int>(BaseRefToPyData(res)), 1);
  110. }
  111. TEST_F(TestCompileSegmentRunner, test_RunOperation2) {
  112. VectorRef args({1, 2});
  113. auto res = RunOperation(std::make_shared<PrimitivePy>(py::str(prim::kPrimScalarGt->name()), py::none()), args);
  114. ASSERT_EQ(py::cast<bool>(BaseRefToPyData(res)), false);
  115. }
  116. } // namespace compile
  117. } // namespace mindspore