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.

stream_allocator.cc 78 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
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
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
4 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
5 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
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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/stream_allocator.h"
  17. #include <algorithm>
  18. #include <memory>
  19. #include "graph/build/logical_stream_allocator.h"
  20. #include "common/omg_util.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/ge_context.h"
  23. #include "graph/utils/graph_utils.h"
  24. #include "init/gelib.h"
  25. using std::map;
  26. using std::set;
  27. using std::string;
  28. using std::vector;
  29. namespace {
  30. const int64_t kTaskNumPerNormalNode = 3;
  31. const int64_t kTaskNumPerHcclNode = 245;
  32. const char *const kTrueStr = "true";
  33. const char *const kFalseStr = "false";
  34. const size_t kEventMultiplexingItemCount = 3;
  35. const size_t kKeyWordIndex = 0;
  36. const size_t kNodeNameIndex = 1;
  37. const size_t kEventIdIndex = 2;
  38. const char *const kSend = "SendTo";
  39. const char *const kRecv = "RecvFrom";
  40. const char kDelim = ';';
  41. inline bool HasContinuousStreamLabel(const ge::OpDescPtr &op_desc, std::string &continuous_stream_label) {
  42. if (ge::AttrUtils::GetStr(op_desc, ge::ATTR_NAME_CONTINUOUS_STREAM_LABEL, continuous_stream_label)) {
  43. GELOGD("node[%s] get continuous_stream_label %s", op_desc->GetName().c_str(), continuous_stream_label.c_str());
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool IsHcclOp(const string &op_type) {
  49. const set<string> hccl_op_types({ge::HCOMBROADCAST, ge::HCOMALLGATHER,
  50. ge::HCOMALLREDUCE, ge::HCOMREDUCESCATTER, ge::HCOMREDUCE});
  51. return hccl_op_types.find(op_type) != hccl_op_types.end();
  52. }
  53. ge::Status ParseNodeEventMultiplexing(const ge::NodePtr &node,
  54. const std::vector<std::string> &raw_event_multiplexing,
  55. std::unordered_map<ge::NodePtr, std::vector<std::pair<std::string, uint32_t>>> &node_to_send,
  56. std::unordered_map<ge::NodePtr, std::vector<std::pair<std::string, uint32_t>>> &node_to_recv) {
  57. GE_CHECK_NOTNULL(node);
  58. for (const auto &str : raw_event_multiplexing) {
  59. std::vector<std::string> ele = ge::StringUtils::Split(str, kDelim);
  60. if (ele.size() != kEventMultiplexingItemCount) {
  61. GELOGE(ge::PARAM_INVALID, "[Check][RawMultiplexing]Size error, node:%s, require size:%zu, actually:%zu.",
  62. node->GetName().c_str(), kEventMultiplexingItemCount, ele.size());
  63. REPORT_INNER_ERROR("E19999", "Raw event multiplexing is invalid, node:%s, require size:%zu, actually:%zu.",
  64. node->GetName().c_str(), kEventMultiplexingItemCount, ele.size());
  65. return ge::PARAM_INVALID;
  66. }
  67. int value;
  68. try {
  69. value = std::stoi(ele[kEventIdIndex]);
  70. } catch (std::invalid_argument &) {
  71. GELOGE(ge::PARAM_INVALID, "[Throw][Exception]Event id is invalid, node:%s, raw:%s.",
  72. node->GetName().c_str(), ele[kEventIdIndex].c_str());
  73. REPORT_INNER_ERROR("E19999", "Event id is invalid, node:%s, raw:%s.",
  74. node->GetName().c_str(), ele[kEventIdIndex].c_str());
  75. return ge::PARAM_INVALID;
  76. } catch (std::out_of_range &) {
  77. GELOGE(ge::PARAM_INVALID, "[Throw][Exception]Event id is out of range, node:%s, raw:%s.",
  78. node->GetName().c_str(), ele[kEventIdIndex].c_str());
  79. REPORT_INNER_ERROR("E19999", "Event id is out of range, node:%s, raw:%s.",
  80. node->GetName().c_str(), ele[kEventIdIndex].c_str());
  81. return ge::PARAM_INVALID;
  82. }
  83. if (value < 0) {
  84. GELOGE(ge::PARAM_INVALID, "[Check][EventId]Event id is out of range, node:%s, raw:%s, value:%d.",
  85. node->GetName().c_str(), ele[kEventIdIndex].c_str(), value);
  86. REPORT_INNER_ERROR("E19999", "Event id is out of range, node:%s, raw:%s, value:%d.",
  87. node->GetName().c_str(), ele[kEventIdIndex].c_str(), value);
  88. return ge::PARAM_INVALID;
  89. }
  90. if (ele[kKeyWordIndex] == kSend) {
  91. node_to_send[node].emplace_back(std::make_pair(ele[kNodeNameIndex], static_cast<uint32_t>(value)));
  92. } else if (ele[kKeyWordIndex] == kRecv) {
  93. node_to_recv[node].emplace_back(std::make_pair(ele[kNodeNameIndex], static_cast<uint32_t>(value)));
  94. } else {
  95. GELOGE(ge::PARAM_INVALID, "[Check][KeyWord]Key word is not supported, node:%s, key:%s.",
  96. node->GetName().c_str(), ele[kEventIdIndex].c_str());
  97. REPORT_INNER_ERROR("E19999", "Key word is not supported, node:%s, key:%s.",
  98. node->GetName().c_str(), ele[kEventIdIndex].c_str());
  99. return ge::PARAM_INVALID;
  100. }
  101. }
  102. return ge::SUCCESS;
  103. }
  104. ge::Status ParseAllNodeEventMultiplexing(const ge::ComputeGraphPtr &graph,
  105. std::unordered_map<std::string, ge::NodePtr> &name_to_node_map,
  106. std::unordered_map<ge::NodePtr, std::vector<std::pair<std::string, uint32_t>>> &node_to_send,
  107. std::unordered_map<ge::NodePtr, std::vector<std::pair<std::string, uint32_t>>> &node_to_recv) {
  108. for (const auto &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  109. ge::OpDescPtr op_desc = node->GetOpDesc();
  110. GE_CHECK_NOTNULL(op_desc);
  111. name_to_node_map.insert({node->GetName(), node});
  112. std::vector<std::string> raw_event_multiplexing;
  113. if (!(op_desc->HasAttr(ge::ATTR_NAME_EVENT_MULTIPLEXING))) {
  114. continue;
  115. }
  116. bool get_attr = ge::AttrUtils::GetListStr(op_desc, ge::ATTR_NAME_EVENT_MULTIPLEXING, raw_event_multiplexing);
  117. if (!get_attr) {
  118. GELOGE(ge::PARAM_INVALID, "[Get][Attr]Node:%s.", node->GetName().c_str());
  119. REPORT_INNER_ERROR("E19999", "Failed to get raw event multiplexing, node:%s.", node->GetName().c_str());
  120. return ge::PARAM_INVALID;
  121. }
  122. auto parse_ret = ParseNodeEventMultiplexing(node, raw_event_multiplexing, node_to_send, node_to_recv);
  123. if (parse_ret != ge::SUCCESS) {
  124. GELOGE(parse_ret, "[Parse][Eventmultiplexing]Node:%s.", node->GetName().c_str());
  125. REPORT_INNER_ERROR("E19999", "Failed to parse node event multiplexing, node:%s.", node->GetName().c_str());
  126. return parse_ret;
  127. }
  128. }
  129. return ge::SUCCESS;
  130. }
  131. std::vector<uint32_t> GetIntersection(std::vector<uint32_t> &a, std::vector<uint32_t> &b) {
  132. std::unordered_set<uint32_t> ele_of_a(a.begin(), a.end());
  133. std::vector<uint32_t> res;
  134. for (auto &ele : b) {
  135. if (ele_of_a.count(ele) > 0) {
  136. res.emplace_back(ele);
  137. }
  138. }
  139. return res;
  140. }
  141. } // namespace
  142. namespace ge {
  143. StreamAllocator::StreamAllocator(ComputeGraphPtr whole_graph, const Graph2SubGraphInfoList &subgraphs)
  144. : whole_graph_(std::move(whole_graph)), subgraphs_(subgraphs) {
  145. string single_stream_str;
  146. (void)GetContext().GetOption(ENABLE_SINGLE_STREAM, single_stream_str);
  147. const set<string> stream_options = {"", kTrueStr, kFalseStr};
  148. if (stream_options.find(single_stream_str) == stream_options.end()) {
  149. GELOGW("The value %s of the %s option is invalid, it should be true or false.", single_stream_str.c_str(),
  150. ENABLE_SINGLE_STREAM);
  151. }
  152. enable_single_stream_ = (single_stream_str == kTrueStr) ? true : false;
  153. GELOGD("Enable single stream: %s.", enable_single_stream_ ? kTrueStr : kFalseStr);
  154. }
  155. Status StreamAllocator::AssignLogicalStreams(const std::map<std::string, int> &max_parallel_num, bool hcom_parallel) {
  156. GE_CHECK_NOTNULL(whole_graph_);
  157. GE_DUMP(whole_graph_, "BeforeAssignedLogicalStreams");
  158. auto gelib = GELib::GetInstance();
  159. if (gelib == nullptr) {
  160. REPORT_INNER_ERROR("E19999", "Check GELib instance nullptr, graph:%s", whole_graph_->GetName().c_str());
  161. GELOGE(FAILED, "[Get][Instance] of GELib failed. graph:%s", whole_graph_->GetName().c_str());
  162. return FAILED;
  163. }
  164. const map<string, SchedulerConf> &scheduler_confs = gelib->DNNEngineManagerObj().GetSchedulers();
  165. LogicalStreamAllocator logical_allocator(scheduler_confs, max_parallel_num);
  166. logical_allocator.EnableSingleStream(enable_single_stream_);
  167. logical_allocator.EnableHcomParallel(hcom_parallel);
  168. Status status = logical_allocator.Assign(whole_graph_, subgraphs_, stream_num_);
  169. if (status != SUCCESS) {
  170. GELOGE(status, "[Assign][LogicalStreams] failed. graph:%s", whole_graph_->GetName().c_str());
  171. return status;
  172. }
  173. GE_DUMP(whole_graph_, "AfterAssignedLogicalStreams");
  174. return SUCCESS;
  175. }
  176. // After allocating the logical stream in the graph, refresh the stream in the
  177. // graph and insert the synchronization node.
  178. Status StreamAllocator::RefreshRealStream(int64_t &stream_num, int64_t &event_num) {
  179. GE_CHECK_NOTNULL(whole_graph_);
  180. GE_DUMP(whole_graph_, "BeforeRefreshRealStream");
  181. Status status = AssignSingleStream();
  182. if (status != SUCCESS) {
  183. GELOGE(status, "[Assign][SingleStream] failed! graph:%s", whole_graph_->GetName().c_str());
  184. return status;
  185. }
  186. status = SetActiveStreamsByLabel();
  187. if (status != SUCCESS) {
  188. GELOGE(status, "[Set][ActiveStreams] By Label failed! graph:%s", whole_graph_->GetName().c_str());
  189. return status;
  190. }
  191. status = SetActiveStreamsForSubgraphs();
  192. if (status != SUCCESS) {
  193. GELOGE(status, "[Set][ActiveStreams] For Subgraphs failed. graph:%s", whole_graph_->GetName().c_str());
  194. return status;
  195. }
  196. status = InsertSyncEvents();
  197. if (status != SUCCESS) {
  198. GELOGE(status, "[Insert][SyncEventId] failed! graph:%s", whole_graph_->GetName().c_str());
  199. return status;
  200. }
  201. status = OptimizeSyncEvents();
  202. if (status != SUCCESS) {
  203. GELOGE(status, "[Optimize][SyncEventId] failed! graph:%s", whole_graph_->GetName().c_str());
  204. return status;
  205. }
  206. vector<set<int64_t>> split_streams(stream_num_);
  207. status = SplitStreams(split_streams);
  208. if (status != SUCCESS) {
  209. GELOGE(status, "[Split][Streams] failed! graph:%s", whole_graph_->GetName().c_str());
  210. return status;
  211. }
  212. status = UpdateActiveStreams(split_streams);
  213. if (status != SUCCESS) {
  214. GELOGE(status, "[Update][ActiveStreams] failed! graph:%s", whole_graph_->GetName().c_str());
  215. return status;
  216. }
  217. status = RefreshContinuousEvents();
  218. if (status != SUCCESS) {
  219. GELOGE(status, "[Refresh][ContinuousEvents] failed! graph:%s", whole_graph_->GetName().c_str());
  220. return status;
  221. }
  222. status = RefreshEventsWithReuse();
  223. if (status != SUCCESS) {
  224. GELOGE(status, "[Refresh][Events] With Reuse failed! graph:%s", whole_graph_->GetName().c_str());
  225. return status;
  226. }
  227. status = InsertSyncEventNodes();
  228. if (status != SUCCESS) {
  229. GELOGE(status, "[Insert][SyncEventNode] failed! graph:%s", whole_graph_->GetName().c_str());
  230. return status;
  231. }
  232. DumpEvents();
  233. GE_DUMP(whole_graph_, "AfterRefreshRealStream");
  234. for (const NodePtr &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  235. GE_CHECK_NOTNULL(node->GetOpDesc());
  236. auto stream_id = node->GetOpDesc()->GetStreamId();
  237. if (stream_id == kInvalidStream) {
  238. node->GetOpDesc()->SetStreamId(0);
  239. }
  240. }
  241. if (stream_num_ == 0) {
  242. GELOGI("None of nodes need to assign stream, stream num is 0, it will cause error, so change it to 1");
  243. stream_num_ = 1;
  244. }
  245. GELOGD("stream num: %ld, event num: %u.", stream_num_, event_num_);
  246. stream_num = stream_num_;
  247. event_num = static_cast<int64_t>(event_num_);
  248. return SUCCESS;
  249. }
  250. Status StreamAllocator::AssignSingleStream() {
  251. if (!enable_single_stream_) {
  252. return SUCCESS;
  253. }
  254. if (stream_num_ > 1) {
  255. REPORT_INNER_ERROR("E19999", "The number of ts streams is %ld, only one is supported",
  256. stream_num_);
  257. GELOGE(FAILED, "[Check][Param] The number of ts streams is %ld, only one is supported.", stream_num_);
  258. return FAILED;
  259. }
  260. int64_t task_count = 0;
  261. for (const NodePtr &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  262. string op_type = node->GetType();
  263. if (IsHcclOp(op_type)) {
  264. task_count += kTaskNumPerHcclNode;
  265. } else {
  266. task_count += kTaskNumPerNormalNode;
  267. }
  268. }
  269. uint32_t max_normal_stream_count = 0;
  270. uint32_t max_normal_task_count = 0;
  271. Status status = GetMaxStreamAndTask(false, max_normal_stream_count, max_normal_task_count);
  272. if (status != SUCCESS) {
  273. GELOGE(status, "[Get][MaxCount] of normal stream and task failed. graph:%s", whole_graph_->GetName().c_str());
  274. return status;
  275. }
  276. if (task_count > static_cast<int64_t>(max_normal_task_count)) {
  277. uint32_t max_huge_stream_count = 0;
  278. uint32_t max_huge_task_count = 0;
  279. Status status = GetMaxStreamAndTask(true, max_huge_stream_count, max_huge_task_count);
  280. if (status == SUCCESS) {
  281. int64_t huge_stream = 0;
  282. GELOGI("Use huge stream %ld.", huge_stream);
  283. huge_streams_.emplace_back(huge_stream);
  284. } else {
  285. GELOGW(
  286. "The estimated task count %ld is greater than the max count of normal stream,"
  287. " but the huge stream is not supported.",
  288. task_count);
  289. }
  290. }
  291. return SUCCESS;
  292. }
  293. Status StreamAllocator::SetActiveStreamsByLabel() {
  294. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  295. OpDescPtr op_desc = node->GetOpDesc();
  296. GE_CHECK_NOTNULL(op_desc);
  297. string stream_label;
  298. if (AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label) && !stream_label.empty()) {
  299. int64_t stream_id = op_desc->GetStreamId();
  300. if (stream_id != kInvalidStream) {
  301. labeled_streams_[stream_label].emplace(stream_id);
  302. }
  303. }
  304. }
  305. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  306. GE_CHECK_NOTNULL(node->GetOpDesc());
  307. vector<string> activated_label_list;
  308. if (!AttrUtils::GetListStr(node->GetOpDesc(), ATTR_NAME_ACTIVE_LABEL_LIST, activated_label_list) ||
  309. activated_label_list.empty()) {
  310. continue;
  311. }
  312. vector<uint32_t> activated_stream_list;
  313. for (string &activated_label : activated_label_list) {
  314. specific_activated_labels_[activated_label].emplace(node);
  315. for (int64_t activated_stream : labeled_streams_[activated_label]) {
  316. activated_stream_list.push_back(static_cast<uint32_t>(activated_stream));
  317. specific_activated_streams_.emplace(activated_stream);
  318. specific_activated_streams_nodes_map_[activated_stream].emplace(node);
  319. GELOGI("Node %s active stream %ld by %s.", node->GetName().c_str(), activated_stream, activated_label.c_str());
  320. }
  321. }
  322. GE_CHK_BOOL_EXEC(AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, activated_stream_list),
  323. REPORT_INNER_ERROR("E19999", "Set Attr:%s for op:%s(%s) failed",
  324. ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  325. node->GetName().c_str(), node->GetType().c_str());
  326. GELOGE(FAILED, "[Set][Attr] %s for op:%s(%s) failed",
  327. ATTR_NAME_ACTIVE_STREAM_LIST.c_str(), node->GetName().c_str(), node->GetType().c_str());
  328. return FAILED);
  329. }
  330. return SUCCESS;
  331. }
  332. Status StreamAllocator::SetActiveStreamsForSubgraphs() {
  333. for (auto &subgraph : whole_graph_->GetAllSubgraphs()) {
  334. GE_CHECK_NOTNULL(subgraph);
  335. NodePtr first_active_node = nullptr;
  336. // Get all streams in subgraph.
  337. set<int64_t> subgraph_streams;
  338. for (auto &node : subgraph->GetDirectNode()) {
  339. OpDescPtr op_desc = node->GetOpDesc();
  340. GE_CHECK_NOTNULL(op_desc);
  341. // Skip streams with label
  342. string stream_label;
  343. if (AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label) && !stream_label.empty()) {
  344. continue;
  345. }
  346. int64_t stream_id = op_desc->GetStreamId();
  347. if (stream_id != kInvalidStream) {
  348. subgraph_streams.emplace(stream_id);
  349. GELOGI("Add stream %ld to active_stream_list of node %s of graph %s", stream_id, node->GetName().c_str(),
  350. subgraph->GetName().c_str());
  351. }
  352. bool is_first_active = false;
  353. if (AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_FIRST_ACTIVE, is_first_active) && is_first_active) {
  354. first_active_node = node;
  355. }
  356. }
  357. if (first_active_node == nullptr) {
  358. continue;
  359. }
  360. subgraph_first_active_node_map_[subgraph] = first_active_node;
  361. // Set active streams for StreamActive.
  362. subgraph_streams.erase(first_active_node->GetOpDesc()->GetStreamId());
  363. vector<uint32_t> active_streams;
  364. for (int64_t active_stream : subgraph_streams) {
  365. active_streams.emplace_back(static_cast<uint32_t>(active_stream));
  366. specific_activated_streams_.emplace(active_stream);
  367. }
  368. if (!AttrUtils::SetListInt(first_active_node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  369. REPORT_INNER_ERROR("E19999", "Set Attr:%s for op:%s(%s) failed",
  370. ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  371. first_active_node->GetName().c_str(), first_active_node->GetType().c_str());
  372. GELOGE(FAILED, "[Set][Attr] active streams for node %s failed.", first_active_node->GetName().c_str());
  373. return FAILED;
  374. }
  375. }
  376. return SUCCESS;
  377. }
  378. // Insert the send/recv event id to the graph
  379. Status StreamAllocator::InsertSyncEvents() {
  380. auto ffts_filter = [](const Node &node, const char *, const ComputeGraphPtr &) {
  381. return !(node.GetOpDesc()->HasAttr(ATTR_NAME_FFTS_SUB_GRAPH) ||
  382. node.GetOpDesc()->HasAttr(ATTR_NAME_FFTS_PLUS_SUB_GRAPH));
  383. };
  384. for (const auto &cur_node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag(), nullptr, ffts_filter)) {
  385. // Take the adjacent points, then judge whether need to insert the event
  386. for (const OutDataAnchorPtr &anchor : cur_node->GetAllOutDataAnchors()) {
  387. for (const InDataAnchorPtr &peer_in_anchor : anchor->GetPeerInDataAnchors()) {
  388. NodePtr next_node = peer_in_anchor->GetOwnerNode();
  389. Status status = InsertOneEventInTwoNodes(cur_node, next_node);
  390. if (status != SUCCESS) {
  391. GELOGE(status, "[Insert][OneEvent] In Two Nodes failed! cur node:%s", cur_node->GetName().c_str());
  392. return status;
  393. }
  394. }
  395. }
  396. /// If the two nodes of the control side belong to two streams,
  397. /// you also need to add the send/recv event.
  398. if (cur_node->GetOutControlAnchor() != nullptr) {
  399. for (const AnchorPtr &peer_in_anchor : cur_node->GetOutControlAnchor()->GetPeerAnchors()) {
  400. NodePtr next_node = peer_in_anchor->GetOwnerNode();
  401. Status status = InsertOneEventInTwoNodes(cur_node, next_node);
  402. if (status != SUCCESS) {
  403. GELOGE(status, "[Insert][OneEvent] In Two Nodes failed! cur node:%s", cur_node->GetName().c_str());
  404. return status;
  405. }
  406. }
  407. }
  408. }
  409. Status status = InsertEventsForSubgraph();
  410. if (status != SUCCESS) {
  411. GELOGE(status, "[Insert][Events] Between Sub And Parent GraphNodes failed! graph:%s",
  412. whole_graph_->GetName().c_str());
  413. return status;
  414. }
  415. return SUCCESS;
  416. }
  417. // Insert one send/recv event in two nodes
  418. Status StreamAllocator::InsertOneEventInTwoNodes(const NodePtr &cur_node, const NodePtr &next_node) {
  419. GE_CHECK_NOTNULL(cur_node->GetOpDesc());
  420. GE_CHECK_NOTNULL(next_node->GetOpDesc());
  421. // No need to insert events after node that do not assign streams.
  422. int64_t cur_stream_id = cur_node->GetOpDesc()->GetStreamId();
  423. if (cur_stream_id == kInvalidStream) {
  424. GELOGD("No need to insert event after node %s.", cur_node->GetName().c_str());
  425. return SUCCESS;
  426. }
  427. // No need to insert events between nodes in the same stream.
  428. int64_t next_stream_id = next_node->GetOpDesc()->GetStreamId();
  429. if (cur_stream_id == next_stream_id) {
  430. return SUCCESS;
  431. }
  432. if (((cur_node->GetType() == ENTER) || (cur_node->GetType() == REFENTER)) && (next_node->GetType() != STREAMACTIVE)) {
  433. GELOGD("No need to insert event between %s and %s.", cur_node->GetName().c_str(), next_node->GetName().c_str());
  434. return SUCCESS;
  435. }
  436. if (next_stream_id == kInvalidStream) {
  437. REPORT_INNER_ERROR("E19999", "Stream id of next_node %s(%s) should not be %ld",
  438. next_node->GetName().c_str(), next_node->GetType().c_str(), kInvalidStream);
  439. GELOGE(FAILED, "[Check][Param] Stream id of next_node %s should not be %ld",
  440. next_node->GetName().c_str(), kInvalidStream);
  441. return FAILED;
  442. }
  443. // No event needs to be inserted between the active node and the activated stream.
  444. string next_node_label;
  445. if (AttrUtils::GetStr(next_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, next_node_label) && !next_node_label.empty()) {
  446. auto iter = specific_activated_labels_.find(next_node_label);
  447. if (iter != specific_activated_labels_.end()) {
  448. for (const auto &active_node : iter->second) {
  449. OpDescPtr active_op = active_node->GetOpDesc();
  450. GE_CHECK_NOTNULL(active_op);
  451. if ((cur_stream_id == active_op->GetStreamId()) && (cur_node->GetOpDesc()->GetId() <= active_op->GetId())) {
  452. GELOGI("No need to insert event between node %s and %s.", cur_node->GetName().c_str(),
  453. next_node->GetName().c_str());
  454. return SUCCESS;
  455. }
  456. }
  457. }
  458. }
  459. // Add send and receive events.
  460. AddSendEventId(cur_node, event_num_);
  461. AddRecvEventId(next_node, event_num_);
  462. GELOGD("Insert event %u between node %s(stream %ld) and %s(stream %ld)", event_num_, cur_node->GetName().c_str(),
  463. cur_stream_id, next_node->GetName().c_str(), next_stream_id);
  464. ++event_num_;
  465. return SUCCESS;
  466. }
  467. Status StreamAllocator::InsertEventsForSubgraph() {
  468. for (const auto &subgraph : whole_graph_->GetAllSubgraphs()) {
  469. GE_CHECK_NOTNULL(subgraph);
  470. const auto parent_node = subgraph->GetParentNode();
  471. if (parent_node != nullptr && (parent_node->GetOpDesc()->HasAttr(ATTR_NAME_FFTS_SUB_GRAPH) ||
  472. parent_node->GetOpDesc()->HasAttr(ATTR_NAME_FFTS_PLUS_SUB_GRAPH) ||
  473. parent_node->GetOpDesc()->HasAttr(ATTR_NAME_THREAD_SCOPE_ID))) {
  474. GELOGD("Skip ffts subgraph, parent node is %s.", parent_node->GetName().c_str());
  475. continue;
  476. }
  477. for (const auto &node : subgraph->GetDirectNode()) {
  478. auto op_desc = node->GetOpDesc();
  479. GE_CHECK_NOTNULL(op_desc);
  480. bool is_subgraph_end_node = false;
  481. if (!AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_END_NODE, is_subgraph_end_node) || !is_subgraph_end_node) {
  482. continue;
  483. }
  484. const auto parent_node = subgraph->GetParentNode();
  485. GE_CHECK_NOTNULL(parent_node);
  486. // Insert events between subgraph end node and parent node's out nodes
  487. for (const auto &next_node : parent_node->GetOutAllNodes()) {
  488. Status status = InsertOneEventInTwoNodes(node, next_node);
  489. if (status != SUCCESS) {
  490. GELOGE(status, "[Insert][OneEvent] In Two Nodes failed! node:%s", node->GetName().c_str());
  491. return status;
  492. }
  493. }
  494. break;
  495. }
  496. }
  497. return SUCCESS;
  498. }
  499. // Optimize the event in the graph, delete the redundant sync event according to the stream information
  500. Status StreamAllocator::OptimizeSyncEvents() {
  501. map<int64_t, vector<NodePtr>> stream_nodes;
  502. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  503. GE_CHECK_NOTNULL(node->GetOpDesc());
  504. int64_t stream_id = node->GetOpDesc()->GetStreamId();
  505. stream_nodes[stream_id].emplace_back(node);
  506. }
  507. Status status = OptimizeBySendEvents(stream_nodes);
  508. if (status != SUCCESS) {
  509. GELOGE(status, "[Optimize][StreamNodes] By Send Events failed! graph:%s", whole_graph_->GetName().c_str());
  510. return status;
  511. }
  512. status = OptimizeByRecvEvents(stream_nodes);
  513. if (status != SUCCESS) {
  514. GELOGE(status, "[Optimize][StreamNodes] By Recv Events failed! graph:%s", whole_graph_->GetName().c_str());
  515. return status;
  516. }
  517. status = OptimizeByStreamActivate();
  518. if (status != SUCCESS) {
  519. GELOGE(status, "[Call][OptimizeByStreamActivate] failed! graph:%s", whole_graph_->GetName().c_str());
  520. return status;
  521. }
  522. for (auto pair : node_to_send_events_) {
  523. if (pair.first->GetType() == STREAMSWITCH) {
  524. for (auto event_id : pair.second) {
  525. GELOGI("Curren switch node is %s, remove send event_id %d.", pair.first->GetName().c_str(), event_id);
  526. RmvSendEventId(pair.first, event_id);
  527. auto recv_node = GetNodeFromRecvEventId(event_id);
  528. GELOGI("Curren recv_node is %s, remove recv event_id %d.", recv_node->GetName().c_str(), event_id);
  529. RmvRecvEventId(recv_node, event_id);
  530. }
  531. }
  532. }
  533. return SUCCESS;
  534. }
  535. /// Optimization scenario: one stream has multiple send events in one node,
  536. /// and multiple nodes for recv events on another stream
  537. /// Example:
  538. /// Stream0 Stream1
  539. /// N1 - - - event - > N1
  540. /// \ |
  541. /// \ v
  542. /// - - event - > N2
  543. Status StreamAllocator::OptimizeBySendEvents(const map<int64_t, vector<NodePtr>> &stream_nodes) {
  544. for (const auto &one_pair : stream_nodes) {
  545. // The nodes on a stream in order
  546. const vector<NodePtr> &nodes = one_pair.second;
  547. map<NodePtr, uint32_t> send_node_to_event_id;
  548. for (const auto &recv_node_ptr : nodes) {
  549. GE_CHECK_NOTNULL(recv_node_ptr);
  550. // Get all recv events of the current node, then traverse the event
  551. vector<uint32_t> recv_events;
  552. GetRecvEventIdList(recv_node_ptr, recv_events);
  553. for (const auto &event_id : recv_events) {
  554. NodePtr send_node_ptr = GetNodeFromSendEventId(event_id);
  555. GE_CHECK_NOTNULL(send_node_ptr);
  556. /// If the record to the stream is found in the map,
  557. /// and the recv node is the node, then remove sync event
  558. if (send_node_to_event_id.find(send_node_ptr) != send_node_to_event_id.end()) {
  559. RmvSendEventId(send_node_ptr, event_id);
  560. RmvRecvEventId(recv_node_ptr, event_id);
  561. GELOGI("Remove event %u between node %s and node %s", event_id, send_node_ptr->GetName().c_str(),
  562. recv_node_ptr->GetName().c_str());
  563. } else {
  564. send_node_to_event_id[send_node_ptr] = event_id;
  565. }
  566. }
  567. }
  568. }
  569. return SUCCESS;
  570. }
  571. /// Scenario: multiple send nodes on a stream sent to a single recv node on the destination stream
  572. /// Example:
  573. /// Stream0 Stream1
  574. /// N1 - -
  575. /// | |
  576. /// | - - event - - -
  577. /// | |
  578. /// V V
  579. /// N2 - - - event - > N2
  580. Status StreamAllocator::OptimizeByRecvEvents(const map<int64_t, vector<NodePtr>> &stream_nodes) {
  581. for (const auto &one_pair : stream_nodes) {
  582. // The nodes on a stream in order
  583. const vector<NodePtr> &nodes = one_pair.second;
  584. map<NodePtr, uint32_t> recv_node_to_event_id;
  585. for (const auto &send_node_ptr : nodes) {
  586. GE_CHECK_NOTNULL(send_node_ptr);
  587. // Get all send events of the current node, then traverse the event
  588. vector<uint32_t> send_id_list;
  589. GetSendEventIdList(send_node_ptr, send_id_list);
  590. for (const auto &event_id : send_id_list) {
  591. NodePtr recv_node_ptr = GetNodeFromRecvEventId(event_id);
  592. GE_CHECK_NOTNULL(recv_node_ptr);
  593. /// If the record to the stream is found in the map,
  594. /// and the send node is the node, then remove sync event
  595. auto it = recv_node_to_event_id.find(recv_node_ptr);
  596. if (it != recv_node_to_event_id.end()) {
  597. uint32_t pre_event_id = it->second;
  598. NodePtr pre_send_node_ptr = GetNodeFromSendEventId(pre_event_id);
  599. GE_CHECK_NOTNULL(pre_send_node_ptr);
  600. RmvSendEventId(pre_send_node_ptr, pre_event_id);
  601. RmvRecvEventId(recv_node_ptr, pre_event_id);
  602. GELOGI("Remove event %u between node %s and node %s.", event_id, pre_send_node_ptr->GetName().c_str(),
  603. recv_node_ptr->GetName().c_str());
  604. }
  605. recv_node_to_event_id[recv_node_ptr] = event_id;
  606. }
  607. }
  608. }
  609. return SUCCESS;
  610. }
  611. Status StreamAllocator::OptimizeByStreamActivate() {
  612. auto node_to_send_events_temp = node_to_send_events_;
  613. for (const auto &node_event_id_pair : node_to_send_events_temp) {
  614. const NodePtr &send_node_ptr = node_event_id_pair.first;
  615. for (const auto &event_id : node_event_id_pair.second) {
  616. NodePtr recv_node_ptr = GetNodeFromRecvEventId(event_id);
  617. GE_CHECK_NOTNULL(recv_node_ptr);
  618. if (IsRecvNodeActivatedBySendNode(send_node_ptr, recv_node_ptr)) {
  619. RmvSendEventId(send_node_ptr, event_id);
  620. RmvRecvEventId(recv_node_ptr, event_id);
  621. GELOGI("Remove event %u between node %s and node %s.", event_id, send_node_ptr->GetName().c_str(),
  622. recv_node_ptr->GetName().c_str());
  623. }
  624. }
  625. }
  626. return SUCCESS;
  627. }
  628. // In situation : stream(normal) -> stream(streamActivate)->
  629. // -> stream(streamSwitch) -> stream(streamActivate) -> stream(stream true or false)
  630. // No need to insert an event between node in stream(normal) and node in stream(stream true or false)
  631. bool StreamAllocator::IsRecvNodeActivatedBySendNode(const NodePtr &send_node_ptr, const NodePtr &recv_node_ptr) const {
  632. GE_CHECK_NOTNULL_EXEC(send_node_ptr->GetOpDesc(),
  633. REPORT_INNER_ERROR("E19999", "Check param send_node_ptr nullptr");
  634. GELOGE(FAILED, "[Check][Param] op desc is nullptr");
  635. return false);
  636. GE_CHECK_NOTNULL_EXEC(recv_node_ptr->GetOpDesc(),
  637. REPORT_INNER_ERROR("E19999", "Check param recv_node_ptr nullptr");
  638. GELOGE(FAILED, "[Check][Param] op desc is nullptr");
  639. return false);
  640. auto cur_stream_id = send_node_ptr->GetOpDesc()->GetStreamId();
  641. if (AttrUtils::HasAttr(recv_node_ptr->GetOpDesc(), ATTR_NAME_STREAM_LABEL)) {
  642. // find streamActivate node
  643. auto iter = specific_activated_streams_nodes_map_.find(recv_node_ptr->GetOpDesc()->GetStreamId());
  644. set<NodePtr> activate_stream_nodes;
  645. if (iter != specific_activated_streams_nodes_map_.end()) {
  646. activate_stream_nodes = iter->second;
  647. }
  648. set<NodePtr> visited_nodes{recv_node_ptr};
  649. while (!activate_stream_nodes.empty()) {
  650. set<NodePtr> activate_stream_nodes_temp;
  651. for (const auto &activate_stream_node : activate_stream_nodes) {
  652. GE_IF_BOOL_EXEC(activate_stream_node->GetOpDesc() == nullptr, continue);
  653. if (visited_nodes.find(activate_stream_node) != visited_nodes.end() ||
  654. AttrUtils::HasAttr(activate_stream_node->GetOpDesc(), ATTR_NAME_IS_LOOP_ACTIVE)) {
  655. return false;
  656. }
  657. ///
  658. /// stream_0 --> stream_2 --> stream_3 --> stream_4
  659. /// /\ |
  660. /// | \/
  661. /// | stream_1 --> stream_5 --> stream_6 --> stream_7
  662. /// | /\ | |
  663. /// | | \/ |
  664. /// | |---------- stream_8 |
  665. /// | |
  666. /// |-----------------------------------------------------------|
  667. ///
  668. /// Exit1(S7) Exit2(S7) Exit3(S7)
  669. /// \ / |
  670. /// AddN(S1) NextIteration(S7)
  671. /// | |
  672. /// NextIteration(S1) /
  673. /// | /
  674. /// | /
  675. /// StreamActive(S7)
  676. ///
  677. /// Event between Exit1/Exit2 and AddN should not be optimized
  678. ///
  679. if (IsActiveAfterNextIteration(activate_stream_node)) {
  680. continue;
  681. }
  682. visited_nodes.insert(activate_stream_node);
  683. // nodes in stream link to streamActivate no need to add event before activated node
  684. for (const auto &pre_activate_stream_node : activate_stream_node->GetInNodes()) {
  685. GE_IF_BOOL_EXEC(pre_activate_stream_node->GetOpDesc() == nullptr, continue);
  686. if (pre_activate_stream_node->GetOpDesc()->GetStreamId() == cur_stream_id &&
  687. pre_activate_stream_node->GetOpDesc()->GetId() >= send_node_ptr->GetOpDesc()->GetId()) {
  688. return true;
  689. }
  690. auto in_nodes_of_pre = pre_activate_stream_node->GetInNodes();
  691. if (std::find(in_nodes_of_pre.begin(), in_nodes_of_pre.end(), send_node_ptr) != in_nodes_of_pre.end()) {
  692. return true;
  693. }
  694. }
  695. auto iterator = specific_activated_streams_nodes_map_.find(activate_stream_node->GetOpDesc()->GetStreamId());
  696. if (iterator != specific_activated_streams_nodes_map_.end()) {
  697. auto active_nodes = iterator->second;
  698. for (const auto &active_node : active_nodes) {
  699. activate_stream_nodes_temp.emplace(active_node);
  700. }
  701. }
  702. }
  703. activate_stream_nodes = activate_stream_nodes_temp;
  704. }
  705. }
  706. return false;
  707. }
  708. bool StreamAllocator::IsActiveAfterNextIteration(const NodePtr &active_node_ptr) const {
  709. if ((active_node_ptr == nullptr) || active_node_ptr->GetInControlNodes().empty()) {
  710. return false;
  711. }
  712. for (const auto &in_node : active_node_ptr->GetInControlNodes()) {
  713. if ((in_node->GetType() != NEXTITERATION) && (in_node->GetType() != REFNEXTITERATION)) {
  714. return false;
  715. }
  716. }
  717. return true;
  718. }
  719. // Split the stream according to the maximum number of nodes in the stream.
  720. Status StreamAllocator::SplitStreams(vector<set<int64_t>> &split_streams) {
  721. if (enable_single_stream_ || stream_num_ == 0) {
  722. GELOGI("The single stream option is enabled or the number of streams is 0, no need to split streams.");
  723. return SUCCESS;
  724. }
  725. // stream_node_num_vec records the number of all nodes on each stream
  726. // added_stream_num_vec records the number of streams that each stream needs to increase
  727. // new_stream_id_vec records the new physical stream id for each stream
  728. vector<int64_t> stream_node_num_vec(stream_num_);
  729. vector<int64_t> added_stream_num_vec(stream_num_);
  730. vector<int64_t> new_stream_id_vec(stream_num_);
  731. map<string, int64_t> stream_continuous_2_node_num_map;
  732. map<string, vector<NodePtr>> stream_continuous_2_nodes_map;
  733. map<int64_t, vector<NodePtr>> stream_2_nodes_map;
  734. vector<NodePtr> pre_node_vec(stream_num_);
  735. int64_t last_stream_id = stream_num_ - 1;
  736. for (auto i = 0; i <= last_stream_id; i++) {
  737. stream_node_num_vec[i] = 0;
  738. added_stream_num_vec[i] = 0;
  739. new_stream_id_vec[i] = i;
  740. pre_node_vec[i] = nullptr;
  741. }
  742. uint32_t max_stream_count = 0;
  743. uint32_t max_task_count = 0;
  744. GE_CHK_STATUS_RET(GetMaxStreamAndTask(false, max_stream_count, max_task_count),
  745. "[Get][MaxCount] of stream and task failed.");
  746. for (const auto &cur_node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  747. GE_CHECK_NOTNULL(cur_node);
  748. auto op_desc = cur_node->GetOpDesc();
  749. GE_CHECK_NOTNULL(op_desc);
  750. int64_t stream_id = op_desc->GetStreamId();
  751. if (stream_id == kInvalidStream) {
  752. continue;
  753. }
  754. if (stream_id > last_stream_id) {
  755. REPORT_INNER_ERROR("E19999", "streamid(%ld) > last_stream_id(%ld), check invalid",
  756. stream_id, last_stream_id);
  757. GELOGE(FAILED, "[Check][Param] SplitStreams:streamid(%ld) > last_stream_id(%ld)", stream_id, last_stream_id);
  758. return FAILED;
  759. }
  760. bool is_stream_first_node = (stream_node_num_vec[stream_id] == 0);
  761. AddNodeNum(cur_node, stream_node_num_vec[stream_id]);
  762. stream_2_nodes_map[stream_id].push_back(cur_node);
  763. // The maximum number of tasks per stream.
  764. int64_t max_node_num_one_stream = GetMaxNodeNumPerStream(cur_node, max_task_count);
  765. std::string continuous_stream_label;
  766. if (HasContinuousStreamLabel(op_desc, continuous_stream_label)) {
  767. stream_continuous_2_node_num_map[continuous_stream_label]++;
  768. // return error
  769. if (stream_continuous_2_node_num_map[continuous_stream_label] > max_node_num_one_stream) {
  770. REPORT_INNER_ERROR("E19999", "Check node[%s] stream_id[%ld] continuous stream label[%s] unsatisfied",
  771. op_desc->GetName().c_str(), stream_id, continuous_stream_label.c_str());
  772. GELOGE(FAILED, "[Check][Param] SplitStreams:node[%s] stream_id[%ld] continuous stream label[%s] unsatisfied ",
  773. op_desc->GetName().c_str(), stream_id, continuous_stream_label.c_str());
  774. return FAILED;
  775. }
  776. stream_continuous_2_nodes_map[continuous_stream_label].push_back(cur_node);
  777. }
  778. // Split the stream if it exceeds the maximum number of nodes in the stream.
  779. if (NeedSpiltNewStream(stream_node_num_vec[stream_id], max_node_num_one_stream, op_desc, is_stream_first_node)) {
  780. last_stream_id++;
  781. GELOGI(
  782. "stream_node_num_vec[%ld]= %ld > max_node_num_one_stream : %ld, "
  783. "It's time to split the stream, split newly-added stream id is %ld",
  784. stream_id, stream_node_num_vec[stream_id], max_node_num_one_stream, last_stream_id);
  785. NodePtr pre_node = pre_node_vec[stream_id];
  786. stream_node_num_vec[stream_id] = 0;
  787. AddNodeNum(cur_node, stream_node_num_vec[stream_id]);
  788. // try spilt a new stream and move same continuous stream label nodes from this stream
  789. bool not_use_cur = false;
  790. NodePtr not_cur = nullptr;
  791. std::string cur_continuous_stream_label;
  792. if (HasContinuousStreamLabel(op_desc, cur_continuous_stream_label)) {
  793. // get stored nodes
  794. auto nodes = stream_continuous_2_nodes_map[cur_continuous_stream_label];
  795. GE_RETURN_WITH_LOG_IF_FALSE(!nodes.empty(),
  796. "[Check][Param] split stream with continuous stream label %s failed",
  797. cur_continuous_stream_label.c_str());
  798. for (const auto &node : nodes) {
  799. auto stored_op_desc = node->GetOpDesc();
  800. GE_CHECK_NOTNULL(stored_op_desc);
  801. stored_op_desc->SetStreamId(last_stream_id);
  802. AddNodeNum(node, stream_node_num_vec[stream_id]);
  803. }
  804. not_use_cur = true;
  805. not_cur = nodes.front();
  806. GE_CHECK_NOTNULL(not_cur);
  807. GELOGI("split from first node %s with continuous stream label %s", not_cur->GetName().c_str(),
  808. cur_continuous_stream_label.c_str());
  809. auto iter = std::find(stream_2_nodes_map[stream_id].begin(), stream_2_nodes_map[stream_id].end(), not_cur);
  810. GE_RETURN_WITH_LOG_IF_FALSE(
  811. (iter != stream_2_nodes_map[stream_id].end()) && (iter != stream_2_nodes_map[stream_id].begin()),
  812. "[Check][Param] split stream with continuous stream label %s failed", cur_continuous_stream_label.c_str());
  813. iter--;
  814. pre_node = *iter;
  815. }
  816. added_stream_num_vec[stream_id]++;
  817. new_stream_id_vec[stream_id] = last_stream_id;
  818. split_streams[stream_id].emplace(last_stream_id);
  819. split_ori_stream_map_[last_stream_id] = stream_id;
  820. node_split_stream_map_[cur_node] = last_stream_id;
  821. // Add the send/recv event to the first and last nodes of the split stream.
  822. if (pre_node != nullptr) {
  823. GE_CHK_STATUS_RET(AddEventId(pre_node, not_cur, cur_node, not_use_cur),
  824. "[Add][EventId] failed, pre node:%s, not cur node:%s, cur node:%s.",
  825. pre_node->GetName().c_str(), not_cur->GetName().c_str(), cur_node->GetName().c_str());
  826. }
  827. }
  828. /// If the split stream num is greater than 1, the node behind the same
  829. /// stream must reset the new stream id.
  830. if (added_stream_num_vec[stream_id] >= 1) {
  831. op_desc->SetStreamId(new_stream_id_vec[stream_id]);
  832. }
  833. pre_node_vec[stream_id] = cur_node;
  834. }
  835. if (last_stream_id >= 0) {
  836. stream_num_ = last_stream_id + 1;
  837. }
  838. return SUCCESS;
  839. }
  840. bool StreamAllocator::NeedSpiltNewStream(int64_t stream_node_num, int64_t max_node_num_one_stream,
  841. const OpDescPtr &op_desc, bool is_stream_first_node) const {
  842. if (is_stream_first_node) {
  843. GELOGD("First node of stream does not need to split new stream");
  844. return false;
  845. }
  846. const set<string> label_op_types({LABELSET, LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX});
  847. bool is_first_active_node = false;
  848. (void)AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_FIRST_ACTIVE, is_first_active_node);
  849. return (stream_node_num > max_node_num_one_stream && op_desc->GetSubgraphInstanceNames().empty() &&
  850. !is_first_active_node && label_op_types.count(op_desc->GetType()) == 0);
  851. }
  852. Status StreamAllocator::UpdateActiveStreams(const vector<set<int64_t>> &split_streams) {
  853. UpdateLabelStreams(split_streams);
  854. for (auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  855. if ((node->GetType() == STREAMSWITCH) || (node->GetType() == STREAMSWITCHN)) {
  856. if (UpdateActiveStreamsForSwitchNode(node) != SUCCESS) {
  857. GELOGE(FAILED, "[Update][ActiveStreams] for switch node: %s failed.", node->GetName().c_str());
  858. return FAILED;
  859. }
  860. } else {
  861. if (UpdateActiveStreamsForActiveNode(split_streams, node) != SUCCESS) {
  862. GELOGE(FAILED, "[Update][ActiveStreams] for active node: %s failed.", node->GetName().c_str());
  863. return FAILED;
  864. }
  865. }
  866. }
  867. Status status = UpdateActiveStreamsForSubgraphs();
  868. if (status != SUCCESS) {
  869. GELOGE(status, "[Update][ActiveStreams] for subgraphs failed! graph:%s", whole_graph_->GetName().c_str());
  870. return status;
  871. }
  872. status = SetActiveStreamsForLoop();
  873. if (status != SUCCESS) {
  874. GELOGE(status, "[Set][ActiveStreams] For Loop failed! graph:%s", whole_graph_->GetName().c_str());
  875. return status;
  876. }
  877. return SUCCESS;
  878. }
  879. void StreamAllocator::UpdateLabelStreams(const vector<set<int64_t>> &split_streams) {
  880. for (size_t i = 0; i < split_streams.size(); i++) {
  881. auto &streams = split_streams[i];
  882. if (streams.empty()) {
  883. continue;
  884. }
  885. if (specific_activated_streams_.count(static_cast<int64_t>(i)) > 0) {
  886. specific_activated_streams_.insert(streams.begin(), streams.end());
  887. }
  888. for (auto &labeled_stream : labeled_streams_) {
  889. if (labeled_stream.second.count(static_cast<int64_t>(i)) > 0) {
  890. labeled_stream.second.insert(streams.begin(), streams.end());
  891. break;
  892. }
  893. }
  894. }
  895. }
  896. Status StreamAllocator::UpdateActiveStreamsForSwitchNode(NodePtr &switch_node) {
  897. vector<NodePtr> active_nodes;
  898. if (InsertActiveNodesAfterSwitch(switch_node, active_nodes) != SUCCESS) {
  899. GELOGE(FAILED, "[Insert][ActiveNodes] after node %s failed.", switch_node->GetName().c_str());
  900. return FAILED;
  901. }
  902. if (active_nodes.empty()) {
  903. return SUCCESS;
  904. }
  905. vector<int64_t> stream_ids;
  906. for (auto &active_node : active_nodes) {
  907. GE_CHECK_NOTNULL(active_node->GetOpDesc());
  908. active_node->GetOpDesc()->SetStreamId(stream_num_);
  909. stream_ids.emplace_back(stream_num_);
  910. specific_activated_streams_.emplace(stream_num_);
  911. stream_num_++;
  912. }
  913. auto op_desc = switch_node->GetOpDesc();
  914. GE_CHECK_NOTNULL(op_desc);
  915. if (!AttrUtils::SetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, stream_ids)) {
  916. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  917. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  918. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  919. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  920. return FAILED;
  921. }
  922. return SUCCESS;
  923. }
  924. Status StreamAllocator::InsertActiveNodesAfterSwitch(NodePtr &switch_node, vector<NodePtr> &active_nodes) {
  925. GE_CHECK_NOTNULL(switch_node);
  926. OpDescPtr switch_desc = switch_node->GetOpDesc();
  927. GE_CHECK_NOTNULL(switch_desc);
  928. vector<string> ori_active_label_list;
  929. if (!AttrUtils::GetListStr(switch_desc, ATTR_NAME_ACTIVE_LABEL_LIST, ori_active_label_list) ||
  930. ori_active_label_list.empty()) {
  931. REPORT_INNER_ERROR("E19999", "Get Attr:%s fail from op:%s(%s)", ATTR_NAME_ACTIVE_LABEL_LIST.c_str(),
  932. switch_node->GetName().c_str(), switch_node->GetType().c_str());
  933. GELOGE(INTERNAL_ERROR, "[Get][Attr] active label list of switch %s failed.", switch_node->GetName().c_str());
  934. return INTERNAL_ERROR;
  935. }
  936. vector<string> active_label_list;
  937. vector<NodePtr> added_active_nodes;
  938. if (AddActiveNodes(switch_node, ori_active_label_list, active_label_list, added_active_nodes) != SUCCESS) {
  939. GELOGE(FAILED, "[Add][ActiveNodes] after node %s failed.", switch_node->GetName().c_str());
  940. return FAILED;
  941. }
  942. if (SetActiveLabelList(switch_node, active_label_list) != SUCCESS) {
  943. GELOGE(FAILED, "[Set][ActiveLabelList] failed, node:%s", switch_node->GetName().c_str());
  944. return FAILED;
  945. }
  946. if (added_active_nodes.empty()) {
  947. return SUCCESS;
  948. }
  949. for (auto &active_node : added_active_nodes) {
  950. GE_CHECK_NOTNULL(switch_node->GetOutControlAnchor());
  951. if (switch_node->GetOutControlAnchor()->LinkTo(active_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  952. REPORT_CALL_ERROR("E19999", "Link from %s to %s failed",
  953. switch_node->GetName().c_str(), active_node->GetName().c_str());
  954. GELOGE(FAILED, "[Link][Nodes] from %s to %s failed.",
  955. switch_node->GetName().c_str(), active_node->GetName().c_str());
  956. return FAILED;
  957. }
  958. active_nodes.emplace_back(active_node);
  959. }
  960. return SUCCESS;
  961. }
  962. Status StreamAllocator::UpdateActiveStreamsForActiveNode(const vector<set<int64_t>> &split_streams, NodePtr &node) {
  963. GE_CHECK_NOTNULL(node->GetOpDesc());
  964. vector<uint32_t> active_streams;
  965. if (AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  966. vector<uint32_t> new_active_streams = active_streams;
  967. for (uint32_t logical_stream : active_streams) {
  968. if (static_cast<size_t>(logical_stream) >= split_streams.size()) {
  969. REPORT_INNER_ERROR("E19999", "Check logical stream:%u is out of range:%zu",
  970. logical_stream, split_streams.size());
  971. GELOGE(FAILED, "[Check][Param] logical stream:%u is out of range(0, %zu).",
  972. logical_stream, split_streams.size());
  973. return FAILED;
  974. }
  975. const set<int64_t> &new_split_streams = split_streams[logical_stream];
  976. for (int64_t split_stream : new_split_streams) {
  977. for (const auto &node_stream : node_split_stream_map_) {
  978. if (split_stream == node_stream.second) {
  979. if (node_stream.first->GetOwnerComputeGraph() == node->GetOwnerComputeGraph()) {
  980. new_active_streams.emplace_back(static_cast<uint32_t>(split_stream));
  981. GELOGI("Add stream %ld to active_stream_list of node %s of graph %s", split_stream,
  982. node->GetName().c_str(), node->GetOwnerComputeGraph()->GetName().c_str());
  983. }
  984. break;
  985. }
  986. }
  987. }
  988. }
  989. if (!AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, new_active_streams)) {
  990. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  991. node->GetName().c_str(), node->GetType().c_str());
  992. GELOGE(FAILED, "[Set][Attr] active streams for node %s failed.", node->GetName().c_str());
  993. return FAILED;
  994. }
  995. }
  996. return SUCCESS;
  997. }
  998. Status StreamAllocator::UpdateActiveStreamsForSubgraphs() {
  999. // Update active stream list for active nodes
  1000. for (auto &node_stream_pair : node_split_stream_map_) {
  1001. auto node = node_stream_pair.first;
  1002. auto subgraph = node->GetOwnerComputeGraph();
  1003. if (subgraph->GetParentNode() == nullptr) {
  1004. continue;
  1005. }
  1006. // Skip streams with label
  1007. GE_CHECK_NOTNULL(node->GetOpDesc());
  1008. string stream_label;
  1009. if (AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label) && !stream_label.empty()) {
  1010. continue;
  1011. }
  1012. auto it = subgraph_first_active_node_map_.find(subgraph);
  1013. if (it == subgraph_first_active_node_map_.end()) {
  1014. continue;
  1015. }
  1016. const auto &active_node = it->second;
  1017. GE_CHECK_NOTNULL(active_node);
  1018. auto active_op = active_node->GetOpDesc();
  1019. GE_CHECK_NOTNULL(active_op);
  1020. vector<uint32_t> active_streams;
  1021. (void)AttrUtils::GetListInt(active_op, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams);
  1022. set<uint32_t> new_active_streams(active_streams.begin(), active_streams.end());
  1023. // specific_activated_streams_ has already contained new split activated stream
  1024. int64_t new_split_stream = node_stream_pair.second;
  1025. if (IsActivated(new_split_stream)) {
  1026. continue;
  1027. }
  1028. specific_activated_streams_.emplace(new_split_stream);
  1029. new_active_streams.emplace(static_cast<uint32_t>(new_split_stream));
  1030. active_streams.assign(new_active_streams.begin(), new_active_streams.end());
  1031. if (!AttrUtils::SetListInt(active_op, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1032. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1033. active_op->GetName().c_str(), active_op->GetType().c_str());
  1034. GELOGE(FAILED, "[Set][Attr] active streams for node %s failed.", active_node->GetName().c_str());
  1035. return FAILED;
  1036. }
  1037. }
  1038. return SUCCESS;
  1039. }
  1040. bool StreamAllocator::IsActivated(int64_t stream_id) const {
  1041. const auto &iter = split_ori_stream_map_.find(stream_id);
  1042. if (iter == split_ori_stream_map_.end()) {
  1043. REPORT_INNER_ERROR("E19999", "Find original stream_id failed, split_stream_id=%ld", stream_id);
  1044. GELOGE(INTERNAL_ERROR, "[CheckActivated][Check] Find original stream_id failed, split_stream_id=%ld", stream_id);
  1045. return false;
  1046. }
  1047. int64_t ori_stream_id = iter->second;
  1048. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1049. auto op_desc = node->GetOpDesc();
  1050. vector<uint32_t> active_streams;
  1051. if (op_desc == nullptr || !AttrUtils::GetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1052. continue;
  1053. }
  1054. if (std::find(active_streams.begin(), active_streams.end(), stream_id) != active_streams.end() ||
  1055. std::find(active_streams.begin(), active_streams.end(), ori_stream_id) != active_streams.end()) {
  1056. return true;
  1057. }
  1058. }
  1059. return false;
  1060. }
  1061. // Iteraotor loop :
  1062. // StreamSwitch -> StreamActive
  1063. // FpBp loop:
  1064. // StreamSwitch -> AssignAdd -> StreamActive
  1065. NodePtr FindSwitchNodeBeforeLoopActiveNode(const NodePtr &active_node) {
  1066. for (auto pre_node : active_node->GetInControlNodes()) {
  1067. if (pre_node->GetType() == STREAMSWITCH) {
  1068. return pre_node;
  1069. }
  1070. for (auto pre_pre_node : pre_node->GetInControlNodes()) {
  1071. if (pre_pre_node->GetType() == STREAMSWITCH) {
  1072. return pre_pre_node;
  1073. }
  1074. }
  1075. }
  1076. return nullptr;
  1077. }
  1078. Status StreamAllocator::SetActiveStreamsForLoop() {
  1079. vector<uint32_t> loop_active_streams;
  1080. for (int64_t stream_id = 0; stream_id < stream_num_; stream_id++) {
  1081. if (specific_activated_streams_.count(stream_id) == 0) {
  1082. loop_active_streams.emplace_back(static_cast<uint32_t>(stream_id));
  1083. }
  1084. }
  1085. map<int64_t, NodePtr> stream_id_to_last_node;
  1086. set<int64_t> streams_skip_iterator_event;
  1087. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1088. int64_t stream_id = node->GetOpDesc()->GetStreamId();
  1089. if (find(loop_active_streams.begin(), loop_active_streams.end(), stream_id) != loop_active_streams.end()) {
  1090. stream_id_to_last_node[stream_id] = node;
  1091. // last node in stream which has streamswitch or IF may be not execute, it will cause block if add event on them
  1092. if (node->GetOpDesc()->GetType() == STREAMSWITCH) {
  1093. streams_skip_iterator_event.insert(stream_id);
  1094. }
  1095. }
  1096. }
  1097. // Set the stream that needs to be activated
  1098. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1099. GE_CHECK_NOTNULL(node->GetOpDesc());
  1100. bool is_loop_active = false;
  1101. (void)AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_IS_LOOP_ACTIVE, is_loop_active);
  1102. if (is_loop_active) {
  1103. vector<string> activated_label_list;
  1104. NodePtr pre_switch_node = FindSwitchNodeBeforeLoopActiveNode(node);
  1105. if (pre_switch_node == nullptr) {
  1106. REPORT_INNER_ERROR("E19999", "Find switch node before loop active node %s fail",
  1107. node->GetName().c_str());
  1108. GELOGE(FAILED, "[Find][SwitchNode] before loop active node %s failed", node->GetName().c_str());
  1109. return FAILED;
  1110. }
  1111. if (!AttrUtils::GetListStr(node->GetOpDesc(), ATTR_NAME_ACTIVE_LABEL_LIST, activated_label_list) ||
  1112. activated_label_list.empty()) {
  1113. GE_CHK_BOOL_EXEC(AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, loop_active_streams),
  1114. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)",
  1115. ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1116. node->GetName().c_str(), node->GetType().c_str());
  1117. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1118. node->GetName().c_str(), node->GetType().c_str());
  1119. return FAILED);
  1120. for (const auto &stream_id : loop_active_streams) {
  1121. GELOGI("Active stream %u for node: %s.", stream_id, node->GetName().c_str());
  1122. }
  1123. // In switch group optimze case, some data input branch may exec slowly.
  1124. // when condition input branch judge false and some switch has no false branch,
  1125. // In this condition, data branch has no synchronize point,
  1126. // it may cause some stream actived by iterator next step when this stream still alive.
  1127. // If above situation happen, active message will lose, cause process block in next iteration.
  1128. // In order to avoid this abnormal happen,
  1129. // add event between each last node and iterator switch node
  1130. GELOGI("there are %zu next iterator target streams has streamswitch node.", streams_skip_iterator_event.size());
  1131. for (auto iter : stream_id_to_last_node) {
  1132. if (streams_skip_iterator_event.find(iter.first) != streams_skip_iterator_event.end()) {
  1133. GELOGI("Skip stream %ld which has streamswitch node when adding event to next iterator active node",
  1134. iter.first);
  1135. continue;
  1136. }
  1137. if (iter.second->GetOwnerComputeGraph()->GetParentGraph() != nullptr) {
  1138. GELOGI("Skip stream %ld which is last node in subgraph when adding event to next iterator active node",
  1139. iter.first);
  1140. continue;
  1141. }
  1142. AddSendEventId(iter.second, event_num_);
  1143. AddRecvEventId(pre_switch_node, event_num_);
  1144. event_num_++;
  1145. }
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. return CheckStreamActived();
  1151. }
  1152. Status StreamAllocator::CheckStreamActived() const {
  1153. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1154. GE_CHECK_NOTNULL(node->GetOpDesc());
  1155. vector<uint32_t> active_streams;
  1156. if (AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1157. uint32_t stream_id = static_cast<uint32_t>(node->GetOpDesc()->GetStreamId());
  1158. auto iter = find(active_streams.begin(), active_streams.end(), stream_id);
  1159. if (iter != active_streams.end()) {
  1160. REPORT_INNER_ERROR("E19999", "Node:%s(%s) cannot active its own stream %u, check invalid ",
  1161. node->GetName().c_str(), node->GetType().c_str(), stream_id);
  1162. GELOGE(FAILED, "[Check][Param] Node %s cannot active its own stream %u.", node->GetName().c_str(), stream_id);
  1163. return FAILED;
  1164. }
  1165. }
  1166. }
  1167. return SUCCESS;
  1168. }
  1169. Status StreamAllocator::ReuseEvent(bool send_to,
  1170. const std::unordered_map<std::string, ge::NodePtr> &name_to_node_map,
  1171. const std::unordered_map<ge::NodePtr, std::vector<std::pair<std::string, uint32_t>>> &node_to_event_id) {
  1172. for (const auto &node_event_id : node_to_event_id) {
  1173. ge::NodePtr curr_node = node_event_id.first;
  1174. NodePtr send_node = send_to ? curr_node : nullptr;
  1175. NodePtr recv_node = send_to ? nullptr : curr_node;
  1176. for (const auto &event_pair : node_event_id.second) {
  1177. auto peer_node_iter = name_to_node_map.find(event_pair.first);
  1178. if (peer_node_iter == name_to_node_map.end()) {
  1179. GELOGE(PARAM_INVALID, "[Get][Node]Name:%s.", event_pair.first.c_str());
  1180. REPORT_INNER_ERROR("E19999", "Failed to find node, name:%s.", event_pair.first.c_str());
  1181. return PARAM_INVALID;
  1182. }
  1183. recv_node = send_to ? peer_node_iter->second : recv_node;
  1184. send_node = send_to ? send_node : peer_node_iter->second;
  1185. GE_CHECK_NOTNULL(send_node);
  1186. GE_CHECK_NOTNULL(recv_node);
  1187. auto event_id = GetIntersection(node_to_send_events_[send_node], node_to_recv_events_[recv_node]);
  1188. uint32_t new_event = event_pair.second + event_num_;
  1189. if (event_id.empty()) {
  1190. GELOGI("[Check][Optimized]Send:%s, recv:%s.", send_node->GetName().c_str(), recv_node->GetName().c_str());
  1191. continue;
  1192. } else if (event_id.size() != 1) {
  1193. GELOGW("[Check][Event]More than one event are found between %s and %s, event num:%zu.",
  1194. send_node->GetName().c_str(), recv_node->GetName().c_str(), event_id.size());
  1195. }
  1196. uint32_t old_event = event_id[0];
  1197. auto reuse_event_id = [] (vector<uint32_t> &event_list, uint32_t old_event, uint32_t new_event) -> void {
  1198. event_list.erase(std::remove(event_list.begin(), event_list.end(), old_event), event_list.end());
  1199. event_list.push_back(new_event);
  1200. return;
  1201. };
  1202. reuse_event_id(node_to_send_events_[send_node], old_event, new_event);
  1203. reuse_event_id(node_to_recv_events_[recv_node], old_event, new_event);
  1204. GELOGI("[Reuse][Event]Replace event successfully, send node:%s, recv node:%s, old id:%u, new id:%u.",
  1205. send_node->GetName().c_str(), recv_node->GetName().c_str(), old_event, new_event);
  1206. }
  1207. }
  1208. return ge::SUCCESS;
  1209. }
  1210. // Refresh events to reuse events
  1211. Status StreamAllocator::RefreshEventsWithReuse() {
  1212. GELOGI("[Refresh][Events]Refresh events with reuse, stream num:%ld, original event num:%u.", stream_num_, event_num_);
  1213. if (event_num_ <= kEventReuseThreshold) {
  1214. GELOGI("[Check][ReuseThreshold]Event used num is %u, less than %u, skip reuse.",
  1215. event_num_, kEventReuseThreshold);
  1216. return SUCCESS;
  1217. }
  1218. std::unordered_map<std::string, NodePtr> name_to_node_map;
  1219. std::unordered_map<NodePtr, std::vector<std::pair<std::string, uint32_t>>> node_to_send;
  1220. std::unordered_map<NodePtr, std::vector<std::pair<std::string, uint32_t>>> node_to_recv;
  1221. Status ret = ParseAllNodeEventMultiplexing(whole_graph_, name_to_node_map, node_to_send, node_to_recv);
  1222. if (ret != SUCCESS) {
  1223. GELOGE(ret, "[Parse][AllNodeEventMultiplexing]Graph:%s.", whole_graph_->GetName().c_str());
  1224. REPORT_INNER_ERROR("E19999", "Failed to parse all node event multiplexing, graph:%s.",
  1225. whole_graph_->GetName().c_str());
  1226. return ret;
  1227. }
  1228. if (node_to_send.empty() && node_to_recv.empty()) {
  1229. return SUCCESS;
  1230. }
  1231. ret = ReuseEvent(true, name_to_node_map, node_to_send);
  1232. if (ret != SUCCESS) {
  1233. GELOGE(ret, "[Reuse][Event]Phase:Send, graph:%s.", whole_graph_->GetName().c_str());
  1234. REPORT_INNER_ERROR("E19999", "Failed to reuse event, phase:Send, graph:%s.", whole_graph_->GetName().c_str());
  1235. return ret;
  1236. }
  1237. ret = ReuseEvent(false, name_to_node_map, node_to_recv);
  1238. if (ret != SUCCESS) {
  1239. GELOGE(ret, "[Reuse][Event]Phase:Recv, graph:%s.", whole_graph_->GetName().c_str());
  1240. REPORT_INNER_ERROR("E19999", "Failed to reuse event, phase:Recv, graph:%s.", whole_graph_->GetName().c_str());
  1241. return ret;
  1242. }
  1243. Status status = RefreshContinuousEvents();
  1244. if (status != SUCCESS) {
  1245. GELOGE(status, "[Refresh][ContinuousEvents]Graph:%s.", whole_graph_->GetName().c_str());
  1246. REPORT_INNER_ERROR("E19999", "Failed to refresh continuous events, graph:%s.", whole_graph_->GetName().c_str());
  1247. return status;
  1248. }
  1249. GELOGI("[Refresh][Events]RefreshEventsWithReuse successfully, event num:%u.", event_num_);
  1250. return SUCCESS;
  1251. }
  1252. // Refresh events to continuous events
  1253. Status StreamAllocator::RefreshContinuousEvents() {
  1254. // Establish a mapping relationship from old to new event id
  1255. map<uint32_t, uint32_t> old_to_new_events;
  1256. uint32_t new_event_id = 0;
  1257. for (const auto &one_pair : node_to_send_events_) {
  1258. for (const auto &event_id : one_pair.second) {
  1259. if (old_to_new_events.find(event_id) == old_to_new_events.end()) {
  1260. old_to_new_events[event_id] = new_event_id;
  1261. new_event_id++;
  1262. }
  1263. }
  1264. }
  1265. // Refresh send event id
  1266. for (auto &one_pair : node_to_send_events_) {
  1267. vector<uint32_t> &send_events = one_pair.second;
  1268. for (size_t i = 0; i < send_events.size(); i++) {
  1269. auto find_it = old_to_new_events.find(send_events[i]);
  1270. if (find_it == old_to_new_events.end()) {
  1271. REPORT_INNER_ERROR("E19999", "Check invalid send event %u", send_events[i]);
  1272. GELOGE(FAILED, "[Check][Param] RefreshContinuousEvents: invalid send event %u", send_events[i]);
  1273. return FAILED;
  1274. }
  1275. send_events[i] = find_it->second;
  1276. }
  1277. }
  1278. // Refresh recv event id
  1279. for (auto &one_pair : node_to_recv_events_) {
  1280. vector<uint32_t> &recv_events = one_pair.second;
  1281. for (size_t i = 0; i < recv_events.size(); i++) {
  1282. auto find_it = old_to_new_events.find(recv_events[i]);
  1283. if (find_it == old_to_new_events.end()) {
  1284. REPORT_INNER_ERROR("E19999", "Check invalid recv event %u", recv_events[i]);
  1285. GELOGE(FAILED, "[Check][Param] RefreshContinuousEvents: invalid recv event %u", recv_events[i]);
  1286. return FAILED;
  1287. }
  1288. recv_events[i] = find_it->second;
  1289. }
  1290. }
  1291. event_num_ = static_cast<uint32_t>(old_to_new_events.size());
  1292. return SUCCESS;
  1293. }
  1294. // Insert the real send/recv node in the graph
  1295. Status StreamAllocator::InsertSyncEventNodes() {
  1296. unordered_map<string, uint32_t> sync_event_name;
  1297. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1298. // Add the node corresponding to the recv event
  1299. vector<uint32_t> recv_event_id_list;
  1300. GetRecvEventIdList(node, recv_event_id_list);
  1301. GE_CHECK_NOTNULL(node->GetOpDesc());
  1302. GE_CHECK_NOTNULL(node->GetInControlAnchor());
  1303. GE_CHECK_NOTNULL(node->GetOutControlAnchor());
  1304. for (auto &event_id : recv_event_id_list) {
  1305. string recv_node_name = whole_graph_->GetName() + "_Recv_" + to_string(event_id);
  1306. auto iter = sync_event_name.find(recv_node_name);
  1307. if (iter == sync_event_name.end()) {
  1308. sync_event_name[recv_node_name] = 1;
  1309. } else {
  1310. recv_node_name = recv_node_name + "_Reuse_" + to_string(iter->second);
  1311. ++(iter->second);
  1312. }
  1313. OpDescPtr op_desc_ptr = MakeShared<OpDesc>(recv_node_name, RECV);
  1314. GE_CHECK_NOTNULL(op_desc_ptr);
  1315. int64_t temp_stream_id = node->GetOpDesc()->GetStreamId();
  1316. op_desc_ptr->SetStreamId(temp_stream_id);
  1317. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(op_desc_ptr, RECV_ATTR_EVENT_ID, event_id),
  1318. REPORT_INNER_ERROR("E19999", "Set Attr:%s for op:%s(%s) failed, event_id:%u,",
  1319. RECV_ATTR_EVENT_ID.c_str(),
  1320. node->GetName().c_str(), node->GetType().c_str(), event_id);
  1321. GELOGE(FAILED, "[Set][Attr] %s for op:%s(%s) failed, event_id:%u,",
  1322. RECV_ATTR_EVENT_ID.c_str(), node->GetName().c_str(), node->GetType().c_str(), event_id);
  1323. return FAILED);
  1324. (void)AttrUtils::SetListStr(op_desc_ptr, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
  1325. std::move(std::vector<std::string>()));
  1326. NodePtr recv_node = node->GetOwnerComputeGraph()->AddNode(op_desc_ptr);
  1327. GE_CHECK_NOTNULL(recv_node);
  1328. GE_CHECK_NOTNULL(recv_node->GetOutControlAnchor());
  1329. Status status = GraphUtils::AddEdge(recv_node->GetOutControlAnchor(), node->GetInControlAnchor());
  1330. if (status != SUCCESS) {
  1331. REPORT_INNER_ERROR("E19999", "Add edge from node %s to node %s failed",
  1332. recv_node->GetName().c_str(), node->GetName().c_str());
  1333. GELOGE(status, "[Add][Edge] for node %s and node %s failed.", recv_node->GetName().c_str(),
  1334. node->GetName().c_str());
  1335. return status;
  1336. }
  1337. GELOGI("Insert recv event %u before node: %s.", event_id, node->GetName().c_str());
  1338. }
  1339. // Add the node corresponding to the send event
  1340. vector<uint32_t> send_event_id_list;
  1341. GetSendEventIdList(node, send_event_id_list);
  1342. for (auto &event_id : send_event_id_list) {
  1343. string send_node_name = whole_graph_->GetName() + "_Send_" + to_string(event_id);
  1344. auto iter = sync_event_name.find(send_node_name);
  1345. if (iter == sync_event_name.end()) {
  1346. sync_event_name[send_node_name] = 1;
  1347. } else {
  1348. send_node_name = send_node_name + "_Reuse_" + to_string(iter->second);
  1349. ++(iter->second);
  1350. }
  1351. OpDescPtr op_desc_ptr = MakeShared<OpDesc>(send_node_name, SEND);
  1352. GE_CHECK_NOTNULL(op_desc_ptr);
  1353. int64_t temp_stream_id = node->GetOpDesc()->GetStreamId();
  1354. op_desc_ptr->SetStreamId(temp_stream_id);
  1355. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(op_desc_ptr, SEND_ATTR_EVENT_ID, event_id), GELOGE(FAILED, "SetInt failed.");
  1356. return FAILED);
  1357. (void)AttrUtils::SetListStr(op_desc_ptr, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
  1358. std::move(std::vector<std::string>()));
  1359. NodePtr send_node = node->GetOwnerComputeGraph()->AddNode(op_desc_ptr);
  1360. GE_CHECK_NOTNULL(send_node);
  1361. GE_CHECK_NOTNULL(send_node->GetInControlAnchor());
  1362. Status status = GraphUtils::AddEdge(node->GetOutControlAnchor(), send_node->GetInControlAnchor());
  1363. if (status != SUCCESS) {
  1364. REPORT_INNER_ERROR("E19999", "Add edge from node %s to node %s failed",
  1365. node->GetName().c_str(), send_node->GetName().c_str());
  1366. GELOGE(status, "[Add][Edge] for node %s and node %s failed.", node->GetName().c_str(),
  1367. send_node->GetName().c_str());
  1368. return status;
  1369. }
  1370. GELOGI("Insert send event %u after node: %s.", event_id, node->GetName().c_str());
  1371. }
  1372. }
  1373. Status status = whole_graph_->InsertGraphEvents();
  1374. if (status != SUCCESS) {
  1375. REPORT_CALL_ERROR("E19999", "Insert Graph Events fail, graph:%s,",
  1376. whole_graph_->GetName().c_str());
  1377. GELOGE(status, "[Insert][GraphEvents] Graph ReorderEventNodes failed, graph:%s,",
  1378. whole_graph_->GetName().c_str());
  1379. return status;
  1380. }
  1381. return SUCCESS;
  1382. }
  1383. void StreamAllocator::DumpEvents() {
  1384. map<int64_t, vector<NodePtr>> after_refresh_stream_nodes;
  1385. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1386. GE_IF_BOOL_EXEC(node->GetOpDesc() == nullptr, continue);
  1387. int64_t stream_id = node->GetOpDesc()->GetStreamId();
  1388. after_refresh_stream_nodes[stream_id].emplace_back(node);
  1389. }
  1390. for (const auto &one_pair : after_refresh_stream_nodes) {
  1391. int64_t stream_id = one_pair.first;
  1392. GELOGD("After RefreshRealStream: stream %ld.", stream_id);
  1393. for (const auto &node : one_pair.second) {
  1394. if (node == nullptr || node->GetOpDesc() == nullptr) {
  1395. continue;
  1396. }
  1397. string send_event_str;
  1398. for (const auto &send_event_id : node_to_send_events_[node]) {
  1399. send_event_str += " " + to_string(send_event_id);
  1400. }
  1401. if (!send_event_str.empty()) {
  1402. GELOGI("node: %s, id: %ld, stream id :%ld, send events: %s.", node->GetName().c_str(),
  1403. node->GetOpDesc()->GetId(), node->GetOpDesc()->GetStreamId(), send_event_str.c_str());
  1404. }
  1405. string recv_event_str;
  1406. for (const auto &recv_event_id : node_to_recv_events_[node]) {
  1407. recv_event_str += " " + to_string(recv_event_id);
  1408. }
  1409. if (!recv_event_str.empty()) {
  1410. GELOGI("node: %s, id: %ld, stream id :%ld, recv events: %s.", node->GetName().c_str(),
  1411. node->GetOpDesc()->GetId(), node->GetOpDesc()->GetStreamId(), recv_event_str.c_str());
  1412. }
  1413. }
  1414. }
  1415. }
  1416. Status StreamAllocator::GetMaxStreamAndTask(bool huge_stream, uint32_t &max_stream_count, uint32_t &max_task_count) {
  1417. uint32_t stream_type = RT_NORMAL_STREAM;
  1418. if (huge_stream) {
  1419. stream_type = RT_HUGE_STREAM;
  1420. }
  1421. rtError_t ret = rtGetMaxStreamAndTask(stream_type, &max_stream_count, &max_task_count);
  1422. if (ret != RT_ERROR_NONE) {
  1423. REPORT_CALL_ERROR("E19999", "call rtGetMaxStreamAndTask fail, ret:%d, stream_type:%u,",
  1424. static_cast<int>(ret), stream_type);
  1425. GELOGE(FAILED, "[Call][RtGetMaxStreamAndTask] Get max stream and task count by rts failed, "
  1426. "ret:%d, stream_type:%u,", static_cast<int>(ret), stream_type);
  1427. return FAILED;
  1428. }
  1429. GELOGD("Allowed max stream count: %u, max task count per stream: %u.", max_stream_count, max_task_count);
  1430. return SUCCESS;
  1431. }
  1432. int64_t StreamAllocator::GetMaxNodeNumPerStream(const NodePtr &node, uint32_t max_task_count) {
  1433. int64_t max_node_num_one_stream = static_cast<int64_t>(max_task_count);
  1434. string op_type = node->GetType();
  1435. if (IsHcclOp(op_type)) {
  1436. max_node_num_one_stream /= kTaskNumPerHcclNode;
  1437. } else {
  1438. max_node_num_one_stream /= kTaskNumPerNormalNode;
  1439. }
  1440. if (max_node_num_one_stream == 0) {
  1441. max_node_num_one_stream = 1;
  1442. }
  1443. return max_node_num_one_stream;
  1444. }
  1445. void StreamAllocator::AddNodeNum(const NodePtr &node, int64_t &node_num) {
  1446. node_num++;
  1447. vector<uint32_t> events;
  1448. GetSendEventIdList(node, events);
  1449. node_num += static_cast<int64_t>(events.size());
  1450. GetRecvEventIdList(node, events);
  1451. node_num += static_cast<int64_t>(events.size());
  1452. }
  1453. // Insert send event id on a node
  1454. void StreamAllocator::AddSendEventId(const NodePtr &node, uint32_t event_id) {
  1455. node_to_send_events_[node].emplace_back(event_id);
  1456. }
  1457. // Insert recv event id on a node
  1458. void StreamAllocator::AddRecvEventId(const NodePtr &node, uint32_t event_id) {
  1459. node_to_recv_events_[node].emplace_back(event_id);
  1460. }
  1461. // Remove send event id from a node
  1462. void StreamAllocator::RmvSendEventId(const NodePtr &node, uint32_t event_id) {
  1463. auto find_it = node_to_send_events_.find(node);
  1464. if (find_it == node_to_send_events_.end()) {
  1465. return;
  1466. }
  1467. vector<uint32_t> &send_events = find_it->second;
  1468. for (auto it = send_events.begin(); it != send_events.end(); ++it) {
  1469. if (*it == event_id) {
  1470. send_events.erase(it);
  1471. return;
  1472. }
  1473. }
  1474. }
  1475. // Remove recv event id from a node
  1476. void StreamAllocator::RmvRecvEventId(const NodePtr &node, uint32_t event_id) {
  1477. auto find_it = node_to_recv_events_.find(node);
  1478. if (find_it == node_to_recv_events_.end()) {
  1479. return;
  1480. }
  1481. vector<uint32_t> &recv_events = find_it->second;
  1482. for (auto it = recv_events.begin(); it != recv_events.end(); ++it) {
  1483. if (*it == event_id) {
  1484. recv_events.erase(it);
  1485. return;
  1486. }
  1487. }
  1488. }
  1489. // Get send event id list from a node
  1490. void StreamAllocator::GetSendEventIdList(const NodePtr &node, vector<uint32_t> &send_list) const {
  1491. send_list.clear();
  1492. auto find_it = node_to_send_events_.find(node);
  1493. if (find_it != node_to_send_events_.end()) {
  1494. send_list = find_it->second;
  1495. }
  1496. }
  1497. // Get recv event id list from a node
  1498. void StreamAllocator::GetRecvEventIdList(const NodePtr &node, vector<uint32_t> &recv_list) const {
  1499. recv_list.clear();
  1500. auto find_it = node_to_recv_events_.find(node);
  1501. if (find_it != node_to_recv_events_.end()) {
  1502. recv_list = find_it->second;
  1503. }
  1504. }
  1505. // Get a specific send node according to the recv event
  1506. NodePtr StreamAllocator::GetNodeFromSendEventId(uint32_t send_event_id) const {
  1507. for (const auto &one_pair : node_to_send_events_) {
  1508. const vector<uint32_t> &events = one_pair.second;
  1509. for (const auto &event_id : events) {
  1510. if (event_id == send_event_id) {
  1511. return one_pair.first;
  1512. }
  1513. }
  1514. }
  1515. return nullptr;
  1516. }
  1517. // Get a specific recv node according to the recv event
  1518. NodePtr StreamAllocator::GetNodeFromRecvEventId(uint32_t recv_event_id) const {
  1519. for (const auto &one_pair : node_to_recv_events_) {
  1520. const vector<uint32_t> &events = one_pair.second;
  1521. for (const auto &event_id : events) {
  1522. if (event_id == recv_event_id) {
  1523. return one_pair.first;
  1524. }
  1525. }
  1526. }
  1527. return nullptr;
  1528. }
  1529. Status StreamAllocator::AddEventId(const NodePtr &pre_node, const NodePtr &not_cur, const NodePtr &cur_node,
  1530. bool not_use_cur) {
  1531. GELOGI("Add send event %u for node %s", event_num_, pre_node->GetName().c_str());
  1532. AddSendEventId(pre_node, event_num_);
  1533. if (not_use_cur) {
  1534. GE_CHECK_NOTNULL(not_cur);
  1535. GELOGI("Add recv event %u for node %s", event_num_, not_cur->GetName().c_str());
  1536. AddRecvEventId(not_cur, event_num_);
  1537. } else {
  1538. GELOGI("Add recv event %u for node %s", event_num_, cur_node->GetName().c_str());
  1539. AddRecvEventId(cur_node, event_num_);
  1540. }
  1541. ++event_num_;
  1542. return SUCCESS;
  1543. }
  1544. Status StreamAllocator::AddActiveNodes(NodePtr &switch_node, const vector<string> &ori_active_label_list,
  1545. vector<string> &active_label_list, vector<NodePtr> &added_active_nodes) {
  1546. size_t label_num = ori_active_label_list.size();
  1547. for (size_t i = 0; i < label_num; i++) {
  1548. const string &active_label = ori_active_label_list[i];
  1549. if (labeled_streams_.find(active_label) == labeled_streams_.end()) {
  1550. REPORT_INNER_ERROR("E19999", "can not find stream label:%s", active_label.c_str());
  1551. GELOGE(FAILED, "[Check][Param] can not find stream label %s", active_label.c_str());
  1552. return FAILED;
  1553. }
  1554. if (labeled_streams_[active_label].size() <= 1) {
  1555. active_label_list.emplace_back(active_label);
  1556. continue;
  1557. }
  1558. string name = switch_node->GetName() + "_" + STREAMACTIVE + "_" + std::to_string(i);
  1559. GELOGI("Create StreamActive op %s after node %s.", name.c_str(), switch_node->GetName().c_str());
  1560. OpDescPtr active_op_desc = MakeShared<OpDesc>(name, STREAMACTIVE);
  1561. GE_CHECK_NOTNULL(active_op_desc);
  1562. NodePtr active_node = whole_graph_->AddNode(active_op_desc);
  1563. GE_CHECK_NOTNULL(active_node);
  1564. for (NodePtr &node : switch_node->GetOutControlNodes()) {
  1565. OpDescPtr op_desc = node->GetOpDesc();
  1566. GE_CHECK_NOTNULL(op_desc);
  1567. string stream_label;
  1568. // If GetStr failed, stream_label is empty.
  1569. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label);
  1570. if (stream_label != active_label) {
  1571. continue;
  1572. }
  1573. GE_CHECK_NOTNULL(switch_node->GetOutControlAnchor());
  1574. if (switch_node->GetOutControlAnchor()->Unlink(node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  1575. REPORT_CALL_ERROR("E19999", "Unlink %s to %s failed",
  1576. switch_node->GetName().c_str(), node->GetName().c_str());
  1577. GELOGE(FAILED, "[Unlink][Nodes] %s to %s failed.", switch_node->GetName().c_str(), node->GetName().c_str());
  1578. return FAILED;
  1579. }
  1580. GE_CHECK_NOTNULL(active_node->GetOutControlAnchor());
  1581. if (active_node->GetOutControlAnchor()->LinkTo(node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  1582. REPORT_CALL_ERROR("E19999", "Link %s to %s failed",
  1583. active_node->GetName().c_str(), node->GetName().c_str());
  1584. GELOGE(FAILED, "[Link][Nodes] %s to %s failed.", active_node->GetName().c_str(), node->GetName().c_str());
  1585. return FAILED;
  1586. }
  1587. }
  1588. if (SetSwitchBranchNodeLabel(active_node, name) != SUCCESS) {
  1589. GELOGE(FAILED, "[Set][SwitchBranchNodeLabel] failed, node:%s.", active_node->GetName().c_str());
  1590. return FAILED;
  1591. }
  1592. if (SetStreamLabel(active_node, name) != SUCCESS) {
  1593. GELOGE(FAILED, "[Set][StreamLabel] failed, node:%s.", active_node->GetName().c_str());
  1594. return FAILED;
  1595. }
  1596. if (SetActiveLabelList(active_node, {active_label}) != SUCCESS) {
  1597. GELOGE(FAILED, "[Set][ActiveLabelList] failed, node:%s.", active_node->GetName().c_str());
  1598. return FAILED;
  1599. }
  1600. if (SetActiveStreamList(active_node, active_label) != SUCCESS) {
  1601. GELOGE(FAILED, "[Set][ActiveStreamList] failed, node:%s.", active_node->GetName().c_str());
  1602. return FAILED;
  1603. }
  1604. added_active_nodes.emplace_back(active_node);
  1605. active_label_list.emplace_back(name);
  1606. }
  1607. return SUCCESS;
  1608. }
  1609. Status StreamAllocator::SetActiveStreamList(NodePtr &active_node, const string &active_label) {
  1610. if (labeled_streams_.find(active_label) == labeled_streams_.end()) {
  1611. REPORT_INNER_ERROR("E19999", "Can not find stream label:%s", active_label.c_str());
  1612. GELOGE(FAILED, "[Check][Param] Can not find stream label %s.", active_label.c_str());
  1613. return FAILED;
  1614. }
  1615. set<int64_t> &streams = labeled_streams_[active_label];
  1616. vector<int64_t> active_streams(streams.begin(), streams.end());
  1617. if (!AttrUtils::SetListInt(active_node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1618. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1619. active_node->GetName().c_str(), active_node->GetType().c_str());
  1620. GELOGE(FAILED, "[Set][Attr] %s failed for op:%s(%s).", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1621. active_node->GetName().c_str(), active_node->GetType().c_str());
  1622. return FAILED;
  1623. }
  1624. return SUCCESS;
  1625. }
  1626. } // namespace ge

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