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

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

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