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 80 kB

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

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