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

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

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