You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

graph_mem_assigner.cc 106 kB

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

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