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

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

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