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

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