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

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

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