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

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

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