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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/jit/parse/parse.h"
  22. #include "include/common/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. }
  66. /* # skip ut test cases temporarily
  67. // Test case 2: test parse object, transfore the CELL instance to api.
  68. TEST_F(TestParserClass, TestParseMethod) {
  69. py::object obj_ = python_adapter::CallPyFn("gtest_input.pipeline.parse.parse_class", "test_parse_object_instance");
  70. Parser::InitParserEnvironment(obj_);
  71. FuncGraphPtr func_graph = ParsePythonCode(obj_);
  72. ASSERT_TRUE(nullptr != func_graph);
  73. // save the func_graph to manager
  74. std::shared_ptr<FuncGraphManager> manager = Manage(func_graph);
  75. // call resolve
  76. bool ret_ = ResolveAll(manager);
  77. ASSERT_TRUE(ret_);
  78. }
  79. // Test case 3: common test for debug ptest case
  80. TEST_F(TestParserClass, TestParseCompileAPI) {
  81. python_adapter::CallPyFn("gtest_input.pipeline.parse.parse_compile", "test_build");
  82. MS_LOG(DEBUG) << "Test end";
  83. }
  84. */
  85. } // namespace parse
  86. } // namespace mindspore