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

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