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 59 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
3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. GE_CHECK_NOTNULL(kernel_info_store);
  365. GE_CHK_STATUS_RET(UpdateAnchorStatus(node), "[Call][UpdateAnchorStatus] node:%s(%s) failed", name.c_str(),
  366. type.c_str());
  367. if (node->GetOpDesc()->HasAttr(ATTR_NAME_FFTS_SUB_GRAPH)) {
  368. GE_CHK_STATUS_RET(UpdateAnchorStatusForFfts(node), "[Call][UpdateAnchorStatusForFfts] node:%s(%s) failed",
  369. name.c_str(), type.c_str());
  370. }
  371. // Profiling task
  372. size_t task_list_size_before = task_def_list.size();
  373. GE_CHK_STATUS_RET(InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  374. int64_t op_id = op_desc->GetId();
  375. // Compatible with dynamic shape scenes, the default is 0
  376. int64_t stream_id = 0;
  377. if (!is_unknown_shape) {
  378. stream_id = op_desc->GetStreamId();
  379. GE_CHK_STATUS_RET(SetKnownShapeStream(run_context, stream_id),
  380. "[Set][KnownShapeStream] node[name:%s(%s), id:%ld] stream id is invalid.",
  381. name.c_str(), type.c_str(), op_id);
  382. }
  383. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task.", op_kernel_lib_name.c_str(),
  384. name.c_str(), type.c_str(), op_id, stream_id);
  385. GE_TIMESTAMP_RESTART(GenerateTask);
  386. auto ret = OpsKernelBuilderManager::Instance().GenerateTask(*node, run_context, task_def_list);
  387. GE_TIMESTAMP_ADD(GenerateTask);
  388. if (ret != SUCCESS) {
  389. REPORT_CALL_ERROR("E19999", "Call OpsKernelBuilderManager GenerateTask fail for op:%s(%s)",
  390. node->GetName().c_str(), node->GetType().c_str());
  391. GELOGE(ret, "[Generate][Task] fail for op:%s(%s)", node->GetName().c_str(), node->GetType().c_str());
  392. return ret;
  393. }
  394. // Profiling task
  395. GE_CHK_STATUS_RET(InsertProfilingTaskAfter(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  396. size_t task_list_size_after = task_def_list.size();
  397. // If tasks is reduced
  398. if (task_list_size_after < task_list_size_before) {
  399. REPORT_INNER_ERROR("E19999", "Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task "
  400. "but task num from %zu to %zu, check invalid", op_kernel_lib_name.c_str(), name.c_str(),
  401. type.c_str(), op_id, stream_id, task_list_size_before, task_list_size_after);
  402. GELOGE(FAILED, "[Check][Param] Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task. "
  403. "but task num from %zu to %zu.", op_kernel_lib_name.c_str(), name.c_str(), type.c_str(),
  404. op_id, stream_id, task_list_size_before, task_list_size_after);
  405. return FAILED;
  406. }
  407. // Reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  408. for (size_t idx = task_list_size_before; idx < task_list_size_after; ++idx) {
  409. task_def_list[idx].set_stream_id(static_cast<uint32_t>(stream_id));
  410. op_name_map[idx] = name;
  411. TaskDef *task_def_ptr = &task_def_list[idx];
  412. GE_CHECK_NOTNULL(task_def_ptr);
  413. // Set opsKernelInfoStorePtr for hccl which will be use in DistributeTask and InitTaskInfo
  414. if (op_kernel_lib_name == kKernelInfoNameHccl) {
  415. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(kernel_info_store.get()));
  416. }
  417. }
  418. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task finished, generate %zu task(s).",
  419. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id,
  420. task_list_size_after - task_list_size_before);
  421. }
  422. GE_TIMESTAMP_CALLNUM_EVENT_END(GenerateTask, "GraphBuild::GenerateTask");
  423. return SUCCESS;
  424. }
  425. Status TaskGenerator::GenerateTaskForFusionNode(FusionTaskInfo &fusion_task_info,
  426. std::map<int64_t, std::vector<NodePtr>> &fusion_nodes,
  427. std::unordered_set<Node *> &fusion_nodes_seen) {
  428. Status ret = SUCCESS;
  429. int64_t group_key;
  430. auto &run_context = fusion_task_info.run_context;
  431. auto &graph = fusion_task_info.graph;
  432. auto &node = fusion_task_info.node;
  433. auto &fusion_op_desc = fusion_task_info.fusion_op_desc;
  434. auto &node_index = fusion_task_info.node_index;
  435. const auto &ops_kernel_manager = fusion_task_info.ops_kernel_manager;
  436. auto &task_def_list = fusion_task_info.task_def_list;
  437. auto &op_name_map = fusion_task_info.op_name_map;
  438. auto &profiling_point = fusion_task_info.profiling_point;
  439. auto &all_reduce_nodes = fusion_task_info.all_reduce_nodes;
  440. // If op_desc have this attr, call nodes with same group key in a stream together
  441. if (ge::AttrUtils::GetInt(fusion_op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key) &&
  442. (fusion_nodes_seen.count(node.get()) == 0)) {
  443. GELOGI("Fusion: start fusion group index[%ld], nodes size[%zu].", group_key, fusion_nodes[group_key].size());
  444. for (auto &fusion_node : fusion_nodes[group_key]) {
  445. OpDescPtr op_desc = fusion_node->GetOpDesc();
  446. UpdateOpIsVarAttr(op_desc, graph->GetSessionID());
  447. std::string fusion_node_name = fusion_node->GetName();
  448. std::string fusion_node_type = fusion_node->GetType();
  449. std::string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  450. if (op_kernel_lib_name.empty()) {
  451. GELOGI("Fusion: fusion_node[name:%s(%s)] task no need to generate task.", fusion_node_name.c_str(),
  452. fusion_node_type.c_str());
  453. continue;
  454. }
  455. bool attr_notask = false;
  456. GE_IF_BOOL_EXEC(ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask,
  457. GELOGI("Fusion: fusion_node[name:%s, type:%s] does not need to generate task.",
  458. fusion_node_name.c_str(), fusion_node_type.c_str());
  459. continue);
  460. size_t task_list_size_before = task_def_list.size();
  461. OpsKernelInfoStorePtr kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(op_kernel_lib_name);
  462. if (kernel_info_store == nullptr) {
  463. REPORT_INNER_ERROR("E19999", "Get ops kernel info store failed for op:%s(%s), op_kernel_name:%s",
  464. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  465. op_kernel_lib_name.c_str());
  466. GELOGE(INTERNAL_ERROR, "[Call][GetOpsKernelInfoStore] Fusion: No ops kernel store or ops kernel builder found."
  467. " fusion_node:%s(%s), op_kernel_lib_name=%s.",
  468. fusion_node_name.c_str(), fusion_node_type.c_str(), op_kernel_lib_name.c_str());
  469. return INTERNAL_ERROR;
  470. }
  471. ret = UpdateAnchorStatus(fusion_node);
  472. if (ret != SUCCESS) {
  473. GELOGE(ret, "[Update][AnchorStatus] fusion_node:%s(%s) failed", fusion_node_name.c_str(),
  474. fusion_node_type.c_str());
  475. return ret;
  476. }
  477. int64_t op_id = op_desc->GetId();
  478. int64_t stream_id = op_desc->GetStreamId();
  479. if (stream_id < 0 || stream_id >= (int64_t)run_context.graphStreamList.size()) {
  480. REPORT_INNER_ERROR("E19999", "Fusion: fusion_node[name:%s(%s), id:%ld] stream id is invalid, "
  481. "stream list size=%zu", fusion_node_name.c_str(), fusion_node_type.c_str(),
  482. op_id, run_context.graphStreamList.size());
  483. GELOGE(INTERNAL_ERROR, "[Check][Param] Fusion: fusion_node[name:%s(%s), id:%ld] stream id is invalid, "
  484. "stream list size=%zu", fusion_node_name.c_str(), fusion_node_type.c_str(), op_id,
  485. run_context.graphStreamList.size());
  486. return INTERNAL_ERROR;
  487. }
  488. // profiling task
  489. (void)InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list);
  490. run_context.stream = run_context.graphStreamList[stream_id];
  491. GELOGI("Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld] task.",
  492. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  493. ret = OpsKernelBuilderManager::Instance().GenerateTask(*fusion_node, run_context, task_def_list);
  494. if (ret != SUCCESS) {
  495. REPORT_CALL_ERROR("E19999", " Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  496. "id:%ld, stream_id:%ld] task failed", op_kernel_lib_name.c_str(),
  497. fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  498. GELOGE(ret, "[Generate][Task] Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  499. "id:%ld, stream_id:%ld] task failed.",
  500. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  501. return ret;
  502. }
  503. // profiling task
  504. (void)InsertProfilingTaskAfter(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list);
  505. size_t task_list_size_after = task_def_list.size();
  506. // if tasks is reduced
  507. if (task_list_size_after < task_list_size_before) {
  508. REPORT_INNER_ERROR("E19999", "InsertProfilingTask for fusion_node:[fusion_node_name:%s(%s), kernel_name:%s"
  509. "id:%ld, stream_id:%ld] task, but task num from %zu to %zu, check invalid",
  510. fusion_node_name.c_str(), fusion_node_type.c_str(), op_kernel_lib_name.c_str(),
  511. op_id, stream_id, task_list_size_before, task_list_size_after);
  512. GELOGE(FAILED, "[Check][Param] Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  513. "id:%ld, stream_id:%ld] task. but task num from %zu to %zu.",
  514. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  515. task_list_size_before, task_list_size_after);
  516. return FAILED;
  517. }
  518. // reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  519. void *ops_kernel_info_store_ptr = kernel_info_store.get();
  520. for (size_t idx = task_list_size_before; idx < task_list_size_after; ++idx) {
  521. task_def_list[idx].set_stream_id(static_cast<uint32_t>(stream_id));
  522. op_name_map[idx] = fusion_node_name;
  523. // set opsKernelInfoStorePtr and op_index, the two fields be use in DistributeTask and InitTaskInfo
  524. TaskDef *task_def_ptr = &task_def_list[idx];
  525. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(ops_kernel_info_store_ptr));
  526. }
  527. GELOGI("Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld]"
  528. " task finished, generate %zu task(s).",
  529. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  530. task_list_size_after - task_list_size_before);
  531. // record nodes which have call generate task successfully
  532. fusion_nodes_seen.insert(fusion_node.get());
  533. node_index++;
  534. }
  535. }
  536. // without tag or has been seen, skip directly
  537. return ret;
  538. }
  539. Status TaskGenerator::UpdateAnchorStatusForFfts(const NodePtr &node) {
  540. GELOGD("Start UpdateAnchorStatusForFfts for %s.", node->GetName().c_str());
  541. if (!node->GetOpDesc()->GetSubgraphInstanceNames().empty()) {
  542. for (size_t i = 0; i < node->GetOpDesc()->GetSubgraphInstanceNames().size(); ++i) {
  543. auto sub_graph = NodeUtils::GetSubgraph(*node, i);
  544. GE_CHECK_NOTNULL(sub_graph);
  545. GELOGD("Start update anchor status for %s.", sub_graph->GetName().c_str());
  546. for (auto &ffts_node : sub_graph->GetDirectNode()) {
  547. GE_CHK_STATUS_RET(UpdateAnchorStatus(ffts_node), "[Call][UpdateAnchorStatus] node:%s(%s) failed",
  548. ffts_node->GetName().c_str(), ffts_node->GetType().c_str());
  549. }
  550. }
  551. }
  552. return SUCCESS;
  553. }
  554. Status TaskGenerator::UpdateAnchorStatus(const NodePtr &node) {
  555. GELOGD("Start UpdateAnchorStatus for %s.", node->GetName().c_str());
  556. if (NodeUtils::SetAllAnchorStatus(node) != GRAPH_SUCCESS) {
  557. REPORT_CALL_ERROR("E19999", "SetAllAnchorStatus fail for op:%s(%s)",
  558. node->GetName().c_str(), node->GetType().c_str());
  559. GELOGE(INTERNAL_ERROR, "[Set][AllAnchorStatus] failed, op:%s(%s)",
  560. node->GetName().c_str(), node->GetType().c_str());
  561. return INTERNAL_ERROR;
  562. }
  563. for (auto &anchor : node->GetAllInDataAnchors()) {
  564. auto peer_anchor = anchor->GetPeerOutAnchor();
  565. if (peer_anchor == nullptr) {
  566. if (AnchorUtils::SetStatus(anchor, ANCHOR_SUSPEND) != GRAPH_SUCCESS) {
  567. REPORT_CALL_ERROR("E19999", "Set in peer anchor status fail for op:%s(%s), anchor_index:%d",
  568. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  569. GELOGE(INTERNAL_ERROR, "[Set][Status] failed, op:%s(%s), anchor_index:%d",
  570. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  571. return INTERNAL_ERROR;
  572. }
  573. continue;
  574. }
  575. std::string const_type;
  576. bool is_const = NodeUtils::GetConstOpType(peer_anchor->GetOwnerNode(), const_type);
  577. if (is_const && (const_type == CONSTANT)) {
  578. if (AnchorUtils::SetStatus(anchor, ANCHOR_CONST) != GRAPH_SUCCESS) {
  579. REPORT_CALL_ERROR("E19999", "Set in anchor CONST status fail for op:%s(%s), anchor_index:%d",
  580. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  581. GELOGE(INTERNAL_ERROR, "[Set][Status] failed. op:%s(%s), anchor_index:%d.",
  582. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  583. return INTERNAL_ERROR;
  584. }
  585. } else {
  586. if (AnchorUtils::SetStatus(anchor, ANCHOR_DATA) != GRAPH_SUCCESS) {
  587. REPORT_CALL_ERROR("E19999", "Set in anchor DATA status fail for op:%s(%s), anchor_index:%d",
  588. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  589. GELOGE(INTERNAL_ERROR, "[Set][Status] failed, op:%s(%s), anchor_index:%d.",
  590. node->GetName().c_str(), node->GetType().c_str(), anchor->GetIdx());
  591. return INTERNAL_ERROR;
  592. }
  593. }
  594. }
  595. return SUCCESS;
  596. }
  597. Status TaskGenerator::MarkNodeAndSetIndex(ComputeGraphPtr &graph) {
  598. auto ge_lib = GELib::GetInstance();
  599. if ((ge_lib == nullptr) || !ge_lib->InitFlag()) {
  600. REPORT_INNER_ERROR("E19999", "Check GELib instance not init before");
  601. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Check][Param] GE is not initialized or is finalized.");
  602. return GE_CLI_GE_NOT_INITIALIZED;
  603. }
  604. const auto all_nodes = graph->GetNodes(graph->GetGraphUnknownFlag());
  605. if (all_nodes.empty()) {
  606. REPORT_INNER_ERROR("E19999", "Check param all_nodes empty in graph:%s",
  607. graph->GetName().c_str());
  608. GELOGE(GE_GRAPH_GRAPH_NODE_NULL, "[Check][Param] Graph's node is empty, graph:%s", graph->GetName().c_str());
  609. return GE_GRAPH_GRAPH_NODE_NULL;
  610. }
  611. map<int64_t, vector<OpDescPtr>> all_stream_ops;
  612. for (auto &node : all_nodes) {
  613. OpDescPtr op_desc = node->GetOpDesc();
  614. GE_CHECK_NOTNULL(op_desc);
  615. // Reset op kernel lib name
  616. if (op_desc->GetOpKernelLibName().empty()) {
  617. (void)ge_lib->DNNEngineManagerObj().GetDNNEngineName(node);
  618. }
  619. (void)op_desc->DelAttr(kIsFirstNode);
  620. (void)op_desc->DelAttr(kIsLastNode);
  621. all_stream_ops[op_desc->GetStreamId()].emplace_back(op_desc);
  622. }
  623. bool is_single_stream = all_stream_ops.size() == 1;
  624. for (const auto &stream_ops : all_stream_ops) {
  625. Status status = MarkFirstAndLastOps(stream_ops.second, is_single_stream);
  626. if (status != SUCCESS) {
  627. GELOGE(status, "[Mark][FirstAndLastOps] failed, graph:%s.", graph->GetName().c_str());
  628. return status;
  629. }
  630. }
  631. return SUCCESS;
  632. }
  633. Status TaskGenerator::MarkFirstAndLastOps(const vector<OpDescPtr> &ops, bool is_single_stream) const {
  634. vector<vector<OpDescPtr>> continuous_op_lists(1);
  635. const set<string> separator_types(
  636. {LABELSET, LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX, STREAMSWITCH, STREAMSWITCHN});
  637. for (auto &op_desc : ops) {
  638. bool attr_notask = false;
  639. if (ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask) {
  640. continue;
  641. }
  642. string op_type = op_desc->GetType();
  643. if ((!is_single_stream && !op_desc->GetSubgraphInstanceNames().empty()) || separator_types.count(op_type) != 0) {
  644. continuous_op_lists.emplace_back(vector<OpDescPtr>());
  645. } else {
  646. continuous_op_lists.back().emplace_back(op_desc);
  647. }
  648. }
  649. GELOGD("Number of continuous node lists is %zu.", continuous_op_lists.size());
  650. for (const auto &continuous_ops : continuous_op_lists) {
  651. map<string, std::pair<OpDescPtr, OpDescPtr>> first_and_last_ops;
  652. for (auto &op_desc : continuous_ops) {
  653. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  654. if (op_kernel_lib_name.empty()) {
  655. REPORT_INNER_ERROR("E19999", "Get ops kernel info store failed for op:%s(%s), op_kernel_name:%s",
  656. op_desc->GetName().c_str(), op_desc->GetType().c_str(), op_kernel_lib_name.c_str());
  657. GELOGE(INTERNAL_ERROR, "[Check][Param] node:%s(%s) get op kernel lib failed.", op_desc->GetName().c_str(),
  658. op_desc->GetType().c_str());
  659. return INTERNAL_ERROR;
  660. }
  661. auto it = first_and_last_ops.find(op_kernel_lib_name);
  662. if (it == first_and_last_ops.end()) {
  663. first_and_last_ops.emplace(op_kernel_lib_name, std::make_pair(op_desc, op_desc));
  664. } else {
  665. it->second.second = op_desc;
  666. }
  667. }
  668. for (auto &it : first_and_last_ops) {
  669. auto &op_pair = it.second;
  670. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.first, kIsFirstNode, true),
  671. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", kIsFirstNode,
  672. op_pair.first->GetName().c_str(), op_pair.first->GetType().c_str());
  673. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", kIsFirstNode,
  674. op_pair.first->GetName().c_str(), op_pair.first->GetType().c_str());
  675. return FAILED);
  676. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.second, kIsLastNode, true),
  677. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", kIsLastNode,
  678. op_pair.second->GetName().c_str(), op_pair.second->GetType().c_str());
  679. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", kIsLastNode,
  680. op_pair.second->GetName().c_str(), op_pair.second->GetType().c_str());
  681. return FAILED);
  682. }
  683. }
  684. return SUCCESS;
  685. }
  686. Status TaskGenerator::AutoFindFpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point) const {
  687. GELOGI("Start AutoFindFpOpIndex");
  688. OpDescPtr fp_op_desc = nullptr;
  689. uint32_t current_idx = 0;
  690. uint32_t first_fp = 0;
  691. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  692. OpDescPtr op_desc = node->GetOpDesc();
  693. GE_CHECK_NOTNULL(op_desc);
  694. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  695. if (op_kernel_lib_name.empty()) {
  696. continue;
  697. }
  698. auto type = op_desc->GetType();
  699. std::string original_type;
  700. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, original_type);
  701. if (kFpNodeTypes.find(type) != kFpNodeTypes.end() || kFpNodeTypes.find(original_type) != kFpNodeTypes.end()) {
  702. auto out_anchor = node->GetOutDataAnchor(0);
  703. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  704. GE_CHECK_NOTNULL(peer_in_anchor);
  705. auto in_node_desc = peer_in_anchor->GetOwnerNode()->GetOpDesc();
  706. GE_CHECK_NOTNULL(in_node_desc);
  707. if (fp_op_desc == nullptr || ((in_node_desc->GetId()) < (fp_op_desc->GetId()))) {
  708. fp_op_desc = in_node_desc;
  709. }
  710. }
  711. break;
  712. }
  713. }
  714. if (fp_op_desc == nullptr) {
  715. GELOGW("not find fp_op_desc.");
  716. return SUCCESS;
  717. }
  718. GELOGI("Find fp_op_desc is %s, id is %ld", fp_op_desc->GetName().c_str(), fp_op_desc->GetId());
  719. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  720. OpDescPtr op_desc = node->GetOpDesc();
  721. GE_CHECK_NOTNULL(op_desc);
  722. current_idx++;
  723. if (op_desc->GetName() == fp_op_desc->GetName()) {
  724. first_fp = current_idx;
  725. GELOGI("First fp name is %s, idx is %u", op_desc->GetName().c_str(), first_fp);
  726. break;
  727. }
  728. }
  729. profiling_point.fp_index = first_fp;
  730. return SUCCESS;
  731. }
  732. Status TaskGenerator::AutoFindBpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  733. vector<uint32_t> &all_reduce_nodes) const {
  734. GELOGI("Start AutoFindBpOpIndex");
  735. NodePtr bp_node = nullptr;
  736. uint32_t current_idx = 0;
  737. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  738. OpDescPtr op_desc = node->GetOpDesc();
  739. GE_CHECK_NOTNULL(op_desc);
  740. current_idx++;
  741. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  742. if (op_kernel_lib_name.empty()) {
  743. continue;
  744. }
  745. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  746. bp_node = node;
  747. all_reduce_nodes.emplace_back(current_idx);
  748. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  749. }
  750. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  751. if (bp_node == nullptr) {
  752. bp_node = node;
  753. }
  754. }
  755. if (graph->GetNeedIteration()) {
  756. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGNADD) {
  757. profiling_point.end_index.insert(current_idx);
  758. GELOGI("Iter end name %s, idx %u, from Node_Output_IteratorCtrl_StreamSwitch_StreamActive",
  759. op_desc->GetName().c_str(), current_idx);
  760. }
  761. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGN) {
  762. profiling_point.end_index.insert(current_idx);
  763. GELOGI("Iter end name %s, idx %u, from FlowCtrl_LoopCond_ASSIGN",
  764. op_desc->GetName().c_str(), current_idx);
  765. }
  766. } else {
  767. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  768. profiling_point.end_index.insert(current_idx);
  769. GELOGI("Iter end name %s, idx %u, from NETOUTPUT", op_desc->GetName().c_str(), current_idx);
  770. }
  771. }
  772. }
  773. if (bp_node == nullptr) {
  774. GELOGW("not find bp_node.");
  775. return SUCCESS;
  776. }
  777. return FindLastBpFromBpNode(graph, bp_node, profiling_point.bp_index);
  778. }
  779. Status TaskGenerator::FindLastBpFromBpNode(const ComputeGraphPtr &graph, const NodePtr &target_node,
  780. uint32_t &bp_index) const {
  781. bp_index = 0;
  782. auto target_desc = target_node->GetOpDesc();
  783. GE_CHECK_NOTNULL(target_desc);
  784. OpDescPtr bp_op_desc = nullptr;
  785. for (auto &in_node : target_node->GetInAllNodes()) {
  786. GE_CHECK_NOTNULL(in_node);
  787. auto in_node_desc = in_node->GetOpDesc();
  788. GE_CHECK_NOTNULL(in_node_desc);
  789. if ((bp_op_desc == nullptr || (in_node_desc->GetId() > bp_op_desc->GetId())) &&
  790. (in_node_desc->GetStreamId() == target_desc->GetStreamId())){
  791. bp_op_desc = in_node_desc;
  792. }
  793. }
  794. if (bp_op_desc == nullptr) {
  795. GELOGI("Did not find bp node.");
  796. return SUCCESS;
  797. }
  798. uint32_t current_idx = 0;
  799. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  800. OpDescPtr op_desc = node->GetOpDesc();
  801. GE_CHECK_NOTNULL(op_desc);
  802. current_idx++;
  803. if (op_desc->GetName() == bp_op_desc->GetName()) {
  804. bp_index = current_idx;
  805. GELOGI("Find bp name %s, idx %u", op_desc->GetName().c_str(), bp_index);
  806. break;
  807. }
  808. }
  809. GELOGI("Last bp node[%s], type[%s], index[%u], stream id[%ld]", bp_op_desc->GetName().c_str(),
  810. bp_op_desc->GetType().c_str(), bp_index, bp_op_desc->GetStreamId());
  811. return SUCCESS;
  812. }
  813. Status TaskGenerator::FindFpOfEnv(const ComputeGraphPtr &graph, const std::string &fp_point_str,
  814. ProfilingPoint &profiling_point) const {
  815. GELOGI("Start FindFpOfEnv");
  816. uint32_t current_idx = 0;
  817. uint32_t first_fp = 0;
  818. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  819. OpDescPtr op_desc = node->GetOpDesc();
  820. GE_CHECK_NOTNULL(node->GetOpDesc());
  821. current_idx++;
  822. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  823. if (op_kernel_lib_name.empty()) {
  824. continue;
  825. }
  826. if (first_fp == 0 && IsProfPoint(op_desc, fp_point_str)) {
  827. first_fp = current_idx;
  828. GELOGI("First fp name from env is %s, idx %u", op_desc->GetName().c_str(), first_fp);
  829. }
  830. }
  831. profiling_point.fp_index = first_fp;
  832. return SUCCESS;
  833. }
  834. Status TaskGenerator::FindBpOfEnv(const ComputeGraphPtr &graph, const std::string &bp_point_str,
  835. ProfilingPoint &profiling_point, vector<uint32_t> &all_reduce_nodes) const {
  836. GELOGI("Start FindBpOfEnv");
  837. uint32_t current_idx = 0;
  838. uint32_t last_bp = 0;
  839. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  840. OpDescPtr op_desc = node->GetOpDesc();
  841. GE_CHECK_NOTNULL(node->GetOpDesc());
  842. current_idx++;
  843. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  844. if (op_kernel_lib_name.empty()) {
  845. continue;
  846. }
  847. if (graph->GetNeedIteration()) {
  848. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGNADD) {
  849. profiling_point.end_index.insert(current_idx);
  850. GELOGI("Iter end name %s, idx %u, from Node_Output_IteratorCtrl_StreamSwitch_StreamActive",
  851. op_desc->GetName().c_str(), current_idx);
  852. }
  853. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGN) {
  854. profiling_point.end_index.insert(current_idx);
  855. GELOGI("Iter end name %s, idx %u, from FlowCtrl_LoopCond_ASSIGN",
  856. op_desc->GetName().c_str(), current_idx);
  857. }
  858. } else {
  859. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  860. profiling_point.end_index.insert(current_idx);
  861. GELOGI("Iter end name %s, idx %u, from NETOUTPUT", op_desc->GetName().c_str(), current_idx);
  862. }
  863. }
  864. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  865. all_reduce_nodes.emplace_back(current_idx);
  866. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  867. }
  868. if (IsProfPoint(op_desc, bp_point_str)) {
  869. last_bp = current_idx;
  870. GELOGI("Last bp name from env is %s, idx %u", op_desc->GetName().c_str(), last_bp);
  871. }
  872. }
  873. profiling_point.bp_index = last_bp;
  874. return SUCCESS;
  875. }
  876. Status TaskGenerator::GetFpBpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  877. vector<uint32_t> &all_reduce_nodes, std::string &fp_point_str,
  878. std::string &bp_point_str) const {
  879. ProfilingManager::Instance().GetFpBpPoint(fp_point_str, bp_point_str);
  880. Status ret = SUCCESS;
  881. if (fp_point_str.empty()) {
  882. ret = AutoFindFpOpIndex(graph, profiling_point);
  883. if (ret != SUCCESS) {
  884. GELOGW("First forward profiling op_index not set and FindFpOpIndex failed.");
  885. return FAILED;
  886. }
  887. }
  888. if (bp_point_str.empty()) {
  889. ret = AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes);
  890. if (ret != SUCCESS) {
  891. GELOGW("Last backward profiling op_index not set and FindBpOpIndex failed.");
  892. return FAILED;
  893. }
  894. }
  895. return SUCCESS;
  896. }
  897. Status TaskGenerator::FindProfilingNodeIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  898. std::vector<uint32_t> &all_reduce_nodes) {
  899. return FindProfilingTaskIndex(graph, profiling_point, all_reduce_nodes);
  900. }
  901. Status TaskGenerator::FindProfilingTaskIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  902. vector<uint32_t> &all_reduce_nodes) const {
  903. GE_CHECK_NOTNULL(graph);
  904. const char *profiling_mode = std::getenv(kProfilingMode);
  905. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  906. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  907. if (!is_profiling) {
  908. GELOGD("Profiling is not open.");
  909. return SUCCESS;
  910. }
  911. // subgraph of dynamic graph no need to find index, has been found in parent graph
  912. if (IsSubGraphOfDynamicGraph(graph)) {
  913. GELOGI("Graph[%s] is subgraph of dynamic graph, no need to find index.", graph->GetName().c_str());
  914. return SUCCESS;
  915. }
  916. GELOGI("Start get FP/BP index.");
  917. std::string fp_point_str;
  918. std::string bp_point_str;
  919. Status ret = GetFpBpIndex(graph, profiling_point, all_reduce_nodes, fp_point_str, bp_point_str);
  920. if (ret != SUCCESS) {
  921. GELOGW("Get FP_POINT BP_POINT failed.");
  922. return SUCCESS;
  923. }
  924. GELOGI("fp_point_str:%s, bp_point_str:%s.", fp_point_str.c_str(), bp_point_str.c_str());
  925. if (!fp_point_str.empty()) {
  926. ret = FindFpOfEnv(graph, fp_point_str, profiling_point);
  927. if (ret != SUCCESS) {
  928. GELOGW("First backward profiling op name set but FindFpOfEnv failed.");
  929. return SUCCESS;
  930. }
  931. }
  932. if (!bp_point_str.empty()) {
  933. ret = FindBpOfEnv(graph, bp_point_str, profiling_point, all_reduce_nodes);
  934. if (ret != SUCCESS) {
  935. GELOGW("Last backward profiling op name set but FindBpOfEnv failed.");
  936. return SUCCESS;
  937. }
  938. }
  939. bool train_graph = graph->GetNeedIteration();
  940. if (profiling_point.fp_index == 0 && train_graph) {
  941. GELOGW("First forward op name can't be found in graph for training trace.");
  942. }
  943. if (profiling_point.bp_index == 0 && train_graph) {
  944. GELOGW("Last backward op name can't be found in graph for training trace.");
  945. }
  946. return SUCCESS;
  947. }
  948. Status TaskGenerator::InsertProfilingArTaskBefore(const OpDescPtr &op_desc, std::vector<uint32_t> &all_reduce_nodes,
  949. uint32_t node_index, std::vector<domi::TaskDef> &task_def_list,
  950. bool is_insert_bp_profiling_task) {
  951. bool is_insert_all_reduce_task = false;
  952. int64_t ar_log_id = 0xFFFF;
  953. if (is_insert_bp_profiling_task) {
  954. (void)ge::AttrUtils::GetInt(op_desc, ATTR_NAME_INSERT_PROFILILNG_TASK_LOG_ID, ar_log_id);
  955. is_insert_all_reduce_task = true;
  956. }
  957. if (!is_insert_all_reduce_task) {
  958. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  959. if (all_reduce_nodes[i] == node_index) {
  960. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  961. REPORT_INNER_ERROR("E19999", "Multiply result is out of range when calc profiling ar log id "
  962. "for node:%s(%s)", op_desc->GetName().c_str(), op_desc->GetType().c_str());
  963. GELOGE(FAILED, "[Check][Param] Multiply result is out of range. node:%s(%s)",
  964. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  965. return FAILED);
  966. ar_log_id = i * kProfilingArStep + kProfilingArStartLogid;
  967. is_insert_all_reduce_task = true;
  968. break;
  969. }
  970. }
  971. }
  972. if (is_insert_all_reduce_task) {
  973. GELOGI("The start allreduce operator is %s, idx %u, log_id %ld", op_desc->GetName().c_str(), node_index, ar_log_id);
  974. TaskDef ar_task_def;
  975. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  976. ar_task_def.set_stream_id(op_desc->GetStreamId());
  977. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  978. if (ar_log_def != nullptr) {
  979. ar_log_def->set_logid(ar_log_id);
  980. ar_log_def->set_notify(false);
  981. }
  982. task_def_list.push_back(ar_task_def);
  983. }
  984. return SUCCESS;
  985. }
  986. Status TaskGenerator::InsertProfilingTaskBefore(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  987. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  988. vector<domi::TaskDef> &task_def_list) {
  989. const char *profiling_mode = std::getenv(kProfilingMode);
  990. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  991. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  992. bool is_insert_fp_profiling_task = false;
  993. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_FP_PROFILILNG_TASK, is_insert_fp_profiling_task);
  994. bool is_insert_bp_profiling_task = false;
  995. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_BP_PROFILILNG_TASK, is_insert_bp_profiling_task);
  996. bool no_insert_profiling_task = ((profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  997. (profiling_point.end_index.empty())) &&
  998. (!(is_insert_fp_profiling_task || is_insert_bp_profiling_task));
  999. if (!is_profiling || no_insert_profiling_task) {
  1000. return SUCCESS;
  1001. }
  1002. GELOGD("Insert fp profiling task: %d, insert bp profiling task: %d, fp index: %u, bp index: %u, end index size: %zu",
  1003. is_insert_fp_profiling_task, is_insert_bp_profiling_task, profiling_point.fp_index, profiling_point.bp_index,
  1004. profiling_point.end_index.size());
  1005. if ((profiling_point.fp_index == node_index) || is_insert_fp_profiling_task) {
  1006. uint64_t jobid_log_id = ge::GetContext().TraceId();
  1007. GELOGI("The first FP operator is %s, idx %u, job_id %lu", op_desc->GetName().c_str(), node_index, jobid_log_id);
  1008. TaskDef job_task_def;
  1009. job_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1010. job_task_def.set_stream_id(op_desc->GetStreamId());
  1011. LogTimeStampDef *job_log_def = job_task_def.mutable_log_timestamp();
  1012. if (job_log_def != nullptr) {
  1013. job_log_def->set_logid(jobid_log_id);
  1014. job_log_def->set_notify(false);
  1015. }
  1016. task_def_list.emplace_back(job_task_def);
  1017. TaskDef fp_task_def;
  1018. fp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1019. fp_task_def.set_stream_id(op_desc->GetStreamId());
  1020. LogTimeStampDef *fp_log_def = fp_task_def.mutable_log_timestamp();
  1021. if (fp_log_def != nullptr) {
  1022. fp_log_def->set_logid(kProfilingFpStartLogid);
  1023. fp_log_def->set_notify(false);
  1024. }
  1025. task_def_list.emplace_back(fp_task_def);
  1026. }
  1027. bool is_all_reduce = (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE);
  1028. if (is_all_reduce) {
  1029. (void)InsertProfilingArTaskBefore(op_desc, all_reduce_nodes, node_index,
  1030. task_def_list, is_insert_bp_profiling_task);
  1031. }
  1032. return SUCCESS;
  1033. }
  1034. Status TaskGenerator::InsertProfilingArTaskAfter(const OpDescPtr &op_desc, std::vector<uint32_t> &all_reduce_nodes,
  1035. uint32_t node_index, std::vector<domi::TaskDef> &task_def_list,
  1036. bool is_insert_bp_profiling_task) {
  1037. bool is_insert_all_reduce_task = false;
  1038. int64_t ar_log_id = 0xFFFF;
  1039. if (is_insert_bp_profiling_task) {
  1040. (void)ge::AttrUtils::GetInt(op_desc, ATTR_NAME_INSERT_PROFILILNG_TASK_LOG_ID, ar_log_id);
  1041. ar_log_id += 1;
  1042. is_insert_all_reduce_task = true;
  1043. }
  1044. if (!is_insert_all_reduce_task) {
  1045. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  1046. if (all_reduce_nodes[i] == node_index) {
  1047. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  1048. REPORT_INNER_ERROR("E19999", "Multiply result is out of range when calc profiling ar log id "
  1049. "for node:%s(%s)", op_desc->GetName().c_str(), op_desc->GetType().c_str());
  1050. GELOGE(FAILED, "[Check][Param] Multiply result is out of range. node:%s(%s)",
  1051. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  1052. return FAILED);
  1053. ar_log_id = i * kProfilingArStep + kProfilingArEndLogid;
  1054. is_insert_all_reduce_task = true;
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. if (is_insert_all_reduce_task) {
  1060. GELOGI("The start allreduce operator is %s, idx %u, log_id %ld", op_desc->GetName().c_str(), node_index, ar_log_id);
  1061. TaskDef ar_task_def;
  1062. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1063. ar_task_def.set_stream_id(op_desc->GetStreamId());
  1064. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  1065. if (ar_log_def != nullptr) {
  1066. ar_log_def->set_logid(ar_log_id);
  1067. ar_log_def->set_notify(false);
  1068. }
  1069. task_def_list.push_back(ar_task_def);
  1070. }
  1071. return SUCCESS;
  1072. }
  1073. Status TaskGenerator::InsertProfilingTaskAfter(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  1074. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  1075. vector<domi::TaskDef> &task_def_list) {
  1076. GE_CHECK_NOTNULL(op_desc);
  1077. const char *profiling_mode = std::getenv(kProfilingMode);
  1078. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  1079. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  1080. bool is_insert_bp_profiling_task = false;
  1081. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_BP_PROFILILNG_TASK, is_insert_bp_profiling_task);
  1082. bool is_insert_end_profiling_task = false;
  1083. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_INSERT_END_PROFILILNG_TASK, is_insert_end_profiling_task);
  1084. bool no_insert_profiling_task = ((profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  1085. (profiling_point.end_index.empty())) &&
  1086. (!(is_insert_bp_profiling_task || is_insert_end_profiling_task));
  1087. if (!is_profiling || no_insert_profiling_task) {
  1088. return SUCCESS;
  1089. }
  1090. GELOGD("Insert bp profiling task: %d, insert end profiling task: %d, fp index: %u, bp index: %u, end index size: %zu",
  1091. is_insert_bp_profiling_task, is_insert_end_profiling_task, profiling_point.fp_index, profiling_point.bp_index,
  1092. profiling_point.end_index.size());
  1093. bool is_all_reduce = (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE);
  1094. if ((profiling_point.bp_index == node_index) || (!is_all_reduce && is_insert_bp_profiling_task)) {
  1095. GELOGI("The last BP operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  1096. TaskDef bp_task_def;
  1097. bp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1098. bp_task_def.set_stream_id(op_desc->GetStreamId());
  1099. LogTimeStampDef *bp_log_def = bp_task_def.mutable_log_timestamp();
  1100. GE_CHECK_NOTNULL(bp_log_def);
  1101. bp_log_def->set_logid(kProfilingBpEndLogid);
  1102. bp_log_def->set_notify(false);
  1103. task_def_list.emplace_back(bp_task_def);
  1104. }
  1105. if (profiling_point.end_index.find(node_index) != profiling_point.end_index.end() ||
  1106. is_insert_end_profiling_task) {
  1107. GELOGI("The iteration end operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  1108. TaskDef end_task_def;
  1109. end_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  1110. end_task_def.set_stream_id(op_desc->GetStreamId());
  1111. LogTimeStampDef *end_log_def = end_task_def.mutable_log_timestamp();
  1112. GE_CHECK_NOTNULL(end_log_def);
  1113. end_log_def->set_logid(kProfilingIterEndLogid);
  1114. end_log_def->set_notify(true);
  1115. task_def_list.emplace_back(end_task_def);
  1116. }
  1117. if (is_all_reduce) {
  1118. (void)InsertProfilingArTaskAfter(op_desc, all_reduce_nodes, node_index,
  1119. task_def_list, is_insert_bp_profiling_task);
  1120. }
  1121. return SUCCESS;
  1122. }
  1123. bool TaskGenerator::IsProfPoint(const OpDescPtr &op, const std::string &name) {
  1124. if (op == nullptr) {
  1125. return false;
  1126. }
  1127. if (op->GetName() == name) {
  1128. return true;
  1129. }
  1130. std::vector<std::string> original_op_names;
  1131. bool ret = AttrUtils::GetListStr(op, ge::ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, original_op_names);
  1132. if (!ret) {
  1133. return false;
  1134. }
  1135. for (auto &origin_name : original_op_names) {
  1136. if (origin_name == name) {
  1137. return true;
  1138. }
  1139. }
  1140. return false;
  1141. }
  1142. Status TaskGenerator::SetUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  1143. GE_CHK_RT_RET(rtStreamCreate(&stream, 0));
  1144. run_context.stream = stream;
  1145. rtError_t rt_ret = rtModelBindStream(run_context.model, stream, 0);
  1146. if (rt_ret != RT_ERROR_NONE) {
  1147. REPORT_CALL_ERROR("E19999", "Call rtModelBindStream failed, ret:0x%X", rt_ret);
  1148. GELOGE(FAILED, "[Call][RtModelBindStream] failed, ret: 0x%X", rt_ret);
  1149. GE_CHK_RT_RET(rtStreamDestroy(stream));
  1150. return FAILED;
  1151. }
  1152. return SUCCESS;
  1153. }
  1154. Status TaskGenerator::DestroyUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  1155. GE_CHK_RT(rtModelUnbindStream(run_context.model, stream));
  1156. GE_CHK_RT_RET(rtStreamDestroy(stream));
  1157. return SUCCESS;
  1158. }
  1159. Status TaskGenerator::SetKnownShapeStream(RunContext &run_context, int64_t stream_id) {
  1160. if (stream_id < 0 || stream_id >= static_cast<int64_t>(run_context.graphStreamList.size())) {
  1161. GELOGE(INTERNAL_ERROR, "[Check][Param] Stream id[%ld] is invalid, stream list size=%zu", stream_id,
  1162. run_context.graphStreamList.size());
  1163. return INTERNAL_ERROR;
  1164. }
  1165. run_context.stream = run_context.graphStreamList[stream_id];
  1166. return SUCCESS;
  1167. }
  1168. } // namespace ge

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