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.

reshape_remove_pass_unittest.cc 6.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "graph/passes/reshape_remove_pass.h"
  17. #include <gtest/gtest.h>
  18. #include <set>
  19. #include <string>
  20. #include "graph_builder_utils.h"
  21. namespace ge {
  22. class UtestReshapeRemovePass : public testing::Test {
  23. protected:
  24. void SetUp() {}
  25. void TearDown() {}
  26. };
  27. namespace {
  28. /// netoutput1
  29. /// |
  30. /// transdata1
  31. /// |
  32. /// reshape1
  33. /// | \
  34. /// var1 const1
  35. ut::GraphBuilder Graph1Builder() {
  36. ut::GraphBuilder builder = ut::GraphBuilder("g1");
  37. auto var1 = builder.AddNode("var1", "Variable", 0, 1);
  38. ;
  39. auto const1 = builder.AddNode("const1", "Const", 0, 1);
  40. auto reshape1 = builder.AddNode("reshape1", "Reshape", 2, 1);
  41. auto transdata1 = builder.AddNode("transdata1", "Transdata", 1, 1);
  42. auto netoutput1 = builder.AddNode("netoutput1", "Netoutput", 1, 0);
  43. builder.AddDataEdge(var1, 0, reshape1, 0);
  44. builder.AddDataEdge(const1, 0, reshape1, 1);
  45. builder.AddDataEdge(reshape1, 0, transdata1, 0);
  46. builder.AddDataEdge(transdata1, 0, netoutput1, 0);
  47. return builder;
  48. }
  49. /// netoutput1
  50. /// | \
  51. ///transdata1 \
  52. /// | \
  53. /// reshape1 reshape2
  54. /// | \ / \
  55. /// var1 const1 var2
  56. ut::GraphBuilder Graph2Builder() {
  57. ut::GraphBuilder builder = ut::GraphBuilder("g2");
  58. auto var1 = builder.AddNode("var1", "Variable", 0, 1);
  59. auto const1 = builder.AddNode("const1", "Const", 0, 1);
  60. auto var2 = builder.AddNode("var2", "Variable", 0, 1);
  61. auto reshape1 = builder.AddNode("reshape1", "Reshape", 2, 1);
  62. auto reshape2 = builder.AddNode("reshape2", "Reshape", 2, 1);
  63. auto transdata1 = builder.AddNode("transdata1", "Transdata", 1, 1);
  64. auto netoutput1 = builder.AddNode("netoutput1", "Netoutput", 2, 0);
  65. builder.AddDataEdge(var1, 0, reshape1, 0);
  66. builder.AddDataEdge(const1, 0, reshape1, 1);
  67. builder.AddDataEdge(var2, 0, reshape2, 0);
  68. builder.AddDataEdge(const1, 0, reshape2, 1);
  69. builder.AddDataEdge(reshape1, 0, transdata1, 0);
  70. builder.AddDataEdge(reshape2, 0, netoutput1, 1);
  71. builder.AddDataEdge(transdata1, 0, netoutput1, 0);
  72. return builder;
  73. }
  74. /// netoutput1
  75. /// | \
  76. ///transdata1 \
  77. /// | \
  78. /// reshape1 transdata2
  79. /// | \ /
  80. /// var1 const1
  81. ut::GraphBuilder Graph3Builder() {
  82. ut::GraphBuilder builder = ut::GraphBuilder("g2");
  83. auto var1 = builder.AddNode("var1", "Variable", 0, 1);
  84. auto const1 = builder.AddNode("const1", "Const", 0, 1);
  85. auto reshape1 = builder.AddNode("reshape1", "Reshape", 2, 1);
  86. auto transdata2 = builder.AddNode("transdata2", "Transdata", 1, 1);
  87. auto transdata1 = builder.AddNode("transdata1", "Transdata", 1, 1);
  88. auto netoutput1 = builder.AddNode("netoutput1", "Netoutput", 2, 0);
  89. builder.AddDataEdge(var1, 0, reshape1, 0);
  90. builder.AddDataEdge(const1, 0, reshape1, 1);
  91. builder.AddDataEdge(const1, 0, transdata2, 0);
  92. builder.AddDataEdge(reshape1, 0, transdata1, 0);
  93. builder.AddDataEdge(transdata2, 0, netoutput1, 1);
  94. builder.AddDataEdge(transdata1, 0, netoutput1, 0);
  95. return builder;
  96. }
  97. } // namespace
  98. TEST_F(UtestReshapeRemovePass, reshape_remove_with_const) {
  99. auto builder = Graph1Builder();
  100. auto graph = builder.GetGraph();
  101. NamesToPass names_to_pass;
  102. names_to_pass.push_back({"Test", new ReshapeRemovePass});
  103. GEPass pass(graph);
  104. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  105. for (auto &name_to_pass : names_to_pass) {
  106. delete name_to_pass.second;
  107. }
  108. EXPECT_EQ(graph->FindNode("reshape1"), nullptr);
  109. auto const1 = graph->FindNode("const1");
  110. EXPECT_TRUE(const1->GetOutNodes().empty());
  111. EXPECT_TRUE(const1->GetInNodes().empty());
  112. auto var1 = graph->FindNode("var1");
  113. EXPECT_EQ(var1->GetOutNodes().size(), 1);
  114. EXPECT_EQ(var1->GetOutDataNodes().at(0)->GetName(), "transdata1");
  115. }
  116. TEST_F(UtestReshapeRemovePass, reshape_remove_without_const_two_reshape) {
  117. auto builder = Graph2Builder();
  118. auto graph = builder.GetGraph();
  119. NamesToPass names_to_pass;
  120. names_to_pass.push_back({"Test", new ReshapeRemovePass});
  121. GEPass pass(graph);
  122. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  123. for (auto &name_to_pass : names_to_pass) {
  124. delete name_to_pass.second;
  125. }
  126. EXPECT_EQ(graph->FindNode("reshape1"), nullptr);
  127. auto const1 = graph->FindNode("const1");
  128. EXPECT_TRUE(const1->GetOutNodes().empty());
  129. EXPECT_TRUE(const1->GetInNodes().empty());
  130. auto var1 = graph->FindNode("var1");
  131. EXPECT_EQ(var1->GetOutNodes().size(), 1);
  132. EXPECT_EQ(var1->GetOutDataNodes().at(0)->GetName(), "transdata1");
  133. auto netoutput1 = graph->FindNode("netoutput1");
  134. EXPECT_EQ(netoutput1->GetInNodes().size(), 2);
  135. std::set<std::string> names;
  136. for (auto node : netoutput1->GetInNodes()) {
  137. names.insert(node->GetName());
  138. }
  139. EXPECT_EQ(names, std::set<std::string>({"var2", "transdata1"}));
  140. }
  141. TEST_F(UtestReshapeRemovePass, reshape_remove_without_const) {
  142. auto builder = Graph3Builder();
  143. auto graph = builder.GetGraph();
  144. NamesToPass names_to_pass;
  145. names_to_pass.push_back({"Test", new ReshapeRemovePass});
  146. GEPass pass(graph);
  147. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  148. for (auto &name_to_pass : names_to_pass) {
  149. delete name_to_pass.second;
  150. }
  151. EXPECT_EQ(graph->FindNode("reshape1"), nullptr);
  152. auto const1 = graph->FindNode("const1");
  153. auto var1 = graph->FindNode("var1");
  154. EXPECT_EQ(var1->GetOutNodes().size(), 1);
  155. EXPECT_EQ(var1->GetOutDataNodes().at(0)->GetName(), "transdata1");
  156. EXPECT_NE(const1, nullptr);
  157. EXPECT_EQ(const1->GetOutNodes().size(), 1);
  158. }
  159. } // namespace ge

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