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 56 kB

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

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