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.

model_builder_unittest.cc 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "graph/anchor.h"
  19. #include "graph/attr_value.h"
  20. #include "graph/debug/ge_attr_define.h"
  21. #include "graph/utils/graph_utils.h"
  22. #include "graph/utils/node_utils.h"
  23. #include "graph/utils/op_desc_utils.h"
  24. #include "graph/utils/tensor_utils.h"
  25. #include "omg/omg_inner_types.h"
  26. #include "../passes/graph_builder_utils.h"
  27. #define protected public
  28. #define private public
  29. #include "graph/build/model_builder.h"
  30. #undef protected
  31. #undef private
  32. using namespace std;
  33. using namespace testing;
  34. using namespace ge;
  35. using domi::GetContext;
  36. class UtestModelBuilderTest : public testing::Test {
  37. public:
  38. ge::OpDescPtr CreateOpWithWsSize(const string &name, int64_t wsByte, const string &type = "some") {
  39. ge::OpDescPtr op_def = make_shared<ge::OpDesc>(name, type);
  40. auto desc_temp_ptr = make_shared<ge::GeTensorDesc>();
  41. auto desc_temp = *desc_temp_ptr;
  42. TensorUtils::SetSize(desc_temp, 1024);
  43. op_def->AddInputDesc(desc_temp);
  44. op_def->AddOutputDesc(desc_temp);
  45. std::vector<int64_t> workspace_bytes;
  46. workspace_bytes.push_back(wsByte);
  47. op_def->SetWorkspaceBytes(workspace_bytes);
  48. return op_def;
  49. }
  50. ge::OpDescPtr CreateRefOpWithWsSize(const string &name, int64_t wsByte, const string &type = "some") {
  51. ge::OpDescPtr op_def = make_shared<ge::OpDesc>(name, type);
  52. auto desc_temp_ptr = make_shared<ge::GeTensorDesc>();
  53. auto desc_temp = *desc_temp_ptr;
  54. TensorUtils::SetSize(desc_temp, 1024);
  55. op_def->AddInputDesc(desc_temp);
  56. auto desc_output_ptr = make_shared<ge::GeTensorDesc>();
  57. auto desc_output = *desc_output_ptr;
  58. TensorUtils::SetSize(desc_output, 6500);
  59. ge::TensorUtils::SetReuseInput(desc_output, true);
  60. ge::TensorUtils::SetReuseInputIndex(desc_output, 0);
  61. op_def->AddOutputDesc(desc_output);
  62. std::vector<int64_t> workspace_bytes;
  63. workspace_bytes.push_back(wsByte);
  64. op_def->SetWorkspaceBytes(workspace_bytes);
  65. return op_def;
  66. }
  67. void MakeGraph(ge::ComputeGraphPtr &graph) {
  68. ge::OpDescPtr op_def_a = CreateOpWithWsSize("A", 6000);
  69. op_def_a->SetStreamId(0);
  70. ge::OpDescPtr op_def_b = CreateOpWithWsSize("B", 120000);
  71. op_def_b->SetStreamId(0);
  72. ge::OpDescPtr op_def_c = CreateOpWithWsSize("C", 16000);
  73. op_def_c->SetStreamId(1);
  74. ge::OpDescPtr op_def_d = CreateOpWithWsSize("D", 24000);
  75. op_def_d->SetStreamId(2);
  76. ge::OpDescPtr op_def_e = CreateOpWithWsSize("E", 24000);
  77. op_def_e->SetStreamId(3);
  78. ge::OpDescPtr op_def_f = CreateOpWithWsSize("F", 30000);
  79. op_def_f->SetStreamId(2);
  80. ge::OpDescPtr op_def_g = CreateOpWithWsSize("G", 32000);
  81. op_def_g->SetStreamId(3);
  82. ge::OpDescPtr op_def_h = CreateOpWithWsSize("H", 48000);
  83. op_def_h->SetStreamId(2);
  84. ge::OpDescPtr op_def_i = CreateOpWithWsSize("I", 60000);
  85. op_def_i->SetStreamId(2);
  86. ge::OpDescPtr op_def_j = CreateOpWithWsSize("J", 256000, NETOUTPUT);
  87. op_def_j->SetStreamId(3);
  88. // add node
  89. ge::NodePtr node_a = graph->AddNode(op_def_a);
  90. ge::NodePtr node_b = graph->AddNode(op_def_b);
  91. ge::NodePtr node_c = graph->AddNode(op_def_c);
  92. ge::NodePtr node_d = graph->AddNode(op_def_d);
  93. ge::NodePtr node_e = graph->AddNode(op_def_e);
  94. ge::NodePtr node_f = graph->AddNode(op_def_f);
  95. ge::NodePtr node_g = graph->AddNode(op_def_g);
  96. ge::NodePtr node_h = graph->AddNode(op_def_h);
  97. ge::NodePtr node_i = graph->AddNode(op_def_i);
  98. ge::NodePtr node_j = graph->AddNode(op_def_j);
  99. // add edge
  100. ge::GraphUtils::AddEdge(node_a->GetOutDataAnchor(0), node_b->GetInDataAnchor(0));
  101. ge::GraphUtils::AddEdge(node_a->GetOutDataAnchor(0), node_c->GetInDataAnchor(0));
  102. ge::GraphUtils::AddEdge(node_b->GetOutDataAnchor(0), node_d->GetInDataAnchor(0));
  103. ge::GraphUtils::AddEdge(node_b->GetOutDataAnchor(0), node_e->GetInDataAnchor(0));
  104. ge::GraphUtils::AddEdge(node_c->GetOutDataAnchor(0), node_g->GetInDataAnchor(0));
  105. ge::GraphUtils::AddEdge(node_d->GetOutDataAnchor(0), node_f->GetInDataAnchor(0));
  106. ge::GraphUtils::AddEdge(node_e->GetOutDataAnchor(0), node_g->GetInDataAnchor(1));
  107. ge::GraphUtils::AddEdge(node_f->GetOutDataAnchor(0), node_h->GetInDataAnchor(0));
  108. ge::GraphUtils::AddEdge(node_g->GetOutDataAnchor(0), node_j->GetInDataAnchor(0));
  109. ge::GraphUtils::AddEdge(node_h->GetOutDataAnchor(0), node_i->GetInDataAnchor(0));
  110. ge::GraphUtils::AddEdge(node_i->GetOutDataAnchor(0), node_j->GetInDataAnchor(1));
  111. GetContext().out_nodes_map["H"] = {0};
  112. GetContext().out_nodes_map["I"] = {0};
  113. GetContext().out_nodes_map["J"] = {0};
  114. graph->TopologicalSorting();
  115. }
  116. protected:
  117. void SetUp() {}
  118. void TearDown() { GetContext().out_nodes_map.clear(); }
  119. };
  120. // when check GetMemoryRanges return fail, Assign return fail
  121. TEST_F(UtestModelBuilderTest, SetInputIsConst) {
  122. Graph2SubGraphInfoList subgraphs;
  123. std::map<std::string, int> stream_max_parallel_num;
  124. ge::ComputeGraphPtr graph = make_shared<ge::ComputeGraph>("");
  125. MakeGraph(graph);
  126. graph->TopologicalSorting();
  127. ge::ModelBuilder builder(0, graph, subgraphs, stream_max_parallel_num, false);
  128. EXPECT_EQ(builder.PreBuildModel(), SUCCESS);
  129. }

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