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_test.cc 9.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "debug/draw.h"
  23. namespace mindspore {
  24. namespace parse {
  25. class TestParser : public UT::Common {
  26. public:
  27. TestParser() {}
  28. virtual void SetUp();
  29. virtual void TearDown();
  30. py::function fn;
  31. py::function GetPythonFunction(std::string function);
  32. };
  33. void TestParser::SetUp() { UT::InitPythonPath(); }
  34. void TestParser::TearDown() {}
  35. py::function TestParser::GetPythonFunction(std::string function) {
  36. // init resource
  37. try {
  38. fn = python_adapter::GetPyFn("gtest_input.pipeline.parse.parser_test", function.c_str());
  39. return fn;
  40. } catch (...) {
  41. MS_LOG(ERROR) << "get fn failure!!!";
  42. }
  43. return py::none();
  44. }
  45. TEST_F(TestParser, TestParseApi) {
  46. // Test null fn
  47. py::function fn_null;
  48. FuncGraphPtr func_graph = ParsePythonCode(fn_null);
  49. ASSERT_TRUE(nullptr == func_graph);
  50. // Test parse api
  51. GetPythonFunction("test_f");
  52. func_graph = ParsePythonCode(fn);
  53. ASSERT_TRUE(nullptr != func_graph);
  54. }
  55. TEST_F(TestParser, TestParseAst) {
  56. GetPythonFunction("test_f");
  57. ParseFunctionAst ast = ParseFunctionAst(fn);
  58. bool succ = ast.InitParseAstInfo();
  59. ASSERT_TRUE(succ = true);
  60. // get FunctionDef node
  61. py::object node = ast.GetAstNode();
  62. // check arg
  63. std::string fun_args[] = {"x", "y"};
  64. std::string fun_name = "test_f";
  65. py::list args = ast.GetArgs(node);
  66. for (std::size_t i = 0; i < args.size(); i++) {
  67. py::str pyArg = args[i].attr("arg");
  68. std::string arg = pyArg;
  69. ASSERT_STREQ(arg.c_str(), fun_args[i].c_str());
  70. }
  71. // check function name
  72. // get function name
  73. py::str name = python_adapter::GetPyObjAttr(node, "name");
  74. std::string function_name = name;
  75. ASSERT_STREQ(function_name.c_str(), fun_name.c_str());
  76. }
  77. TEST_F(TestParser, TestParseGraphSuccess) {
  78. GetPythonFunction("test_f");
  79. // parse fn to graph
  80. FuncGraphPtr func_graph = ParsePythonCode(fn);
  81. ASSERT_TRUE(nullptr != func_graph);
  82. }
  83. TEST_F(TestParser, TestParseGraphIf) {
  84. GetPythonFunction("test_if");
  85. FuncGraphPtr ret_val = ParsePythonCode(fn);
  86. ASSERT_TRUE(nullptr != ret_val);
  87. }
  88. TEST_F(TestParser, TestParseGraphIfExp) {
  89. GetPythonFunction("test_ifexp");
  90. FuncGraphPtr ret_val = ParsePythonCode(fn);
  91. ASSERT_TRUE(nullptr != ret_val);
  92. }
  93. TEST_F(TestParser, TestParseGraphIfNested) {
  94. GetPythonFunction("test_if_nested");
  95. FuncGraphPtr ret_val = ParsePythonCode(fn);
  96. ASSERT_TRUE(nullptr != ret_val);
  97. }
  98. TEST_F(TestParser, TestParseWhile) {
  99. GetPythonFunction("test_while");
  100. FuncGraphPtr ret_val = ParsePythonCode(fn);
  101. ASSERT_TRUE(nullptr != ret_val);
  102. }
  103. TEST_F(TestParser, TestParseGraphNum) {
  104. FuncGraphPtr ret_val;
  105. GetPythonFunction("testDoNum");
  106. ret_val = ParsePythonCode(fn);
  107. ASSERT_TRUE(nullptr != ret_val);
  108. }
  109. TEST_F(TestParser, TestParseGraphStr) {
  110. FuncGraphPtr ret_val;
  111. GetPythonFunction("testDoStr");
  112. ret_val = ParsePythonCode(fn);
  113. ASSERT_TRUE(nullptr != ret_val);
  114. }
  115. TEST_F(TestParser, TestParseGraphNamedConst) {
  116. FuncGraphPtr ret_val;
  117. GetPythonFunction("testDoNamedConstTrue");
  118. ret_val = ParsePythonCode(fn);
  119. ASSERT_TRUE(nullptr != ret_val);
  120. GetPythonFunction("testDoNamedConstFalse");
  121. ret_val = ParsePythonCode(fn);
  122. ASSERT_TRUE(nullptr != ret_val);
  123. GetPythonFunction("testDoNamedConstNone");
  124. ret_val = ParsePythonCode(fn);
  125. ASSERT_TRUE(nullptr != ret_val);
  126. }
  127. TEST_F(TestParser, TestParseGraphForStatement) {
  128. GetPythonFunction("test_for");
  129. FuncGraphPtr func_graph = ParsePythonCode(fn);
  130. ASSERT_TRUE(nullptr != func_graph);
  131. // save the func_graph to manager
  132. std::shared_ptr<FuncGraphManager> manager = Manage(func_graph);
  133. // call resolve
  134. bool ret_ = ResolveAll(manager);
  135. ASSERT_TRUE(ret_);
  136. }
  137. TEST_F(TestParser, TestParseGraphCompareExprLt) {
  138. GetPythonFunction("test_compare_lt");
  139. FuncGraphPtr ret_val = ParsePythonCode(fn);
  140. ASSERT_TRUE(nullptr != ret_val);
  141. }
  142. TEST_F(TestParser, TestParseGraphCompareExprGt) {
  143. GetPythonFunction("test_compare_gt");
  144. FuncGraphPtr ret_val = ParsePythonCode(fn);
  145. ASSERT_TRUE(nullptr != ret_val);
  146. }
  147. TEST_F(TestParser, TestParseGraphCompareExprLe) {
  148. GetPythonFunction("test_compare_le");
  149. FuncGraphPtr ret_val = ParsePythonCode(fn);
  150. ASSERT_TRUE(nullptr != ret_val);
  151. }
  152. TEST_F(TestParser, TestParseGraphCompareExprNe) {
  153. GetPythonFunction("test_compare_ne");
  154. FuncGraphPtr ret_val = ParsePythonCode(fn);
  155. ASSERT_TRUE(nullptr != ret_val);
  156. }
  157. TEST_F(TestParser, TestParseGraphCompareExprGe) {
  158. GetPythonFunction("test_compare_ge");
  159. FuncGraphPtr ret_val = ParsePythonCode(fn);
  160. ASSERT_TRUE(nullptr != ret_val);
  161. }
  162. TEST_F(TestParser, TestParseGraphCompareExprEq) {
  163. GetPythonFunction("test_compare_eq");
  164. FuncGraphPtr ret_val = ParsePythonCode(fn);
  165. ASSERT_TRUE(nullptr != ret_val);
  166. }
  167. TEST_F(TestParser, TestParseGraphBoolOpTwoAnd) {
  168. GetPythonFunction("test_boolop_two_and");
  169. FuncGraphPtr ret_val = ParsePythonCode(fn);
  170. ASSERT_TRUE(nullptr != ret_val);
  171. }
  172. TEST_F(TestParser, TestParseGraphBoolOpThreeAnd) {
  173. GetPythonFunction("test_boolop_three_and");
  174. FuncGraphPtr ret_val = ParsePythonCode(fn);
  175. ASSERT_TRUE(nullptr != ret_val);
  176. }
  177. TEST_F(TestParser, TestParseGraphBoolOpTwoOr) {
  178. GetPythonFunction("test_boolop_two_or");
  179. FuncGraphPtr ret_val = ParsePythonCode(fn);
  180. ASSERT_TRUE(nullptr != ret_val);
  181. }
  182. TEST_F(TestParser, TestParseGraphBoolOpThreeOr) {
  183. GetPythonFunction("test_boolop_three_or");
  184. FuncGraphPtr ret_val = ParsePythonCode(fn);
  185. ASSERT_TRUE(nullptr != ret_val);
  186. }
  187. TEST_F(TestParser, TestParseGraphBoolOpMixAndOr) {
  188. GetPythonFunction("test_boolop_mix_and_or");
  189. FuncGraphPtr ret_val = ParsePythonCode(fn);
  190. ASSERT_TRUE(nullptr != ret_val);
  191. }
  192. TEST_F(TestParser, TestParseGraphLambda) {
  193. GetPythonFunction("test_lambda");
  194. FuncGraphPtr ret_val = ParsePythonCode(fn);
  195. ASSERT_TRUE(nullptr != ret_val);
  196. }
  197. TEST_F(TestParser, TestParseGraphFuncDef) {
  198. GetPythonFunction("test_funcdef");
  199. FuncGraphPtr ret_val = ParsePythonCode(fn);
  200. ASSERT_TRUE(nullptr != ret_val);
  201. }
  202. TEST_F(TestParser, TestParseGraphSimpleClosure) {
  203. GetPythonFunction("test_simple_closure");
  204. FuncGraphPtr ret_val = ParsePythonCode(fn);
  205. ASSERT_TRUE(nullptr != ret_val);
  206. }
  207. TEST_F(TestParser, TestParseGraphTestTuple) {
  208. GetPythonFunction("test_tuple_fn");
  209. FuncGraphPtr ret_val = ParsePythonCode(fn);
  210. ASSERT_TRUE(nullptr != ret_val);
  211. }
  212. TEST_F(TestParser, TestParseGraphTupleAssign) {
  213. GetPythonFunction("test_assign_tuple");
  214. FuncGraphPtr ret_val = ParsePythonCode(fn);
  215. ASSERT_TRUE(nullptr != ret_val);
  216. }
  217. TEST_F(TestParser, TestParseGraphTestList) {
  218. GetPythonFunction("test_list_fn");
  219. FuncGraphPtr ret_val = ParsePythonCode(fn);
  220. ASSERT_TRUE(nullptr != ret_val);
  221. }
  222. TEST_F(TestParser, TestParseGraphUnaryOp) {
  223. GetPythonFunction("test_unary");
  224. FuncGraphPtr ret_val = ParsePythonCode(fn);
  225. ASSERT_TRUE(nullptr != ret_val);
  226. }
  227. TEST_F(TestParser, TestParseGraphAguassign) {
  228. GetPythonFunction("test_augassign");
  229. FuncGraphPtr ret_val = ParsePythonCode(fn);
  230. ASSERT_TRUE(nullptr != ret_val);
  231. }
  232. TEST_F(TestParser, TestParseSystemFunction) {
  233. GetPythonFunction("test_sys_call");
  234. FuncGraphPtr ret_val = ParsePythonCode(fn);
  235. ASSERT_TRUE(nullptr != ret_val);
  236. }
  237. TEST_F(TestParser, TestParseGraphBoolNot) {
  238. GetPythonFunction("test_bool_not");
  239. FuncGraphPtr ret_val = ParsePythonCode(fn);
  240. ASSERT_TRUE(nullptr != ret_val);
  241. // save the func_graph to manager
  242. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  243. // call resolve
  244. bool ret_ = ResolveAll(manager);
  245. ASSERT_TRUE(ret_);
  246. }
  247. TEST_F(TestParser, TestCallPythonFnUseTupleParamete) {
  248. GetPythonFunction("test_call_fn_use_tuple");
  249. py::tuple params = py::tuple(5);
  250. params[0] = 0;
  251. params[1] = 1;
  252. params[2] = 2.0;
  253. params[3] = fn;
  254. params[4] = "test_call_fn_use_tuple";
  255. py::object result =
  256. python_adapter::CallPyFn("gtest_input.pipeline.parse.parser_test", "test_call_fn_use_tuple", params);
  257. int ret_size = py::cast<int>(result);
  258. ASSERT_EQ(ret_size, 5);
  259. }
  260. TEST_F(TestParser, TestParseGraphSubscriptSetitem) {
  261. GetPythonFunction("test_subscript_setitem");
  262. FuncGraphPtr ret_val = ParsePythonCode(fn);
  263. ASSERT_TRUE(nullptr != ret_val);
  264. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  265. bool ret_ = ResolveAll(manager);
  266. ASSERT_TRUE(ret_);
  267. }
  268. TEST_F(TestParser, TestParseGraphDict) {
  269. GetPythonFunction("test_dict");
  270. FuncGraphPtr ret_val = ParsePythonCode(fn);
  271. ASSERT_TRUE(nullptr != ret_val);
  272. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  273. bool ret_ = ResolveAll(manager);
  274. ASSERT_TRUE(ret_);
  275. }
  276. TEST_F(TestParser, TestParseGraphCallVargs) {
  277. GetPythonFunction("test_call_variable");
  278. FuncGraphPtr ret_val = ParsePythonCode(fn);
  279. ASSERT_TRUE(nullptr != ret_val);
  280. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  281. bool ret_ = ResolveAll(manager);
  282. ASSERT_TRUE(ret_);
  283. }
  284. TEST_F(TestParser, TestParserUndefinedVar) {
  285. py::function fn_ = python_adapter::GetPyFn("gtest_input.pipeline.parse.parser_test", "test_parse_undefined_var");
  286. // parse undefined var
  287. EXPECT_THROW({ ParsePythonCode(fn_); }, std::runtime_error);
  288. }
  289. } // namespace parse
  290. } // namespace mindspore