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.

task_generator_unittest.cc 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/task_generator.h"
  30. #include "graph/manager/graph_mem_manager.h"
  31. #include "graph/manager/graph_var_manager.h"
  32. #undef protected
  33. #undef private
  34. using namespace std;
  35. using namespace testing;
  36. using namespace ge;
  37. namespace {
  38. const char *const kIsInputVar = "INPUT_IS_VAR";
  39. const char *const kIsOutputVar = "OUTPUT_IS_VAR";
  40. }
  41. class UtestTaskGeneratorTest : public testing::Test {
  42. public:
  43. ge::ComputeGraphPtr BuildGraphFpProfiling() {
  44. ge::ut::GraphBuilder builder("graph");
  45. auto data = builder.AddNode("data", "phony", 1, 1);
  46. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  47. auto netoutput = builder.AddNode("netoutput", "NetOutput", 2, 0);
  48. auto op_desc = data->GetOpDesc();
  49. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, "IteratorV2");
  50. op_desc->SetOpKernelLibName("GE");
  51. builder.AddDataEdge(data, 0, addn1, 0);
  52. builder.AddDataEdge(addn1, 0, netoutput, 0);
  53. return builder.GetGraph();
  54. }
  55. ge::ComputeGraphPtr BuildGraphBpProfiling() {
  56. ge::ut::GraphBuilder builder("graph");
  57. auto data = builder.AddNode("data", "phony", 1, 1);
  58. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  59. auto netoutput = builder.AddNode("Node_Output", "NetOutput", 2, 0);
  60. auto data_desc = data->GetOpDesc();
  61. (void)AttrUtils::SetStr(data_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, "IteratorV2");
  62. data_desc->SetOpKernelLibName("GE");
  63. auto output_desc = netoutput->GetOpDesc();
  64. output_desc->SetOpKernelLibName("output");
  65. builder.AddDataEdge(data, 0, addn1, 0);
  66. builder.AddControlEdge(addn1, netoutput);
  67. return builder.GetGraph();
  68. }
  69. ge::ComputeGraphPtr BuildGraphWithVar(int64_t session_id) {
  70. // init
  71. MemManager::Instance().Initialize(std::vector<rtMemType_t>({RT_MEMORY_HBM}));
  72. VarManager::Instance(session_id)->Init(0, 0, 0, 0);
  73. ge::ut::GraphBuilder builder("graph");
  74. auto var_input = builder.AddNode("var", "Variable", 1, 1);
  75. auto const_input = builder.AddNode("const", "Const", 1, 1);
  76. auto assign = builder.AddNode("assgin", "Assign", 2, 1);
  77. // add link
  78. builder.AddDataEdge(var_input, 0, assign, 0);
  79. builder.AddDataEdge(const_input, 0, assign, 1);
  80. // set offset
  81. var_input->GetOpDesc()->SetOutputOffset({10000});
  82. const_input->GetOpDesc()->SetOutputOffset({1000});
  83. assign->GetOpDesc()->SetInputOffset({10100, 1000});
  84. assign->GetOpDesc()->SetOutputOffset({10100});
  85. // set inner offset
  86. int64_t inner_offset = 100;
  87. ge::AttrUtils::SetInt(assign->GetOpDesc()->MutableInputDesc(0), ATTR_NAME_INNER_OFFSET, inner_offset);
  88. ge::AttrUtils::SetInt(assign->GetOpDesc()->MutableOutputDesc(0), ATTR_NAME_INNER_OFFSET, inner_offset);
  89. // add var addr
  90. VarManager::Instance(session_id)->var_resource_->var_offset_map_.emplace(10000, RT_MEMORY_HBM);
  91. return builder.GetGraph();
  92. }
  93. protected:
  94. void SetUp() {}
  95. void TearDown() {}
  96. };
  97. TEST_F(UtestTaskGeneratorTest, AutoFindFpOpIndex) {
  98. auto graph = BuildGraphFpProfiling();
  99. TaskGenerator task_generator(nullptr, 0);
  100. ProfilingPoint profiling_point;
  101. profiling_point.fp_index = -1;
  102. EXPECT_EQ(task_generator.AutoFindFpOpIndex(graph, profiling_point), SUCCESS);
  103. // addn1 is fp
  104. EXPECT_EQ(profiling_point.fp_index, 2);
  105. }
  106. TEST_F(UtestTaskGeneratorTest, FindLastBpFromBpNode) {
  107. auto graph = BuildGraphBpProfiling();
  108. TaskGenerator task_generator(nullptr, 0);
  109. auto net_output = graph->FindNode("Node_Output");
  110. // netoutput has no data input, return default value 0
  111. uint32_t bp_index = 0;
  112. EXPECT_EQ(task_generator.FindLastBpFromBpNode(graph, net_output, bp_index), 0);
  113. EXPECT_EQ(bp_index, 2);
  114. }
  115. TEST_F(UtestTaskGeneratorTest, UpdateOpIsVarAttr) {
  116. int64_t session_id = 0;
  117. ge::ComputeGraphPtr graph = BuildGraphWithVar(session_id);
  118. graph->SetSessionID(session_id);
  119. TaskGenerator task_generator(nullptr, 0);
  120. auto assign = graph->FindNode("assgin");
  121. task_generator.UpdateOpIsVarAttr(assign->GetOpDesc(), session_id);
  122. // input
  123. vector<bool> input_var;
  124. AttrUtils::GetListBool(assign->GetOpDesc(), kIsInputVar, input_var);
  125. EXPECT_EQ(input_var.size(), 2);
  126. EXPECT_EQ(input_var[0], true);
  127. EXPECT_EQ(input_var[1], false);
  128. // output
  129. vector<bool> output_var;
  130. AttrUtils::GetListBool(assign->GetOpDesc(), kIsOutputVar, output_var);
  131. EXPECT_EQ(output_var.size(), 1);
  132. EXPECT_EQ(output_var[0], true);
  133. MemManager::Instance().Finalize();
  134. }
  135. TEST_F(UtestTaskGeneratorTest, AutoFindBpOpIndex) {
  136. auto graph = BuildGraphBpProfiling();
  137. TaskGenerator task_generator(nullptr, 0);
  138. auto net_output = graph->FindNode("Node_Output");
  139. ProfilingPoint profiling_point;
  140. vector<uint32_t> all_reduce_nodes;
  141. EXPECT_EQ(task_generator.AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes), SUCCESS);
  142. auto output_desc = net_output->GetOpDesc();
  143. output_desc->SetType("HcomAllReduce");
  144. output_desc->SetName("hcom");
  145. EXPECT_EQ(task_generator.AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes), SUCCESS);
  146. }

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