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.cc 60 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 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
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 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
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /**
  2. * Copyright 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 "graph/build/task_generator.h"
  17. #include <string>
  18. #include <utility>
  19. #include "common/profiling/profiling_manager.h"
  20. #include "framework/common/types.h"
  21. #include "framework/common/util.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #include "graph/ge_context.h"
  25. #include "graph/manager/graph_var_manager.h"
  26. #include "graph/model_serialize.h"
  27. #include "graph/utils/node_utils.h"
  28. #include "graph/utils/tensor_utils.h"
  29. #include "graph/utils/type_utils.h"
  30. #include "common/ge_call_wrapper.h"
  31. #include "init/gelib.h"
  32. #include "graph/ge_local_context.h"
  33. #include "external/ge/ge_api_types.h"
  34. #include "opskernel_manager/ops_kernel_builder_manager.h"
  35. using domi::LogTimeStampDef;
  36. using domi::ModelTaskDef;
  37. using domi::TaskDef;
  38. using std::map;
  39. using std::set;
  40. using std::string;
  41. using std::vector;
  42. namespace {
  43. const char *const kIsFirstNode = "is_first_node";
  44. const char *const kIsLastNode = "is_last_node";
  45. const char *const kIsInputVar = "INPUT_IS_VAR";
  46. const char *const kIsOutputVar = "OUTPUT_IS_VAR";
  47. const char *const kProfilingMode = "PROFILING_MODE";
  48. const char *const kIteratorV2 = "IteratorV2";
  49. const char *const kKernelInfoNameHccl = "ops_kernel_info_hccl";
  50. const uint32_t kProfilingArStep = 2;
  51. const uint64_t kProfilingFpStartLogid = 1;
  52. const uint64_t kProfilingBpEndLogid = 2;
  53. const uint64_t kProfilingArStartLogid = 3;
  54. const uint64_t kProfilingArEndLogid = 4;
  55. const uint64_t kProfilingIterEndLogid = 65535;
  56. const int64_t kHashFactor = 100000;
  57. const int64_t kInvalidGroupId = -1;
  58. const std::set<std::string> kFpNodeTypes = {ge::DATA, ge::GETNEXT, kIteratorV2};
  59. } // namespace
  60. namespace ge {
  61. TaskGenerator::TaskGenerator(uint8_t *var_mem_base, uint64_t var_mem_size) {
  62. var_mem_base_ = var_mem_base;
  63. var_mem_size_ = var_mem_size;
  64. }
  65. TaskGenerator::~TaskGenerator() {}
  66. Status TaskGenerator::GetTaskInfo(Model &model, ComputeGraphPtr &graph, uint64_t session_id, RunContext &run_context) {
  67. GELOGD("Begin to Get TaskInfo. session_id=%lu", session_id);
  68. // Check params
  69. if (graph == nullptr) {
  70. REPORT_INNER_ERROR("E19999", "Check param graph is null, session_id:%lu", session_id);
  71. GELOGE(PARAM_INVALID, "[Check][Param] GetTaskInfo param graph is null. session_id=%lu", session_id);
  72. return PARAM_INVALID;
  73. }
  74. std::vector<TaskDef> task_def_list;
  75. std::map<uint32_t, string> op_name_map;
  76. GE_DUMP(graph, "GenerateTaskBefore");
  77. Status ret = GenerateTask(run_context, graph, task_def_list, op_name_map);
  78. GE_DUMP(graph, "GenerateTaskAfter");
  79. if (ret != SUCCESS) {
  80. GELOGE(ret, "[Generate][Task] failed. session_id=%lu", session_id);
  81. return ret;
  82. }
  83. // op_name_map used when graph load
  84. graph->SetGraphOpName(op_name_map);
  85. // Set op_name for infer profiling
  86. vector<string> op_name;
  87. for (auto &iter : op_name_map) {
  88. op_name.push_back(iter.second);
  89. }
  90. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListStr(model, ATTR_MODEL_TASK_INDEX_OP_NAME, op_name),
  91. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for model:%s",
  92. ATTR_MODEL_TASK_INDEX_OP_NAME.c_str(), model.GetName().c_str());
  93. GELOGE(FAILED, "[Set][Attr] %s fail for model:%s",
  94. ATTR_MODEL_TASK_INDEX_OP_NAME.c_str(), model.GetName().c_str());
  95. return FAILED);
  96. GELOGI("GenerateTask Success, task list:%zu, op map:%zu, logic mem base:%p, logic weight base:%p, logic var base:%p",
  97. task_def_list.size(), op_name_map.size(), run_context.dataMemBase, run_context.weightMemBase, var_mem_base_);
  98. // Init and serialize model_task_def
  99. ModelTaskDef model_task_def;
  100. model_task_def.set_memory_size(run_context.dataMemSize);
  101. model_task_def.set_weight_size(run_context.weightMemSize);
  102. for (const TaskDef &task_def_temp : task_def_list) {
  103. TaskDef *task_def = model_task_def.add_task();
  104. if (task_def == nullptr) {
  105. REPORT_INNER_ERROR("E19999", "Add task_def in ModelTaskDef fail, session_id:%lu, graph:%s, model:%s",
  106. session_id, graph->GetName().c_str(), model.GetName().c_str());
  107. GELOGE(FAILED, "[Check][Param] task_def is nullptr, session_id:%lu, graph:%s, model:%s",
  108. session_id, graph->GetName().c_str(), model.GetName().c_str());
  109. return FAILED;
  110. }
  111. *task_def = task_def_temp;
  112. }
  113. ret = AddModelTaskToModel(model_task_def, session_id, model, run_context);
  114. if (ret != SUCCESS) {
  115. GELOGE(ret, "[Add][ModelTask] To Model failed. session_id=%lu", session_id);
  116. return ret;
  117. }
  118. GELOGD("Get TaskInfo success. session_id=%lu", session_id);
  119. return SUCCESS;
  120. }
  121. Status TaskGenerator::AddModelTaskToModel(const ModelTaskDef &model_task_def, uint64_t session_id, ge::Model &model,
  122. RunContext &run_context) {
  123. GE_CHK_BOOL_EXEC(
  124. AttrUtils::SetInt(model, MODEL_ATTR_TASK_GEN_BASE_ADDR, reinterpret_cast<uintptr_t>(run_context.dataMemBase)),
  125. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for model:%s",
  126. MODEL_ATTR_TASK_GEN_BASE_ADDR.c_str(), model.GetName().c_str());
  127. GELOGE(FAILED, "[Set][Attr] %s fail for model:%s",
  128. MODEL_ATTR_TASK_GEN_BASE_ADDR.c_str(), model.GetName().c_str());
  129. return FAILED);
  130. GE_CHK_BOOL_EXEC(
  131. AttrUtils::SetInt(model, MODEL_ATTR_TASK_GEN_WEIGHT_ADDR, reinterpret_cast<uintptr_t>(run_context.weightMemBase)),
  132. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for model:%s",
  133. MODEL_ATTR_TASK_GEN_WEIGHT_ADDR.c_str(), model.GetName().c_str());
  134. GELOGE(FAILED, "[Set][Attr] %s fail for model:%s",
  135. MODEL_ATTR_TASK_GEN_WEIGHT_ADDR.c_str(), model.GetName().c_str());
  136. return FAILED);
  137. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(model, ATTR_MODEL_TASK_GEN_VAR_ADDR, reinterpret_cast<uintptr_t>(var_mem_base_)),
  138. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for model:%s",
  139. ATTR_MODEL_TASK_GEN_VAR_ADDR.c_str(), model.GetName().c_str());
  140. GELOGE(FAILED, "[Set][Attr] %s fail for model:%s",
  141. ATTR_MODEL_TASK_GEN_VAR_ADDR.c_str(), model.GetName().c_str());
  142. return FAILED);
  143. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(model, ATTR_MODEL_VAR_SIZE, var_mem_size_),
  144. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for model:%s",
  145. ATTR_MODEL_VAR_SIZE.c_str(), model.GetName().c_str());
  146. GELOGE(FAILED, "[Set][Attr] %s fail for model:%s",
  147. ATTR_MODEL_VAR_SIZE.c_str(), model.GetName().c_str());
  148. return FAILED);
  149. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(model, MODEL_ATTR_SESSION_ID, session_id),
  150. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for mode:%s",
  151. MODEL_ATTR_SESSION_ID.c_str(), model.GetName().c_str());
  152. GELOGE(FAILED, "[Set][Attr] %s fail for mode:%s",
  153. MODEL_ATTR_SESSION_ID.c_str(), model.GetName().c_str());
  154. return FAILED);
  155. size_t task_size = model_task_def.ByteSizeLong();
  156. ge::Buffer serial_buff(task_size);
  157. if (!model_task_def.SerializePartialToArray(serial_buff.GetData(), static_cast<int>(task_size))) {
  158. REPORT_INNER_ERROR("E19999", "model_task_def's serialize failed, model name = %s, task_size=%zu",
  159. model.GetName().c_str(), task_size);
  160. GELOGE(FAILED, "[Call][SerializePartialToArray] failed, model name = %s, task_size=%zu.",
  161. model.GetName().c_str(), task_size);
  162. return FAILED;
  163. }
  164. if (!AttrUtils::SetZeroCopyBytes(model, MODEL_ATTR_TASKS, std::move(serial_buff))) {
  165. REPORT_INNER_ERROR("E19999", "Set model task to model failed, model name = %s, task_size=%zu",
  166. model.GetName().c_str(), task_size);
  167. GELOGE(FAILED, "[Call][SetZeroCopyBytes] Set model task to model failed, model name = %s, task_size=%zu.",
  168. model.GetName().c_str(), task_size);
  169. return FAILED;
  170. }
  171. return SUCCESS;
  172. }
  173. Status TaskGenerator::UpdateOpIsVarAttr(const OpDescPtr &op_desc, uint64_t session_id) {
  174. GELOGD("Update is var attr, node[name:%s(%s), id:%ld, stream_id:%ld].", op_desc->GetName().c_str(),
  175. op_desc->GetType().c_str(), op_desc->GetId(), op_desc->GetStreamId());
  176. // input
  177. vector<int64_t> input_offsets = op_desc->GetInputOffset();
  178. if (!(input_offsets.empty())) {
  179. vector<bool> input_var;
  180. size_t valid_input_index = 0;
  181. for (uint32_t i = 0; i < op_desc->GetAllInputsSize(); i++) {
  182. vector<int64_t> output_list;
  183. auto input_tensor_desc = op_desc->MutableInputDesc(i);
  184. if (input_tensor_desc == nullptr) {
  185. continue;
  186. }
  187. if (valid_input_index >= input_offsets.size()) {
  188. break;
  189. }
  190. int64_t inner_offset = 0;
  191. (void)ge::AttrUtils::GetInt(input_tensor_desc, ATTR_NAME_INNER_OFFSET, inner_offset);
  192. GELOGD("Node[%s] input[%u] has inner_offset[%ld]", op_desc->GetName().c_str(), i, inner_offset);
  193. input_var.push_back(VarManager::Instance(session_id)->IsVarAddr(input_offsets[valid_input_index] - inner_offset));
  194. valid_input_index++;
  195. }
  196. GE_CHK_BOOL_EXEC(AttrUtils::SetListBool(op_desc, kIsInputVar, input_var),
  197. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", kIsInputVar,
  198. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  199. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", kIsInputVar, op_desc->GetName().c_str(),
  200. op_desc->GetType().c_str());
  201. return FAILED);
  202. }
  203. // output
  204. vector<int64_t> output_offsets = op_desc->GetOutputOffset();
  205. if (!(output_offsets.empty())) {
  206. vector<bool> output_var;
  207. size_t valid_output_index = 0;
  208. for (uint32_t i = 0; i < op_desc->GetAllOutputsDescSize(); i++) {
  209. vector<int64_t> output_list;
  210. auto output_tensor_desc = op_desc->MutableOutputDesc(i);
  211. if (output_tensor_desc == nullptr) {
  212. continue;
  213. }
  214. if (valid_output_index >= output_offsets.size()) {
  215. break;
  216. }
  217. int64_t inner_offset = 0;
  218. (void)ge::AttrUtils::GetInt(output_tensor_desc, ATTR_NAME_INNER_OFFSET, inner_offset);
  219. GELOGD("Node[%s] output[%u] has inner_offset[%ld]", op_desc->GetName().c_str(), i, inner_offset);
  220. output_var.push_back(
  221. VarManager::Instance(session_id)->IsVarAddr(output_offsets[valid_output_index] - inner_offset));
  222. valid_output_index++;
  223. }
  224. GE_CHK_BOOL_EXEC(AttrUtils::SetListBool(op_desc, kIsOutputVar, output_var),
  225. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", kIsOutputVar,
  226. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  227. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", kIsOutputVar, op_desc->GetName().c_str(),
  228. op_desc->GetType().c_str());
  229. return FAILED);
  230. }
  231. return SUCCESS;
  232. }
  233. Status TaskGenerator::SaveFusionNodes(map<int64_t, std::vector<NodePtr>> &fusion_nodes, ComputeGraphPtr &graph) {
  234. std::map<NodePtr, int64_t> nodes_with_group_attr;
  235. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  236. OpDescPtr op_desc = node->GetOpDesc();
  237. GE_CHECK_NOTNULL(op_desc);
  238. int64_t group_id = kInvalidGroupId;
  239. string name = node->GetName();
  240. string type = node->GetType();
  241. // For fusion ddb pass, task def must be continuous.
  242. // Part1: store
  243. // If op_desc have this tag, store it in the map firstly,
  244. // call the elements in the map GenerateTask at last
  245. // l1 and l2 is for now
  246. if (ge::AttrUtils::GetInt(op_desc, ATTR_NAME_L1_FUSION_GROUP_ID, group_id) ||
  247. ge::AttrUtils::GetInt(op_desc, ATTR_NAME_L2_FUSION_GROUP_ID, group_id)) {
  248. auto stream_id = op_desc->GetStreamId();
  249. auto group_key = group_id + stream_id * kHashFactor;
  250. (void)ge::AttrUtils::SetInt(op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key);
  251. GELOGD("Fusion: store node[name:%s(%s), group id:%ld, group key:%ld, stream_id:%ld] task.", name.c_str(),
  252. type.c_str(), group_id, group_key, op_desc->GetStreamId());
  253. fusion_nodes[group_key].push_back(node);
  254. nodes_with_group_attr.insert({node, group_id});
  255. }
  256. // if node's all in nodes both with same group attr
  257. // and it have no attr or group attr different
  258. // which means bad case, return error
  259. bool call_check = true;
  260. std::set<int64_t> input_group_ids;
  261. for (const auto &input_node : node->GetInNodes()) {
  262. auto iter = nodes_with_group_attr.find(input_node);
  263. if (iter == nodes_with_group_attr.end()) {
  264. call_check = false;
  265. break;
  266. } else {
  267. input_group_ids.insert(iter->second);
  268. }
  269. }
  270. call_check = (call_check && (input_group_ids.size() == 1));
  271. if (call_check) {
  272. auto input_group_id = *input_group_ids.begin();
  273. if (group_id != input_group_id) {
  274. GELOGW("Fusion: node[name:%s(%s) with group id:%ld and diff from it's input nodes's group id:%ld ",
  275. name.c_str(), type.c_str(), group_id, input_group_id);
  276. }
  277. }
  278. }
  279. GELOGD("Fusion: get fusion group numbers [%zu].", fusion_nodes.size());
  280. return SUCCESS;
  281. }
  282. bool TaskGenerator::IsSubGraphOfDynamicGraph(const ComputeGraphPtr &graph) const {
  283. auto parent_graph_ptr = graph->GetParentGraph();
  284. if (parent_graph_ptr == nullptr) {
  285. return false;
  286. }
  287. auto root_graph_ptr = GraphUtils::FindRootGraph(parent_graph_ptr);
  288. if (root_graph_ptr == nullptr) {
  289. return false;
  290. }
  291. return root_graph_ptr->GetGraphUnknownFlag();
  292. }
  293. Status TaskGenerator::GenerateTask(RunContext &run_context, ComputeGraphPtr &graph,
  294. vector<domi::TaskDef> &task_def_list, map<uint32_t, string> &op_name_map) {
  295. GELOGD("Beign to generate task, graph name is %s.", graph->GetName().c_str());
  296. std::shared_ptr<GELib> ge_lib = GELib::GetInstance();
  297. if ((ge_lib == nullptr) || !ge_lib->InitFlag()) {
  298. REPORT_INNER_ERROR("E19999", "Check GELib instance not init before");
  299. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Check][Param] GenerateTask failed, because GELib instance not init before.");
  300. return GE_CLI_GE_NOT_INITIALIZED;
  301. }
  302. GE_CHK_STATUS_RET(MarkNodeAndSetIndex(graph),
  303. "[Call][MarkNodeAndSetIndex] failed, graph:%s.", graph->GetName().c_str());
  304. ProfilingPoint profiling_point;
  305. vector<uint32_t> all_reduce_nodes;
  306. GE_CHK_STATUS_RET(FindProfilingTaskIndex(graph, profiling_point, all_reduce_nodes));
  307. const OpsKernelManager &ops_kernel_manager = ge_lib->OpsKernelManagerObj();
  308. GE_TIMESTAMP_CALLNUM_START(GenerateTask);
  309. // map store fusion nodes
  310. map<int64_t, std::vector<NodePtr>> fusion_nodes;
  311. string buffer_optimize = "off_optimize";
  312. (void)ge::GetContext().GetOption(BUFFER_OPTIMIZE, buffer_optimize);
  313. if (buffer_optimize != "off_optimize") {
  314. GE_CHK_STATUS_RET(SaveFusionNodes(fusion_nodes, graph));
  315. }
  316. std::unordered_set<Node *> fusion_nodes_seen;
  317. int64_t group_key;
  318. uint32_t node_index = 0;
  319. rtStream_t stream = nullptr;
  320. bool is_unknown_shape = graph->GetGraphUnknownFlag() || GetContext().GetHostExecFlag();
  321. if (is_unknown_shape) {
  322. GE_CHK_STATUS_RET(SetUnknownShapeStream(run_context, stream),
  323. "[Set][UnknownShapeStream] failed, graph:%s.", graph->GetName().c_str());
  324. }
  325. std::function<void()> callback = [&]() {
  326. if (is_unknown_shape) {
  327. if (DestroyUnknownShapeStream(run_context, stream) != SUCCESS) {
  328. GELOGE(FAILED, "[Destroy][UnknownShapeStream] failed.");
  329. }
  330. }
  331. };
  332. GE_MAKE_GUARD(release, callback);
  333. auto ffts_filter = [](const Node &node, const char *, const ComputeGraphPtr &) {
  334. return !node.GetOpDesc()->HasAttr(ATTR_NAME_FFTS_SUB_GRAPH);
  335. };
  336. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag(), nullptr, ffts_filter)) {
  337. OpDescPtr op_desc = node->GetOpDesc();
  338. GE_CHECK_NOTNULL(op_desc);
  339. node_index++;
  340. string name = node->GetName();
  341. string type = node->GetType();
  342. bool attr_notask = false;
  343. bool get_attr_notask_flag = ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask);
  344. GE_IF_BOOL_EXEC(get_attr_notask_flag && attr_notask,
  345. GELOGI("Node[name:%s, type:%s] does not need to generate task.", name.c_str(), type.c_str());
  346. continue);
  347. GE_CHK_STATUS_RET(UpdateOpIsVarAttr(op_desc, graph->GetSessionID()));
  348. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  349. // For fusion ddb pass, task def must be continuous.
  350. // Part2: Call
  351. auto fusion_task_info =
  352. FusionTaskInfo{run_context, graph, node, op_desc, node_index, ge_lib,
  353. ops_kernel_manager, task_def_list, op_name_map, profiling_point, all_reduce_nodes};
  354. GE_CHK_STATUS_RET(GenerateTaskForFusionNode(fusion_task_info, fusion_nodes, fusion_nodes_seen),
  355. "[Call][GenerateTaskForFusionNode] node:%s(%s) failed", name.c_str(), type.c_str());
  356. // continue directly
  357. if (ge::AttrUtils::GetInt(op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key)) {
  358. GELOGI("Fusion node[name:%s, type:%s] do not need generate task again.", name.c_str(), type.c_str());
  359. continue;
  360. }
  361. GE_CHK_BOOL_EXEC_INFO(!op_kernel_lib_name.empty(), continue,
  362. "Node[name:%s, type:%s] does not need to generate task.", name.c_str(), type.c_str());
  363. auto kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(op_kernel_lib_name);
  364. if (kernel_info_store == nullptr) {
  365. REPORT_INNER_ERROR("E19999", "Get ops kernel info store failed for op:%s(%s), op_kernel_name:%s",
  366. node->GetName().c_str(), node->GetType().c_str(), op_kernel_lib_name.c_str());
  367. GELOGE(INTERNAL_ERROR, "[Call][GetOpsKernelInfoStore] No ops kernel store or ops kernel builder found. "
  368. "node:%s(%s), op_kernel_lib_name=%s.", name.c_str(), type.c_str(), op_kernel_lib_name.c_str());
  369. return INTERNAL_ERROR;
  370. }
  371. GE_CHK_STATUS_RET(UpdateAnchorStatus(node), "[Call][UpdateAnchorStatus] node:%s(%s) failed", name.c_str(),
  372. type.c_str());
  373. if (node->GetOpDesc()->HasAttr(ATTR_NAME_FFTS_SUB_GRAPH)) {
  374. GE_CHK_STATUS_RET(UpdateAnchorStatusForFfts(node), "[Call][UpdateAnchorStatusForFfts] node:%s(%s) failed",
  375. name.c_str(), type.c_str());
  376. }
  377. // Profiling task
  378. size_t task_list_size_before = task_def_list.size();
  379. GE_CHK_STATUS_RET(InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  380. int64_t op_id = op_desc->GetId();
  381. // Compatible with dynamic shape scenes, the default is 0
  382. int64_t stream_id = 0;
  383. if (!is_unknown_shape) {
  384. stream_id = op_desc->GetStreamId();
  385. GE_CHK_STATUS_RET(SetKnownShapeStream(run_context, stream_id),
  386. "[Set][KnownShapeStream] node[name:%s(%s), id:%ld] stream id is invalid.",
  387. name.c_str(), type.c_str(), op_id);
  388. }
  389. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task.", op_kernel_lib_name.c_str(),
  390. name.c_str(), type.c_str(), op_id, stream_id);
  391. GE_TIMESTAMP_RESTART(GenerateTask);
  392. auto ret = OpsKernelBuilderManager::Instance().GenerateTask(*node, run_context, task_def_list);
  393. GE_TIMESTAMP_ADD(GenerateTask);
  394. if (ret != SUCCESS) {
  395. REPORT_CALL_ERROR("E19999", "Call OpsKernelBuilderManager GenerateTask fail for op:%s(%s)",
  396. node->GetName().c_str(), node->GetType().c_str());
  397. GELOGE(ret, "[Generate][Task] fail for op:%s(%s)", node->GetName().c_str(), node->GetType().c_str());
  398. return ret;
  399. }
  400. // Profiling task
  401. GE_CHK_STATUS_RET(InsertProfilingTaskAfter(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  402. size_t task_list_size_after = task_def_list.size();
  403. // If tasks is reduced
  404. if (task_list_size_after < task_list_size_before) {
  405. REPORT_INNER_ERROR("E19999", "Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task "
  406. "but task num from %zu to %zu, check invalid", op_kernel_lib_name.c_str(), name.c_str(),
  407. type.c_str(), op_id, stream_id, task_list_size_before, task_list_size_after);
  408. GELOGE(FAILED, "[Check][Param] Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task. "
  409. "but task num from %zu to %zu.", op_kernel_lib_name.c_str(), name.c_str(), type.c_str(),
  410. op_id, stream_id, task_list_size_before, task_list_size_after);
  411. return FAILED;
  412. }
  413. // Reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  414. for (size_t idx = task_list_size_before; idx < task_list_size_after; ++idx) {
  415. task_def_list[idx].set_stream_id(static_cast<uint32_t>(stream_id));
  416. op_name_map[idx] = name;
  417. TaskDef *task_def_ptr = &task_def_list[idx];
  418. GE_CHECK_NOTNULL(task_def_ptr);
  419. // Set opsKernelInfoStorePtr for hccl which will be use in DistributeTask and InitTaskInfo
  420. if (op_kernel_lib_name == kKernelInfoNameHccl) {
  421. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(kernel_info_store.get()));
  422. }
  423. }
  424. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task finished, generate %zu task(s).",
  425. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id,
  426. task_list_size_after - task_list_size_before);
  427. }
  428. GE_TIMESTAMP_CALLNUM_EVENT_END(GenerateTask, "GraphBuild::GenerateTask");
  429. return SUCCESS;
  430. }
  431. Status TaskGenerator::GenerateTaskForFusionNode(FusionTaskInfo &fusion_task_info,
  432. std::map<int64_t, std::vector<NodePtr>> &fusion_nodes,
  433. std::unordered_set<Node *> &fusion_nodes_seen) {
  434. Status ret = SUCCESS;
  435. int64_t group_key;
  436. auto &run_context = fusion_task_info.run_context;
  437. auto &graph = fusion_task_info.graph;
  438. auto &node = fusion_task_info.node;
  439. auto &fusion_op_desc = fusion_task_info.fusion_op_desc;
  440. auto &node_index = fusion_task_info.node_index;
  441. const auto &ops_kernel_manager = fusion_task_info.ops_kernel_manager;
  442. auto &task_def_list = fusion_task_info.task_def_list;
  443. auto &op_name_map = fusion_task_info.op_name_map;
  444. auto &profiling_point = fusion_task_info.profiling_point;
  445. auto &all_reduce_nodes = fusion_task_info.all_reduce_nodes;
  446. // If op_desc have this attr, call nodes with same group key in a stream together
  447. if (ge::AttrUtils::GetInt(fusion_op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key) &&
  448. (fusion_nodes_seen.count(node.get()) == 0)) {
  449. GELOGI("Fusion: start fusion group index[%ld], nodes size[%zu].", group_key, fusion_nodes[group_key].size());
  450. for (auto &fusion_node : fusion_nodes[group_key]) {
  451. OpDescPtr op_desc = fusion_node->GetOpDesc();
  452. UpdateOpIsVarAttr(op_desc, graph->GetSessionID());
  453. std::string fusion_node_name = fusion_node->GetName();
  454. std::string fusion_node_type = fusion_node->GetType();
  455. std::string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  456. if (op_kernel_lib_name.empty()) {
  457. GELOGI("Fusion: fusion_node[name:%s(%s)] task no need to generate task.", fusion_node_name.c_str(),
  458. fusion_node_type.c_str());
  459. continue;
  460. }
  461. bool attr_notask = false;
  462. GE_IF_BOOL_EXEC(ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask,
  463. GELOGI("Fusion: fusion_node[name:%s, type:%s] does not need to generate task.",
  464. fusion_node_name.c_str(), fusion_node_type.c_str());
  465. continue);
  466. size_t task_list_size_before = task_def_list.size();
  467. OpsKernelInfoStorePtr kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(op_kernel_lib_name);
  468. if (kernel_info_store == nullptr) {
  469. REPORT_INNER_ERROR("E19999", "Get ops kernel info store failed for op:%s(%s), op_kernel_name:%s",
  470. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  471. op_kernel_lib_name.c_str());
  472. GELOGE(INTERNAL_ERROR, "[Call][GetOpsKernelInfoStore] Fusion: No ops kernel store or ops kernel builder found."
  473. " fusion_node:%s(%s), op_kernel_lib_name=%s.",
  474. fusion_node_name.c_str(), fusion_node_type.c_str(), op_kernel_lib_name.c_str());
  475. return INTERNAL_ERROR;
  476. }
  477. ret = UpdateAnchorStatus(fusion_node);
  478. if (ret != SUCCESS) {
  479. GELOGE(ret, "[Update][AnchorStatus] fusion_node:%s(%s) failed", fusion_node_name.c_str(),
  480. fusion_node_type.c_str());
  481. return ret;
  482. }
  483. int64_t op_id = op_desc->GetId();
  484. int64_t stream_id = op_desc->GetStreamId();
  485. if (stream_id < 0 || stream_id >= (int64_t)run_context.graphStreamList.size()) {
  486. REPORT_INNER_ERROR("E19999", "Fusion: fusion_node[name:%s(%s), id:%ld] stream id is invalid, "
  487. "stream list size=%zu", fusion_node_name.c_str(), fusion_node_type.c_str(),
  488. op_id, run_context.graphStreamList.size());
  489. GELOGE(INTERNAL_ERROR, "[Check][Param] Fusion: fusion_node[name:%s(%s), id:%ld] stream id is invalid, "
  490. "stream list size=%zu", fusion_node_name.c_str(), fusion_node_type.c_str(), op_id,
  491. run_context.graphStreamList.size());
  492. return INTERNAL_ERROR;
  493. }
  494. // profiling task
  495. (void)InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list);
  496. run_context.stream = run_context.graphStreamList[stream_id];
  497. GELOGI("Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld] task.",
  498. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  499. ret = OpsKernelBuilderManager::Instance().GenerateTask(*fusion_node, run_context, task_def_list);
  500. if (ret != SUCCESS) {
  501. REPORT_CALL_ERROR("E19999", " Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  502. "id:%ld, stream_id:%ld] task failed", op_kernel_lib_name.c_str(),
  503. fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  504. GELOGE(ret, "[Generate][Task] Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  505. "id:%ld, stream_id:%ld] task failed.",
  506. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  507. return ret;
  508. }
  509. // profiling task
  510. (void)InsertProfilingTaskAfter(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list);
  511. size_t task_list_size_after = task_def_list.size();
  512. // if tasks is reduced
  513. if (task_list_size_after < task_list_size_before) {
  514. REPORT_INNER_ERROR("E19999", "InsertProfilingTask for fusion_node:[fusion_node_name:%s(%s), kernel_name:%s"
  515. "id:%ld, stream_id:%ld] task, but task num from %zu to %zu, check invalid",
  516. fusion_node_name.c_str(), fusion_node_type.c_str(), op_kernel_lib_name.c_str(),
  517. op_id, stream_id, task_list_size_before, task_list_size_after);
  518. GELOGE(FAILED, "[Check][Param] Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  519. "id:%ld, stream_id:%ld] task. but task num from %zu to %zu.",
  520. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  521. task_list_size_before, task_list_size_after);
  522. return FAILED;
  523. }
  524. // reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  525. void *ops_kernel_info_store_ptr = kernel_info_store.get();
  526. for (size_t idx = task_list_size_before; idx < task_list_size_after; ++idx) {
  527. task_def_list[idx].set_stream_id(static_cast<uint32_t>(stream_id));
  528. op_name_map[idx] = fusion_node_name;
  529. // set opsKernelInfoStorePtr and op_index, the two fields be use in DistributeTask and InitTaskInfo
  530. TaskDef *task_def_ptr = &task_def_list[idx];
  531. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(ops_kernel_info_store_ptr));
  532. }
  533. GELOGI("Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld]"
  534. " task finished, generate %zu task(s).",
  535. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  536. task_list_size_after - task_list_size_before);
  537. // record nodes which have call generate task successfully
  538. fusion_nodes_seen.insert(fusion_node.get());
  539. node_index++;
  540. }
  541. }
  542. // without tag or has been seen, skip directly
  543. return ret;
  544. }
  545. Status TaskGenerator::UpdateAnchorStatusForFfts(const NodePtr &node) {
  546. GELOGD("Start UpdateAnchorStatusForFfts for %s.", node->GetName().c_str());
  547. if (!node->GetOpDesc()->GetSubgraphInstanceNames().empty()) {
  548. for (size_t i = 0; i < node->GetOpDesc()->GetSubgraphInstanceNames().size(); ++i) {
  549. auto sub_graph = NodeUtils::GetSubgraph(*node, i);
  550. GE_CHECK_NOTNULL(sub_graph);
  551. GELOGD("Start update anchor status for %s.", sub_graph->GetName().c_str());
  552. for (auto &ffts_node : sub_graph->GetDirectNode()) {
  553. GE_CHK_STATUS_RET(UpdateAnchorStatus(ffts_node), "[Call][UpdateAnchorStatus] node:%s(%s) failed",
  554. ffts_node->GetName().c_str(), ffts_node->GetType().c_str());
  555. }
  556. }
  557. }
  558. return SUCCESS;
  559. }
  560. Status TaskGenerator::UpdateAnchorStatus(const NodePtr &node) {
  561. GELOGD("Start UpdateAnchorStatus for %s.", node->GetName().c_str());
  562. if (NodeUtils::SetAllAnchorStatus(node) != GRAPH_SUCCESS) {
  563. REPORT_CALL_ERROR("E19999", "SetAllAnchorStatus fail for op:%s(%s)",
  564. node->GetName().c_str(), node->GetType().c_str());
  565. GELOGE(INTERNAL_ERROR, "[Set][AllAnchorStatus] failed, op:%s(%s)",
  566. node->GetName().c_str(), node->GetType().c_str());
  567. return INTERNAL_ERROR;
  568. }
  569. for (auto &anchor : node->GetAllInDataAnchors()) {
  570. auto peer_anchor = anchor->GetPeerOutAnchor();
  571. if (peer_anchor == nullptr) {
  572. if (AnchorUtils::SetStatus(anchor, ANCHOR_SUSPEND) != GRAPH_SUCCESS) {
  573. REPORT_CALL_ERROR("E19999", "Set in peer anchor status fail for op:%s(%s), anchor_index:%d",
  574. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  575. GELOGE(INTERNAL_ERROR, "[Set][Status] failed, op:%s(%s), anchor_index:%d",
  576. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  577. return INTERNAL_ERROR;
  578. }
  579. continue;
  580. }
  581. std::string const_type;
  582. bool is_const = NodeUtils::GetConstOpType(peer_anchor->GetOwnerNode(), const_type);
  583. if (is_const && (const_type == CONSTANT)) {
  584. if (AnchorUtils::SetStatus(anchor, ANCHOR_CONST) != GRAPH_SUCCESS) {
  585. REPORT_CALL_ERROR("E19999", "Set in anchor CONST status fail for op:%s(%s), anchor_index:%d",
  586. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  587. GELOGE(INTERNAL_ERROR, "[Set][Status] failed. op:%s(%s), anchor_index:%d.",
  588. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  589. return INTERNAL_ERROR;
  590. }
  591. } else {
  592. if (AnchorUtils::SetStatus(anchor, ANCHOR_DATA) != GRAPH_SUCCESS) {
  593. REPORT_CALL_ERROR("E19999", "Set in anchor DATA status fail for op:%s(%s), anchor_index:%d",
  594. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  595. GELOGE(INTERNAL_ERROR, "[Set][Status] failed, op:%s(%s), anchor_index:%d.",
  596. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  597. return INTERNAL_ERROR;
  598. }
  599. }
  600. }
  601. return SUCCESS;
  602. }
  603. Status TaskGenerator::MarkNodeAndSetIndex(ComputeGraphPtr &graph) {
  604. auto ge_lib = GELib::GetInstance();
  605. if ((ge_lib == nullptr) || !ge_lib->InitFlag()) {
  606. REPORT_INNER_ERROR("E19999", "Check GELib instance not init before");
  607. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Check][Param] GE is not initialized or is finalized.");
  608. return GE_CLI_GE_NOT_INITIALIZED;
  609. }
  610. const auto all_nodes = graph->GetNodes(graph->GetGraphUnknownFlag());
  611. if (all_nodes.empty()) {
  612. REPORT_INNER_ERROR("E19999", "Check param all_nodes empty in graph:%s",
  613. graph->GetName().c_str());
  614. GELOGE(GE_GRAPH_GRAPH_NODE_NULL, "[Check][Param] Graph's node is empty, graph:%s", graph->GetName().c_str());
  615. return GE_GRAPH_GRAPH_NODE_NULL;
  616. }
  617. map<int64_t, vector<OpDescPtr>> all_stream_ops;
  618. for (auto &node : all_nodes) {
  619. OpDescPtr op_desc = node->GetOpDesc();
  620. GE_CHECK_NOTNULL(op_desc);
  621. // Reset op kernel lib name
  622. if (op_desc->GetOpKernelLibName().empty()) {
  623. (void)ge_lib->DNNEngineManagerObj().GetDNNEngineName(node);
  624. }
  625. (void)op_desc->DelAttr(kIsFirstNode);
  626. (void)op_desc->DelAttr(kIsLastNode);
  627. all_stream_ops[op_desc->GetStreamId()].emplace_back(op_desc);
  628. }
  629. bool is_single_stream = all_stream_ops.size() == 1;
  630. for (const auto &stream_ops : all_stream_ops) {
  631. Status status = MarkFirstAndLastOps(stream_ops.second, is_single_stream);
  632. if (status != SUCCESS) {
  633. GELOGE(status, "[Mark][FirstAndLastOps] failed, graph:%s.", graph->GetName().c_str());
  634. return status;
  635. }
  636. }
  637. return SUCCESS;
  638. }
  639. Status TaskGenerator::MarkFirstAndLastOps(const vector<OpDescPtr> &ops, bool is_single_stream) const {
  640. vector<vector<OpDescPtr>> continuous_op_lists(1);
  641. const set<string> separator_types(
  642. {LABELSET, LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX, STREAMSWITCH, STREAMSWITCHN});
  643. for (auto &op_desc : ops) {
  644. bool attr_notask = false;
  645. if (ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask) {
  646. continue;
  647. }
  648. string op_type = op_desc->GetType();
  649. if ((!is_single_stream && !op_desc->GetSubgraphInstanceNames().empty()) || separator_types.count(op_type) != 0) {
  650. continuous_op_lists.emplace_back(vector<OpDescPtr>());
  651. } else {
  652. continuous_op_lists.back().emplace_back(op_desc);
  653. }
  654. }
  655. GELOGD("Number of continuous node lists is %zu.", continuous_op_lists.size());
  656. for (const auto &continuous_ops : continuous_op_lists) {
  657. map<string, std::pair<OpDescPtr, OpDescPtr>> first_and_last_ops;
  658. for (auto &op_desc : continuous_ops) {
  659. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  660. if (op_kernel_lib_name.empty()) {
  661. REPORT_INNER_ERROR("E19999", "Get ops kernel info store failed for op:%s(%s), op_kernel_name:%s",
  662. op_desc->GetName().c_str(), op_desc->GetType().c_str(), op_kernel_lib_name.c_str());
  663. GELOGE(INTERNAL_ERROR, "[Check][Param] node:%s(%s) get op kernel lib failed.", op_desc->GetName().c_str(),
  664. op_desc->GetType().c_str());
  665. return INTERNAL_ERROR;
  666. }
  667. auto it = first_and_last_ops.find(op_kernel_lib_name);
  668. if (it == first_and_last_ops.end()) {
  669. first_and_last_ops.emplace(op_kernel_lib_name, std::make_pair(op_desc, op_desc));
  670. } else {
  671. it->second.second = op_desc;
  672. }
  673. }
  674. for (auto &it : first_and_last_ops) {
  675. auto &op_pair = it.second;
  676. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.first, kIsFirstNode, true),
  677. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", kIsFirstNode,
  678. op_pair.first->GetName().c_str(), op_pair.first->GetType().c_str());
  679. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", kIsFirstNode,
  680. op_pair.first->GetName().c_str(), op_pair.first->GetType().c_str());
  681. return FAILED);
  682. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.second, kIsLastNode, true),
  683. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", kIsLastNode,
  684. op_pair.second->GetName().c_str(), op_pair.second->GetType().c_str());
  685. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", kIsLastNode,
  686. op_pair.second->GetName().c_str(), op_pair.second->GetType().c_str());
  687. return FAILED);
  688. }
  689. }
  690. return SUCCESS;
  691. }
  692. Status TaskGenerator::AutoFindFpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point) const {
  693. GELOGI("Start AutoFindFpOpIndex");
  694. OpDescPtr fp_op_desc = nullptr;
  695. uint32_t current_idx = 0;
  696. uint32_t first_fp = 0;
  697. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  698. OpDescPtr op_desc = node->GetOpDesc();
  699. GE_CHECK_NOTNULL(op_desc);
  700. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  701. if (op_kernel_lib_name.empty()) {
  702. continue;
  703. }
  704. auto type = op_desc->GetType();
  705. std::string original_type;
  706. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, original_type);
  707. if (kFpNodeTypes.find(type) != kFpNodeTypes.end() || kFpNodeTypes.find(original_type) != kFpNodeTypes.end()) {
  708. auto out_anchor = node->GetOutDataAnchor(0);
  709. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  710. GE_CHECK_NOTNULL(peer_in_anchor);
  711. auto in_node_desc = peer_in_anchor->GetOwnerNode()->GetOpDesc();
  712. GE_CHECK_NOTNULL(in_node_desc);
  713. if (fp_op_desc == nullptr || ((in_node_desc->GetId()) < (fp_op_desc->GetId()))) {
  714. fp_op_desc = in_node_desc;
  715. }
  716. }
  717. break;
  718. }
  719. }
  720. if (fp_op_desc == nullptr) {
  721. GELOGW("not find fp_op_desc.");
  722. return SUCCESS;
  723. }
  724. GELOGI("Find fp_op_desc is %s, id is %ld", fp_op_desc->GetName().c_str(), fp_op_desc->GetId());
  725. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  726. OpDescPtr op_desc = node->GetOpDesc();
  727. GE_CHECK_NOTNULL(op_desc);
  728. current_idx++;
  729. if (op_desc->GetName() == fp_op_desc->GetName()) {
  730. first_fp = current_idx;
  731. GELOGI("First fp name is %s, idx is %u", op_desc->GetName().c_str(), first_fp);
  732. break;
  733. }
  734. }
  735. profiling_point.fp_index = first_fp;
  736. return SUCCESS;
  737. }
  738. Status TaskGenerator::AutoFindBpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  739. vector<uint32_t> &all_reduce_nodes) const {
  740. GELOGI("Start AutoFindBpOpIndex");
  741. NodePtr bp_node = nullptr;
  742. uint32_t current_idx = 0;
  743. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  744. OpDescPtr op_desc = node->GetOpDesc();
  745. GE_CHECK_NOTNULL(op_desc);
  746. current_idx++;
  747. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  748. if (op_kernel_lib_name.empty()) {
  749. continue;
  750. }
  751. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  752. bp_node = node;
  753. all_reduce_nodes.emplace_back(current_idx);
  754. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  755. }
  756. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  757. if (bp_node == nullptr) {
  758. bp_node = node;
  759. }
  760. }
  761. if (graph->GetNeedIteration()) {
  762. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGNADD) {
  763. profiling_point.end_index.insert(current_idx);
  764. GELOGI("Iter end name %s, idx %u, from Node_Output_IteratorCtrl_StreamSwitch_StreamActive",
  765. op_desc->GetName().c_str(), current_idx);
  766. }
  767. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGN) {
  768. profiling_point.end_index.insert(current_idx);
  769. GELOGI("Iter end name %s, idx %u, from FlowCtrl_LoopCond_ASSIGN",
  770. op_desc->GetName().c_str(), current_idx);
  771. }
  772. } else {
  773. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  774. profiling_point.end_index.insert(current_idx);
  775. GELOGI("Iter end name %s, idx %u, from NETOUTPUT", op_desc->GetName().c_str(), current_idx);
  776. }
  777. }
  778. }
  779. if (bp_node == nullptr) {
  780. GELOGW("not find bp_node.");
  781. return SUCCESS;
  782. }
  783. return FindLastBpFromBpNode(graph, bp_node, profiling_point.bp_index);
  784. }
  785. Status TaskGenerator::FindLastBpFromBpNode(const ComputeGraphPtr &graph, const NodePtr &target_node,
  786. uint32_t &bp_index) const {
  787. bp_index = 0;
  788. auto target_desc = target_node->GetOpDesc();
  789. GE_CHECK_NOTNULL(target_desc);
  790. OpDescPtr bp_op_desc = nullptr;
  791. for (auto &in_node : target_node->GetInAllNodes()) {
  792. GE_CHECK_NOTNULL(in_node);
  793. auto in_node_desc = in_node->GetOpDesc();
  794. GE_CHECK_NOTNULL(in_node_desc);
  795. if ((bp_op_desc == nullptr || (in_node_desc->GetId() > bp_op_desc->GetId())) &&
  796. (in_node_desc->GetStreamId() == target_desc->GetStreamId())){
  797. bp_op_desc = in_node_desc;
  798. }
  799. }
  800. if (bp_op_desc == nullptr) {
  801. GELOGI("Did not find bp node.");
  802. return SUCCESS;
  803. }
  804. uint32_t current_idx = 0;
  805. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  806. OpDescPtr op_desc = node->GetOpDesc();
  807. GE_CHECK_NOTNULL(op_desc);
  808. current_idx++;
  809. if (op_desc->GetName() == bp_op_desc->GetName()) {
  810. bp_index = current_idx;
  811. GELOGI("Find bp name %s, idx %u", op_desc->GetName().c_str(), bp_index);
  812. break;
  813. }
  814. }
  815. GELOGI("Last bp node[%s], type[%s], index[%u], stream id[%ld]", bp_op_desc->GetName().c_str(),
  816. bp_op_desc->GetType().c_str(), bp_index, bp_op_desc->GetStreamId());
  817. return SUCCESS;
  818. }
  819. Status TaskGenerator::FindFpOfEnv(const ComputeGraphPtr &graph, const std::string &fp_point_str,
  820. ProfilingPoint &profiling_point) const {
  821. GELOGI("Start FindFpOfEnv");
  822. uint32_t current_idx = 0;
  823. uint32_t first_fp = 0;
  824. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  825. OpDescPtr op_desc = node->GetOpDesc();
  826. GE_CHECK_NOTNULL(node->GetOpDesc());
  827. current_idx++;
  828. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  829. if (op_kernel_lib_name.empty()) {
  830. continue;
  831. }
  832. if (first_fp == 0 && IsProfPoint(op_desc, fp_point_str)) {
  833. first_fp = current_idx;
  834. GELOGI("First fp name from env is %s, idx %u", op_desc->GetName().c_str(), first_fp);
  835. }
  836. }
  837. profiling_point.fp_index = first_fp;
  838. return SUCCESS;
  839. }
  840. Status TaskGenerator::FindBpOfEnv(const ComputeGraphPtr &graph, const std::string &bp_point_str,
  841. ProfilingPoint &profiling_point, vector<uint32_t> &all_reduce_nodes) const {
  842. GELOGI("Start FindBpOfEnv");
  843. uint32_t current_idx = 0;
  844. uint32_t last_bp = 0;
  845. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  846. OpDescPtr op_desc = node->GetOpDesc();
  847. GE_CHECK_NOTNULL(node->GetOpDesc());
  848. current_idx++;
  849. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  850. if (op_kernel_lib_name.empty()) {
  851. continue;
  852. }
  853. if (graph->GetNeedIteration()) {
  854. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGNADD) {
  855. profiling_point.end_index.insert(current_idx);
  856. GELOGI("Iter end name %s, idx %u, from Node_Output_IteratorCtrl_StreamSwitch_StreamActive",
  857. op_desc->GetName().c_str(), current_idx);
  858. }
  859. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGN) {
  860. profiling_point.end_index.insert(current_idx);
  861. GELOGI("Iter end name %s, idx %u, from FlowCtrl_LoopCond_ASSIGN",
  862. op_desc->GetName().c_str(), current_idx);
  863. }
  864. } else {
  865. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  866. profiling_point.end_index.insert(current_idx);
  867. GELOGI("Iter end name %s, idx %u, from NETOUTPUT", op_desc->GetName().c_str(), current_idx);
  868. }
  869. }
  870. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  871. all_reduce_nodes.emplace_back(current_idx);
  872. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  873. }
  874. if (IsProfPoint(op_desc, bp_point_str)) {
  875. last_bp = current_idx;
  876. GELOGI("Last bp name from env is %s, idx %u", op_desc->GetName().c_str(), last_bp);
  877. }
  878. }
  879. profiling_point.bp_index = last_bp;
  880. return SUCCESS;
  881. }
  882. Status TaskGenerator::GetFpBpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  883. vector<uint32_t> &all_reduce_nodes, std::string &fp_point_str,
  884. std::string &bp_point_str) const {
  885. ProfilingManager::Instance().GetFpBpPoint(fp_point_str, bp_point_str);
  886. Status ret = SUCCESS;
  887. if (fp_point_str.empty()) {
  888. ret = AutoFindFpOpIndex(graph, profiling_point);
  889. if (ret != SUCCESS) {
  890. GELOGW("First forward profiling op_index not set and FindFpOpIndex failed.");
  891. return FAILED;
  892. }
  893. }
  894. if (bp_point_str.empty()) {
  895. ret = AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes);
  896. if (ret != SUCCESS) {
  897. GELOGW("Last backward profiling op_index not set and FindBpOpIndex failed.");
  898. return FAILED;
  899. }
  900. }
  901. return SUCCESS;
  902. }
  903. Status TaskGenerator::FindProfilingNodeIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  904. std::vector<uint32_t> &all_reduce_nodes) {
  905. return FindProfilingTaskIndex(graph, profiling_point, all_reduce_nodes);
  906. }
  907. Status TaskGenerator::FindProfilingTaskIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  908. vector<uint32_t> &all_reduce_nodes) const {
  909. GE_CHECK_NOTNULL(graph);
  910. const char *profiling_mode = std::getenv(kProfilingMode);
  911. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  912. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  913. if (!is_profiling) {
  914. GELOGD("Profiling is not open.");
  915. return SUCCESS;
  916. }
  917. // subgraph of dynamic graph no need to find index, has been found in parent graph
  918. if (IsSubGraphOfDynamicGraph(graph)) {
  919. GELOGI("Graph[%s] is subgraph of dynamic graph, no need to find index.", graph->GetName().c_str());
  920. return SUCCESS;
  921. }
  922. GELOGI("Start get FP/BP index.");
  923. std::string fp_point_str;
  924. std::string bp_point_str;
  925. Status ret = GetFpBpIndex(graph, profiling_point, all_reduce_nodes, fp_point_str, bp_point_str);
  926. if (ret != SUCCESS) {
  927. GELOGW("Get FP_POINT BP_POINT failed.");
  928. return SUCCESS;
  929. }
  930. GELOGI("fp_point_str:%s, bp_point_str:%s.", fp_point_str.c_str(), bp_point_str.c_str());
  931. if (!fp_point_str.empty()) {
  932. ret = FindFpOfEnv(graph, fp_point_str, profiling_point);
  933. if (ret != SUCCESS) {
  934. GELOGW("First backward profiling op name set but FindFpOfEnv failed.");
  935. return SUCCESS;
  936. }
  937. }
  938. if (!bp_point_str.empty()) {
  939. ret = FindBpOfEnv(graph, bp_point_str, profiling_point, all_reduce_nodes);
  940. if (ret != SUCCESS) {
  941. GELOGW("Last backward profiling op name set but FindBpOfEnv failed.");
  942. return SUCCESS;
  943. }
  944. }
  945. bool train_graph = graph->GetNeedIteration();
  946. if (profiling_point.fp_index == 0 && train_graph) {
  947. GELOGW("First forward op name can't be found in graph for training trace.");
  948. }
  949. if (profiling_point.bp_index == 0 && train_graph) {
  950. GELOGW("Last backward op name can't be found in graph for training trace.");
  951. }
  952. return SUCCESS;
  953. }
  954. Status TaskGenerator::InsertProfilingArTaskBefore(const OpDescPtr &op_desc, std::vector<uint32_t> &all_reduce_nodes,
  955. uint32_t node_index, std::vector<domi::TaskDef> &task_def_list,
  956. bool is_insert_bp_profiling_task) {
  957. bool is_insert_all_reduce_task = false;
  958. int64_t ar_log_id = 0xFFFF;
  959. if (is_insert_bp_profiling_task) {
  960. (void)ge::AttrUtils::GetInt(op_desc, ATTR_NAME_INSERT_PROFILILNG_TASK_LOG_ID, ar_log_id);
  961. is_insert_all_reduce_task = true;
  962. }
  963. if (!is_insert_all_reduce_task) {
  964. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  965. if (all_reduce_nodes[i] == node_index) {
  966. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  967. REPORT_INNER_ERROR("E19999", "Multiply result is out of range when calc profiling ar log id "
  968. "for node:%s(%s)", op_desc->GetName().c_str(), op_desc->GetType().c_str());
  969. GELOGE(FAILED, "[Check][Param] Multiply result is out of range. node:%s(%s)",
  970. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  971. return FAILED);
  972. ar_log_id = i * kProfilingArStep + kProfilingArStartLogid;
  973. is_insert_all_reduce_task = true;
  974. break;
  975. }
  976. }
  977. }
  978. if (is_insert_all_reduce_task) {
  979. GELOGI("The start allreduce operator is %s, idx %u, log_id %ld", op_desc->GetName().c_str(), node_index, ar_log_id);
  980. TaskDef ar_task_def;
  981. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  982. ar_task_def.set_stream_id(op_desc->GetStreamId());
  983. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  984. if (ar_log_def != nullptr) {
  985. ar_log_def->set_logid(ar_log_id);
  986. ar_log_def->set_notify(false);
  987. }
  988. task_def_list.push_back(ar_task_def);
  989. }
  990. return SUCCESS;
  991. }
  992. Status TaskGenerator::InsertProfilingTaskBefore(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  993. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  994. vector<domi::TaskDef> &task_def_list) {
  995. const char *profiling_mode = std::getenv(kProfilingMode);
  996. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  997. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  998. bool is_insert_fp_profiling_task = false;
  999. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_FP_PROFILILNG_TASK, is_insert_fp_profiling_task);
  1000. bool is_insert_bp_profiling_task = false;
  1001. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_BP_PROFILILNG_TASK, is_insert_bp_profiling_task);
  1002. bool no_insert_profiling_task = ((profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  1003. (profiling_point.end_index.empty())) &&
  1004. (!(is_insert_fp_profiling_task || is_insert_bp_profiling_task));
  1005. if (!is_profiling || no_insert_profiling_task) {
  1006. return SUCCESS;
  1007. }
  1008. GELOGD("Insert fp profiling task: %d, insert bp profiling task: %d, fp index: %u, bp index: %u, end index size: %zu",
  1009. is_insert_fp_profiling_task, is_insert_bp_profiling_task, profiling_point.fp_index, profiling_point.bp_index,
  1010. profiling_point.end_index.size());
  1011. if ((profiling_point.fp_index == node_index) || is_insert_fp_profiling_task) {
  1012. uint64_t jobid_log_id = ge::GetContext().TraceId();
  1013. GELOGI("The first FP operator is %s, idx %u, job_id %lu", op_desc->GetName().c_str(), node_index, jobid_log_id);
  1014. TaskDef job_task_def;
  1015. job_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1016. job_task_def.set_stream_id(op_desc->GetStreamId());
  1017. LogTimeStampDef *job_log_def = job_task_def.mutable_log_timestamp();
  1018. if (job_log_def != nullptr) {
  1019. job_log_def->set_logid(jobid_log_id);
  1020. job_log_def->set_notify(false);
  1021. }
  1022. task_def_list.emplace_back(job_task_def);
  1023. TaskDef fp_task_def;
  1024. fp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1025. fp_task_def.set_stream_id(op_desc->GetStreamId());
  1026. LogTimeStampDef *fp_log_def = fp_task_def.mutable_log_timestamp();
  1027. if (fp_log_def != nullptr) {
  1028. fp_log_def->set_logid(kProfilingFpStartLogid);
  1029. fp_log_def->set_notify(false);
  1030. }
  1031. task_def_list.emplace_back(fp_task_def);
  1032. }
  1033. bool is_all_reduce = (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE);
  1034. if (is_all_reduce) {
  1035. (void)InsertProfilingArTaskBefore(op_desc, all_reduce_nodes, node_index,
  1036. task_def_list, is_insert_bp_profiling_task);
  1037. }
  1038. return SUCCESS;
  1039. }
  1040. Status TaskGenerator::InsertProfilingArTaskAfter(const OpDescPtr &op_desc, std::vector<uint32_t> &all_reduce_nodes,
  1041. uint32_t node_index, std::vector<domi::TaskDef> &task_def_list,
  1042. bool is_insert_bp_profiling_task) {
  1043. bool is_insert_all_reduce_task = false;
  1044. int64_t ar_log_id = 0xFFFF;
  1045. if (is_insert_bp_profiling_task) {
  1046. (void)ge::AttrUtils::GetInt(op_desc, ATTR_NAME_INSERT_PROFILILNG_TASK_LOG_ID, ar_log_id);
  1047. ar_log_id += 1;
  1048. is_insert_all_reduce_task = true;
  1049. }
  1050. if (!is_insert_all_reduce_task) {
  1051. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  1052. if (all_reduce_nodes[i] == node_index) {
  1053. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  1054. REPORT_INNER_ERROR("E19999", "Multiply result is out of range when calc profiling ar log id "
  1055. "for node:%s(%s)", op_desc->GetName().c_str(), op_desc->GetType().c_str());
  1056. GELOGE(FAILED, "[Check][Param] Multiply result is out of range. node:%s(%s)",
  1057. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  1058. return FAILED);
  1059. ar_log_id = i * kProfilingArStep + kProfilingArEndLogid;
  1060. is_insert_all_reduce_task = true;
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. if (is_insert_all_reduce_task) {
  1066. GELOGI("The start allreduce operator is %s, idx %u, log_id %ld", op_desc->GetName().c_str(), node_index, ar_log_id);
  1067. TaskDef ar_task_def;
  1068. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1069. ar_task_def.set_stream_id(op_desc->GetStreamId());
  1070. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  1071. if (ar_log_def != nullptr) {
  1072. ar_log_def->set_logid(ar_log_id);
  1073. ar_log_def->set_notify(false);
  1074. }
  1075. task_def_list.push_back(ar_task_def);
  1076. }
  1077. return SUCCESS;
  1078. }
  1079. Status TaskGenerator::InsertProfilingTaskAfter(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  1080. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  1081. vector<domi::TaskDef> &task_def_list) {
  1082. GE_CHECK_NOTNULL(op_desc);
  1083. const char *profiling_mode = std::getenv(kProfilingMode);
  1084. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  1085. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  1086. bool is_insert_bp_profiling_task = false;
  1087. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_BP_PROFILILNG_TASK, is_insert_bp_profiling_task);
  1088. bool is_insert_end_profiling_task = false;
  1089. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_END_PROFILILNG_TASK, is_insert_end_profiling_task);
  1090. bool no_insert_profiling_task = ((profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  1091. (profiling_point.end_index.empty())) &&
  1092. (!(is_insert_bp_profiling_task || is_insert_end_profiling_task));
  1093. if (!is_profiling || no_insert_profiling_task) {
  1094. return SUCCESS;
  1095. }
  1096. GELOGD("Insert bp profiling task: %d, insert end profiling task: %d, fp index: %u, bp index: %u, end index size: %zu",
  1097. is_insert_bp_profiling_task, is_insert_end_profiling_task, profiling_point.fp_index, profiling_point.bp_index,
  1098. profiling_point.end_index.size());
  1099. bool is_all_reduce = (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE);
  1100. if ((profiling_point.bp_index == node_index) || (!is_all_reduce && is_insert_bp_profiling_task)) {
  1101. GELOGI("The last BP operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  1102. TaskDef bp_task_def;
  1103. bp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1104. bp_task_def.set_stream_id(op_desc->GetStreamId());
  1105. LogTimeStampDef *bp_log_def = bp_task_def.mutable_log_timestamp();
  1106. GE_CHECK_NOTNULL(bp_log_def);
  1107. bp_log_def->set_logid(kProfilingBpEndLogid);
  1108. bp_log_def->set_notify(false);
  1109. task_def_list.emplace_back(bp_task_def);
  1110. }
  1111. if (profiling_point.end_index.find(node_index) != profiling_point.end_index.end() ||
  1112. is_insert_end_profiling_task) {
  1113. GELOGI("The iteration end operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  1114. TaskDef end_task_def;
  1115. end_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1116. end_task_def.set_stream_id(op_desc->GetStreamId());
  1117. LogTimeStampDef *end_log_def = end_task_def.mutable_log_timestamp();
  1118. GE_CHECK_NOTNULL(end_log_def);
  1119. end_log_def->set_logid(kProfilingIterEndLogid);
  1120. end_log_def->set_notify(true);
  1121. task_def_list.emplace_back(end_task_def);
  1122. }
  1123. if (is_all_reduce) {
  1124. (void)InsertProfilingArTaskAfter(op_desc, all_reduce_nodes, node_index,
  1125. task_def_list, is_insert_bp_profiling_task);
  1126. }
  1127. return SUCCESS;
  1128. }
  1129. bool TaskGenerator::IsProfPoint(const OpDescPtr &op, const std::string &name) {
  1130. if (op == nullptr) {
  1131. return false;
  1132. }
  1133. if (op->GetName() == name) {
  1134. return true;
  1135. }
  1136. std::vector<std::string> original_op_names;
  1137. bool ret = AttrUtils::GetListStr(op, ge::ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, original_op_names);
  1138. if (!ret) {
  1139. return false;
  1140. }
  1141. for (auto &origin_name : original_op_names) {
  1142. if (origin_name == name) {
  1143. return true;
  1144. }
  1145. }
  1146. return false;
  1147. }
  1148. Status TaskGenerator::SetUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  1149. GE_CHK_RT_RET(rtStreamCreate(&stream, 0));
  1150. run_context.stream = stream;
  1151. rtError_t rt_ret = rtModelBindStream(run_context.model, stream, 0);
  1152. if (rt_ret != RT_ERROR_NONE) {
  1153. REPORT_CALL_ERROR("E19999", "Call rtModelBindStream failed, ret:0x%X", rt_ret);
  1154. GELOGE(FAILED, "[Call][RtModelBindStream] failed, ret: 0x%X", rt_ret);
  1155. GE_CHK_RT_RET(rtStreamDestroy(stream));
  1156. return FAILED;
  1157. }
  1158. return SUCCESS;
  1159. }
  1160. Status TaskGenerator::DestroyUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  1161. GE_CHK_RT(rtModelUnbindStream(run_context.model, stream));
  1162. GE_CHK_RT_RET(rtStreamDestroy(stream));
  1163. return SUCCESS;
  1164. }
  1165. Status TaskGenerator::SetKnownShapeStream(RunContext &run_context, int64_t stream_id) {
  1166. if (stream_id < 0 || stream_id >= static_cast<int64_t>(run_context.graphStreamList.size())) {
  1167. GELOGE(INTERNAL_ERROR, "[Check][Param] Stream id[%ld] is invalid, stream list size=%zu", stream_id,
  1168. run_context.graphStreamList.size());
  1169. return INTERNAL_ERROR;
  1170. }
  1171. run_context.stream = run_context.graphStreamList[stream_id];
  1172. return SUCCESS;
  1173. }
  1174. } // namespace ge

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