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

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