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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. 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, 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. // draw graph
  137. int i = 0;
  138. for (auto tmp : manager->func_graphs()) {
  139. std::string name = "ut_parser_for_loop_" + std::to_string(i) + ".dot";
  140. draw::Draw(name, tmp);
  141. i++;
  142. }
  143. }
  144. TEST_F(TestParser, TestParseGraphCompareExprLt) {
  145. GetPythonFunction("test_compare_lt");
  146. FuncGraphPtr ret_val = ParsePythonCode(fn);
  147. ASSERT_TRUE(nullptr != ret_val);
  148. }
  149. TEST_F(TestParser, TestParseGraphCompareExprGt) {
  150. GetPythonFunction("test_compare_gt");
  151. FuncGraphPtr ret_val = ParsePythonCode(fn);
  152. ASSERT_TRUE(nullptr != ret_val);
  153. }
  154. TEST_F(TestParser, TestParseGraphCompareExprLe) {
  155. GetPythonFunction("test_compare_le");
  156. FuncGraphPtr ret_val = ParsePythonCode(fn);
  157. ASSERT_TRUE(nullptr != ret_val);
  158. }
  159. TEST_F(TestParser, TestParseGraphCompareExprNe) {
  160. GetPythonFunction("test_compare_ne");
  161. FuncGraphPtr ret_val = ParsePythonCode(fn);
  162. ASSERT_TRUE(nullptr != ret_val);
  163. }
  164. TEST_F(TestParser, TestParseGraphCompareExprGe) {
  165. GetPythonFunction("test_compare_ge");
  166. FuncGraphPtr ret_val = ParsePythonCode(fn);
  167. ASSERT_TRUE(nullptr != ret_val);
  168. }
  169. TEST_F(TestParser, TestParseGraphCompareExprEq) {
  170. GetPythonFunction("test_compare_eq");
  171. FuncGraphPtr ret_val = ParsePythonCode(fn);
  172. ASSERT_TRUE(nullptr != ret_val);
  173. }
  174. TEST_F(TestParser, TestParseGraphBoolOpTwoAnd) {
  175. GetPythonFunction("test_boolop_two_and");
  176. FuncGraphPtr ret_val = ParsePythonCode(fn);
  177. ASSERT_TRUE(nullptr != ret_val);
  178. }
  179. TEST_F(TestParser, TestParseGraphBoolOpThreeAnd) {
  180. GetPythonFunction("test_boolop_three_and");
  181. FuncGraphPtr ret_val = ParsePythonCode(fn);
  182. ASSERT_TRUE(nullptr != ret_val);
  183. }
  184. TEST_F(TestParser, TestParseGraphBoolOpTwoOr) {
  185. GetPythonFunction("test_boolop_two_or");
  186. FuncGraphPtr ret_val = ParsePythonCode(fn);
  187. ASSERT_TRUE(nullptr != ret_val);
  188. }
  189. TEST_F(TestParser, TestParseGraphBoolOpThreeOr) {
  190. GetPythonFunction("test_boolop_three_or");
  191. FuncGraphPtr ret_val = ParsePythonCode(fn);
  192. ASSERT_TRUE(nullptr != ret_val);
  193. }
  194. TEST_F(TestParser, TestParseGraphBoolOpMixAndOr) {
  195. GetPythonFunction("test_boolop_mix_and_or");
  196. FuncGraphPtr ret_val = ParsePythonCode(fn);
  197. ASSERT_TRUE(nullptr != ret_val);
  198. }
  199. TEST_F(TestParser, TestParseGraphLambda) {
  200. GetPythonFunction("test_lambda");
  201. FuncGraphPtr ret_val = ParsePythonCode(fn);
  202. ASSERT_TRUE(nullptr != ret_val);
  203. }
  204. TEST_F(TestParser, TestParseGraphFuncDef) {
  205. GetPythonFunction("test_funcdef");
  206. FuncGraphPtr ret_val = ParsePythonCode(fn);
  207. ASSERT_TRUE(nullptr != ret_val);
  208. }
  209. TEST_F(TestParser, TestParseGraphSimpleClosure) {
  210. GetPythonFunction("test_simple_closure");
  211. FuncGraphPtr ret_val = ParsePythonCode(fn);
  212. ASSERT_TRUE(nullptr != ret_val);
  213. }
  214. TEST_F(TestParser, TestParseGraphTestTuple) {
  215. GetPythonFunction("test_tuple_fn");
  216. FuncGraphPtr ret_val = ParsePythonCode(fn);
  217. ASSERT_TRUE(nullptr != ret_val);
  218. }
  219. TEST_F(TestParser, TestParseGraphTupleAssign) {
  220. GetPythonFunction("test_assign_tuple");
  221. FuncGraphPtr ret_val = ParsePythonCode(fn);
  222. ASSERT_TRUE(nullptr != ret_val);
  223. }
  224. TEST_F(TestParser, TestParseGraphTestList) {
  225. GetPythonFunction("test_list_fn");
  226. FuncGraphPtr ret_val = ParsePythonCode(fn);
  227. ASSERT_TRUE(nullptr != ret_val);
  228. }
  229. TEST_F(TestParser, TestParseGraphUnaryOp) {
  230. GetPythonFunction("test_unary");
  231. FuncGraphPtr ret_val = ParsePythonCode(fn);
  232. ASSERT_TRUE(nullptr != ret_val);
  233. }
  234. TEST_F(TestParser, TestParseGraphAguassign) {
  235. GetPythonFunction("test_augassign");
  236. FuncGraphPtr ret_val = ParsePythonCode(fn);
  237. ASSERT_TRUE(nullptr != ret_val);
  238. }
  239. TEST_F(TestParser, TestParseSystemFunction) {
  240. GetPythonFunction("test_sys_call");
  241. FuncGraphPtr ret_val = ParsePythonCode(fn);
  242. ASSERT_TRUE(nullptr != ret_val);
  243. }
  244. TEST_F(TestParser, TestParseGraphBoolNot) {
  245. GetPythonFunction("test_bool_not");
  246. FuncGraphPtr ret_val = ParsePythonCode(fn);
  247. ASSERT_TRUE(nullptr != ret_val);
  248. // save the func_graph to manager
  249. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  250. // call resolve
  251. bool ret_ = ResolveAll(manager);
  252. ASSERT_TRUE(ret_);
  253. // draw graph
  254. int i = 0;
  255. for (auto tmp : manager->func_graphs()) {
  256. std::string name = "ut_parser_for_not_" + std::to_string(i) + ".dot";
  257. draw::Draw(name, tmp);
  258. i++;
  259. }
  260. }
  261. TEST_F(TestParser, TestCallPythonFnUseTupleParamete) {
  262. GetPythonFunction("test_call_fn_use_tuple");
  263. py::tuple params = py::tuple(5);
  264. params[0] = 0;
  265. params[1] = 1;
  266. params[2] = 2.0;
  267. params[3] = fn;
  268. params[4] = "test_call_fn_use_tuple";
  269. py::object result =
  270. python_adapter::CallPyFn("gtest_input.pipeline.parse.parser_test", "test_call_fn_use_tuple", params);
  271. int ret_size = py::cast<int>(result);
  272. ASSERT_EQ(ret_size, 5);
  273. }
  274. TEST_F(TestParser, TestParseGraphSubscriptSetitem) {
  275. GetPythonFunction("test_subscript_setitem");
  276. FuncGraphPtr ret_val = ParsePythonCode(fn);
  277. ASSERT_TRUE(nullptr != ret_val);
  278. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  279. bool ret_ = ResolveAll(manager);
  280. ASSERT_TRUE(ret_);
  281. }
  282. TEST_F(TestParser, TestParseGraphDict) {
  283. GetPythonFunction("test_dict");
  284. FuncGraphPtr ret_val = ParsePythonCode(fn);
  285. ASSERT_TRUE(nullptr != ret_val);
  286. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  287. bool ret_ = ResolveAll(manager);
  288. ASSERT_TRUE(ret_);
  289. }
  290. TEST_F(TestParser, TestParseGraphCallVargs) {
  291. GetPythonFunction("test_call_variable");
  292. FuncGraphPtr ret_val = ParsePythonCode(fn);
  293. ASSERT_TRUE(nullptr != ret_val);
  294. std::shared_ptr<FuncGraphManager> manager = Manage(ret_val);
  295. bool ret_ = ResolveAll(manager);
  296. ASSERT_TRUE(ret_);
  297. }
  298. TEST_F(TestParser, TestParserUndefinedVar) {
  299. py::function fn_ = python_adapter::GetPyFn("gtest_input.pipeline.parse.parser_test", "test_parse_undefined_var");
  300. // parse undefined var
  301. EXPECT_THROW({ ParsePythonCode(fn_); }, std::runtime_error);
  302. }
  303. } // namespace parse
  304. } // namespace mindspore