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.

opt_test.cc 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/visitor.h"
  22. #include "ir/func_graph_cloner.h"
  23. #include "frontend/optimizer/opt.h"
  24. #include "frontend/optimizer/anf_visitor.h"
  25. #include "frontend/optimizer/irpass.h"
  26. #include "frontend/optimizer/irpass/arithmetic_simplify.h"
  27. #include "debug/draw.h"
  28. #include "frontend/operator/ops.h"
  29. #include "frontend/optimizer/cse.h"
  30. namespace mindspore {
  31. namespace opt {
  32. class TestOptOpt : public UT::Common {
  33. public:
  34. TestOptOpt() : getPyFun("gtest_input.optimizer.opt_test", true) {}
  35. class IdempotentEliminater : public AnfVisitor {
  36. public:
  37. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  38. x_ = nullptr;
  39. AnfVisitor::Match(P, {irpass::IsCNode})(node);
  40. if (x_ == nullptr || node->func_graph() == nullptr) {
  41. return nullptr;
  42. }
  43. return node->func_graph()->NewCNode({NewValueNode(P), x_});
  44. };
  45. void Visit(const CNodePtr &cnode) override {
  46. if (IsPrimitiveCNode(cnode, P) && cnode->inputs().size() == 2) {
  47. x_ = cnode->input(1);
  48. }
  49. }
  50. private:
  51. AnfNodePtr x_{nullptr};
  52. };
  53. class QctToP : public AnfVisitor {
  54. public:
  55. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  56. v_ = nullptr;
  57. AnfVisitor::Match(Q, {irpass::IsVNode})(node);
  58. if (v_ == nullptr || node->func_graph() == nullptr) {
  59. return nullptr;
  60. }
  61. return node->func_graph()->NewCNode({NewValueNode(P), v_});
  62. };
  63. void Visit(const ValueNodePtr &vnode) override { v_ = vnode; }
  64. private:
  65. AnfNodePtr v_{nullptr};
  66. };
  67. void SetUp() {
  68. elim_Z = MakeSubstitution(std::make_shared<irpass::ArithmeticSimplify>(), "elim_Z", prim::kPrimScalarAdd);
  69. elim_R = MakeSubstitution(std::make_shared<irpass::PrimEliminater>(R), "elim_R", R);
  70. idempotent_P = MakeSubstitution(std::make_shared<IdempotentEliminater>(), "idempotent_P", P);
  71. Qct_to_P = MakeSubstitution(std::make_shared<QctToP>(), "Qct_to_P", Q);
  72. }
  73. bool CheckTransform(FuncGraphPtr gbefore, FuncGraphPtr gafter, const SubstitutionList &transform) {
  74. equiv_node.clear();
  75. equiv_graph.clear();
  76. FuncGraphPtr gbefore_clone = BasicClone(gbefore);
  77. OptimizerPtr optimizer = std::make_shared<Optimizer>("ut_test", std::make_shared<pipeline::Resource>());
  78. transform(gbefore_clone, optimizer);
  79. return Isomorphic(gbefore_clone, gafter, &equiv_graph, &equiv_node);
  80. }
  81. bool CheckOpt(FuncGraphPtr before, FuncGraphPtr after, std::vector<SubstitutionPtr> opts = {}) {
  82. SubstitutionList eq(opts);
  83. return CheckTransform(before, after, eq);
  84. }
  85. public:
  86. UT::PyFuncGraphFetcher getPyFun;
  87. FuncGraphPairMapEquiv equiv_graph;
  88. NodeMapEquiv equiv_node;
  89. static const PrimitivePtr P;
  90. static const PrimitivePtr Q;
  91. static const PrimitivePtr R;
  92. SubstitutionPtr elim_Z;
  93. SubstitutionPtr elim_R;
  94. SubstitutionPtr idempotent_P;
  95. SubstitutionPtr Qct_to_P;
  96. };
  97. const PrimitivePtr TestOptOpt::P = std::make_shared<Primitive>("P");
  98. const PrimitivePtr TestOptOpt::Q = std::make_shared<Primitive>("Q");
  99. const PrimitivePtr TestOptOpt::R = std::make_shared<Primitive>("R");
  100. TEST_F(TestOptOpt, TestCheckOptIsClone) {
  101. FuncGraphPtr before = getPyFun.CallAndParseRet("test_add_zero", "before_1");
  102. ASSERT_TRUE(nullptr != before);
  103. ASSERT_TRUE(CheckOpt(before, before));
  104. ASSERT_FALSE(CheckOpt(before, before, std::vector<SubstitutionPtr>({elim_Z})));
  105. }
  106. TEST_F(TestOptOpt, Elim) {
  107. FuncGraphPtr before = getPyFun.CallAndParseRet("test_add_zero", "before_1");
  108. FuncGraphPtr after = getPyFun.CallAndParseRet("test_add_zero", "after");
  109. ASSERT_TRUE(nullptr != before);
  110. ASSERT_TRUE(nullptr != after);
  111. ASSERT_TRUE(CheckOpt(before, after, std::vector<SubstitutionPtr>({elim_Z})));
  112. }
  113. TEST_F(TestOptOpt, ElimTwo) {
  114. FuncGraphPtr before = getPyFun.CallAndParseRet("test_add_zero", "before_2");
  115. FuncGraphPtr after = getPyFun.CallAndParseRet("test_add_zero", "after");
  116. ASSERT_TRUE(nullptr != before);
  117. ASSERT_TRUE(nullptr != after);
  118. ASSERT_TRUE(CheckOpt(before, after, std::vector<SubstitutionPtr>({elim_Z})));
  119. }
  120. TEST_F(TestOptOpt, ElimR) {
  121. FuncGraphPtr before = getPyFun.CallAndParseRet("test_elimR", "before_1");
  122. FuncGraphPtr after = getPyFun.CallAndParseRet("test_elimR", "after");
  123. ASSERT_TRUE(nullptr != before);
  124. ASSERT_TRUE(nullptr != after);
  125. ASSERT_TRUE(CheckOpt(before, after, std::vector<SubstitutionPtr>({elim_R})));
  126. }
  127. TEST_F(TestOptOpt, idempotent) {
  128. FuncGraphPtr before_2 = getPyFun.CallAndParseRet("test_idempotent", "before_2");
  129. FuncGraphPtr before_1 = getPyFun.CallAndParseRet("test_idempotent", "before_1");
  130. FuncGraphPtr after = getPyFun.CallAndParseRet("test_idempotent", "after");
  131. ASSERT_TRUE(nullptr != before_2);
  132. ASSERT_TRUE(nullptr != before_1);
  133. ASSERT_TRUE(nullptr != after);
  134. ASSERT_TRUE(CheckOpt(before_1, after, std::vector<SubstitutionPtr>({idempotent_P})));
  135. ASSERT_TRUE(CheckOpt(before_2, after, std::vector<SubstitutionPtr>({idempotent_P})));
  136. }
  137. TEST_F(TestOptOpt, ConstantVariable) {
  138. FuncGraphPtr before = getPyFun.CallAndParseRet("test_constant_variable", "before_1");
  139. FuncGraphPtr after = getPyFun.CallAndParseRet("test_constant_variable", "after");
  140. ASSERT_TRUE(nullptr != before);
  141. ASSERT_TRUE(nullptr != after);
  142. ASSERT_TRUE(CheckOpt(before, after, std::vector<SubstitutionPtr>({Qct_to_P})));
  143. }
  144. TEST_F(TestOptOpt, CSE) {
  145. // test a simple cse testcase test_f1
  146. FuncGraphPtr test_graph1 = getPyFun.CallAndParseRet("test_cse", "test_f1");
  147. ASSERT_TRUE(nullptr != test_graph1);
  148. // add func_graph the GraphManager
  149. FuncGraphManagerPtr manager1 = Manage(test_graph1);
  150. draw::Draw("opt_cse_before_1.dot", test_graph1);
  151. ASSERT_EQ(manager1->all_nodes().size(), 9);
  152. auto cse = std::make_shared<CSE>();
  153. ASSERT_TRUE(cse != nullptr);
  154. bool is_changed = cse->Cse(test_graph1, manager1);
  155. ASSERT_TRUE(is_changed);
  156. ASSERT_EQ(manager1->all_nodes().size(), 8);
  157. draw::Draw("opt_cse_after_1.dot", test_graph1);
  158. // test a more complicated case test_f2
  159. FuncGraphPtr test_graph2 = getPyFun.CallAndParseRet("test_cse", "test_f2");
  160. ASSERT_TRUE(nullptr != test_graph2);
  161. FuncGraphManagerPtr manager2 = Manage(test_graph2);
  162. draw::Draw("opt_cse_before_2.dot", test_graph2);
  163. ASSERT_EQ(manager2->all_nodes().size(), 16);
  164. is_changed = cse->Cse(test_graph2, manager2);
  165. ASSERT_TRUE(is_changed);
  166. ASSERT_EQ(manager2->all_nodes().size(), 12);
  167. draw::Draw("opt_cse_after_2.dot", test_graph2);
  168. }
  169. } // namespace opt
  170. } // namespace mindspore