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

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

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