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

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