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

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