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.

parser_class_test.cc 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <iostream>
  17. #include <string>
  18. #include "common/common_test.h"
  19. #include "common/py_func_graph_fetcher.h"
  20. #include "utils/log_adapter.h"
  21. #include "pipeline/parse/parse.h"
  22. #include "debug/draw.h"
  23. namespace mindspore {
  24. namespace parse {
  25. class TestParserClass : public UT::Common {
  26. public:
  27. TestParserClass() {}
  28. virtual void SetUp();
  29. virtual void TearDown();
  30. };
  31. void TestParserClass::SetUp() { UT::InitPythonPath(); }
  32. void TestParserClass::TearDown() {}
  33. // Test case1 : test class method
  34. TEST_F(TestParserClass, TestParseDataClassApi) {
  35. py::function fn_ = python_adapter::GetPyFn("gtest_input.pipeline.parse.parser_test", "test_class_fn");
  36. Parser::InitParserEnvironment(fn_);
  37. FuncGraphPtr func_graph = ParsePythonCode(fn_);
  38. ASSERT_TRUE(nullptr != func_graph);
  39. // save the func func_graph to manager
  40. std::shared_ptr<FuncGraphManager> manager = Manage(func_graph);
  41. // call resolve
  42. bool ret_ = ResolveAll(manager);
  43. ASSERT_TRUE(ret_);
  44. // check the dataclass
  45. bool is_dataclass = false;
  46. py::object dataclass_obj;
  47. // check the dataclass
  48. for (auto node : manager->all_nodes()) {
  49. if (node->isa<ValueNode>()) {
  50. ValuePtr value = node->cast<ValueNodePtr>()->value();
  51. if (value->isa<PyObjectWrapper>()) {
  52. if (IsValueNode<ClassObject>(node)) {
  53. is_dataclass = true;
  54. dataclass_obj = value->cast<std::shared_ptr<PyObjectWrapper>>()->obj();
  55. }
  56. }
  57. }
  58. }
  59. ASSERT_TRUE(is_dataclass);
  60. // parse data class method
  61. py::object inf_method = python_adapter::GetPyObjAttr(dataclass_obj, "inf");
  62. FuncGraphPtr graph_inf = ParsePythonCode(inf_method);
  63. ASSERT_TRUE(nullptr != graph_inf);
  64. manager->AddFuncGraph(graph_inf);
  65. // draw graph
  66. int i = 0;
  67. for (auto tmp : manager->func_graphs()) {
  68. std::string name = "ut_parser_class_" + std::to_string(i) + ".dot";
  69. draw::Draw(name, tmp);
  70. i++;
  71. }
  72. }
  73. /* # skip ut test cases temporarily
  74. // Test case 2: test parse object, transfore the CELL instance to api.
  75. TEST_F(TestParserClass, TestParseMethod) {
  76. py::object obj_ = python_adapter::CallPyFn("gtest_input.pipeline.parse.parse_class", "test_parse_object_instance");
  77. Parser::InitParserEnvironment(obj_);
  78. FuncGraphPtr func_graph = ParsePythonCode(obj_);
  79. ASSERT_TRUE(nullptr != func_graph);
  80. draw::Draw("ut_parser_method_x.dot", func_graph);
  81. // save the func_graph to manager
  82. std::shared_ptr<FuncGraphManager> manager = Manage(func_graph);
  83. // call resolve
  84. bool ret_ = ResolveAll(manager);
  85. ASSERT_TRUE(ret_);
  86. // draw graph
  87. int i = 0;
  88. for (auto tmp : manager->func_graphs()) {
  89. std::string name = "ut_parser_method_" + std::to_string(i) + ".dot";
  90. draw::Draw(name, tmp);
  91. i++;
  92. }
  93. }
  94. // Test case 3: common test for debug ptest case
  95. TEST_F(TestParserClass, TestParseCompileAPI) {
  96. python_adapter::CallPyFn("gtest_input.pipeline.parse.parse_compile", "test_build");
  97. MS_LOG(DEBUG) << "Test end";
  98. }
  99. */
  100. } // namespace parse
  101. } // namespace mindspore