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.0 kB

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