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

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "init/gelib.h"
  30. #include "ge/opskernel_manager/ops_kernel_builder_manager.h"
  31. #include "graph/build/task_generator.h"
  32. #include "graph/ge_local_context.h"
  33. #include "graph/manager/graph_mem_manager.h"
  34. #include "graph/manager/graph_var_manager.h"
  35. #undef protected
  36. #undef private
  37. using namespace std;
  38. using namespace testing;
  39. using namespace ge;
  40. namespace {
  41. const char *const kIsInputVar = "INPUT_IS_VAR";
  42. const char *const kIsOutputVar = "OUTPUT_IS_VAR";
  43. const char *const kKernelInfoNameHccl = "ops_kernel_info_hccl";
  44. } // namespace
  45. class UtestTaskGeneratorTest : public testing::Test {
  46. public:
  47. struct FakeOpsKernelBuilder : OpsKernelBuilder {
  48. FakeOpsKernelBuilder(){};
  49. private:
  50. Status Initialize(const map<std::string, std::string> &options) override {
  51. return SUCCESS;
  52. };
  53. Status Finalize() override {
  54. return SUCCESS;
  55. };
  56. Status CalcOpRunningParam(Node &node) override {
  57. return SUCCESS;
  58. };
  59. Status GenerateTask(const Node &node, RunContext &context, std::vector<domi::TaskDef> &tasks) override {
  60. domi::TaskDef task_def;
  61. tasks.push_back(task_def);
  62. return SUCCESS;
  63. };
  64. };
  65. struct FakeOpsKernelInfoStore : OpsKernelInfoStore {
  66. FakeOpsKernelInfoStore() = default;
  67. private:
  68. Status Initialize(const std::map<std::string, std::string> &options) override {
  69. return SUCCESS;
  70. };
  71. Status Finalize() override {
  72. return SUCCESS;
  73. };
  74. bool CheckSupported(const OpDescPtr &op_desc, std::string &reason) const override {
  75. return true;
  76. };
  77. void GetAllOpsKernelInfo(std::map<std::string, ge::OpInfo> &infos) const override{};
  78. };
  79. ge::ComputeGraphPtr BuildGraphFpProfiling() {
  80. ge::ut::GraphBuilder builder("graph");
  81. auto data = builder.AddNode("data", "phony", 1, 1);
  82. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  83. auto netoutput = builder.AddNode("netoutput", "NetOutput", 2, 0);
  84. auto op_desc = data->GetOpDesc();
  85. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, "IteratorV2");
  86. op_desc->SetOpKernelLibName("GE");
  87. builder.AddDataEdge(data, 0, addn1, 0);
  88. builder.AddDataEdge(addn1, 0, netoutput, 0);
  89. return builder.GetGraph();
  90. }
  91. ge::ComputeGraphPtr BuildGraphBpProfiling() {
  92. ge::ut::GraphBuilder builder("graph");
  93. auto data = builder.AddNode("data", "phony", 1, 1);
  94. auto addn1 = builder.AddNode("addn1", "AddN", 1, 1);
  95. auto netoutput = builder.AddNode("Node_Output", "NetOutput", 2, 0);
  96. auto data_desc = data->GetOpDesc();
  97. (void)AttrUtils::SetStr(data_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, "IteratorV2");
  98. data_desc->SetOpKernelLibName("GE");
  99. auto output_desc = netoutput->GetOpDesc();
  100. output_desc->SetOpKernelLibName("output");
  101. builder.AddDataEdge(data, 0, addn1, 0);
  102. builder.AddControlEdge(addn1, netoutput);
  103. return builder.GetGraph();
  104. }
  105. ge::ComputeGraphPtr BuildGraphWithVar(int64_t session_id) {
  106. // init
  107. MemManager::Instance().Initialize(std::vector<rtMemType_t>({RT_MEMORY_HBM}));
  108. VarManager::Instance(session_id)->Init(0, 0, 0, 0);
  109. ge::ut::GraphBuilder builder("graph");
  110. auto var_input = builder.AddNode("var", "Variable", 1, 1);
  111. auto const_input = builder.AddNode("const", "Const", 1, 1);
  112. auto assign = builder.AddNode("assgin", "Assign", 2, 1);
  113. // add link
  114. builder.AddDataEdge(var_input, 0, assign, 0);
  115. builder.AddDataEdge(const_input, 0, assign, 1);
  116. // set offset
  117. var_input->GetOpDesc()->SetOutputOffset({10000});
  118. const_input->GetOpDesc()->SetOutputOffset({1000});
  119. assign->GetOpDesc()->SetInputOffset({10100, 1000});
  120. assign->GetOpDesc()->SetOutputOffset({10100});
  121. // set inner offset
  122. int64_t inner_offset = 100;
  123. ge::AttrUtils::SetInt(assign->GetOpDesc()->MutableInputDesc(0), ATTR_NAME_INNER_OFFSET, inner_offset);
  124. ge::AttrUtils::SetInt(assign->GetOpDesc()->MutableOutputDesc(0), ATTR_NAME_INNER_OFFSET, inner_offset);
  125. // add var addr
  126. VarManager::Instance(session_id)->var_resource_->var_offset_map_.emplace(10000, RT_MEMORY_HBM);
  127. return builder.GetGraph();
  128. }
  129. ge::ComputeGraphPtr BuildHcclGraph() {
  130. ge::ut::GraphBuilder builder("graph");
  131. auto hccl_node = builder.AddNode("hccl_phony_node", "HCCL_PHONY", 0, 0);
  132. auto op_desc = hccl_node->GetOpDesc();
  133. op_desc->SetOpKernelLibName(kKernelInfoNameHccl);
  134. op_desc->SetStreamId(0);
  135. return builder.GetGraph();
  136. }
  137. protected:
  138. void SetUp() {}
  139. void TearDown() {}
  140. };
  141. TEST_F(UtestTaskGeneratorTest, AutoFindFpOpIndex) {
  142. auto graph = BuildGraphFpProfiling();
  143. TaskGenerator task_generator(nullptr, 0);
  144. ProfilingPoint profiling_point;
  145. profiling_point.fp_index = -1;
  146. EXPECT_EQ(task_generator.AutoFindFpOpIndex(graph, profiling_point), SUCCESS);
  147. // addn1 is fp
  148. EXPECT_EQ(profiling_point.fp_index, 2);
  149. }
  150. TEST_F(UtestTaskGeneratorTest, FindLastBpFromBpNode) {
  151. auto graph = BuildGraphBpProfiling();
  152. TaskGenerator task_generator(nullptr, 0);
  153. auto net_output = graph->FindNode("Node_Output");
  154. // netoutput has no data input, return default value 0
  155. uint32_t bp_index = 0;
  156. EXPECT_EQ(task_generator.FindLastBpFromBpNode(graph, net_output, bp_index), 0);
  157. EXPECT_EQ(bp_index, 2);
  158. }
  159. TEST_F(UtestTaskGeneratorTest, UpdateOpIsVarAttr) {
  160. int64_t session_id = 0;
  161. ge::ComputeGraphPtr graph = BuildGraphWithVar(session_id);
  162. graph->SetSessionID(session_id);
  163. TaskGenerator task_generator(nullptr, 0);
  164. auto assign = graph->FindNode("assgin");
  165. task_generator.UpdateOpIsVarAttr(assign->GetOpDesc(), session_id);
  166. // input
  167. vector<bool> input_var;
  168. AttrUtils::GetListBool(assign->GetOpDesc(), kIsInputVar, input_var);
  169. EXPECT_EQ(input_var.size(), 2);
  170. EXPECT_EQ(input_var[0], true);
  171. EXPECT_EQ(input_var[1], false);
  172. // output
  173. vector<bool> output_var;
  174. AttrUtils::GetListBool(assign->GetOpDesc(), kIsOutputVar, output_var);
  175. EXPECT_EQ(output_var.size(), 1);
  176. EXPECT_EQ(output_var[0], true);
  177. MemManager::Instance().Finalize();
  178. }
  179. TEST_F(UtestTaskGeneratorTest, AutoFindBpOpIndex) {
  180. auto graph = BuildGraphBpProfiling();
  181. TaskGenerator task_generator(nullptr, 0);
  182. auto net_output = graph->FindNode("Node_Output");
  183. ProfilingPoint profiling_point;
  184. vector<uint32_t> all_reduce_nodes;
  185. EXPECT_EQ(task_generator.AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes), SUCCESS);
  186. auto output_desc = net_output->GetOpDesc();
  187. output_desc->SetType("HcomAllReduce");
  188. output_desc->SetName("hcom");
  189. EXPECT_EQ(task_generator.AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes), SUCCESS);
  190. setenv("PROFILING_MODE", "true", true);
  191. EXPECT_EQ(task_generator.FindProfilingTaskIndex(graph, profiling_point, all_reduce_nodes), SUCCESS);
  192. EXPECT_EQ(profiling_point.end_index.size(), 1);
  193. EXPECT_EQ(*profiling_point.end_index.begin(), 3);
  194. }
  195. TEST_F(UtestTaskGeneratorTest, GenerateTask) {
  196. map<string, string> options;
  197. Status ret = ge::GELib::Initialize(options);
  198. EXPECT_EQ(ret, SUCCESS);
  199. shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  200. EXPECT_NE(instance_ptr, nullptr);
  201. OpsKernelInfoStorePtr ops_kernel_info_store_ptr = MakeShared<FakeOpsKernelInfoStore>();
  202. instance_ptr->opsManager_.ops_kernel_store_.insert(make_pair(kKernelInfoNameHccl, ops_kernel_info_store_ptr));
  203. OpsKernelBuilderManager &builder_manager_instance_ptr = ge::OpsKernelBuilderManager::Instance();
  204. OpsKernelBuilderPtr fake_builder = MakeShared<FakeOpsKernelBuilder>();
  205. builder_manager_instance_ptr.ops_kernel_builders_[kKernelInfoNameHccl] = fake_builder;
  206. auto graph = BuildHcclGraph();
  207. TaskGenerator task_generator(nullptr, 0);
  208. RunContext run_context;
  209. run_context.graphStreamList.push_back(static_cast<void *>(ops_kernel_info_store_ptr.get()));
  210. vector<uint32_t> all_reduce_nodes;
  211. vector<domi::TaskDef> task_def_list;
  212. map<uint32_t, string> op_name_map;
  213. EXPECT_EQ(task_generator.GenerateTask(run_context, graph, task_def_list, op_name_map), SUCCESS);
  214. EXPECT_EQ(task_def_list.size(), 1);
  215. EXPECT_EQ(task_def_list[0].ops_kernel_store_ptr(), reinterpret_cast<uintptr_t>(ops_kernel_info_store_ptr.get()));
  216. }
  217. TEST_F(UtestTaskGeneratorTest, SetFpBpByOptions) {
  218. map<std::string, string> options_map = {
  219. { OPTION_EXEC_PROFILING_OPTIONS,
  220. R"({"fp_point":"fp_node","bp_point":"bp_node"})"}};
  221. ge::GEThreadLocalContext &context = GetThreadLocalContext();
  222. context.SetGraphOption(options_map);
  223. auto graph = BuildGraphBpProfiling();
  224. TaskGenerator task_generator(nullptr, 0);
  225. ProfilingPoint profiling_point;
  226. vector<uint32_t> all_reduce_nodes;
  227. std::string fp_str;
  228. std::string bp_str;
  229. EXPECT_EQ(task_generator.GetFpBpIndex(graph, profiling_point, all_reduce_nodes, fp_str, bp_str), SUCCESS);
  230. EXPECT_EQ(fp_str, "fp_node");
  231. EXPECT_EQ(bp_str, "bp_node");
  232. }

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