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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /**
  2. * Copyright 2019-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. using domi::LogTimeStampDef;
  35. using domi::ModelTaskDef;
  36. using domi::TaskDef;
  37. using std::map;
  38. using std::set;
  39. using std::string;
  40. using std::vector;
  41. namespace {
  42. const char *const kIsFirstNode = "is_first_node";
  43. const char *const kIsLastNode = "is_last_node";
  44. const char *const kIsInputVar = "INPUT_IS_VAR";
  45. const char *const kIsOutputVar = "OUTPUT_IS_VAR";
  46. const char *const kProfilingMode = "PROFILING_MODE";
  47. const char *const kProfilingFpPoint = "FP_POINT";
  48. const char *const kProfilingBpPoint = "BP_POINT";
  49. const uint32_t kProfilingArStep = 2;
  50. const uint64_t kProfilingFpStartLogid = 1;
  51. const uint64_t kProfilingBpEndLogid = 2;
  52. const uint64_t kProfilingArStartLogid = 3;
  53. const uint64_t kProfilingArEndLogid = 4;
  54. const uint64_t kProfilingIterEndLogid = 255;
  55. const int64_t kHashFactor = 100000;
  56. const int64_t kInvalidGroupId = -1;
  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. GELOGI("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. GELOGI("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. GELOGI("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. OpsKernelInfoStorePtr kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(op_kernel_lib_name);
  284. if (kernel_info_store == nullptr) {
  285. GELOGE(INTERNAL_ERROR, "No ops kernel store found. node:%s(%s), op_kernel_lib_name=%s.", name.c_str(),
  286. type.c_str(), op_kernel_lib_name.c_str());
  287. return INTERNAL_ERROR;
  288. }
  289. GE_CHK_STATUS_RET(UpdateAnchorStatus(node), "Call UpdateAnchorStatus node:%s(%s) failed", name.c_str(),
  290. type.c_str());
  291. // Profiling task
  292. size_t task_list_size_before = task_def_list.size();
  293. GE_CHK_STATUS_RET(InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  294. int64_t op_id = op_desc->GetId();
  295. // Compatible with dynamic shape scenes, the default is 0
  296. int64_t stream_id = 0;
  297. if (!is_unknown_shape) {
  298. stream_id = op_desc->GetStreamId();
  299. GE_CHK_STATUS_RET(SetKnownShapeStream(run_context, stream_id), "node[name:%s(%s), id:%ld] stream id is invalid.",
  300. name.c_str(), type.c_str(), op_id);
  301. }
  302. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task.", op_kernel_lib_name.c_str(),
  303. name.c_str(), type.c_str(), op_id, stream_id);
  304. GE_TIMESTAMP_RESTART(GenerateTask);
  305. auto ret = kernel_info_store->GenerateTask(*node, run_context, task_def_list);
  306. GE_TIMESTAMP_ADD(GenerateTask);
  307. if (ret != SUCCESS) {
  308. GELOGE(ret, "Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task failed.",
  309. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id);
  310. return ret;
  311. }
  312. // Profiling task
  313. GE_CHK_STATUS_RET(InsertProfilingTaskAfter(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list));
  314. size_t task_list_size_after = task_def_list.size();
  315. // If tasks is reduced
  316. if (task_list_size_after < task_list_size_before) {
  317. GELOGE(FAILED, "Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task. but task num from %zu to %zu.",
  318. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id, task_list_size_before,
  319. task_list_size_after);
  320. return FAILED;
  321. }
  322. // Reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  323. void *ops_kernel_info_store_ptr = kernel_info_store.get();
  324. for (size_t idx = task_list_size_before; idx < task_list_size_after; ++idx) {
  325. task_def_list[idx].set_stream_id(static_cast<uint32_t>(stream_id));
  326. op_name_map[idx] = name;
  327. // Set opsKernelInfoStorePtr and op_index, the two fields be use in DistributeTask and InitTaskInfo
  328. TaskDef *task_def_ptr = &task_def_list[idx];
  329. GE_CHECK_NOTNULL(task_def_ptr);
  330. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(ops_kernel_info_store_ptr));
  331. }
  332. GELOGD("Call %s to generate node[name:%s(%s), id:%ld, stream_id:%ld] task finished, generate %zu task(s).",
  333. op_kernel_lib_name.c_str(), name.c_str(), type.c_str(), op_id, stream_id,
  334. task_list_size_after - task_list_size_before);
  335. }
  336. GE_TIMESTAMP_CALLNUM_EVENT_END(GenerateTask, "GraphBuild::GenerateTask");
  337. return SUCCESS;
  338. }
  339. Status TaskGenerator::GenerateTaskForFusionNode(FusionTaskInfo &fusion_task_info,
  340. std::map<int64_t, std::vector<NodePtr>> &fusion_nodes,
  341. std::unordered_set<Node *> &fusion_nodes_seen) {
  342. Status ret = SUCCESS;
  343. int64_t group_key;
  344. auto &run_context = fusion_task_info.run_context;
  345. auto &graph = fusion_task_info.graph;
  346. auto &node = fusion_task_info.node;
  347. auto &fusion_op_desc = fusion_task_info.fusion_op_desc;
  348. auto &node_index = fusion_task_info.node_index;
  349. const auto &ops_kernel_manager = fusion_task_info.ops_kernel_manager;
  350. auto &task_def_list = fusion_task_info.task_def_list;
  351. auto &op_name_map = fusion_task_info.op_name_map;
  352. auto &profiling_point = fusion_task_info.profiling_point;
  353. auto &all_reduce_nodes = fusion_task_info.all_reduce_nodes;
  354. // If op_desc have this attr, call nodes with same group key in a stream together
  355. if (ge::AttrUtils::GetInt(fusion_op_desc, ATTR_NAME_FUSION_GROUP_KEY, group_key) &&
  356. (fusion_nodes_seen.count(node.get()) == 0)) {
  357. GELOGI("Fusion: start fusion group index[%ld], nodes size[%zu].", group_key, fusion_nodes[group_key].size());
  358. for (auto &fusion_node : fusion_nodes[group_key]) {
  359. OpDescPtr op_desc = fusion_node->GetOpDesc();
  360. UpdateOpIsVarAttr(op_desc, graph->GetSessionID());
  361. std::string fusion_node_name = fusion_node->GetName();
  362. std::string fusion_node_type = fusion_node->GetType();
  363. std::string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  364. if (op_kernel_lib_name.empty()) {
  365. GELOGI("Fusion: fusion_node[name:%s(%s)] task no need to generate task.", fusion_node_name.c_str(),
  366. fusion_node_type.c_str());
  367. continue;
  368. }
  369. bool attr_notask = false;
  370. GE_IF_BOOL_EXEC(ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask,
  371. GELOGI("Fusion: fusion_node[name:%s, type:%s] does not need to generate task.",
  372. fusion_node_name.c_str(), fusion_node_type.c_str());
  373. continue);
  374. size_t task_list_size_before = task_def_list.size();
  375. OpsKernelInfoStorePtr kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(op_kernel_lib_name);
  376. if (kernel_info_store == nullptr) {
  377. GELOGE(INTERNAL_ERROR, "Fusion: No ops kernel store found. fusion_node:%s(%s), op_kernel_lib_name=%s.",
  378. fusion_node_name.c_str(), fusion_node_type.c_str(), op_kernel_lib_name.c_str());
  379. return INTERNAL_ERROR;
  380. }
  381. ret = UpdateAnchorStatus(fusion_node);
  382. if (ret != SUCCESS) {
  383. GELOGE(ret, "Fusion: Call UpdateAnchorStatus fusion_node:%s(%s) failed", fusion_node_name.c_str(),
  384. fusion_node_type.c_str());
  385. return ret;
  386. }
  387. int64_t op_id = op_desc->GetId();
  388. int64_t stream_id = op_desc->GetStreamId();
  389. if (stream_id < 0 || stream_id >= (int64_t)run_context.graphStreamList.size()) {
  390. GELOGE(INTERNAL_ERROR, "Fusion: fusion_node[name:%s(%s), id:%ld] stream id is invalid, stream list size=%zu",
  391. fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, run_context.graphStreamList.size());
  392. return INTERNAL_ERROR;
  393. }
  394. // profiling task
  395. (void)InsertProfilingTaskBefore(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list);
  396. run_context.stream = run_context.graphStreamList[stream_id];
  397. GELOGI("Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld] task.",
  398. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  399. ret = kernel_info_store->GenerateTask(*fusion_node, run_context, task_def_list);
  400. if (ret != SUCCESS) {
  401. GELOGE(ret,
  402. "Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  403. "id:%ld, stream_id:%ld] task failed.",
  404. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id);
  405. return ret;
  406. }
  407. // profiling task
  408. (void)InsertProfilingTaskAfter(op_desc, profiling_point, all_reduce_nodes, node_index, task_def_list);
  409. size_t task_list_size_after = task_def_list.size();
  410. // if tasks is reduced
  411. if (task_list_size_after < task_list_size_before) {
  412. GELOGE(FAILED,
  413. "Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), "
  414. "id:%ld, stream_id:%ld] task. but task num from %zu to %zu.",
  415. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  416. task_list_size_before, task_list_size_after);
  417. return FAILED;
  418. }
  419. // reset stream id to ge stream id, as graph load must use ge stream to reassign stream
  420. void *ops_kernel_info_store_ptr = kernel_info_store.get();
  421. for (size_t idx = task_list_size_before; idx < task_list_size_after; ++idx) {
  422. task_def_list[idx].set_stream_id(static_cast<uint32_t>(stream_id));
  423. op_name_map[idx] = fusion_node_name;
  424. // set opsKernelInfoStorePtr and op_index, the two fields be use in DistributeTask and InitTaskInfo
  425. TaskDef *task_def_ptr = &task_def_list[idx];
  426. task_def_ptr->set_ops_kernel_store_ptr(reinterpret_cast<uintptr_t>(ops_kernel_info_store_ptr));
  427. }
  428. GELOGI(
  429. "Fusion: Call %s to generate fusion_node:[fusion_node_name:%s(%s), id:%ld, stream_id:%ld]"
  430. " task finished, generate %u task(s).",
  431. op_kernel_lib_name.c_str(), fusion_node_name.c_str(), fusion_node_type.c_str(), op_id, stream_id,
  432. task_list_size_after - task_list_size_before);
  433. // record nodes which have call generate task successfully
  434. fusion_nodes_seen.insert(fusion_node.get());
  435. node_index++;
  436. }
  437. }
  438. // without tag or has been seen, skip directly
  439. return ret;
  440. }
  441. Status TaskGenerator::UpdateAnchorStatus(const NodePtr &node) {
  442. if (NodeUtils::SetAllAnchorStatus(node) != GRAPH_SUCCESS) {
  443. GELOGE(INTERNAL_ERROR, "NodeUtils::SetAllAnchorStatus failed.");
  444. return INTERNAL_ERROR;
  445. }
  446. for (auto &anchor : node->GetAllInDataAnchors()) {
  447. auto peer_anchor = anchor->GetPeerOutAnchor();
  448. if (peer_anchor == nullptr) {
  449. if (AnchorUtils::SetStatus(anchor, ANCHOR_SUSPEND) != GRAPH_SUCCESS) {
  450. GELOGE(INTERNAL_ERROR, "AnchorUtils::SetStatus failed.");
  451. return INTERNAL_ERROR;
  452. }
  453. continue;
  454. }
  455. std::string const_type;
  456. bool is_const = NodeUtils::GetConstOpType(peer_anchor->GetOwnerNode(), const_type);
  457. if (is_const && (const_type == CONSTANT)) {
  458. if (AnchorUtils::SetStatus(anchor, ANCHOR_CONST) != GRAPH_SUCCESS) {
  459. GELOGE(INTERNAL_ERROR, "AnchorUtils::SetStatus failed.");
  460. return INTERNAL_ERROR;
  461. }
  462. } else {
  463. if (AnchorUtils::SetStatus(anchor, ANCHOR_DATA) != GRAPH_SUCCESS) {
  464. GELOGE(INTERNAL_ERROR, "AnchorUtils::SetStatus failed.");
  465. return INTERNAL_ERROR;
  466. }
  467. }
  468. }
  469. return SUCCESS;
  470. }
  471. Status TaskGenerator::MarkNodeAndSetIndex(ComputeGraphPtr &graph) {
  472. auto ge_lib = GELib::GetInstance();
  473. if ((ge_lib == nullptr) || !ge_lib->InitFlag()) {
  474. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GE is not initialized or is finalized.");
  475. return GE_CLI_GE_NOT_INITIALIZED;
  476. }
  477. const auto all_nodes = graph->GetNodes(graph->GetGraphUnknownFlag());
  478. if (all_nodes.empty()) {
  479. GELOGE(GE_GRAPH_GRAPH_NODE_NULL, "Graph's node is empty");
  480. return GE_GRAPH_GRAPH_NODE_NULL;
  481. }
  482. int64_t node_index = 0;
  483. for (auto &node : all_nodes) {
  484. OpDescPtr op_desc = node->GetOpDesc();
  485. GE_CHECK_NOTNULL(op_desc);
  486. op_desc->SetId(node_index++);
  487. }
  488. map<int64_t, vector<OpDescPtr>> all_stream_ops;
  489. for (auto &node : all_nodes) {
  490. OpDescPtr op_desc = node->GetOpDesc();
  491. GE_CHECK_NOTNULL(op_desc);
  492. // Reset op kernel lib name
  493. if (op_desc->GetOpKernelLibName().empty()) {
  494. (void)ge_lib->DNNEngineManagerObj().GetDNNEngineName(node);
  495. }
  496. (void)op_desc->DelAttr(kIsFirstNode);
  497. (void)op_desc->DelAttr(kIsLastNode);
  498. all_stream_ops[op_desc->GetStreamId()].emplace_back(op_desc);
  499. }
  500. bool is_single_stream = all_stream_ops.size() == 1;
  501. for (const auto &stream_ops : all_stream_ops) {
  502. Status status = MarkFirstAndLastOps(stream_ops.second, is_single_stream);
  503. if (status != SUCCESS) {
  504. GELOGE(status, "Mark first and last nodes failed.");
  505. return status;
  506. }
  507. }
  508. return SUCCESS;
  509. }
  510. Status TaskGenerator::MarkFirstAndLastOps(const vector<OpDescPtr> &ops, bool is_single_stream) const {
  511. vector<vector<OpDescPtr>> continuous_op_lists(1);
  512. const set<string> separator_types(
  513. {LABELSET, LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX, STREAMSWITCH, STREAMSWITCHN});
  514. for (auto &op_desc : ops) {
  515. bool attr_notask = false;
  516. if (ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOTASK, attr_notask) && attr_notask) {
  517. continue;
  518. }
  519. string op_type = op_desc->GetType();
  520. if (!is_single_stream && (!op_desc->GetSubgraphInstanceNames().empty() || separator_types.count(op_type) != 0)) {
  521. continuous_op_lists.emplace_back(vector<OpDescPtr>());
  522. } else {
  523. continuous_op_lists.back().emplace_back(op_desc);
  524. }
  525. }
  526. GELOGI("Number of continuous node lists is %zu.", continuous_op_lists.size());
  527. for (const auto &continuous_ops : continuous_op_lists) {
  528. map<string, std::pair<OpDescPtr, OpDescPtr>> first_and_last_ops;
  529. for (auto &op_desc : continuous_ops) {
  530. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  531. if (op_kernel_lib_name.empty()) {
  532. GELOGE(INTERNAL_ERROR, "node:%s(%s) get op kernel lib failed.", op_desc->GetName().c_str(),
  533. op_desc->GetType().c_str());
  534. return INTERNAL_ERROR;
  535. }
  536. auto it = first_and_last_ops.find(op_kernel_lib_name);
  537. if (it == first_and_last_ops.end()) {
  538. first_and_last_ops.emplace(op_kernel_lib_name, std::make_pair(op_desc, op_desc));
  539. } else {
  540. it->second.second = op_desc;
  541. }
  542. }
  543. for (auto &it : first_and_last_ops) {
  544. auto &op_pair = it.second;
  545. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.first, kIsFirstNode, true), GELOGE(FAILED, "SetBool failed.");
  546. return FAILED);
  547. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetBool(op_pair.second, kIsLastNode, true), GELOGE(FAILED, "SetBool failed.");
  548. return FAILED);
  549. }
  550. }
  551. return SUCCESS;
  552. }
  553. Status TaskGenerator::AutoFindFpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point) const {
  554. GELOGI("Start AutoFindFpOpIndex");
  555. OpDescPtr fp_op_desc = nullptr;
  556. uint32_t current_idx = 0;
  557. uint32_t first_fp = 0;
  558. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  559. OpDescPtr op_desc = node->GetOpDesc();
  560. GE_CHECK_NOTNULL(op_desc);
  561. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  562. if (op_kernel_lib_name.empty()) {
  563. continue;
  564. }
  565. if (op_desc->GetType() == GETNEXT || op_desc->GetType() == DATA) {
  566. auto out_anchor = node->GetOutDataAnchor(0);
  567. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  568. GE_CHECK_NOTNULL(peer_in_anchor);
  569. auto in_node_desc = peer_in_anchor->GetOwnerNode()->GetOpDesc();
  570. GE_CHECK_NOTNULL(in_node_desc);
  571. if (fp_op_desc == nullptr || ((in_node_desc->GetId()) < (fp_op_desc->GetId()))) {
  572. fp_op_desc = in_node_desc;
  573. }
  574. }
  575. break;
  576. }
  577. }
  578. if (fp_op_desc == nullptr) {
  579. GELOGW("not find fp_op_desc.");
  580. return SUCCESS;
  581. }
  582. GELOGI("Find fp_op_desc is %s, id is %ld", fp_op_desc->GetName().c_str(), fp_op_desc->GetId());
  583. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  584. OpDescPtr op_desc = node->GetOpDesc();
  585. GE_CHECK_NOTNULL(op_desc);
  586. current_idx++;
  587. if (op_desc->GetName() == fp_op_desc->GetName()) {
  588. first_fp = current_idx;
  589. GELOGI("First fp name is %s, idx is %u", op_desc->GetName().c_str(), first_fp);
  590. break;
  591. }
  592. }
  593. profiling_point.fp_index = first_fp;
  594. return SUCCESS;
  595. }
  596. Status TaskGenerator::AutoFindBpOpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  597. vector<uint32_t> &all_reduce_nodes) const {
  598. GELOGI("Start AutoFindBpOpIndex");
  599. NodePtr bp_node = nullptr;
  600. uint32_t current_idx = 0;
  601. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  602. OpDescPtr op_desc = node->GetOpDesc();
  603. GE_CHECK_NOTNULL(op_desc);
  604. current_idx++;
  605. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  606. if (op_kernel_lib_name.empty()) {
  607. continue;
  608. }
  609. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  610. bp_node = node;
  611. all_reduce_nodes.emplace_back(current_idx);
  612. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  613. }
  614. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  615. if (bp_node == nullptr) {
  616. bp_node = node;
  617. }
  618. }
  619. if (graph->GetNeedIteration()) {
  620. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT + '_' + NODE_NAME_STREAM_SWITCH + "_StreamActive") {
  621. profiling_point.end_index.insert(current_idx);
  622. GELOGI("Iter end name %s, idx %u, from Node_Output_IteratorCtrl_StreamSwitch_StreamActive",
  623. op_desc->GetName().c_str(), current_idx);
  624. }
  625. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGN) {
  626. profiling_point.end_index.insert(current_idx);
  627. GELOGI("Iter end name %s, idx %u, from FlowCtrl_LoopCond_ASSIGN",
  628. op_desc->GetName().c_str(), current_idx);
  629. }
  630. } else {
  631. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  632. profiling_point.end_index.insert(current_idx);
  633. GELOGI("Iter end name %s, idx %u, from NETOUTPUT", op_desc->GetName().c_str(), current_idx);
  634. }
  635. }
  636. }
  637. if (bp_node == nullptr) {
  638. GELOGW("not find bp_node.");
  639. return SUCCESS;
  640. }
  641. profiling_point.bp_index = FindLastBpFromBpNode(graph, bp_node);
  642. return SUCCESS;
  643. }
  644. uint32_t TaskGenerator::FindLastBpFromBpNode(const ComputeGraphPtr &graph, const NodePtr &bp_node) const {
  645. uint32_t last_bp = 0;
  646. OpDescPtr bp_op_desc = nullptr;
  647. for (auto &in_anchor : bp_node->GetAllInDataAnchors()) {
  648. auto out_anchor = in_anchor->GetPeerOutAnchor();
  649. if (out_anchor == nullptr || out_anchor->GetOwnerNode() == nullptr) {
  650. continue;
  651. }
  652. auto out_node_desc = out_anchor->GetOwnerNode()->GetOpDesc();
  653. GE_CHECK_NOTNULL(out_node_desc);
  654. if (bp_op_desc == nullptr || ((out_node_desc->GetId()) > (bp_op_desc->GetId()))) {
  655. bp_op_desc = out_node_desc;
  656. }
  657. GELOGI("bp_op_desc is %s, id is %ld", bp_op_desc->GetName().c_str(), bp_op_desc->GetId());
  658. }
  659. GE_CHECK_NOTNULL(bp_op_desc);
  660. uint32_t current_idx = 0;
  661. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  662. OpDescPtr op_desc = node->GetOpDesc();
  663. GE_CHECK_NOTNULL(op_desc);
  664. current_idx++;
  665. if (op_desc->GetName() == bp_op_desc->GetName()) {
  666. last_bp = current_idx;
  667. GELOGI("First bp name %s, idx %u", op_desc->GetName().c_str(), last_bp);
  668. break;
  669. }
  670. }
  671. return last_bp;
  672. }
  673. Status TaskGenerator::FindFpOfEnv(const ComputeGraphPtr &graph, const std::string &fp_point_str,
  674. ProfilingPoint &profiling_point) const {
  675. GELOGI("Start FindFpOfEnv");
  676. uint32_t current_idx = 0;
  677. uint32_t first_fp = 0;
  678. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  679. OpDescPtr op_desc = node->GetOpDesc();
  680. GE_CHECK_NOTNULL(node->GetOpDesc());
  681. current_idx++;
  682. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  683. if (op_kernel_lib_name.empty()) {
  684. continue;
  685. }
  686. if (first_fp == 0 && IsProfPoint(op_desc, fp_point_str)) {
  687. first_fp = current_idx;
  688. GELOGI("First fp name from env is %s, idx %u", op_desc->GetName().c_str(), first_fp);
  689. }
  690. }
  691. profiling_point.fp_index = first_fp;
  692. return SUCCESS;
  693. }
  694. Status TaskGenerator::FindBpOfEnv(const ComputeGraphPtr &graph, const std::string &bp_point_str,
  695. ProfilingPoint &profiling_point, vector<uint32_t> &all_reduce_nodes) const {
  696. GELOGI("Start FindBpOfEnv");
  697. uint32_t current_idx = 0;
  698. uint32_t last_bp = 0;
  699. for (auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  700. OpDescPtr op_desc = node->GetOpDesc();
  701. GE_CHECK_NOTNULL(node->GetOpDesc());
  702. current_idx++;
  703. string op_kernel_lib_name = op_desc->GetOpKernelLibName();
  704. if (op_kernel_lib_name.empty()) {
  705. continue;
  706. }
  707. if (graph->GetNeedIteration()) {
  708. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT + '_' + NODE_NAME_STREAM_SWITCH + "_StreamActive") {
  709. profiling_point.end_index.insert(current_idx);
  710. GELOGI("Iter end name %s, idx %u, from Node_Output_IteratorCtrl_StreamSwitch_StreamActive",
  711. op_desc->GetName().c_str(), current_idx);
  712. }
  713. if (op_desc->GetName() == NODE_NAME_FLOWCTRL_LOOP_ASSIGN) {
  714. profiling_point.end_index.insert(current_idx);
  715. GELOGI("Iter end name %s, idx %u, from FlowCtrl_LoopCond_ASSIGN",
  716. op_desc->GetName().c_str(), current_idx);
  717. }
  718. } else {
  719. if (op_desc->GetName() == NODE_NAME_NET_OUTPUT) {
  720. profiling_point.end_index.insert(current_idx);
  721. GELOGI("Iter end name %s, idx %u, from NETOUTPUT", op_desc->GetName().c_str(), current_idx);
  722. }
  723. }
  724. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HVDCALLBACKALLREDUCE) {
  725. all_reduce_nodes.emplace_back(current_idx);
  726. GELOGI("Allreduce name %s, idx %u", op_desc->GetName().c_str(), current_idx);
  727. }
  728. if (IsProfPoint(op_desc, bp_point_str)) {
  729. last_bp = current_idx;
  730. GELOGI("Last bp name from env is %s, idx %u", op_desc->GetName().c_str(), last_bp);
  731. }
  732. }
  733. profiling_point.bp_index = last_bp;
  734. return SUCCESS;
  735. }
  736. Status TaskGenerator::GetFpBpIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  737. vector<uint32_t> &all_reduce_nodes, std::string &fp_point_str,
  738. std::string &bp_point_str) const {
  739. if (ge::GetContext().GetOption(OPTION_EXEC_PROFILING_FPPONIT_OPTIONS, fp_point_str) == SUCCESS &&
  740. ge::GetContext().GetOption(OPTION_EXEC_PROFILING_BPPONIT_OPTIONS, bp_point_str) == SUCCESS &&
  741. !fp_point_str.empty() && !bp_point_str.empty()) {
  742. return SUCCESS;
  743. }
  744. Status ret = SUCCESS;
  745. const char *fp_point = std::getenv(kProfilingFpPoint);
  746. if (fp_point == nullptr) {
  747. ret = AutoFindFpOpIndex(graph, profiling_point);
  748. if (ret != SUCCESS) {
  749. GELOGW("First forward profiling op_index not set and FindFpOpIndex failed.");
  750. return FAILED;
  751. }
  752. } else {
  753. fp_point_str = string(fp_point);
  754. GELOGI("Get fp_point_str from env %s", fp_point_str.c_str());
  755. }
  756. const char *bp_point = std::getenv(kProfilingBpPoint);
  757. if (bp_point == nullptr) {
  758. ret = AutoFindBpOpIndex(graph, profiling_point, all_reduce_nodes);
  759. if (ret != SUCCESS) {
  760. GELOGW("Last backward profiling op_index not set and FindBpOpIndex failed.");
  761. return FAILED;
  762. }
  763. } else {
  764. bp_point_str = string(bp_point);
  765. GELOGI("Get bp_point_str from env %s", bp_point_str.c_str());
  766. }
  767. return SUCCESS;
  768. }
  769. Status TaskGenerator::FindProfilingTaskIndex(const ComputeGraphPtr &graph, ProfilingPoint &profiling_point,
  770. vector<uint32_t> &all_reduce_nodes) const {
  771. GELOGI("Start FindProfilingTaskIndex.");
  772. GE_CHECK_NOTNULL(graph);
  773. const char *profiling_mode = std::getenv(kProfilingMode);
  774. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  775. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  776. if (!is_profiling) {
  777. GELOGW("Profiling is not open.");
  778. return SUCCESS;
  779. }
  780. GELOGI("Start get FP/BP index.");
  781. std::string fp_point_str;
  782. std::string bp_point_str;
  783. Status ret = GetFpBpIndex(graph, profiling_point, all_reduce_nodes, fp_point_str, bp_point_str);
  784. if (ret != SUCCESS) {
  785. GELOGW("Get FP_POINT BP_POINT failed.");
  786. return SUCCESS;
  787. }
  788. GELOGI("fp_point_str:%s, bp_point_str:%s.", fp_point_str.c_str(), bp_point_str.c_str());
  789. if (!fp_point_str.empty()) {
  790. ret = FindFpOfEnv(graph, fp_point_str, profiling_point);
  791. if (ret != SUCCESS) {
  792. GELOGW("First backward profiling op name set but FindFpOfEnv failed.");
  793. return SUCCESS;
  794. }
  795. }
  796. if (!bp_point_str.empty()) {
  797. ret = FindBpOfEnv(graph, bp_point_str, profiling_point, all_reduce_nodes);
  798. if (ret != SUCCESS) {
  799. GELOGW("Last backward profiling op name set but FindBpOfEnv failed.");
  800. return SUCCESS;
  801. }
  802. }
  803. bool train_graph = graph->GetNeedIteration();
  804. if (profiling_point.fp_index == 0 && train_graph) {
  805. GELOGW("First forward op name can't be found in graph for training trace.");
  806. }
  807. if (profiling_point.bp_index == 0 && train_graph) {
  808. GELOGW("Last backward op name can't be found in graph for training trace.");
  809. }
  810. return SUCCESS;
  811. }
  812. Status TaskGenerator::InsertProfilingTaskBefore(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  813. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  814. vector<domi::TaskDef> &task_def_list) {
  815. const char *profiling_mode = std::getenv(kProfilingMode);
  816. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  817. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  818. if (!is_profiling || (profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  819. (profiling_point.end_index.empty())) {
  820. return SUCCESS;
  821. }
  822. if (profiling_point.fp_index == node_index) {
  823. uint64_t jobid_log_id = ge::GetContext().TraceId();
  824. GELOGI("The first FP operator is %s, idx %u, job_id %lu", op_desc->GetName().c_str(), node_index, jobid_log_id);
  825. TaskDef job_task_def;
  826. job_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  827. job_task_def.set_stream_id(op_desc->GetStreamId());
  828. LogTimeStampDef *job_log_def = job_task_def.mutable_log_timestamp();
  829. if (job_log_def != nullptr) {
  830. job_log_def->set_logid(jobid_log_id);
  831. job_log_def->set_notify(false);
  832. }
  833. task_def_list.emplace_back(job_task_def);
  834. TaskDef fp_task_def;
  835. fp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  836. fp_task_def.set_stream_id(op_desc->GetStreamId());
  837. LogTimeStampDef *fp_log_def = fp_task_def.mutable_log_timestamp();
  838. if (fp_log_def != nullptr) {
  839. fp_log_def->set_logid(kProfilingFpStartLogid);
  840. fp_log_def->set_notify(false);
  841. }
  842. task_def_list.emplace_back(fp_task_def);
  843. }
  844. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  845. if (all_reduce_nodes[i] != node_index) {
  846. continue;
  847. }
  848. GELOGI("The start allreduce operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  849. TaskDef ar_task_def;
  850. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  851. ar_task_def.set_stream_id(op_desc->GetStreamId());
  852. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  853. if (ar_log_def != nullptr) {
  854. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  855. GELOGE(FAILED, "Multiply result is out of range.");
  856. return FAILED);
  857. auto log_id = i * kProfilingArStep + kProfilingArStartLogid;
  858. ar_log_def->set_logid(log_id);
  859. ar_log_def->set_notify(false);
  860. }
  861. task_def_list.push_back(ar_task_def);
  862. }
  863. return SUCCESS;
  864. }
  865. Status TaskGenerator::InsertProfilingTaskAfter(const OpDescPtr &op_desc, const ProfilingPoint &profiling_point,
  866. vector<uint32_t> &all_reduce_nodes, uint32_t node_index,
  867. vector<domi::TaskDef> &task_def_list) {
  868. GE_CHECK_NOTNULL(op_desc);
  869. const char *profiling_mode = std::getenv(kProfilingMode);
  870. bool is_profiling = (profiling_mode != nullptr) || ProfilingManager::Instance().ProfilingOn() ||
  871. ProfilingManager::Instance().ProfilingTrainingTraceOn();
  872. if (!is_profiling || (profiling_point.fp_index == 0) || (profiling_point.bp_index == 0) ||
  873. (profiling_point.end_index.empty())) {
  874. return SUCCESS;
  875. }
  876. if (profiling_point.bp_index == node_index) {
  877. GELOGI("The last BP operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  878. TaskDef bp_task_def;
  879. bp_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  880. bp_task_def.set_stream_id(op_desc->GetStreamId());
  881. LogTimeStampDef *bp_log_def = bp_task_def.mutable_log_timestamp();
  882. GE_CHECK_NOTNULL(bp_log_def);
  883. bp_log_def->set_logid(kProfilingBpEndLogid);
  884. bp_log_def->set_notify(false);
  885. task_def_list.emplace_back(bp_task_def);
  886. }
  887. if (profiling_point.end_index.find(node_index) != profiling_point.end_index.end()) {
  888. GELOGI("The iteration end operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  889. TaskDef end_task_def;
  890. end_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  891. end_task_def.set_stream_id(op_desc->GetStreamId());
  892. LogTimeStampDef *end_log_def = end_task_def.mutable_log_timestamp();
  893. GE_CHECK_NOTNULL(end_log_def);
  894. end_log_def->set_logid(kProfilingIterEndLogid);
  895. end_log_def->set_notify(true);
  896. task_def_list.emplace_back(end_task_def);
  897. }
  898. for (size_t i = 0; i < all_reduce_nodes.size(); i++) {
  899. if (all_reduce_nodes[i] != node_index) {
  900. continue;
  901. }
  902. GELOGI("The end allreduce operator is %s, idx %u", op_desc->GetName().c_str(), node_index);
  903. TaskDef ar_task_def;
  904. ar_task_def.set_type(RT_MODEL_TASK_PROFILER_TRACE);
  905. ar_task_def.set_stream_id(op_desc->GetStreamId());
  906. LogTimeStampDef *ar_log_def = ar_task_def.mutable_log_timestamp();
  907. GE_CHECK_NOTNULL(ar_log_def);
  908. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  909. GELOGE(FAILED, "Multiply result is out of range.");
  910. return FAILED);
  911. auto log_id = i * kProfilingArStep + kProfilingArEndLogid;
  912. ar_log_def->set_logid(log_id);
  913. ar_log_def->set_notify(false);
  914. task_def_list.emplace_back(ar_task_def);
  915. }
  916. return SUCCESS;
  917. }
  918. bool TaskGenerator::IsProfPoint(const OpDescPtr &op, const std::string &name) {
  919. if (op == nullptr) {
  920. return false;
  921. }
  922. if (op->GetName() == name) {
  923. return true;
  924. }
  925. std::vector<std::string> original_op_names;
  926. bool ret = AttrUtils::GetListStr(op, ge::ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, original_op_names);
  927. if (!ret) {
  928. return false;
  929. }
  930. for (auto &origin_name : original_op_names) {
  931. if (origin_name == name) {
  932. return true;
  933. }
  934. }
  935. return false;
  936. }
  937. Status TaskGenerator::SetUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  938. GE_CHK_RT_RET(rtStreamCreate(&stream, 0));
  939. run_context.stream = stream;
  940. rtError_t rt_ret = rtModelBindStream(run_context.model, stream, 0);
  941. if (rt_ret != RT_ERROR_NONE) {
  942. GELOGE(FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  943. GE_CHK_RT_RET(rtStreamDestroy(stream));
  944. return FAILED;
  945. }
  946. return SUCCESS;
  947. }
  948. Status TaskGenerator::DestroyUnknownShapeStream(RunContext &run_context, rtStream_t &stream) {
  949. GE_CHK_RT(rtModelUnbindStream(run_context.model, stream));
  950. GE_CHK_RT_RET(rtStreamDestroy(stream));
  951. return SUCCESS;
  952. }
  953. Status TaskGenerator::SetKnownShapeStream(RunContext &run_context, int64_t stream_id) {
  954. if (stream_id < 0 || stream_id >= static_cast<int64_t>(run_context.graphStreamList.size())) {
  955. GELOGE(INTERNAL_ERROR, "Stream id[%ld] is invalid, stream list size=%zu", stream_id,
  956. run_context.graphStreamList.size());
  957. return INTERNAL_ERROR;
  958. }
  959. run_context.stream = run_context.graphStreamList[stream_id];
  960. return SUCCESS;
  961. }
  962. } // namespace ge

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