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

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