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 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/passes/graph_builder_utils.h"
  25. #include "../graph/manager/graph_manager.h"
  26. #include "all_ops.h"
  27. using namespace std;
  28. namespace ge {
  29. class UtestGeGenerator : public testing::Test {
  30. protected:
  31. void SetUp() {}
  32. void TearDown() {}
  33. };
  34. namespace {
  35. ComputeGraphPtr MakeGraph() {
  36. ge::ut::GraphBuilder builder("graph");
  37. auto data = builder.AddNode("data", "Data", 1, 1);
  38. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  39. builder.AddDataEdge(data, 0, addn1, 0);
  40. return builder.GetGraph();
  41. }
  42. } // namespace
  43. /*
  44. TEST_F(UtestGeGenerator, test_build_single_op_offline) {
  45. GeTensorDesc tensor_desc(GeShape(), FORMAT_NCHW, DT_FLOAT);
  46. TensorUtils::SetSize(tensor_desc, 512);
  47. shared_ptr<OpDesc> op_desc = make_shared<OpDesc>("Add", "add");
  48. EXPECT_EQ(op_desc->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  49. EXPECT_EQ(op_desc->AddInputDesc(tensor_desc), GRAPH_SUCCESS);
  50. EXPECT_EQ(op_desc->AddOutputDesc(tensor_desc), GRAPH_SUCCESS);
  51. GeTensor tensor(tensor_desc);
  52. const vector<GeTensor> inputs = { tensor, tensor };
  53. const vector<GeTensor> outputs = { tensor };
  54. // not Initialize, impl is null.
  55. GeGenerator generator;
  56. EXPECT_EQ(generator.BuildSingleOpModel(op_desc, inputs, outputs, "offline_"), PARAM_INVALID);
  57. // const map<string, string> &options
  58. generator.Initialize({});
  59. EXPECT_EQ(generator.BuildSingleOpModel(op_desc, inputs, outputs, "offline_"), GE_GENERATOR_GRAPH_MANAGER_BUILD_GRAPH_FAILED);
  60. }
  61. */
  62. TEST_F(UtestGeGenerator, test_build_single_op_online) {
  63. GeTensorDesc tensor_desc;
  64. shared_ptr<OpDesc> op_desc = make_shared<OpDesc>("Add", "add");
  65. op_desc->AddInputDesc(tensor_desc);
  66. op_desc->AddInputDesc(tensor_desc);
  67. op_desc->AddOutputDesc(tensor_desc);
  68. GeTensor tensor(tensor_desc);
  69. const vector<GeTensor> inputs = { tensor, tensor };
  70. const vector<GeTensor> outputs = { tensor };
  71. GeGenerator generator;
  72. generator.Initialize({});
  73. ModelBufferData model_buffer;
  74. EXPECT_EQ(generator.BuildSingleOpModel(op_desc, inputs, outputs, ENGINE_AIVECTOR, model_buffer), FAILED);
  75. }
  76. TEST_F(UtestGeGenerator, test_graph_manager) {
  77. GraphManager graph_manager;
  78. GraphPartitioner graph_partitioner;
  79. auto root_graph = MakeGraph();
  80. auto sub_graph = MakeGraph();
  81. root_graph->AddSubGraph(sub_graph);
  82. auto sgi = MakeShared<SubGraphInfo>();
  83. // set engine name
  84. sgi->SetEngineName("AIcoreEngine");
  85. sgi->SetSubGraph(sub_graph);
  86. auto sgi_gelocal = MakeShared<SubGraphInfo>();
  87. // set engine name
  88. sgi_gelocal->SetEngineName("GELOCAL");
  89. sgi_gelocal->SetSubGraph(sub_graph);
  90. graph_partitioner.graph_2_input_subgraph_[root_graph] = sgi_gelocal;
  91. graph_partitioner.graph_2_subgraph_list_.insert({root_graph, {sgi, sgi_gelocal}});
  92. graph_partitioner.graph_2_subgraph_list_.insert({sub_graph, {sgi, sgi_gelocal}});
  93. EXPECT_EQ(graph_manager.ConvertGraphToFile(root_graph, graph_partitioner, "./"), GRAPH_SUCCESS);
  94. }
  95. TEST_F(UtestGeGenerator, test_set_model_name) {
  96. GeGenerator generator;
  97. generator.Initialize({});
  98. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(GeRootModel());
  99. ComputeGraphPtr graph = make_shared<ComputeGraph>(ComputeGraph("graph"));
  100. (void)AttrUtils::SetBool(graph, "_dynamic_shape_partitioned", true);
  101. ge_root_model->root_graph_ = std::move(graph);
  102. EXPECT_EQ(generator.SetModelNameForDump(ge_root_model), SUCCESS);
  103. }
  104. } // namespace ge

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