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

task_generator.cc 44 kB

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

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