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

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