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.

graph_mem_assigner.cc 77 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
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
4 years ago
5 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
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 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
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
4 years ago
5 years ago
4 years ago
5 years ago
4 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
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
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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/memory/graph_mem_assigner.h"
  17. #include <cstring>
  18. #include <set>
  19. #include "common/math/math_util.h"
  20. #include "common/util/error_manager/error_manager.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "graph/build/memory/hybrid_mem_assigner.h"
  23. #include "graph/build/memory/var_mem_assign_util.h"
  24. #include "graph/build/memory/block_mem_assigner.h"
  25. #include "graph/common/omg_util.h"
  26. #include "graph/debug/ge_attr_define.h"
  27. #include "graph/ge_attr_value.h"
  28. #include "graph/manager/graph_var_manager.h"
  29. #include "graph/utils/tensor_utils.h"
  30. #include "graph/utils/type_utils.h"
  31. namespace {
  32. const int kDataOutputIndex = 0;
  33. const int kAllInputAddrIsAtomic = -1;
  34. const int kVirtualInputNodeMemoryReuse = 0;
  35. const int kVirtualOutputNodeMemoryReuse = 1;
  36. const size_t kVirtualInputNodeOutputSize = 1;
  37. const size_t kVirtualOutputNodeInputSize = 1;
  38. const size_t kVirtualNodeDataIndex = 0;
  39. const char *const kMbatchNodeNameFlag = "_ascend_mbatch_batch_";
  40. int64_t GetSymbolOutputOffset(const std::map<std::string, std::string> &anchor_to_symbol,
  41. const std::map<std::string, std::list<ge::NodeIndexIO>> &symbol_to_anchors,
  42. const ge::NodePtr &node, const uint32_t i) {
  43. ge::NodeIndexIO cur_node_index_io(node, i, ge::kOut);
  44. auto iter1 = anchor_to_symbol.find(cur_node_index_io.ToString());
  45. if (iter1 == anchor_to_symbol.end()) {
  46. return ge::kInvalidOffset;
  47. }
  48. auto out_symbol = iter1->second;
  49. auto iter2 = symbol_to_anchors.find(out_symbol);
  50. if (iter2 == symbol_to_anchors.end()) {
  51. return ge::kInvalidOffset;
  52. }
  53. for (const auto &node_index_io : iter2->second) {
  54. if (node_index_io.value_ == out_symbol) {
  55. vector<int64_t> output_list = node->GetOpDesc()->GetOutputOffset();
  56. vector<int64_t> symbol_output_list = node_index_io.node_->GetOpDesc()->GetOutputOffset();
  57. if (node_index_io.index_ >= symbol_output_list.size()) {
  58. return ge::kInvalidOffset;
  59. }
  60. GELOGD("Node %s %uth output offset is %ld, Symbol %s output offset is %ld.", node->GetName().c_str(), i,
  61. output_list[i], iter2->first.c_str(), symbol_output_list.at(node_index_io.index_));
  62. return symbol_output_list.at(node_index_io.index_);
  63. }
  64. }
  65. return ge::kInvalidOffset;
  66. }
  67. } // namespace
  68. namespace ge {
  69. Status VariableMemoryAssigner::Assign() {
  70. Status result = ge::VarMemAssignUtil::AssignConstantOpMemory(compute_graph_);
  71. if (result != ge::SUCCESS) {
  72. return result;
  73. }
  74. result = ge::VarMemAssignUtil::AssignVarMemory(compute_graph_);
  75. if (result != ge::SUCCESS) {
  76. return result;
  77. }
  78. return ge::SUCCESS;
  79. }
  80. Status VariableMemoryAssigner::AssignVarAttr2Nodes() {
  81. Status result = ge::VarMemAssignUtil::AssignVarAttr2Nodes(compute_graph_);
  82. if (result != ge::SUCCESS) {
  83. return result;
  84. }
  85. return ge::SUCCESS;
  86. }
  87. Status GraphMemoryAssigner::AssignMemory() {
  88. ge::HybridMemAssignerPtr mem_assigner(new(std::nothrow) HybridMemAssigner(compute_graph_));
  89. if (mem_assigner->Assign() != ge::SUCCESS) {
  90. GELOGE(ge::FAILED, "Memory assigner failed");
  91. return ge::FAILED;
  92. }
  93. MemoryOffset memory_offset(RT_MEMORY_HBM, mem_assigner->GetMemOffset());
  94. memory_offset_.emplace(RT_MEMORY_HBM, memory_offset);
  95. if (mem_assigner->GetP2PMemOffset() > 0) {
  96. MemoryOffset p2p_memory_offset(RT_MEMORY_P2P_DDR, mem_assigner->GetP2PMemOffset());
  97. memory_offset_.emplace(RT_MEMORY_P2P_DDR, p2p_memory_offset);
  98. }
  99. auto session_id = compute_graph_->GetSessionID();
  100. int64_t var_size_before_assign = ge::VarManager::Instance(session_id)->GetVarMemSize(RT_MEMORY_HBM);
  101. auto variable_assigner =
  102. std::unique_ptr<ge::VariableMemoryAssigner>(new(std::nothrow) ge::VariableMemoryAssigner(compute_graph_));
  103. if (variable_assigner == nullptr) {
  104. GELOGE(ge::FAILED, "Alloc VariableMemoryAssigner failed.");
  105. return ge::FAILED;
  106. }
  107. if (variable_assigner->Assign() != ge::SUCCESS) {
  108. return ge::FAILED;
  109. }
  110. int64_t var_size_assign = ge::VarManager::Instance(session_id)->GetVarMemSize(RT_MEMORY_HBM) - var_size_before_assign;
  111. GELOGI("GraphMemoryAssigner::AssignMemory variable size = %ld", var_size_assign);
  112. mem_assigner_ = std::move(mem_assigner);
  113. return ge::SUCCESS;
  114. }
  115. ge::Status GraphMemoryAssigner::AssignVarAttr2Nodes() {
  116. auto variable_assigner =
  117. std::unique_ptr<ge::VariableMemoryAssigner>(new(std::nothrow) ge::VariableMemoryAssigner(compute_graph_));
  118. if (variable_assigner == nullptr) {
  119. GELOGE(ge::FAILED, "Alloc VariableMemoryAssigner failed.");
  120. return ge::FAILED;
  121. }
  122. if (variable_assigner->AssignVarAttr2Nodes() != ge::SUCCESS) {
  123. return ge::FAILED;
  124. }
  125. return ge::SUCCESS;
  126. }
  127. ge::Status GraphMemoryAssigner::CalculateTensorRealSizeAndOutSize(const ge::ConstGeTensorDescPtr &output_desc,
  128. int64_t dim_index, int64_t &output_mem_size,
  129. int64_t &batch_dim_num, int64_t &out_size) {
  130. graphStatus graph_status = ge::TensorUtils::GetSize(*output_desc, out_size);
  131. if (graph_status != GRAPH_SUCCESS) {
  132. GELOGE(FAILED, "Opdesc GetSize failed!");
  133. return FAILED;
  134. }
  135. GeShape output_shape = output_desc->GetShape();
  136. std::vector<int64_t> output_dims = output_shape.GetDims();
  137. if (dim_index >= static_cast<int64_t>(output_dims.size())) {
  138. GELOGE(FAILED, "Invaild value(%ld) of attr _reuse_input_on_dim_index, which is out of data range [0, %zu).",
  139. dim_index, output_dims.size());
  140. return FAILED;
  141. }
  142. for (int64_t index = 0; index < dim_index; index++) {
  143. FMK_INT64_MULCHECK(batch_dim_num, output_dims[index]);
  144. batch_dim_num *= output_dims[index];
  145. output_dims[index] = 1;
  146. }
  147. output_shape = GeShape(output_dims);
  148. Format out_format = output_desc->GetFormat();
  149. DataType data_type = output_desc->GetDataType();
  150. graph_status = ge::TensorUtils::CalcTensorMemSize(output_shape, out_format, data_type, output_mem_size);
  151. if (graph_status != GRAPH_SUCCESS) {
  152. GELOGE(graph_status, "Opdesc CalcTensorMemSize failed!");
  153. return FAILED;
  154. }
  155. if (output_mem_size < 0) {
  156. GELOGE(FAILED, "After calculating tensor memory size, output_mem_size = %ld, out of data range [0, %ld]",
  157. output_mem_size, INT64_MAX);
  158. return FAILED;
  159. }
  160. return SUCCESS;
  161. }
  162. Status GraphMemoryAssigner::GetMaxBatchLabel(const map<string, vector<NodePtr>> &mem_reuse_virtual_nodes_map,
  163. int32_t mem_reuse_model, string &max_batch_label) {
  164. for (auto &i_map : mem_reuse_virtual_nodes_map) {
  165. vector<NodePtr> virtual_nodes_list = i_map.second;
  166. vector<int64_t> max_shape_dims;
  167. size_t max_batch_dim = 0;
  168. bool max_batch_dim_find = false;
  169. for (size_t i = 0; i < virtual_nodes_list.size(); ++i) {
  170. GE_CHECK_NOTNULL(virtual_nodes_list[i]);
  171. OpDescPtr op_desc = virtual_nodes_list[i]->GetOpDesc();
  172. GE_CHECK_NOTNULL(op_desc);
  173. ge::ConstGeTensorDescPtr input_output_desc;
  174. if (mem_reuse_model == kVirtualInputNodeMemoryReuse) {
  175. input_output_desc = op_desc->GetOutputDescPtr(kVirtualNodeDataIndex);
  176. } else if (mem_reuse_model == kVirtualOutputNodeMemoryReuse) {
  177. input_output_desc = op_desc->GetInputDescPtr(kVirtualNodeDataIndex);
  178. } else {
  179. GELOGE(FAILED, "Invalid parameter memory reuse model, which is: %d.", mem_reuse_model);
  180. return FAILED;
  181. }
  182. GE_CHECK_NOTNULL(input_output_desc);
  183. if (i == 0) {
  184. // All ops must have ATTR_NAME_BATCH_LABEL, no need to check return value.
  185. (void) ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, max_batch_label);
  186. max_shape_dims = input_output_desc->GetShape().GetDims();
  187. } else {
  188. vector<int64_t> current_shape_dims = input_output_desc->GetShape().GetDims();
  189. if (current_shape_dims.size() != max_shape_dims.size()) {
  190. GELOGE(FAILED, "The shape size of several nodes between multiple batches does not match.");
  191. return FAILED;
  192. }
  193. for (size_t j = 0; j < current_shape_dims.size(); ++j) {
  194. if (current_shape_dims[j] == max_shape_dims[j]) {
  195. continue;
  196. }
  197. if (max_batch_dim_find && max_batch_dim != j) {
  198. GELOGE(FAILED, "The shape of several nodes between multiple batches does not match.");
  199. return FAILED;
  200. }
  201. max_batch_dim_find = true;
  202. max_batch_dim = j;
  203. if (current_shape_dims[j] > max_shape_dims[j]) {
  204. max_shape_dims[j] = current_shape_dims[j];
  205. // All ops must have ATTR_NAME_BATCH_LABEL, no need to check return value.
  206. (void) ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, max_batch_label);
  207. }
  208. // Only compare the first different dim in shape.
  209. break;
  210. }
  211. }
  212. }
  213. // In every element of virtual_input_nodes_map, the label of the max batch node is the same.
  214. break;
  215. }
  216. return SUCCESS;
  217. }
  218. Status GraphMemoryAssigner::ReAssignMemory(bool is_loop_graph, map<int64_t, size_t> &mem_type_to_offset) {
  219. if (memory_offset_.empty()) {
  220. GELOGE(FAILED, "memory_offset_ is empty.");
  221. return ge::FAILED;
  222. }
  223. GE_CHK_STATUS_RET(ReAssignContinuousMemory(is_loop_graph), "ReAssignContinuousMemory Failed!");
  224. GE_CHK_STATUS_RET(ReAssignReuseAndNoPaddingContinuousInputMemory(),
  225. "ReAssignReuseAndNoPaddingContinuousInputMemory Failed!");
  226. GE_CHK_STATUS_RET(ReAssignReuseAndNoPaddingContinuousOutputMemory(),
  227. "ReAssignReuseAndNoPaddingContinuousOutputMemory Failed!");
  228. GE_CHK_STATUS_RET(ReAssignAtomicMemory(is_loop_graph), "ReAssignAtomicMemory Failed!");
  229. size_t total_mem_offset = 0;
  230. for (auto pair : memory_offset_) {
  231. mem_type_to_offset[pair.first] = pair.second.mem_offset_;
  232. total_mem_offset += pair.second.mem_offset_;
  233. }
  234. auto session_id = compute_graph_->GetSessionID();
  235. if (total_mem_offset > VarManager::Instance(session_id)->GetGraphMemoryMaxSize()) {
  236. GELOGE(ge::FAILED, "Current memoffset %zu is greater than memory manager malloc max size %zu", total_mem_offset,
  237. VarManager::Instance(session_id)->GetGraphMemoryMaxSize());
  238. for (auto iter : mem_type_to_offset) {
  239. ErrorManager::GetInstance().ATCReportErrMessage("E19022", {"memType", "size", "item", "maxsize"},
  240. {std::to_string(iter.first), std::to_string(iter.second), "featuremap",
  241. std::to_string(VarManager::Instance(session_id)->GetGraphMemoryMaxSize())});
  242. }
  243. return ge::FAILED;
  244. }
  245. return SUCCESS;
  246. }
  247. Status GraphMemoryAssigner::AssignZeroCopyMemory(map<int64_t, size_t> &mem_offset, size_t &zero_mem_copy_size) {
  248. BlockMemAssignerPtr priority_assigner = std::move(mem_assigner_->GetPriorityAssinger());
  249. GE_IF_BOOL_EXEC(priority_assigner == nullptr, GELOGE(FAILED, "Get priority_assigner failed."); return ge::FAILED;);
  250. size_t mem_offset_tmp = mem_offset[RT_MEMORY_HBM];
  251. // set offset for zero copy block
  252. for (auto &memory_block : priority_assigner->GetMemoryBlocks()) {
  253. if (memory_block == nullptr || memory_block->deleted_block_ || !memory_block->is_zero_copy_) {
  254. continue;
  255. }
  256. memory_block->Resize();
  257. memory_block->SetHeadOffset(mem_offset[RT_MEMORY_HBM]);
  258. mem_offset[RT_MEMORY_HBM] += memory_block->Size();
  259. memory_block->SetTailOffset(mem_offset[RT_MEMORY_HBM] - 1);
  260. }
  261. GELOGI("mem_offset_ include zero_copy_memory is %zu.", mem_offset[RT_MEMORY_HBM]);
  262. // set offset for zero copy nodes
  263. priority_assigner->SetOpMemOffset(true);
  264. zero_mem_copy_size = mem_offset[RT_MEMORY_HBM] - mem_offset_tmp;
  265. auto iter = memory_offset_.find(RT_MEMORY_HBM);
  266. if (iter == memory_offset_.end()) {
  267. GELOGE(FAILED, "Memory offset don't have memory type[hbm].");
  268. return FAILED;
  269. }
  270. iter->second.mem_offset_ = mem_offset[RT_MEMORY_HBM];
  271. GELOGI("max_mem_offset:%zu, mem_offset:%zu, zero_mem_copy_size:%zu.", mem_offset[RT_MEMORY_HBM], mem_offset_tmp,
  272. zero_mem_copy_size);
  273. return SUCCESS;
  274. }
  275. Status GraphMemoryAssigner::ReAssignContinuousMemory(bool is_loop_graph) {
  276. GELOGI("Begin to reassign continuous memory");
  277. Status ret;
  278. for (auto &node : compute_graph_->GetAllNodes()) {
  279. // Get the continuous input type of the node, default is false
  280. bool is_input_continuous = false;
  281. GE_CHECK_NOTNULL(node->GetOpDesc());
  282. // If GetBool fail, is_input_continuous is false.
  283. (void) ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_CONTINUOUS_INPUT, is_input_continuous);
  284. // Assign continuous input memory
  285. if (is_input_continuous) {
  286. int64_t memory_type = RT_MEMORY_HBM;
  287. GE_CHK_STATUS_RET(GetNodeMemoryType(node, memory_type, "input"), "Get node memory type failed.");
  288. int64_t mem_clean_start = 0;
  289. int64_t mem_clean_size = 0;
  290. ret = AssignContinuousInputMemory(node, mem_clean_start, mem_clean_size, memory_type);
  291. if (ret != ge::SUCCESS) {
  292. GELOGE(ret, "Assign continuous input memory failed!");
  293. return ret;
  294. }
  295. // Clean up atomic address, eg, hcom node
  296. vector<int32_t> input_indexes;
  297. // If GetListInt fail, input_indexes is empty.
  298. (void) ge::AttrUtils::GetListInt(node->GetOpDesc(), ATOMIC_ATTR_INPUT_INDEX, input_indexes);
  299. if (!input_indexes.empty() && input_indexes[0] == kAllInputAddrIsAtomic) {
  300. // check whether there is an atomic conflict between the current node and the peer out node
  301. if (!CheckInputIsSupportAtomic(node)) {
  302. GELOGE(ge::FAILED,
  303. "There is an atomic conflict between the current node and the peer out node, not supported!");
  304. return ge::FAILED;
  305. }
  306. const auto &in_control_anchor = node->GetInControlAnchor();
  307. GE_CHECK_NOTNULL(in_control_anchor);
  308. for (const auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  309. auto peer_out_node = peer_out_control_anchor->GetOwnerNode();
  310. if (peer_out_node->GetType() == ATOMICADDRCLEAN) {
  311. ret = SetAtomicCleanAttr(peer_out_node, {mem_clean_start}, {mem_clean_size});
  312. if (ret != SUCCESS) {
  313. GELOGE(ret, "Failed to set attr for atomic addr clean node %s.", peer_out_node->GetName().c_str());
  314. return ret;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. // Get the reference type of the node, default is false
  321. bool is_ref = false;
  322. // If GetBool fail, is_ref is false.
  323. (void) ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_REFERENCE, is_ref);
  324. // Get the continuous output type of the node, default is false
  325. bool is_output_continuous = false;
  326. // If GetBool fail, is_output_continuous is false.
  327. (void) ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_CONTINUOUS_OUTPUT, is_output_continuous);
  328. // If the output is ref type and refers to the ref of an input, the name of the output
  329. // and the input are the same. Ge encounters ref type, finds matching relationship according
  330. // to the names of input and output, and allocates the same memory address, eg: HCOMBroadcast
  331. if (!is_ref && is_output_continuous) { // Assign continuous output memory
  332. ret = AssignContinuousOutputMemory(node);
  333. if (ret != ge::SUCCESS) {
  334. GELOGE(ret, "Assign reference memory failed!");
  335. return ret;
  336. }
  337. }
  338. }
  339. for (auto pair : memory_offset_) {
  340. GELOGI("After reassign continuous memory, memory type = %ld, memoffset = %zu.", pair.first,
  341. pair.second.mem_offset_);
  342. }
  343. return ge::SUCCESS;
  344. }
  345. Status GraphMemoryAssigner::AssignContinuousInputMemory(const ge::NodePtr &node, int64_t &continuous_mem_start,
  346. int64_t &continuous_mem_size, int64_t memory_type) {
  347. GELOGI("Current node %s needs continuous input.", node->GetName().c_str());
  348. bool continuous_input_alloc = false;
  349. (void) ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_CONTINUOUS_INPUT_ALLOC, continuous_input_alloc);
  350. auto iter = memory_offset_.find(memory_type);
  351. if (iter == memory_offset_.end()) {
  352. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", memory_type);
  353. return FAILED;
  354. }
  355. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  356. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  357. GE_IF_BOOL_EXEC(peer_out_data_anchor == nullptr, continue);
  358. auto peer_op_desc = peer_out_data_anchor->GetOwnerNode()->GetOpDesc();
  359. GE_IF_BOOL_EXEC(peer_op_desc == nullptr, continue);
  360. bool is_peer_output_continuous = false;
  361. // If GetBool fail, is_peer_output_continuous is false.
  362. (void) ge::AttrUtils::GetBool(peer_op_desc, ATTR_NAME_CONTINUOUS_OUTPUT, is_peer_output_continuous);
  363. // Get peer node output size, if size == 1(peer node has only one output), continuous input of the node and
  364. // continuous output of the previous node is the same, we can support it. If size != 1, there may be
  365. // conflict between the two, we can not support it.
  366. auto peer_output_size = peer_op_desc->GetOutputsSize();
  367. GE_IF_BOOL_EXEC(is_peer_output_continuous && (peer_output_size != 1),
  368. GELOGE(PARAM_INVALID,
  369. "Current node %s requires continuous input, while the previous node %s requires "
  370. "continuous output. There may be conflict between the two. This node is not supported now.",
  371. node->GetOpDesc()->GetName().c_str(), peer_op_desc->GetName().c_str());
  372. return PARAM_INVALID;);
  373. bool is_peer_reference = false;
  374. // If GetBool fail, is_peer_reference is false.
  375. (void) AttrUtils::GetBool(peer_op_desc, ATTR_NAME_REFERENCE, is_peer_reference);
  376. GE_IF_BOOL_EXEC(is_peer_reference,
  377. GELOGE(PARAM_INVALID,
  378. "Current node %s requires continuous input, while the previous node %s requires "
  379. "reference. There may be conflict between the two. This node is not supported now.",
  380. node->GetOpDesc()->GetName().c_str(), peer_op_desc->GetName().c_str());
  381. return PARAM_INVALID;);
  382. vector<int64_t> output_list = peer_op_desc->GetOutputOffset();
  383. std::vector<int64_t> offsets_for_fusion = {};
  384. bool has_offset_attr =
  385. AttrUtils::GetListInt(peer_op_desc, ATTR_NAME_OUTPUT_OFFSET_FOR_BUFFER_FUSION, offsets_for_fusion);
  386. if (peer_out_data_anchor->GetIdx() < static_cast<int>(output_list.size())) {
  387. if (continuous_input_alloc && !has_offset_attr) {
  388. if (in_data_anchor->GetIdx() == 0) {
  389. continuous_mem_start = output_list.at(peer_out_data_anchor->GetIdx());
  390. }
  391. // can not use else if, incase only one input
  392. if (in_data_anchor->GetIdx() == static_cast<int>(node->GetAllInDataAnchors().size()) - 1) {
  393. int64_t tensor_desc_size = 0;
  394. Status ret = ge::TensorUtils::GetSize(*(peer_op_desc->GetOutputDescPtr(peer_out_data_anchor->GetIdx())),
  395. tensor_desc_size);
  396. GE_IF_BOOL_EXEC(ret != ge::SUCCESS, GELOGE(FAILED, "GetSize failed."); return FAILED;);
  397. tensor_desc_size = (tensor_desc_size + MEM_ALIGN_SIZE - 1) / MEM_ALIGN_SIZE * MEM_ALIGN_SIZE;
  398. continuous_mem_size =
  399. output_list.at(peer_out_data_anchor->GetIdx()) - continuous_mem_start + tensor_desc_size + MEM_ALIGN_SIZE;
  400. }
  401. GELOGI(
  402. "[IMAS]Check Continuous input : Set %s name[%s] output[%d] offset to [%zu] stream_id[%ld] size[%zu] "
  403. "real_size[%u].",
  404. node->GetOwnerComputeGraph()->GetName().c_str(), peer_op_desc->GetName().c_str(),
  405. peer_out_data_anchor->GetIdx(), output_list.at(peer_out_data_anchor->GetIdx()), peer_op_desc->GetStreamId(),
  406. 0, 0);
  407. continue;
  408. }
  409. output_list.at(peer_out_data_anchor->GetIdx()) = iter->second.mem_offset_;
  410. } else {
  411. GELOGE(FAILED, "index : %d is out of range.", peer_out_data_anchor->GetIdx());
  412. return FAILED;
  413. }
  414. peer_op_desc->SetOutputOffset(output_list);
  415. size_t pre_mem_offset = iter->second.mem_offset_;
  416. int64_t tensor_desc_size = 0;
  417. if (has_offset_attr) {
  418. if (peer_out_data_anchor->GetIdx() < static_cast<int>(offsets_for_fusion.size())) {
  419. auto offset_for_fusion = offsets_for_fusion[peer_out_data_anchor->GetIdx()];
  420. iter->second.mem_offset_ += offset_for_fusion;
  421. } else {
  422. GELOGE(FAILED, "fusion: peer node %s index : %d is out of range.", peer_op_desc->GetName().c_str(),
  423. peer_out_data_anchor->GetIdx());
  424. return FAILED;
  425. }
  426. } else {
  427. Status ret =
  428. TensorUtils::GetSize(*(peer_op_desc->GetOutputDescPtr(peer_out_data_anchor->GetIdx())), tensor_desc_size);
  429. GE_IF_BOOL_EXEC(ret != ge::SUCCESS, GELOGE(FAILED, "GetSize failed."); return FAILED;);
  430. iter->second.mem_offset_ += tensor_desc_size;
  431. }
  432. // If set tensor_actual_size, Memory alignment is not required.
  433. int32_t is_tensor_actual_size = 0;
  434. ge::AttrUtils::GetInt(peer_op_desc, ATTR_NAME_GET_TENSOR_ACTUAL_SIZE, is_tensor_actual_size);
  435. if (is_tensor_actual_size == 0) {
  436. AlignMemOffset(MEM_ALIGN_SIZE, memory_type);
  437. }
  438. GELOGI(
  439. "[IMAS]Continuous input : Set %s name[%s] output[%d] offset to [%zu] stream_id[%ld] size[%zu] "
  440. "real_size[%ld].", node->GetOwnerComputeGraph()->GetName().c_str(), peer_op_desc->GetName().c_str(),
  441. peer_out_data_anchor->GetIdx(), pre_mem_offset, peer_op_desc->GetStreamId(),
  442. (iter->second.mem_offset_ - pre_mem_offset), tensor_desc_size);
  443. }
  444. iter->second.mem_offset_ += MEM_ALIGN_SIZE;
  445. if (!continuous_input_alloc) {
  446. continuous_mem_size = iter->second.mem_offset_ - continuous_mem_start;
  447. }
  448. return SUCCESS;
  449. }
  450. Status GraphMemoryAssigner::AssignContinuousOutputMemory(const ge::NodePtr &node) {
  451. GELOGI("Current node %s needs continuous output.", node->GetName().c_str());
  452. auto out_op_desc = node->GetOpDesc();
  453. GE_IF_BOOL_EXEC(out_op_desc == nullptr, GELOGE(ge::FAILED, "out_op_desc is null."); return ge::FAILED);
  454. vector<int64_t> output_list = out_op_desc->GetOutputOffset();
  455. if ((out_op_desc->GetOutputsSize() > output_list.size()) || (output_list.size() == 0)) {
  456. GELOGE(ge::FAILED, "The size %zu of node output desc is more than output_list's size %zu.",
  457. out_op_desc->GetOutputsSize(), output_list.size());
  458. return ge::FAILED;
  459. }
  460. size_t mem_offset = output_list[0];
  461. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  462. output_list[out_data_anchor->GetIdx()] = mem_offset;
  463. int64_t tensor_desc_size = 0;
  464. if (ge::TensorUtils::GetSize(*(out_op_desc->GetOutputDescPtr(out_data_anchor->GetIdx())), tensor_desc_size) !=
  465. ge::SUCCESS) {
  466. GELOGE(FAILED, "GetSize failed.");
  467. return FAILED;
  468. }
  469. mem_offset += tensor_desc_size;
  470. if (mem_offset <= 0) {
  471. return FAILED;
  472. }
  473. mem_offset = (mem_offset + MEM_ALIGN_SIZE - 1) / MEM_ALIGN_SIZE * MEM_ALIGN_SIZE;
  474. GELOGI(
  475. "[IMAS]Continuous output : Set %s name[%s] output[%d] offset to [%zu] stream_id[%ld] size[%ld] "
  476. "real_size[%ld].",
  477. node->GetOwnerComputeGraph()->GetName().c_str(), out_op_desc->GetName().c_str(), out_data_anchor->GetIdx(),
  478. output_list[out_data_anchor->GetIdx()], out_op_desc->GetStreamId(), tensor_desc_size, tensor_desc_size);
  479. }
  480. out_op_desc->SetOutputOffset(output_list);
  481. return ge::SUCCESS;
  482. }
  483. Status GraphMemoryAssigner::ReAssignVirtualInputNodeMemory(NodePtr node, size_t &mem_offset_reuse) {
  484. OpDescPtr op_desc = node->GetOpDesc();
  485. vector<int64_t> output_list = op_desc->GetOutputOffset();
  486. if (output_list.empty()) {
  487. GELOGE(FAILED, "Outputoffset is empty node name:%s", node->GetName().c_str());
  488. return FAILED;
  489. }
  490. output_list.at(0) = mem_offset_reuse;
  491. op_desc->SetOutputOffset(output_list);
  492. GELOGI("Set virtual input node %s output offset to %zu.", op_desc->GetName().c_str(), mem_offset_reuse);
  493. int64_t attr_dim_index;
  494. bool get_attr_dim_flag = ge::AttrUtils::GetInt(op_desc, ATTR_NAME_REUSE_INPUT_ON_DIM_INDEX, attr_dim_index);
  495. if (!get_attr_dim_flag) {
  496. GELOGE(FAILED, "Get attr _reuse_input_on_dim_index failed.");
  497. return FAILED;
  498. }
  499. size_t extra_memory_size = 0;
  500. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  501. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  502. GE_CHECK_NOTNULL(peer_out_data_anchor);
  503. auto peer_op_desc = peer_out_data_anchor->GetOwnerNode()->GetOpDesc();
  504. GE_CHECK_NOTNULL(peer_op_desc);
  505. vector<int64_t> output_offsets = peer_op_desc->GetOutputOffset();
  506. if (peer_out_data_anchor->GetIdx() >= static_cast<int>(output_offsets.size())) {
  507. GELOGE(ge::FAILED, "Index : %d is out of range.", peer_out_data_anchor->GetIdx());
  508. return ge::FAILED;
  509. }
  510. output_offsets.at(peer_out_data_anchor->GetIdx()) = mem_offset_reuse;
  511. peer_op_desc->SetOutputOffset(output_offsets);
  512. size_t pre_mem_offset = mem_offset_reuse;
  513. // Calculate tensor real size of each piece of data and out size of complete data
  514. ge::ConstGeTensorDescPtr output_desc = peer_op_desc->GetOutputDescPtr(peer_out_data_anchor->GetIdx());
  515. GE_CHECK_NOTNULL(output_desc);
  516. int64_t output_mem_size;
  517. int64_t batch_dim_num = 1;
  518. int64_t out_size;
  519. if (CalculateTensorRealSizeAndOutSize(output_desc, attr_dim_index, output_mem_size, batch_dim_num, out_size) !=
  520. SUCCESS) {
  521. GELOGE(FAILED, "CalculateTensorRealSizeAndOutSize failed for node %s output [%d].",
  522. peer_op_desc->GetName().c_str(), peer_out_data_anchor->GetIdx());
  523. return FAILED;
  524. }
  525. mem_offset_reuse += output_mem_size;
  526. extra_memory_size = extra_memory_size + out_size - output_mem_size;
  527. GELOGI("[IMAS]Virtual node optimize: set %s name[%s] output[%d] offset to [%zu] stream_id[%ld] size[%ld] "
  528. "real_size[%ld].",
  529. node->GetOwnerComputeGraph()->GetName().c_str(), peer_op_desc->GetName().c_str(),
  530. peer_out_data_anchor->GetIdx(), pre_mem_offset, peer_op_desc->GetStreamId(), out_size,
  531. output_mem_size);
  532. }
  533. mem_offset_reuse += extra_memory_size;
  534. size_t after_mem_offset = mem_offset_reuse;
  535. GELOGI("After reassign virtual input node[name: %s, type: %s] memory, memory offset = %zu.",
  536. op_desc->GetName().c_str(), op_desc->GetType().c_str(), after_mem_offset);
  537. return SUCCESS;
  538. }
  539. Status GraphMemoryAssigner::ReAssignReuseAndNoPaddingContinuousInputMemory() {
  540. map<string, vector<NodePtr>> mem_reuse_virtual_input_nodes_map;
  541. int64_t memory_type = RT_MEMORY_HBM;
  542. for (const auto &n : compute_graph_->GetAllNodes()) {
  543. OpDescPtr op_desc = n->GetOpDesc();
  544. GE_CHECK_NOTNULL(op_desc);
  545. bool attr_continuous = false;
  546. bool get_continuous_flag = ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_INPUT, attr_continuous);
  547. GE_IF_BOOL_EXEC(!get_continuous_flag, continue);
  548. bool attr_reuse = false;
  549. bool get_reuse_flag = ge::AttrUtils::GetBool(op_desc, ATTR_NAME_OUTPUT_REUSE_INPUT, attr_reuse);
  550. GE_IF_BOOL_EXEC(!get_reuse_flag, continue);
  551. if (attr_reuse && attr_continuous) {
  552. if (op_desc->GetOutputsSize() != kVirtualInputNodeOutputSize) {
  553. // When current virtual node has several outputs, can't directly determine which input is the tensor for reuse.
  554. GELOGE(FAILED, "Only one output is supported, current virtual node %s has %zu inputs.", n->GetName().c_str(),
  555. op_desc->GetOutputsSize());
  556. return FAILED;
  557. }
  558. GE_CHK_STATUS_RET(GetNodeMemoryType(n, memory_type, "input"), "Get node memory type failed.");
  559. auto iter = memory_offset_.find(memory_type);
  560. if (iter == memory_offset_.end()) {
  561. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", memory_type);
  562. return FAILED;
  563. }
  564. GELOGD("Start to reassign memory for virtual input node, memory offset = %zu, memory type = %ld.",
  565. iter->second.mem_offset_, memory_type);
  566. string batch_label_string;
  567. // Not all ops have ATTR_NAME_BATCH_LABEL, no need to check return value, only check out parameter
  568. (void) ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, batch_label_string);
  569. if (batch_label_string.empty()) {
  570. size_t node_mem_offset = iter->second.mem_offset_;
  571. // No ATTR_NAME_BATCH_LABEL, no need to reuse memory.
  572. Status status = ReAssignVirtualInputNodeMemory(n, node_mem_offset);
  573. if (status != SUCCESS) {
  574. GELOGE(FAILED, "Reassign memory of virtual input node failed, node name: %s.", n->GetName().c_str());
  575. return FAILED;
  576. }
  577. iter->second.mem_offset_ = node_mem_offset;
  578. AlignMemOffset(MEM_ALIGN_SIZE, memory_type);
  579. GELOGD("After reassign memory for virtual input node, align memory = %zu, memory type = %ld.",
  580. iter->second.mem_offset_, memory_type);
  581. } else {
  582. // Has ATTR_NAME_BATCH_LABEL, for dynamic multi-batch node, need to reuse memory.
  583. string current_node_full_name = op_desc->GetName();
  584. size_t pos = current_node_full_name.find(kMbatchNodeNameFlag);
  585. if (pos == string::npos) {
  586. GELOGE(FAILED, "Cannot find key string [%s] of multi-batch in name of virtual input node, node name: %s.",
  587. kMbatchNodeNameFlag, n->GetName().c_str());
  588. return FAILED;
  589. }
  590. string fixed_name = current_node_full_name.substr(0, pos);
  591. vector<NodePtr> parallel_virtual_input_nodes;
  592. if (mem_reuse_virtual_input_nodes_map.count(fixed_name) != 0) {
  593. parallel_virtual_input_nodes = mem_reuse_virtual_input_nodes_map[fixed_name];
  594. }
  595. parallel_virtual_input_nodes.emplace_back(n);
  596. mem_reuse_virtual_input_nodes_map[fixed_name] = parallel_virtual_input_nodes;
  597. }
  598. }
  599. }
  600. int32_t mem_reuse_model = 0;
  601. if (ReAssignVirtualNodesMemory(mem_reuse_virtual_input_nodes_map, mem_reuse_model) != SUCCESS) {
  602. GELOGE(FAILED, "Reassign memory of virtual input nodes failed.");
  603. return FAILED;
  604. }
  605. return SUCCESS;
  606. }
  607. Status GraphMemoryAssigner::ReAssignVirtualOutputNodeMemory(NodePtr node, size_t &mem_offset_reuse) {
  608. OpDescPtr op_desc = node->GetOpDesc();
  609. // 1. set memory of to be reused input tensor
  610. auto in_data_anchor_list = node->GetAllInDataAnchors();
  611. auto peer_out_data_anchor = in_data_anchor_list.at(0)->GetPeerOutAnchor();
  612. GE_CHECK_NOTNULL(peer_out_data_anchor);
  613. auto peer_op_desc = peer_out_data_anchor->GetOwnerNode()->GetOpDesc();
  614. GE_CHECK_NOTNULL(peer_op_desc);
  615. vector<int64_t> in_node_output_offsets = peer_op_desc->GetOutputOffset();
  616. if (peer_out_data_anchor->GetIdx() >= static_cast<int>(in_node_output_offsets.size())) {
  617. GELOGE(FAILED, "Index : %d is out of range.", peer_out_data_anchor->GetIdx());
  618. return FAILED;
  619. }
  620. in_node_output_offsets.at(peer_out_data_anchor->GetIdx()) = mem_offset_reuse;
  621. peer_op_desc->SetOutputOffset(in_node_output_offsets);
  622. GELOGI("Set virtual output node %s input data offset to %zu.", op_desc->GetName().c_str(), mem_offset_reuse);
  623. // 2. set memory of output tensor
  624. vector<int64_t> output_list = op_desc->GetOutputOffset();
  625. if (output_list.empty()) {
  626. GELOGE(FAILED, "Outputoffset is empty, node name: %s", node->GetName().c_str());
  627. return FAILED;
  628. }
  629. if (op_desc->GetOutputsSize() > output_list.size()) {
  630. GELOGE(FAILED, "The size %zu of op_desc is more than output_list's size %zu.", op_desc->GetOutputsSize(),
  631. output_list.size());
  632. return FAILED;
  633. }
  634. int64_t attr_dim_index;
  635. bool get_attr_dim_flag = ge::AttrUtils::GetInt(op_desc, ATTR_NAME_REUSE_INPUT_ON_DIM_INDEX, attr_dim_index);
  636. if (!get_attr_dim_flag) {
  637. GELOGE(FAILED, "Get attr _reuse_input_on_dim_index failed.");
  638. return FAILED;
  639. }
  640. size_t extra_memory_size = 0;
  641. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  642. output_list[out_data_anchor->GetIdx()] = mem_offset_reuse;
  643. size_t pre_mem_offset = mem_offset_reuse;
  644. // calculate tensor real size of each piece of data and out size of complete data
  645. ge::ConstGeTensorDescPtr output_desc = op_desc->GetOutputDescPtr(out_data_anchor->GetIdx());
  646. GE_CHECK_NOTNULL(output_desc);
  647. int64_t output_mem_size;
  648. int64_t batch_dim_num = 1;
  649. int64_t out_size;
  650. if (CalculateTensorRealSizeAndOutSize(output_desc, attr_dim_index, output_mem_size, batch_dim_num, out_size) !=
  651. SUCCESS) {
  652. GELOGE(FAILED, "CalculateTensorRealSizeAndOutSize failed for node %s output [%d].",
  653. op_desc->GetName().c_str(), out_data_anchor->GetIdx());
  654. return FAILED;
  655. }
  656. mem_offset_reuse += output_mem_size;
  657. extra_memory_size = extra_memory_size + out_size - output_mem_size;
  658. GELOGI("[IMAS]Virtual node optimize: set %s name[%s] output[%d] offset to [%zu], size[%ld], real_size[%ld].",
  659. node->GetOwnerComputeGraph()->GetName().c_str(), op_desc->GetName().c_str(), out_data_anchor->GetIdx(),
  660. pre_mem_offset, out_size, output_mem_size);
  661. }
  662. op_desc->SetOutputOffset(output_list);
  663. mem_offset_reuse += extra_memory_size;
  664. size_t after_mem_offset = mem_offset_reuse;
  665. GELOGI("After reassign virtual output node[name: %s, type: %s] memory, memory offset = %zu.",
  666. op_desc->GetName().c_str(), op_desc->GetType().c_str(), after_mem_offset);
  667. return SUCCESS;
  668. }
  669. Status GraphMemoryAssigner::ReAssignReuseAndNoPaddingContinuousOutputMemory() {
  670. map<string, vector<NodePtr>> mem_reuse_virtual_output_nodes_map;
  671. int64_t memory_type = RT_MEMORY_HBM;
  672. for (const auto &n : compute_graph_->GetAllNodes()) {
  673. OpDescPtr op_desc = n->GetOpDesc();
  674. GE_CHECK_NOTNULL(op_desc);
  675. bool attr_continuous = false;
  676. bool get_continuous_flag = ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_OUTPUT, attr_continuous);
  677. GE_IF_BOOL_EXEC(!get_continuous_flag, continue);
  678. bool attr_reuse = false;
  679. bool get_reuse_flag = ge::AttrUtils::GetBool(op_desc, ATTR_NAME_OUTPUT_REUSE_INPUT, attr_reuse);
  680. GE_IF_BOOL_EXEC(!get_reuse_flag, continue);
  681. if (attr_reuse && attr_continuous) {
  682. auto in_data_anchor_list = n->GetAllInDataAnchors();
  683. if (in_data_anchor_list.size() != kVirtualOutputNodeInputSize) {
  684. // When current virtual node has several inputs, can't directly determine which input is the tensor for reuse.
  685. GELOGE(FAILED, "Only one input is supported, current virtual node %s has %zu inputs.", n->GetName().c_str(),
  686. in_data_anchor_list.size());
  687. return FAILED;
  688. }
  689. GE_CHK_STATUS_RET(GetNodeMemoryType(n, memory_type, "output"), "Get node memory type failed.");
  690. auto iter = memory_offset_.find(memory_type);
  691. if (iter == memory_offset_.end()) {
  692. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", memory_type);
  693. return FAILED;
  694. }
  695. GELOGD("Start to reassign memory for virtual output node, memory offset = %zu, memory type = %ld.",
  696. iter->second.mem_offset_, memory_type);
  697. string batch_label_string;
  698. // Not all ops have ATTR_NAME_BATCH_LABEL, no need to check return value, only check out parameter
  699. (void) ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, batch_label_string);
  700. if (batch_label_string.empty()) {
  701. size_t node_mem_offset = iter->second.mem_offset_;
  702. // No ATTR_NAME_BATCH_LABEL, no need to reuse memory.
  703. Status status = ReAssignVirtualOutputNodeMemory(n, node_mem_offset);
  704. if (status != SUCCESS) {
  705. GELOGE(FAILED, "Reassign memory of virtual output node failed, node name: %s.", n->GetName().c_str());
  706. return FAILED;
  707. }
  708. iter->second.mem_offset_ = node_mem_offset;
  709. AlignMemOffset(MEM_ALIGN_SIZE, memory_type);
  710. GELOGD("After reassign memory for virtual output node, align memory = %zu, memory type = %ld.",
  711. iter->second.mem_offset_, memory_type);
  712. } else {
  713. // Has ATTR_NAME_BATCH_LABEL, for dynamic multi-batch node, need to reuse memory.
  714. string current_node_full_name = op_desc->GetName();
  715. size_t pos = current_node_full_name.find(kMbatchNodeNameFlag);
  716. if (pos == string::npos) {
  717. GELOGE(FAILED, "Cannot find key string [%s] of multi-batch in name of virtual output node, node name: %s.",
  718. kMbatchNodeNameFlag, n->GetName().c_str());
  719. return FAILED;
  720. }
  721. string fixed_name = current_node_full_name.substr(0, pos);
  722. vector<NodePtr> parallel_virtual_output_nodes;
  723. if (mem_reuse_virtual_output_nodes_map.count(fixed_name) != 0) {
  724. parallel_virtual_output_nodes = mem_reuse_virtual_output_nodes_map[fixed_name];
  725. }
  726. parallel_virtual_output_nodes.emplace_back(n);
  727. mem_reuse_virtual_output_nodes_map[fixed_name] = parallel_virtual_output_nodes;
  728. }
  729. }
  730. }
  731. int32_t mem_reuse_model = 1;
  732. if (ReAssignVirtualNodesMemory(mem_reuse_virtual_output_nodes_map, mem_reuse_model) != SUCCESS) {
  733. GELOGE(FAILED, "Reassign memory of virtual output nodes failed.");
  734. return FAILED;
  735. }
  736. return SUCCESS;
  737. }
  738. Status GraphMemoryAssigner::ReAssignVirtualNodesMemory(map<string, vector<NodePtr>> &mem_reuse_nodes_map,
  739. int32_t mem_reuse_model) {
  740. // Find max batch label value
  741. string max_batch_label;
  742. GE_CHK_STATUS_RET(GetMaxBatchLabel(mem_reuse_nodes_map, mem_reuse_model, max_batch_label),
  743. "Get max batch label failed.");
  744. GELOGI("The batch label of max batch virtual nodes is %s.", max_batch_label.c_str());
  745. PrintMemoryOffset();
  746. vector<size_t> nodes_mem_offset_list;
  747. for (auto &i_map : mem_reuse_nodes_map) {
  748. vector<NodePtr> virtual_nodes_list = i_map.second;
  749. int64_t memory_type = RT_MEMORY_HBM;
  750. GE_CHK_STATUS_RET(GetNodeListMemoryType(virtual_nodes_list, mem_reuse_model, memory_type),
  751. "Get node list memory type failed.");
  752. auto iter = memory_offset_.find(memory_type);
  753. if (iter == memory_offset_.end()) {
  754. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", memory_type);
  755. return FAILED;
  756. }
  757. size_t max_batch_node_mem_offset = iter->second.mem_offset_;
  758. nodes_mem_offset_list.emplace_back(max_batch_node_mem_offset);
  759. for (auto &i_node : virtual_nodes_list) {
  760. // Op_desc is not nullptr, it has been checked.
  761. OpDescPtr op_desc = i_node->GetOpDesc();
  762. string batch_label_string;
  763. // All ops must have ATTR_NAME_BATCH_LABEL, no need to check return value.
  764. (void) ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, batch_label_string);
  765. if (batch_label_string == max_batch_label) {
  766. Status status = SUCCESS;
  767. if (mem_reuse_model == kVirtualInputNodeMemoryReuse) {
  768. status = ReAssignVirtualInputNodeMemory(i_node, max_batch_node_mem_offset);
  769. } else if (mem_reuse_model == kVirtualOutputNodeMemoryReuse) {
  770. status = ReAssignVirtualOutputNodeMemory(i_node, max_batch_node_mem_offset);
  771. } else {
  772. GELOGE(FAILED, "Invalid parameter memory reuse model, which is: %d.", mem_reuse_model);
  773. return FAILED;
  774. }
  775. if (status != SUCCESS) {
  776. GELOGE(FAILED, "Reassign memory of virtual node failed, node name: %s.", i_node->GetName().c_str());
  777. return FAILED;
  778. }
  779. iter->second.mem_offset_ = max_batch_node_mem_offset;
  780. AlignMemOffset(MEM_ALIGN_SIZE, memory_type);
  781. GELOGD("After reassign memory for virtual node, align memory = %zu, memory type = %ld.",
  782. iter->second.mem_offset_, memory_type);
  783. // Only assign memory of max batch nodes.
  784. break;
  785. }
  786. }
  787. }
  788. PrintMemoryOffset();
  789. size_t memory_reuse_index = 0;
  790. for (auto &i_map : mem_reuse_nodes_map) {
  791. vector<NodePtr> virtual_nodes_list = i_map.second;
  792. for (auto &i_node : virtual_nodes_list) {
  793. size_t remaining_batch_node_mem_offset = nodes_mem_offset_list[memory_reuse_index];
  794. Status status = SUCCESS;
  795. if (mem_reuse_model == kVirtualInputNodeMemoryReuse) {
  796. status = ReAssignVirtualInputNodeMemory(i_node, remaining_batch_node_mem_offset);
  797. } else if (mem_reuse_model == kVirtualOutputNodeMemoryReuse) {
  798. status = ReAssignVirtualOutputNodeMemory(i_node, remaining_batch_node_mem_offset);
  799. } else {
  800. GELOGE(FAILED, "Invalid parameter memory reuse model, which is: %d.", mem_reuse_model);
  801. return FAILED;
  802. }
  803. if (status != SUCCESS) {
  804. GELOGE(FAILED, "Reassign memory of virtual node failed, node name: %s.", i_node->GetName().c_str());
  805. return FAILED;
  806. }
  807. }
  808. memory_reuse_index++;
  809. }
  810. return SUCCESS;
  811. }
  812. Status GraphMemoryAssigner::ReAssignAtomicMemory(bool is_loop_graph) {
  813. map<NodePtr, vector<NodePtr>> normal_atomic_and_clean_nodes_map;
  814. vector<NodePtr> connecting_output_atomic_nodes;
  815. Status status = FilterAtomicNodesForMemoryAssign(normal_atomic_and_clean_nodes_map, connecting_output_atomic_nodes);
  816. if (status != SUCCESS) {
  817. GELOGE(status, "Failed to filter atomic nodes for memory assignment.");
  818. return status;
  819. }
  820. auto mem_iter = memory_offset_.find(RT_MEMORY_HBM);
  821. if (mem_iter == memory_offset_.end()) {
  822. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", RT_MEMORY_HBM);
  823. return FAILED;
  824. }
  825. for (auto &iter : normal_atomic_and_clean_nodes_map) {
  826. int64_t atomic_mem_start = static_cast<int64_t>(mem_iter->second.mem_offset_);
  827. GELOGD("Begin to reAssign atomic memory, atomic address memory start = %ld", atomic_mem_start);
  828. vector<int64_t> mem_offset_end;
  829. status = AssignAtomicOutputAndWorkspaceMemory(atomic_node, mem_offset_end);
  830. if (status != SUCCESS) {
  831. GELOGE(status, "Assign atomic output and workspace memory failed, node name is %s.",
  832. atomic_node->GetName().c_str());
  833. return status;
  834. }
  835. }
  836. int64_t atomic_mem_size = static_cast<int64_t>(mem_iter->second.mem_offset_) - atomic_mem_start;
  837. if (atomic_mem_size != 0) {
  838. GE_CHK_STATUS_RET(SetAtomicCleanAttr(iter.first, {atomic_mem_start}, {atomic_mem_size}),
  839. "Failed to set attr for atomic addr clean node %s.", iter.first->GetName().c_str());
  840. }
  841. }
  842. if (AssignConnectNetOutputAtomicMemory(connecting_output_atomic_nodes) != SUCCESS) {
  843. GELOGE(FAILED, "Failed to assign memory of nodes that connect to netoutput.");
  844. return FAILED;
  845. }
  846. return SUCCESS;
  847. }
  848. Status GraphMemoryAssigner::FilterAtomicNodesForMemoryAssign(map<NodePtr, vector<NodePtr>> &normal_atomic_nodes_map,
  849. vector<NodePtr> &connecting_output_atomic_nodes) {
  850. GE_CHECK_NOTNULL(compute_graph_);
  851. for (const auto &node : compute_graph_->GetAllNodes()) {
  852. if (node->GetType() == ATOMICADDRCLEAN) {
  853. vector<NodePtr> tmp_normal_atomic_nodes;
  854. const auto &out_control_anchor = node->GetOutControlAnchor();
  855. GE_CHECK_NOTNULL(out_control_anchor);
  856. for (const auto &peer_in_control_anchor : out_control_anchor->GetPeerInControlAnchors()) {
  857. if (peer_in_control_anchor != nullptr) {
  858. auto peer_in_node = peer_in_control_anchor->GetOwnerNode();
  859. auto peer_in_node_desc = peer_in_node->GetOpDesc();
  860. if (peer_in_node_desc != nullptr) {
  861. bool is_atomic_node = false;
  862. // If GetBool fail, is_atomic_node is false.
  863. (void) ge::AttrUtils::GetBool(peer_in_node_desc, ATOMIC_ATTR_IS_ATOMIC_NODE, is_atomic_node);
  864. if (is_atomic_node) {
  865. bool is_reference = false;
  866. // If GetBool fail, is_reference is false.
  867. (void) ge::AttrUtils::GetBool(peer_in_node_desc, ATTR_NAME_REFERENCE, is_reference);
  868. if (is_reference) {
  869. GELOGE(ge::PARAM_INVALID, "The node %s cannot have both atomic and is_reference attribute.",
  870. peer_in_node_desc->GetName().c_str());
  871. return ge::PARAM_INVALID;
  872. }
  873. vector<int> is_connecting_output;
  874. // If GetBool fail, attr is_connecting_output is an empty vector.
  875. (void) ge::AttrUtils::GetListInt(peer_in_node_desc, ATTR_NAME_NODE_CONNECT_OUTPUT, is_connecting_output);
  876. if (is_connecting_output.empty()) {
  877. tmp_normal_atomic_nodes.emplace_back(peer_in_node);
  878. continue;
  879. }
  880. connecting_output_atomic_nodes.emplace_back(peer_in_node);
  881. tmp_normal_atomic_nodes.clear();
  882. break;
  883. }
  884. }
  885. }
  886. }
  887. if (!tmp_normal_atomic_nodes.empty()) {
  888. normal_atomic_nodes_map[node] = tmp_normal_atomic_nodes;
  889. }
  890. }
  891. }
  892. return SUCCESS;
  893. }
  894. Status GraphMemoryAssigner::AssignAtomicOutputAndWorkspaceMemory(const ge::NodePtr &node,
  895. vector<int64_t> &mem_offset_end) {
  896. auto node_op_desc = node->GetOpDesc();
  897. // Assign atomic node output memory
  898. Status ret = AssignAtomicOutputMemory(node, mem_offset_end);
  899. if (ret != SUCCESS) {
  900. GELOGE(ret, "Failed to assign atomic output memory, node is %s.", node_op_desc->GetName().c_str());
  901. return ret;
  902. }
  903. // Check and assign atomic node workspace memory
  904. map<string, map<int64_t, int64_t>> atomic_workspace_info;
  905. atomic_workspace_info = node_op_desc->TryGetExtAttr(EXT_ATTR_ATOMIC_WORKSPACE_INFO, atomic_workspace_info);
  906. if (!atomic_workspace_info.empty()) {
  907. bool is_fusion_node = false;
  908. // If GetBool fail, is_fusion_node is false.
  909. (void) ge::AttrUtils::GetBool(node_op_desc, ATOMIC_ATTR_IS_FUSION_NODE, is_fusion_node);
  910. if (is_fusion_node) {
  911. // Assign fusion atomic node workspace memory
  912. ret = AssignFusionAtomicWorkspaceMemory(node_op_desc, atomic_workspace_info, mem_offset_end);
  913. } else {
  914. // Assign single ordinary atomic node workspace memory, not include fusion node
  915. ret = AssignOrdinaryAtomicWorkspaceMemory(node_op_desc, atomic_workspace_info, mem_offset_end);
  916. }
  917. if (ret != SUCCESS) {
  918. GELOGE(ret, "Assign atomic workspace memory failed, node is %s.", node_op_desc->GetName().c_str());
  919. return ret;
  920. }
  921. } else {
  922. GELOGW("Current atomic node %s does not have attr ATOMIC_WORKSPACE_INFO.", node->GetName().c_str());
  923. }
  924. return SUCCESS;
  925. }
  926. Status GraphMemoryAssigner::AssignConnectNetOutputAtomicMemory(vector<NodePtr> &connect_netoutput_nodes) {
  927. auto iter = memory_offset_.find(RT_MEMORY_HBM);
  928. if (iter == memory_offset_.end()) {
  929. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", RT_MEMORY_HBM);
  930. return FAILED;
  931. }
  932. for (auto &node : connect_netoutput_nodes) {
  933. GE_CHECK_NOTNULL(node);
  934. if (node->GetOpDesc() == nullptr) {
  935. GELOGW("Current node %s op desc is nullptr, memory assignment is skipped.", node->GetName().c_str());
  936. continue;
  937. }
  938. // Atomic memory start addr
  939. int64_t original_atomic_mem_start = static_cast<int64_t>(iter->second.mem_offset_);
  940. GELOGD("Start to assign memory of atomic node, node name: %s, node type: %s, mem_offset: %ld.",
  941. node->GetName().c_str(), node->GetOpDesc()->GetType().c_str(), original_atomic_mem_start);
  942. vector<int64_t> mem_offset_end;
  943. if (AssignAtomicOutputAndWorkspaceMemory(node, mem_offset_end) != SUCCESS) {
  944. GELOGE(FAILED, "Assign atomic output and workspace memory failed, node is %s.", node->GetName().c_str());
  945. return FAILED;
  946. }
  947. // All atomic nodes use atomic_addr_clean op independently, so we need to set the attr separately.
  948. if (SetIndependentAtomicAttr(node, original_atomic_mem_start, mem_offset_end) != SUCCESS) {
  949. GELOGE(FAILED, "Failed to set atomic attr separately.");
  950. return FAILED;
  951. }
  952. }
  953. return SUCCESS;
  954. }
  955. Status GraphMemoryAssigner::AssignReferenceMemory() {
  956. for (auto &node : compute_graph_->GetDirectNode()) {
  957. // Get the reference type of the node, default is false
  958. bool is_ref = false;
  959. // If GetBool fail, is_ref is false.
  960. (void) ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_REFERENCE, is_ref);
  961. if (!is_ref) {
  962. continue;
  963. }
  964. GELOGI("Current node %s needs to support the reference relationship between output and input.",
  965. node->GetName().c_str());
  966. auto out_op_desc = node->GetOpDesc();
  967. GE_IF_BOOL_EXEC(out_op_desc == nullptr, GELOGE(ge::FAILED, "out_op_desc is null."); return ge::FAILED);
  968. vector<int64_t> output_list = out_op_desc->GetOutputOffset();
  969. if (out_op_desc->GetOutputsSize() > output_list.size()) {
  970. GELOGE(ge::FAILED, "The size %zu of node output desc is more than output_list's size %zu.",
  971. out_op_desc->GetOutputsSize(), output_list.size());
  972. return ge::FAILED;
  973. }
  974. map<string, int> input_name_index;
  975. for (const auto &input_name : out_op_desc->GetAllInputNames()) {
  976. int index = out_op_desc->GetInputIndexByName(input_name);
  977. input_name_index.emplace(input_name, index);
  978. }
  979. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  980. string out_data_anchor_name = out_op_desc->GetOutputNameByIndex(out_data_anchor->GetIdx());
  981. auto iter = input_name_index.find(out_data_anchor_name);
  982. if (iter != input_name_index.end()) {
  983. int index = iter->second;
  984. GELOGI("Reference memory: input anchor index = %d, input anchor name = %s, output anchor name = %s.", index,
  985. iter->first.c_str(), out_data_anchor_name.c_str());
  986. GE_CHECK_NOTNULL(node->GetInDataAnchor(index));
  987. auto peer_out_anchor = node->GetInDataAnchor(index)->GetPeerOutAnchor();
  988. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  989. int peer_out_anchor_index = peer_out_anchor->GetIdx();
  990. auto peer_out_node = peer_out_anchor->GetOwnerNode();
  991. auto peer_out_op_desc = peer_out_node->GetOpDesc();
  992. GE_CHECK_NOTNULL(peer_out_op_desc);
  993. output_list[out_data_anchor->GetIdx()] = peer_out_op_desc->GetOutputOffset()[peer_out_anchor_index];
  994. GELOGI("Reference output : Set %s name[%s] output[%d] offset to [%ld] stream_id[%ld]",
  995. node->GetOwnerComputeGraph()->GetName().c_str(), peer_out_op_desc->GetName().c_str(),
  996. out_data_anchor->GetIdx(), output_list[out_data_anchor->GetIdx()], peer_out_op_desc->GetStreamId());
  997. } else {
  998. GELOGI("Reference output : origin %s name[%s] output[%d] offset is [%ld] stream_id[%ld]",
  999. node->GetOwnerComputeGraph()->GetName().c_str(), out_op_desc->GetName().c_str(),
  1000. out_data_anchor->GetIdx(), output_list[out_data_anchor->GetIdx()], out_op_desc->GetStreamId());
  1001. }
  1002. }
  1003. out_op_desc->SetOutputOffset(output_list);
  1004. }
  1005. return ge::SUCCESS;
  1006. }
  1007. bool GraphMemoryAssigner::CheckInputIsSupportAtomic(const ge::NodePtr &node) {
  1008. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  1009. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  1010. if (peer_out_data_anchor == nullptr) {
  1011. continue;
  1012. }
  1013. auto peer_op_desc = peer_out_data_anchor->GetOwnerNode()->GetOpDesc();
  1014. if (peer_op_desc == nullptr) {
  1015. continue;
  1016. }
  1017. if ((peer_op_desc->GetType() == CONSTANTOP) || (peer_op_desc->GetType() == AIPP_DATA_TYPE) ||
  1018. (peer_op_desc->GetType() == VARIABLE)) {
  1019. GELOGE(ge::FAILED,
  1020. "The current node is %s, and the peer out node is %s. Currently, this scenario is not supported",
  1021. node->GetName().c_str(), peer_op_desc->GetName().c_str());
  1022. return false;
  1023. }
  1024. }
  1025. return true;
  1026. }
  1027. Status GraphMemoryAssigner::AssignAtomicOutputMemory(const ge::NodePtr &node, vector<int64_t> &mem_offset_end) {
  1028. auto op_desc = node->GetOpDesc();
  1029. GE_IF_BOOL_EXEC(op_desc == nullptr, GELOGE(ge::FAILED, "op_desc is null."); return ge::FAILED);
  1030. mem_offset_end.clear();
  1031. GELOGD("Begin to assign atomic output memory, node = %s.", op_desc->GetName().c_str());
  1032. vector<int64_t> atomic_output_index;
  1033. // If GetListInt fail, atomic_output_index is empty.
  1034. (void) ge::AttrUtils::GetListInt(op_desc, ATOMIC_ATTR_OUTPUT_INDEX, atomic_output_index);
  1035. // Check atomic output
  1036. vector<int64_t> output_list = op_desc->GetOutputOffset();
  1037. if (atomic_output_index.size() > output_list.size()) {
  1038. GELOGE(ge::FAILED, "The size of atomic_output_index is more than the size of output_list");
  1039. return ge::FAILED;
  1040. }
  1041. auto output_list_size = static_cast<int64_t>(output_list.size());
  1042. auto iter = memory_offset_.find(RT_MEMORY_HBM);
  1043. if (iter == memory_offset_.end()) {
  1044. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", RT_MEMORY_HBM);
  1045. return FAILED;
  1046. }
  1047. for (auto &output_index : atomic_output_index) {
  1048. if (output_index >= output_list_size) {
  1049. GELOGE(ge::PARAM_INVALID, "The output index %ld is more than the size %ld of output_list.", output_index,
  1050. output_list_size);
  1051. return ge::PARAM_INVALID;
  1052. }
  1053. // If the input of the cascade op needs to clear the atomic addr, there is no need to clear it separately here
  1054. bool is_assigned_mem = false;
  1055. if (GetMemoryAssignmentStatus(node, output_index, is_assigned_mem) != SUCCESS) {
  1056. GELOGE(ge::FAILED, "Failed to get memory assignment of node %s.", node->GetName().c_str());
  1057. return ge::FAILED;
  1058. }
  1059. // If you have already assigned an atomic address, skip it, and you don't need to reassign it.
  1060. if (is_assigned_mem) {
  1061. GELOGI(
  1062. "Node %s atomic output : we have assigned atomic memory as the input of next node in "
  1063. "ReAssignContinuousMemory function.",
  1064. op_desc->GetName().c_str());
  1065. continue;
  1066. }
  1067. auto output_desc = op_desc->GetAllOutputsDescPtr().at(output_index);
  1068. int64_t size = 0;
  1069. if (ge::TensorUtils::GetSize(*output_desc, size) != SUCCESS) {
  1070. GELOGI("Get size failed");
  1071. }
  1072. output_list[output_index] = iter->second.mem_offset_;
  1073. GELOGI("[IMAS]Atomic output : Set %s name[%s] output[%ld] offset to [%zu] stream_id[%ld] size[%ld] real_size[%ld].",
  1074. compute_graph_->GetName().c_str(), op_desc->GetName().c_str(), output_index,
  1075. iter->second.mem_offset_, op_desc->GetStreamId(), size, size);
  1076. iter->second.mem_offset_ += size;
  1077. AlignMemOffset(MEM_ALIGN_SIZE, RT_MEMORY_HBM);
  1078. mem_offset_end.emplace_back(iter->second.mem_offset_);
  1079. }
  1080. op_desc->SetOutputOffset(output_list);
  1081. return ge::SUCCESS;
  1082. }
  1083. Status GraphMemoryAssigner::GetMemoryAssignmentStatus(const ge::NodePtr &node, int64_t output_index,
  1084. bool &is_mem_assigned) {
  1085. if (static_cast<size_t>(output_index) >= node->GetAllOutDataAnchors().size()) {
  1086. GELOGE(ge::PARAM_INVALID, "Output index %ld is more than the size of node's AllOutDataAnchors.", output_index);
  1087. return ge::PARAM_INVALID;
  1088. }
  1089. auto out_data_anchor = node->GetAllOutDataAnchors().at(output_index);
  1090. GE_CHECK_NOTNULL(out_data_anchor);
  1091. auto input_anchors = out_data_anchor->GetPeerInDataAnchors();
  1092. for (auto &input_anchor : input_anchors) {
  1093. auto output_node = input_anchor->GetOwnerNode();
  1094. /// Get input atomic attr of peer output op, if atomic_input_index[0] = -1, indicates that the atomic address
  1095. /// has been assigned
  1096. vector<int64_t> atomic_input_index;
  1097. (void) ge::AttrUtils::GetListInt(output_node->GetOpDesc(), ATOMIC_ATTR_INPUT_INDEX, atomic_input_index);
  1098. if (!atomic_input_index.empty() && (atomic_input_index[0] == kAllInputAddrIsAtomic)) {
  1099. is_mem_assigned = true;
  1100. break;
  1101. }
  1102. }
  1103. return SUCCESS;
  1104. }
  1105. Status GraphMemoryAssigner::AssignOrdinaryAtomicWorkspaceMemory(const ge::OpDescPtr &op_desc,
  1106. map<string, map<int64_t, int64_t>> &workspace_info,
  1107. vector<int64_t> &mem_offset_end) {
  1108. GELOGI("Begin to reassign normal atomic memory, node = %s.", op_desc->GetName().c_str());
  1109. auto mem_type_iter = memory_offset_.find(RT_MEMORY_HBM);
  1110. if (mem_type_iter == memory_offset_.end()) {
  1111. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", RT_MEMORY_HBM);
  1112. return FAILED;
  1113. }
  1114. vector<int64_t> workspace_vector = op_desc->GetWorkspace();
  1115. for (auto iter = workspace_info.begin(); iter != workspace_info.end(); ++iter) {
  1116. if (op_desc->GetName() != iter->first) {
  1117. GELOGE(ge::PARAM_INVALID, "The node name %s and the node name %s in workspace info are inconsistent.",
  1118. op_desc->GetName().c_str(), iter->first.c_str());
  1119. return ge::PARAM_INVALID;
  1120. }
  1121. if (iter->second.empty()) {
  1122. continue;
  1123. }
  1124. for (auto &info_iter : iter->second) {
  1125. auto workspace_index = static_cast<uint64_t>(info_iter.first);
  1126. auto workspace_size = info_iter.second;
  1127. if (workspace_index >= workspace_vector.size()) {
  1128. GELOGE(ge::PARAM_INVALID, "The workspace index %lu is more than the size %zu of workspace vector.",
  1129. workspace_index, workspace_vector.size());
  1130. return ge::PARAM_INVALID;
  1131. }
  1132. workspace_vector[workspace_index] = mem_type_iter->second.mem_offset_;
  1133. GELOGI(
  1134. "[IMAS]Atomic ordinary workspace : Set %s name[%s] workspace[%lu] offset to [%zu] stream_id[%ld] "
  1135. "size[%ld] real_size[%ld].",
  1136. compute_graph_->GetName().c_str(), op_desc->GetName().c_str(), workspace_index,
  1137. mem_type_iter->second.mem_offset_, op_desc->GetStreamId(), workspace_size, workspace_size);
  1138. mem_type_iter->second.mem_offset_ += workspace_size;
  1139. mem_offset_end.emplace_back(mem_type_iter->second.mem_offset_);
  1140. }
  1141. }
  1142. op_desc->SetWorkspace(workspace_vector);
  1143. return SUCCESS;
  1144. }
  1145. Status GraphMemoryAssigner::AssignFusionAtomicWorkspaceMemory(const ge::OpDescPtr &op_desc,
  1146. map<string, map<int64_t, int64_t>> &workspace_info,
  1147. vector<int64_t> &mem_offset_end) {
  1148. GELOGI("Begin to reassign fusion atomic memory, node = %s.", op_desc->GetName().c_str());
  1149. auto mem_type_iter = memory_offset_.find(RT_MEMORY_HBM);
  1150. if (mem_type_iter == memory_offset_.end()) {
  1151. GELOGE(FAILED, "Memory offset don't have memory type[%ld].", RT_MEMORY_HBM);
  1152. return FAILED;
  1153. }
  1154. map<string, map<int64_t, int64_t>> sub_node_workspace_offset;
  1155. for (auto &iter : workspace_info) {
  1156. if (iter.second.empty()) {
  1157. continue;
  1158. }
  1159. map<int64_t, int64_t> index_offset;
  1160. for (auto &info_iter : iter.second) {
  1161. auto workspace_index = static_cast<uint64_t>(info_iter.first);
  1162. auto workspace_size = info_iter.second;
  1163. size_t workspace_offset = mem_type_iter->second.mem_offset_;
  1164. GELOGI(
  1165. "[IMAS]Atomic fusion workspace : Set %s name[%s] workspace[%lu] offset to [%zu] stream_id[%ld] size[%ld] "
  1166. "real_size[%ld].", compute_graph_->GetName().c_str(), op_desc->GetName().c_str(), workspace_index,
  1167. mem_type_iter->second.mem_offset_, op_desc->GetStreamId(), workspace_size, workspace_size);
  1168. mem_type_iter->second.mem_offset_ += workspace_size;
  1169. mem_offset_end.emplace_back(mem_type_iter->second.mem_offset_);
  1170. index_offset.insert(std::make_pair(workspace_index, workspace_offset));
  1171. }
  1172. sub_node_workspace_offset.insert(std::make_pair(iter.first, index_offset));
  1173. }
  1174. if (!(op_desc->SetExtAttr(EXT_ATTR_ATOMIC_WORKSPACE_OFFSET, sub_node_workspace_offset))) {
  1175. GELOGE(FAILED, "Set EXT_ATTR_ATOMIC_WORKSPACE_OFFSET failed, op name:%s.", op_desc->GetName().c_str());
  1176. return FAILED;
  1177. }
  1178. return SUCCESS;
  1179. }
  1180. Status GraphMemoryAssigner::CheckOffset() {
  1181. std::map<std::string, std::string> anchor_to_symbol;
  1182. std::map<std::string, std::list<NodeIndexIO>> symbol_to_anchors;
  1183. if (GraphUtils::GetRefMapping(compute_graph_, symbol_to_anchors, anchor_to_symbol) != GRAPH_SUCCESS) {
  1184. GELOGE(FAILED, "Get ref-mapping for graph %s failed.", compute_graph_->GetName().c_str());
  1185. return FAILED;
  1186. }
  1187. for (const ge::NodePtr &node : compute_graph_->GetAllNodes()) {
  1188. GE_CHECK_NOTNULL(node->GetOpDesc());
  1189. vector<int64_t> input_list = node->GetOpDesc()->GetInputOffset();
  1190. for (auto input : input_list) {
  1191. if (input == ge::kInvalidOffset) {
  1192. GELOGE(FAILED, "Invalid offset in node: %s input: %ld.", node->GetName().c_str(), ge::kInvalidOffset);
  1193. return FAILED;
  1194. }
  1195. }
  1196. bool need_update_output = false;
  1197. vector<int64_t> output_list = node->GetOpDesc()->GetOutputOffset();
  1198. for (uint32_t i = 0; i < output_list.size(); ++i) {
  1199. if (output_list[i] == ge::kInvalidOffset) {
  1200. GELOGE(FAILED, "Invalid offset in node: %s output: %ld.", node->GetName().c_str(), ge::kInvalidOffset);
  1201. return FAILED;
  1202. }
  1203. if (node->GetType() == IDENTITY || node->GetType() == READVARIABLEOP) {
  1204. auto symbol_offset = GetSymbolOutputOffset(anchor_to_symbol, symbol_to_anchors, node, i);
  1205. if (symbol_offset != ge::kInvalidOffset && output_list[i] != symbol_offset) {
  1206. output_list[i] = symbol_offset;
  1207. need_update_output = true;
  1208. }
  1209. }
  1210. }
  1211. if (need_update_output) {
  1212. node->GetOpDesc()->SetOutputOffset(output_list);
  1213. }
  1214. vector<int64_t> workspace_list = node->GetOpDesc()->GetWorkspace();
  1215. for (auto workspace : workspace_list) {
  1216. if (workspace == ge::kInvalidOffset) {
  1217. GELOGE(FAILED, "Invalid offset in node: %s workspace: %ld.", node->GetName().c_str(), ge::kInvalidOffset);
  1218. return FAILED;
  1219. }
  1220. }
  1221. }
  1222. return SUCCESS;
  1223. }
  1224. ge::Status GraphMemoryAssigner::SetInputOffset() {
  1225. if (memory_offset_.empty()) {
  1226. GELOGE(FAILED, "memory_offset_ is empty.");
  1227. return FAILED;
  1228. }
  1229. for (auto pair : memory_offset_) {
  1230. GEEVENT("[IMAS]AfterAssignMemory : %s memoffset[%zu], memory type[%ld]", compute_graph_->GetName().c_str(),
  1231. pair.second.mem_offset_, pair.first);
  1232. }
  1233. for (const ge::NodePtr &node : compute_graph_->GetAllNodes()) {
  1234. if (UpdateOpInputOffset(node) != ge::SUCCESS) {
  1235. GELOGE(ge::FAILED, "Update op input offset failed");
  1236. return ge::FAILED;
  1237. }
  1238. }
  1239. return ge::SUCCESS;
  1240. }
  1241. NodePtr GraphMemoryAssigner::GetKnownInputNode(const NodePtr &node) const {
  1242. if (!node->GetOpDesc()->HasAttr(ATTR_NAME_PARENT_NODE_INDEX)) {
  1243. return node;
  1244. }
  1245. if (NodeUtils::IsDynamicShape(node)) {
  1246. return node;
  1247. }
  1248. return NodeUtils::GetParentInput(node);
  1249. }
  1250. ge::Status GraphMemoryAssigner::UpdateConstArgsOffset(const NodePtr &node, vector<int64_t> &input_list) const {
  1251. uint32_t parent_index = 0;
  1252. if (!AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  1253. return SUCCESS;
  1254. }
  1255. // Subgraph Data Node, check for constant input.
  1256. std::string op_type;
  1257. const auto &in_node = NodeUtils::GetParentInput(node);
  1258. if (NodeUtils::GetConstOpType(in_node, op_type)) {
  1259. input_list = in_node->GetOpDesc()->GetOutputOffset();
  1260. node->GetOpDesc()->SetOutputOffset(input_list); // Set Data output same as const output.
  1261. return SUCCESS; // Constant input.
  1262. }
  1263. // Memory allocated for dynamic shape subgraph Data.
  1264. if (NodeUtils::IsDynamicShape(node)) {
  1265. return SUCCESS;
  1266. }
  1267. const auto &owner = node->GetOwnerComputeGraph();
  1268. const auto &parent_desc = owner->GetParentNode()->GetOpDesc();
  1269. const auto parent_inputs = parent_desc->GetInputOffset();
  1270. if (parent_inputs.size() <= parent_index) {
  1271. GELOGE(FAILED, "Get Parent input offset failed, node: %s, input size: %zu, parent index: %u",
  1272. node->GetName().c_str(), parent_inputs.size(), parent_index);
  1273. return FAILED;
  1274. }
  1275. input_list = {parent_inputs[parent_index]};
  1276. node->GetOpDesc()->SetOutputOffset(input_list); // Set Data output same as parent input.
  1277. return SUCCESS;
  1278. }
  1279. ge::Status GraphMemoryAssigner::UpdateOpInputOffset(const NodePtr &node, vector<int64_t> &input_list) const {
  1280. vector<int64_t> origin_input_list;
  1281. vector<int64_t> memory_type;
  1282. auto tmp_op_desc = node->GetOpDesc();
  1283. origin_input_list = tmp_op_desc->GetInputOffset();
  1284. int64_t valid_input_index = 0;
  1285. bool has_mem_type_attr = ge::AttrUtils::GetListInt(tmp_op_desc, ATTR_NAME_INPUT_MEM_TYPE_LIST, memory_type);
  1286. for (const auto &anchor : node->GetAllInDataAnchors()) {
  1287. vector<int64_t> output_list;
  1288. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  1289. if (peer_out_anchor == nullptr) {
  1290. continue;
  1291. }
  1292. // If the current node not broadcast, the OutputOffset of the previous node is used to update the input_list
  1293. auto last_peer_out_node = peer_out_anchor->GetOwnerNode();
  1294. auto last_peer_out_op_desc = last_peer_out_node->GetOpDesc();
  1295. GE_CHECK_NOTNULL(last_peer_out_op_desc);
  1296. output_list = last_peer_out_op_desc->GetOutputOffset();
  1297. auto out_index = static_cast<unsigned long>(peer_out_anchor->GetIdx());
  1298. if (output_list.size() > static_cast<size_t>(out_index)) {
  1299. int64_t input_offset = output_list.at(out_index);
  1300. if (has_mem_type_attr && !origin_input_list.empty()) {
  1301. auto input_size = tmp_op_desc->GetInputsSize();
  1302. auto ori_input_offset_list_size = origin_input_list.size();
  1303. auto mem_type_size = memory_type.size();
  1304. if ((input_size != mem_type_size) || (input_size != ori_input_offset_list_size)) {
  1305. GELOGE(ge::FAILED,
  1306. "fusion: node[%s] input_size[%zu] diff from memory_type_size[%zu]"
  1307. " from ori_input_offset_list_size[%lu]",
  1308. tmp_op_desc->GetName().c_str(), input_size, mem_type_size, ori_input_offset_list_size);
  1309. return ge::FAILED;
  1310. }
  1311. // not hbm keep orignal inputoffest
  1312. // hbm inputoffset = original inputoffset + outputoffset
  1313. input_offset = (memory_type[valid_input_index] == RT_MEMORY_L1 ? origin_input_list[valid_input_index]
  1314. : origin_input_list[valid_input_index] + output_list.at(out_index));
  1315. }
  1316. const auto &in_node = GetKnownInputNode(peer_out_anchor->GetOwnerNode());
  1317. if (in_node->GetType() == CONSTANT) {
  1318. GeTensorDesc tensor_desc = tmp_op_desc->GetInputDesc(static_cast<uint32_t>(anchor->GetIdx()));
  1319. GE_CHK_STATUS(TensorUtils::GetDataOffset(tensor_desc, input_offset));
  1320. }
  1321. GELOGI("%s node[%s] input[%d] is set from node[%s] out index[%lu] offset[%ld]",
  1322. has_mem_type_attr == true ? "Fusion" : "",
  1323. tmp_op_desc->GetName().c_str(),
  1324. valid_input_index,
  1325. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
  1326. out_index,
  1327. input_offset);
  1328. input_list.emplace_back(input_offset);
  1329. valid_input_index++;
  1330. }
  1331. }
  1332. return ge::SUCCESS;
  1333. }
  1334. ge::Status GraphMemoryAssigner::UpdateOpInputOffset(const NodePtr &node) const {
  1335. GE_CHECK_NOTNULL(node->GetOpDesc());
  1336. vector<int64_t> input_list;
  1337. if (node->GetType() == HCOMBROADCAST || node->GetType() == HVDCALLBACKBROADCAST) {
  1338. for (const auto &anchor : node->GetAllInDataAnchors()) {
  1339. vector<int64_t> output_list;
  1340. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  1341. if (peer_out_anchor == nullptr) {
  1342. continue;
  1343. }
  1344. auto last_peer_out_node = peer_out_anchor->GetOwnerNode();
  1345. // If the current node is broadcast and the preceding node is variable, because InputOffset has been set
  1346. // in function:AssignVarAttr2Nodes, then the InputOffset of the broadcast node is taken to update the input_list.
  1347. // Otherwise, the OutputOffset of the previous node is used to update the input_list.
  1348. if (last_peer_out_node->GetType() != VARIABLE) {
  1349. auto last_peer_out_op_desc = last_peer_out_node->GetOpDesc();
  1350. GE_CHECK_NOTNULL(last_peer_out_op_desc);
  1351. output_list = last_peer_out_op_desc->GetOutputOffset();
  1352. if (output_list.size() > static_cast<size_t>(peer_out_anchor->GetIdx())) {
  1353. input_list.emplace_back(output_list.at(peer_out_anchor->GetIdx()));
  1354. }
  1355. } else {
  1356. vector<int64_t> cur_node_input_list;
  1357. auto cur_node_op_desc = node->GetOpDesc();
  1358. GE_CHECK_NOTNULL(cur_node_op_desc);
  1359. cur_node_input_list = cur_node_op_desc->GetInputOffset();
  1360. if (cur_node_input_list.size() > static_cast<size_t>(anchor->GetIdx())) {
  1361. input_list.emplace_back(cur_node_input_list.at(anchor->GetIdx()));
  1362. }
  1363. }
  1364. }
  1365. } else if (node->GetType() == DATA_TYPE) {
  1366. if (UpdateConstArgsOffset(node, input_list) != SUCCESS) {
  1367. GELOGE(FAILED, "Update data: %s args offset failed.", node->GetName().c_str());
  1368. return FAILED;
  1369. }
  1370. } else {
  1371. if (UpdateOpInputOffset(node, input_list) != SUCCESS) {
  1372. GELOGE(FAILED, "Update node: %s input offset failed.", node->GetName().c_str());
  1373. return FAILED;
  1374. }
  1375. }
  1376. node->GetOpDesc()->SetInputOffset(input_list);
  1377. return SUCCESS;
  1378. }
  1379. Status GraphMemoryAssigner::SetIndependentAtomicAttr(const ge::NodePtr &node, int64_t atomic_mem_start,
  1380. const vector<int64_t> &mem_offset_end) {
  1381. GELOGD("Start to set independent atomic attr, atomic_addr_clean memory offset start is %ld", atomic_mem_start);
  1382. // Parsing offset and size vectors
  1383. vector<int64_t> memory_offset_start;
  1384. vector<int64_t> memory_offset_size;
  1385. memory_offset_start.emplace_back(atomic_mem_start);
  1386. for (size_t i = 0; i < mem_offset_end.size(); ++i) {
  1387. memory_offset_start.emplace_back(mem_offset_end[i]);
  1388. // Number 1 means element index
  1389. auto size = memory_offset_start[i + 1] - memory_offset_start[i];
  1390. memory_offset_size.emplace_back(size);
  1391. }
  1392. memory_offset_start.pop_back();
  1393. const auto &in_control_anchor = node->GetInControlAnchor();
  1394. if (!memory_offset_size.empty() && in_control_anchor != nullptr) {
  1395. for (auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  1396. if (peer_out_control_anchor == nullptr) {
  1397. continue;
  1398. }
  1399. auto peer_out_node = peer_out_control_anchor->GetOwnerNode();
  1400. auto peer_out_node_desc = peer_out_node->GetOpDesc();
  1401. if (peer_out_node_desc == nullptr) {
  1402. continue;
  1403. }
  1404. GELOGD("Current node memory_offset vector size is %zu, node name %s, node type is %s.", memory_offset_size.size(),
  1405. peer_out_node_desc->GetName().c_str(), peer_out_node_desc->GetType().c_str());
  1406. if (peer_out_node_desc->GetType() == ATOMICADDRCLEAN) {
  1407. if (SetAtomicCleanAttr(peer_out_node, memory_offset_start, memory_offset_size) != SUCCESS) {
  1408. GELOGE(FAILED, "Set atomic clean attr failed.");
  1409. return FAILED;
  1410. }
  1411. }
  1412. }
  1413. }
  1414. return SUCCESS;
  1415. }
  1416. ge::Status GraphMemoryAssigner::SetAtomicCleanAttr(const NodePtr &node, const vector<int64_t> &atomic_mem_start,
  1417. const vector<int64_t> &atomic_mem_size) {
  1418. auto node_op_desc = node->GetOpDesc();
  1419. if (node_op_desc != nullptr) {
  1420. GELOGD("Node %s, set atomic clean attr start.", node->GetName().c_str());
  1421. vector<int64_t> workspace_vector = node_op_desc->GetWorkspace();
  1422. vector<int64_t> workspace_byte_vector = node_op_desc->GetWorkspaceBytes();
  1423. workspace_vector.insert(workspace_vector.end(), atomic_mem_start.begin(), atomic_mem_start.end());
  1424. workspace_byte_vector.insert(workspace_byte_vector.end(), atomic_mem_size.begin(), atomic_mem_size.end());
  1425. node_op_desc->SetWorkspace(workspace_vector);
  1426. node_op_desc->SetWorkspaceBytes(workspace_byte_vector);
  1427. std::vector<int64_t> mem_start_vector;
  1428. // If GetListInt fail, mem_start_vector is empty.
  1429. (void) ge::AttrUtils::GetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_START, mem_start_vector);
  1430. mem_start_vector.insert(mem_start_vector.end(), atomic_mem_start.begin(), atomic_mem_start.end());
  1431. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_START, mem_start_vector),
  1432. GELOGE(FAILED, "SetListInt failed.");
  1433. return FAILED);
  1434. std::vector<int64_t> mem_size_vector;
  1435. // If GetListInt fail, mem_size_vector is empty.
  1436. (void) ge::AttrUtils::GetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_MEM_SIZE, mem_size_vector);
  1437. mem_size_vector.insert(mem_size_vector.end(), atomic_mem_size.begin(), atomic_mem_size.end());
  1438. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_MEM_SIZE, mem_size_vector),
  1439. GELOGE(FAILED, "SetListInt failed.");
  1440. return FAILED);
  1441. std::stringstream ss;
  1442. for (auto iter : atomic_mem_start) {
  1443. ss << iter << " ";
  1444. }
  1445. string atomic_mem_start_str = ss.str();
  1446. ss.clear();
  1447. ss.str("");
  1448. for (auto iter : atomic_mem_size) {
  1449. ss << iter << " ";
  1450. }
  1451. string atomic_mem_size_str = ss.str();
  1452. GELOGI("[IMAS]SetAtomicCleanAttr : Set graph[%s] atomic_node[%s] output offset [%s] size[%s] streamid[%ld]",
  1453. node->GetOwnerComputeGraph()->GetName().c_str(), node_op_desc->GetName().c_str(),
  1454. atomic_mem_start_str.c_str(), atomic_mem_size_str.c_str(), node->GetOpDesc()->GetStreamId());
  1455. }
  1456. return SUCCESS;
  1457. }
  1458. void GraphMemoryAssigner::AlignMemOffset(const int64_t &mem_align_size, int64_t memory_type) {
  1459. if (mem_align_size <= 0) {
  1460. return;
  1461. }
  1462. auto iter = memory_offset_.find(memory_type);
  1463. if (iter == memory_offset_.end()) {
  1464. GELOGW("Memory offset don't have memory type[%ld].", memory_type);
  1465. return;
  1466. }
  1467. iter->second.mem_offset_ =
  1468. (iter->second.mem_offset_ + mem_align_size - 1) / mem_align_size * mem_align_size;
  1469. }
  1470. ge::Status GraphMemoryAssigner::GetNodeListMemoryType(const vector<NodePtr> &nodes, int32_t mem_reuse_model,
  1471. int64_t &memory_type) {
  1472. memory_type = RT_MEMORY_HBM;
  1473. // In the dynamic batch scenario, the memory attributes of nodes are the same.
  1474. for (auto &n : nodes) {
  1475. if (mem_reuse_model == kVirtualInputNodeMemoryReuse) {
  1476. GE_CHK_STATUS_RET(GetNodeMemoryType(n, memory_type, "input"), "Get node memory type failed.")
  1477. break;
  1478. }
  1479. if (mem_reuse_model == kVirtualOutputNodeMemoryReuse) {
  1480. GE_CHK_STATUS_RET(GetNodeMemoryType(n, memory_type, "output"), "Get node memory type failed.");
  1481. break;
  1482. }
  1483. }
  1484. return SUCCESS;
  1485. }
  1486. ge::Status GraphMemoryAssigner::GetNodeMemoryType(const NodePtr &node, int64_t &memory_type, string input_or_output) {
  1487. memory_type = RT_MEMORY_HBM;
  1488. vector<int64_t> mem_type_list;
  1489. if (input_or_output == "input") {
  1490. (void) ge::AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_INPUT_MEM_TYPE_LIST, mem_type_list);
  1491. }
  1492. if (input_or_output == "output") {
  1493. (void) ge::AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_OUTPUT_MEM_TYPE_LIST, mem_type_list);
  1494. }
  1495. if (mem_type_list.empty()) {
  1496. if (memory_offset_.find(memory_type) == memory_offset_.end()) {
  1497. GELOGE(FAILED, "Memory offset map does not have memory type[%ld].", memory_type);
  1498. return FAILED;
  1499. }
  1500. return SUCCESS;
  1501. }
  1502. if (mem_type_list.size() != node->GetAllInDataAnchorsSize()) {
  1503. GELOGE(FAILED, "The size[%zu] of mem type list is not equal to the size of in data anchor[%u].",
  1504. mem_type_list.size(), node->GetAllInDataAnchorsSize());
  1505. return FAILED;
  1506. }
  1507. if (!CheckContinuousMemType(mem_type_list)) {
  1508. GELOGE(FAILED, "Check continuous memory type failed.");
  1509. return FAILED;
  1510. }
  1511. // It is continuous memory and memory type is the same, so use the first memory.
  1512. memory_type = mem_type_list[0];
  1513. return SUCCESS;
  1514. }
  1515. bool GraphMemoryAssigner::CheckContinuousMemType(vector<int64_t> mem_type_list) {
  1516. if (mem_type_list.size() == 0) {
  1517. return true;
  1518. }
  1519. int64_t mem_type_tmp = mem_type_list[0];
  1520. for (auto mem_type : mem_type_list) {
  1521. if (mem_type != mem_type_tmp) {
  1522. GELOGW("The memory is continuous, but the type of the input memory is inconsistent. They are [%ld] and [%ld].",
  1523. mem_type_tmp, mem_type);
  1524. return false;
  1525. }
  1526. }
  1527. if (memory_offset_.find(mem_type_tmp) == memory_offset_.end()) {
  1528. GELOGW("Memory offset map does not have memory type[%ld].", mem_type_tmp);
  1529. return false;
  1530. }
  1531. return true;
  1532. }
  1533. void GraphMemoryAssigner::PrintMemoryOffset() {
  1534. for (auto pair : memory_offset_) {
  1535. // Assign memory of max batch nodes that have the same batch label.
  1536. GELOGD("Reassign memory for max batch virtual nodes, memory type = %ld, memory offset = %zu.",
  1537. pair.first, pair.second.mem_offset_);
  1538. }
  1539. }
  1540. } // namespace ge

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