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

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

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