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

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