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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. auto graph_partition = std::make_shared<GraphPartition>(nonlinear_ops, b->name());
  50. auto segments = graph_partition->Partition(g);
  51. VectorRef args({1.0, 2.0});
  52. auto convertResult = MsVmConvert(segments[0], "");
  53. auto runResult = (*(convertResult.run))(args);
  54. ASSERT_TRUE(runResult.size() == 1 && py::cast<double>(BaseRefToPyData(runResult[0])) == 3.0);
  55. }
  56. TEST_F(TestCompileSegmentRunner, test_MsVmConvert2) {
  57. FuncGraphPtr g = get_py_fun_("scalar_mul");
  58. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  59. BackendPtr b = std::make_shared<Backend>("vm");
  60. auto graph_partition = std::make_shared<GraphPartition>(nonlinear_ops, b->name());
  61. auto segments = graph_partition->Partition(g);
  62. VectorRef args({1.0, 2.0});
  63. auto convertResult = MsVmConvert(segments[0], "");
  64. auto runResult = (*(convertResult.run))(args);
  65. ASSERT_TRUE(runResult.size() == 1 && py::cast<double>(BaseRefToPyData(runResult[0])) == 2.0);
  66. }
  67. TEST_F(TestCompileSegmentRunner, test_if) {
  68. FuncGraphPtr g = get_py_fun_("test_if");
  69. std::shared_ptr<mindspore::FuncGraphManager> manager = mindspore::Manage(g);
  70. BackendPtr b = std::make_shared<Backend>("vm");
  71. auto graph_partition = std::make_shared<GraphPartition>(nonlinear_ops, b->name());
  72. auto segments = graph_partition->Partition(g);
  73. VectorRef args({1.0, 2.0});
  74. auto convertResult = MsVmConvert(segments[0], "");
  75. auto runResult = (*(convertResult.run))(args);
  76. auto result = py::cast<bool>(BaseRefToPyData(runResult[0]));
  77. ASSERT_TRUE(runResult.size() == 1 && result == false);
  78. }
  79. TEST_F(TestCompileSegmentRunner, test_RunOperation1) {
  80. VectorRef args({1});
  81. auto res = RunOperation(std::make_shared<PrimitivePy>(py::str(prim::kPrimIdentity->name()), py::none()), args);
  82. ASSERT_EQ(py::cast<int>(BaseRefToPyData(res)), 1);
  83. }
  84. TEST_F(TestCompileSegmentRunner, test_RunOperation2) {
  85. VectorRef args({1, 2});
  86. auto res = RunOperation(std::make_shared<PrimitivePy>(py::str(prim::kPrimScalarGt->name()), py::none()), args);
  87. ASSERT_EQ(py::cast<bool>(BaseRefToPyData(res)), false);
  88. }
  89. } // namespace compile
  90. } // namespace mindspore