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.

mem_assigner_unittest.cc 7.5 kB

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #define protected public
  27. #define private public
  28. #include "graph/build/memory/binary_block_mem_assigner.h"
  29. #include "graph/build/memory/hybrid_mem_assigner.h"
  30. #include "graph/build/memory/max_block_mem_assigner.h"
  31. #undef protected
  32. #undef private
  33. using namespace std;
  34. using namespace testing;
  35. using namespace ge;
  36. using domi::GetContext;
  37. class UtestMemoryAssignerTest : public testing::Test {
  38. public:
  39. ge::OpDescPtr createOpWithWsSize(const string &name, int64_t wsByte, const string &type = "some") {
  40. ge::OpDescPtr op_def = make_shared<ge::OpDesc>(name, type);
  41. auto desc_temp_ptr = make_shared<ge::GeTensorDesc>();
  42. auto desc_temp = *desc_temp_ptr;
  43. TensorUtils::SetSize(desc_temp, 1024);
  44. op_def->AddInputDesc(desc_temp);
  45. op_def->AddOutputDesc(desc_temp);
  46. std::vector<int64_t> workspace_bytes;
  47. workspace_bytes.push_back(wsByte);
  48. op_def->SetWorkspaceBytes(workspace_bytes);
  49. return op_def;
  50. }
  51. void make_graph(ge::ComputeGraphPtr graph) {
  52. ge::OpDescPtr op_def_a = createOpWithWsSize("A", 6000);
  53. op_def_a->SetStreamId(0);
  54. ge::OpDescPtr op_def_b = createOpWithWsSize("B", 120000);
  55. op_def_b->SetStreamId(0);
  56. ge::OpDescPtr op_def_c = createOpWithWsSize("C", 16000);
  57. op_def_c->SetStreamId(1);
  58. ge::OpDescPtr op_def_d = createOpWithWsSize("D", 24000);
  59. op_def_d->SetStreamId(2);
  60. ge::OpDescPtr op_def_e = createOpWithWsSize("E", 24000);
  61. op_def_e->SetStreamId(3);
  62. ge::OpDescPtr op_def_f = createOpWithWsSize("F", 30000);
  63. op_def_f->SetStreamId(2);
  64. ge::OpDescPtr op_def_g = createOpWithWsSize("G", 32000);
  65. op_def_g->SetStreamId(3);
  66. ge::OpDescPtr op_def_h = createOpWithWsSize("H", 48000);
  67. op_def_h->SetStreamId(2);
  68. ge::OpDescPtr op_def_i = createOpWithWsSize("I", 60000);
  69. op_def_i->SetStreamId(2);
  70. ge::OpDescPtr op_def_j = createOpWithWsSize("J", 256000, NETOUTPUT);
  71. op_def_j->SetStreamId(3);
  72. // add node
  73. ge::NodePtr node_a = graph->AddNode(op_def_a);
  74. ge::NodePtr node_b = graph->AddNode(op_def_b);
  75. ge::NodePtr node_c = graph->AddNode(op_def_c);
  76. ge::NodePtr node_d = graph->AddNode(op_def_d);
  77. ge::NodePtr node_e = graph->AddNode(op_def_e);
  78. ge::NodePtr node_f = graph->AddNode(op_def_f);
  79. ge::NodePtr node_g = graph->AddNode(op_def_g);
  80. ge::NodePtr node_h = graph->AddNode(op_def_h);
  81. ge::NodePtr node_i = graph->AddNode(op_def_i);
  82. ge::NodePtr node_j = graph->AddNode(op_def_j);
  83. // add edge
  84. ge::GraphUtils::AddEdge(node_a->GetOutDataAnchor(0), node_b->GetInDataAnchor(0));
  85. ge::GraphUtils::AddEdge(node_a->GetOutDataAnchor(0), node_c->GetInDataAnchor(0));
  86. ge::GraphUtils::AddEdge(node_b->GetOutDataAnchor(0), node_d->GetInDataAnchor(0));
  87. ge::GraphUtils::AddEdge(node_b->GetOutDataAnchor(0), node_e->GetInDataAnchor(0));
  88. ge::GraphUtils::AddEdge(node_c->GetOutDataAnchor(0), node_g->GetInDataAnchor(0));
  89. ge::GraphUtils::AddEdge(node_d->GetOutDataAnchor(0), node_f->GetInDataAnchor(0));
  90. ge::GraphUtils::AddEdge(node_e->GetOutDataAnchor(0), node_g->GetInDataAnchor(1));
  91. ge::GraphUtils::AddEdge(node_f->GetOutDataAnchor(0), node_h->GetInDataAnchor(0));
  92. ge::GraphUtils::AddEdge(node_g->GetOutDataAnchor(0), node_j->GetInDataAnchor(0));
  93. ge::GraphUtils::AddEdge(node_h->GetOutDataAnchor(0), node_i->GetInDataAnchor(0));
  94. ge::GraphUtils::AddEdge(node_i->GetOutDataAnchor(0), node_j->GetInDataAnchor(1));
  95. GetContext().out_nodes_map["H"] = {0};
  96. GetContext().out_nodes_map["I"] = {0};
  97. GetContext().out_nodes_map["J"] = {0};
  98. graph->TopologicalSorting();
  99. }
  100. void make_reuse_graph(ge::ComputeGraphPtr graph) {
  101. ge::OpDescPtr op_def_a = createOpWithWsSize("A", 6000);
  102. ge::OpDescPtr op_def_b = createOpWithWsSize("B", 120000);
  103. ge::OpDescPtr op_def_c = make_shared<ge::OpDesc>("C", "Some");
  104. auto desc_input_ptr = make_shared<ge::GeTensorDesc>();
  105. auto desc_input = *desc_input_ptr;
  106. TensorUtils::SetSize(desc_input, 1024);
  107. op_def_c->AddInputDesc(desc_input);
  108. auto desc_output_ptr = make_shared<ge::GeTensorDesc>();
  109. auto desc_output = *desc_output_ptr;
  110. TensorUtils::SetSize(desc_output, 6500);
  111. ge::TensorUtils::SetReuseInput(desc_output, true);
  112. ge::TensorUtils::SetReuseInputIndex(desc_output, 0);
  113. op_def_c->AddOutputDesc(desc_output);
  114. ge::OpDescPtr op_def_d = make_shared<ge::OpDesc>("D", "CONSTANT");
  115. ge::NodePtr node_a = graph->AddNode(op_def_a);
  116. ge::NodePtr node_b = graph->AddNode(op_def_b);
  117. ge::NodePtr node_c = graph->AddNode(op_def_c);
  118. ge::NodePtr node_d = graph->AddNode(op_def_d);
  119. ge::GraphUtils::AddEdge(node_a->GetOutDataAnchor(0), node_b->GetInDataAnchor(0));
  120. ge::GraphUtils::AddEdge(node_a->GetOutDataAnchor(0), node_c->GetInDataAnchor(0));
  121. ge::GraphUtils::AddEdge(node_a->GetOutDataAnchor(0), node_d->GetInDataAnchor(0));
  122. GetContext().out_nodes_map["B"] = {0};
  123. GetContext().out_nodes_map["C"] = {0};
  124. graph->TopologicalSorting();
  125. }
  126. protected:
  127. void SetUp() {}
  128. void TearDown() { GetContext().out_nodes_map.clear(); }
  129. };
  130. TEST_F(UtestMemoryAssignerTest, MemoryBlock_Resize_RealSizeList_is_empty) {
  131. ge::ComputeGraphPtr graph = make_shared<ge::ComputeGraph>("");
  132. ge::OpDescPtr op_def_a = createOpWithWsSize("A", 6000);
  133. ge::NodePtr node_a = graph->AddNode(op_def_a);
  134. MemoryBlock* memory_block = new MemoryBlock(0);
  135. memory_block->Init(1, kOutput, node_a, 0, 1);
  136. memory_block->real_size_list_.clear();
  137. memory_block->Resize();
  138. EXPECT_EQ(memory_block->Size(), 0);
  139. delete memory_block;
  140. }
  141. namespace ge {
  142. class MockBlockMemAssigner : public BlockMemAssigner {
  143. public:
  144. explicit MockBlockMemAssigner(ge::ComputeGraphPtr compute_graph, const std::map<std::string, std::string> &anchor_to_symbol, const std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors) : BlockMemAssigner(compute_graph, anchor_to_symbol, symbol_to_anchors) {};
  145. virtual ~MockBlockMemAssigner(){};
  146. Status GetMemoryRanges(std::vector<int64_t> &ranges) override { return FAILED; }
  147. };
  148. } // namespace ge
  149. // when check GetMemoryRanges return fail, Assign return fail
  150. TEST_F(UtestMemoryAssignerTest, Mock_block_mem_assigner_failed) {
  151. ge::ComputeGraphPtr graph = make_shared<ge::ComputeGraph>("");
  152. make_graph(graph);
  153. std::map<std::string, std::string> anchor_to_symbol;
  154. std::map<std::string, std::list<NodeIndexIO>> symbol_to_anchors;
  155. EXPECT_EQ(GraphUtils::GetRefMapping(graph, symbol_to_anchors, anchor_to_symbol), GRAPH_SUCCESS);
  156. MockBlockMemAssigner mock_assigner(graph, anchor_to_symbol, symbol_to_anchors);
  157. EXPECT_EQ(mock_assigner.Assign(), FAILED);
  158. }

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