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.

graph_preprocess_unittest.cc 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include <memory>
  18. #include "common/ge_inner_error_codes.h"
  19. #include "common/types.h"
  20. #include "common/util.h"
  21. #include "graph/passes/graph_builder_utils.h"
  22. #include "graph/utils/attr_utils.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #define private public
  25. #define protected public
  26. #include "graph/preprocess/graph_preprocess.h"
  27. #include "ge/ge_api.h"
  28. #undef private
  29. #undef protected
  30. using namespace std;
  31. namespace ge {
  32. class UtestGraphPreproces : public testing::Test {
  33. protected:
  34. void SetUp() {
  35. }
  36. void TearDown() {
  37. }
  38. };
  39. ComputeGraphPtr BuildGraph1(){
  40. auto builder = ut::GraphBuilder("g1");
  41. auto data1 = builder.AddNode("data1",DATA,1,1);
  42. auto data_opdesc = data1->GetOpDesc();
  43. AttrUtils::SetInt(data_opdesc, ATTR_NAME_INDEX, 0);
  44. data1->UpdateOpDesc(data_opdesc);
  45. return builder.GetGraph();
  46. }
  47. ComputeGraphPtr BuildGraph2() {
  48. auto builder = ut::GraphBuilder("g2");
  49. auto data1 = builder.AddNode("data1", DATA, 1, 1, FORMAT_NCHW, DT_FLOAT, std::vector<int64_t>({22, -1}));
  50. ge::AttrUtils::SetStr(data1->GetOpDesc(), ATTR_ATC_USER_DEFINE_DATATYPE, "DT_INT8");
  51. auto data_opdesc = data1->GetOpDesc();
  52. AttrUtils::SetInt(data_opdesc, ATTR_NAME_INDEX, 0);
  53. data1->UpdateOpDesc(data_opdesc);
  54. return builder.GetGraph();
  55. }
  56. ComputeGraphPtr BuildGraph3() {
  57. auto builder = ut::GraphBuilder("g3");
  58. auto data1 = builder.AddNode("data1", DATA, 1, 1, FORMAT_NCHW, DT_FLOAT);
  59. ge::AttrUtils::SetStr(data1->GetOpDesc(), ATTR_ATC_USER_DEFINE_DATATYPE, "DT_INT8");
  60. auto data_opdesc = data1->GetOpDesc();
  61. AttrUtils::SetInt(data_opdesc, ATTR_NAME_INDEX, 0);
  62. data1->UpdateOpDesc(data_opdesc);
  63. return builder.GetGraph();
  64. }
  65. /*
  66. * MapIndex Data1 subgraph1 subgraph2
  67. * \ /
  68. * Case ===> Data2 Data3
  69. * |
  70. * Netoutput
  71. */
  72. ComputeGraphPtr BuildGraph4() {
  73. auto builder = ut::GraphBuilder("mbatch_Case");
  74. auto data1 = builder.AddNode("data1", DATA, 1, 1);
  75. auto data_desc = data1->GetOpDesc();
  76. AttrUtils::SetStr(data_desc, ATTR_ATC_USER_DEFINE_DATATYPE, "DT_FLOAT16");
  77. AttrUtils::SetStr(data_desc, "mbatch-switch-name", "case1");
  78. AttrUtils::SetInt(data_desc, ATTR_NAME_INDEX, 0);
  79. auto mapindex1 = builder.AddNode("mapindex1", "MapIndex", 0, 1);
  80. auto case1 = builder.AddNode("case1", CASE, 2, 1);
  81. auto netoutput1 = builder.AddNode("netoutput1", NETOUTPUT, 1, 0);
  82. builder.AddDataEdge(mapindex1, 0, case1, 0);
  83. builder.AddDataEdge(data1, 0, case1, 1);
  84. builder.AddDataEdge(case1, 0, netoutput1, 0);
  85. return builder.GetGraph();
  86. }
  87. ComputeGraphPtr BuildGraph4_Subgraph(string graph_name) {
  88. auto builder = ut::GraphBuilder(graph_name);
  89. auto data1 = builder.AddNode(graph_name + "_data1", DATA, 1, 1);
  90. auto data_desc = data1->GetOpDesc();
  91. AttrUtils::SetInt(data_desc, ATTR_NAME_PARENT_NODE_INDEX, 1);
  92. return builder.GetGraph();
  93. }
  94. TEST_F(UtestGraphPreproces, test_dynamic_input_shape_parse) {
  95. ge::GraphPrepare graph_prepare;
  96. graph_prepare.compute_graph_ = BuildGraph1();
  97. // prepare user_input & graph option
  98. ge::GeTensorDesc tensor1;
  99. tensor1.SetFormat(ge::FORMAT_NCHW);
  100. tensor1.SetShape(ge::GeShape({3, 12, 5, 5}));
  101. tensor1.SetDataType(ge::DT_FLOAT);
  102. GeTensor input1(tensor1);
  103. std::vector<GeTensor> user_input = {input1};
  104. std::map<string,string> graph_option = {{"ge.exec.dynamicGraphExecuteMode","dynamic_execute"},
  105. {"ge.exec.dataInputsShapeRange","[3,1~20,2~10,5]"}};
  106. auto ret = graph_prepare.UpdateInput(user_input, graph_option);
  107. EXPECT_EQ(ret, ge::SUCCESS);
  108. // check data node output shape_range and shape
  109. auto data_node = graph_prepare.compute_graph_->FindNode("data1");
  110. auto data_output_desc = data_node->GetOpDesc()->GetOutputDescPtr(0);
  111. vector<int64_t> expect_shape = {3,-1,-1,5};
  112. auto result_shape = data_output_desc->GetShape();
  113. EXPECT_EQ(result_shape.GetDimNum(), expect_shape.size());
  114. for(size_t i =0; i< expect_shape.size(); ++i){
  115. EXPECT_EQ(result_shape.GetDim(i), expect_shape.at(i));
  116. }
  117. }
  118. TEST_F(UtestGraphPreproces, test_check_user_input) {
  119. ge::GraphPrepare graph_prepare;
  120. graph_prepare.compute_graph_ = BuildGraph1();
  121. vector<int64_t> dim = {2, -3};
  122. GeTensor tensor;
  123. tensor.SetTensorDesc(GeTensorDesc(GeShape(dim)));
  124. std::vector<GeTensor> user_input;
  125. user_input.emplace_back(tensor);
  126. Status ret = graph_prepare.CheckUserInput(user_input);
  127. EXPECT_EQ(ret, GE_GRAPH_INIT_FAILED);
  128. }
  129. TEST_F(UtestGraphPreproces, test_update_input_output1) {
  130. ge::GraphPrepare graph_prepare;
  131. graph_prepare.compute_graph_ = BuildGraph3();
  132. Status ret = graph_prepare.UpdateInputOutputByOptions();
  133. EXPECT_EQ(ret, SUCCESS);
  134. }
  135. TEST_F(UtestGraphPreproces, test_update_dtype_mbatch_case) {
  136. ge::GraphPrepare graph_prepare;
  137. graph_prepare.compute_graph_ = BuildGraph4();
  138. auto parent_graph = graph_prepare.compute_graph_;
  139. auto subgraph1 = BuildGraph4_Subgraph("subgraph1");
  140. auto subgraph2 = BuildGraph4_Subgraph("subgraph2");
  141. auto data1 = parent_graph->FindNode("data1");
  142. auto data_desc = data1->GetOpDesc();
  143. auto case_node = parent_graph->FindNode("case1");
  144. EXPECT_NE(case_node, nullptr);
  145. case_node->GetOpDesc()->AddSubgraphName("subgraph1");
  146. case_node->GetOpDesc()->SetSubgraphInstanceName(0, "subgraph1");
  147. subgraph1->SetParentNode(case_node);
  148. subgraph1->SetParentGraph(parent_graph);
  149. EXPECT_EQ(parent_graph->AddSubgraph("subgraph1", subgraph1), GRAPH_SUCCESS);
  150. case_node->GetOpDesc()->AddSubgraphName("subgraph2");
  151. case_node->GetOpDesc()->SetSubgraphInstanceName(1, "subgraph2");
  152. subgraph2->SetParentNode(case_node);
  153. subgraph2->SetParentGraph(parent_graph);
  154. EXPECT_EQ(parent_graph->AddSubgraph("subgraph2", subgraph2), GRAPH_SUCCESS);
  155. Status ret = graph_prepare.UpdateInputOutputByOptions();
  156. EXPECT_EQ(ret, SUCCESS);
  157. auto case_desc = case_node->GetOpDesc();
  158. auto case_input = case_desc->MutableInputDesc(1);
  159. EXPECT_EQ(case_input->GetDataType(), 1);
  160. auto sub1_data1 = subgraph1->FindNode("subgraph1_data1");
  161. EXPECT_NE(sub1_data1, nullptr);
  162. auto data1_desc = sub1_data1->GetOpDesc();
  163. auto data1_input = data1_desc->MutableInputDesc(0);
  164. EXPECT_EQ(data1_input->GetDataType(), 1);
  165. auto data1_output = data1_desc->MutableOutputDesc(0);
  166. EXPECT_EQ(data1_output->GetDataType(), 1);
  167. }
  168. }

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