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

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