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

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