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.

logical_stream_allocator.cc 27 kB

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
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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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/logical_stream_allocator.h"
  17. #include <queue>
  18. #include "common/ge/ge_util.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/common/fmk_error_codes.h"
  21. #include "framework/common/types.h"
  22. #include "graph/debug/ge_attr_define.h"
  23. #include "graph/utils/graph_utils.h"
  24. #include "graph/common/ge_call_wrapper.h"
  25. using std::map;
  26. using std::set;
  27. using std::string;
  28. using std::vector;
  29. using std::queue;
  30. namespace ge {
  31. LogicalStreamPass::LogicalStreamPass(const string &name) : name_(name) {}
  32. const string &LogicalStreamPass::GetName() const { return name_; }
  33. bool LogicalStreamPass::IsEngineSkip(const Subgraph &subgraph) const { return subgraph.engine_conf.skip_assign_stream; }
  34. bool LogicalStreamPass::IsEngineAttach(const Subgraph &subgraph) const { return subgraph.engine_conf.attach; }
  35. bool LogicalStreamPass::IsEngineIndependent(const Subgraph &subgraph) const { return subgraph.engine_conf.independent; }
  36. bool LogicalStreamPass::HasStreamLabel(const Subgraph &subgraph) const {
  37. return !subgraph.subgraph_info.GetStreamLabel().empty();
  38. }
  39. bool LogicalStreamPass::HasAssignedStream(const Subgraph &subgraph) const {
  40. return subgraph.stream_id != kInvalidStream;
  41. }
  42. Status AssignByLabelPass::Run(ComputeGraphPtr graph, const vector<SubgraphPtr> &subgraphs, Context &context) {
  43. bool changed = false;
  44. int64_t &next_stream = context.next_stream;
  45. map<string, int64_t> label_streams;
  46. for (const SubgraphPtr &subgraph : subgraphs) {
  47. const string &stream_label = subgraph->subgraph_info.GetStreamLabel();
  48. if (!stream_label.empty()) {
  49. // Subgraphs of the same stream_label are assigned to the same stream,
  50. // and different stream_labels are assigned new streams.
  51. auto iter = label_streams.find(stream_label);
  52. if (iter != label_streams.end()) {
  53. subgraph->stream_id = iter->second;
  54. } else {
  55. subgraph->stream_id = next_stream;
  56. GELOGI("Assign new stream %ld for label %s.", next_stream, stream_label.c_str());
  57. label_streams.emplace(stream_label, next_stream);
  58. ++next_stream;
  59. }
  60. changed = true;
  61. }
  62. }
  63. return changed ? SUCCESS : NOT_CHANGED;
  64. }
  65. Status IndependentStreamPass::Run(ComputeGraphPtr graph, const vector<SubgraphPtr> &subgraphs, Context &context) {
  66. bool changed = false;
  67. int64_t &next_stream = context.next_stream;
  68. // <engine, <label, stream>>
  69. map<string, map<string, int64_t>> engine_streams;
  70. for (const SubgraphPtr &subgraph : subgraphs) {
  71. if (!IsEngineIndependent(*subgraph)) {
  72. continue;
  73. }
  74. const string &engine = subgraph->engine_conf.id;
  75. const string &stream_label = subgraph->subgraph_info.GetStreamLabel();
  76. auto &label_streams = engine_streams[engine];
  77. auto iter = label_streams.find(stream_label);
  78. if (iter != label_streams.end()) {
  79. subgraph->stream_id = iter->second;
  80. } else {
  81. subgraph->stream_id = next_stream;
  82. GELOGI("Assign new independent stream %ld for engine %s (label: %s).", next_stream, engine.c_str(),
  83. stream_label.c_str());
  84. label_streams.emplace(stream_label, next_stream);
  85. ++next_stream;
  86. }
  87. changed = true;
  88. }
  89. return changed ? SUCCESS : NOT_CHANGED;
  90. }
  91. Status AssignByDependencyPass::Run(ComputeGraphPtr graph, const vector<SubgraphPtr> &subgraphs, Context &context) {
  92. bool changed = false;
  93. map<NodePtr, SubgraphPtr> end_subgraph_map;
  94. map<NodePtr, SubgraphPtr> pld_subgraph_map;
  95. InitEndSubgraphMap(subgraphs, end_subgraph_map);
  96. InitPldSubgraphMap(subgraphs, pld_subgraph_map);
  97. for (const SubgraphPtr &subgraph : subgraphs) {
  98. if (HasAssignedStream(*subgraph)) {
  99. continue;
  100. }
  101. SubgraphPtr reusable_subgraph = GetReusableSubgraph(subgraph, end_subgraph_map, pld_subgraph_map);
  102. if (reusable_subgraph != nullptr) {
  103. if (HasAssignedStream(*reusable_subgraph)) {
  104. subgraph->stream_id = reusable_subgraph->stream_id;
  105. } else {
  106. int64_t stream_id = AssignNewStream(reusable_subgraph);
  107. subgraph->stream_id = stream_id;
  108. GELOGI("Reusable subgraph %s has not been assigned a stream, now assign new stream %ld.",
  109. reusable_subgraph->name.c_str(), stream_id);
  110. }
  111. if (reusable_subgraph->reused_subgraph != nullptr) {
  112. reusable_subgraph = reusable_subgraph->reused_subgraph;
  113. }
  114. subgraph->reused_subgraph = reusable_subgraph;
  115. reused_subgraphs_.emplace_back(subgraph, reusable_subgraph);
  116. GELOGI("Subgraph %s of engine %s reuses stream of subgraph %s of engine %s.", subgraph->name.c_str(),
  117. subgraph->engine_conf.id.c_str(), reusable_subgraph->name.c_str(),
  118. reusable_subgraph->engine_conf.id.c_str());
  119. } else {
  120. (void)AssignNewStream(subgraph);
  121. }
  122. changed = true;
  123. }
  124. UpdateAssignedSubgraphs(context);
  125. UpdateReusedSubgraphs();
  126. return changed ? SUCCESS : NOT_CHANGED;
  127. }
  128. void AssignByDependencyPass::InitEndSubgraphMap(const vector<SubgraphPtr> &subgraphs,
  129. map<NodePtr, SubgraphPtr> &end_subgraph_map) {
  130. for (const auto &subgraph : subgraphs) {
  131. const SubGraphInfo &subgraph_info = subgraph->subgraph_info;
  132. for (const auto &item : subgraph_info.GetEnd2PldMap()) {
  133. end_subgraph_map.emplace(item.first, subgraph);
  134. }
  135. }
  136. }
  137. void AssignByDependencyPass::InitPldSubgraphMap(const vector<SubgraphPtr> &subgraphs,
  138. map<NodePtr, SubgraphPtr> &pld_subgraph_map) {
  139. for (const auto &subgraph : subgraphs) {
  140. const SubGraphInfo &subgraph_info = subgraph->subgraph_info;
  141. for (const auto &item : subgraph_info.GetPld2EndMap()) {
  142. pld_subgraph_map.emplace(item.first, subgraph);
  143. }
  144. }
  145. }
  146. bool AssignByDependencyPass::CouldReuse(const SubgraphPtr &subgraph, const SubgraphPtr &pred_subgraph,
  147. const map<NodePtr, SubgraphPtr> &pld_subgraph_map) {
  148. if ((subgraph == nullptr) || (pred_subgraph == nullptr)) {
  149. return false;
  150. }
  151. if (subgraph->engine_conf.scheduler_id != pred_subgraph->engine_conf.scheduler_id) {
  152. return false;
  153. }
  154. if (IsEngineIndependent(*pred_subgraph) || HasStreamLabel(*pred_subgraph)) {
  155. return false;
  156. }
  157. // If the engine of the predecessor subgraph is the same as the other successor subgraphs, the stream is not reused.
  158. for (const auto &end_pld_pair : pred_subgraph->subgraph_info.GetEnd2PldMap()) {
  159. auto iter = pld_subgraph_map.find(end_pld_pair.second);
  160. if (iter != pld_subgraph_map.end()) {
  161. const SubgraphPtr &pred_subgraph_succ = iter->second;
  162. if (pred_subgraph_succ != subgraph && pred_subgraph_succ->engine_conf.id == pred_subgraph->engine_conf.id) {
  163. return false;
  164. }
  165. }
  166. }
  167. if ((subgraph->engine_conf.id == pred_subgraph->engine_conf.id) || IsEngineAttach(*subgraph)) {
  168. return true;
  169. }
  170. if ((pred_subgraph->reused_subgraph != nullptr) &&
  171. (pred_subgraph->reused_subgraph->engine_conf.id == subgraph->engine_conf.id)) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. LogicalStreamPass::SubgraphPtr AssignByDependencyPass::GetReusableSubgraph(
  177. const SubgraphPtr &subgraph, const map<NodePtr, SubgraphPtr> &end_subgraph_map,
  178. const map<NodePtr, SubgraphPtr> &pld_subgraph_map) {
  179. const SubGraphInfo &subgraph_info = subgraph->subgraph_info;
  180. for (const auto &pld_2_end : subgraph_info.GetPld2EndMap()) {
  181. const NodePtr &peer_end = pld_2_end.second;
  182. auto iter = end_subgraph_map.find(peer_end);
  183. if (iter != end_subgraph_map.end()) {
  184. const SubgraphPtr &pred_subgraph = iter->second;
  185. if (CouldReuse(subgraph, pred_subgraph, pld_subgraph_map)) {
  186. return pred_subgraph;
  187. }
  188. }
  189. }
  190. return nullptr;
  191. }
  192. int64_t AssignByDependencyPass::AssignNewStream(SubgraphPtr subgraph) {
  193. const string &engine_name = subgraph->engine_conf.id;
  194. int64_t max_parallel_num = subgraph->max_parallel_num;
  195. int64_t stream_id = 0;
  196. auto next_iter = engine_next_streams_.find(engine_name);
  197. if (next_iter != engine_next_streams_.end()) {
  198. stream_id = next_iter->second;
  199. }
  200. if (stream_id >= max_parallel_num) {
  201. stream_id = 0;
  202. }
  203. subgraph->stream_id = stream_id;
  204. engine_next_streams_[engine_name] = stream_id + 1;
  205. assigned_subgraphs_.emplace_back(subgraph);
  206. if ((stream_id + 1) > engine_stream_num_[engine_name]) {
  207. engine_stream_num_[engine_name] = stream_id + 1;
  208. }
  209. GELOGI("Subgraph %s assigns new temp stream %ld (engine: %s).", subgraph->name.c_str(), stream_id,
  210. engine_name.c_str());
  211. return stream_id;
  212. }
  213. void AssignByDependencyPass::UpdateAssignedSubgraphs(Context &context) {
  214. // If the default stream is valid, the first assigned stream will reuse the default stream id
  215. // and other streams use new id. To ensure that the id of the new stream is continuous,
  216. // we first subtract one from next_stream.
  217. int64_t to_be_updated_stream = kInvalidStream;
  218. if (context.default_stream != kInvalidStream) {
  219. context.next_stream--;
  220. to_be_updated_stream = context.next_stream;
  221. }
  222. // Update the starting stream id for each engine.
  223. int64_t &next_stream = context.next_stream;
  224. map<string, int64_t> engine_start_streams;
  225. for (const auto &item : engine_stream_num_) {
  226. int64_t stream_count = item.second;
  227. engine_start_streams[item.first] = next_stream;
  228. next_stream += stream_count;
  229. }
  230. // Update the subgraph streams assigned by engine.
  231. for (auto &subgraph : assigned_subgraphs_) {
  232. subgraph->stream_id += engine_start_streams[subgraph->engine_conf.id];
  233. if (subgraph->stream_id == to_be_updated_stream) {
  234. subgraph->stream_id = context.default_stream;
  235. GELOGI("Subgraph %s of engine %s reuses default stream %ld.", subgraph->name.c_str(),
  236. subgraph->engine_conf.id.c_str(), context.default_stream);
  237. } else {
  238. GELOGI("Stream of subgraph %s has been updated to %ld.", subgraph->name.c_str(), subgraph->stream_id);
  239. }
  240. }
  241. }
  242. void AssignByDependencyPass::UpdateReusedSubgraphs() {
  243. // Update streams for the subgraphs of reusing stream.
  244. for (const auto &item : reused_subgraphs_) {
  245. auto &cur_subgraph = item.first;
  246. auto &reused_graph = item.second;
  247. cur_subgraph->stream_id = reused_graph->stream_id;
  248. GELOGI("Stream of subgraph %s has been updated to %ld.", cur_subgraph->name.c_str(), cur_subgraph->stream_id);
  249. }
  250. }
  251. Status SingleStreamPass::Run(ComputeGraphPtr graph, const vector<SubgraphPtr> &subgraphs, Context &context) {
  252. // context.default_stream can be kInvalidStream only when graph is the root graph.
  253. int64_t new_stream = context.default_stream;
  254. if (new_stream == kInvalidStream) {
  255. new_stream = context.next_stream;
  256. ++context.next_stream;
  257. }
  258. for (const SubgraphPtr &subgraph : subgraphs) {
  259. if (!HasAssignedStream(*subgraph)) {
  260. const string &stream_label = subgraph->subgraph_info.GetStreamLabel();
  261. if (!stream_label.empty()) {
  262. GELOGE(INTERNAL_ERROR, "Stream labels are not supported (subgraph: %s, stream label: %s).",
  263. subgraph->name.c_str(), stream_label.c_str());
  264. return INTERNAL_ERROR;
  265. }
  266. subgraph->stream_id = new_stream;
  267. }
  268. }
  269. return SUCCESS;
  270. }
  271. Status NodeStreamUpdatePass::Run(ComputeGraphPtr graph, const vector<SubgraphPtr> &subgraphs, Context &context) {
  272. // Check if all subgraphs have been assigned a stream.
  273. for (const SubgraphPtr &subgraph : subgraphs) {
  274. const string &engine_name = subgraph->engine_conf.id;
  275. if (!IsEngineSkip(*subgraph) && !HasAssignedStream(*subgraph)) {
  276. GELOGE(INTERNAL_ERROR, "Subgraph %s has not yet been assigned a stream (engine: %s).", subgraph->name.c_str(),
  277. engine_name.c_str());
  278. return INTERNAL_ERROR;
  279. } else {
  280. GELOGI("Subgraph %s is assigned stream %ld (engine: %s).", subgraph->name.c_str(), subgraph->stream_id,
  281. engine_name.c_str());
  282. }
  283. }
  284. // Init the stream id of node.
  285. for (NodePtr &node : graph->GetDirectNode()) {
  286. GE_CHECK_NOTNULL(node->GetOpDesc());
  287. node->GetOpDesc()->SetStreamId(kInvalidStream);
  288. }
  289. // Set the stream id of the subgraph to the node.
  290. for (const SubgraphPtr &subgraph : subgraphs) {
  291. int64_t stream_id = subgraph->stream_id;
  292. const string &engine_name = subgraph->engine_conf.id;
  293. auto compute_graph = subgraph->subgraph_info.GetSubGraph();
  294. for (NodePtr &node : compute_graph->GetDirectNode()) {
  295. GE_CHECK_NOTNULL(node->GetOpDesc());
  296. if (node->GetOpDesc()->HasAttr(ATTR_NAME_RTS_LABEL_NODE)) {
  297. node->GetOpDesc()->SetStreamId(context.default_stream);
  298. GELOGD("Node %s of type %s in subgraph %s is assigned parent stream %ld (engine: %s).", node->GetName().c_str(),
  299. node->GetType().c_str(), subgraph->name.c_str(), context.default_stream, engine_name.c_str());
  300. } else if (IsEngineSkip(*subgraph) && node->GetInNodes().empty()) {
  301. GELOGD("Node %s of type %s in subgraph %s doesn't need to assign a stream (engine: %s).",
  302. node->GetName().c_str(), node->GetType().c_str(), subgraph->name.c_str(), engine_name.c_str());
  303. } else {
  304. node->GetOpDesc()->SetStreamId(stream_id);
  305. GELOGD("Node %s of type %s in subgraph %s is assigned stream %ld (engine: %s).", node->GetName().c_str(),
  306. node->GetType().c_str(), subgraph->name.c_str(), stream_id, engine_name.c_str());
  307. }
  308. }
  309. }
  310. // Update stream id for nodes belong to skipped engine subgraph
  311. GE_CHK_STATUS_RET(UpdateForSkippedEngine(graph, subgraphs));
  312. return SUCCESS;
  313. }
  314. int64_t NodeStreamUpdatePass::GetSingleInoutStream(const NodePtr &node) const {
  315. set<int64_t> stream_ids;
  316. for (const auto &in_node : node->GetInAllNodes()) {
  317. GE_CHECK_NOTNULL_EXEC(in_node->GetOpDesc(), return kInvalidStream);
  318. int64_t stream_id = in_node->GetOpDesc()->GetStreamId();
  319. if (stream_id != kInvalidStream) {
  320. stream_ids.insert(stream_id);
  321. }
  322. }
  323. for (const auto &out_node : node->GetOutAllNodes()) {
  324. GE_CHECK_NOTNULL_EXEC(out_node->GetOpDesc(), return kInvalidStream);
  325. int64_t stream_id = out_node->GetOpDesc()->GetStreamId();
  326. if (stream_id != kInvalidStream) {
  327. stream_ids.insert(stream_id);
  328. }
  329. }
  330. if (stream_ids.size() == 1) {
  331. int64_t stream_id = *(stream_ids.begin());
  332. GELOGI("The stream of all input and output nodes of node %s (type: %s) is %ld.", node->GetName().c_str(),
  333. node->GetType().c_str(), stream_id);
  334. return stream_id;
  335. }
  336. return kInvalidStream;
  337. }
  338. Status NodeStreamUpdatePass::UpdateForSkippedEngine(const ComputeGraphPtr &graph,
  339. const vector<SubgraphPtr> &subgraphs) {
  340. set<OpDescPtr> ops_without_label;
  341. // Check if subgraph is engine skipped and without stream label or not
  342. for (const SubgraphPtr &subgraph : subgraphs) {
  343. if (IsEngineSkip(*subgraph)) {
  344. auto compute_graph = subgraph->subgraph_info.GetSubGraph();
  345. for (NodePtr &node : compute_graph->GetDirectNode()) {
  346. auto op_desc = node->GetOpDesc();
  347. GE_CHECK_NOTNULL(op_desc);
  348. auto stream_id = op_desc->GetStreamId();
  349. if (stream_id != kInvalidStream && !HasStreamLabel(*subgraph)) {
  350. ops_without_label.emplace(op_desc);
  351. }
  352. }
  353. }
  354. }
  355. // Try reassign the stream id
  356. for (ge::NodePtr &node : graph->GetDirectNode()) {
  357. auto op_desc = node->GetOpDesc();
  358. GE_CHECK_NOTNULL(op_desc);
  359. int64_t stream_id = op_desc->GetStreamId();
  360. if (ops_without_label.find(op_desc) != ops_without_label.end()) {
  361. if (AreAllPredStreamsInvalid(node) && op_desc->GetSubgraphInstanceNames().empty()) {
  362. op_desc->SetStreamId(kInvalidStream);
  363. GELOGI("Node %s of type %s reassign to stream %ld from stream %ld.", node->GetName().c_str(),
  364. node->GetType().c_str(), kInvalidStream, stream_id);
  365. } else if (!node->GetOutAllNodes().empty()) {
  366. int64_t inout_stream = GetSingleInoutStream(node);
  367. if (inout_stream != kInvalidStream) {
  368. op_desc->SetStreamId(inout_stream);
  369. GELOGI("Node %s of type %s reassign to stream %ld from stream %ld.", node->GetName().c_str(),
  370. node->GetType().c_str(), inout_stream, stream_id);
  371. }
  372. }
  373. }
  374. }
  375. return SUCCESS;
  376. }
  377. bool NodeStreamUpdatePass::AreAllPredStreamsInvalid(const NodePtr &node) const {
  378. for (const auto &pre_node : node->GetInAllNodes()) {
  379. auto pre_node_desc = pre_node->GetOpDesc();
  380. if (pre_node_desc != nullptr) {
  381. int64_t stream_id = pre_node_desc->GetStreamId();
  382. if (stream_id != kInvalidStream) {
  383. return false;
  384. }
  385. }
  386. }
  387. return true;
  388. }
  389. Status AllReduceParallelPass::Run(ComputeGraphPtr graph, const vector<SubgraphPtr> &subgraphs, Context &context) {
  390. if (!context.enable_hcom_parallel) {
  391. return NOT_CHANGED;
  392. }
  393. GELOGI("AllReduceParallelPass is enabled.");
  394. GE_DUMP(graph, "BeforeAllReduceParallel");
  395. // All successors of HcomAllReduce.
  396. set<NodePtr> all_reduce_succs;
  397. for (const NodePtr &node : graph->GetDirectNode()) {
  398. if (!IsHcomNode(node->GetType()) ||
  399. node->GetInDataNodes().size() <= 1) {
  400. continue;
  401. }
  402. string reduce_stream_label;
  403. GE_CHECK_NOTNULL(node->GetOpDesc());
  404. (void)AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, reduce_stream_label);
  405. set<NodePtr> cur_nodes = {node};
  406. while (!cur_nodes.empty()) {
  407. set<NodePtr> all_out_data_nodes;
  408. for (auto &curr_node : cur_nodes) {
  409. for (const NodePtr &out_node : curr_node->GetOutDataNodes()) {
  410. string out_stream_label;
  411. GE_CHECK_NOTNULL(out_node->GetOpDesc());
  412. (void)AttrUtils::GetStr(out_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, out_stream_label);
  413. // normally, Allreduce do not have streamLabel. when in horovod scenario Allreduce will have streamLabel
  414. bool isSuccessorParallel =
  415. (out_stream_label == reduce_stream_label) || (!reduce_stream_label.empty() && out_stream_label.empty());
  416. if (isSuccessorParallel) {
  417. all_reduce_succs.emplace(out_node);
  418. all_out_data_nodes.emplace(out_node);
  419. }
  420. }
  421. }
  422. cur_nodes = all_out_data_nodes;
  423. }
  424. }
  425. map<int64_t, int64_t> old_stream_to_new;
  426. for (const NodePtr &node : all_reduce_succs) {
  427. GE_CHECK_NOTNULL(node->GetOpDesc());
  428. auto old_stream = node->GetOpDesc()->GetStreamId();
  429. if (old_stream != kInvalidStream) {
  430. int64_t new_stream = kInvalidStream;
  431. auto iter = old_stream_to_new.find(old_stream);
  432. if (iter != old_stream_to_new.end()) {
  433. new_stream = iter->second;
  434. } else {
  435. new_stream = context.next_stream;
  436. context.next_stream++;
  437. old_stream_to_new.emplace(old_stream, new_stream);
  438. }
  439. if (!IsHcomNode(node->GetType())) {
  440. GELOGI("Stream of node %s has been updated from %ld to %ld.", node->GetName().c_str(), old_stream, new_stream);
  441. node->GetOpDesc()->SetStreamId(new_stream);
  442. }
  443. }
  444. }
  445. return !all_reduce_succs.empty() ? SUCCESS : NOT_CHANGED;
  446. }
  447. bool AllReduceParallelPass::IsHcomNode(const std::string& node_type) {
  448. return (node_type == HCOMALLREDUCE || node_type == HVDCALLBACKALLREDUCE);
  449. }
  450. LogicalStreamAllocator::LogicalStreamAllocator(const map<string, SchedulerConf> &scheduler_confs,
  451. const map<string, int> &max_parallel_num)
  452. : scheduler_confs_(scheduler_confs), max_parallel_num_(max_parallel_num) {}
  453. void LogicalStreamAllocator::EnableSingleStream(bool enable) { context_.enable_single_stream = enable; }
  454. void LogicalStreamAllocator::EnableHcomParallel(bool enable) { context_.enable_hcom_parallel = enable; }
  455. Status LogicalStreamAllocator::Assign(const ComputeGraphPtr &root_graph, const Graph2SubGraphInfoList &subgraph_map,
  456. int64_t &stream_num) {
  457. GE_CHECK_NOTNULL(root_graph);
  458. map<string, EngineConfPtr> engine_confs;
  459. GE_TIMESTAMP_START(InitEngineConfs);
  460. for (const auto &item : scheduler_confs_) {
  461. const SchedulerConf &scheduler = item.second;
  462. for (const auto &engine_pair : scheduler.cal_engines) {
  463. EngineConfPtr engine_conf = engine_pair.second;
  464. if (engine_conf != nullptr) {
  465. engine_confs[engine_pair.first] = engine_conf;
  466. }
  467. }
  468. }
  469. GE_TIMESTAMP_END(InitEngineConfs, "GraphBuilder::AssignStreamInitEngineConfs");
  470. Status status = DoAssign(root_graph, subgraph_map, engine_confs);
  471. if (status != SUCCESS) {
  472. GELOGE(status, "Assign streams failed.");
  473. return status;
  474. }
  475. vector<ComputeGraphPtr> subgraphs = root_graph->GetAllSubgraphs();
  476. for (const ComputeGraphPtr &subgraph : subgraphs) {
  477. Status status = DoAssign(subgraph, subgraph_map, engine_confs);
  478. if (status != SUCCESS) {
  479. GELOGE(status, "Assign streams failed.");
  480. return status;
  481. }
  482. }
  483. RefreshContinuousStreams(root_graph);
  484. stream_num = context_.next_stream;
  485. GELOGI("Assigned logical stream num: %ld.", stream_num);
  486. return SUCCESS;
  487. }
  488. Status LogicalStreamAllocator::DoAssign(const ComputeGraphPtr &graph, const Graph2SubGraphInfoList &subgraph_map,
  489. const map<string, EngineConfPtr> &engine_confs) {
  490. GE_CHECK_NOTNULL(graph);
  491. NodePtr parent_node = graph->GetParentNode();
  492. if (parent_node == nullptr || parent_node->GetOpDesc() == nullptr) {
  493. context_.default_stream = kInvalidStream;
  494. } else {
  495. context_.default_stream = parent_node->GetOpDesc()->GetStreamId();
  496. }
  497. auto iter = subgraph_map.find(graph);
  498. if (iter == subgraph_map.end()) {
  499. GELOGE(FAILED, "Graph %s not found.", graph->GetName().c_str());
  500. return FAILED;
  501. }
  502. const vector<SubGraphInfoPtr> &subgraph_info_list = iter->second;
  503. vector<SubgraphPtr> subgraphs;
  504. GE_TIMESTAMP_START(ConvertSubgraphs);
  505. Status status = ConvertSubgraphs(subgraph_info_list, engine_confs, subgraphs);
  506. GE_TIMESTAMP_END(ConvertSubgraphs, "GraphBuilder::AssignStreamConvertSubgraphs");
  507. if (status != SUCCESS) {
  508. GELOGE(status, "Create subgraphs failed.");
  509. return status;
  510. }
  511. GELOGI("Subgraphs of graph %s:", graph->GetName().c_str());
  512. for (const auto &subgraph : subgraphs) {
  513. if (subgraph != nullptr) {
  514. GELOGI("subgraph: %s", subgraph->name.c_str());
  515. }
  516. }
  517. return RunPasses(graph, subgraphs);
  518. }
  519. Status LogicalStreamAllocator::ConvertSubgraphs(const vector<SubGraphInfoPtr> &subgraph_infos,
  520. const map<string, EngineConfPtr> &engine_confs,
  521. vector<SubgraphPtr> &subgraphs) {
  522. for (auto &subgraph_info : subgraph_infos) {
  523. GE_CHECK_NOTNULL(subgraph_info);
  524. string subgraph_name;
  525. ComputeGraphPtr computer_graph = subgraph_info->GetSubGraph();
  526. if (computer_graph != nullptr) {
  527. subgraph_name = computer_graph->GetName();
  528. }
  529. const string &engine_name = subgraph_info->GetEngineName();
  530. auto engine_conf_iter = engine_confs.find(engine_name);
  531. if ((engine_conf_iter == engine_confs.end()) || (engine_conf_iter->second == nullptr)) {
  532. GELOGE(INTERNAL_ERROR, "Engine conf of subgraph %s not found (engine name: %s).", subgraph_name.c_str(),
  533. engine_name.c_str());
  534. return INTERNAL_ERROR;
  535. }
  536. SubgraphPtr subgraph = MakeShared<Subgraph>(*subgraph_info, *engine_conf_iter->second);
  537. GE_CHECK_NOTNULL(subgraph);
  538. subgraph->name = subgraph_name;
  539. auto parallel_iter = max_parallel_num_.find(engine_name);
  540. if (parallel_iter != max_parallel_num_.end()) {
  541. subgraph->max_parallel_num = parallel_iter->second;
  542. }
  543. subgraphs.emplace_back(subgraph);
  544. }
  545. return SUCCESS;
  546. }
  547. Status LogicalStreamAllocator::RunPasses(const ComputeGraphPtr &graph, const vector<SubgraphPtr> &subgraphs) {
  548. vector<LogicalStreamPassPtr> passes;
  549. if (context_.enable_single_stream) {
  550. passes.emplace_back(MakeShared<SingleStreamPass>());
  551. passes.emplace_back(MakeShared<NodeStreamUpdatePass>());
  552. } else {
  553. passes.emplace_back(MakeShared<AssignByLabelPass>());
  554. passes.emplace_back(MakeShared<IndependentStreamPass>());
  555. passes.emplace_back(MakeShared<AssignByDependencyPass>());
  556. passes.emplace_back(MakeShared<NodeStreamUpdatePass>());
  557. passes.emplace_back(MakeShared<AllReduceParallelPass>());
  558. }
  559. for (auto &pass : passes) {
  560. GE_CHECK_NOTNULL(pass);
  561. Status status = pass->Run(graph, subgraphs, context_);
  562. if (status == SUCCESS) {
  563. GELOGI("Stream pass %s return SUCCESS.", pass->GetName().c_str());
  564. } else if (status == NOT_CHANGED) {
  565. GELOGI("Stream pass %s return NOT_CHANGED.", pass->GetName().c_str());
  566. } else {
  567. GELOGE(status, "Stream pass %s failed.", pass->GetName().c_str());
  568. return status;
  569. }
  570. }
  571. return SUCCESS;
  572. }
  573. void LogicalStreamAllocator::RefreshContinuousStreams(const ComputeGraphPtr &graph) {
  574. int64_t stream_num = context_.next_stream;
  575. vector<bool> stream_has_node(stream_num);
  576. for (const NodePtr &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  577. if (node != nullptr) {
  578. auto op_desc = node->GetOpDesc();
  579. if (op_desc != nullptr) {
  580. int64_t stream_id = op_desc->GetStreamId();
  581. if (stream_id != kInvalidStream && stream_id < stream_num) {
  582. stream_has_node[stream_id] = true;
  583. }
  584. }
  585. }
  586. }
  587. context_.next_stream = 0;
  588. vector<int64_t> old_to_new_streams(stream_num, kInvalidStream);
  589. for (size_t old_stream = 0; old_stream < stream_has_node.size(); ++old_stream) {
  590. if (stream_has_node[old_stream]) {
  591. old_to_new_streams[old_stream] = context_.next_stream;
  592. ++context_.next_stream;
  593. }
  594. }
  595. for (const NodePtr &node : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  596. auto op_desc = node->GetOpDesc();
  597. if (op_desc != nullptr) {
  598. int64_t stream_id = op_desc->GetStreamId();
  599. if (stream_id != kInvalidStream && stream_id < stream_num) {
  600. op_desc->SetStreamId(old_to_new_streams[stream_id]);
  601. }
  602. }
  603. }
  604. }
  605. } // namespace ge

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