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

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