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

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

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