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

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