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 77 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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. for (const auto &cur_node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  387. // Take the adjacent points, then judge whether need to insert the event
  388. for (const OutDataAnchorPtr &anchor : cur_node->GetAllOutDataAnchors()) {
  389. for (const InDataAnchorPtr &peer_in_anchor : anchor->GetPeerInDataAnchors()) {
  390. NodePtr next_node = peer_in_anchor->GetOwnerNode();
  391. Status status = InsertOneEventInTwoNodes(cur_node, next_node);
  392. if (status != SUCCESS) {
  393. GELOGE(status, "[Insert][OneEvent] In Two Nodes failed! cur node:%s", cur_node->GetName().c_str());
  394. return status;
  395. }
  396. }
  397. }
  398. /// If the two nodes of the control side belong to two streams,
  399. /// you also need to add the send/recv event.
  400. if (cur_node->GetOutControlAnchor() != nullptr) {
  401. for (const AnchorPtr &peer_in_anchor : cur_node->GetOutControlAnchor()->GetPeerAnchors()) {
  402. NodePtr next_node = peer_in_anchor->GetOwnerNode();
  403. Status status = InsertOneEventInTwoNodes(cur_node, next_node);
  404. if (status != SUCCESS) {
  405. GELOGE(status, "[Insert][OneEvent] In Two Nodes failed! cur node:%s", cur_node->GetName().c_str());
  406. return status;
  407. }
  408. }
  409. }
  410. }
  411. Status status = InsertEventsForSubgraph();
  412. if (status != SUCCESS) {
  413. GELOGE(status, "[Insert][Events] Between Sub And Parent GraphNodes failed! graph:%s",
  414. whole_graph_->GetName().c_str());
  415. return status;
  416. }
  417. return SUCCESS;
  418. }
  419. // Insert one send/recv event in two nodes
  420. Status StreamAllocator::InsertOneEventInTwoNodes(const NodePtr &cur_node, const NodePtr &next_node) {
  421. GE_CHECK_NOTNULL(cur_node->GetOpDesc());
  422. GE_CHECK_NOTNULL(next_node->GetOpDesc());
  423. // No need to insert events after node that do not assign streams.
  424. int64_t cur_stream_id = cur_node->GetOpDesc()->GetStreamId();
  425. if (cur_stream_id == kInvalidStream) {
  426. GELOGD("No need to insert event after node %s.", cur_node->GetName().c_str());
  427. return SUCCESS;
  428. }
  429. // No need to insert events between nodes in the same stream.
  430. int64_t next_stream_id = next_node->GetOpDesc()->GetStreamId();
  431. if (cur_stream_id == next_stream_id) {
  432. return SUCCESS;
  433. }
  434. if (((cur_node->GetType() == ENTER) || (cur_node->GetType() == REFENTER)) && (next_node->GetType() != STREAMACTIVE)) {
  435. GELOGD("No need to insert event between %s and %s.", cur_node->GetName().c_str(), next_node->GetName().c_str());
  436. return SUCCESS;
  437. }
  438. if (next_stream_id == kInvalidStream) {
  439. REPORT_INNER_ERROR("E19999", "Stream id of next_node %s(%s) should not be %ld",
  440. next_node->GetName().c_str(), next_node->GetType().c_str(), kInvalidStream);
  441. GELOGE(FAILED, "[Check][Param] Stream id of next_node %s should not be %ld",
  442. next_node->GetName().c_str(), kInvalidStream);
  443. return FAILED;
  444. }
  445. // No event needs to be inserted between the active node and the activated stream.
  446. string next_node_label;
  447. if (AttrUtils::GetStr(next_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, next_node_label) && !next_node_label.empty()) {
  448. auto iter = specific_activated_labels_.find(next_node_label);
  449. if (iter != specific_activated_labels_.end()) {
  450. for (const auto &active_node : iter->second) {
  451. OpDescPtr active_op = active_node->GetOpDesc();
  452. GE_CHECK_NOTNULL(active_op);
  453. if ((cur_stream_id == active_op->GetStreamId()) && (cur_node->GetOpDesc()->GetId() <= active_op->GetId())) {
  454. GELOGI("No need to insert event between node %s and %s.", cur_node->GetName().c_str(),
  455. next_node->GetName().c_str());
  456. return SUCCESS;
  457. }
  458. }
  459. }
  460. }
  461. // Add send and receive events.
  462. AddSendEventId(cur_node, event_num_);
  463. AddRecvEventId(next_node, event_num_);
  464. GELOGD("Insert event %u between node %s(stream %ld) and %s(stream %ld)", event_num_, cur_node->GetName().c_str(),
  465. cur_stream_id, next_node->GetName().c_str(), next_stream_id);
  466. ++event_num_;
  467. return SUCCESS;
  468. }
  469. Status StreamAllocator::InsertEventsForSubgraph() {
  470. for (const auto &subgraph : whole_graph_->GetAllSubgraphs()) {
  471. GE_CHECK_NOTNULL(subgraph);
  472. for (const auto &node : subgraph->GetDirectNode()) {
  473. auto op_desc = node->GetOpDesc();
  474. GE_CHECK_NOTNULL(op_desc);
  475. bool is_subgraph_end_node = false;
  476. if (!AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_END_NODE, is_subgraph_end_node) || !is_subgraph_end_node) {
  477. continue;
  478. }
  479. const auto parent_node = subgraph->GetParentNode();
  480. GE_CHECK_NOTNULL(parent_node);
  481. // Insert events between subgraph end node and parent node's out nodes
  482. for (const auto &next_node : parent_node->GetOutAllNodes()) {
  483. Status status = InsertOneEventInTwoNodes(node, next_node);
  484. if (status != SUCCESS) {
  485. GELOGE(status, "[Insert][OneEvent] In Two Nodes failed! node:%s", node->GetName().c_str());
  486. return status;
  487. }
  488. }
  489. break;
  490. }
  491. }
  492. return SUCCESS;
  493. }
  494. // Optimize the event in the graph, delete the redundant sync event according to the stream information
  495. Status StreamAllocator::OptimizeSyncEvents() {
  496. map<int64_t, vector<NodePtr>> stream_nodes;
  497. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  498. GE_CHECK_NOTNULL(node->GetOpDesc());
  499. int64_t stream_id = node->GetOpDesc()->GetStreamId();
  500. stream_nodes[stream_id].emplace_back(node);
  501. }
  502. Status status = OptimizeBySendEvents(stream_nodes);
  503. if (status != SUCCESS) {
  504. GELOGE(status, "[Optimize][StreamNodes] By Send Events failed! graph:%s", whole_graph_->GetName().c_str());
  505. return status;
  506. }
  507. status = OptimizeByRecvEvents(stream_nodes);
  508. if (status != SUCCESS) {
  509. GELOGE(status, "[Optimize][StreamNodes] By Recv Events failed! graph:%s", whole_graph_->GetName().c_str());
  510. return status;
  511. }
  512. status = OptimizeByStreamActivate();
  513. if (status != SUCCESS) {
  514. GELOGE(status, "[Call][OptimizeByStreamActivate] failed! graph:%s", whole_graph_->GetName().c_str());
  515. return status;
  516. }
  517. for (auto pair : node_to_send_events_) {
  518. if (pair.first->GetType() == STREAMSWITCH) {
  519. for (auto event_id : pair.second) {
  520. GELOGI("Curren switch node is %s, remove send event_id %d.", pair.first->GetName().c_str(), event_id);
  521. RmvSendEventId(pair.first, event_id);
  522. auto recv_node = GetNodeFromRecvEventId(event_id);
  523. GELOGI("Curren recv_node is %s, remove recv event_id %d.", recv_node->GetName().c_str(), event_id);
  524. RmvRecvEventId(recv_node, event_id);
  525. }
  526. }
  527. }
  528. return SUCCESS;
  529. }
  530. /// Optimization scenario: one stream has multiple send events in one node,
  531. /// and multiple nodes for recv events on another stream
  532. /// Example:
  533. /// Stream0 Stream1
  534. /// N1 - - - event - > N1
  535. /// \ |
  536. /// \ v
  537. /// - - event - > N2
  538. Status StreamAllocator::OptimizeBySendEvents(const map<int64_t, vector<NodePtr>> &stream_nodes) {
  539. for (const auto &one_pair : stream_nodes) {
  540. // The nodes on a stream in order
  541. const vector<NodePtr> &nodes = one_pair.second;
  542. map<NodePtr, uint32_t> send_node_to_event_id;
  543. for (const auto &recv_node_ptr : nodes) {
  544. GE_CHECK_NOTNULL(recv_node_ptr);
  545. // Get all recv events of the current node, then traverse the event
  546. vector<uint32_t> recv_events;
  547. GetRecvEventIdList(recv_node_ptr, recv_events);
  548. for (const auto &event_id : recv_events) {
  549. NodePtr send_node_ptr = GetNodeFromSendEventId(event_id);
  550. GE_CHECK_NOTNULL(send_node_ptr);
  551. /// If the record to the stream is found in the map,
  552. /// and the recv node is the node, then remove sync event
  553. if (send_node_to_event_id.find(send_node_ptr) != send_node_to_event_id.end()) {
  554. RmvSendEventId(send_node_ptr, event_id);
  555. RmvRecvEventId(recv_node_ptr, event_id);
  556. GELOGI("Remove event %u between node %s and node %s", event_id, send_node_ptr->GetName().c_str(),
  557. recv_node_ptr->GetName().c_str());
  558. } else {
  559. send_node_to_event_id[send_node_ptr] = event_id;
  560. }
  561. }
  562. }
  563. }
  564. return SUCCESS;
  565. }
  566. /// Scenario: multiple send nodes on a stream sent to a single recv node on the destination stream
  567. /// Example:
  568. /// Stream0 Stream1
  569. /// N1 - -
  570. /// | |
  571. /// | - - event - - -
  572. /// | |
  573. /// V V
  574. /// N2 - - - event - > N2
  575. Status StreamAllocator::OptimizeByRecvEvents(const map<int64_t, vector<NodePtr>> &stream_nodes) {
  576. for (const auto &one_pair : stream_nodes) {
  577. // The nodes on a stream in order
  578. const vector<NodePtr> &nodes = one_pair.second;
  579. map<NodePtr, uint32_t> recv_node_to_event_id;
  580. for (const auto &send_node_ptr : nodes) {
  581. GE_CHECK_NOTNULL(send_node_ptr);
  582. // Get all send events of the current node, then traverse the event
  583. vector<uint32_t> send_id_list;
  584. GetSendEventIdList(send_node_ptr, send_id_list);
  585. for (const auto &event_id : send_id_list) {
  586. NodePtr recv_node_ptr = GetNodeFromRecvEventId(event_id);
  587. GE_CHECK_NOTNULL(recv_node_ptr);
  588. /// If the record to the stream is found in the map,
  589. /// and the send node is the node, then remove sync event
  590. auto it = recv_node_to_event_id.find(recv_node_ptr);
  591. if (it != recv_node_to_event_id.end()) {
  592. uint32_t pre_event_id = it->second;
  593. NodePtr pre_send_node_ptr = GetNodeFromSendEventId(pre_event_id);
  594. GE_CHECK_NOTNULL(pre_send_node_ptr);
  595. RmvSendEventId(pre_send_node_ptr, pre_event_id);
  596. RmvRecvEventId(recv_node_ptr, pre_event_id);
  597. GELOGI("Remove event %u between node %s and node %s.", event_id, pre_send_node_ptr->GetName().c_str(),
  598. recv_node_ptr->GetName().c_str());
  599. }
  600. recv_node_to_event_id[recv_node_ptr] = event_id;
  601. }
  602. }
  603. }
  604. return SUCCESS;
  605. }
  606. Status StreamAllocator::OptimizeByStreamActivate() {
  607. auto node_to_send_events_temp = node_to_send_events_;
  608. for (const auto &node_event_id_pair : node_to_send_events_temp) {
  609. const NodePtr &send_node_ptr = node_event_id_pair.first;
  610. for (const auto &event_id : node_event_id_pair.second) {
  611. NodePtr recv_node_ptr = GetNodeFromRecvEventId(event_id);
  612. GE_CHECK_NOTNULL(recv_node_ptr);
  613. if (IsRecvNodeActivatedBySendNode(send_node_ptr, recv_node_ptr)) {
  614. RmvSendEventId(send_node_ptr, event_id);
  615. RmvRecvEventId(recv_node_ptr, event_id);
  616. GELOGI("Remove event %u between node %s and node %s.", event_id, send_node_ptr->GetName().c_str(),
  617. recv_node_ptr->GetName().c_str());
  618. }
  619. }
  620. }
  621. return SUCCESS;
  622. }
  623. // In situation : stream(normal) -> stream(streamActivate)->
  624. // -> stream(streamSwitch) -> stream(streamActivate) -> stream(stream true or false)
  625. // No need to insert an event between node in stream(normal) and node in stream(stream true or false)
  626. bool StreamAllocator::IsRecvNodeActivatedBySendNode(const NodePtr &send_node_ptr, const NodePtr &recv_node_ptr) const {
  627. GE_CHECK_NOTNULL_EXEC(send_node_ptr->GetOpDesc(),
  628. REPORT_INNER_ERROR("E19999", "Check param send_node_ptr nullptr");
  629. GELOGE(FAILED, "[Check][Param] op desc is nullptr");
  630. return false);
  631. GE_CHECK_NOTNULL_EXEC(recv_node_ptr->GetOpDesc(),
  632. REPORT_INNER_ERROR("E19999", "Check param recv_node_ptr nullptr");
  633. GELOGE(FAILED, "[Check][Param] op desc is nullptr");
  634. return false);
  635. auto cur_stream_id = send_node_ptr->GetOpDesc()->GetStreamId();
  636. if (AttrUtils::HasAttr(recv_node_ptr->GetOpDesc(), ATTR_NAME_STREAM_LABEL)) {
  637. // find streamActivate node
  638. auto iter = specific_activated_streams_nodes_map_.find(recv_node_ptr->GetOpDesc()->GetStreamId());
  639. set<NodePtr> activate_stream_nodes;
  640. if (iter != specific_activated_streams_nodes_map_.end()) {
  641. activate_stream_nodes = iter->second;
  642. }
  643. set<NodePtr> visited_nodes{recv_node_ptr};
  644. while (!activate_stream_nodes.empty()) {
  645. set<NodePtr> activate_stream_nodes_temp;
  646. for (const auto &activate_stream_node : activate_stream_nodes) {
  647. GE_IF_BOOL_EXEC(activate_stream_node->GetOpDesc() == nullptr, continue);
  648. if (visited_nodes.find(activate_stream_node) != visited_nodes.end() ||
  649. AttrUtils::HasAttr(activate_stream_node->GetOpDesc(), ATTR_NAME_IS_LOOP_ACTIVE)) {
  650. return false;
  651. }
  652. ///
  653. /// stream_0 --> stream_2 --> stream_3 --> stream_4
  654. /// /\ |
  655. /// | \/
  656. /// | stream_1 --> stream_5 --> stream_6 --> stream_7
  657. /// | /\ | |
  658. /// | | \/ |
  659. /// | |---------- stream_8 |
  660. /// | |
  661. /// |-----------------------------------------------------------|
  662. ///
  663. /// Exit1(S7) Exit2(S7) Exit3(S7)
  664. /// \ / |
  665. /// AddN(S1) NextIteration(S7)
  666. /// | |
  667. /// NextIteration(S1) /
  668. /// | /
  669. /// | /
  670. /// StreamActive(S7)
  671. ///
  672. /// Event between Exit1/Exit2 and AddN should not be optimized
  673. ///
  674. if (IsActiveAfterNextIteration(activate_stream_node)) {
  675. continue;
  676. }
  677. visited_nodes.insert(activate_stream_node);
  678. // nodes in stream link to streamActivate no need to add event before activated node
  679. for (const auto &pre_activate_stream_node : activate_stream_node->GetInNodes()) {
  680. GE_IF_BOOL_EXEC(pre_activate_stream_node->GetOpDesc() == nullptr, continue);
  681. if (pre_activate_stream_node->GetOpDesc()->GetStreamId() == cur_stream_id &&
  682. pre_activate_stream_node->GetOpDesc()->GetId() >= send_node_ptr->GetOpDesc()->GetId()) {
  683. return true;
  684. }
  685. auto in_nodes_of_pre = pre_activate_stream_node->GetInNodes();
  686. if (std::find(in_nodes_of_pre.begin(), in_nodes_of_pre.end(), send_node_ptr) != in_nodes_of_pre.end()) {
  687. return true;
  688. }
  689. }
  690. auto iterator = specific_activated_streams_nodes_map_.find(activate_stream_node->GetOpDesc()->GetStreamId());
  691. if (iterator != specific_activated_streams_nodes_map_.end()) {
  692. auto active_nodes = iterator->second;
  693. for (const auto &active_node : active_nodes) {
  694. activate_stream_nodes_temp.emplace(active_node);
  695. }
  696. }
  697. }
  698. activate_stream_nodes = activate_stream_nodes_temp;
  699. }
  700. }
  701. return false;
  702. }
  703. bool StreamAllocator::IsActiveAfterNextIteration(const NodePtr &active_node_ptr) const {
  704. if ((active_node_ptr == nullptr) || active_node_ptr->GetInControlNodes().empty()) {
  705. return false;
  706. }
  707. for (const auto &in_node : active_node_ptr->GetInControlNodes()) {
  708. if ((in_node->GetType() != NEXTITERATION) && (in_node->GetType() != REFNEXTITERATION)) {
  709. return false;
  710. }
  711. }
  712. return true;
  713. }
  714. // Split the stream according to the maximum number of nodes in the stream.
  715. Status StreamAllocator::SplitStreams(vector<set<int64_t>> &split_streams) {
  716. if (enable_single_stream_ || stream_num_ == 0) {
  717. GELOGI("The single stream option is enabled or the number of streams is 0, no need to split streams.");
  718. return SUCCESS;
  719. }
  720. // stream_node_num_vec records the number of all nodes on each stream
  721. // added_stream_num_vec records the number of streams that each stream needs to increase
  722. // new_stream_id_vec records the new physical stream id for each stream
  723. vector<int64_t> stream_node_num_vec(stream_num_);
  724. vector<int64_t> added_stream_num_vec(stream_num_);
  725. vector<int64_t> new_stream_id_vec(stream_num_);
  726. map<string, int64_t> stream_continuous_2_node_num_map;
  727. map<string, vector<NodePtr>> stream_continuous_2_nodes_map;
  728. map<int64_t, vector<NodePtr>> stream_2_nodes_map;
  729. vector<NodePtr> pre_node_vec(stream_num_);
  730. int64_t last_stream_id = stream_num_ - 1;
  731. for (auto i = 0; i <= last_stream_id; i++) {
  732. stream_node_num_vec[i] = 0;
  733. added_stream_num_vec[i] = 0;
  734. new_stream_id_vec[i] = i;
  735. pre_node_vec[i] = nullptr;
  736. }
  737. uint32_t max_stream_count = 0;
  738. uint32_t max_task_count = 0;
  739. GE_CHK_STATUS_RET(GetMaxStreamAndTask(false, max_stream_count, max_task_count),
  740. "[Get][MaxCount] of stream and task failed.");
  741. for (const auto &cur_node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  742. GE_CHECK_NOTNULL(cur_node);
  743. auto op_desc = cur_node->GetOpDesc();
  744. GE_CHECK_NOTNULL(op_desc);
  745. int64_t stream_id = op_desc->GetStreamId();
  746. if (stream_id == kInvalidStream) {
  747. continue;
  748. }
  749. if (stream_id > last_stream_id) {
  750. REPORT_INNER_ERROR("E19999", "streamid(%ld) > last_stream_id(%ld), check invalid",
  751. stream_id, last_stream_id);
  752. GELOGE(FAILED, "[Check][Param] SplitStreams:streamid(%ld) > last_stream_id(%ld)", stream_id, last_stream_id);
  753. return FAILED;
  754. }
  755. bool is_stream_first_node = (stream_node_num_vec[stream_id] == 0);
  756. AddNodeNum(cur_node, stream_node_num_vec[stream_id]);
  757. stream_2_nodes_map[stream_id].push_back(cur_node);
  758. // The maximum number of tasks per stream.
  759. int64_t max_node_num_one_stream = GetMaxNodeNumPerStream(cur_node, max_task_count);
  760. std::string continuous_stream_label;
  761. if (HasContinuousStreamLabel(op_desc, continuous_stream_label)) {
  762. stream_continuous_2_node_num_map[continuous_stream_label]++;
  763. // return error
  764. if (stream_continuous_2_node_num_map[continuous_stream_label] > max_node_num_one_stream) {
  765. REPORT_INNER_ERROR("E19999", "Check node[%s] stream_id[%ld] continuous stream label[%s] unsatisfied",
  766. op_desc->GetName().c_str(), stream_id, continuous_stream_label.c_str());
  767. GELOGE(FAILED, "[Check][Param] SplitStreams:node[%s] stream_id[%ld] continuous stream label[%s] unsatisfied ",
  768. op_desc->GetName().c_str(), stream_id, continuous_stream_label.c_str());
  769. return FAILED;
  770. }
  771. stream_continuous_2_nodes_map[continuous_stream_label].push_back(cur_node);
  772. }
  773. // Split the stream if it exceeds the maximum number of nodes in the stream.
  774. if (NeedSpiltNewStream(stream_node_num_vec[stream_id], max_node_num_one_stream, op_desc, is_stream_first_node)) {
  775. last_stream_id++;
  776. GELOGI(
  777. "stream_node_num_vec[%ld]= %ld > max_node_num_one_stream : %ld, "
  778. "It's time to split the stream, split newly-added stream id is %ld",
  779. stream_id, stream_node_num_vec[stream_id], max_node_num_one_stream, last_stream_id);
  780. NodePtr pre_node = pre_node_vec[stream_id];
  781. stream_node_num_vec[stream_id] = 0;
  782. AddNodeNum(cur_node, stream_node_num_vec[stream_id]);
  783. // try spilt a new stream and move same continuous stream label nodes from this stream
  784. bool not_use_cur = false;
  785. NodePtr not_cur = nullptr;
  786. std::string cur_continuous_stream_label;
  787. if (HasContinuousStreamLabel(op_desc, cur_continuous_stream_label)) {
  788. // get stored nodes
  789. auto nodes = stream_continuous_2_nodes_map[cur_continuous_stream_label];
  790. GE_RETURN_WITH_LOG_IF_FALSE(!nodes.empty(),
  791. "[Check][Param] split stream with continuous stream label %s failed",
  792. cur_continuous_stream_label.c_str());
  793. for (const auto &node : nodes) {
  794. auto stored_op_desc = node->GetOpDesc();
  795. GE_CHECK_NOTNULL(stored_op_desc);
  796. stored_op_desc->SetStreamId(last_stream_id);
  797. AddNodeNum(node, stream_node_num_vec[stream_id]);
  798. }
  799. not_use_cur = true;
  800. not_cur = nodes.front();
  801. GE_CHECK_NOTNULL(not_cur);
  802. GELOGI("split from first node %s with continuous stream label %s", not_cur->GetName().c_str(),
  803. cur_continuous_stream_label.c_str());
  804. auto iter = std::find(stream_2_nodes_map[stream_id].begin(), stream_2_nodes_map[stream_id].end(), not_cur);
  805. GE_RETURN_WITH_LOG_IF_FALSE(
  806. (iter != stream_2_nodes_map[stream_id].end()) && (iter != stream_2_nodes_map[stream_id].begin()),
  807. "[Check][Param] split stream with continuous stream label %s failed", cur_continuous_stream_label.c_str());
  808. iter--;
  809. pre_node = *iter;
  810. }
  811. added_stream_num_vec[stream_id]++;
  812. new_stream_id_vec[stream_id] = last_stream_id;
  813. split_streams[stream_id].emplace(last_stream_id);
  814. split_ori_stream_map_[last_stream_id] = stream_id;
  815. node_split_stream_map_[cur_node] = last_stream_id;
  816. // Add the send/recv event to the first and last nodes of the split stream.
  817. if (pre_node != nullptr) {
  818. GE_CHK_STATUS_RET(AddEventId(pre_node, not_cur, cur_node, not_use_cur),
  819. "[Add][EventId] failed, pre node:%s, not cur node:%s, cur node:%s.",
  820. pre_node->GetName().c_str(), not_cur->GetName().c_str(), cur_node->GetName().c_str());
  821. }
  822. }
  823. /// If the split stream num is greater than 1, the node behind the same
  824. /// stream must reset the new stream id.
  825. if (added_stream_num_vec[stream_id] >= 1) {
  826. op_desc->SetStreamId(new_stream_id_vec[stream_id]);
  827. }
  828. pre_node_vec[stream_id] = cur_node;
  829. }
  830. if (last_stream_id >= 0) {
  831. stream_num_ = last_stream_id + 1;
  832. }
  833. return SUCCESS;
  834. }
  835. bool StreamAllocator::NeedSpiltNewStream(int64_t stream_node_num, int64_t max_node_num_one_stream,
  836. const OpDescPtr &op_desc, bool is_stream_first_node) const {
  837. if (is_stream_first_node) {
  838. GELOGD("First node of stream does not need to split new stream");
  839. return false;
  840. }
  841. const set<string> label_op_types({LABELSET, LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX});
  842. bool is_first_active_node = false;
  843. (void)AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_FIRST_ACTIVE, is_first_active_node);
  844. return (stream_node_num > max_node_num_one_stream && op_desc->GetSubgraphInstanceNames().empty() &&
  845. !is_first_active_node && label_op_types.count(op_desc->GetType()) == 0);
  846. }
  847. Status StreamAllocator::UpdateActiveStreams(const vector<set<int64_t>> &split_streams) {
  848. UpdateLabelStreams(split_streams);
  849. for (auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  850. if ((node->GetType() == STREAMSWITCH) || (node->GetType() == STREAMSWITCHN)) {
  851. if (UpdateActiveStreamsForSwitchNode(node) != SUCCESS) {
  852. GELOGE(FAILED, "[Update][ActiveStreams] for switch node: %s failed.", node->GetName().c_str());
  853. return FAILED;
  854. }
  855. } else {
  856. if (UpdateActiveStreamsForActiveNode(split_streams, node) != SUCCESS) {
  857. GELOGE(FAILED, "[Update][ActiveStreams] for active node: %s failed.", node->GetName().c_str());
  858. return FAILED;
  859. }
  860. }
  861. }
  862. Status status = UpdateActiveStreamsForSubgraphs();
  863. if (status != SUCCESS) {
  864. GELOGE(status, "[Update][ActiveStreams] for subgraphs failed! graph:%s", whole_graph_->GetName().c_str());
  865. return status;
  866. }
  867. status = SetActiveStreamsForLoop();
  868. if (status != SUCCESS) {
  869. GELOGE(status, "[Set][ActiveStreams] For Loop failed! graph:%s", whole_graph_->GetName().c_str());
  870. return status;
  871. }
  872. return SUCCESS;
  873. }
  874. void StreamAllocator::UpdateLabelStreams(const vector<set<int64_t>> &split_streams) {
  875. for (size_t i = 0; i < split_streams.size(); i++) {
  876. auto &streams = split_streams[i];
  877. if (streams.empty()) {
  878. continue;
  879. }
  880. if (specific_activated_streams_.count(static_cast<int64_t>(i)) > 0) {
  881. specific_activated_streams_.insert(streams.begin(), streams.end());
  882. }
  883. for (auto &labeled_stream : labeled_streams_) {
  884. if (labeled_stream.second.count(static_cast<int64_t>(i)) > 0) {
  885. labeled_stream.second.insert(streams.begin(), streams.end());
  886. break;
  887. }
  888. }
  889. }
  890. }
  891. Status StreamAllocator::UpdateActiveStreamsForSwitchNode(NodePtr &switch_node) {
  892. vector<NodePtr> active_nodes;
  893. if (InsertActiveNodesAfterSwitch(switch_node, active_nodes) != SUCCESS) {
  894. GELOGE(FAILED, "[Insert][ActiveNodes] after node %s failed.", switch_node->GetName().c_str());
  895. return FAILED;
  896. }
  897. if (active_nodes.empty()) {
  898. return SUCCESS;
  899. }
  900. vector<int64_t> stream_ids;
  901. for (auto &active_node : active_nodes) {
  902. GE_CHECK_NOTNULL(active_node->GetOpDesc());
  903. active_node->GetOpDesc()->SetStreamId(stream_num_);
  904. stream_ids.emplace_back(stream_num_);
  905. specific_activated_streams_.emplace(stream_num_);
  906. stream_num_++;
  907. }
  908. auto op_desc = switch_node->GetOpDesc();
  909. GE_CHECK_NOTNULL(op_desc);
  910. if (!AttrUtils::SetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, stream_ids)) {
  911. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  912. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  913. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  914. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  915. return FAILED;
  916. }
  917. return SUCCESS;
  918. }
  919. Status StreamAllocator::InsertActiveNodesAfterSwitch(NodePtr &switch_node, vector<NodePtr> &active_nodes) {
  920. GE_CHECK_NOTNULL(switch_node);
  921. OpDescPtr switch_desc = switch_node->GetOpDesc();
  922. GE_CHECK_NOTNULL(switch_desc);
  923. vector<string> ori_active_label_list;
  924. if (!AttrUtils::GetListStr(switch_desc, ATTR_NAME_ACTIVE_LABEL_LIST, ori_active_label_list) ||
  925. ori_active_label_list.empty()) {
  926. REPORT_INNER_ERROR("E19999", "Get Attr:%s fail from op:%s(%s)", ATTR_NAME_ACTIVE_LABEL_LIST.c_str(),
  927. switch_node->GetName().c_str(), switch_node->GetType().c_str());
  928. GELOGE(INTERNAL_ERROR, "[Get][Attr] active label list of switch %s failed.", switch_node->GetName().c_str());
  929. return INTERNAL_ERROR;
  930. }
  931. vector<string> active_label_list;
  932. vector<NodePtr> added_active_nodes;
  933. if (AddActiveNodes(switch_node, ori_active_label_list, active_label_list, added_active_nodes) != SUCCESS) {
  934. GELOGE(FAILED, "[Add][ActiveNodes] after node %s failed.", switch_node->GetName().c_str());
  935. return FAILED;
  936. }
  937. if (SetActiveLabelList(switch_node, active_label_list) != SUCCESS) {
  938. GELOGE(FAILED, "[Set][ActiveLabelList] failed, node:%s", switch_node->GetName().c_str());
  939. return FAILED;
  940. }
  941. if (added_active_nodes.empty()) {
  942. return SUCCESS;
  943. }
  944. for (auto &active_node : added_active_nodes) {
  945. GE_CHECK_NOTNULL(switch_node->GetOutControlAnchor());
  946. if (switch_node->GetOutControlAnchor()->LinkTo(active_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  947. REPORT_CALL_ERROR("E19999", "Link from %s to %s failed",
  948. switch_node->GetName().c_str(), active_node->GetName().c_str());
  949. GELOGE(FAILED, "[Link][Nodes] from %s to %s failed.",
  950. switch_node->GetName().c_str(), active_node->GetName().c_str());
  951. return FAILED;
  952. }
  953. active_nodes.emplace_back(active_node);
  954. }
  955. return SUCCESS;
  956. }
  957. Status StreamAllocator::UpdateActiveStreamsForActiveNode(const vector<set<int64_t>> &split_streams, NodePtr &node) {
  958. GE_CHECK_NOTNULL(node->GetOpDesc());
  959. vector<uint32_t> active_streams;
  960. if (AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  961. vector<uint32_t> new_active_streams = active_streams;
  962. for (uint32_t logical_stream : active_streams) {
  963. if (static_cast<size_t>(logical_stream) >= split_streams.size()) {
  964. REPORT_INNER_ERROR("E19999", "Check logical stream:%u is out of range:%zu",
  965. logical_stream, split_streams.size());
  966. GELOGE(FAILED, "[Check][Param] logical stream:%u is out of range(0, %zu).",
  967. logical_stream, split_streams.size());
  968. return FAILED;
  969. }
  970. const set<int64_t> &new_split_streams = split_streams[logical_stream];
  971. for (int64_t split_stream : new_split_streams) {
  972. for (const auto &node_stream : node_split_stream_map_) {
  973. if (split_stream == node_stream.second) {
  974. if (node_stream.first->GetOwnerComputeGraph() == node->GetOwnerComputeGraph()) {
  975. new_active_streams.emplace_back(static_cast<uint32_t>(split_stream));
  976. GELOGI("Add stream %ld to active_stream_list of node %s of graph %s", split_stream,
  977. node->GetName().c_str(), node->GetOwnerComputeGraph()->GetName().c_str());
  978. }
  979. break;
  980. }
  981. }
  982. }
  983. }
  984. if (!AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, new_active_streams)) {
  985. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  986. node->GetName().c_str(), node->GetType().c_str());
  987. GELOGE(FAILED, "[Set][Attr] active streams for node %s failed.", node->GetName().c_str());
  988. return FAILED;
  989. }
  990. }
  991. return SUCCESS;
  992. }
  993. Status StreamAllocator::UpdateActiveStreamsForSubgraphs() {
  994. // Update active stream list for active nodes
  995. for (auto &node_stream_pair : node_split_stream_map_) {
  996. auto node = node_stream_pair.first;
  997. auto subgraph = node->GetOwnerComputeGraph();
  998. if (subgraph->GetParentNode() == nullptr) {
  999. continue;
  1000. }
  1001. // Skip streams with label
  1002. GE_CHECK_NOTNULL(node->GetOpDesc());
  1003. string stream_label;
  1004. if (AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label) && !stream_label.empty()) {
  1005. continue;
  1006. }
  1007. auto it = subgraph_first_active_node_map_.find(subgraph);
  1008. if (it == subgraph_first_active_node_map_.end()) {
  1009. continue;
  1010. }
  1011. const auto &active_node = it->second;
  1012. GE_CHECK_NOTNULL(active_node);
  1013. auto active_op = active_node->GetOpDesc();
  1014. GE_CHECK_NOTNULL(active_op);
  1015. vector<uint32_t> active_streams;
  1016. (void)AttrUtils::GetListInt(active_op, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams);
  1017. set<uint32_t> new_active_streams(active_streams.begin(), active_streams.end());
  1018. // specific_activated_streams_ has already contained new split activated stream
  1019. int64_t new_split_stream = node_stream_pair.second;
  1020. if (IsActivated(new_split_stream)) {
  1021. continue;
  1022. }
  1023. specific_activated_streams_.emplace(new_split_stream);
  1024. new_active_streams.emplace(static_cast<uint32_t>(new_split_stream));
  1025. active_streams.assign(new_active_streams.begin(), new_active_streams.end());
  1026. if (!AttrUtils::SetListInt(active_op, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1027. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1028. active_op->GetName().c_str(), active_op->GetType().c_str());
  1029. GELOGE(FAILED, "[Set][Attr] active streams for node %s failed.", active_node->GetName().c_str());
  1030. return FAILED;
  1031. }
  1032. }
  1033. return SUCCESS;
  1034. }
  1035. bool StreamAllocator::IsActivated(int64_t stream_id) const {
  1036. const auto &iter = split_ori_stream_map_.find(stream_id);
  1037. if (iter == split_ori_stream_map_.end()) {
  1038. REPORT_INNER_ERROR("E19999", "Find original stream_id failed, split_stream_id=%ld", stream_id);
  1039. GELOGE(INTERNAL_ERROR, "[CheckActivated][Check] Find original stream_id failed, split_stream_id=%ld", stream_id);
  1040. return false;
  1041. }
  1042. int64_t ori_stream_id = iter->second;
  1043. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1044. auto op_desc = node->GetOpDesc();
  1045. vector<uint32_t> active_streams;
  1046. if (op_desc == nullptr || !AttrUtils::GetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1047. continue;
  1048. }
  1049. if (std::find(active_streams.begin(), active_streams.end(), stream_id) != active_streams.end() ||
  1050. std::find(active_streams.begin(), active_streams.end(), ori_stream_id) != active_streams.end()) {
  1051. return true;
  1052. }
  1053. }
  1054. return false;
  1055. }
  1056. // Iteraotor loop :
  1057. // StreamSwitch -> StreamActive
  1058. // FpBp loop:
  1059. // StreamSwitch -> AssignAdd -> StreamActive
  1060. NodePtr FindSwitchNodeBeforeLoopActiveNode(const NodePtr &active_node) {
  1061. for (auto pre_node : active_node->GetInControlNodes()) {
  1062. if (pre_node->GetType() == STREAMSWITCH) {
  1063. return pre_node;
  1064. }
  1065. for (auto pre_pre_node : pre_node->GetInControlNodes()) {
  1066. if (pre_pre_node->GetType() == STREAMSWITCH) {
  1067. return pre_pre_node;
  1068. }
  1069. }
  1070. }
  1071. return nullptr;
  1072. }
  1073. Status StreamAllocator::SetActiveStreamsForLoop() {
  1074. vector<uint32_t> loop_active_streams;
  1075. for (int64_t stream_id = 0; stream_id < stream_num_; stream_id++) {
  1076. if (specific_activated_streams_.count(stream_id) == 0) {
  1077. loop_active_streams.emplace_back(static_cast<uint32_t>(stream_id));
  1078. }
  1079. }
  1080. map<int64_t, NodePtr> stream_id_to_last_node;
  1081. set<int64_t> streams_skip_iterator_event;
  1082. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1083. int64_t stream_id = node->GetOpDesc()->GetStreamId();
  1084. if (find(loop_active_streams.begin(), loop_active_streams.end(), stream_id) != loop_active_streams.end()) {
  1085. stream_id_to_last_node[stream_id] = node;
  1086. // last node in stream which has streamswitch or IF may be not execute, it will cause block if add event on them
  1087. if (node->GetOpDesc()->GetType() == STREAMSWITCH) {
  1088. streams_skip_iterator_event.insert(stream_id);
  1089. }
  1090. }
  1091. }
  1092. // Set the stream that needs to be activated
  1093. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1094. GE_CHECK_NOTNULL(node->GetOpDesc());
  1095. bool is_loop_active = false;
  1096. if (AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_IS_LOOP_ACTIVE, is_loop_active) && is_loop_active) {
  1097. vector<string> activated_label_list;
  1098. NodePtr pre_switch_node = FindSwitchNodeBeforeLoopActiveNode(node);
  1099. if (pre_switch_node == nullptr) {
  1100. REPORT_INNER_ERROR("E19999", "Find switch node before loop active node %s fail",
  1101. node->GetName().c_str());
  1102. GELOGE(FAILED, "[Find][SwitchNode] before loop active node %s failed", node->GetName().c_str());
  1103. return FAILED;
  1104. }
  1105. if (!AttrUtils::GetListStr(node->GetOpDesc(), ATTR_NAME_ACTIVE_LABEL_LIST, activated_label_list) ||
  1106. activated_label_list.empty()) {
  1107. GE_CHK_BOOL_EXEC(AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, loop_active_streams),
  1108. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)",
  1109. ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1110. node->GetName().c_str(), node->GetType().c_str());
  1111. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1112. node->GetName().c_str(), node->GetType().c_str());
  1113. return FAILED);
  1114. for (const auto &stream_id : loop_active_streams) {
  1115. GELOGI("Active stream %u for node: %s.", stream_id, node->GetName().c_str());
  1116. }
  1117. // In switch group optimze case, some data input branch may exec slowly.
  1118. // when condition input branch judge false and some switch has no false branch,
  1119. // In this condition, data branch has no synchronize point,
  1120. // it may cause some stream actived by iterator next step when this stream still alive.
  1121. // If above situation happen, active message will lose, cause process block in next iteration.
  1122. // In order to avoid this abnormal happen,
  1123. // add event between each last node and iterator switch node
  1124. GELOGI("there are %zu next iterator target streams has streamswitch node.", streams_skip_iterator_event.size());
  1125. for (auto iter : stream_id_to_last_node) {
  1126. if (streams_skip_iterator_event.find(iter.first) != streams_skip_iterator_event.end()) {
  1127. GELOGI("Skip stream %ld which has streamswitch node when adding event to next iterator active node",
  1128. iter.first);
  1129. continue;
  1130. }
  1131. if (iter.second->GetOwnerComputeGraph()->GetParentGraph() != nullptr) {
  1132. GELOGI("Skip stream %ld which is last node in subgraph when adding event to next iterator active node",
  1133. iter.first);
  1134. continue;
  1135. }
  1136. AddSendEventId(iter.second, event_num_);
  1137. AddRecvEventId(pre_switch_node, event_num_);
  1138. event_num_++;
  1139. }
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. return CheckStreamActived();
  1145. }
  1146. Status StreamAllocator::CheckStreamActived() const {
  1147. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1148. GE_CHECK_NOTNULL(node->GetOpDesc());
  1149. vector<uint32_t> active_streams;
  1150. if (AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1151. uint32_t stream_id = static_cast<uint32_t>(node->GetOpDesc()->GetStreamId());
  1152. auto iter = find(active_streams.begin(), active_streams.end(), stream_id);
  1153. if (iter != active_streams.end()) {
  1154. REPORT_INNER_ERROR("E19999", "Node:%s(%s) cannot active its own stream %u, check invalid ",
  1155. node->GetName().c_str(), node->GetType().c_str(), stream_id);
  1156. GELOGE(FAILED, "[Check][Param] Node %s cannot active its own stream %u.", node->GetName().c_str(), stream_id);
  1157. return FAILED;
  1158. }
  1159. }
  1160. }
  1161. return SUCCESS;
  1162. }
  1163. Status StreamAllocator::ReuseEvent(bool send_to,
  1164. const std::unordered_map<std::string, ge::NodePtr> &name_to_node_map,
  1165. const std::unordered_map<ge::NodePtr, std::vector<std::pair<std::string, uint32_t>>> &node_to_event_id) {
  1166. for (const auto &node_event_id : node_to_event_id) {
  1167. ge::NodePtr curr_node = node_event_id.first;
  1168. NodePtr send_node = send_to ? curr_node : nullptr;
  1169. NodePtr recv_node = send_to ? nullptr : curr_node;
  1170. for (const auto &event_pair : node_event_id.second) {
  1171. auto peer_node_iter = name_to_node_map.find(event_pair.first);
  1172. if (peer_node_iter == name_to_node_map.end()) {
  1173. GELOGE(PARAM_INVALID, "[Get][Node]Name:%s.", event_pair.first.c_str());
  1174. REPORT_INNER_ERROR("E19999", "Failed to find node, name:%s.", event_pair.first.c_str());
  1175. return PARAM_INVALID;
  1176. }
  1177. recv_node = send_to ? peer_node_iter->second : recv_node;
  1178. send_node = send_to ? send_node : peer_node_iter->second;
  1179. GE_CHECK_NOTNULL(send_node);
  1180. GE_CHECK_NOTNULL(recv_node);
  1181. auto event_id = GetIntersection(node_to_send_events_[send_node], node_to_recv_events_[recv_node]);
  1182. uint32_t new_event = event_pair.second + event_num_;
  1183. if (event_id.empty()) {
  1184. GELOGI("[Check][Optimized]Send:%s, recv:%s.", send_node->GetName().c_str(), recv_node->GetName().c_str());
  1185. continue;
  1186. } else if (event_id.size() != 1) {
  1187. GELOGW("[Check][Event]More than one event are found between %s and %s, event num:%zu.",
  1188. send_node->GetName().c_str(), recv_node->GetName().c_str(), event_id.size());
  1189. }
  1190. uint32_t old_event = event_id[0];
  1191. auto reuse_event_id = [] (vector<uint32_t> &event_list, uint32_t old_event, uint32_t new_event) -> void {
  1192. event_list.erase(std::remove(event_list.begin(), event_list.end(), old_event), event_list.end());
  1193. event_list.push_back(new_event);
  1194. return;
  1195. };
  1196. reuse_event_id(node_to_send_events_[send_node], old_event, new_event);
  1197. reuse_event_id(node_to_recv_events_[recv_node], old_event, new_event);
  1198. GELOGI("[Reuse][Event]Replace event successfully, send node:%s, recv node:%s, old id:%u, new id:%u.",
  1199. send_node->GetName().c_str(), recv_node->GetName().c_str(), old_event, new_event);
  1200. }
  1201. }
  1202. return ge::SUCCESS;
  1203. }
  1204. // Refresh events to reuse events
  1205. Status StreamAllocator::RefreshEventsWithReuse() {
  1206. GELOGI("[Refresh][Events]Refresh events with reuse, stream num:%ld, original event num:%u.", stream_num_, event_num_);
  1207. if (event_num_ <= kEventReuseThreshold) {
  1208. GELOGI("[Check][ReuseThreshold]Event used num is %u, less than %u, skip reuse.",
  1209. event_num_, kEventReuseThreshold);
  1210. return SUCCESS;
  1211. }
  1212. std::unordered_map<std::string, NodePtr> name_to_node_map;
  1213. std::unordered_map<NodePtr, std::vector<std::pair<std::string, uint32_t>>> node_to_send;
  1214. std::unordered_map<NodePtr, std::vector<std::pair<std::string, uint32_t>>> node_to_recv;
  1215. Status ret = ParseAllNodeEventMultiplexing(whole_graph_, name_to_node_map, node_to_send, node_to_recv);
  1216. if (ret != SUCCESS) {
  1217. GELOGE(ret, "[Parse][AllNodeEventMultiplexing]Graph:%s.", whole_graph_->GetName().c_str());
  1218. REPORT_INNER_ERROR("E19999", "Failed to parse all node event multiplexing, graph:%s.",
  1219. whole_graph_->GetName().c_str());
  1220. return ret;
  1221. }
  1222. if (node_to_send.empty() && node_to_recv.empty()) {
  1223. return SUCCESS;
  1224. }
  1225. ret = ReuseEvent(true, name_to_node_map, node_to_send);
  1226. if (ret != SUCCESS) {
  1227. GELOGE(ret, "[Reuse][Event]Phase:Send, graph:%s.", whole_graph_->GetName().c_str());
  1228. REPORT_INNER_ERROR("E19999", "Failed to reuse event, phase:Send, graph:%s.", whole_graph_->GetName().c_str());
  1229. return ret;
  1230. }
  1231. ret = ReuseEvent(false, name_to_node_map, node_to_recv);
  1232. if (ret != SUCCESS) {
  1233. GELOGE(ret, "[Reuse][Event]Phase:Recv, graph:%s.", whole_graph_->GetName().c_str());
  1234. REPORT_INNER_ERROR("E19999", "Failed to reuse event, phase:Recv, graph:%s.", whole_graph_->GetName().c_str());
  1235. return ret;
  1236. }
  1237. Status status = RefreshContinuousEvents();
  1238. if (status != SUCCESS) {
  1239. GELOGE(status, "[Refresh][ContinuousEvents]Graph:%s.", whole_graph_->GetName().c_str());
  1240. REPORT_INNER_ERROR("E19999", "Failed to refresh continuous events, graph:%s.", whole_graph_->GetName().c_str());
  1241. return status;
  1242. }
  1243. GELOGI("[Refresh][Events]RefreshEventsWithReuse successfully, event num:%u.", event_num_);
  1244. return SUCCESS;
  1245. }
  1246. // Refresh events to continuous events
  1247. Status StreamAllocator::RefreshContinuousEvents() {
  1248. // Establish a mapping relationship from old to new event id
  1249. map<uint32_t, uint32_t> old_to_new_events;
  1250. uint32_t new_event_id = 0;
  1251. for (const auto &one_pair : node_to_send_events_) {
  1252. for (const auto &event_id : one_pair.second) {
  1253. if (old_to_new_events.find(event_id) == old_to_new_events.end()) {
  1254. old_to_new_events[event_id] = new_event_id;
  1255. new_event_id++;
  1256. }
  1257. }
  1258. }
  1259. // Refresh send event id
  1260. for (auto &one_pair : node_to_send_events_) {
  1261. vector<uint32_t> &send_events = one_pair.second;
  1262. for (size_t i = 0; i < send_events.size(); i++) {
  1263. auto find_it = old_to_new_events.find(send_events[i]);
  1264. if (find_it == old_to_new_events.end()) {
  1265. REPORT_INNER_ERROR("E19999", "Check invalid send event %u", send_events[i]);
  1266. GELOGE(FAILED, "[Check][Param] RefreshContinuousEvents: invalid send event %u", send_events[i]);
  1267. return FAILED;
  1268. }
  1269. send_events[i] = find_it->second;
  1270. }
  1271. }
  1272. // Refresh recv event id
  1273. for (auto &one_pair : node_to_recv_events_) {
  1274. vector<uint32_t> &recv_events = one_pair.second;
  1275. for (size_t i = 0; i < recv_events.size(); i++) {
  1276. auto find_it = old_to_new_events.find(recv_events[i]);
  1277. if (find_it == old_to_new_events.end()) {
  1278. REPORT_INNER_ERROR("E19999", "Check invalid recv event %u", recv_events[i]);
  1279. GELOGE(FAILED, "[Check][Param] RefreshContinuousEvents: invalid recv event %u", recv_events[i]);
  1280. return FAILED;
  1281. }
  1282. recv_events[i] = find_it->second;
  1283. }
  1284. }
  1285. event_num_ = static_cast<uint32_t>(old_to_new_events.size());
  1286. return SUCCESS;
  1287. }
  1288. // Insert the real send/recv node in the graph
  1289. Status StreamAllocator::InsertSyncEventNodes() {
  1290. unordered_map<string, uint32_t> sync_event_name;
  1291. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1292. // Add the node corresponding to the recv event
  1293. vector<uint32_t> recv_event_id_list;
  1294. GetRecvEventIdList(node, recv_event_id_list);
  1295. GE_CHECK_NOTNULL(node->GetOpDesc());
  1296. GE_CHECK_NOTNULL(node->GetInControlAnchor());
  1297. GE_CHECK_NOTNULL(node->GetOutControlAnchor());
  1298. for (auto &event_id : recv_event_id_list) {
  1299. string recv_node_name = whole_graph_->GetName() + "_Recv_" + to_string(event_id);
  1300. auto iter = sync_event_name.find(recv_node_name);
  1301. if (iter == sync_event_name.end()) {
  1302. sync_event_name[recv_node_name] = 1;
  1303. } else {
  1304. recv_node_name = recv_node_name + "_Reuse_" + to_string(iter->second);
  1305. ++(iter->second);
  1306. }
  1307. OpDescPtr op_desc_ptr = MakeShared<OpDesc>(recv_node_name, RECV);
  1308. GE_CHECK_NOTNULL(op_desc_ptr);
  1309. int64_t temp_stream_id = node->GetOpDesc()->GetStreamId();
  1310. op_desc_ptr->SetStreamId(temp_stream_id);
  1311. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(op_desc_ptr, RECV_ATTR_EVENT_ID, event_id),
  1312. REPORT_INNER_ERROR("E19999", "Set Attr:%s for op:%s(%s) failed, event_id:%u,",
  1313. RECV_ATTR_EVENT_ID.c_str(),
  1314. node->GetName().c_str(), node->GetType().c_str(), event_id);
  1315. GELOGE(FAILED, "[Set][Attr] %s for op:%s(%s) failed, event_id:%u,",
  1316. RECV_ATTR_EVENT_ID.c_str(), node->GetName().c_str(), node->GetType().c_str(), event_id);
  1317. return FAILED);
  1318. (void)AttrUtils::SetListStr(op_desc_ptr, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
  1319. std::move(std::vector<std::string>()));
  1320. NodePtr recv_node = node->GetOwnerComputeGraph()->AddNode(op_desc_ptr);
  1321. GE_CHECK_NOTNULL(recv_node);
  1322. GE_CHECK_NOTNULL(recv_node->GetOutControlAnchor());
  1323. Status status = GraphUtils::AddEdge(recv_node->GetOutControlAnchor(), node->GetInControlAnchor());
  1324. if (status != SUCCESS) {
  1325. REPORT_INNER_ERROR("E19999", "Add edge from node %s to node %s failed",
  1326. recv_node->GetName().c_str(), node->GetName().c_str());
  1327. GELOGE(status, "[Add][Edge] for node %s and node %s failed.", recv_node->GetName().c_str(),
  1328. node->GetName().c_str());
  1329. return status;
  1330. }
  1331. GELOGI("Insert recv event %u before node: %s.", event_id, node->GetName().c_str());
  1332. }
  1333. // Add the node corresponding to the send event
  1334. vector<uint32_t> send_event_id_list;
  1335. GetSendEventIdList(node, send_event_id_list);
  1336. for (auto &event_id : send_event_id_list) {
  1337. string send_node_name = whole_graph_->GetName() + "_Send_" + to_string(event_id);
  1338. auto iter = sync_event_name.find(send_node_name);
  1339. if (iter == sync_event_name.end()) {
  1340. sync_event_name[send_node_name] = 1;
  1341. } else {
  1342. send_node_name = send_node_name + "_Reuse_" + to_string(iter->second);
  1343. ++(iter->second);
  1344. }
  1345. OpDescPtr op_desc_ptr = MakeShared<OpDesc>(send_node_name, SEND);
  1346. GE_CHECK_NOTNULL(op_desc_ptr);
  1347. int64_t temp_stream_id = node->GetOpDesc()->GetStreamId();
  1348. op_desc_ptr->SetStreamId(temp_stream_id);
  1349. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(op_desc_ptr, SEND_ATTR_EVENT_ID, event_id), GELOGE(FAILED, "SetInt failed.");
  1350. return FAILED);
  1351. (void)AttrUtils::SetListStr(op_desc_ptr, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
  1352. std::move(std::vector<std::string>()));
  1353. NodePtr send_node = node->GetOwnerComputeGraph()->AddNode(op_desc_ptr);
  1354. GE_CHECK_NOTNULL(send_node);
  1355. GE_CHECK_NOTNULL(send_node->GetInControlAnchor());
  1356. Status status = GraphUtils::AddEdge(node->GetOutControlAnchor(), send_node->GetInControlAnchor());
  1357. if (status != SUCCESS) {
  1358. REPORT_INNER_ERROR("E19999", "Add edge from node %s to node %s failed",
  1359. node->GetName().c_str(), send_node->GetName().c_str());
  1360. GELOGE(status, "[Add][Edge] for node %s and node %s failed.", node->GetName().c_str(),
  1361. send_node->GetName().c_str());
  1362. return status;
  1363. }
  1364. GELOGI("Insert send event %u after node: %s.", event_id, node->GetName().c_str());
  1365. }
  1366. }
  1367. Status status = whole_graph_->InsertGraphEvents();
  1368. if (status != SUCCESS) {
  1369. REPORT_CALL_ERROR("E19999", "Insert Graph Events fail, graph:%s,",
  1370. whole_graph_->GetName().c_str());
  1371. GELOGE(status, "[Insert][GraphEvents] Graph ReorderEventNodes failed, graph:%s,",
  1372. whole_graph_->GetName().c_str());
  1373. return status;
  1374. }
  1375. return SUCCESS;
  1376. }
  1377. void StreamAllocator::DumpEvents() {
  1378. map<int64_t, vector<NodePtr>> after_refresh_stream_nodes;
  1379. for (const auto &node : whole_graph_->GetNodes(whole_graph_->GetGraphUnknownFlag())) {
  1380. GE_IF_BOOL_EXEC(node->GetOpDesc() == nullptr, continue);
  1381. int64_t stream_id = node->GetOpDesc()->GetStreamId();
  1382. after_refresh_stream_nodes[stream_id].emplace_back(node);
  1383. }
  1384. for (const auto &one_pair : after_refresh_stream_nodes) {
  1385. int64_t stream_id = one_pair.first;
  1386. GELOGD("After RefreshRealStream: stream %ld.", stream_id);
  1387. for (const auto &node : one_pair.second) {
  1388. if (node == nullptr || node->GetOpDesc() == nullptr) {
  1389. continue;
  1390. }
  1391. string send_event_str;
  1392. for (const auto &send_event_id : node_to_send_events_[node]) {
  1393. send_event_str += " " + to_string(send_event_id);
  1394. }
  1395. if (!send_event_str.empty()) {
  1396. GELOGI("node: %s, id: %ld, stream id :%ld, send events: %s.", node->GetName().c_str(),
  1397. node->GetOpDesc()->GetId(), node->GetOpDesc()->GetStreamId(), send_event_str.c_str());
  1398. }
  1399. string recv_event_str;
  1400. for (const auto &recv_event_id : node_to_recv_events_[node]) {
  1401. recv_event_str += " " + to_string(recv_event_id);
  1402. }
  1403. if (!recv_event_str.empty()) {
  1404. GELOGI("node: %s, id: %ld, stream id :%ld, recv events: %s.", node->GetName().c_str(),
  1405. node->GetOpDesc()->GetId(), node->GetOpDesc()->GetStreamId(), recv_event_str.c_str());
  1406. }
  1407. }
  1408. }
  1409. }
  1410. Status StreamAllocator::GetMaxStreamAndTask(bool huge_stream, uint32_t &max_stream_count, uint32_t &max_task_count) {
  1411. uint32_t stream_type = RT_NORMAL_STREAM;
  1412. if (huge_stream) {
  1413. stream_type = RT_HUGE_STREAM;
  1414. }
  1415. rtError_t ret = rtGetMaxStreamAndTask(stream_type, &max_stream_count, &max_task_count);
  1416. if (ret != RT_ERROR_NONE) {
  1417. REPORT_CALL_ERROR("E19999", "call rtGetMaxStreamAndTask fail, ret:%d, stream_type:%u,",
  1418. static_cast<int>(ret), stream_type);
  1419. GELOGE(FAILED, "[Call][RtGetMaxStreamAndTask] Get max stream and task count by rts failed, "
  1420. "ret:%d, stream_type:%u,", static_cast<int>(ret), stream_type);
  1421. return FAILED;
  1422. }
  1423. GELOGD("Allowed max stream count: %u, max task count per stream: %u.", max_stream_count, max_task_count);
  1424. return SUCCESS;
  1425. }
  1426. int64_t StreamAllocator::GetMaxNodeNumPerStream(const NodePtr &node, uint32_t max_task_count) {
  1427. int64_t max_node_num_one_stream = static_cast<int64_t>(max_task_count);
  1428. string op_type = node->GetType();
  1429. if (IsHcclOp(op_type)) {
  1430. max_node_num_one_stream /= kTaskNumPerHcclNode;
  1431. } else {
  1432. max_node_num_one_stream /= kTaskNumPerNormalNode;
  1433. }
  1434. if (max_node_num_one_stream == 0) {
  1435. max_node_num_one_stream = 1;
  1436. }
  1437. return max_node_num_one_stream;
  1438. }
  1439. void StreamAllocator::AddNodeNum(const NodePtr &node, int64_t &node_num) {
  1440. node_num++;
  1441. vector<uint32_t> events;
  1442. GetSendEventIdList(node, events);
  1443. node_num += static_cast<int64_t>(events.size());
  1444. GetRecvEventIdList(node, events);
  1445. node_num += static_cast<int64_t>(events.size());
  1446. }
  1447. // Insert send event id on a node
  1448. void StreamAllocator::AddSendEventId(const NodePtr &node, uint32_t event_id) {
  1449. node_to_send_events_[node].emplace_back(event_id);
  1450. }
  1451. // Insert recv event id on a node
  1452. void StreamAllocator::AddRecvEventId(const NodePtr &node, uint32_t event_id) {
  1453. node_to_recv_events_[node].emplace_back(event_id);
  1454. }
  1455. // Remove send event id from a node
  1456. void StreamAllocator::RmvSendEventId(const NodePtr &node, uint32_t event_id) {
  1457. auto find_it = node_to_send_events_.find(node);
  1458. if (find_it == node_to_send_events_.end()) {
  1459. return;
  1460. }
  1461. vector<uint32_t> &send_events = find_it->second;
  1462. for (auto it = send_events.begin(); it != send_events.end(); ++it) {
  1463. if (*it == event_id) {
  1464. send_events.erase(it);
  1465. return;
  1466. }
  1467. }
  1468. }
  1469. // Remove recv event id from a node
  1470. void StreamAllocator::RmvRecvEventId(const NodePtr &node, uint32_t event_id) {
  1471. auto find_it = node_to_recv_events_.find(node);
  1472. if (find_it == node_to_recv_events_.end()) {
  1473. return;
  1474. }
  1475. vector<uint32_t> &recv_events = find_it->second;
  1476. for (auto it = recv_events.begin(); it != recv_events.end(); ++it) {
  1477. if (*it == event_id) {
  1478. recv_events.erase(it);
  1479. return;
  1480. }
  1481. }
  1482. }
  1483. // Get send event id list from a node
  1484. void StreamAllocator::GetSendEventIdList(const NodePtr &node, vector<uint32_t> &send_list) const {
  1485. send_list.clear();
  1486. auto find_it = node_to_send_events_.find(node);
  1487. if (find_it != node_to_send_events_.end()) {
  1488. send_list = find_it->second;
  1489. }
  1490. }
  1491. // Get recv event id list from a node
  1492. void StreamAllocator::GetRecvEventIdList(const NodePtr &node, vector<uint32_t> &recv_list) const {
  1493. recv_list.clear();
  1494. auto find_it = node_to_recv_events_.find(node);
  1495. if (find_it != node_to_recv_events_.end()) {
  1496. recv_list = find_it->second;
  1497. }
  1498. }
  1499. // Get a specific send node according to the recv event
  1500. NodePtr StreamAllocator::GetNodeFromSendEventId(uint32_t send_event_id) const {
  1501. for (const auto &one_pair : node_to_send_events_) {
  1502. const vector<uint32_t> &events = one_pair.second;
  1503. for (const auto &event_id : events) {
  1504. if (event_id == send_event_id) {
  1505. return one_pair.first;
  1506. }
  1507. }
  1508. }
  1509. return nullptr;
  1510. }
  1511. // Get a specific recv node according to the recv event
  1512. NodePtr StreamAllocator::GetNodeFromRecvEventId(uint32_t recv_event_id) const {
  1513. for (const auto &one_pair : node_to_recv_events_) {
  1514. const vector<uint32_t> &events = one_pair.second;
  1515. for (const auto &event_id : events) {
  1516. if (event_id == recv_event_id) {
  1517. return one_pair.first;
  1518. }
  1519. }
  1520. }
  1521. return nullptr;
  1522. }
  1523. Status StreamAllocator::AddEventId(const NodePtr &pre_node, const NodePtr &not_cur, const NodePtr &cur_node,
  1524. bool not_use_cur) {
  1525. GELOGI("Add send event %u for node %s", event_num_, pre_node->GetName().c_str());
  1526. AddSendEventId(pre_node, event_num_);
  1527. if (not_use_cur) {
  1528. GE_CHECK_NOTNULL(not_cur);
  1529. GELOGI("Add recv event %u for node %s", event_num_, not_cur->GetName().c_str());
  1530. AddRecvEventId(not_cur, event_num_);
  1531. } else {
  1532. GELOGI("Add recv event %u for node %s", event_num_, cur_node->GetName().c_str());
  1533. AddRecvEventId(cur_node, event_num_);
  1534. }
  1535. ++event_num_;
  1536. return SUCCESS;
  1537. }
  1538. Status StreamAllocator::AddActiveNodes(NodePtr &switch_node, const vector<string> &ori_active_label_list,
  1539. vector<string> &active_label_list, vector<NodePtr> &added_active_nodes) {
  1540. size_t label_num = ori_active_label_list.size();
  1541. for (size_t i = 0; i < label_num; i++) {
  1542. const string &active_label = ori_active_label_list[i];
  1543. if (labeled_streams_.find(active_label) == labeled_streams_.end()) {
  1544. REPORT_INNER_ERROR("E19999", "can not find stream label:%s", active_label.c_str());
  1545. GELOGE(FAILED, "[Check][Param] can not find stream label %s", active_label.c_str());
  1546. return FAILED;
  1547. }
  1548. if (labeled_streams_[active_label].size() <= 1) {
  1549. active_label_list.emplace_back(active_label);
  1550. continue;
  1551. }
  1552. string name = switch_node->GetName() + "_" + STREAMACTIVE + "_" + std::to_string(i);
  1553. GELOGI("Create StreamActive op %s after node %s.", name.c_str(), switch_node->GetName().c_str());
  1554. OpDescPtr active_op_desc = MakeShared<OpDesc>(name, STREAMACTIVE);
  1555. GE_CHECK_NOTNULL(active_op_desc);
  1556. NodePtr active_node = whole_graph_->AddNode(active_op_desc);
  1557. GE_CHECK_NOTNULL(active_node);
  1558. for (NodePtr &node : switch_node->GetOutControlNodes()) {
  1559. OpDescPtr op_desc = node->GetOpDesc();
  1560. GE_CHECK_NOTNULL(op_desc);
  1561. string stream_label;
  1562. // If GetStr failed, stream_label is empty.
  1563. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label);
  1564. if (stream_label != active_label) {
  1565. continue;
  1566. }
  1567. GE_CHECK_NOTNULL(switch_node->GetOutControlAnchor());
  1568. if (switch_node->GetOutControlAnchor()->Unlink(node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  1569. REPORT_CALL_ERROR("E19999", "Unlink %s to %s failed",
  1570. switch_node->GetName().c_str(), node->GetName().c_str());
  1571. GELOGE(FAILED, "[Unlink][Nodes] %s to %s failed.", switch_node->GetName().c_str(), node->GetName().c_str());
  1572. return FAILED;
  1573. }
  1574. GE_CHECK_NOTNULL(active_node->GetOutControlAnchor());
  1575. if (active_node->GetOutControlAnchor()->LinkTo(node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  1576. REPORT_CALL_ERROR("E19999", "Link %s to %s failed",
  1577. active_node->GetName().c_str(), node->GetName().c_str());
  1578. GELOGE(FAILED, "[Link][Nodes] %s to %s failed.", active_node->GetName().c_str(), node->GetName().c_str());
  1579. return FAILED;
  1580. }
  1581. }
  1582. if (SetSwitchBranchNodeLabel(active_node, name) != SUCCESS) {
  1583. GELOGE(FAILED, "[Set][SwitchBranchNodeLabel] failed, node:%s.", active_node->GetName().c_str());
  1584. return FAILED;
  1585. }
  1586. if (SetStreamLabel(active_node, name) != SUCCESS) {
  1587. GELOGE(FAILED, "[Set][StreamLabel] failed, node:%s.", active_node->GetName().c_str());
  1588. return FAILED;
  1589. }
  1590. if (SetActiveLabelList(active_node, {active_label}) != SUCCESS) {
  1591. GELOGE(FAILED, "[Set][ActiveLabelList] failed, node:%s.", active_node->GetName().c_str());
  1592. return FAILED;
  1593. }
  1594. if (SetActiveStreamList(active_node, active_label) != SUCCESS) {
  1595. GELOGE(FAILED, "[Set][ActiveStreamList] failed, node:%s.", active_node->GetName().c_str());
  1596. return FAILED;
  1597. }
  1598. added_active_nodes.emplace_back(active_node);
  1599. active_label_list.emplace_back(name);
  1600. }
  1601. return SUCCESS;
  1602. }
  1603. Status StreamAllocator::SetActiveStreamList(NodePtr &active_node, const string &active_label) {
  1604. if (labeled_streams_.find(active_label) == labeled_streams_.end()) {
  1605. REPORT_INNER_ERROR("E19999", "Can not find stream label:%s", active_label.c_str());
  1606. GELOGE(FAILED, "[Check][Param] Can not find stream label %s.", active_label.c_str());
  1607. return FAILED;
  1608. }
  1609. set<int64_t> &streams = labeled_streams_[active_label];
  1610. vector<int64_t> active_streams(streams.begin(), streams.end());
  1611. if (!AttrUtils::SetListInt(active_node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
  1612. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1613. active_node->GetName().c_str(), active_node->GetType().c_str());
  1614. GELOGE(FAILED, "[Set][Attr] %s failed for op:%s(%s).", ATTR_NAME_ACTIVE_STREAM_LIST.c_str(),
  1615. active_node->GetName().c_str(), active_node->GetType().c_str());
  1616. return FAILED;
  1617. }
  1618. return SUCCESS;
  1619. }
  1620. } // namespace ge

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