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

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