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

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