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.

ge_generator_unittest.cc 6.9 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 private public
  18. #define protected public
  19. #include "generator/ge_generator.h"
  20. #include "graph/utils/tensor_utils.h"
  21. #include "graph/attr_value.h"
  22. #include "graph/debug/ge_attr_define.h"
  23. #include "graph/utils/graph_utils.h"
  24. #include "graph/operator_factory_impl.h"
  25. #include "../graph/passes/graph_builder_utils.h"
  26. #include "../graph/manager/graph_manager.h"
  27. #include "all_ops.h"
  28. using namespace std;
  29. namespace ge {
  30. class UtestGeGenerator : public testing::Test {
  31. protected:
  32. void SetUp() {}
  33. void TearDown() {}
  34. };
  35. namespace {
  36. ComputeGraphPtr MakeGraph() {
  37. ge::ut::GraphBuilder builder("graph");
  38. auto data = builder.AddNode("data", "Data", 1, 1);
  39. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  40. builder.AddDataEdge(data, 0, addn1, 0);
  41. return builder.GetGraph();
  42. }
  43. static GeAttrValue::NamedAttrs CreateNamedAttrs(const string &name, std::map<string, GeAttrValue> map) {
  44. GeAttrValue::NamedAttrs named_attrs;
  45. named_attrs.SetName(name);
  46. for (auto it : map) {
  47. named_attrs.SetAttr(it.first, it.second);
  48. }
  49. return named_attrs;
  50. }
  51. } // namespace
  52. /*
  53. TEST_F(UtestGeGenerator, test_build_single_op_offline) {
  54. GeTensorDesc tensor_desc(GeShape(), FORMAT_NCHW, DT_FLOAT);
  55. TensorUtils::SetSize(tensor_desc, 512);
  56. shared_ptr<OpDesc> op_desc = make_shared<OpDesc>("Add", "add");
  57. EXPECT_EQ(op_desc->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  58. EXPECT_EQ(op_desc->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  59. EXPECT_EQ(op_desc->AddOutputDesc(tensor_desc), GRAPH_SUCCESS);
  60. GeTensor tensor(tensor_desc);
  61. const vector<GeTensor> inputs = { tensor, tensor };
  62. const vector<GeTensor> outputs = { tensor };
  63. // not Initialize, impl is null.
  64. GeGenerator generator;
  65. EXPECT_EQ(generator.BuildSingleOpModel(op_desc, inputs, outputs, "offline_"), PARAM_INVALID);
  66. // const map<string, string> &options
  67. generator.Initialize({});
  68. EXPECT_EQ(generator.BuildSingleOpModel(op_desc, inputs, outputs, "offline_"), GE_GENERATOR_GRAPH_MANAGER_BUILD_GRAPH_FAILED);
  69. }
  70. */
  71. graphStatus TestFunc(Operator &op) { return 0; }
  72. graphStatus TestFunc1(Operator &op) { return 1; }
  73. TEST_F(UtestGeGenerator, test_infer_format_for_single_op) {
  74. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("graph_name");
  75. auto graph = GraphUtils::CreateGraphFromComputeGraph(compute_graph);
  76. OperatorFactoryImpl::RegisterInferFormatFunc("Add", TestFunc);
  77. shared_ptr<OpDesc> op_desc = make_shared<OpDesc>("add", "add");
  78. compute_graph->AddNode(op_desc);
  79. GeGenerator generator;
  80. EXPECT_EQ(generator.InferFormatForSingleOp(op_desc, graph), SUCCESS);
  81. shared_ptr<OpDesc> op_desc1 = make_shared<OpDesc>("Add", "Add");
  82. compute_graph->AddNode(op_desc1);
  83. EXPECT_EQ(generator.InferFormatForSingleOp(op_desc1, graph), SUCCESS);
  84. OperatorFactoryImpl::RegisterInferFormatFunc("MatMulV2", TestFunc1);
  85. shared_ptr<OpDesc> op_desc2 = make_shared<OpDesc>("MatMulV2", "MatMulV2");
  86. GeTensorDesc tensor_desc;
  87. EXPECT_EQ(op_desc2->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  88. EXPECT_EQ(op_desc2->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  89. EXPECT_EQ(op_desc2->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  90. EXPECT_EQ(op_desc2->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  91. EXPECT_EQ(op_desc2->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  92. EXPECT_EQ(op_desc2->AddOutputDesc(tensor_desc), GRAPH_SUCCESS);
  93. EXPECT_EQ(op_desc2->AddOutputDesc(tensor_desc), GRAPH_SUCCESS);
  94. compute_graph->AddNode(op_desc2);
  95. EXPECT_EQ(generator.InferFormatForSingleOp(op_desc2, graph), FAILED);
  96. }
  97. TEST_F(UtestGeGenerator, test_build_single_op_online) {
  98. GeTensorDesc tensor_desc;
  99. shared_ptr<OpDesc> op_desc = make_shared<OpDesc>("Add", "add");
  100. op_desc->AddInputDesc(tensor_desc);
  101. op_desc->AddInputDesc(tensor_desc);
  102. op_desc->AddOutputDesc(tensor_desc);
  103. GeTensor tensor(tensor_desc);
  104. const vector<GeTensor> inputs = { tensor, tensor };
  105. const vector<GeTensor> outputs = { tensor };
  106. GeGenerator generator;
  107. generator.Initialize({});
  108. ModelBufferData model_buffer;
  109. EXPECT_EQ(generator.BuildSingleOpModel(op_desc, inputs, outputs, ENGINE_AIVECTOR, false, model_buffer), FAILED);
  110. }
  111. TEST_F(UtestGeGenerator, test_check_aicore) {
  112. GeGenerator generator;
  113. generator.Initialize({});
  114. auto graph = MakeGraph();
  115. EXPECT_EQ(generator.CheckNoAicore(graph), true);
  116. }
  117. TEST_F(UtestGeGenerator, test_graph_manager) {
  118. GraphManager graph_manager;
  119. GraphPartitioner graph_partitioner;
  120. auto root_graph = MakeGraph();
  121. auto sub_graph = MakeGraph();
  122. root_graph->AddSubGraph(sub_graph);
  123. auto sgi = MakeShared<SubGraphInfo>();
  124. // set engine name
  125. sgi->SetEngineName("AIcoreEngine");
  126. sgi->SetSubGraph(sub_graph);
  127. auto sgi_gelocal = MakeShared<SubGraphInfo>();
  128. // set engine name
  129. sgi_gelocal->SetEngineName("GELOCAL");
  130. sgi_gelocal->SetSubGraph(sub_graph);
  131. graph_partitioner.graph_2_input_subgraph_[root_graph] = sgi_gelocal;
  132. graph_partitioner.graph_2_subgraph_list_.insert({root_graph, {sgi, sgi_gelocal}});
  133. graph_partitioner.graph_2_subgraph_list_.insert({sub_graph, {sgi, sgi_gelocal}});
  134. EXPECT_EQ(graph_manager.ConvertGraphToFile(root_graph, graph_partitioner, "./"), GRAPH_SUCCESS);
  135. }
  136. TEST_F(UtestGeGenerator, test_set_model_name) {
  137. GeGenerator generator;
  138. generator.Initialize({});
  139. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(GeRootModel());
  140. ComputeGraphPtr graph = make_shared<ComputeGraph>(ComputeGraph("graph"));
  141. (void)AttrUtils::SetBool(graph, "_dynamic_shape_partitioned", true);
  142. ge_root_model->root_graph_ = std::move(graph);
  143. EXPECT_EQ(generator.SetModelNameForDump(ge_root_model), SUCCESS);
  144. }
  145. TEST_F(UtestGeGenerator, test_remove_const) {
  146. GeGenerator generator;
  147. GeTensorDesc tensor_desc;
  148. GeTensor tensor(tensor_desc);
  149. const vector<GeTensor> inputs = {tensor};
  150. vector<GeTensor> outputs;
  151. generator.RemoveConst(inputs, outputs);
  152. }
  153. TEST_F(UtestGeGenerator, test_generate_online_model) {
  154. GeTensorDesc tensor_desc;
  155. GeTensor tensor(tensor_desc);
  156. const vector<GeTensor> inputs = { tensor, tensor };
  157. auto compute_graph = MakeGraph();
  158. compute_graph->TopologicalSorting();
  159. Graph graph = ge::GraphUtils::CreateGraphFromComputeGraph(compute_graph);
  160. GeGenerator generator;
  161. generator.Initialize({});
  162. std::string name;
  163. EXPECT_NE(generator.GenerateOfflineModel(graph, name, inputs), SUCCESS);
  164. }
  165. } // namespace ge

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