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.

lib_test.cc 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <memory>
  18. #include "common/common_test.h"
  19. #include "common/py_func_graph_fetcher.h"
  20. #include "ir/anf.h"
  21. #include "ir/func_graph_cloner.h"
  22. #include "ir/manager.h"
  23. #include "ir/visitor.h"
  24. #include "optimizer/irpass.h"
  25. #include "pipeline/resource.h"
  26. #include "debug/draw.h"
  27. #include "pipeline/parse/data_converter.h"
  28. namespace mindspore {
  29. namespace opt {
  30. using abstract::AnalysisResult;
  31. class TestOptLib : public UT::Common {
  32. public:
  33. TestOptLib() : getPyFun("gtest_input.optimizer.opt_test", true), irpass() {}
  34. void SetUp() {
  35. UT::InitPythonPath();
  36. parse::data_converter::ClearObjectCache();
  37. }
  38. FuncGraphPtr RunTransform(FuncGraphPtr gbefore, const SubstitutionList &transform) {
  39. equiv_node.clear();
  40. equiv_graph.clear();
  41. FuncGraphPtr gbefore_clone = BasicClone(gbefore);
  42. OptimizerPtr optimizer = std::make_shared<Optimizer>("ut_test", std::make_shared<pipeline::Resource>());
  43. transform(gbefore_clone, optimizer);
  44. return gbefore_clone;
  45. }
  46. FuncGraphPtr RunSubs(FuncGraphPtr before, std::vector<SubstitutionPtr> opts = {}) {
  47. SubstitutionList eq(opts);
  48. return RunTransform(before, eq);
  49. }
  50. bool CheckTransform(FuncGraphPtr gbefore, FuncGraphPtr gafter, const SubstitutionList &transform,
  51. bool save_graphs = false) {
  52. equiv_node.clear();
  53. equiv_graph.clear();
  54. FuncGraphPtr gbefore_clone = BasicClone(gbefore);
  55. OptimizerPtr optimizer = std::make_shared<Optimizer>("ut_test", std::make_shared<pipeline::Resource>());
  56. transform(gbefore_clone, optimizer);
  57. if (save_graphs) {
  58. draw::Draw("before.dot", gbefore);
  59. draw::Draw("after.dot", gbefore_clone);
  60. draw::Draw("expected.dot", gafter);
  61. }
  62. return Isomorphic(gbefore_clone, gafter, &equiv_graph, &equiv_node);
  63. }
  64. bool CheckOpt(FuncGraphPtr before, FuncGraphPtr after, std::vector<SubstitutionPtr> opts = {},
  65. bool save_graphs = false) {
  66. if (nullptr == before || nullptr == after) {
  67. return false;
  68. }
  69. SubstitutionList eq(opts);
  70. return CheckTransform(before, after, eq, save_graphs);
  71. }
  72. public:
  73. UT::PyFuncGraphFetcher getPyFun;
  74. FuncGraphPairMapEquiv equiv_graph;
  75. NodeMapEquiv equiv_node;
  76. irpass::OptimizeIRPassLib irpass;
  77. };
  78. TEST_F(TestOptLib, test_expendJ) {
  79. FuncGraphPtr before = getPyFun("test_expendJ");
  80. ASSERT_TRUE(nullptr != before);
  81. FuncGraphPtr after = RunSubs(before, std::vector<SubstitutionPtr>({irpass.expand_jprim_}));
  82. }
  83. TEST_F(TestOptLib, test_simplify_always_true_false) {
  84. FuncGraphPtr before1 = getPyFun.CallAndParseRet("test_simplify_always_true_false", "before_1");
  85. FuncGraphPtr before2 = getPyFun.CallAndParseRet("test_simplify_always_true_false", "before_2");
  86. FuncGraphPtr after = getPyFun.CallAndParseRet("test_simplify_always_true_false", "after");
  87. auto patterns = std::vector<SubstitutionPtr>({irpass.switch_simplify_});
  88. ASSERT_TRUE(CheckOpt(before1, after, patterns));
  89. ASSERT_TRUE(CheckOpt(before2, after, patterns));
  90. }
  91. TEST_F(TestOptLib, test_inline) {
  92. FuncGraphPtr before1 = getPyFun.CallAndParseRet("test_inline", "before");
  93. FuncGraphPtr after = getPyFun.CallAndParseRet("test_inline", "after");
  94. // add infer and renormalize
  95. std::shared_ptr<mindspore::pipeline::Resource> res = std::make_shared<mindspore::pipeline::Resource>();
  96. AbstractBasePtrList args_spec_list;
  97. AbstractBasePtr abstract_v1 = abstract::FromValue(1, true);
  98. AbstractBasePtr abstract_v2 = abstract::FromValue(2, true);
  99. args_spec_list.push_back(abstract_v1);
  100. args_spec_list.push_back(abstract_v2);
  101. AnalysisResult result = pipeline::AbstractAnalyze(res, before1, args_spec_list);
  102. FuncGraphPtr new_graph = pipeline::ProgramSpecialize(res, before1, result.context);
  103. auto patterns = std::vector<SubstitutionPtr>({irpass.arithmetic_simplify_, irpass.switch_simplify_, irpass.inline_});
  104. ASSERT_TRUE(CheckOpt(new_graph, after, patterns));
  105. }
  106. TEST_F(TestOptLib, test_inline_successively) {
  107. FuncGraphPtr before = getPyFun.CallAndParseRet("test_inline_successively", "before");
  108. FuncGraphPtr after = getPyFun.CallAndParseRet("test_inline_successively", "after");
  109. auto patterns = std::vector<SubstitutionPtr>({irpass.inline_});
  110. ASSERT_TRUE(CheckOpt(before, after, patterns));
  111. }
  112. TEST_F(TestOptLib, test_inline_closure) {
  113. FuncGraphPtr before = getPyFun.CallAndParseRet("test_inline_closure", "before");
  114. FuncGraphPtr after = getPyFun.CallAndParseRet("test_inline_closure", "after");
  115. auto patterns = std::vector<SubstitutionPtr>({irpass.inline_});
  116. ASSERT_TRUE(CheckOpt(before, after, patterns));
  117. }
  118. TEST_F(TestOptLib, test_inline_deep_closure) {
  119. FuncGraphPtr before = getPyFun.CallAndParseRet("test_inline_deep_closure", "before");
  120. FuncGraphPtr after = getPyFun.CallAndParseRet("test_inline_deep_closure", "after");
  121. auto patterns = std::vector<SubstitutionPtr>({irpass.inline_});
  122. ASSERT_TRUE(CheckOpt(before, after, patterns));
  123. }
  124. TEST_F(TestOptLib, test_inline_new_closure) {
  125. FuncGraphPtr before = getPyFun.CallAndParseRet("test_inline_new_closure", "before");
  126. FuncGraphPtr after = getPyFun.CallAndParseRet("test_inline_new_closure", "after");
  127. auto patterns = std::vector<SubstitutionPtr>({irpass.inline_});
  128. ASSERT_TRUE(CheckOpt(before, after, patterns));
  129. }
  130. TEST_F(TestOptLib, test_inline_while) {
  131. FuncGraphPtr before = getPyFun.CallAndParseRet("test_inline_while", "before");
  132. auto patterns = std::vector<SubstitutionPtr>({irpass.inline_});
  133. FuncGraphPtr after_ = RunSubs(before, patterns);
  134. ASSERT_TRUE(CheckOpt(before, before, patterns));
  135. }
  136. TEST_F(TestOptLib, test_arithmetic) {
  137. FuncGraphPtr b1_0 = getPyFun.CallAndParseRet("test_arithmetic", "multiply_by_zero_l");
  138. FuncGraphPtr b2_0 = getPyFun.CallAndParseRet("test_arithmetic", "multiply_by_zero_r");
  139. FuncGraphPtr b1 = getPyFun.CallAndParseRet("test_arithmetic", "multiply_by_one_l");
  140. FuncGraphPtr b2 = getPyFun.CallAndParseRet("test_arithmetic", "multiply_by_one_r");
  141. FuncGraphPtr b3 = getPyFun.CallAndParseRet("test_arithmetic", "add_zero_l");
  142. FuncGraphPtr b4 = getPyFun.CallAndParseRet("test_arithmetic", "add_zero_r");
  143. FuncGraphPtr b5 = getPyFun.CallAndParseRet("test_arithmetic", "elim_identity");
  144. FuncGraphPtr after = getPyFun.CallAndParseRet("test_arithmetic", "after");
  145. FuncGraphPtr after_0 = getPyFun.CallAndParseRet("test_arithmetic", "after_0");
  146. auto patterns = std::vector<SubstitutionPtr>({irpass.arithmetic_simplify_});
  147. ASSERT_TRUE(CheckOpt(b1_0, after_0, patterns));
  148. ASSERT_TRUE(CheckOpt(b2_0, after_0, patterns));
  149. ASSERT_TRUE(CheckOpt(b1, after, patterns));
  150. ASSERT_TRUE(CheckOpt(b2, after, patterns));
  151. ASSERT_TRUE(CheckOpt(b3, after, patterns));
  152. ASSERT_TRUE(CheckOpt(b4, after, patterns));
  153. ASSERT_TRUE(CheckOpt(b5, after, patterns));
  154. }
  155. TEST_F(TestOptLib, test_elim_cast_same_dtype) {
  156. FuncGraphPtr before = getPyFun.CallAndParseRet("test_elim_cast_same_dtype", "fp32_cast_fp32");
  157. FuncGraphPtr after = getPyFun.CallAndParseRet("test_elim_cast_same_dtype", "after");
  158. // construct such case that cast srcT equal dstT
  159. auto &inputs = before->output()->cast<CNodePtr>()->inputs();
  160. if (inputs.size() > 2) {
  161. auto cast_node = inputs[0];
  162. auto cast_py = cast_node->cast<ValueNodePtr>()->value()->cast<PrimitivePyPtr>();
  163. cast_py->set_attr("SrcT", TypeIdToType(kNumberTypeFloat32));
  164. cast_py->set_attr("DstT", TypeIdToType(kNumberTypeFloat32));
  165. auto x_node = inputs[1];
  166. std::vector<int> shp = {2, 3};
  167. tensor::TensorPtr x_tensor = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp);
  168. auto x_abstract = x_tensor->ToAbstract();
  169. x_node->set_abstract(x_abstract);
  170. TypePtr t = std::make_shared<TensorType>(std::make_shared<Float>(32));
  171. ValueNodePtr val = std::make_shared<ValueNode>(t);
  172. auto t_abstract = t->ToAbstract();
  173. val->set_abstract(t_abstract);
  174. before->output()->cast<CNodePtr>()->set_input(2, val);
  175. }
  176. FuncGraphPtr gbefore_clone = BasicClone(before);
  177. auto patterns = std::vector<SubstitutionPtr>({irpass.cast_eliminate_});
  178. ASSERT_TRUE(CheckOpt(before, after, patterns));
  179. TypePtr t = std::make_shared<Float>(32);
  180. ValueNodePtr val = std::make_shared<ValueNode>(t);
  181. auto t_abstract = t->ToAbstract();
  182. val->set_abstract(t_abstract);
  183. gbefore_clone->output()->cast<CNodePtr>()->set_input(2, val);
  184. ASSERT_TRUE(CheckOpt(gbefore_clone, after, patterns));
  185. }
  186. TEST_F(TestOptLib, test_elim_reshape_same_shape) {
  187. FuncGraphPtr before = getPyFun.CallAndParseRet("elim_reshape_same_shape", "reshape_to_2_3");
  188. FuncGraphPtr after = getPyFun.CallAndParseRet("elim_reshape_same_shape", "after");
  189. // construct such case that shape is equal to reshape target
  190. auto &inputs = before->output()->cast<CNodePtr>()->inputs();
  191. if (inputs.size() > 1) {
  192. auto x_node = inputs[1];
  193. std::vector<int> shp = {2, 3};
  194. tensor::TensorPtr x_tensor = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp);
  195. auto x_abstract = x_tensor->ToAbstract();
  196. x_node->set_abstract(x_abstract);
  197. }
  198. auto patterns = std::vector<SubstitutionPtr>({irpass.reshape_eliminate_});
  199. ASSERT_TRUE(CheckOpt(before, after, patterns));
  200. if (inputs.size() > 1) {
  201. auto x_node = inputs[1];
  202. std::vector<int> shp = {3, 2};
  203. tensor::TensorPtr x_tensor = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp);
  204. auto x_abstract = x_tensor->ToAbstract();
  205. x_node->set_abstract(x_abstract);
  206. }
  207. ASSERT_FALSE(CheckOpt(before, after, patterns));
  208. }
  209. TEST_F(TestOptLib, elim_two_reshape) {
  210. FuncGraphPtr before = getPyFun.CallAndParseRet("elim_two_reshape", "before");
  211. FuncGraphPtr after = getPyFun.CallAndParseRet("elim_two_reshape", "after");
  212. auto patterns = std::vector<SubstitutionPtr>({irpass.reshape_eliminate_});
  213. ASSERT_TRUE(CheckOpt(before, after, patterns));
  214. }
  215. TEST_F(TestOptLib, elim_two_cast) {
  216. FuncGraphPtr before = getPyFun.CallAndParseRet("elim_two_cast", "before");
  217. FuncGraphPtr after = getPyFun.CallAndParseRet("elim_two_cast", "after");
  218. auto patterns = std::vector<SubstitutionPtr>({irpass.cast_eliminate_});
  219. ASSERT_TRUE(CheckOpt(before, after, patterns));
  220. }
  221. TEST_F(TestOptLib, test_elim_transpose) {
  222. FuncGraphPtr before = getPyFun.CallAndParseRet("test_elim_transpose", "before");
  223. FuncGraphPtr after = getPyFun.CallAndParseRet("test_elim_transpose", "after");
  224. auto patterns = std::vector<SubstitutionPtr>({irpass.transpose_eliminate_});
  225. ASSERT_TRUE(CheckOpt(before, after, patterns));
  226. }
  227. TEST_F(TestOptLib, test_elim_tile_multiply_one) {
  228. FuncGraphPtr before = getPyFun.CallAndParseRet("test_elim_tile_multiply_one", "before");
  229. FuncGraphPtr after = getPyFun.CallAndParseRet("test_elim_tile_multiply_one", "after");
  230. auto patterns = std::vector<SubstitutionPtr>({irpass.tile_eliminate_});
  231. ASSERT_TRUE(CheckOpt(before, after, patterns, true));
  232. }
  233. TEST_F(TestOptLib, test_elim_reduce_mean_shape_one) {
  234. FuncGraphPtr before = getPyFun.CallAndParseRet("test_elim_reduce_mean_shape_one", "before");
  235. FuncGraphPtr after = getPyFun.CallAndParseRet("test_elim_reduce_mean_shape_one", "after");
  236. // construct such case that input x shape is (1), keepdims is true
  237. auto inputs = before->output()->cast<CNodePtr>()->inputs();
  238. if (inputs.size() > 2) {
  239. auto x_node = inputs[1];
  240. std::vector<int> shp = {1};
  241. tensor::TensorPtr x_tensor = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp);
  242. auto x_abstract = x_tensor->ToAbstract();
  243. x_node->set_abstract(x_abstract);
  244. auto reduce_node = inputs[0];
  245. auto reduce = reduce_node->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  246. reduce->set_attr("keep_dims", std::make_shared<BoolImm>(true));
  247. }
  248. auto patterns = std::vector<SubstitutionPtr>({irpass.reduce_eliminate_});
  249. ASSERT_TRUE(CheckOpt(before, after, patterns));
  250. }
  251. TEST_F(TestOptLib, test_elim_all_shape_one) {
  252. FuncGraphPtr before = getPyFun.CallAndParseRet("test_elim_all_shape_one", "before");
  253. FuncGraphPtr after = getPyFun.CallAndParseRet("test_elim_all_shape_one", "after");
  254. // construct such case that input x shape is (1) keep_dims is true
  255. auto inputs = before->output()->cast<CNodePtr>()->inputs();
  256. if (inputs.size() > 2) {
  257. auto x_node = inputs[1];
  258. std::vector<int> shp = {1};
  259. tensor::TensorPtr x_tensor = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp);
  260. auto x_abstract = x_tensor->ToAbstract();
  261. x_node->set_abstract(x_abstract);
  262. auto reduce_node = inputs[0];
  263. auto reduce = reduce_node->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  264. reduce->set_attr("keep_dims", std::make_shared<BoolImm>(true));
  265. }
  266. auto patterns = std::vector<SubstitutionPtr>({irpass.reduce_eliminate_});
  267. ASSERT_TRUE(CheckOpt(before, after, patterns));
  268. }
  269. TEST_F(TestOptLib, test_elim_sum_shape_one) {
  270. FuncGraphPtr before = getPyFun.CallAndParseRet("test_elim_sum_shape_one", "before");
  271. FuncGraphPtr after = getPyFun.CallAndParseRet("test_elim_sum_shape_one", "after");
  272. // construct such case that input x shape is (1) keepdims is true
  273. auto inputs = before->output()->cast<CNodePtr>()->inputs();
  274. if (inputs.size() > 2) {
  275. auto x_node = inputs[1];
  276. std::vector<int> shp = {1};
  277. tensor::TensorPtr x_tensor = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp);
  278. auto x_abstract = x_tensor->ToAbstract();
  279. x_node->set_abstract(x_abstract);
  280. auto reduce_node = inputs[0];
  281. auto reduce = reduce_node->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  282. reduce->set_attr("keep_dims", std::make_shared<BoolImm>(true));
  283. }
  284. auto patterns = std::vector<SubstitutionPtr>({irpass.reduce_eliminate_});
  285. ASSERT_TRUE(CheckOpt(before, after, patterns));
  286. }
  287. TEST_F(TestOptLib, test_tuple_getitem) {
  288. FuncGraphPtr make_get_0 = getPyFun.CallAndParseRet("test_tuple_getitem", "make_get_0");
  289. FuncGraphPtr make_get_1 = getPyFun.CallAndParseRet("test_tuple_getitem", "make_get_1");
  290. FuncGraphPtr after_0 = getPyFun.CallAndParseRet("test_tuple_getitem", "after_0");
  291. FuncGraphPtr after_1 = getPyFun.CallAndParseRet("test_tuple_getitem", "after_1");
  292. auto patterns = std::vector<SubstitutionPtr>({irpass.item_tuple_eliminate_});
  293. ASSERT_TRUE(CheckOpt(make_get_0, after_0, patterns));
  294. ASSERT_TRUE(CheckOpt(make_get_1, after_1, patterns));
  295. }
  296. TEST_F(TestOptLib, test_tuple_setitem) {
  297. FuncGraphPtr before_0 = getPyFun.CallAndParseRet("test_tuple_setitem", "before_0");
  298. FuncGraphPtr before_1 = getPyFun.CallAndParseRet("test_tuple_setitem", "before_1");
  299. FuncGraphPtr after_0 = getPyFun.CallAndParseRet("test_tuple_setitem", "after_0");
  300. FuncGraphPtr after_1 = getPyFun.CallAndParseRet("test_tuple_setitem", "after_1");
  301. auto patterns = std::vector<SubstitutionPtr>({irpass.item_tuple_eliminate_});
  302. ASSERT_TRUE(CheckOpt(before_0, after_0, patterns));
  303. ASSERT_TRUE(CheckOpt(before_1, after_1, patterns));
  304. }
  305. TEST_F(TestOptLib, test_tuple_get_set_item) {
  306. FuncGraphPtr before_0 = getPyFun.CallAndParseRet("test_tuple_get_set_item", "before_0");
  307. FuncGraphPtr after_0 = getPyFun.CallAndParseRet("test_tuple_get_set_item", "after_0");
  308. FuncGraphPtr before_1 = getPyFun.CallAndParseRet("test_tuple_get_set_item", "before_0");
  309. FuncGraphPtr after_1 = getPyFun.CallAndParseRet("test_tuple_get_set_item", "after_0");
  310. auto patterns = std::vector<SubstitutionPtr>({irpass.item_tuple_eliminate_});
  311. ASSERT_TRUE(CheckOpt(before_0, after_0, patterns));
  312. ASSERT_TRUE(CheckOpt(before_1, after_1, patterns));
  313. }
  314. TEST_F(TestOptLib, test_partial) {
  315. FuncGraphPtr before = getPyFun.CallAndParseRet("test_partial", "before");
  316. FuncGraphPtr after = getPyFun.CallAndParseRet("test_partial", "after");
  317. auto patterns = std::vector<SubstitutionPtr>({irpass.partial_eliminate_});
  318. ASSERT_TRUE(CheckOpt(before, after, patterns));
  319. }
  320. TEST_F(TestOptLib, test_replace_applicator) {
  321. FuncGraphPtr before1 = getPyFun.CallAndParseRet("test_replace_applicator", "before1");
  322. FuncGraphPtr before2 = getPyFun.CallAndParseRet("test_replace_applicator", "before2");
  323. FuncGraphPtr before3 = getPyFun.CallAndParseRet("test_replace_applicator", "before3");
  324. FuncGraphPtr after = getPyFun.CallAndParseRet("test_replace_applicator", "after");
  325. auto patterns = std::vector<SubstitutionPtr>({irpass.replace_applicator_});
  326. ASSERT_TRUE(CheckOpt(before1, after, patterns));
  327. ASSERT_TRUE(CheckOpt(before2, after, patterns));
  328. ASSERT_TRUE(CheckOpt(before3, before3, patterns));
  329. }
  330. TEST_F(TestOptLib, test_specialize_on_graph_arguments) {
  331. FuncGraphPtr before = getPyFun.CallAndParseRet("test_specialize_on_graph_arguments", "before");
  332. FuncGraphPtr after = getPyFun.CallAndParseRet("test_specialize_on_graph_arguments", "after");
  333. auto patterns = std::vector<SubstitutionPtr>({irpass.specialize_transform_});
  334. ASSERT_TRUE(CheckOpt(before, after, patterns));
  335. }
  336. TEST_F(TestOptLib, test_incorporate_getitem) {
  337. FuncGraphPtr before1 = getPyFun.CallAndParseRet("test_incorporate_getitem", "before1");
  338. FuncGraphPtr before2 = getPyFun.CallAndParseRet("test_incorporate_getitem", "before2");
  339. FuncGraphPtr after1 = getPyFun.CallAndParseRet("test_incorporate_getitem", "after1");
  340. FuncGraphPtr after2 = getPyFun.CallAndParseRet("test_incorporate_getitem", "after2");
  341. auto patterns = std::vector<SubstitutionPtr>({irpass.incorporate_getitem_});
  342. ASSERT_TRUE(CheckOpt(before1, after1, patterns));
  343. ASSERT_TRUE(CheckOpt(before2, after2, patterns));
  344. }
  345. TEST_F(TestOptLib, test_incorporate_getitem_through_switch) {
  346. FuncGraphPtr before = getPyFun.CallAndParseRet("test_incorporate_getitem_through_switch", "before");
  347. FuncGraphPtr after = getPyFun.CallAndParseRet("test_incorporate_getitem_through_switch", "after");
  348. auto patterns = std::vector<SubstitutionPtr>({irpass.incorporate_getitem_switch_});
  349. ASSERT_TRUE(CheckOpt(before, after, patterns));
  350. }
  351. TEST_F(TestOptLib, test_incorporate_call) {
  352. FuncGraphPtr before = getPyFun.CallAndParseRet("test_incorporate_call", "before");
  353. FuncGraphPtr after = getPyFun.CallAndParseRet("test_incorporate_call", "after");
  354. auto patterns = std::vector<SubstitutionPtr>({irpass.incorporate_call_});
  355. ASSERT_TRUE(CheckOpt(before, after, patterns));
  356. }
  357. TEST_F(TestOptLib, test_incorporate_call_through_switch) {
  358. FuncGraphPtr before = getPyFun.CallAndParseRet("test_incorporate_call_through_switch", "before");
  359. FuncGraphPtr after = getPyFun.CallAndParseRet("test_incorporate_call_through_switch", "after");
  360. auto patterns = std::vector<SubstitutionPtr>({
  361. irpass.incorporate_call_switch_,
  362. irpass.incorporate_call_,
  363. irpass.arithmetic_simplify_,
  364. });
  365. ASSERT_TRUE(CheckOpt(before, after, patterns));
  366. }
  367. TEST_F(TestOptLib, test_float_tuple_getitem_through_switch) {
  368. FuncGraphPtr before = getPyFun.CallAndParseRet("test_float_tuple_getitem_through_switch", "before");
  369. FuncGraphPtr after = getPyFun.CallAndParseRet("test_float_tuple_getitem_through_switch", "after");
  370. auto patterns = std::vector<SubstitutionPtr>({irpass.float_tuple_getitem_switch_});
  371. ASSERT_TRUE(CheckOpt(before, after, patterns));
  372. }
  373. TEST_F(TestOptLib, test_merge_addn) {
  374. FuncGraphPtr before = getPyFun.CallAndParseRet("test_merge_addn", "before");
  375. FuncGraphPtr after = getPyFun.CallAndParseRet("test_merge_addn", "after");
  376. auto patterns = std::vector<SubstitutionPtr>({irpass.merge_addn_});
  377. ASSERT_TRUE(CheckOpt(before, after, patterns));
  378. }
  379. TEST_F(TestOptLib, test_filter_addn_zero) {
  380. FuncGraphPtr before1 = getPyFun.CallAndParseRet("test_addn_zero", "before_1");
  381. FuncGraphPtr after = getPyFun.CallAndParseRet("test_addn_zero", "after");
  382. FuncGraphPtr before2 = getPyFun.CallAndParseRet("test_addn_zero", "before_2");
  383. FuncGraphPtr before3 = getPyFun.CallAndParseRet("test_addn_zero", "before_3");
  384. FuncGraphPtr before4 = getPyFun.CallAndParseRet("test_addn_zero", "before_4");
  385. auto patterns = std::vector<SubstitutionPtr>({irpass.addn_zero_filter_});
  386. ASSERT_TRUE(CheckOpt(before1, after, patterns));
  387. ASSERT_TRUE(CheckOpt(before2, after, patterns));
  388. ASSERT_TRUE(CheckOpt(before3, after, patterns));
  389. ASSERT_TRUE(CheckOpt(before4, before4, patterns));
  390. }
  391. TEST_F(TestOptLib, test_minmax_grad) {
  392. FuncGraphPtr before11 = getPyFun.CallAndParseRet("test_minmax_grad", "before_11");
  393. FuncGraphPtr before12 = getPyFun.CallAndParseRet("test_minmax_grad", "before_12");
  394. FuncGraphPtr before2 = getPyFun.CallAndParseRet("test_minmax_grad", "before_2");
  395. FuncGraphPtr before31 = getPyFun.CallAndParseRet("test_minmax_grad", "before_31");
  396. FuncGraphPtr before32 = getPyFun.CallAndParseRet("test_minmax_grad", "before_32");
  397. FuncGraphPtr before4 = getPyFun.CallAndParseRet("test_minmax_grad", "before_4");
  398. auto patterns = std::vector<SubstitutionPtr>({irpass.minmaximum_grad_});
  399. ASSERT_TRUE(CheckOpt(before11, before11, patterns));
  400. ASSERT_TRUE(CheckOpt(before12, before12, patterns));
  401. ASSERT_TRUE(CheckOpt(before2, before2, patterns));
  402. ASSERT_TRUE(CheckOpt(before31, before31, patterns));
  403. ASSERT_TRUE(CheckOpt(before32, before32, patterns));
  404. ASSERT_TRUE(CheckOpt(before4, before4, patterns));
  405. }
  406. TEST_F(TestOptLib, test_reducesum_one) {
  407. FuncGraphPtr before1 = getPyFun.CallAndParseRet("test_reducesum_one", "before_1");
  408. FuncGraphPtr before2 = getPyFun.CallAndParseRet("test_reducesum_one", "before_2");
  409. FuncGraphPtr before3 = getPyFun.CallAndParseRet("test_reducesum_one", "before_3");
  410. FuncGraphPtr before4 = getPyFun.CallAndParseRet("test_reducesum_one", "before_4");
  411. FuncGraphPtr after1 = getPyFun.CallAndParseRet("test_reducesum_one", "after_1");
  412. FuncGraphPtr after2 = getPyFun.CallAndParseRet("test_reducesum_one", "after_2");
  413. FuncGraphPtr after3 = getPyFun.CallAndParseRet("test_reducesum_one", "after_3");
  414. auto patterns = std::vector<SubstitutionPtr>({irpass.reduce_eliminate_});
  415. std::vector<int> shp = {3, 2, 2, 1};
  416. tensor::TensorPtr x_tensor = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp);
  417. auto x_abstract = x_tensor->ToAbstract();
  418. std::vector<int> shp2 = {3, 2, 1, 1};
  419. tensor::TensorPtr x_tensor2 = std::make_shared<tensor::Tensor>(kFloat32->type_id(), shp2);
  420. auto x_abstract2 = x_tensor2->ToAbstract();
  421. auto inputs = before1->output()->cast<CNodePtr>()->inputs();
  422. if (inputs.size() > 1) {
  423. auto x_node = inputs[1];
  424. x_node->set_abstract(x_abstract);
  425. }
  426. ASSERT_TRUE(CheckOpt(before1, after1, patterns));
  427. auto inputs2 = before2->output()->cast<CNodePtr>()->inputs();
  428. if (inputs2.size() > 1) {
  429. auto x_node2 = inputs2[1];
  430. x_node2->set_abstract(x_abstract2);
  431. }
  432. ASSERT_TRUE(CheckOpt(before2, after1, patterns));
  433. auto inputs3 = before2->output()->cast<CNodePtr>()->inputs();
  434. if (inputs3.size() > 1) {
  435. auto x_node3 = inputs3[1];
  436. x_node3->set_abstract(x_abstract);
  437. }
  438. ASSERT_TRUE(CheckOpt(before2, before2, patterns));
  439. auto inputs4 = before3->output()->cast<CNodePtr>()->inputs();
  440. if (inputs4.size() > 1) {
  441. auto x_node4 = inputs4[1];
  442. x_node4->set_abstract(x_abstract);
  443. }
  444. ASSERT_TRUE(CheckOpt(before3, after2, patterns));
  445. auto inputs5 = before4->output()->cast<CNodePtr>()->inputs();
  446. if (inputs5.size() > 1) {
  447. auto x_node5 = inputs5[1];
  448. x_node5->set_abstract(x_abstract2);
  449. }
  450. ASSERT_TRUE(CheckOpt(before4, after3, patterns));
  451. }
  452. TEST_F(TestOptLib, test_print_tuple_wrapper) {
  453. FuncGraphPtr before1 = getPyFun.CallAndParseRet("test_print_tuple_wrapper", "before1");
  454. FuncGraphPtr before2 = getPyFun.CallAndParseRet("test_print_tuple_wrapper", "before2");
  455. FuncGraphPtr before3 = getPyFun.CallAndParseRet("test_print_tuple_wrapper", "before3");
  456. FuncGraphPtr after1 = getPyFun.CallAndParseRet("test_print_tuple_wrapper", "after1");
  457. FuncGraphPtr after2 = getPyFun.CallAndParseRet("test_print_tuple_wrapper", "after2");
  458. auto patterns = std::vector<SubstitutionPtr>({irpass.print_tuple_wrapper_});
  459. ASSERT_TRUE(CheckOpt(before1, after1, patterns));
  460. ASSERT_TRUE(CheckOpt(before2, after2, patterns));
  461. ASSERT_TRUE(CheckOpt(before3, before3, patterns));
  462. }
  463. TEST_F(TestOptLib, test_constant_duplicate_mul) {
  464. FuncGraphPtr beforell = getPyFun.CallAndParseRet("test_constant_duplicate_mul", "beforell");
  465. FuncGraphPtr beforelr = getPyFun.CallAndParseRet("test_constant_duplicate_mul", "beforelr");
  466. FuncGraphPtr beforerl = getPyFun.CallAndParseRet("test_constant_duplicate_mul", "beforerl");
  467. FuncGraphPtr beforerr = getPyFun.CallAndParseRet("test_constant_duplicate_mul", "beforerr");
  468. FuncGraphPtr after = getPyFun.CallAndParseRet("test_constant_duplicate_mul", "after");
  469. auto patterns = std::vector<SubstitutionPtr>({irpass.arithmetic_simplify_});
  470. ASSERT_TRUE(CheckOpt(beforell, after, patterns));
  471. ASSERT_TRUE(CheckOpt(beforelr, after, patterns));
  472. ASSERT_TRUE(CheckOpt(beforerl, after, patterns));
  473. ASSERT_TRUE(CheckOpt(beforerr, after, patterns));
  474. }
  475. } // namespace opt
  476. } // namespace mindspore