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.

identity_pass_unittest.cc 6.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Copyright 2019-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 <gtest/gtest.h>
  17. #define protected public
  18. #define private public
  19. #include "graph/passes/identity_pass.h"
  20. #include "common/op/ge_op_utils.h"
  21. #include "common/types.h"
  22. #include "graph/anchor.h"
  23. #include "graph/attr_value.h"
  24. #include "graph/compute_graph.h"
  25. #include "graph/op_desc.h"
  26. #include "graph/passes/base_pass.h"
  27. #include "graph/utils/attr_utils.h"
  28. #include "graph/utils/graph_utils.h"
  29. #include "graph/utils/op_desc_utils.h"
  30. #include "graph/utils/tensor_utils.h"
  31. #include "graph_builder_utils.h"
  32. #include "inc/pass_manager.h"
  33. #undef protected
  34. #undef private
  35. using namespace std;
  36. using namespace testing;
  37. using namespace ge;
  38. class UtestIdentityPass : public Test {
  39. protected:
  40. NodePtr AddNode(ComputeGraphPtr graph, const string &name, const string &type, int32_t in_anchors_num = 1,
  41. int32_t out_anchors_num = 1) {
  42. GeTensorDesc tensor_desc;
  43. OpDescPtr opdesc = make_shared<OpDesc>(name, type);
  44. for (int32_t i = 0; i < in_anchors_num; i++) {
  45. opdesc->AddInputDesc(tensor_desc);
  46. }
  47. for (int32_t i = 0; i < out_anchors_num; i++) {
  48. opdesc->AddOutputDesc(tensor_desc);
  49. }
  50. NodePtr node = graph->AddNode(opdesc);
  51. return node;
  52. }
  53. };
  54. /// merge1
  55. /// |
  56. /// identity1
  57. /// | \c
  58. /// var1 var2
  59. static ComputeGraphPtr BuildGraph1() {
  60. ge::ut::GraphBuilder builder("g1");
  61. auto var1 = builder.AddNode("var1", "Variable", 0, 1);
  62. auto var2 = builder.AddNode("var2", "Variable", 0, 1);
  63. auto identity1 = builder.AddNode("identity1", "Identity", 1, 1);
  64. auto merge1 = builder.AddNode("merge1", "Merge", 1, 1);
  65. builder.AddDataEdge(var1, 0, identity1, 0);
  66. builder.AddControlEdge(var2, identity1);
  67. builder.AddDataEdge(identity1, 0, merge1, 0);
  68. return builder.GetGraph();
  69. }
  70. /// addn1
  71. /// |c
  72. /// identity1
  73. /// |
  74. /// switch1
  75. /// |
  76. /// var1
  77. static ComputeGraphPtr BuildGraph2() {
  78. ge::ut::GraphBuilder builder("g1");
  79. auto var1 = builder.AddNode("var1", "Variable", 0, 1);
  80. auto switch1 = builder.AddNode("switch1", "Switch", 2, 2);
  81. auto identity1 = builder.AddNode("identity1", "Identity", 1, 1);
  82. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  83. builder.AddDataEdge(var1, 0, switch1, 0);
  84. builder.AddDataEdge(switch1, 0, identity1, 0);
  85. builder.AddControlEdge(identity1, addn1);
  86. return builder.GetGraph();
  87. }
  88. /// addn1
  89. /// |
  90. /// identity1
  91. /// |
  92. /// switch1
  93. /// |
  94. /// var1
  95. static ComputeGraphPtr BuildGraph3() {
  96. ge::ut::GraphBuilder builder("g3");
  97. auto var1 = builder.AddNode("var1", "Variable", 0, 1);
  98. auto switch1 = builder.AddNode("switch1", "Switch", 2, 2);
  99. auto identity1 = builder.AddNode("identity1", "Identity", 1, 1);
  100. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  101. builder.AddDataEdge(var1, 0, switch1, 0);
  102. builder.AddDataEdge(switch1, 0, identity1, 0);
  103. builder.AddDataEdge(identity1, 0, addn1, 0);
  104. return builder.GetGraph();
  105. }
  106. TEST_F(UtestIdentityPass, succ) {
  107. ComputeGraphPtr graph = std::make_shared<ComputeGraph>("test");
  108. NodePtr node = AddNode(graph, "Identity", IDENTITY);
  109. NodePtr reduce_min_node = AddNode(graph, "reduceMin", REDUCEMIN);
  110. GraphUtils::AddEdge(node->GetOutDataAnchor(0), reduce_min_node->GetInDataAnchor(0));
  111. IdentityPass pass(true);
  112. Status status = pass.Run(node);
  113. EXPECT_EQ(status, SUCCESS);
  114. NodePtr found_node = graph->FindNode("Identity");
  115. EXPECT_EQ(found_node, nullptr);
  116. status = pass.Run(reduce_min_node);
  117. EXPECT_EQ(status, SUCCESS);
  118. string type2 = "FrameworkOp";
  119. node->GetOpDesc()->SetType(type2);
  120. status = pass.Run(node);
  121. NodePtr node_err = AddNode(graph, "Identity", IDENTITY, 1, 2);
  122. status = pass.Run(node_err);
  123. EXPECT_EQ(status, ge::PARAM_INVALID);
  124. }
  125. TEST_F(UtestIdentityPass, skip_merge) {
  126. auto graph = BuildGraph1();
  127. ge::GEPass pass(graph);
  128. ge::NamesToPass names_to_pass;
  129. IdentityPass identity_pass(false);
  130. names_to_pass.emplace_back("IdentityPass", &identity_pass);
  131. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  132. auto identity1 = graph->FindNode("identity1");
  133. EXPECT_NE(identity1, nullptr);
  134. EXPECT_EQ(identity1->GetOutNodes().size(), 1);
  135. EXPECT_EQ(identity1->GetOutDataNodes().at(0)->GetName(), "merge1");
  136. EXPECT_EQ(identity1->GetInNodes().size(), 2);
  137. names_to_pass.clear();
  138. IdentityPass force_pass(true);
  139. names_to_pass.emplace_back("ForceIdentityPass", &force_pass);
  140. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  141. identity1 = graph->FindNode("identity1");
  142. EXPECT_EQ(identity1, nullptr);
  143. }
  144. TEST_F(UtestIdentityPass, skip_switch) {
  145. auto graph = BuildGraph2();
  146. ge::GEPass pass(graph);
  147. ge::NamesToPass names_to_pass;
  148. IdentityPass identity_pass(false);
  149. names_to_pass.emplace_back("IdentityPass", &identity_pass);
  150. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  151. auto identity1 = graph->FindNode("identity1");
  152. EXPECT_NE(identity1, nullptr);
  153. EXPECT_EQ(identity1->GetInNodes().size(), 1);
  154. EXPECT_EQ(identity1->GetInDataNodes().at(0)->GetName(), "switch1");
  155. names_to_pass.clear();
  156. IdentityPass force_pass(true);
  157. names_to_pass.emplace_back("ForceIdentityPass", &force_pass);
  158. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  159. identity1 = graph->FindNode("identity1");
  160. EXPECT_EQ(identity1, nullptr);
  161. }
  162. TEST_F(UtestIdentityPass, norm_after_switch) {
  163. auto graph = BuildGraph3();
  164. ge::GEPass pass(graph);
  165. ge::NamesToPass names_to_pass;
  166. IdentityPass identity_pass(false);
  167. names_to_pass.emplace_back("IdentityPass", &identity_pass);
  168. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  169. auto identity1 = graph->FindNode("identity1");
  170. EXPECT_EQ(identity1, nullptr);
  171. auto switch1 = graph->FindNode("switch1");
  172. EXPECT_EQ(switch1->GetOutNodes().size(), 1);
  173. EXPECT_EQ(switch1->GetOutDataNodes().at(0)->GetName(), "addn1");
  174. }

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示