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 41 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "graph/build/task_generator.h"
  17. #include <string>
  18. #include <utility>
  19. #include "common/profiling/profiling_manager.h"
  20. #include "common/types.h"
  21. #include "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 "graph/common/ge_call_wrapper.h"
  31. #include "init/gelib.h"
  32. using domi::LogTimeStampDef;
  33. using domi::ModelTaskDef;
  34. using domi::TaskDef;
  35. using std::map;
  36. using std::set;
  37. using std::string;
  38. using std::vector;
  39. namespace {
  40. const char *const kIsFirstNode = "is_first_node";
  41. const char *const kIsLastNode = "is_last_node";
  42. const char *const kIsInputVar = "INPUT_IS_VAR";
  43. const char *const kIsOutputVar = "OUTPUT_IS_VAR";
  44. const char *const kProfilingMode = "PROFILING_MODE";
  45. const char *const kProfilingFpPoint = "FP_POINT";
  46. const char *const kProfilingBpPoint = "BP_POINT";
  47. const uint32_t kProfilingArStep = 2;
  48. const uint64_t kProfilingFpStartLogid = 1;
  49. const uint64_t kProfilingBpEndLogid = 2;
  50. const uint64_t kProfilingArStartLogid = 3;
  51. const uint64_t kProfilingArEndLogid = 4;
  52. const uint64_t kProfilingIterEndLogid = 255;
  53. const int64_t kHashFactor = 100000;
  54. const int64_t kInvalidGroupId = -1;
  55. } // namespace
  56. namespace ge {
  57. TaskGenerator::TaskGenerator(uint8_t *var_mem_base, uint64_t var_mem_size) {
  58. var_mem_base_ = var_mem_base;
  59. var_mem_size_ = var_mem_size;
  60. }
  61. TaskGenerator::~TaskGenerator() {}
  62. Status TaskGenerator::GetTaskInfo(Model &model, ComputeGraphPtr &graph, uint64_t session_id, RunContext &run_context) {
  63. GELOGI("Begin to Get TaskInfo. session_id=%lu", session_id);
  64. // Check params
  65. if (graph == nullptr) {
  66. GELOGE(PARAM_INVALID, "GetTaskInfo param graph is null. session_id=%lu", session_id);
  67. return PARAM_INVALID;
  68. }
  69. std::vector<TaskDef> task_def_list;
  70. std::map<uint32_t, string> op_name_map;
  71. GE_DUMP(graph, "GenerateTaskBefore");
  72. Status ret = GenerateTask(run_context, graph, task_def_list, op_name_map);
  73. GE_DUMP(graph, "GenerateTaskAfter");
  74. if (ret != SUCCESS) {
  75. GELOGE(ret, "GenerateTask failed. session_id=%lu", session_id);
  76. return ret;
  77. }
  78. // op_name_map used when graph load
  79. graph->SetGraphOpName(op_name_map);
  80. // Set op_name for infer profiling
  81. vector<string> op_name;
  82. for (auto &iter : op_name_map) {
  83. op_name.push_back(iter.second);
  84. }
  85. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListStr(model, ATTR_MODEL_TASK_INDEX_OP_NAME, op_name),
  86. GELOGE(FAILED, "SetListStr failed.");
  87. return FAILED);
  88. GELOGI("GenerateTask Success, task list:%zu, op map:%zu, logic mem base:%p, logic weight base:%p, logic var base:%p",
  89. task_def_list.size(), op_name_map.size(), run_context.dataMemBase, run_context.weightMemBase, var_mem_base_);
  90. // Init and serialize model_task_def
  91. ModelTaskDef model_task_def;
  92. model_task_def.set_memory_size(run_context.dataMemSize);
  93. model_task_def.set_weight_size(run_context.weightMemSize);
  94. for (const TaskDef &task_def_temp : task_def_list) {
  95. TaskDef *task_def = model_task_def.add_task();
  96. if (task_def == nullptr) {
  97. GELOGE(FAILED, "task_def is nullptr.");
  98. return FAILED;
  99. }
  100. *task_def = task_def_temp;
  101. }
  102. ret = AddModelTaskToModel(model_task_def, session_id, model, run_context);
  103. if (ret != SUCCESS) {
  104. GELOGE(ret, "AddModelTaskToModel failed. session_id=%lu", session_id);
  105. return ret;
  106. }
  107. GELOGI("Get TaskInfo success. session_id=%lu", session_id);
  108. return SUCCESS;
  109. }
  110. Status TaskGenerator::AddModelTaskToModel(const ModelTaskDef &model_task_def, uint64_t session_id, ge::Model &model,
  111. RunContext &run_context) {
  112. GE_CHK_BOOL_EXEC(
  113. AttrUtils::SetInt(model, MODEL_ATTR_TASK_GEN_BASE_ADDR, reinterpret_cast<uintptr_t>(run_context.dataMemBase)),
  114. GELOGE(FAILED, "SetInt MODEL_ATTR_TASK_GEN_BASE_ADDR failed.");
  115. return FAILED);
  116. GE_CHK_BOOL_EXEC(
  117. AttrUtils::SetInt(model, MODEL_ATTR_TASK_GEN_WEIGHT_ADDR, reinterpret_cast<uintptr_t>(run_context.weightMemBase)),
  118. GELOGE(FAILED, "SetInt MODEL_ATTR_TASK_GEN_WEIGHT_ADDR failed.");
  119. return FAILED);
  120. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(model, ATTR_MODEL_TASK_GEN_VAR_ADDR, reinterpret_cast<uintptr_t>(var_mem_base_)),
  121. GELOGE(FAILED, "SetInt ATTR_MODEL_TASK_GEN_VAR_ADDR failed.");
  122. return FAILED);
  123. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(model, ATTR_MODEL_VAR_SIZE, var_mem_size_),
  124. GELOGE(FAILED, "SetInt ATTR_MODEL_VAR_SIZE failed.");
  125. return FAILED);
  126. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(model, MODEL_ATTR_SESSION_ID, session_id),
  127. GELOGE(FAILED, "SetInt MODEL_ATTR_SESSION_ID failed.");
  128. return FAILED);
  129. size_t task_size = model_task_def.ByteSizeLong();
  130. ge::Buffer serial_buff(task_size);
  131. if (!model_task_def.SerializePartialToArray(serial_buff.GetData(), static_cast<int>(task_size))) {
  132. GELOGE(FAILED, "model_task_def's serialize failed, model name = %s, task_size=%zu.", model.GetName().c_str(),
  133. task_size);
  134. return FAILED;
  135. }
  136. if (!AttrUtils::SetZeroCopyBytes(model, MODEL_ATTR_TASKS, std::move(serial_buff))) {
  137. GELOGE(FAILED, "Set model task to model failed, model name = %s, task_size=%zu.", model.GetName().c_str(),
  138. task_size);
  139. return FAILED;
  140. }
  141. return SUCCESS;
  142. }
  143. Status TaskGenerator::UpdateOpIsVarAttr(const OpDescPtr &op_desc, uint64_t session_id) {
  144. vector<int64_t> input_offsets = op_desc->GetInputOffset();
  145. GELOGD("Update is var attr, node[name:%s(%s), id:%ld, stream_id:%ld].", op_desc->GetName().c_str(),
  146. op_desc->GetType().c_str(), op_desc->GetId(), op_desc->GetStreamId());
  147. if (!(input_offsets.empty())) {
  148. vector<bool> input_var;
  149. for (int64_t input : input_offsets) {
  150. input_var.push_back(VarManager::Instance(session_id)->IsVarAddr(input));
  151. }
  152. GE_CHK_BOOL_EXEC(AttrUtils::SetListBool(op_desc, kIsInputVar, input_var), GELOGE(FAILED, "SetListBool failed.");
  153. return FAILED);
  154. }
  155. vector<int64_t> output_offsets = op_desc->GetOutputOffset();
  156. if (!(output_offsets.empty())) {
  157. vector<bool> output_var;
  158. for (int64_t output : output_offsets) {
  159. output_var.push_back(VarManager::Instance(session_id)->IsVarAddr(output));
  160. }
  161. GE_CHK_BOOL_EXEC(AttrUtils::SetListBool(op_desc, kIsOutputVar, output_var), GELOGE(FAILED, "SetListBool failed.");
  162. return FAILED);
  163. }
  164. return SUCCESS;
  165. }
  166. Status TaskGenerator::SaveFusionNodes(map<int64_t, std::vector<NodePtr>> &fusion_nodes, ComputeGraphPtr &graph) {
  167. std::map<NodePtr, int64_t> nodes_with_group_attr;
  168. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  169. OpDescPtr op_desc = node->GetOpDesc();
  170. GE_CHECK_NOTNULL(op_desc);
  171. int64_t group_id = kInvalidGroupId;
  172. string name = node->GetName();
  173. string type = node->GetType();
  174. // For fusion ddb pass, task def must be continuous.
  175. // Part1: store
  176. // If op_desc have this tag, store it in the map firstly,
  177. // call the elements in the map GenerateTask at last
  178. // l1 and l2 is for now
  179. if (ge::AttrUtils::GetInt(op_desc, ATTR_NAME_L1_FUSION_GROUP_ID, group_id) ||
  180. ge::AttrUtils::GetInt(op_desc, ATTR_NAME_L2_FUSION_GROUP_ID, group_id)) {
  181. auto stream_id = op_desc->GetStreamId();
  182. auto group_key = group_id + stream_id * kHashFactor;
  183. (void)ge::AttrUtils::SetInt(op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key);
  184. GELOGD("Fusion: store node[name:%s(%s), group id:%ld, group key:%ld, stream_id:%ld] task.", name.c_str(),
  185. type.c_str(), group_id, group_key, op_desc->GetStreamId());
  186. fusion_nodes[group_key].push_back(node);
  187. nodes_with_group_attr.insert({node, group_id});
  188. }
  189. // if node's all in nodes both with same group attr
  190. // and it have no attr or group attr different
  191. // which means bad case, return error
  192. bool call_check = true;
  193. std::unordered_set<int64_t> input_group_ids;
  194. for (const auto &input_node : node->GetInNodes()) {
  195. auto iter = nodes_with_group_attr.find(input_node);
  196. if (iter == nodes_with_group_attr.end()) {
  197. call_check = false;
  198. break;
  199. } else {
  200. input_group_ids.insert(iter->second);
  201. }
  202. }
  203. call_check = (call_check && (input_group_ids.size() == 1));
  204. if (call_check) {
  205. auto input_group_id = *input_group_ids.begin();
  206. if (group_id != input_group_id) {
  207. GELOGW("Fusion: node[name:%s(%s) with group id:%ld and diff from it's input nodes's group id:%ld ",
  208. name.c_str(), type.c_str(), group_id, input_group_id);
  209. }
  210. }
  211. }
  212. GELOGI("Fusion: get fusion group numbers [%zu].", fusion_nodes.size());
  213. return SUCCESS;
  214. }
  215. Status TaskGenerator::GenerateTask(RunContext &run_context, ComputeGraphPtr &graph,
  216. vector<domi::TaskDef> &task_def_list, map<uint32_t, string> &op_name_map) {
  217. GELOGD("Beign to generate task, graph name is %s.", graph->GetName().c_str());
  218. std::shared_ptr<GELib> ge_lib = GELib::GetInstance();
  219. if ((ge_lib == nullptr) || !ge_lib->InitFlag()) {
  220. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GenerateTask failed.");
  221. return GE_CLI_GE_NOT_INITIALIZED;
  222. }
  223. GE_CHK_STATUS_RET(MarkNodeAndSetIndex(graph), "MarkNodeAndSetIndex failed.");
  224. ProfilingPoint profiling_point;
  225. vector<uint32_t> all_reduce_nodes;
  226. GE_CHK_STATUS_RET(FindProfilingTaskIndex(graph, profiling_point, all_reduce_nodes));
  227. const OpsKernelManager &ops_kernel_manager = ge_lib->OpsKernelManagerObj();
  228. GE_TIMESTAMP_CALLNUM_START(GenerateTask);
  229. // map store fusion nodes
  230. map<int64_t, std::vector<NodePtr>> fusion_nodes;
  231. string buffer_optimize = "off_optimize";
  232. (void)ge::GetContext().GetOption(BUFFER_OPTIMIZE, buffer_optimize);
  233. if (buffer_optimize != "off_optimize") {
  234. GE_CHK_STATUS_RET(SaveFusionNodes(fusion_nodes, graph));
  235. }
  236. std::unordered_set<Node *> fusion_nodes_seen;
  237. int64_t group_key;
  238. uint32_t node_index = 0;
  239. rtStream_t stream = nullptr;
  240. bool is_unknown_shape = graph->GetGraphUnknownFlag() || GetContext().GetHostExecFlag();
  241. if (is_unknown_shape) {
  242. GE_CHK_STATUS_RET(SetUnknownShapeStream(run_context, stream), "Set unknown shape stream failed.");
  243. }
  244. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  245. OpDescPtr op_desc = node->GetOpDesc();
  246. GE_CHECK_NOTNULL(op_desc);
  247. node_index++;
  248. string name = node->GetName();
  249. string type = node->GetType();
  250. bool attr_notask = false;
  251. bool get_attr_notask_flag = ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask);
  252. GE_IF_BOOL_EXEC(get_attr_notask_flag && attr_notask,
  253. GELOGI("Node[name:%s, type:%s] does not need to generate task.", name.c_str(), type.c_str());
  254. continue);
  255. GE_CHK_STATUS_RET(UpdateOpIsVarAttr(op_desc, graph->GetSessionID()));
  256. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  257. // For fusion ddb pass, task def must be continuous.
  258. // Part2: Call
  259. auto fusion_task_info =
  260. FusionTaskInfo{run_context, graph, node, op_desc, node_index, ge_lib,
  261. ops_kernel_manager, task_def_list, op_name_map, profiling_point, all_reduce_nodes};
  262. GE_CHK_STATUS_RET(GenerateTaskForFusionNode(fusion_task_info, fusion_nodes, fusion_nodes_seen),
  263. "Call GenerateTaskForFusionNode node:%s(%s) failed", name.c_str(), type.c_str());
  264. // continue directly
  265. if (ge::AttrUtils::GetInt(op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key)) {
  266. GELOGI("Fusion node[name:%s, type:%s] do not need generate task again.", name.c_str(), type.c_str());
  267. continue;
  268. }
  269. if (op_kernel_lib_name.empty()) {
  270. GELOGI("Node[name:%s, type:%s] does not need to generate task.", name.c_str(), type.c_str());
  271. continue;
  272. }
  273. OpsKernelInfoStorePtr kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(op_kernel_lib_name);
  274. if (kernel_info_store == nullptr) {
  275. GELOGE(INTERNAL_ERROR, "No ops kernel store found. node:%s(%s), op_kernel_lib_name=%s.", name.c_str(),
  276. type.c_str(), op_kernel_lib_name.c_str());
  277. return INTERNAL_ERROR;
  278. }
  279. GE_CHK_STATUS_RET(UpdateAnchorStatus(node), "Call UpdateAnchorStatus node:%s(%s) failed", name.c_str(),
  280. type.c_str());
  281. // Profiling task
  282. size_t task_list_size_before = task_def_list.size();
  283. GE_CHK_STATUS_RET(InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  284. int64_t op_id = op_desc->GetId();
  285. // Compatible with dynamic shape scenes, the default is 0
  286. int64_t stream_id = 0;
  287. if (!is_unknown_shape) {
  288. stream_id = op_desc->GetStreamId();
  289. GE_CHK_STATUS_RET(SetKnownShapeStream(run_context, stream_id), "node[name:%s(%s), id:%ld] stream id is invalid.",
  290. name.c_str(), type.c_str(), op_id);
  291. }
  292. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task.", op_kernel_lib_name.c_str(),
  293. name.c_str(), type.c_str(), op_id, stream_id);
  294. GE_TIMESTAMP_RESTART(GenerateTask);
  295. auto ret = kernel_info_store->GenerateTask(*node, run_context, task_def_list);
  296. GE_TIMESTAMP_ADD(GenerateTask);
  297. if (ret != SUCCESS) {
  298. GELOGE(ret, "Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task failed.",
  299. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id);
  300. return ret;
  301. }
  302. // Profiling task
  303. GE_CHK_STATUS_RET(InsertProfilingTaskAfter(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  304. size_t task_list_size_after = task_def_list.size();
  305. // If tasks is reduced
  306. if (task_list_size_after < task_list_size_before) {
  307. GELOGE(FAILED, "Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task. but task num from %zu to %zu.",
  308. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id, task_list_size_before,
  309. task_list_size_after);
  310. return FAILED;
  311. }
  312. // Reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  313. void *ops_kernel_info_store_ptr = kernel_info_store.get();
  314. for (size_t idx = task_list_size_before; idx < task_list_size_after; ++idx) {
  315. task_def_list[idx].set_stream_id(static_cast<uint32_t>(stream_id));
  316. op_name_map[idx] = name;
  317. // Set opsKernelInfoStorePtr and op_index, the two fields be use in DistributeTask and InitTaskInfo
  318. TaskDef *task_def_ptr = &task_def_list[idx];
  319. GE_CHECK_NOTNULL(task_def_ptr);
  320. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(ops_kernel_info_store_ptr));
  321. }
  322. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task finished, generate %zu task(s).",
  323. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id,
  324. task_list_size_after - task_list_size_before);
  325. }
  326. if (is_unknown_shape) {
  327. GE_CHK_STATUS_RET(DestroyUnknownShapeStream(run_context, stream), "Destory unknown shape stream failed.");
  328. }
  329. GE_TIMESTAMP_CALLNUM_EVENT_END(GenerateTask, "GraphBuild::GenerateTask");
  330. return SUCCESS;
  331. }
  332. Status TaskGenerator::GenerateTaskForFusionNode(FusionTaskInfo &fusion_task_info,
  333. std::map<int64_t, std::vector<NodePtr>> &fusion_nodes,
  334. std::unordered_set<Node *> &fusion_nodes_seen) {
  335. Status ret = SUCCESS;
  336. int64_t group_key;
  337. auto &run_context = fusion_task_info.run_context;
  338. auto &graph = fusion_task_info.graph;
  339. auto &node = fusion_task_info.node;
  340. auto &fusion_op_desc = fusion_task_info.fusion_op_desc;
  341. auto &node_index = fusion_task_info.node_index;
  342. const auto &ops_kernel_manager = fusion_task_info.ops_kernel_manager;
  343. auto &task_def_list = fusion_task_info.task_def_list;
  344. auto &op_name_map = fusion_task_info.op_name_map;
  345. auto &profiling_point = fusion_task_info.profiling_point;
  346. auto &all_reduce_nodes = fusion_task_info.all_reduce_nodes;
  347. // If op_desc have this attr, call nodes with same group key in a stream together
  348. if (ge::AttrUtils::GetInt(fusion_op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key) &&
  349. (fusion_nodes_seen.count(node.get()) == 0)) {
  350. GELOGI("Fusion: start fusion group index[%ld], nodes size[%zu].", group_key, fusion_nodes[group_key].size());
  351. for (auto &fusion_node : fusion_nodes[group_key]) {
  352. OpDescPtr op_desc = fusion_node->GetOpDesc();
  353. UpdateOpIsVarAttr(op_desc, graph->GetSessionID());
  354. std::string fusion_node_name = fusion_node->GetName();
  355. std::string fusion_node_type = fusion_node->GetType();
  356. std::string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  357. if (op_kernel_lib_name.empty()) {
  358. GELOGI("Fusion: fusion_node[name:%s(%s)] task no need to generate task.", fusion_node_name.c_str(),
  359. fusion_node_type.c_str());
  360. continue;
  361. }
  362. bool attr_notask = false;
  363. GE_IF_BOOL_EXEC(ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask,
  364. GELOGI("Fusion: fusion_node[name:%s, type:%s] does not need to generate task.",
  365. fusion_node_name.c_str(), fusion_node_type.c_str());
  366. continue);
  367. size_t task_list_size_before = task_def_list.size();
  368. OpsKernelInfoStorePtr kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(op_kernel_lib_name);
  369. if (kernel_info_store == nullptr) {
  370. GELOGE(INTERNAL_ERROR, "Fusion: No ops kernel store found. fusion_node:%s(%s), op_kernel_lib_name=%s.",
  371. fusion_node_name.c_str(), fusion_node_type.c_str(), op_kernel_lib_name.c_str());
  372. return INTERNAL_ERROR;
  373. }
  374. ret = UpdateAnchorStatus(fusion_node);
  375. if (ret != SUCCESS) {
  376. GELOGE(ret, "Fusion: Call UpdateAnchorStatus fusion_node:%s(%s) failed", fusion_node_name.c_str(),
  377. fusion_node_type.c_str());
  378. return ret;
  379. }
  380. int64_t op_id = op_desc->GetId();
  381. int64_t stream_id = op_desc->GetStreamId();
  382. if (stream_id < 0 || stream_id >= (int64_t)run_context.graphStreamList.size()) {
  383. GELOGE(INTERNAL_ERROR, "Fusion: fusion_node[name:%s(%s), id:%ld] stream id is invalid, stream list size=%zu",
  384. fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, run_context.graphStreamList.size());
  385. return INTERNAL_ERROR;
  386. }
  387. // profiling task
  388. (void)InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list);
  389. run_context.stream = run_context.graphStreamList[stream_id];
  390. GELOGI("Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld] task.",
  391. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  392. ret = kernel_info_store->GenerateTask(*fusion_node, run_context, task_def_list);
  393. if (ret != SUCCESS) {
  394. GELOGE(ret,
  395. "Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  396. "id:%ld, stream_id:%ld] task failed.",
  397. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  398. return ret;
  399. }
  400. // profiling task
  401. (void)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. GELOGE(FAILED,
  406. "Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  407. "id:%ld, stream_id:%ld] task. but task num from %zu to %zu.",
  408. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  409. task_list_size_before, task_list_size_after);
  410. return FAILED;
  411. }
  412. // reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  413. void *ops_kernel_info_store_ptr = kernel_info_store.get();
  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] = fusion_node_name;
  417. // set opsKernelInfoStorePtr and op_index, the two fields be use in DistributeTask and InitTaskInfo
  418. TaskDef *task_def_ptr = &task_def_list[idx];
  419. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(ops_kernel_info_store_ptr));
  420. }
  421. GELOGI(
  422. "Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld]"
  423. " task finished, generate %u task(s).",
  424. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  425. task_list_size_after - task_list_size_before);
  426. // record nodes which have call generate task successfully
  427. fusion_nodes_seen.insert(fusion_node.get());
  428. node_index++;
  429. }
  430. }
  431. // without tag or has been seen, skip directly
  432. return ret;
  433. }
  434. Status TaskGenerator::UpdateAnchorStatus(const NodePtr &node) {
  435. if (NodeUtils::SetAllAnchorStatus(node) != GRAPH_SUCCESS) {
  436. GELOGE(INTERNAL_ERROR, "NodeUtils::SetAllAnchorStatus failed.");
  437. return INTERNAL_ERROR;
  438. }
  439. for (auto &anchor : node->GetAllInDataAnchors()) {
  440. auto peer_anchor = anchor->GetPeerOutAnchor();
  441. if (peer_anchor == nullptr) {
  442. if (AnchorUtils::SetStatus(anchor, ANCHOR_SUSPEND) != GRAPH_SUCCESS) {
  443. GELOGE(INTERNAL_ERROR, "AnchorUtils::SetStatus failed.");
  444. return INTERNAL_ERROR;
  445. }
  446. continue;
  447. }
  448. std::string const_type;
  449. bool is_const = NodeUtils::GetConstOpType(peer_anchor->GetOwnerNode(), const_type);
  450. if (is_const && (const_type == CONSTANT)) {
  451. if (AnchorUtils::SetStatus(anchor, ANCHOR_CONST) != GRAPH_SUCCESS) {
  452. GELOGE(INTERNAL_ERROR, "AnchorUtils::SetStatus failed.");
  453. return INTERNAL_ERROR;
  454. }
  455. } else {
  456. if (AnchorUtils::SetStatus(anchor, ANCHOR_DATA) != GRAPH_SUCCESS) {
  457. GELOGE(INTERNAL_ERROR, "AnchorUtils::SetStatus failed.");
  458. return INTERNAL_ERROR;
  459. }
  460. }
  461. }
  462. return SUCCESS;
  463. }
  464. Status TaskGenerator::MarkNodeAndSetIndex(ComputeGraphPtr &graph) {
  465. auto ge_lib = GELib::GetInstance();
  466. if ((ge_lib == nullptr) || !ge_lib->InitFlag()) {
  467. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GE is not initialized or is finalized.");
  468. return GE_CLI_GE_NOT_INITIALIZED;
  469. }
  470. const auto all_nodes = graph->GetNodes(graph->GetGraphUnknownFlag());
  471. if (all_nodes.empty()) {
  472. GELOGE(GE_GRAPH_GRAPH_NODE_NULL, "Graph's node is empty");
  473. return GE_GRAPH_GRAPH_NODE_NULL;
  474. }
  475. int64_t node_index = 0;
  476. for (auto &node : all_nodes) {
  477. OpDescPtr op_desc = node->GetOpDesc();
  478. GE_CHECK_NOTNULL(op_desc);
  479. op_desc->SetId(node_index++);
  480. }
  481. map<int64_t, vector<OpDescPtr>> all_stream_ops;
  482. for (auto &node : all_nodes) {
  483. OpDescPtr op_desc = node->GetOpDesc();
  484. GE_CHECK_NOTNULL(op_desc);
  485. // Reset op kernel lib name
  486. if (op_desc->GetOpKernelLibName().empty()) {
  487. (void)ge_lib->DNNEngineManagerObj().GetDNNEngineName(op_desc);
  488. }
  489. all_stream_ops[op_desc->GetStreamId()].emplace_back(op_desc);
  490. }
  491. bool is_single_stream = all_stream_ops.size() == 1;
  492. for (const auto &stream_ops : all_stream_ops) {
  493. Status status = MarkFirstAndLastOps(stream_ops.second, is_single_stream);
  494. if (status != SUCCESS) {
  495. GELOGE(status, "Mark first and last nodes failed.");
  496. return status;
  497. }
  498. }
  499. return SUCCESS;
  500. }
  501. Status TaskGenerator::MarkFirstAndLastOps(const vector<OpDescPtr> &ops, bool is_single_stream) const {
  502. vector<vector<OpDescPtr>> continuous_op_lists(1);
  503. const set<string> separator_types(
  504. {LABELSET, LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX, STREAMSWITCH, STREAMSWITCHN});
  505. for (auto &op_desc : ops) {
  506. bool attr_notask = false;
  507. if (ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask) {
  508. continue;
  509. }
  510. string op_type = op_desc->GetType();
  511. if (!is_single_stream && (!op_desc->GetSubgraphInstanceNames().empty() || separator_types.count(op_type) != 0)) {
  512. continuous_op_lists.emplace_back(vector<OpDescPtr>());
  513. } else {
  514. continuous_op_lists.back().emplace_back(op_desc);
  515. }
  516. }
  517. GELOGI("Number of continuous node lists is %zu.", continuous_op_lists.size());
  518. for (const auto &continuous_ops : continuous_op_lists) {
  519. map<string, std::pair<OpDescPtr, OpDescPtr>> first_and_last_ops;
  520. for (auto &op_desc : continuous_ops) {
  521. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  522. if (op_kernel_lib_name.empty()) {
  523. GELOGE(INTERNAL_ERROR, "node:%s(%s) get op kernel lib failed.", op_desc->GetName().c_str(),
  524. op_desc->GetType().c_str());
  525. return INTERNAL_ERROR;
  526. }
  527. auto it = first_and_last_ops.find(op_kernel_lib_name);
  528. if (it == first_and_last_ops.end()) {
  529. first_and_last_ops.emplace(op_kernel_lib_name, std::make_pair(op_desc, op_desc));
  530. } else {
  531. it->second.second = op_desc;
  532. }
  533. }
  534. for (auto &it : first_and_last_ops) {
  535. auto &op_pair = it.second;
  536. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.first, kIsFirstNode, true), GELOGE(FAILED, "SetBool failed.");
  537. return FAILED);
  538. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.second, kIsLastNode, true), GELOGE(FAILED, "SetBool failed.");
  539. return FAILED);
  540. }
  541. }
  542. return SUCCESS;
  543. }
  544. Status TaskGenerator::AutoFindFpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point) const {
  545. GELOGI("Start AutoFindFpOpIndex");
  546. OpDescPtr fp_op_desc = nullptr;
  547. uint32_t current_idx = 0;
  548. uint32_t first_fp = 0;
  549. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  550. OpDescPtr op_desc = node->GetOpDesc();
  551. GE_CHECK_NOTNULL(op_desc);
  552. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  553. if (op_kernel_lib_name.empty()) {
  554. continue;
  555. }
  556. if (op_desc->GetType() == GETNEXT || op_desc->GetType() == DATA) {
  557. auto out_anchor = node->GetOutDataAnchor(0);
  558. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  559. GE_CHECK_NOTNULL(peer_in_anchor);
  560. auto in_node_desc = peer_in_anchor->GetOwnerNode()->GetOpDesc();
  561. GE_CHECK_NOTNULL(in_node_desc);
  562. if (fp_op_desc == nullptr || ((in_node_desc->GetId()) < (fp_op_desc->GetId()))) {
  563. fp_op_desc = in_node_desc;
  564. }
  565. }
  566. break;
  567. }
  568. }
  569. if (fp_op_desc == nullptr) {
  570. GELOGW("not find fp_op_desc.");
  571. return SUCCESS;
  572. }
  573. GELOGI("Find fp_op_desc is %s, id is %ld", fp_op_desc->GetName().c_str(), fp_op_desc->GetId());
  574. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  575. OpDescPtr op_desc = node->GetOpDesc();
  576. GE_CHECK_NOTNULL(op_desc);
  577. current_idx++;
  578. if (op_desc->GetName() == fp_op_desc->GetName()) {
  579. first_fp = current_idx;
  580. GELOGI("First fp name is %s, idx is %u", op_desc->GetName().c_str(), first_fp);
  581. break;
  582. }
  583. }
  584. profiling_point.fp_index = first_fp;
  585. return SUCCESS;
  586. }
  587. Status TaskGenerator::AutoFindBpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  588. vector<uint32_t> &all_reduce_nodes) const {
  589. GELOGI("Start AutoFindBpOpIndex");
  590. NodePtr bp_node = nullptr;
  591. uint32_t last_bp = 0;
  592. uint32_t iter_end = 0;
  593. uint32_t current_idx = 0;
  594. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  595. OpDescPtr op_desc = node->GetOpDesc();
  596. GE_CHECK_NOTNULL(op_desc);
  597. current_idx++;
  598. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  599. if (op_kernel_lib_name.empty()) {
  600. continue;
  601. }
  602. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  603. bp_node = node;
  604. all_reduce_nodes.emplace_back(current_idx);
  605. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  606. }
  607. if (op_desc->GetType() == NETOUTPUT) {
  608. if (bp_node == nullptr) {
  609. bp_node = node;
  610. }
  611. iter_end = current_idx;
  612. GELOGI("Iter end name %s, idx %u", op_desc->GetName().c_str(), iter_end);
  613. }
  614. }
  615. profiling_point.end_index = iter_end;
  616. if (bp_node == nullptr) {
  617. GELOGW("not find bp_node.");
  618. return SUCCESS;
  619. }
  620. OpDescPtr bp_op_desc = nullptr;
  621. for (auto &in_anchor : bp_node->GetAllInDataAnchors()) {
  622. auto out_anchor = in_anchor->GetPeerOutAnchor();
  623. if (out_anchor == nullptr || out_anchor->GetOwnerNode() == nullptr) {
  624. continue;
  625. }
  626. auto out_node_desc = out_anchor->GetOwnerNode()->GetOpDesc();
  627. GE_CHECK_NOTNULL(out_node_desc);
  628. if (bp_op_desc == nullptr || ((out_node_desc->GetId()) > (bp_op_desc->GetId()))) {
  629. bp_op_desc = out_node_desc;
  630. }
  631. GELOGI("bp_op_desc is %s, id is %ld", bp_op_desc->GetName().c_str(), bp_op_desc->GetId());
  632. }
  633. GE_CHECK_NOTNULL(bp_op_desc);
  634. current_idx = 0;
  635. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  636. OpDescPtr op_desc = node->GetOpDesc();
  637. GE_CHECK_NOTNULL(op_desc);
  638. current_idx++;
  639. if (op_desc->GetName() == bp_op_desc->GetName()) {
  640. last_bp = current_idx;
  641. GELOGI("First bp name %s, idx %u", op_desc->GetName().c_str(), last_bp);
  642. break;
  643. }
  644. }
  645. profiling_point.bp_index = last_bp;
  646. return SUCCESS;
  647. }
  648. Status TaskGenerator::FindFpOfEnv(const ComputeGraphPtr &graph, const std::string &fp_point_str,
  649. ProfilingPoint &profiling_point) const {
  650. GELOGI("Start FindFpOfEnv");
  651. uint32_t current_idx = 0;
  652. uint32_t first_fp = 0;
  653. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  654. OpDescPtr op_desc = node->GetOpDesc();
  655. GE_CHECK_NOTNULL(node->GetOpDesc());
  656. current_idx++;
  657. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  658. if (op_kernel_lib_name.empty()) {
  659. continue;
  660. }
  661. if (first_fp == 0 && IsProfPoint(op_desc, fp_point_str)) {
  662. first_fp = current_idx;
  663. GELOGI("First fp name from env is %s, idx %u", op_desc->GetName().c_str(), first_fp);
  664. }
  665. }
  666. profiling_point.fp_index = first_fp;
  667. return SUCCESS;
  668. }
  669. Status TaskGenerator::FindBpOfEnv(const ComputeGraphPtr &graph, const std::string &bp_point_str,
  670. ProfilingPoint &profiling_point, vector<uint32_t> &all_reduce_nodes) const {
  671. GELOGI("Start FindBpOfEnv");
  672. uint32_t current_idx = 0;
  673. uint32_t iter_end = 0;
  674. uint32_t last_bp = 0;
  675. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  676. OpDescPtr op_desc = node->GetOpDesc();
  677. GE_CHECK_NOTNULL(node->GetOpDesc());
  678. current_idx++;
  679. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  680. if (op_kernel_lib_name.empty()) {
  681. continue;
  682. }
  683. if (op_desc->GetType() == NETOUTPUT) {
  684. iter_end = current_idx;
  685. GELOGI("Iter end name %s, idx %u", op_desc->GetName().c_str(), iter_end);
  686. }
  687. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  688. all_reduce_nodes.emplace_back(current_idx);
  689. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  690. }
  691. if (IsProfPoint(op_desc, bp_point_str)) {
  692. last_bp = current_idx;
  693. GELOGI("Last bp name from env is %s, idx %u", op_desc->GetName().c_str(), last_bp);
  694. }
  695. }
  696. profiling_point.bp_index = last_bp;
  697. profiling_point.end_index = iter_end;
  698. return SUCCESS;
  699. }
  700. Status TaskGenerator::FindProfilingTaskIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  701. vector<uint32_t> &all_reduce_nodes) const {
  702. GELOGI("Start FindProfilingTaskIndex.");
  703. GE_CHECK_NOTNULL(graph);
  704. const char *profiling_mode = std::getenv(kProfilingMode);
  705. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn();
  706. if (!is_profiling) {
  707. return SUCCESS;
  708. }
  709. const char *fp_point = std::getenv(kProfilingFpPoint);
  710. Status ret;
  711. if (fp_point == nullptr) {
  712. ret = AutoFindFpOpIndex(graph, profiling_point);
  713. if (ret != SUCCESS) {
  714. GELOGW("First forward profiling op_index not set and FindFpOpIndex failed.");
  715. return SUCCESS;
  716. }
  717. }
  718. const char *bp_point = std::getenv(kProfilingBpPoint);
  719. if (bp_point == nullptr) {
  720. ret = AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes);
  721. if (ret != SUCCESS) {
  722. GELOGW("Last backward profiling op_index not set and FindBpOpIndex failed.");
  723. return SUCCESS;
  724. }
  725. }
  726. if (fp_point != nullptr) {
  727. string fp_point_str = string(fp_point);
  728. ret = FindFpOfEnv(graph, fp_point_str, profiling_point);
  729. if (ret != SUCCESS) {
  730. GELOGW("First backward profiling op name set but FindFpOfEnv failed.");
  731. return SUCCESS;
  732. }
  733. }
  734. if (bp_point != nullptr) {
  735. string bp_point_str = string(bp_point);
  736. ret = FindBpOfEnv(graph, bp_point_str, profiling_point, all_reduce_nodes);
  737. if (ret != SUCCESS) {
  738. GELOGW("Last backward profiling op name set but FindBpOfEnv failed.");
  739. return SUCCESS;
  740. }
  741. }
  742. bool train_graph = graph->GetNeedIteration();
  743. if (profiling_point.fp_index == 0 && train_graph) {
  744. GELOGW("First forward op name can't be found in graph for training trace.");
  745. }
  746. if (profiling_point.bp_index == 0 && train_graph) {
  747. GELOGW("Last backward op name can't be found in graph for training trace.");
  748. }
  749. return SUCCESS;
  750. }
  751. Status TaskGenerator::InsertProfilingTaskBefore(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  752. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  753. vector<domi::TaskDef> &task_def_list) {
  754. const char *profiling_mode = std::getenv(kProfilingMode);
  755. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn();
  756. if (!is_profiling || (profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  757. (profiling_point.end_index == 0)) {
  758. return SUCCESS;
  759. }
  760. if (profiling_point.fp_index == node_index) {
  761. uint64_t jobid_log_id = ge::GetContext().TraceId();
  762. GELOGI("The first FP operator is %s, idx %u, job_id %lu", op_desc->GetName().c_str(), node_index, jobid_log_id);
  763. TaskDef job_task_def;
  764. job_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  765. job_task_def.set_stream_id(op_desc->GetStreamId());
  766. LogTimeStampDef *job_log_def = job_task_def.mutable_log_timestamp();
  767. if (job_log_def != nullptr) {
  768. job_log_def->set_logid(jobid_log_id);
  769. job_log_def->set_notify(false);
  770. }
  771. task_def_list.emplace_back(job_task_def);
  772. TaskDef fp_task_def;
  773. fp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  774. fp_task_def.set_stream_id(op_desc->GetStreamId());
  775. LogTimeStampDef *fp_log_def = fp_task_def.mutable_log_timestamp();
  776. if (fp_log_def != nullptr) {
  777. fp_log_def->set_logid(kProfilingFpStartLogid);
  778. fp_log_def->set_notify(false);
  779. }
  780. task_def_list.emplace_back(fp_task_def);
  781. }
  782. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  783. if (all_reduce_nodes[i] != node_index) {
  784. continue;
  785. }
  786. GELOGI("The start allreduce operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  787. TaskDef ar_task_def;
  788. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  789. ar_task_def.set_stream_id(op_desc->GetStreamId());
  790. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  791. if (ar_log_def != nullptr) {
  792. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  793. GELOGE(FAILED, "Multiply result is out of range.");
  794. return FAILED);
  795. auto log_id = i * kProfilingArStep + kProfilingArStartLogid;
  796. ar_log_def->set_logid(log_id);
  797. ar_log_def->set_notify(false);
  798. }
  799. task_def_list.push_back(ar_task_def);
  800. }
  801. return SUCCESS;
  802. }
  803. Status TaskGenerator::InsertProfilingTaskAfter(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  804. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  805. vector<domi::TaskDef> &task_def_list) {
  806. GE_CHECK_NOTNULL(op_desc);
  807. const char *profiling_mode = std::getenv(kProfilingMode);
  808. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn();
  809. if (!is_profiling || (profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  810. (profiling_point.end_index == 0)) {
  811. return SUCCESS;
  812. }
  813. if (profiling_point.bp_index == node_index) {
  814. GELOGI("The last BP operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  815. TaskDef bp_task_def;
  816. bp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  817. bp_task_def.set_stream_id(op_desc->GetStreamId());
  818. LogTimeStampDef *bp_log_def = bp_task_def.mutable_log_timestamp();
  819. GE_CHECK_NOTNULL(bp_log_def);
  820. bp_log_def->set_logid(kProfilingBpEndLogid);
  821. bp_log_def->set_notify(false);
  822. task_def_list.emplace_back(bp_task_def);
  823. }
  824. if (profiling_point.end_index == node_index) {
  825. GELOGI("The iteration end operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  826. TaskDef end_task_def;
  827. end_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  828. end_task_def.set_stream_id(op_desc->GetStreamId());
  829. LogTimeStampDef *end_log_def = end_task_def.mutable_log_timestamp();
  830. GE_CHECK_NOTNULL(end_log_def);
  831. end_log_def->set_logid(kProfilingIterEndLogid);
  832. end_log_def->set_notify(true);
  833. task_def_list.emplace_back(end_task_def);
  834. }
  835. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  836. if (all_reduce_nodes[i] != node_index) {
  837. continue;
  838. }
  839. GELOGI("The end allreduce operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  840. TaskDef ar_task_def;
  841. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  842. ar_task_def.set_stream_id(op_desc->GetStreamId());
  843. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  844. GE_CHECK_NOTNULL(ar_log_def);
  845. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  846. GELOGE(FAILED, "Multiply result is out of range.");
  847. return FAILED);
  848. auto log_id = i * kProfilingArStep + kProfilingArEndLogid;
  849. ar_log_def->set_logid(log_id);
  850. ar_log_def->set_notify(false);
  851. task_def_list.emplace_back(ar_task_def);
  852. }
  853. return SUCCESS;
  854. }
  855. bool TaskGenerator::IsProfPoint(const OpDescPtr &op, const std::string &name) {
  856. if (op == nullptr) {
  857. return false;
  858. }
  859. if (op->GetName() == name) {
  860. return true;
  861. }
  862. std::vector<std::string> original_op_names;
  863. bool ret = AttrUtils::GetListStr(op, ge::ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, original_op_names);
  864. if (!ret) {
  865. return false;
  866. }
  867. for (auto &origin_name : original_op_names) {
  868. if (origin_name == name) {
  869. return true;
  870. }
  871. }
  872. return false;
  873. }
  874. Status TaskGenerator::SetUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  875. GE_CHK_RT_RET(rtStreamCreate(&stream, 0));
  876. run_context.stream = stream;
  877. rtError_t rt_ret = rtModelBindStream(run_context.model, stream, 0);
  878. if (rt_ret != RT_ERROR_NONE) {
  879. GELOGE(FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  880. GE_CHK_RT_RET(rtStreamDestroy(stream));
  881. return FAILED;
  882. }
  883. return SUCCESS;
  884. }
  885. Status TaskGenerator::DestroyUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  886. GE_CHK_RT(rtModelUnbindStream(run_context.model, stream));
  887. GE_CHK_RT_RET(rtStreamDestroy(stream));
  888. return SUCCESS;
  889. }
  890. Status TaskGenerator::SetKnownShapeStream(RunContext &run_context, int64_t stream_id) {
  891. if (stream_id < 0 || stream_id >= static_cast<int64_t>(run_context.graphStreamList.size())) {
  892. GELOGE(INTERNAL_ERROR, "Stream id[%ld] is invalid, stream list size=%zu", stream_id,
  893. run_context.graphStreamList.size());
  894. return INTERNAL_ERROR;
  895. }
  896. run_context.stream = run_context.graphStreamList[stream_id];
  897. return SUCCESS;
  898. }
  899. } // namespace ge

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