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

stream_allocator.cc 77 kB

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

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