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

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

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