You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

stream_allocator.cc 78 kB

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

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