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

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