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

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