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

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