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.0 kB

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

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