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 93 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
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 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
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
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 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
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
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
4 years ago
5 years ago
4 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
5 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
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
4 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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. #include "graph/build/memory/buffer_pool_mem_assigner.h"
  33. namespace {
  34. const int kAllInputAddrIsAtomic = -1;
  35. const int kVirtualInputNodeMemoryReuse = 0;
  36. const int kVirtualOutputNodeMemoryReuse = 1;
  37. // One state per bit cannot be repeated
  38. enum ContinuousType { kTypeInput = 1, kTypeInputNoPadding = 2, kTypeOutput = 4, kTypeOutputNoPadding = 8 };
  39. int64_t GetSymbolOutputOffset(const std::map<std::string, std::string> &anchor_to_symbol,
  40. const std::map<std::string, std::list<ge::NodeIndexIO>> &symbol_to_anchors,
  41. const ge::NodePtr &node, const uint32_t i) {
  42. ge::NodeIndexIO cur_node_index_io(node, i, ge::kOut);
  43. auto iter1 = anchor_to_symbol.find(cur_node_index_io.ToString());
  44. if (iter1 == anchor_to_symbol.end()) {
  45. return ge::kInvalidOffset;
  46. }
  47. auto out_symbol = iter1->second;
  48. auto iter2 = symbol_to_anchors.find(out_symbol);
  49. if (iter2 == symbol_to_anchors.end()) {
  50. return ge::kInvalidOffset;
  51. }
  52. for (const auto &node_index_io : iter2->second) {
  53. if (node_index_io.value_ == out_symbol) {
  54. vector<int64_t> output_list = node->GetOpDesc()->GetOutputOffset();
  55. vector<int64_t> symbol_output_list = node_index_io.node_->GetOpDesc()->GetOutputOffset();
  56. if (node_index_io.index_ >= symbol_output_list.size()) {
  57. return ge::kInvalidOffset;
  58. }
  59. GELOGD("Node %s %uth output offset is %ld, Symbol %s output offset is %ld.", node->GetName().c_str(), i,
  60. output_list[i], iter2->first.c_str(), symbol_output_list.at(node_index_io.index_));
  61. return symbol_output_list.at(node_index_io.index_);
  62. }
  63. }
  64. return ge::kInvalidOffset;
  65. }
  66. } // namespace
  67. namespace ge {
  68. Status VariableMemoryAssigner::Assign() {
  69. Status result = ge::VarMemAssignUtil::AssignConstantOpMemory(compute_graph_);
  70. if (result != ge::SUCCESS) {
  71. return result;
  72. }
  73. result = ge::VarMemAssignUtil::AssignVarMemory(compute_graph_);
  74. if (result != ge::SUCCESS) {
  75. return result;
  76. }
  77. return ge::SUCCESS;
  78. }
  79. Status VariableMemoryAssigner::AssignVarAttr2Nodes() {
  80. Status result = ge::VarMemAssignUtil::AssignVarAttr2Nodes(compute_graph_);
  81. if (result != ge::SUCCESS) {
  82. return result;
  83. }
  84. return ge::SUCCESS;
  85. }
  86. Status VariableMemoryAssigner::AssignMemory2HasRefAttrNode() {
  87. Status result = ge::VarMemAssignUtil::AssignMemory2HasRefAttrNode(compute_graph_);
  88. if (result != ge::SUCCESS) {
  89. return result;
  90. }
  91. return ge::SUCCESS;
  92. }
  93. Status GraphMemoryAssigner::AssignMemory() {
  94. ge::HybridMemAssignerPtr mem_assigner(new(std::nothrow) HybridMemAssigner(compute_graph_));
  95. if (mem_assigner->Assign() != ge::SUCCESS) {
  96. GELOGE(ge::FAILED, "[Assign][GraphMem]graph_id:%u, graph_name:%s",
  97. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  98. return ge::FAILED;
  99. }
  100. MemoryOffset memory_offset(RT_MEMORY_HBM, mem_assigner->GetMemOffset());
  101. memory_offset_.emplace(RT_MEMORY_HBM, memory_offset);
  102. if (mem_assigner->GetP2PMemOffset() >= 0) {
  103. MemoryOffset p2p_memory_offset(RT_MEMORY_P2P_DDR, mem_assigner->GetP2PMemOffset());
  104. memory_offset_.emplace(RT_MEMORY_P2P_DDR, p2p_memory_offset);
  105. }
  106. auto session_id = compute_graph_->GetSessionID();
  107. int64_t var_size_before_assign = ge::VarManager::Instance(session_id)->GetVarMemSize(RT_MEMORY_HBM);
  108. auto variable_assigner =
  109. std::unique_ptr<ge::VariableMemoryAssigner>(new(std::nothrow) ge::VariableMemoryAssigner(compute_graph_));
  110. if (variable_assigner == nullptr) {
  111. GELOGE(ge::FAILED, "[New][Object:VariableMemoryAssigner]graph_id:%u, graph_name:%s",
  112. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  113. REPORT_CALL_ERROR("E19999", "New Object:VariableMemoryAssigner failed, "
  114. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  115. return ge::FAILED;
  116. }
  117. if (variable_assigner->Assign() != ge::SUCCESS) {
  118. return ge::FAILED;
  119. }
  120. int64_t var_size_assign = ge::VarManager::Instance(session_id)->GetVarMemSize(RT_MEMORY_HBM) - var_size_before_assign;
  121. GELOGD("GraphMemoryAssigner::AssignMemory variable size = %ld", var_size_assign);
  122. mem_assigner_ = std::move(mem_assigner);
  123. return ge::SUCCESS;
  124. }
  125. ge::Status GraphMemoryAssigner::AssignVarAttr2Nodes() {
  126. auto variable_assigner =
  127. std::unique_ptr<ge::VariableMemoryAssigner>(new(std::nothrow) ge::VariableMemoryAssigner(compute_graph_));
  128. if (variable_assigner == nullptr) {
  129. GELOGE(ge::FAILED, "[New][Object:VariableMemoryAssigner]graph_id:%u, graph_name:%s",
  130. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  131. REPORT_CALL_ERROR("E19999", "New Object:VariableMemoryAssigner failed, "
  132. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  133. return ge::FAILED;
  134. }
  135. if (variable_assigner->AssignVarAttr2Nodes() != ge::SUCCESS) {
  136. return ge::FAILED;
  137. }
  138. return ge::SUCCESS;
  139. }
  140. ge::Status GraphMemoryAssigner::AssignMemory2HasRefAttrNode() {
  141. auto variable_assigner =
  142. std::unique_ptr<ge::VariableMemoryAssigner>(new(std::nothrow) ge::VariableMemoryAssigner(compute_graph_));
  143. if (variable_assigner == nullptr) {
  144. GELOGE(ge::FAILED, "[New][Object:VariableMemoryAssigner]graph_id:%u, graph_name:%s",
  145. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  146. REPORT_CALL_ERROR("E19999", "New Object:VariableMemoryAssigner failed, "
  147. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  148. }
  149. if (variable_assigner->AssignMemory2HasRefAttrNode() != ge::SUCCESS) {
  150. return ge::FAILED;
  151. }
  152. return ge::SUCCESS;
  153. }
  154. ge::Status CalculateTensorRealSizeAndOutSize(const ge::ConstGeTensorDescPtr &output_desc,
  155. int64_t dim_index, int64_t &output_mem_size,
  156. int64_t &batch_dim_num, int64_t &out_size) {
  157. graphStatus graph_status = ge::TensorUtils::GetSize(*output_desc, out_size);
  158. if (graph_status != GRAPH_SUCCESS) {
  159. GELOGE(FAILED, "[Get][TensorSize]");
  160. REPORT_CALL_ERROR("E19999", "Get tensor size failed");
  161. return FAILED;
  162. }
  163. GeShape output_shape = output_desc->GetShape();
  164. std::vector<int64_t> output_dims = output_shape.GetDims();
  165. if (dim_index >= static_cast<int64_t>(output_dims.size())) {
  166. REPORT_INNER_ERROR("E19999", "Inner param dim_index value:%ld invalid, bigger than dim size:%lu in shape:%s",
  167. dim_index, output_dims.size(), output_shape.ToString().c_str());
  168. GELOGE(FAILED, "[Check][Param:dim_index]value:%ld invalid, bigger than dim size:%lu in shape:%s",
  169. dim_index, output_dims.size(), output_shape.ToString().c_str());
  170. return FAILED;
  171. }
  172. for (int64_t index = 0; index < dim_index; index++) {
  173. FMK_INT64_MULCHECK(batch_dim_num, output_dims[index]);
  174. batch_dim_num *= output_dims[index];
  175. output_dims[index] = 1;
  176. }
  177. output_shape = GeShape(output_dims);
  178. Format out_format = output_desc->GetFormat();
  179. DataType data_type = output_desc->GetDataType();
  180. graph_status = ge::TensorUtils::CalcTensorMemSize(output_shape, out_format, data_type, output_mem_size);
  181. if (graph_status != GRAPH_SUCCESS) {
  182. GELOGE(graph_status, "[Calc][TensorSize]");
  183. return FAILED;
  184. }
  185. if (output_mem_size < 0) {
  186. REPORT_INNER_ERROR("E19999", "After calculating, tensor memory size:%ld invalid, less than 0. "
  187. "shape:%s, format:%s, dtype:%s, maybe has dynamic shape",
  188. output_mem_size,
  189. output_shape.ToString().c_str(),
  190. TypeUtils::FormatToSerialString(out_format).c_str(),
  191. TypeUtils::DataTypeToSerialString(data_type).c_str());
  192. GELOGE(FAILED, "[Check][TensorSize]value:%ld invalid after calc, less than 0. shape:%s, format:%s, dtype:%s, "
  193. "maybe has dynamic shape",
  194. output_mem_size,
  195. output_shape.ToString().c_str(),
  196. TypeUtils::FormatToSerialString(out_format).c_str(),
  197. TypeUtils::DataTypeToSerialString(data_type).c_str());
  198. return FAILED;
  199. }
  200. return SUCCESS;
  201. }
  202. Status GraphMemoryAssigner::ReAssignMemory(bool is_loop_graph, map<int64_t, size_t> &mem_type_to_offset) {
  203. if (memory_offset_.empty()) {
  204. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ empty, not expected, graph_id:%u, graph_name:%s",
  205. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  206. GELOGE(FAILED, "[Check][InnerData:memory_offset_]empty is not expected, "
  207. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  208. return ge::FAILED;
  209. }
  210. GE_CHK_STATUS_RET(ReAssignContinuousMemory(is_loop_graph), "ReAssignContinuousMemory Failed!");
  211. GE_CHK_STATUS_RET(ReAssignAtomicMemory(is_loop_graph), "ReAssignAtomicMemory Failed!");
  212. GE_CHK_STATUS_RET(AssignBufferPoolMemory(), "AssignBufferPoolMemory Failed!");
  213. size_t total_mem_offset = 0;
  214. for (auto pair : memory_offset_) {
  215. mem_type_to_offset[pair.first] = pair.second.mem_offset_;
  216. total_mem_offset += pair.second.mem_offset_;
  217. }
  218. auto session_id = compute_graph_->GetSessionID();
  219. if (total_mem_offset > VarManager::Instance(session_id)->GetGraphMemoryMaxSize()) {
  220. GELOGE(ge::FAILED, "[Check][TotalMemOffset] %zu is greater than memory manager malloc max size %zu, "
  221. "graph_id:%u, graph_name:%s, reduce your batchsize or scale your model may solve problem",
  222. total_mem_offset, VarManager::Instance(session_id)->GetGraphMemoryMaxSize(),
  223. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  224. for (auto iter : mem_type_to_offset) {
  225. ErrorManager::GetInstance().ATCReportErrMessage("E19022", {"memType", "size", "item", "maxsize"},
  226. {std::to_string(iter.first), std::to_string(iter.second), "featuremap",
  227. std::to_string(VarManager::Instance(session_id)->GetGraphMemoryMaxSize())});
  228. GEEVENT("[IMAS]AfterAssignMemory : %s memoffset[%zu], memtype[%ld]", compute_graph_->GetName().c_str(),
  229. iter.second, iter.first);
  230. }
  231. return ge::FAILED;
  232. }
  233. return SUCCESS;
  234. }
  235. Status GraphMemoryAssigner::AssignZeroCopyMemory(map<int64_t, size_t> &mem_offset, size_t &zero_mem_copy_size) {
  236. BlockMemAssignerPtr priority_assigner = std::move(mem_assigner_->GetPriorityAssinger());
  237. if (priority_assigner == nullptr) {
  238. REPORT_INNER_ERROR("E19999", "InnerData priority_assigner nullptr, not expected, graph_id:%u, graph_name:%s",
  239. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  240. GELOGE(FAILED, "[Check][InnerData:priority_assigner]nullptr is invalid, "
  241. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  242. return ge::FAILED;
  243. }
  244. size_t mem_offset_tmp = mem_offset[RT_MEMORY_HBM];
  245. // set offset for zero copy block
  246. for (auto &memory_block : priority_assigner->GetMemoryBlocks()) {
  247. if (memory_block == nullptr || memory_block->deleted_block_ || !memory_block->is_zero_copy_) {
  248. continue;
  249. }
  250. memory_block->Resize();
  251. memory_block->SetHeadOffset(mem_offset[RT_MEMORY_HBM]);
  252. mem_offset[RT_MEMORY_HBM] += memory_block->Size();
  253. memory_block->SetTailOffset(mem_offset[RT_MEMORY_HBM] - 1);
  254. }
  255. // set offset for zero copy nodes
  256. priority_assigner->SetOpMemOffset(true);
  257. zero_mem_copy_size = mem_offset[RT_MEMORY_HBM] - mem_offset_tmp;
  258. auto iter = memory_offset_.find(RT_MEMORY_HBM);
  259. if (iter == memory_offset_.end()) {
  260. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ does not have type[HBM], not expected, "
  261. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  262. GELOGE(FAILED, "[Check][InnerData]memory_offset_ does not have memory type[HBM]"
  263. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  264. return FAILED;
  265. }
  266. iter->second.mem_offset_ = mem_offset[RT_MEMORY_HBM];
  267. GELOGD("max_mem_offset:%zu, mem_offset:%zu, zero_mem_copy_size:%zu.", mem_offset[RT_MEMORY_HBM], mem_offset_tmp,
  268. zero_mem_copy_size);
  269. return SUCCESS;
  270. }
  271. uint32_t GetContinuousMemoryType(const OpDescPtr &op_desc) {
  272. if (op_desc == nullptr) {
  273. return 0;
  274. };
  275. bool is_continuous = false;
  276. uint32_t continuous_type = 0;
  277. // If GetBool fail, is_continuous is false.
  278. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_INPUT, is_continuous);
  279. if (is_continuous) {
  280. continuous_type |= kTypeInput;
  281. } else {
  282. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_INPUT, is_continuous);
  283. if (is_continuous) {
  284. bool attr_reuse = false;
  285. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_OUTPUT_REUSE_INPUT, attr_reuse);
  286. if (attr_reuse) {
  287. continuous_type |= kTypeInputNoPadding;
  288. }
  289. }
  290. }
  291. is_continuous = false;
  292. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_OUTPUT, is_continuous);
  293. if (is_continuous) {
  294. continuous_type |= kTypeOutput;
  295. } else {
  296. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_OUTPUT, is_continuous);
  297. if (is_continuous) {
  298. bool attr_reuse = false;
  299. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_OUTPUT_REUSE_INPUT, attr_reuse);
  300. if (attr_reuse) {
  301. continuous_type |= kTypeOutputNoPadding;
  302. }
  303. }
  304. }
  305. if (continuous_type != 0) {
  306. GELOGI("[Get][MemType:Continuous]Current node %s, value is %d", op_desc->GetName().c_str(), continuous_type);
  307. }
  308. return continuous_type;
  309. }
  310. Status GetMemorySize(const OpDescPtr &op_desc, const ge::ConstGeTensorDescPtr &output_desc, uint32_t continuous_type,
  311. int64_t &tensor_size, int64_t &nopadding_size) {
  312. if ((op_desc == nullptr) || (output_desc == nullptr)) {
  313. REPORT_INNER_ERROR("E19999", "InnerData param op_desc or output_desc is nullptr, not expected");
  314. GELOGE(FAILED, "[Check][Param]op_desc or output_desc is nullptr");
  315. }
  316. tensor_size = 0;
  317. nopadding_size = 0;
  318. bool is_nopadding = ((continuous_type & kTypeInputNoPadding) != 0) || ((continuous_type & kTypeOutputNoPadding) != 0);
  319. if (is_nopadding) {
  320. int64_t attr_dim_index;
  321. bool get_attr_dim_flag = ge::AttrUtils::GetInt(op_desc, ATTR_NAME_REUSE_INPUT_ON_DIM_INDEX, attr_dim_index);
  322. if (!get_attr_dim_flag) {
  323. REPORT_INNER_ERROR("E19999", "Get Attr:%s failed, op_name:%s",
  324. ATTR_NAME_REUSE_INPUT_ON_DIM_INDEX.c_str(), op_desc->GetName().c_str());
  325. GELOGE(FAILED, "[Get][Attr:%s]fail for op_name:%s",
  326. ATTR_NAME_REUSE_INPUT_ON_DIM_INDEX.c_str(), op_desc->GetName().c_str());
  327. return FAILED;
  328. }
  329. // Calculate tensor real size of each piece of data and out size of complete data
  330. int64_t batch_dim_num = 1;
  331. if (CalculateTensorRealSizeAndOutSize(output_desc, attr_dim_index, nopadding_size, batch_dim_num, tensor_size) !=
  332. SUCCESS) {
  333. REPORT_CALL_ERROR("E19999", "CalculateTensorRealSizeAndOutSize failed, attr_dim_index:%ld, op_name:%s",
  334. attr_dim_index, op_desc->GetName().c_str());
  335. GELOGE(FAILED, "[Calculate][NopaddingSize]failed for node %s, attr_dim_index:%ld",
  336. op_desc->GetName().c_str(), attr_dim_index);
  337. return FAILED;
  338. }
  339. } else {
  340. if (ge::TensorUtils::GetSize(*output_desc, tensor_size) != ge::SUCCESS) {
  341. REPORT_INNER_ERROR("E19999", "Get Tensor Size failed, op_name:%s", op_desc->GetName().c_str());
  342. GELOGE(FAILED, "[Get][TensorSize]failed in padding case, op_name:%s", op_desc->GetName().c_str());
  343. return FAILED;
  344. }
  345. }
  346. if ((tensor_size < 0) || (nopadding_size < 0)) {
  347. REPORT_INNER_ERROR("E19999", "GetMemorySize fail, "
  348. "tensor_size:%ld or nopadding_size:%ld less than 0, invalid, op_name:%s",
  349. tensor_size, nopadding_size, op_desc->GetName().c_str());
  350. GELOGE(FAILED, "[Get][MemorySize]tensor_size:%ld or nopadding_size:%ld less than 0, invalid, op_name:%s",
  351. tensor_size, nopadding_size, op_desc->GetName().c_str());
  352. return FAILED;
  353. }
  354. return SUCCESS;
  355. }
  356. void AlignMemOffset(int64_t &mem_align_size) {
  357. if (mem_align_size <= 0) {
  358. return;
  359. }
  360. mem_align_size = (mem_align_size + MEM_ALIGN_SIZE - 1) / MEM_ALIGN_SIZE * MEM_ALIGN_SIZE;
  361. }
  362. bool IsContinuousInputConflict(const ge::NodePtr &node, const OpDescPtr &peer_op_desc) {
  363. bool is_peer_output_continuous = false;
  364. // If GetBool fail, is_peer_output_continuous is false.
  365. (void) ge::AttrUtils::GetBool(peer_op_desc, ATTR_NAME_CONTINUOUS_OUTPUT, is_peer_output_continuous);
  366. // Get peer node output size, if size == 1(peer node has only one output), continuous input of the node and
  367. // continuous output of the previous node is the same, we can support it. If size != 1, there may be
  368. // conflict between the two, we can not support it.
  369. auto peer_output_size = peer_op_desc->GetOutputsSize();
  370. GE_IF_BOOL_EXEC(is_peer_output_continuous && (peer_output_size != 1),
  371. std::string error = "Current op" + FmtToStr(node->GetOpDesc()->GetName()) +
  372. " requires continuous input, while the previous op" + FmtToStr(peer_op_desc->GetName()) +
  373. " requires continuous output. There may be conflict between the two." +
  374. "This node is not supported now.";
  375. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  376. return true;);
  377. bool is_peer_reference = false;
  378. // If GetBool fail, is_peer_reference is false.
  379. (void) AttrUtils::GetBool(peer_op_desc, ATTR_NAME_REFERENCE, is_peer_reference);
  380. GE_IF_BOOL_EXEC(is_peer_reference,
  381. std::string warning = "[Check][Continuous]Current op" + FmtToStr(node->GetOpDesc()->GetName()) +
  382. " requires continuous input, while the previous op" + FmtToStr(peer_op_desc->GetName()) +
  383. " is ref. There may be conflict between the two.";
  384. GELOGW("%s", warning.c_str());
  385. return false;);
  386. return false;
  387. }
  388. /// op1 -> node -> op2
  389. /// return true when node is ref from input, and op1 or op2 is reuse input from output
  390. bool GraphMemoryAssigner::IsRefFromInputOpCascade(const NodePtr &node) {
  391. bool ref_from_input = false;
  392. int32_t reuse_in_index = -1;
  393. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  394. ref_from_input = GraphUtils::IsRefFromInput(out_anchor, reuse_in_index);
  395. if (ref_from_input) {
  396. GELOGD("IsRefFromInputOpCascade: cur node:%s:%d is ref", node->GetName().c_str(), reuse_in_index);
  397. break;
  398. }
  399. }
  400. for (const auto &in_anchor : node->GetAllInDataAnchors()) {
  401. const auto &peer_out_anchor = in_anchor->GetPeerOutAnchor();
  402. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  403. if (ref_from_input && GraphUtils::IsRefFromInput(peer_out_anchor, reuse_in_index)) {
  404. GELOGD("IsRefFromInputOpCascade: in node[%s] is ref, reuse index is:%d",
  405. peer_out_anchor->GetOwnerNode()->GetName().c_str(), reuse_in_index);
  406. return true;
  407. }
  408. }
  409. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  410. const auto &peer_in_anchors = out_anchor->GetPeerInDataAnchors();
  411. for (const auto &peer_in_anchor : peer_in_anchors) {
  412. auto peer_in_node = peer_in_anchor->GetOwnerNode();
  413. GE_IF_BOOL_EXEC(peer_in_node == nullptr, continue);
  414. for (const auto &peer_in_node_out_anchor : peer_in_node->GetAllOutDataAnchors()) {
  415. if (ref_from_input && GraphUtils::IsRefFromInput(peer_in_node_out_anchor, reuse_in_index)) {
  416. GELOGD("IsRefFromInputOpCascade: out node[%s] is ref, reuse index is:%d",
  417. peer_in_node_out_anchor->GetOwnerNode()->GetName().c_str(), reuse_in_index);
  418. return true;
  419. }
  420. }
  421. }
  422. }
  423. return false;
  424. }
  425. /// node:in0(in0 reuse out0) -> peer_node:out0
  426. /// update peer_node's 0th output offset with node's 0th output offset
  427. Status GraphMemoryAssigner::UpdateRefOpOffsetReverse(const NodePtr &node) {
  428. map<int32_t, int32_t> out2ins;
  429. GE_CHK_STATUS_RET(TryGetNodeRefIndexes(node, out2ins), "[Get][RefIndexes]fail for node:%s",
  430. node->GetName().c_str());
  431. auto op_desc = node->GetOpDesc();
  432. GE_CHECK_NOTNULL(op_desc);
  433. vector<int64_t> output_list = op_desc->GetOutputOffset();
  434. for (const auto &out2in : out2ins) {
  435. auto reuse_in_anchor = node->GetInDataAnchor(out2in.second);
  436. GE_CHECK_NOTNULL(reuse_in_anchor);
  437. auto peer_out_anchor = reuse_in_anchor->GetPeerOutAnchor();
  438. GE_CHECK_NOTNULL(peer_out_anchor);
  439. auto peer_node = peer_out_anchor->GetOwnerNode();
  440. GE_CHECK_NOTNULL(peer_node);
  441. auto peer_op_desc = peer_node->GetOpDesc();
  442. GE_CHECK_NOTNULL(peer_op_desc);
  443. vector<int64_t> peer_output_list = peer_op_desc->GetOutputOffset();
  444. if ((peer_out_anchor->GetIdx() >= static_cast<int>(peer_output_list.size()))
  445. || (out2in.first >= static_cast<int32_t>(output_list.size()))) {
  446. GELOGW("out of range, peer_out_anchor:%d, peer_output_list size:%zu, out2in:%d, output_list size:%zu",
  447. peer_out_anchor->GetIdx(),
  448. peer_output_list.size(),
  449. out2in.first,
  450. output_list.size());
  451. continue;
  452. }
  453. peer_output_list.at(peer_out_anchor->GetIdx()) = output_list.at(out2in.first);
  454. peer_op_desc->SetOutputOffset(peer_output_list);
  455. GELOGD("UpdateRefOpOffsetReverse: Node[%s] output[%d] is set from node[%s] output index[%d] offset[%ld]",
  456. peer_node->GetName().c_str(),
  457. peer_out_anchor->GetIdx(),
  458. node->GetName().c_str(),
  459. out2in.first,
  460. output_list.at(out2in.first));
  461. }
  462. return SUCCESS;
  463. }
  464. Status GraphMemoryAssigner::ReAssignContinuousMemory(bool is_loop_graph) {
  465. Status ret;
  466. // Stored nodes which need assign continuous input memory in `reverse topo order`
  467. std::vector<NodePtr> nodes_stack;
  468. std::map<NodePtr, uint32_t> node_2_continuous_type;
  469. // Traverse nodes
  470. for (auto &node : compute_graph_->GetAllNodes()) {
  471. GE_CHECK_NOTNULL(node);
  472. uint32_t continuous_type;
  473. auto iter = node_2_continuous_type.find(node);
  474. if (iter == node_2_continuous_type.end()) {
  475. continuous_type = GetContinuousMemoryType(node->GetOpDesc());
  476. node_2_continuous_type.emplace(node, continuous_type);
  477. } else {
  478. continuous_type = iter->second;
  479. }
  480. // Assign continuous input memory
  481. bool continuous_input = ((continuous_type & kTypeInput) != 0) || ((continuous_type & kTypeInputNoPadding) != 0);
  482. if (IsRefFromInputOpCascade(node)) {
  483. nodes_stack.push_back(node);
  484. GELOGD("Ref: Push node:%s to stack", node->GetName().c_str());
  485. } else if (continuous_input) {
  486. if (AssignContinuousInputMemoryWithAtomicProcessDirectly(node, node_2_continuous_type)) {
  487. GE_CHK_STATUS_RET(AssignContinuousInputMemoryWithAtomicProcess(node, continuous_type),
  488. "[Assign][Memory:Continuous:Input]fail for node:%s", node->GetName().c_str())
  489. } else {
  490. nodes_stack.push_back(node);
  491. GELOGD("Continuous: Push node:%s to stack", node->GetName().c_str());
  492. }
  493. }
  494. // Assign continuous output memory
  495. int64_t memory_type = RT_MEMORY_HBM;
  496. bool continuous_output = ((continuous_type & kTypeOutput) != 0) || ((continuous_type & kTypeOutputNoPadding) != 0);
  497. if (continuous_output) {
  498. GE_CHK_STATUS_RET(GetNodeMemoryType(node, memory_type, "output"),
  499. "[Get][MemType]fail for node:%s", node->GetName().c_str());
  500. ret = AssignContinuousOutputMemory(node, memory_type, continuous_type);
  501. if (ret != ge::SUCCESS) {
  502. GELOGE(ret, "[Assign][Memory:Continuous:Ouput]fail for node:%s", node->GetName().c_str());
  503. return ret;
  504. }
  505. }
  506. }
  507. // Assign continuous input memory in `reverse topo order` which stored before
  508. while (!nodes_stack.empty()){
  509. auto node = nodes_stack.back();
  510. nodes_stack.pop_back();
  511. auto iter = node_2_continuous_type.find(node);
  512. if (iter == node_2_continuous_type.end()) {
  513. REPORT_INNER_ERROR("E19999", "Get ContinuousType from node_2_continuous_type map failed for node:%s",
  514. node->GetName().c_str());
  515. GELOGE(FAILED, "[Get][ContinuousType] find fail for node:%s", node->GetName().c_str());
  516. return FAILED;
  517. }
  518. if (((iter->second & kTypeInput) != 0) || ((iter->second & kTypeInputNoPadding) != 0)) {
  519. GE_CHK_STATUS_RET(AssignContinuousInputMemoryWithAtomicProcess(node, iter->second, true),
  520. "[Assign][Memory:Continuous:Input]fail for node:%s.", node->GetName().c_str())
  521. } else {
  522. GE_CHK_STATUS_RET(UpdateRefOpOffsetReverse(node),
  523. "[Update][Memory:Reference:Output]fail for node:%s", node->GetName().c_str())
  524. }
  525. }
  526. for (auto pair : memory_offset_) {
  527. GELOGD("[Reassign][Memory:Continuous]At last, memory type = %ld, mem offset = %zu", pair.first,
  528. pair.second.mem_offset_);
  529. }
  530. return ge::SUCCESS;
  531. }
  532. Status GraphMemoryAssigner::AssignContinuousInputMemory(const ge::NodePtr &node, int64_t &continuous_mem_start,
  533. int64_t &continuous_mem_size, int64_t memory_type, uint32_t continuous_type, bool reverse_refresh) {
  534. GELOGI("[Assign][Memory:Input:Continuous]start for Current node %s", node->GetName().c_str());
  535. auto iter = memory_offset_.find(memory_type);
  536. if (iter == memory_offset_.end()) {
  537. REPORT_INNER_ERROR("E19999", "find memory offset fail for mem_type:%ld, "
  538. "for node:%s, ", memory_type, node->GetName().c_str());
  539. GELOGE(FAILED, "[Find][MemOffset]fail for mem_type:%ld, when AssignContinuousInputMemory for node:%s",
  540. memory_type, node->GetName().c_str());
  541. return FAILED;
  542. }
  543. // The head and tail of hcom continuous input should be added 512
  544. iter->second.mem_offset_ += MEM_ALIGN_SIZE;
  545. continuous_mem_start = iter->second.mem_offset_;
  546. int64_t mem_offset = iter->second.mem_offset_;
  547. int64_t extra_memory_size = 0;
  548. bool is_continuous_input_allocated = false;
  549. auto op_desc = node->GetOpDesc();
  550. GE_CHECK_NOTNULL(op_desc);
  551. vector<int64_t> output_list_this = op_desc->GetOutputOffset();
  552. if (output_list_this.empty()) {
  553. REPORT_INNER_ERROR("E19999", "No output offset in node :%s, not expected",
  554. node->GetName().c_str());
  555. GELOGE(FAILED, "[Get][OutputOffset] empty is invalid, node:%s", node->GetName().c_str());
  556. return FAILED;
  557. }
  558. (void) ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_INPUT_ALLOC, is_continuous_input_allocated);
  559. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  560. GE_IF_BOOL_EXEC(in_data_anchor == nullptr, continue);
  561. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  562. GE_IF_BOOL_EXEC(peer_out_data_anchor == nullptr, continue);
  563. auto peer_op_desc = peer_out_data_anchor->GetOwnerNode()->GetOpDesc();
  564. GE_IF_BOOL_EXEC(peer_op_desc == nullptr, continue);
  565. GE_IF_BOOL_EXEC(IsContinuousInputConflict(node, peer_op_desc), return PARAM_INVALID;);
  566. int64_t tensor_desc_size = 0;
  567. int64_t nopadding_size = 0;
  568. int64_t real_size = 0;
  569. std::vector<int64_t> offsets_of_fusion = {};
  570. bool lx_fusion = AttrUtils::GetListInt(peer_op_desc, ATTR_NAME_OUTPUT_OFFSET_FOR_BUFFER_FUSION, offsets_of_fusion);
  571. lx_fusion = lx_fusion && !offsets_of_fusion.empty();
  572. if (lx_fusion) {
  573. if (peer_out_data_anchor->GetIdx() >= static_cast<int>(offsets_of_fusion.size())) {
  574. std::string error = "fusion: peer node:" + FmtToStr(peer_op_desc->GetName()) +
  575. " anchor_index:" + FmtToStr(peer_out_data_anchor->GetIdx()) +
  576. " is out of range:" + FmtToStr(offsets_of_fusion.size());
  577. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  578. return FAILED;
  579. }
  580. nopadding_size = offsets_of_fusion[peer_out_data_anchor->GetIdx()];
  581. tensor_desc_size = nopadding_size;
  582. } else {
  583. if (GetMemorySize(node->GetOpDesc(), peer_op_desc->GetOutputDescPtr(peer_out_data_anchor->GetIdx()),
  584. continuous_type, tensor_desc_size, nopadding_size) != ge::SUCCESS) {
  585. return FAILED;
  586. }
  587. }
  588. bool is_nopadding = ((continuous_type & kTypeInputNoPadding) != 0) || lx_fusion;
  589. vector<int64_t> output_list = peer_op_desc->GetOutputOffset();
  590. if (peer_out_data_anchor->GetIdx() >= static_cast<int>(output_list.size())) {
  591. std::string error = "peer node:" + FmtToStr(peer_op_desc->GetName()) +
  592. " anchor_index:" + FmtToStr(peer_out_data_anchor->GetIdx()) +
  593. " is out of range:" + FmtToStr(output_list.size());
  594. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  595. return FAILED;
  596. }
  597. // when continuous input has been allocated first input is beginning offset
  598. bool is_allocated_first_input = is_continuous_input_allocated && (in_data_anchor->GetIdx() == 0);
  599. if (is_allocated_first_input) {
  600. std::map<int32_t, int32_t> out2ins;
  601. GE_CHK_STATUS_RET(TryGetNodeRefIndexes(node, out2ins), "[Get][RefIndexes]fail for node: %s", node->GetName().c_str());
  602. // output is beginning offset, set offset for input; only support this case now
  603. if ((out2ins.size() == 1) && (out2ins.begin()->second == 0) && (reverse_refresh)) {
  604. auto peer_output_offset = output_list.at(peer_out_data_anchor->GetIdx());
  605. output_list.at(peer_out_data_anchor->GetIdx()) = output_list_this.at(out2ins.begin()->first);
  606. peer_op_desc->SetOutputOffset(output_list);
  607. GELOGI("[Update][Offset]Node %s out %d ref in %d input node %s, use output offset %ld update %ld",
  608. node->GetName().c_str(), out2ins.begin()->first, out2ins.begin()->second,
  609. peer_op_desc->GetName().c_str(), output_list_this.at(out2ins.begin()->first), peer_output_offset);
  610. } else {
  611. GELOGD("Node %s out %d ref in %d input node %s with total ref numbers %zu.", node->GetName().c_str(),
  612. out2ins.begin()->first, out2ins.begin()->second, peer_op_desc->GetName().c_str(), out2ins.size());
  613. }
  614. // first input is beginning offset
  615. mem_offset = output_list.at(peer_out_data_anchor->GetIdx());
  616. continuous_mem_start = output_list.at(peer_out_data_anchor->GetIdx());
  617. } else {
  618. // set offset for input
  619. output_list.at(peer_out_data_anchor->GetIdx()) = mem_offset;
  620. peer_op_desc->SetOutputOffset(output_list);
  621. }
  622. int64_t align_size = tensor_desc_size;
  623. if (is_nopadding) {
  624. mem_offset += nopadding_size;
  625. extra_memory_size += (tensor_desc_size - nopadding_size);
  626. real_size = nopadding_size;
  627. } else {
  628. ge::AlignMemOffset(align_size);
  629. mem_offset += align_size;
  630. // The head and tail of hcom continuous input should be added 512
  631. extra_memory_size = MEM_ALIGN_SIZE;
  632. real_size = tensor_desc_size;
  633. }
  634. GELOGI("[IMAS]Continuous input : Set %s name[%s] optype[%s] output[%d] offset to [%zu] stream_id[%ld] memtype[%ld] "
  635. "size[%zu] realsize[%ld] nopadding size[%d]", node->GetOwnerComputeGraph()->GetName().c_str(),
  636. peer_op_desc->GetName().c_str(), node->GetType().c_str(), peer_out_data_anchor->GetIdx(),
  637. output_list.at(peer_out_data_anchor->GetIdx()), peer_op_desc->GetStreamId(), memory_type,
  638. is_continuous_input_allocated ? 0UL : align_size, real_size, is_nopadding);
  639. }
  640. mem_offset += extra_memory_size;
  641. ge::AlignMemOffset(mem_offset);
  642. continuous_mem_size = mem_offset - continuous_mem_start;
  643. if (is_continuous_input_allocated) {
  644. // not allocate memory here, so no need add 512 in header
  645. iter->second.mem_offset_ -= MEM_ALIGN_SIZE;
  646. } else {
  647. iter->second.mem_offset_ = mem_offset;
  648. }
  649. return SUCCESS;
  650. }
  651. Status GetFirstInputPeerOutOutputOffset(const ge::NodePtr &node, int64_t &mem_offset) {
  652. auto in_data_anchor_list = node->GetAllInDataAnchors();
  653. if (in_data_anchor_list.empty()) {
  654. REPORT_INNER_ERROR("E19999", "InAnchor list empty in node:%s, not expect",
  655. node->GetName().c_str());
  656. GELOGE(FAILED, "[Get][InAnchor]empty is invalid, node:%s", node->GetName().c_str());
  657. return FAILED;
  658. }
  659. auto peer_out_data_anchor = in_data_anchor_list.at(0)->GetPeerOutAnchor();
  660. GE_IF_BOOL_EXEC(peer_out_data_anchor == nullptr,
  661. REPORT_INNER_ERROR("E19999", "PeerAcnhor is null, not expect for node:%s",
  662. node->GetName().c_str());
  663. GELOGE(ge::FAILED, "[Check][PeerAnchor]null is invalid, node:%s", node->GetName().c_str());
  664. return ge::FAILED);
  665. auto peer_op_desc = peer_out_data_anchor->GetOwnerNode()->GetOpDesc();
  666. GE_IF_BOOL_EXEC(peer_op_desc == nullptr,
  667. REPORT_INNER_ERROR("E19999", "PeerOpDesc is null, not expect for node:%s",
  668. node->GetName().c_str());
  669. GELOGE(ge::FAILED, "[Check][PeerOpDesc]null is invalid, node:%s", node->GetName().c_str());
  670. return ge::FAILED);
  671. vector<int64_t> in_node_output_offsets = peer_op_desc->GetOutputOffset();
  672. if (peer_out_data_anchor->GetIdx() >= static_cast<int>(in_node_output_offsets.size())) {
  673. REPORT_INNER_ERROR("E19999", "PeerAnchorIndex:%d bigger than in_offset size:%lu, judge invalid for node:%s",
  674. peer_out_data_anchor->GetIdx(), in_node_output_offsets.size(), node->GetName().c_str());
  675. GELOGE(FAILED, "[Check][Index:PeerOutDataAnchor]PeerIndex:%d bigger than in_offset size:%lu, node:%s",
  676. peer_out_data_anchor->GetIdx(), in_node_output_offsets.size(), node->GetName().c_str());
  677. return FAILED;
  678. }
  679. mem_offset = in_node_output_offsets.at(peer_out_data_anchor->GetIdx());
  680. return SUCCESS;
  681. }
  682. Status GraphMemoryAssigner::AssignContinuousOutputMemory(const ge::NodePtr &node, int64_t memory_type,
  683. uint32_t continuous_type) {
  684. GELOGI("Current node %s needs continuous output.", node->GetName().c_str());
  685. auto out_op_desc = node->GetOpDesc();
  686. GE_IF_BOOL_EXEC(out_op_desc == nullptr,
  687. REPORT_INNER_ERROR("E19999", "OpDesc is null, not expect for node:%s",
  688. node->GetName().c_str());
  689. GELOGE(ge::FAILED, "[Check][OpDesc]null is invalid, node:%s", node->GetName().c_str()));
  690. vector<int64_t> output_list = out_op_desc->GetOutputOffset();
  691. if ((out_op_desc->GetOutputsSize() > output_list.size()) || (output_list.size() == 0)) {
  692. REPORT_INNER_ERROR("E19999", "Output size:%zu more than output offset size:%zu, invalid in node:%s",
  693. out_op_desc->GetOutputsSize(), output_list.size(), node->GetName().c_str());
  694. GELOGE(ge::FAILED, "[Check][InnerData]Output size:%zu more than output offset size:%zu, invalid in node:%s",
  695. out_op_desc->GetOutputsSize(), output_list.size(), node->GetName().c_str());
  696. return ge::FAILED;
  697. }
  698. int64_t mem_offset = 0;
  699. bool is_nopadding = ((continuous_type & kTypeOutputNoPadding) != 0);
  700. if (is_nopadding) {
  701. // out tensor memory must be reused input tensor memory
  702. if (GetFirstInputPeerOutOutputOffset(node, mem_offset) != SUCCESS) {
  703. return ge::FAILED;
  704. }
  705. } else {
  706. // Get the reference type of the node, default is false
  707. bool is_ref = false;
  708. // If GetBool fail, is_ref is false.
  709. (void) ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_REFERENCE, is_ref);
  710. // If the output is ref type and refers to the ref of an input, the name of the output
  711. // and the input are the same. Ge encounters ref type, finds matching relationship according
  712. // to the names of input and output, and allocates the same memory address, eg: HCOMBroadcast
  713. if (is_ref) {
  714. GELOGI("Current node %s no needs assign continuous output because reference input by name.",
  715. node->GetName().c_str());
  716. return SUCCESS;
  717. }
  718. mem_offset = output_list[0];
  719. }
  720. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  721. output_list[out_data_anchor->GetIdx()] = mem_offset;
  722. int64_t tensor_desc_size = 0;
  723. int64_t nopadding_size = 0;
  724. if (GetMemorySize(out_op_desc, out_op_desc->GetOutputDescPtr(out_data_anchor->GetIdx()), continuous_type,
  725. tensor_desc_size, nopadding_size) != ge::SUCCESS) {
  726. return FAILED;
  727. }
  728. if (is_nopadding) {
  729. mem_offset += nopadding_size;
  730. } else {
  731. mem_offset += tensor_desc_size;
  732. ge::AlignMemOffset(mem_offset);
  733. }
  734. GELOGI("[IMAS]Continuous output : Set %s name[%s] optype[%s] output[%d] offset to [%zu] stream_id[%ld] memtype[%ld]"
  735. " size[%zu] realsize[%ld] nopadding[%d].", node->GetOwnerComputeGraph()->GetName().c_str(),
  736. out_op_desc->GetName().c_str(), node->GetType().c_str(), out_data_anchor->GetIdx(),
  737. output_list[out_data_anchor->GetIdx()], out_op_desc->GetStreamId(), memory_type, 0UL,
  738. is_nopadding ? nopadding_size : tensor_desc_size, is_nopadding);
  739. }
  740. out_op_desc->SetOutputOffset(output_list);
  741. return ge::SUCCESS;
  742. }
  743. Status GraphMemoryAssigner::ReAssignAtomicMemory(bool is_loop_graph) {
  744. // key:dynamic batch, batch name
  745. map<string, map<NodePtr, vector<NodePtr>>> normal_atomic_and_clean_nodes_map;
  746. map<string, vector<NodePtr>> connecting_output_atomic_nodes;
  747. Status status = FilterAtomicNodesForMemoryAssign(normal_atomic_and_clean_nodes_map, connecting_output_atomic_nodes);
  748. if (status != SUCCESS) {
  749. GELOGE(status, "[Filter][AtomicNode]failed in graph_id:%u, graph_name:%s",
  750. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  751. return status;
  752. }
  753. auto mem_iter = memory_offset_.find(RT_MEMORY_HBM);
  754. if (mem_iter == memory_offset_.end()) {
  755. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ does not have type[HBM], not expected, "
  756. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  757. GELOGE(FAILED, "[Check][InnerData]memory_offset_ does not have memory type[HBM]"
  758. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  759. return FAILED;
  760. }
  761. int64_t batch_atomic_mem_start = static_cast<int64_t>(mem_iter->second.mem_offset_);
  762. int64_t batch_max_mem_offset = batch_atomic_mem_start;
  763. for (auto &iter_batch : normal_atomic_and_clean_nodes_map) {
  764. mem_iter->second.mem_offset_ = batch_atomic_mem_start;
  765. for (auto &iter : iter_batch.second) {
  766. int64_t atomic_mem_start = static_cast<int64_t>(mem_iter->second.mem_offset_);
  767. GELOGD("Begin to reAssign atomic memory, atomic address memory start = %ld", atomic_mem_start);
  768. for (auto &atomic_node : iter.second) {
  769. vector<int64_t> mem_offset_end;
  770. status = AssignAtomicOutputAndWorkspaceMemory(atomic_node, mem_offset_end);
  771. if (status != SUCCESS) {
  772. GELOGE(status, "[Assign][Memory]output atomic mem and workspace mem, fail for node name is %s.",
  773. atomic_node->GetName().c_str());
  774. return status;
  775. }
  776. }
  777. int64_t atomic_mem_size = static_cast<int64_t>(mem_iter->second.mem_offset_) - atomic_mem_start;
  778. if (atomic_mem_size != 0) {
  779. GE_CHK_STATUS_RET(SetAtomicCleanAttr(iter.first, {atomic_mem_start}, {atomic_mem_size}, RT_MEMORY_HBM),
  780. "[Set][Attr]fail for atomic addr clean node %s.", iter.first->GetName().c_str());
  781. }
  782. }
  783. batch_max_mem_offset = std::max(batch_max_mem_offset, static_cast<int64_t>(mem_iter->second.mem_offset_));
  784. }
  785. mem_iter->second.mem_offset_ = static_cast<size_t>(batch_max_mem_offset);
  786. batch_atomic_mem_start = batch_max_mem_offset;
  787. for (auto &iter_batch : connecting_output_atomic_nodes) {
  788. mem_iter->second.mem_offset_ = batch_atomic_mem_start;
  789. if (AssignConnectNetOutputAtomicMemory(iter_batch.second) != SUCCESS) {
  790. GELOGE(FAILED, "[Assign][Memory]for nodes that connect to netoutput failed."
  791. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  792. return FAILED;
  793. }
  794. batch_max_mem_offset = std::max(batch_max_mem_offset, static_cast<int64_t>(mem_iter->second.mem_offset_));
  795. }
  796. mem_iter->second.mem_offset_ = static_cast<size_t>(batch_max_mem_offset);
  797. return SUCCESS;
  798. }
  799. Status GraphMemoryAssigner::FilterAtomicNodesForMemoryAssign(
  800. map<string, map<NodePtr, vector<NodePtr>>> &normal_atomic_nodes_map,
  801. map<string, vector<NodePtr>> &connecting_output_atomic_nodes) {
  802. GE_CHECK_NOTNULL(compute_graph_);
  803. for (const auto &node : compute_graph_->GetAllNodes()) {
  804. if (node->GetType() == ATOMICADDRCLEAN) {
  805. map<string, vector<NodePtr>> tmp_normal_atomic_nodes;
  806. const auto &out_control_anchor = node->GetOutControlAnchor();
  807. GE_CHECK_NOTNULL(out_control_anchor);
  808. for (const auto &peer_in_control_anchor : out_control_anchor->GetPeerInControlAnchors()) {
  809. if (peer_in_control_anchor != nullptr) {
  810. auto peer_in_node = peer_in_control_anchor->GetOwnerNode();
  811. auto peer_in_node_desc = peer_in_node->GetOpDesc();
  812. if (peer_in_node_desc != nullptr) {
  813. bool is_atomic_node = false;
  814. // If GetBool fail, is_atomic_node is false.
  815. (void) ge::AttrUtils::GetBool(peer_in_node_desc, ATOMIC_ATTR_IS_ATOMIC_NODE, is_atomic_node);
  816. if (is_atomic_node) {
  817. bool is_reference = false;
  818. // If GetBool fail, is_reference is false.
  819. (void) ge::AttrUtils::GetBool(peer_in_node_desc, ATTR_NAME_REFERENCE, is_reference);
  820. if (is_reference) {
  821. REPORT_INNER_ERROR("E19999", "Op:%s cannot have both atomic and is_reference attribute, "
  822. "not support now", peer_in_node_desc->GetName().c_str());
  823. GELOGE(FAILED, "[Check][Attr]Op:%s cannot have both atomic and is_reference attribute, "
  824. "not support now", peer_in_node_desc->GetName().c_str());
  825. return ge::PARAM_INVALID;
  826. }
  827. std::string batch_label;
  828. (void)ge::AttrUtils::GetStr(peer_in_node_desc, ATTR_NAME_BATCH_LABEL, batch_label);
  829. vector<int> is_connecting_output;
  830. // If GetBool fail, attr is_connecting_output is an empty vector.
  831. (void) ge::AttrUtils::GetListInt(peer_in_node_desc, ATTR_NAME_NODE_CONNECT_OUTPUT, is_connecting_output);
  832. if (is_connecting_output.empty()) {
  833. tmp_normal_atomic_nodes[batch_label].emplace_back(peer_in_node);
  834. continue;
  835. }
  836. connecting_output_atomic_nodes[batch_label].emplace_back(peer_in_node);
  837. tmp_normal_atomic_nodes[batch_label].clear();
  838. break;
  839. }
  840. }
  841. }
  842. }
  843. for (auto &it_atomic_node : tmp_normal_atomic_nodes) {
  844. if (!it_atomic_node.second.empty()) {
  845. normal_atomic_nodes_map[it_atomic_node.first][node] = it_atomic_node.second;
  846. }
  847. }
  848. }
  849. }
  850. return SUCCESS;
  851. }
  852. Status GraphMemoryAssigner::AssignAtomicOutputAndWorkspaceMemory(const ge::NodePtr &node,
  853. vector<int64_t> &mem_offset_end) {
  854. auto node_op_desc = node->GetOpDesc();
  855. // Assign atomic node output memory
  856. Status ret = AssignAtomicOutputMemory(node, mem_offset_end);
  857. if (ret != SUCCESS) {
  858. GELOGE(ret, "[Assign][Memory:Ouput:Atomic]Failed for node:%s.", node_op_desc->GetName().c_str());
  859. return ret;
  860. }
  861. // Check and assign atomic node workspace memory
  862. map<string, map<int64_t, int64_t>> atomic_workspace_info;
  863. atomic_workspace_info = node_op_desc->TryGetExtAttr(EXT_ATTR_ATOMIC_WORKSPACE_INFO, atomic_workspace_info);
  864. if (!atomic_workspace_info.empty()) {
  865. bool is_fusion_node = false;
  866. // If GetBool fail, is_fusion_node is false.
  867. (void) ge::AttrUtils::GetBool(node_op_desc, ATOMIC_ATTR_IS_FUSION_NODE, is_fusion_node);
  868. if (is_fusion_node) {
  869. // Assign fusion atomic node workspace memory
  870. ret = AssignFusionAtomicWorkspaceMemory(node_op_desc, atomic_workspace_info, mem_offset_end);
  871. } else {
  872. // Assign single ordinary atomic node workspace memory, not include fusion node
  873. ret = AssignOrdinaryAtomicWorkspaceMemory(node_op_desc, atomic_workspace_info, mem_offset_end);
  874. }
  875. if (ret != SUCCESS) {
  876. GELOGE(ret, "[Assign][Memory:Atomic:Workspace]fail for node:%s.", node_op_desc->GetName().c_str());
  877. return ret;
  878. }
  879. } else {
  880. GELOGW("Current atomic node %s does not have attr ATOMIC_WORKSPACE_INFO.", node->GetName().c_str());
  881. }
  882. return SUCCESS;
  883. }
  884. Status GraphMemoryAssigner::AssignConnectNetOutputAtomicMemory(vector<NodePtr> &connect_netoutput_nodes) {
  885. auto iter = memory_offset_.find(RT_MEMORY_HBM);
  886. if (iter == memory_offset_.end()) {
  887. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ does not have type[HBM], not expected, "
  888. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  889. GELOGE(FAILED, "[Check][InnerData]memory_offset_ does not have memory type[HBM]"
  890. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  891. return FAILED;
  892. }
  893. for (auto &node : connect_netoutput_nodes) {
  894. GE_CHECK_NOTNULL(node);
  895. if (node->GetOpDesc() == nullptr) {
  896. GELOGW("Current node %s op desc is nullptr, memory assignment is skipped.", node->GetName().c_str());
  897. continue;
  898. }
  899. // Atomic memory start addr
  900. int64_t original_atomic_mem_start = static_cast<int64_t>(iter->second.mem_offset_);
  901. GELOGD("Start to assign memory of atomic node, node name: %s, node type: %s, mem_offset: %ld.",
  902. node->GetName().c_str(), node->GetOpDesc()->GetType().c_str(), original_atomic_mem_start);
  903. vector<int64_t> mem_offset_end;
  904. if (AssignAtomicOutputAndWorkspaceMemory(node, mem_offset_end) != SUCCESS) {
  905. GELOGE(FAILED, "[Assign][Memory]output atomic mem and workspace mem, fail for node name is %s.",
  906. node->GetName().c_str());
  907. return FAILED;
  908. }
  909. // All atomic nodes use atomic_addr_clean op independently, so we need to set the attr separately.
  910. if (SetIndependentAtomicAttr(node, original_atomic_mem_start, mem_offset_end, RT_MEMORY_HBM) != SUCCESS) {
  911. GELOGE(FAILED, "[Set][Attr:IndependentAtomic]fail for node:%s", node->GetName().c_str());
  912. return FAILED;
  913. }
  914. }
  915. return SUCCESS;
  916. }
  917. Status GraphMemoryAssigner::AssignReferenceMemory() {
  918. for (auto &node : compute_graph_->GetDirectNode()) {
  919. // Get the reference type of the node, default is false
  920. bool is_ref = false;
  921. // If GetBool fail, is_ref is false.
  922. (void) ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_REFERENCE, is_ref);
  923. if (!is_ref) {
  924. continue;
  925. }
  926. GELOGI("Current node %s needs to support the reference relationship between output and input.",
  927. node->GetName().c_str());
  928. auto out_op_desc = node->GetOpDesc();
  929. GE_IF_BOOL_EXEC(out_op_desc == nullptr, GELOGE(ge::FAILED, "out_op_desc is null."); return ge::FAILED);
  930. vector<int64_t> output_list = out_op_desc->GetOutputOffset();
  931. if (out_op_desc->GetOutputsSize() > output_list.size()) {
  932. REPORT_INNER_ERROR("E19999", "Output size:%zu more than output offset size:%zu, judge invalid in node:%s",
  933. out_op_desc->GetOutputsSize(), output_list.size(), node->GetName().c_str());
  934. GELOGE(ge::FAILED, "[Check][InnerData]Output size:%zu more than output offset size:%zu, invalid in node:%s",
  935. out_op_desc->GetOutputsSize(), output_list.size(), node->GetName().c_str());
  936. return ge::FAILED;
  937. }
  938. map<string, int> input_name_index;
  939. for (const auto &input_name : out_op_desc->GetAllInputNames()) {
  940. int index = out_op_desc->GetInputIndexByName(input_name);
  941. input_name_index.emplace(input_name, index);
  942. }
  943. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  944. string out_data_anchor_name = out_op_desc->GetOutputNameByIndex(out_data_anchor->GetIdx());
  945. auto iter = input_name_index.find(out_data_anchor_name);
  946. if (iter != input_name_index.end()) {
  947. int index = iter->second;
  948. GELOGI("Reference memory: input anchor index = %d, input anchor name = %s, output anchor name = %s.", index,
  949. iter->first.c_str(), out_data_anchor_name.c_str());
  950. GE_CHECK_NOTNULL(node->GetInDataAnchor(index));
  951. auto peer_out_anchor = node->GetInDataAnchor(index)->GetPeerOutAnchor();
  952. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  953. int peer_out_anchor_index = peer_out_anchor->GetIdx();
  954. auto peer_out_node = peer_out_anchor->GetOwnerNode();
  955. auto peer_out_op_desc = peer_out_node->GetOpDesc();
  956. GE_CHECK_NOTNULL(peer_out_op_desc);
  957. output_list[out_data_anchor->GetIdx()] = peer_out_op_desc->GetOutputOffset()[peer_out_anchor_index];
  958. GELOGI("Reference output : Set %s name[%s] output[%d] offset to [%ld] stream_id[%ld]",
  959. node->GetOwnerComputeGraph()->GetName().c_str(), peer_out_op_desc->GetName().c_str(),
  960. out_data_anchor->GetIdx(), output_list[out_data_anchor->GetIdx()], peer_out_op_desc->GetStreamId());
  961. } else {
  962. GELOGI("Reference output : origin %s name[%s] output[%d] offset is [%ld] stream_id[%ld]",
  963. node->GetOwnerComputeGraph()->GetName().c_str(), out_op_desc->GetName().c_str(),
  964. out_data_anchor->GetIdx(), output_list[out_data_anchor->GetIdx()], out_op_desc->GetStreamId());
  965. }
  966. }
  967. out_op_desc->SetOutputOffset(output_list);
  968. }
  969. return ge::SUCCESS;
  970. }
  971. bool GraphMemoryAssigner::CheckInputIsSupportAtomic(const ge::NodePtr &node) {
  972. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  973. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  974. if (peer_out_data_anchor == nullptr) {
  975. continue;
  976. }
  977. auto peer_op_desc = peer_out_data_anchor->GetOwnerNode()->GetOpDesc();
  978. if (peer_op_desc == nullptr) {
  979. continue;
  980. }
  981. if ((peer_op_desc->GetType() == CONSTANTOP) || (peer_op_desc->GetType() == AIPP_DATA_TYPE) ||
  982. (peer_op_desc->GetType() == VARIABLE)) {
  983. REPORT_INNER_ERROR("E19999", "node(type:%s, name:%s) link to atomic node(name:%s), "
  984. "this situation not supported now",
  985. peer_op_desc->GetType().c_str(), peer_op_desc->GetName().c_str(), node->GetName().c_str());
  986. GELOGE(ge::FAILED, "[Check][Link]node(type:%s, name:%s) link to atomic node(name:%s), "
  987. "this situation not supported now",
  988. peer_op_desc->GetType().c_str(), peer_op_desc->GetName().c_str(), node->GetName().c_str());
  989. return false;
  990. }
  991. }
  992. return true;
  993. }
  994. Status GraphMemoryAssigner::AssignAtomicOutputMemory(const ge::NodePtr &node, vector<int64_t> &mem_offset_end) {
  995. auto op_desc = node->GetOpDesc();
  996. GE_IF_BOOL_EXEC(op_desc == nullptr, GELOGE(ge::FAILED, "op_desc is null."); return ge::FAILED);
  997. mem_offset_end.clear();
  998. GELOGD("Begin to assign atomic output memory, node = %s.", op_desc->GetName().c_str());
  999. vector<int64_t> atomic_output_index;
  1000. // If GetListInt fail, atomic_output_index is empty.
  1001. (void) ge::AttrUtils::GetListInt(op_desc, ATOMIC_ATTR_OUTPUT_INDEX, atomic_output_index);
  1002. // Check atomic output
  1003. vector<int64_t> output_list = op_desc->GetOutputOffset();
  1004. if (atomic_output_index.size() > output_list.size()) {
  1005. std::string error =
  1006. "Op:" + FmtToStr(node->GetName()) + "'s size:" + FmtToStr(atomic_output_index.size()) +
  1007. " of atomic_output_index is more than the size:" + FmtToStr(output_list.size()) + " of output_list";
  1008. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  1009. return ge::FAILED;
  1010. }
  1011. auto output_list_size = static_cast<int64_t>(output_list.size());
  1012. auto iter = memory_offset_.find(RT_MEMORY_HBM);
  1013. if (iter == memory_offset_.end()) {
  1014. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ does not have type[HBM], not expected, "
  1015. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1016. GELOGE(FAILED, "[Check][InnerData]memory_offset_ does not have memory type[HBM]"
  1017. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1018. return FAILED;
  1019. }
  1020. for (auto &output_index : atomic_output_index) {
  1021. if (output_index >= output_list_size) {
  1022. std::string error =
  1023. "Op:" + FmtToStr(node->GetName()) + "'s atomic_output index:" + FmtToStr(output_index) +
  1024. " is more than the size:" + FmtToStr(output_list_size) + " of output_list.";
  1025. GE_ERRORLOG_AND_ERRORMSG(ge::PARAM_INVALID, error.c_str());
  1026. return ge::PARAM_INVALID;
  1027. }
  1028. // If the input of the cascade op needs to clear the atomic addr, there is no need to clear it separately here
  1029. bool is_assigned_mem = false;
  1030. if (GetMemoryAssignmentStatus(node, output_index, is_assigned_mem) != SUCCESS) {
  1031. GELOGE(ge::FAILED, "[Get][MemoryAssignmentStatus]fail for node %s, out_index:%ld",
  1032. node->GetName().c_str(), output_index);
  1033. return ge::FAILED;
  1034. }
  1035. // If you have already assigned an atomic address, skip it, and you don't need to reassign it.
  1036. if (is_assigned_mem) {
  1037. GELOGI(
  1038. "Node %s atomic output : we have assigned atomic memory as the input of next node in "
  1039. "ReAssignContinuousMemory function.",
  1040. op_desc->GetName().c_str());
  1041. continue;
  1042. }
  1043. auto output_desc = op_desc->GetAllOutputsDescPtr().at(output_index);
  1044. int64_t size = 0;
  1045. if (ge::TensorUtils::GetSize(*output_desc, size) != SUCCESS) {
  1046. GELOGI("Get size failed");
  1047. }
  1048. output_list[output_index] = iter->second.mem_offset_;
  1049. std::string batch_label;
  1050. (void)ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, batch_label);
  1051. GELOGI("[IMAS]Atomic output : Set %s name[%s] optype[%s] output[%ld] offset to [%zu] stream_id[%ld] memtype[%u] "
  1052. "size[%ld] real_size[%ld] batch[%s].", compute_graph_->GetName().c_str(), op_desc->GetName().c_str(),
  1053. node->GetType().c_str(), output_index, iter->second.mem_offset_, op_desc->GetStreamId(), RT_MEMORY_HBM,
  1054. size, size, batch_label.c_str());
  1055. iter->second.mem_offset_ += size;
  1056. AlignMemOffset(MEM_ALIGN_SIZE, RT_MEMORY_HBM);
  1057. mem_offset_end.emplace_back(iter->second.mem_offset_);
  1058. }
  1059. op_desc->SetOutputOffset(output_list);
  1060. return ge::SUCCESS;
  1061. }
  1062. Status GraphMemoryAssigner::GetMemoryAssignmentStatus(const ge::NodePtr &node, int64_t output_index,
  1063. bool &is_mem_assigned) {
  1064. if (static_cast<size_t>(output_index) >= node->GetAllOutDataAnchors().size()) {
  1065. std::string error =
  1066. "Op:" + FmtToStr(node->GetName()) + "'s output index:" + FmtToStr(output_index) +
  1067. " is more than the size:" + FmtToStr(node->GetAllOutDataAnchors().size()) + " of node's AllOutDataAnchors.";
  1068. GE_ERRORLOG_AND_ERRORMSG(ge::PARAM_INVALID, error.c_str());
  1069. return ge::PARAM_INVALID;
  1070. }
  1071. auto out_data_anchor = node->GetAllOutDataAnchors().at(output_index);
  1072. GE_CHECK_NOTNULL(out_data_anchor);
  1073. auto input_anchors = out_data_anchor->GetPeerInDataAnchors();
  1074. for (auto &input_anchor : input_anchors) {
  1075. auto output_node = input_anchor->GetOwnerNode();
  1076. /// Get input atomic attr of peer output op, if atomic_input_index[0] = -1, indicates that the atomic address
  1077. /// has been assigned
  1078. vector<int64_t> atomic_input_index;
  1079. (void) ge::AttrUtils::GetListInt(output_node->GetOpDesc(), ATOMIC_ATTR_INPUT_INDEX, atomic_input_index);
  1080. if (!atomic_input_index.empty() && (atomic_input_index[0] == kAllInputAddrIsAtomic)) {
  1081. is_mem_assigned = true;
  1082. break;
  1083. }
  1084. }
  1085. return SUCCESS;
  1086. }
  1087. Status GraphMemoryAssigner::AssignOrdinaryAtomicWorkspaceMemory(const ge::OpDescPtr &op_desc,
  1088. map<string, map<int64_t, int64_t>> &workspace_info,
  1089. vector<int64_t> &mem_offset_end) {
  1090. GELOGI("Begin to reassign normal atomic memory, node = %s.", op_desc->GetName().c_str());
  1091. auto mem_type_iter = memory_offset_.find(RT_MEMORY_HBM);
  1092. if (mem_type_iter == memory_offset_.end()) {
  1093. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ does not have type[HBM], not expected, "
  1094. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1095. GELOGE(FAILED, "[Check][InnerData]memory_offset_ does not have memory type[HBM]"
  1096. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1097. return FAILED;
  1098. }
  1099. vector<int64_t> workspace_vector = op_desc->GetWorkspace();
  1100. for (auto iter = workspace_info.begin(); iter != workspace_info.end(); ++iter) {
  1101. if (op_desc->GetName() != iter->first) {
  1102. std::string error = "The node name" + FmtToStr(op_desc->GetName()) +
  1103. " and the node name" + FmtToStr(iter->first) + " in workspace info are inconsistent.";
  1104. GE_ERRORLOG_AND_ERRORMSG(ge::PARAM_INVALID, error.c_str());
  1105. return ge::PARAM_INVALID;
  1106. }
  1107. if (iter->second.empty()) {
  1108. continue;
  1109. }
  1110. for (auto &info_iter : iter->second) {
  1111. auto workspace_index = static_cast<uint64_t>(info_iter.first);
  1112. auto workspace_size = info_iter.second;
  1113. if (workspace_index >= workspace_vector.size()) {
  1114. std::string error = "The workspace index:" + FmtToStr(workspace_index) +
  1115. " is more than the size:" + FmtToStr(workspace_vector.size()) + " of workspace vector in op:" +
  1116. op_desc->GetName().c_str();
  1117. GE_ERRORLOG_AND_ERRORMSG(ge::PARAM_INVALID, error.c_str());
  1118. return ge::PARAM_INVALID;
  1119. }
  1120. workspace_vector[workspace_index] = mem_type_iter->second.mem_offset_;
  1121. std::string batch_label;
  1122. (void)ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, batch_label);
  1123. GELOGI(
  1124. "[IMAS]Atomic ordinary workspace : Set %s name[%s] optype[%s] workspace[%lu] offset to [%zu] stream_id[%ld] "
  1125. "memtype[%u] size[%ld] real_size[%ld] batch[%s].",
  1126. compute_graph_->GetName().c_str(), op_desc->GetName().c_str(), op_desc->GetType().c_str(), workspace_index,
  1127. mem_type_iter->second.mem_offset_, op_desc->GetStreamId(), RT_MEMORY_HBM, workspace_size, workspace_size,
  1128. batch_label.c_str());
  1129. mem_type_iter->second.mem_offset_ += workspace_size;
  1130. mem_offset_end.emplace_back(mem_type_iter->second.mem_offset_);
  1131. }
  1132. }
  1133. op_desc->SetWorkspace(workspace_vector);
  1134. return SUCCESS;
  1135. }
  1136. Status GraphMemoryAssigner::AssignFusionAtomicWorkspaceMemory(const ge::OpDescPtr &op_desc,
  1137. map<string, map<int64_t, int64_t>> &workspace_info,
  1138. vector<int64_t> &mem_offset_end) {
  1139. GELOGI("Begin to reassign fusion atomic memory, node = %s.", op_desc->GetName().c_str());
  1140. auto mem_type_iter = memory_offset_.find(RT_MEMORY_HBM);
  1141. if (mem_type_iter == memory_offset_.end()) {
  1142. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ does not have type[HBM], not expected, "
  1143. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1144. GELOGE(FAILED, "[Check][InnerData]memory_offset_ does not have memory type[HBM]"
  1145. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1146. return FAILED;
  1147. }
  1148. map<string, map<int64_t, int64_t>> sub_node_workspace_offset;
  1149. for (auto &iter : workspace_info) {
  1150. if (iter.second.empty()) {
  1151. continue;
  1152. }
  1153. map<int64_t, int64_t> index_offset;
  1154. for (auto &info_iter : iter.second) {
  1155. auto workspace_index = static_cast<uint64_t>(info_iter.first);
  1156. auto workspace_size = info_iter.second;
  1157. size_t workspace_offset = mem_type_iter->second.mem_offset_;
  1158. std::string batch_label;
  1159. (void)ge::AttrUtils::GetStr(op_desc, ATTR_NAME_BATCH_LABEL, batch_label);
  1160. GELOGI(
  1161. "[IMAS]Atomic fusion workspace : Set %s name[%s] optype[%s] workspace[%lu] offset to [%zu] stream_id[%ld] "
  1162. "memtype[%u] ssize[%ld] real_size[%ld] batch[%s].", compute_graph_->GetName().c_str(),
  1163. op_desc->GetName().c_str(), op_desc->GetType().c_str(), workspace_index, mem_type_iter->second.mem_offset_,
  1164. op_desc->GetStreamId(), RT_MEMORY_HBM, workspace_size, workspace_size, batch_label.c_str());
  1165. mem_type_iter->second.mem_offset_ += workspace_size;
  1166. mem_offset_end.emplace_back(mem_type_iter->second.mem_offset_);
  1167. index_offset.insert(std::make_pair(workspace_index, workspace_offset));
  1168. }
  1169. sub_node_workspace_offset.insert(std::make_pair(iter.first, index_offset));
  1170. }
  1171. if (!(op_desc->SetExtAttr(EXT_ATTR_ATOMIC_WORKSPACE_OFFSET, sub_node_workspace_offset))) {
  1172. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for node:%s",
  1173. EXT_ATTR_ATOMIC_WORKSPACE_OFFSET.c_str(), op_desc->GetName().c_str());
  1174. GELOGE(FAILED, "[Set][Attr:%s]fail for node:%s.",
  1175. EXT_ATTR_ATOMIC_WORKSPACE_OFFSET.c_str(), 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. REPORT_CALL_ERROR("E19999", "Get ref-mapping for graph %s failed", compute_graph_->GetName().c_str());
  1185. GELOGE(FAILED, "[Get][RefMapping]fail for graph %s", compute_graph_->GetName().c_str());
  1186. return FAILED;
  1187. }
  1188. for (const ge::NodePtr &node : compute_graph_->GetAllNodes()) {
  1189. GE_CHECK_NOTNULL(node->GetOpDesc());
  1190. vector<int64_t> input_list = node->GetOpDesc()->GetInputOffset();
  1191. for (auto input : input_list) {
  1192. if (input == ge::kInvalidOffset) {
  1193. std::string error = "Invalid input offset" + FmtToStr(ge::kInvalidOffset) +
  1194. + " in node" + FmtToStr(node->GetName());
  1195. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  1196. return FAILED;
  1197. }
  1198. }
  1199. bool need_update_output = false;
  1200. vector<int64_t> output_list = node->GetOpDesc()->GetOutputOffset();
  1201. for (uint32_t i = 0; i < output_list.size(); ++i) {
  1202. if (output_list[i] == ge::kInvalidOffset) {
  1203. std::string error = "Invalid output offset" + FmtToStr(ge::kInvalidOffset) +
  1204. + " in node" + FmtToStr(node->GetName());
  1205. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  1206. return FAILED;
  1207. }
  1208. if (node->GetType() == IDENTITY || node->GetType() == READVARIABLEOP) {
  1209. auto symbol_offset = GetSymbolOutputOffset(anchor_to_symbol, symbol_to_anchors, node, i);
  1210. if (symbol_offset != ge::kInvalidOffset && output_list[i] != symbol_offset) {
  1211. output_list[i] = symbol_offset;
  1212. need_update_output = true;
  1213. }
  1214. }
  1215. }
  1216. if (need_update_output) {
  1217. node->GetOpDesc()->SetOutputOffset(output_list);
  1218. }
  1219. vector<int64_t> workspace_list = node->GetOpDesc()->GetWorkspace();
  1220. for (auto workspace : workspace_list) {
  1221. if (workspace == ge::kInvalidOffset) {
  1222. std::string error = "Invalid workspace" + FmtToStr(ge::kInvalidOffset) +
  1223. + " in node" + FmtToStr(node->GetName());
  1224. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  1225. return FAILED;
  1226. }
  1227. }
  1228. // check reuse input and output
  1229. GE_CHK_STATUS_RET(CheckRefNodeOffset(node), "[Check][Offset]fail for node: %s", node->GetName().c_str());
  1230. }
  1231. return SUCCESS;
  1232. }
  1233. ge::Status GraphMemoryAssigner::CheckRefNodeOffset(const NodePtr &node) {
  1234. GE_CHECK_NOTNULL(node);
  1235. std::map<int32_t, int32_t> out2ins;
  1236. GE_CHK_STATUS_RET(TryGetNodeRefIndexes(node, out2ins), "[Get][RefIndexes]fail for node: %s", node->GetName().c_str());
  1237. auto opdesc = node->GetOpDesc();
  1238. GE_CHECK_NOTNULL(opdesc);
  1239. auto output_list = opdesc->GetOutputOffset();
  1240. auto input_list = opdesc->GetInputOffset();
  1241. for (const auto &out2in : out2ins) {
  1242. auto out_i = out2in.first;
  1243. if (static_cast<size_t>(out_i) >= output_list.size()) {
  1244. std::string error = "Node" + FmtToStr(opdesc->GetName()) + "output offset size" +
  1245. FmtToStr(output_list.size()) + "should bigger than ref out index" + FmtToStr(out_i);
  1246. GE_ERRORLOG_AND_ERRORMSG(ge::FAILED, error.c_str());
  1247. return ge::FAILED;
  1248. }
  1249. auto in_i = out2in.second;
  1250. if (static_cast<size_t>(in_i) >= input_list.size()) {
  1251. std::string error = "Node" + FmtToStr(opdesc->GetName()) + "input offset size" +
  1252. FmtToStr(input_list.size()) + "should bigger than ref input index" + FmtToStr(in_i);
  1253. GE_ERRORLOG_AND_ERRORMSG(ge::FAILED, error.c_str());
  1254. return ge::FAILED;
  1255. }
  1256. if (output_list[out_i] != input_list[in_i]) {
  1257. std::string error = "Node" + FmtToStr(opdesc->GetName()) + "input offset " + FmtToStr(input_list[in_i]) +
  1258. "should equal to output offset" + FmtToStr(output_list[out_i]) + "with ref in" +
  1259. FmtToStr(in_i) + "to output" + FmtToStr(out_i);
  1260. GE_ERRORLOG_AND_ERRORMSG(ge::FAILED, error.c_str());
  1261. return ge::FAILED;
  1262. }
  1263. }
  1264. return ge::SUCCESS;
  1265. }
  1266. ge::Status GraphMemoryAssigner::SetInputOffset() {
  1267. if (memory_offset_.empty()) {
  1268. REPORT_INNER_ERROR("E19999", "InnerData memory_offset_ empty, not expected, graph_id:%u, graph_name:%s",
  1269. compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1270. GELOGE(FAILED, "[Check][InnerData:memory_offset_]empty is not expected, "
  1271. "graph_id:%u, graph_name:%s", compute_graph_->GetGraphID(), compute_graph_->GetName().c_str());
  1272. }
  1273. for (auto pair : memory_offset_) {
  1274. GEEVENT("[IMAS]AfterAssignMemory : %s memoffset[%zu], memtype[%ld]", compute_graph_->GetName().c_str(),
  1275. pair.second.mem_offset_, pair.first);
  1276. }
  1277. for (const ge::NodePtr &node : compute_graph_->GetAllNodes()) {
  1278. if (UpdateOpInputOffset(node) != ge::SUCCESS) {
  1279. GELOGE(ge::FAILED, "[Update][Offset:Input]fail for op:%s", node->GetName().c_str());
  1280. return ge::FAILED;
  1281. }
  1282. }
  1283. return ge::SUCCESS;
  1284. }
  1285. NodePtr GraphMemoryAssigner::GetKnownInputNode(const NodePtr &node) const {
  1286. if (!node->GetOpDesc()->HasAttr(ATTR_NAME_PARENT_NODE_INDEX)) {
  1287. return node;
  1288. }
  1289. if (NodeUtils::IsDynamicShape(node)) {
  1290. return node;
  1291. }
  1292. return NodeUtils::GetParentInput(node);
  1293. }
  1294. ge::Status GraphMemoryAssigner::UpdateConstArgsOffset(const NodePtr &node, vector<int64_t> &input_list) const {
  1295. uint32_t parent_index = 0;
  1296. if (!AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  1297. return SUCCESS;
  1298. }
  1299. // Subgraph Data Node, check for constant input.
  1300. std::string op_type;
  1301. const auto &in_node = NodeUtils::GetParentInput(node);
  1302. if (NodeUtils::GetConstOpType(in_node, op_type)) {
  1303. input_list = in_node->GetOpDesc()->GetOutputOffset();
  1304. node->GetOpDesc()->SetOutputOffset(input_list); // Set Data output same as const output.
  1305. return SUCCESS; // Constant input.
  1306. }
  1307. // Memory allocated for dynamic shape subgraph Data.
  1308. if (NodeUtils::IsDynamicShape(node)) {
  1309. return SUCCESS;
  1310. }
  1311. const auto &owner = node->GetOwnerComputeGraph();
  1312. const auto &parent_desc = owner->GetParentNode()->GetOpDesc();
  1313. const auto parent_inputs = parent_desc->GetInputOffset();
  1314. if (parent_inputs.size() <= parent_index) {
  1315. std::string error = "Get Parent input offset failed, node is " + FmtToStr(node->GetName()) +
  1316. + ", input_size is " + FmtToStr(parent_inputs.size()) + ", parent index is " +
  1317. FmtToStr(parent_index);
  1318. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  1319. return FAILED;
  1320. }
  1321. input_list = {parent_inputs[parent_index]};
  1322. node->GetOpDesc()->SetOutputOffset(input_list); // Set Data output same as parent input.
  1323. return SUCCESS;
  1324. }
  1325. ge::Status GraphMemoryAssigner::UpdateOpInputOffset(const NodePtr &node, vector<int64_t> &input_list) const {
  1326. vector<int64_t> origin_input_list;
  1327. vector<int64_t> memory_type;
  1328. auto tmp_op_desc = node->GetOpDesc();
  1329. origin_input_list = tmp_op_desc->GetInputOffset();
  1330. int64_t valid_input_index = 0;
  1331. bool has_mem_type_attr = ge::AttrUtils::GetListInt(tmp_op_desc, ATTR_NAME_INPUT_MEM_TYPE_LIST, memory_type);
  1332. std::map<int32_t, int32_t> out2ins;
  1333. GE_CHK_STATUS_RET(TryGetNodeRefIndexes(node, out2ins), "[Get][RefIndexes]fail for node: %s", node->GetName().c_str());
  1334. for (const auto &anchor : node->GetAllInDataAnchors()) {
  1335. vector<int64_t> output_list;
  1336. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  1337. if (peer_out_anchor == nullptr) {
  1338. continue;
  1339. }
  1340. // If the current node not broadcast, the OutputOffset of the previous node is used to update the input_list
  1341. auto last_peer_out_node = peer_out_anchor->GetOwnerNode();
  1342. auto last_peer_out_op_desc = last_peer_out_node->GetOpDesc();
  1343. GE_CHECK_NOTNULL(last_peer_out_op_desc);
  1344. output_list = last_peer_out_op_desc->GetOutputOffset();
  1345. auto out_index = static_cast<unsigned long>(peer_out_anchor->GetIdx());
  1346. if (output_list.size() > static_cast<size_t>(out_index)) {
  1347. bool is_l1_type = false;
  1348. int64_t input_offset = output_list.at(out_index);
  1349. if (has_mem_type_attr && !origin_input_list.empty()) {
  1350. auto input_size = tmp_op_desc->GetInputsSize();
  1351. auto ori_input_offset_list_size = origin_input_list.size();
  1352. auto mem_type_size = memory_type.size();
  1353. if ((input_size != mem_type_size) || (input_size != ori_input_offset_list_size)) {
  1354. std::string error = "Node" + FmtToStr(tmp_op_desc->GetName()) +
  1355. + " input_size" + FmtToStr(input_size) + " diff from memory_type_size" +
  1356. FmtToStr(mem_type_size) + " from ori_input_offset_list_size" +
  1357. FmtToStr(ori_input_offset_list_size);
  1358. GE_ERRORLOG_AND_ERRORMSG(ge::FAILED, error.c_str());
  1359. return ge::FAILED;
  1360. }
  1361. GELOGD("Node[%s] input[%d] has origin offset[%ld]", tmp_op_desc->GetName().c_str(), anchor->GetIdx(),
  1362. origin_input_list[valid_input_index]);
  1363. // L1 keep original input_offset
  1364. is_l1_type = (memory_type[valid_input_index] == RT_MEMORY_L1);
  1365. if (is_l1_type) {
  1366. input_offset = origin_input_list[valid_input_index];
  1367. } else {
  1368. // hbm input_offset = original input_offset + output_offset
  1369. input_offset = origin_input_list[valid_input_index] + output_list.at(out_index);
  1370. }
  1371. }
  1372. const auto &in_node = GetKnownInputNode(peer_out_anchor->GetOwnerNode());
  1373. if (in_node->GetType() == CONSTANT) {
  1374. GeTensorDesc tensor_desc = tmp_op_desc->GetInputDesc(static_cast<uint32_t>(anchor->GetIdx()));
  1375. GE_CHK_STATUS(TensorUtils::GetDataOffset(tensor_desc, input_offset));
  1376. }
  1377. if (!is_l1_type) {
  1378. // update ref output_offset when input change
  1379. GE_CHK_STATUS_RET(UpdateRefOpOutputOffset(node, out2ins, anchor->GetIdx(), input_offset),
  1380. "[Update][RefOffset]fail for node: %s", node->GetName().c_str());
  1381. }
  1382. GELOGD("Node[%s] input[%d] is set from node[%s] out index[%lu] offset[%ld]", tmp_op_desc->GetName().c_str(),
  1383. anchor->GetIdx(), peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(), out_index,
  1384. input_offset);
  1385. input_list.emplace_back(input_offset);
  1386. valid_input_index++;
  1387. }
  1388. }
  1389. return ge::SUCCESS;
  1390. }
  1391. ge::Status GraphMemoryAssigner::UpdateRefOpOutputOffset(const NodePtr &node, const std::map<int32_t, int32_t> &out2ins,
  1392. const int ref_in, const int64_t input_offset) const {
  1393. auto opdesc = node->GetOpDesc();
  1394. GE_CHECK_NOTNULL(opdesc);
  1395. for (const auto &out2in : out2ins) {
  1396. auto out_i = out2in.first;
  1397. auto in_i = out2in.second;
  1398. if (in_i == ref_in) {
  1399. auto origin_output_list = opdesc->GetOutputOffset();
  1400. if (static_cast<size_t>(out_i) >= origin_output_list.size()) {
  1401. std::string error = "Node" + FmtToStr(opdesc->GetName()) + "output offset size" +
  1402. FmtToStr(origin_output_list.size()) + "should bigger than ref out index" + FmtToStr(out_i);
  1403. GE_ERRORLOG_AND_ERRORMSG(ge::FAILED, error.c_str());
  1404. return ge::FAILED;
  1405. }
  1406. origin_output_list[out_i] = input_offset;
  1407. opdesc->SetOutputOffset(origin_output_list);
  1408. GELOGI("Node[%s] output[%d] is updated from reuse input index[%d] to offset[%ld]", opdesc->GetName().c_str(),
  1409. out_i, ref_in, input_offset);
  1410. }
  1411. }
  1412. return ge::SUCCESS;
  1413. }
  1414. ge::Status GraphMemoryAssigner::UpdateOpInputOffset(const NodePtr &node) const {
  1415. GE_CHECK_NOTNULL(node->GetOpDesc());
  1416. vector<int64_t> input_list;
  1417. if (node->GetType() == HCOMBROADCAST || node->GetType() == HVDCALLBACKBROADCAST) {
  1418. for (const auto &anchor : node->GetAllInDataAnchors()) {
  1419. vector<int64_t> output_list;
  1420. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  1421. if (peer_out_anchor == nullptr) {
  1422. continue;
  1423. }
  1424. auto last_peer_out_node = peer_out_anchor->GetOwnerNode();
  1425. // If the current node is broadcast and the preceding node is variable, because InputOffset has been set
  1426. // in function:AssignVarAttr2Nodes, then the InputOffset of the broadcast node is taken to update the input_list.
  1427. // Otherwise, the OutputOffset of the previous node is used to update the input_list.
  1428. if (last_peer_out_node->GetType() != VARIABLE) {
  1429. auto last_peer_out_op_desc = last_peer_out_node->GetOpDesc();
  1430. GE_CHECK_NOTNULL(last_peer_out_op_desc);
  1431. output_list = last_peer_out_op_desc->GetOutputOffset();
  1432. if (output_list.size() > static_cast<size_t>(peer_out_anchor->GetIdx())) {
  1433. input_list.emplace_back(output_list.at(peer_out_anchor->GetIdx()));
  1434. }
  1435. } else {
  1436. vector<int64_t> cur_node_input_list;
  1437. auto cur_node_op_desc = node->GetOpDesc();
  1438. GE_CHECK_NOTNULL(cur_node_op_desc);
  1439. cur_node_input_list = cur_node_op_desc->GetInputOffset();
  1440. if (cur_node_input_list.size() > static_cast<size_t>(anchor->GetIdx())) {
  1441. input_list.emplace_back(cur_node_input_list.at(anchor->GetIdx()));
  1442. }
  1443. }
  1444. }
  1445. } else if (node->GetType() == DATA_TYPE) {
  1446. if (UpdateConstArgsOffset(node, input_list) != SUCCESS) {
  1447. GELOGE(FAILED, "[Update][Offset:Input:Const]fail for node:%s ", node->GetName().c_str());
  1448. return FAILED;
  1449. }
  1450. } else {
  1451. if (UpdateOpInputOffset(node, input_list) != SUCCESS) {
  1452. GELOGE(FAILED, "[Update][Offset:Input]fail for node:%s", node->GetName().c_str());
  1453. return FAILED;
  1454. }
  1455. }
  1456. node->GetOpDesc()->SetInputOffset(input_list);
  1457. return SUCCESS;
  1458. }
  1459. Status GraphMemoryAssigner::SetIndependentAtomicAttr(const ge::NodePtr &node, int64_t atomic_mem_start,
  1460. const vector<int64_t> &mem_offset_end, int64_t memory_type) {
  1461. GELOGD("Start to set independent atomic attr, atomic_addr_clean memory offset start is %ld", atomic_mem_start);
  1462. // Parsing offset and size vectors
  1463. vector<int64_t> memory_offset_start;
  1464. vector<int64_t> memory_offset_size;
  1465. memory_offset_start.emplace_back(atomic_mem_start);
  1466. for (size_t i = 0; i < mem_offset_end.size(); ++i) {
  1467. memory_offset_start.emplace_back(mem_offset_end[i]);
  1468. // Number 1 means element index
  1469. auto size = memory_offset_start[i + 1] - memory_offset_start[i];
  1470. memory_offset_size.emplace_back(size);
  1471. }
  1472. memory_offset_start.pop_back();
  1473. const auto &in_control_anchor = node->GetInControlAnchor();
  1474. if (!memory_offset_size.empty() && in_control_anchor != nullptr) {
  1475. for (auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  1476. if (peer_out_control_anchor == nullptr) {
  1477. continue;
  1478. }
  1479. auto peer_out_node = peer_out_control_anchor->GetOwnerNode();
  1480. auto peer_out_node_desc = peer_out_node->GetOpDesc();
  1481. if (peer_out_node_desc == nullptr) {
  1482. continue;
  1483. }
  1484. GELOGD("Current node memory_offset vector size is %zu, node name %s, node type is %s.", memory_offset_size.size(),
  1485. peer_out_node_desc->GetName().c_str(), peer_out_node_desc->GetType().c_str());
  1486. if (peer_out_node_desc->GetType() == ATOMICADDRCLEAN) {
  1487. if (SetAtomicCleanAttr(peer_out_node, memory_offset_start, memory_offset_size, memory_type) != SUCCESS) {
  1488. GELOGE(FAILED, "[Set][AtomicCleanAttr]fail for node:%s", peer_out_node->GetName().c_str());
  1489. return FAILED;
  1490. }
  1491. }
  1492. }
  1493. }
  1494. return SUCCESS;
  1495. }
  1496. ge::Status GraphMemoryAssigner::SetAtomicCleanAttr(const NodePtr &node, const vector<int64_t> &atomic_mem_start,
  1497. const vector<int64_t> &atomic_mem_size, int64_t memory_type) {
  1498. auto node_op_desc = node->GetOpDesc();
  1499. if (node_op_desc != nullptr) {
  1500. GELOGD("Node %s, set atomic clean attr start.", node->GetName().c_str());
  1501. vector<int64_t> workspace_vector = node_op_desc->GetWorkspace();
  1502. vector<int64_t> workspace_byte_vector = node_op_desc->GetWorkspaceBytes();
  1503. workspace_vector.insert(workspace_vector.end(), atomic_mem_start.begin(), atomic_mem_start.end());
  1504. workspace_byte_vector.insert(workspace_byte_vector.end(), atomic_mem_size.begin(), atomic_mem_size.end());
  1505. node_op_desc->SetWorkspace(workspace_vector);
  1506. node_op_desc->SetWorkspaceBytes(workspace_byte_vector);
  1507. std::vector<int64_t> mem_start_vector;
  1508. // If GetListInt fail, mem_start_vector is empty.
  1509. (void) ge::AttrUtils::GetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_START, mem_start_vector);
  1510. mem_start_vector.insert(mem_start_vector.end(), atomic_mem_start.begin(), atomic_mem_start.end());
  1511. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_START, mem_start_vector),
  1512. REPORT_INNER_ERROR("E19999", "Set Attr:%s failed, op_name:%s",
  1513. ATTR_NAME_AUTOMIC_ADD_START.c_str(), node_op_desc->GetName().c_str());
  1514. GELOGE(FAILED, "[Set][Attr:%s]fail for op_name:%s",
  1515. ATTR_NAME_AUTOMIC_ADD_START.c_str(), node_op_desc->GetName().c_str());
  1516. return FAILED);
  1517. std::vector<int64_t> mem_size_vector;
  1518. // If GetListInt fail, mem_size_vector is empty.
  1519. (void) ge::AttrUtils::GetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_MEM_SIZE, mem_size_vector);
  1520. mem_size_vector.insert(mem_size_vector.end(), atomic_mem_size.begin(), atomic_mem_size.end());
  1521. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(node_op_desc, ATTR_NAME_AUTOMIC_ADD_MEM_SIZE, mem_size_vector),
  1522. REPORT_INNER_ERROR("E19999", "Set Attr:%s failed, op_name:%s",
  1523. ATTR_NAME_AUTOMIC_ADD_MEM_SIZE.c_str(), node_op_desc->GetName().c_str());
  1524. GELOGE(FAILED, "[Set][Attr:%s]fail for op_name:%s",
  1525. ATTR_NAME_AUTOMIC_ADD_MEM_SIZE.c_str(), node_op_desc->GetName().c_str());
  1526. return FAILED);
  1527. std::stringstream ss;
  1528. for (auto iter : atomic_mem_start) {
  1529. ss << iter << " ";
  1530. }
  1531. string atomic_mem_start_str = ss.str();
  1532. ss.clear();
  1533. ss.str("");
  1534. for (auto iter : atomic_mem_size) {
  1535. ss << iter << " ";
  1536. }
  1537. string atomic_mem_size_str = ss.str();
  1538. GELOGI("[IMAS]SetAtomicCleanAttr : Set %s atomic_node name[%s] optype[%s] output[0] offset to [%s] streamid[%ld]"
  1539. " memtype[%ld] size[%s]",node->GetOwnerComputeGraph()->GetName().c_str(), node_op_desc->GetName().c_str(),
  1540. node->GetType().c_str(), atomic_mem_start_str.c_str(), node->GetOpDesc()->GetStreamId(), memory_type,
  1541. atomic_mem_size_str.c_str());
  1542. }
  1543. return SUCCESS;
  1544. }
  1545. void GraphMemoryAssigner::AlignMemOffset(const int64_t &mem_align_size, int64_t memory_type) {
  1546. if (mem_align_size <= 0) {
  1547. return;
  1548. }
  1549. auto iter = memory_offset_.find(memory_type);
  1550. if (iter == memory_offset_.end()) {
  1551. GELOGW("Memory offset don't have memory type[%ld].", memory_type);
  1552. return;
  1553. }
  1554. iter->second.mem_offset_ =
  1555. (iter->second.mem_offset_ + mem_align_size - 1) / mem_align_size * mem_align_size;
  1556. }
  1557. ge::Status GraphMemoryAssigner::GetNodeListMemoryType(const vector<NodePtr> &nodes, int32_t mem_reuse_model,
  1558. int64_t &memory_type) {
  1559. memory_type = RT_MEMORY_HBM;
  1560. // In the dynamic batch scenario, the memory attributes of nodes are the same.
  1561. for (auto &n : nodes) {
  1562. if (mem_reuse_model == kVirtualInputNodeMemoryReuse) {
  1563. GE_CHK_STATUS_RET(GetNodeMemoryType(n, memory_type, "input"),
  1564. "[Get][MemType:input]fail for node:%s", n->GetName().c_str())
  1565. break;
  1566. }
  1567. if (mem_reuse_model == kVirtualOutputNodeMemoryReuse) {
  1568. GE_CHK_STATUS_RET(GetNodeMemoryType(n, memory_type, "output"),
  1569. "[Get][MemType:output]fail for node:%s", n->GetName().c_str())
  1570. break;
  1571. }
  1572. }
  1573. return SUCCESS;
  1574. }
  1575. ge::Status GraphMemoryAssigner::GetNodeMemoryType(const NodePtr &node, int64_t &memory_type, string input_or_output) {
  1576. memory_type = RT_MEMORY_HBM;
  1577. vector<int64_t> mem_type_list;
  1578. if (input_or_output == "input") {
  1579. (void) ge::AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_INPUT_MEM_TYPE_LIST, mem_type_list);
  1580. }
  1581. if (input_or_output == "output") {
  1582. (void) ge::AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_OUTPUT_MEM_TYPE_LIST, mem_type_list);
  1583. }
  1584. if (mem_type_list.empty()) {
  1585. if (memory_offset_.find(memory_type) == memory_offset_.end()) {
  1586. std::string error = "Memory offset map does not have memory type" + FmtToStr(memory_type) +
  1587. + ", opname is " + FmtToStr(node->GetName()) + ", optype is " + FmtToStr(node->GetType());
  1588. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  1589. return FAILED;
  1590. }
  1591. return SUCCESS;
  1592. }
  1593. if (mem_type_list.size() != node->GetAllInDataAnchorsSize()) {
  1594. std::string error = "The size" + FmtToStr(mem_type_list.size()) +
  1595. " of mem type list is not equal to the size of in data anchor" +
  1596. FmtToStr(node->GetAllInDataAnchorsSize()) + ", opname is " +
  1597. FmtToStr(node->GetName()) + ", optype is " + FmtToStr(node->GetType());
  1598. GE_ERRORLOG_AND_ERRORMSG(FAILED, error.c_str());
  1599. return FAILED;
  1600. }
  1601. if (!CheckContinuousMemType(mem_type_list)) {
  1602. GELOGE(FAILED, "[Check][MemType:Continuous]fail for node:%s", node->GetName().c_str());
  1603. return FAILED;
  1604. }
  1605. // It is continuous memory and memory type is the same, so use the first memory.
  1606. memory_type = mem_type_list[0];
  1607. return SUCCESS;
  1608. }
  1609. bool GraphMemoryAssigner::CheckContinuousMemType(vector<int64_t> mem_type_list) {
  1610. if (mem_type_list.size() == 0) {
  1611. return true;
  1612. }
  1613. int64_t mem_type_tmp = mem_type_list[0];
  1614. for (auto mem_type : mem_type_list) {
  1615. if (mem_type != mem_type_tmp) {
  1616. std::string error = "The memory is continuous, but the type of the input memory is inconsistent. They are " +
  1617. FmtToStr(mem_type_tmp) + " and " + FmtToStr(mem_type);
  1618. ErrorManager::GetInstance().ATCReportErrMessage("E10043", {"reason"}, {error});
  1619. GELOGW("The memory is continuous, but the type of the input memory is inconsistent. They are [%ld] and [%ld].",
  1620. mem_type_tmp, mem_type);
  1621. return false;
  1622. }
  1623. }
  1624. if (memory_offset_.find(mem_type_tmp) == memory_offset_.end()) {
  1625. std::string error = "Memory offset map does not have memory type" + FmtToStr(mem_type_tmp);
  1626. ErrorManager::GetInstance().ATCReportErrMessage("E10043", {"reason"}, {error});
  1627. GELOGW("Memory offset map does not have memory type[%ld].", mem_type_tmp);
  1628. return false;
  1629. }
  1630. return true;
  1631. }
  1632. void GraphMemoryAssigner::PrintMemoryOffset() {
  1633. for (auto pair : memory_offset_) {
  1634. // Assign memory of max batch nodes that have the same batch label.
  1635. GELOGD("Reassign memory for max batch virtual nodes, memory type = %ld, memory offset = %zu.",
  1636. pair.first, pair.second.mem_offset_);
  1637. }
  1638. }
  1639. ge::Status GraphMemoryAssigner::TryGetNodeRefIndexes(const NodePtr &node, map<int32_t, int32_t> &out2ins) const{
  1640. // data and netoutput no need check because only data's output or netoutput's input is used
  1641. if (node->GetType() == DATA || node->GetType() == NETOUTPUT) {
  1642. return ge::SUCCESS;
  1643. }
  1644. for (const auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  1645. int32_t reuse_in_index = -1;
  1646. // nopadding means output[0] reuse input[0], but as history reason,
  1647. // other output index also return true for mem assign in block_mem_assigner
  1648. if (GraphUtils::IsNoPaddingRefFromInput(out_data_anchor, reuse_in_index)) {
  1649. out2ins.emplace(out_data_anchor->GetIdx(), reuse_in_index);
  1650. return ge::SUCCESS;
  1651. }
  1652. bool reuse_input_flag = GraphUtils::IsRefFromInput(out_data_anchor, reuse_in_index);
  1653. if (reuse_input_flag) {
  1654. if (node->GetInDataAnchor(reuse_in_index) != nullptr) {
  1655. out2ins.emplace(out_data_anchor->GetIdx(), reuse_in_index);
  1656. } else {
  1657. REPORT_INNER_ERROR("E19999", "Invalid reuse_input value %d on output %d of node %s, "
  1658. "please check attr reuse_input",
  1659. reuse_in_index, out_data_anchor->GetIdx(), node->GetName().c_str());
  1660. GELOGE(FAILED, "[Check][Attr]Invalid reuse_input value %d on output %d of node %s, "
  1661. "please check attr reuse_input",
  1662. reuse_in_index, out_data_anchor->GetIdx(), node->GetName().c_str());
  1663. return FAILED;
  1664. }
  1665. }
  1666. }
  1667. return ge::SUCCESS;
  1668. }
  1669. bool GraphMemoryAssigner::AssignContinuousInputMemoryWithAtomicProcessDirectly(
  1670. const NodePtr &input_continuous_node, map<NodePtr, uint32_t> &node_2_continuous_type) {
  1671. for (const auto &in_node : input_continuous_node->GetInDataNodes()) {
  1672. if (in_node->GetType() == VARIABLE) {
  1673. GELOGI("node %s 's precursor node %s is variable, do not store.", input_continuous_node->GetName().c_str(),
  1674. in_node->GetName().c_str());
  1675. return true;
  1676. }
  1677. auto iter = node_2_continuous_type.find(in_node);
  1678. // In node's topo order in the front, so function can not be exception
  1679. auto continuous_type = iter->second;
  1680. bool continuous_input = ((continuous_type & kTypeInput) != 0) || ((continuous_type & kTypeInputNoPadding) != 0);
  1681. if (continuous_input) {
  1682. GELOGI("[Store][Node] of %s cause it's precursor node %s need assign continuous input memory",
  1683. input_continuous_node->GetName().c_str(), in_node->GetName().c_str());
  1684. return false;
  1685. }
  1686. }
  1687. for (const auto &out_node : input_continuous_node->GetOutDataNodes()) {
  1688. auto continuous_type = GetContinuousMemoryType(out_node->GetOpDesc());
  1689. node_2_continuous_type.emplace(out_node, continuous_type);
  1690. bool continuous_input = ((continuous_type & kTypeInput) != 0) || ((continuous_type & kTypeInputNoPadding) != 0);
  1691. if (continuous_input) {
  1692. GELOGI("[Store][Node] of %s cause it's succeed node %s need assign continuous input memory",
  1693. input_continuous_node->GetName().c_str(), out_node->GetName().c_str());
  1694. return false;
  1695. }
  1696. }
  1697. return true;
  1698. }
  1699. ge::Status GraphMemoryAssigner::AssignContinuousInputMemoryWithAtomicProcess(const NodePtr &input_continuous_node,
  1700. uint32_t continuous_type,
  1701. bool reverse_refresh) {
  1702. int64_t mem_clean_start = 0;
  1703. int64_t mem_clean_size = 0;
  1704. int64_t memory_type = RT_MEMORY_HBM;
  1705. GE_CHK_STATUS_RET(GetNodeMemoryType(input_continuous_node, memory_type, "input"),
  1706. "[Get][MemType]fail for node:%s", input_continuous_node->GetName().c_str());
  1707. auto ret = AssignContinuousInputMemory(input_continuous_node, mem_clean_start, mem_clean_size, memory_type,
  1708. continuous_type, reverse_refresh);
  1709. if (ret != ge::SUCCESS) {
  1710. GELOGE(ret, "[Assign][Memory:Input:continuous]fail for node:%s", input_continuous_node->GetName().c_str());
  1711. return ret;
  1712. }
  1713. // Clean up atomic address, eg, hcom node
  1714. vector<int32_t> input_indexes;
  1715. // If GetListInt fail, input_indexes is empty.
  1716. (void)ge::AttrUtils::GetListInt(input_continuous_node->GetOpDesc(), ATOMIC_ATTR_INPUT_INDEX, input_indexes);
  1717. if (!input_indexes.empty() && input_indexes[0] == kAllInputAddrIsAtomic) {
  1718. // check whether there is an atomic conflict between the current node and the peer out node
  1719. if (!CheckInputIsSupportAtomic(input_continuous_node)) {
  1720. return ge::FAILED;
  1721. }
  1722. const auto &in_control_anchor = input_continuous_node->GetInControlAnchor();
  1723. GE_CHECK_NOTNULL(in_control_anchor);
  1724. for (const auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  1725. GE_CHECK_NOTNULL(peer_out_control_anchor);
  1726. auto peer_out_node = peer_out_control_anchor->GetOwnerNode();
  1727. if (peer_out_node->GetType() == ATOMICADDRCLEAN) {
  1728. ret = SetAtomicCleanAttr(peer_out_node, {mem_clean_start}, {mem_clean_size}, memory_type);
  1729. if (ret != SUCCESS) {
  1730. GELOGE(ret, "[Set][AtomicCleanAttr]fail for node:%s", peer_out_node->GetName().c_str());
  1731. return ret;
  1732. }
  1733. }
  1734. }
  1735. }
  1736. return ge::SUCCESS;
  1737. }
  1738. Status GraphMemoryAssigner::AssignBufferPoolMemory() {
  1739. auto is_buffer_pool_mem_enable = [] (const ComputeGraphPtr &graph) -> bool {
  1740. for (NodePtr &node : graph->GetAllNodes()) {
  1741. auto op_desc = node->GetOpDesc();
  1742. if (op_desc == nullptr) {
  1743. continue;
  1744. }
  1745. bool has_attrs = op_desc->HasAttr(ATTR_NAME_BUFFER_POOL_ID) && op_desc->HasAttr(ATTR_NAME_BUFFER_POOL_SIZE);
  1746. if (has_attrs) {
  1747. return true;
  1748. }
  1749. }
  1750. return false;
  1751. };
  1752. auto root_graph = GraphUtils::FindRootGraph(compute_graph_);
  1753. GE_CHECK_NOTNULL(root_graph);
  1754. if (root_graph->GetGraphUnknownFlag()) {
  1755. GELOGI("[Check][Enable]Unknown root graph does not support buffer pool memory, graph:%s.",
  1756. compute_graph_->GetName().c_str());
  1757. return SUCCESS;
  1758. }
  1759. if (!is_buffer_pool_mem_enable(compute_graph_)) {
  1760. GELOGD("[Check][Enable]Buffer pool memory is not enable, graph:%s.", compute_graph_->GetName().c_str());
  1761. return SUCCESS;
  1762. }
  1763. map<int64_t, size_t> mem_type_to_offset;
  1764. for (const auto &pair : memory_offset_) {
  1765. mem_type_to_offset[pair.first] = pair.second.mem_offset_;
  1766. }
  1767. BufferPoolMemAssigner buffer_pool_mem_assigner(compute_graph_, mem_type_to_offset);
  1768. Status status = buffer_pool_mem_assigner.Assign();
  1769. if (status != SUCCESS) {
  1770. GELOGE(status, "[Assign][BufferPoolMem]Graph:%s.", compute_graph_->GetName().c_str());
  1771. REPORT_INNER_ERROR("E19999", "Failed to assign buffer pool memory, graph:%s.", compute_graph_->GetName().c_str());
  1772. return status;
  1773. }
  1774. int64_t mem_type = buffer_pool_mem_assigner.GetMemType();
  1775. auto iter = memory_offset_.find(mem_type);
  1776. if (iter == memory_offset_.end()) {
  1777. GELOGE(FAILED, "[Check][MemType]Memory type is not supported, graph:%s, mem type:%ld.",
  1778. compute_graph_->GetName().c_str(), mem_type);
  1779. REPORT_INNER_ERROR("E19999", "Memory type is not supported, graph:%s, mem type:%ld.",
  1780. compute_graph_->GetName().c_str(), mem_type);
  1781. return FAILED;
  1782. }
  1783. iter->second.mem_offset_ = buffer_pool_mem_assigner.GetMemOffset();
  1784. GELOGI("[Assign][BufferPoolMem]Assign buffer pool memory successfully, graph:%s, mem type:%ld, mem offset:%zu.",
  1785. compute_graph_->GetName().c_str(), mem_type, buffer_pool_mem_assigner.GetMemOffset());
  1786. return SUCCESS;
  1787. }
  1788. } // namespace ge

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