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

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