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
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /*
  99. TEST_F(UtestReshapeRemovePass, reshape_remove_with_const) {
  100. auto builder = Graph1Builder();
  101. auto graph = builder.GetGraph();
  102. NamesToPass names_to_pass;
  103. names_to_pass.push_back({"Test", new ReshapeRemovePass});
  104. GEPass pass(graph);
  105. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  106. for (auto &name_to_pass : names_to_pass) {
  107. delete name_to_pass.second;
  108. }
  109. EXPECT_EQ(graph->FindNode("reshape1"), nullptr);
  110. auto const1 = graph->FindNode("const1");
  111. EXPECT_TRUE(const1->GetOutNodes().empty());
  112. EXPECT_TRUE(const1->GetInNodes().empty());
  113. auto var1 = graph->FindNode("var1");
  114. EXPECT_EQ(var1->GetOutNodes().size(), 1);
  115. EXPECT_EQ(var1->GetOutDataNodes().at(0)->GetName(), "transdata1");
  116. }
  117. TEST_F(UtestReshapeRemovePass, reshape_remove_without_const_two_reshape) {
  118. auto builder = Graph2Builder();
  119. auto graph = builder.GetGraph();
  120. NamesToPass names_to_pass;
  121. names_to_pass.push_back({"Test", new ReshapeRemovePass});
  122. GEPass pass(graph);
  123. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  124. for (auto &name_to_pass : names_to_pass) {
  125. delete name_to_pass.second;
  126. }
  127. EXPECT_EQ(graph->FindNode("reshape1"), nullptr);
  128. auto const1 = graph->FindNode("const1");
  129. EXPECT_TRUE(const1->GetOutNodes().empty());
  130. EXPECT_TRUE(const1->GetInNodes().empty());
  131. auto var1 = graph->FindNode("var1");
  132. EXPECT_EQ(var1->GetOutNodes().size(), 1);
  133. EXPECT_EQ(var1->GetOutDataNodes().at(0)->GetName(), "transdata1");
  134. auto netoutput1 = graph->FindNode("netoutput1");
  135. EXPECT_EQ(netoutput1->GetInNodes().size(), 2);
  136. std::set<std::string> names;
  137. for (auto node : netoutput1->GetInNodes()) {
  138. names.insert(node->GetName());
  139. }
  140. EXPECT_EQ(names, std::set<std::string>({"var2", "transdata1"}));
  141. }
  142. TEST_F(UtestReshapeRemovePass, reshape_remove_without_const) {
  143. auto builder = Graph3Builder();
  144. auto graph = builder.GetGraph();
  145. NamesToPass names_to_pass;
  146. names_to_pass.push_back({"Test", new ReshapeRemovePass});
  147. GEPass pass(graph);
  148. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  149. for (auto &name_to_pass : names_to_pass) {
  150. delete name_to_pass.second;
  151. }
  152. EXPECT_EQ(graph->FindNode("reshape1"), nullptr);
  153. auto const1 = graph->FindNode("const1");
  154. auto var1 = graph->FindNode("var1");
  155. EXPECT_EQ(var1->GetOutNodes().size(), 1);
  156. EXPECT_EQ(var1->GetOutDataNodes().at(0)->GetName(), "transdata1");
  157. EXPECT_NE(const1, nullptr);
  158. EXPECT_EQ(const1->GetOutNodes().size(), 1);
  159. }
  160. */
  161. } // namespace ge

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