/** * Copyright 2020 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include "common/common_test.h" #include "common/py_func_graph_fetcher.h" #include "ir/manager.h" #include "utils/log_adapter.h" #include "ir/func_graph_cloner.h" #include "pipeline/jit/parse/parse.h" #include "ir/graph_utils.h" #include "pipeline/jit/resource.h" #include "debug/draw.h" #include "frontend/operator/ops.h" #include "vm/segment_runner.h" #include "vm/transform.h" #include "ir/tensor.h" #include "utils/convert_utils.h" #include "utils/convert_utils_py.h" #include "utils/log_adapter.h" namespace mindspore { namespace compile { using Tensor = tensor::Tensor; class TestCompileSegmentRunner : public UT::Common { public: TestCompileSegmentRunner() : get_py_fun_("gtest_input.vm", true) { UT::InitPythonPath(); } protected: UT::PyFuncGraphFetcher get_py_fun_; VM vm_; }; TEST_F(TestCompileSegmentRunner, test_MsVmConvert1) { FuncGraphPtr g = get_py_fun_("scalar_add"); // g was managed by local variable manager in get_py_fun_ and that manager will be freed as no reference. // so a new manager should be declared to make get_outputs() in segment_runner.cc happy. std::shared_ptr manager = mindspore::Manage(g); BackendPtr b = std::make_shared("vm"); CompileGraph transform_(b); auto splits = transform_.SplitNodes(g); VectorRef args({1.0, 2.0}); std::vector todos(splits.size()); auto it = std::copy_if(std::begin(splits), std::end(splits), std::begin(todos), [](const BaseRef &seg) -> bool { return utils::isa(seg); }); todos.resize(std::distance(todos.begin(), it)); ASSERT_EQ(todos.size(), 1); AnfNodePtrList anf_list; for (auto &item : utils::cast(todos[0])) { anf_list.push_back(utils::cast(item)); } auto convertResult = MsVmConvert(anf_list, ""); auto runResult = (*(convertResult.run))(args); ASSERT_TRUE(runResult.size() == 1 && py::cast(BaseRefToPyData(runResult[0])) == 3.0); } TEST_F(TestCompileSegmentRunner, test_MsVmConvert2) { FuncGraphPtr g = get_py_fun_("scalar_mul"); std::shared_ptr manager = mindspore::Manage(g); BackendPtr b = std::make_shared("vm"); CompileGraph transform_(b); auto splits = transform_.SplitNodes(g); VectorRef args({1.0, 2.0}); std::vector todos(splits.size()); auto it = std::copy_if(std::begin(splits), std::end(splits), std::begin(todos), [](const BaseRef &seg) -> bool { return utils::isa(seg); }); todos.resize(std::distance(todos.begin(), it)); ASSERT_EQ(todos.size(), 1); AnfNodePtrList anf_list; for (auto &item : utils::cast(todos[0])) { anf_list.push_back(utils::cast(item)); } auto convertResult = MsVmConvert(anf_list, ""); auto runResult = (*(convertResult.run))(args); ASSERT_TRUE(runResult.size() == 1 && py::cast(BaseRefToPyData(runResult[0])) == 2.0); } TEST_F(TestCompileSegmentRunner, test_if) { FuncGraphPtr g = get_py_fun_("test_if"); std::shared_ptr manager = mindspore::Manage(g); BackendPtr b = std::make_shared("vm"); CompileGraph transform_(b); auto splits = transform_.SplitNodes(g); VectorRef args({1.0, 2.0}); std::vector todos(splits.size()); auto it = std::copy_if(std::begin(splits), std::end(splits), std::begin(todos), [](const BaseRef &seg) -> bool { return utils::isa(seg); }); todos.resize(std::distance(todos.begin(), it)); ASSERT_EQ(todos.size(), 1); AnfNodePtrList anf_list; for (auto &item : utils::cast(todos[0])) { anf_list.push_back(utils::cast(item)); } auto convertResult = MsVmConvert(anf_list, ""); auto runResult = (*(convertResult.run))(args); auto result = py::cast(BaseRefToPyData(runResult[0])); ASSERT_TRUE(runResult.size() == 1 && result == false); } TEST_F(TestCompileSegmentRunner, test_RunOperation1) { VectorRef args({1}); auto res = RunOperation(std::make_shared(py::str(prim::kPrimIdentity->name()), py::none()), args); ASSERT_EQ(py::cast(BaseRefToPyData(res)), 1); } TEST_F(TestCompileSegmentRunner, test_RunOperation2) { VectorRef args({1, 2}); auto res = RunOperation(std::make_shared(py::str(prim::kPrimScalarGt->name()), py::none()), args); ASSERT_EQ(py::cast(BaseRefToPyData(res)), false); } } // namespace compile } // namespace mindspore