You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

task_generator.cc 44 kB

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

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