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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 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. ParseAst ast = ParseAst(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, TestParseGraphFailure) {
  84. GetPythonFunction("get_no_return_fn");
  85. // create parser
  86. std::shared_ptr<ParseAst> ast = std::make_shared<ParseAst>(fn);
  87. bool succ = ast->InitParseAstInfo();
  88. ASSERT_TRUE(succ = true);
  89. std::shared_ptr<Parser> parser = std::make_shared<Parser>(ast);
  90. // parse ast to graph
  91. FuncGraphPtr func_graph = parser->ParseFuncGraph();
  92. ASSERT_EQ(PARSE_NO_RETURN, parser->errcode());
  93. ASSERT_TRUE(nullptr == func_graph);
  94. }
  95. TEST_F(TestParser, TestParseGraphIf) {
  96. GetPythonFunction("test_if");
  97. FuncGraphPtr ret_val = ParsePythonCode(fn);
  98. ASSERT_TRUE(nullptr != ret_val);
  99. }
  100. TEST_F(TestParser, TestParseGraphIfExp) {
  101. GetPythonFunction("test_ifexp");
  102. FuncGraphPtr ret_val = ParsePythonCode(fn);
  103. ASSERT_TRUE(nullptr != ret_val);
  104. }
  105. TEST_F(TestParser, TestParseGraphIfNested) {
  106. GetPythonFunction("test_if_nested");
  107. FuncGraphPtr ret_val = ParsePythonCode(fn);
  108. ASSERT_TRUE(nullptr != ret_val);
  109. }
  110. TEST_F(TestParser, TestParseWhile) {
  111. GetPythonFunction("test_while");
  112. FuncGraphPtr ret_val = ParsePythonCode(fn);
  113. ASSERT_TRUE(nullptr != ret_val);
  114. }
  115. TEST_F(TestParser, TestParseGraphNum) {
  116. FuncGraphPtr ret_val;
  117. GetPythonFunction("testDoNum");
  118. ret_val = ParsePythonCode(fn);
  119. ASSERT_TRUE(nullptr != ret_val);
  120. }
  121. TEST_F(TestParser, TestParseGraphStr) {
  122. FuncGraphPtr ret_val;
  123. GetPythonFunction("testDoStr");
  124. ret_val = ParsePythonCode(fn);
  125. ASSERT_TRUE(nullptr != ret_val);
  126. }
  127. TEST_F(TestParser, TestParseGraphNamedConst) {
  128. FuncGraphPtr ret_val;
  129. GetPythonFunction("testDoNamedConstTrue");
  130. ret_val = ParsePythonCode(fn);
  131. ASSERT_TRUE(nullptr != ret_val);
  132. GetPythonFunction("testDoNamedConstFalse");
  133. ret_val = ParsePythonCode(fn);
  134. ASSERT_TRUE(nullptr != ret_val);
  135. GetPythonFunction("testDoNamedConstNone");
  136. ret_val = ParsePythonCode(fn);
  137. ASSERT_TRUE(nullptr != ret_val);
  138. }
  139. TEST_F(TestParser, TestParseGraphForStatement) {
  140. GetPythonFunction("test_for");
  141. FuncGraphPtr func_graph = ParsePythonCode(fn);
  142. ASSERT_TRUE(nullptr != func_graph);
  143. // save the func_graph to manager
  144. std::shared_ptr<FuncGraphManager> manager = Manage(func_graph);
  145. // call resolve
  146. bool ret_ = ResolveAll(manager);
  147. ASSERT_TRUE(ret_);
  148. // draw graph
  149. int i = 0;
  150. for (auto tmp : manager->func_graphs()) {
  151. std::string name = "ut_parser_for_loop_" + std::to_string(i) + ".dot";
  152. draw::Draw(name, tmp);
  153. i++;
  154. }
  155. }
  156. TEST_F(TestParser, TestParseGraphCompareExprLt) {
  157. GetPythonFunction("test_compare_lt");
  158. FuncGraphPtr ret_val = ParsePythonCode(fn);
  159. ASSERT_TRUE(nullptr != ret_val);
  160. }
  161. TEST_F(TestParser, TestParseGraphCompareExprGt) {
  162. GetPythonFunction("test_compare_gt");
  163. FuncGraphPtr ret_val = ParsePythonCode(fn);
  164. ASSERT_TRUE(nullptr != ret_val);
  165. }
  166. TEST_F(TestParser, TestParseGraphCompareExprLe) {
  167. GetPythonFunction("test_compare_le");
  168. FuncGraphPtr ret_val = ParsePythonCode(fn);
  169. ASSERT_TRUE(nullptr != ret_val);
  170. }
  171. TEST_F(TestParser, TestParseGraphCompareExprNe) {
  172. GetPythonFunction("test_compare_ne");
  173. FuncGraphPtr ret_val = ParsePythonCode(fn);
  174. ASSERT_TRUE(nullptr != ret_val);
  175. }
  176. TEST_F(TestParser, TestParseGraphCompareExprGe) {
  177. GetPythonFunction("test_compare_ge");
  178. FuncGraphPtr ret_val = ParsePythonCode(fn);
  179. ASSERT_TRUE(nullptr != ret_val);
  180. }
  181. TEST_F(TestParser, TestParseGraphCompareExprEq) {
  182. GetPythonFunction("test_compare_eq");
  183. FuncGraphPtr ret_val = ParsePythonCode(fn);
  184. ASSERT_TRUE(nullptr != ret_val);
  185. }
  186. TEST_F(TestParser, TestParseGraphBoolOpTwoAnd) {
  187. GetPythonFunction("test_boolop_two_and");
  188. FuncGraphPtr ret_val = ParsePythonCode(fn);
  189. ASSERT_TRUE(nullptr != ret_val);
  190. }
  191. TEST_F(TestParser, TestParseGraphBoolOpThreeAnd) {
  192. GetPythonFunction("test_boolop_three_and");
  193. FuncGraphPtr ret_val = ParsePythonCode(fn);
  194. ASSERT_TRUE(nullptr != ret_val);
  195. }
  196. TEST_F(TestParser, TestParseGraphBoolOpTwoOr) {
  197. GetPythonFunction("test_boolop_two_or");
  198. FuncGraphPtr ret_val = ParsePythonCode(fn);
  199. ASSERT_TRUE(nullptr != ret_val);
  200. }
  201. TEST_F(TestParser, TestParseGraphBoolOpThreeOr) {
  202. GetPythonFunction("test_boolop_three_or");
  203. FuncGraphPtr ret_val = ParsePythonCode(fn);
  204. ASSERT_TRUE(nullptr != ret_val);
  205. }
  206. TEST_F(TestParser, TestParseGraphBoolOpMixAndOr) {
  207. GetPythonFunction("test_boolop_mix_and_or");
  208. FuncGraphPtr ret_val = ParsePythonCode(fn);
  209. ASSERT_TRUE(nullptr != ret_val);
  210. }
  211. TEST_F(TestParser, TestParseGraphLambda) {
  212. GetPythonFunction("test_lambda");
  213. FuncGraphPtr ret_val = ParsePythonCode(fn);
  214. ASSERT_TRUE(nullptr != ret_val);
  215. }
  216. TEST_F(TestParser, TestParseGraphFuncDef) {
  217. GetPythonFunction("test_funcdef");
  218. FuncGraphPtr ret_val = ParsePythonCode(fn);
  219. ASSERT_TRUE(nullptr != ret_val);
  220. }
  221. TEST_F(TestParser, TestParseGraphSimpleClosure) {
  222. GetPythonFunction("test_simple_closure");
  223. FuncGraphPtr ret_val = ParsePythonCode(fn);
  224. ASSERT_TRUE(nullptr != ret_val);
  225. }
  226. TEST_F(TestParser, TestParseGraphTestTuple) {
  227. GetPythonFunction("test_tuple_fn");
  228. FuncGraphPtr ret_val = ParsePythonCode(fn);
  229. ASSERT_TRUE(nullptr != ret_val);
  230. }
  231. TEST_F(TestParser, TestParseGraphTupleAssign) {
  232. GetPythonFunction("test_assign_tuple");
  233. FuncGraphPtr ret_val = ParsePythonCode(fn);
  234. ASSERT_TRUE(nullptr != ret_val);
  235. }
  236. TEST_F(TestParser, TestParseGraphTestList) {
  237. GetPythonFunction("test_list_fn");
  238. FuncGraphPtr ret_val = ParsePythonCode(fn);
  239. ASSERT_TRUE(nullptr != ret_val);
  240. }
  241. TEST_F(TestParser, TestParseGraphUnaryOp) {
  242. GetPythonFunction("test_unary");
  243. FuncGraphPtr ret_val = ParsePythonCode(fn);
  244. ASSERT_TRUE(nullptr != ret_val);
  245. }
  246. TEST_F(TestParser, TestParseGraphAguassign) {
  247. GetPythonFunction("test_augassign");
  248. FuncGraphPtr ret_val = ParsePythonCode(fn);
  249. ASSERT_TRUE(nullptr != ret_val);
  250. }
  251. TEST_F(TestParser, TestParseSystemFunction) {
  252. GetPythonFunction("test_sys_call");
  253. FuncGraphPtr ret_val = ParsePythonCode(fn);
  254. ASSERT_TRUE(nullptr != ret_val);
  255. }
  256. TEST_F(TestParser, TestParseGraphBoolNot) {
  257. GetPythonFunction("test_bool_not");
  258. FuncGraphPtr ret_val = ParsePythonCode(fn);
  259. ASSERT_TRUE(nullptr != ret_val);
  260. // save the func_graph to manager
  261. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  262. // call resolve
  263. bool ret_ = ResolveAll(manager);
  264. ASSERT_TRUE(ret_);
  265. // draw graph
  266. int i = 0;
  267. for (auto tmp : manager->func_graphs()) {
  268. std::string name = "ut_parser_for_not_" + std::to_string(i) + ".dot";
  269. draw::Draw(name, tmp);
  270. i++;
  271. }
  272. }
  273. TEST_F(TestParser, TestCallPythonFnUseTupleParamete) {
  274. GetPythonFunction("test_call_fn_use_tuple");
  275. py::tuple params = py::tuple(5);
  276. params[0] = 0;
  277. params[1] = 1;
  278. params[2] = 2.0;
  279. params[3] = fn;
  280. params[4] = "test_call_fn_use_tuple";
  281. py::object result =
  282. python_adapter::CallPyFn("gtest_input.pipeline.parse.parser_test", "test_call_fn_use_tuple", params);
  283. int ret_size = py::cast<int>(result);
  284. ASSERT_EQ(ret_size, 5);
  285. }
  286. TEST_F(TestParser, TestParseGraphSubscriptSetitem) {
  287. GetPythonFunction("test_subscript_setitem");
  288. FuncGraphPtr ret_val = ParsePythonCode(fn);
  289. ASSERT_TRUE(nullptr != ret_val);
  290. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  291. bool ret_ = ResolveAll(manager);
  292. ASSERT_TRUE(ret_);
  293. }
  294. TEST_F(TestParser, TestParseGraphDict) {
  295. GetPythonFunction("test_dict");
  296. FuncGraphPtr ret_val = ParsePythonCode(fn);
  297. ASSERT_TRUE(nullptr != ret_val);
  298. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  299. bool ret_ = ResolveAll(manager);
  300. ASSERT_TRUE(ret_);
  301. }
  302. TEST_F(TestParser, TestParseGraphCallVargs) {
  303. GetPythonFunction("test_call_variable");
  304. FuncGraphPtr ret_val = ParsePythonCode(fn);
  305. ASSERT_TRUE(nullptr != ret_val);
  306. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  307. bool ret_ = ResolveAll(manager);
  308. ASSERT_TRUE(ret_);
  309. }
  310. } // namespace parse
  311. } // namespace mindspore