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

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

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