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.

set_input_output_offset_pass.cc 18 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/passes/set_input_output_offset_pass.h"
  17. #include "runtime/mem.h"
  18. namespace ge {
  19. Status SetInputOutputOffsetPass::Run(ComputeGraphPtr graph) {
  20. GE_CHECK_NOTNULL(graph);
  21. for (auto &node : graph->GetDirectNode()) {
  22. GE_CHECK_NOTNULL(node->GetOpDesc());
  23. vector<int> connect_input;
  24. (void)AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_NODE_CONNECT_INPUT, connect_input);
  25. if (!connect_input.empty()) {
  26. Status ret = SetInputOffset(node, connect_input);
  27. if (ret != SUCCESS) {
  28. GELOGE(ret, "[Set][InputOffset] for node:%s failed.", node->GetName().c_str());
  29. return ret;
  30. }
  31. }
  32. vector<int> connect_output;
  33. (void)AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_NODE_CONNECT_OUTPUT, connect_output);
  34. if (!connect_output.empty()) {
  35. Status ret = SetOutputOffset(node, connect_output);
  36. if (ret != SUCCESS) {
  37. GELOGE(ret, "[Set][OutputOffset] for node:%s failed.", node->GetName().c_str());
  38. return ret;
  39. }
  40. }
  41. }
  42. return SUCCESS;
  43. }
  44. Status SetInputOutputOffsetPass::SetInputOffsetForFusion(const std::vector<int64_t> &memory_type,
  45. const ge::NodePtr &node) {
  46. GELOGI("Start to SetInputOffsetForFusion for %s", node->GetName().c_str());
  47. auto op_desc = node->GetOpDesc();
  48. for (size_t i = 0; i < memory_type.size(); ++i) {
  49. if (memory_type.at(i) != RT_MEMORY_L1) {
  50. std::vector<int64_t> input_offset_of_node;
  51. input_offset_of_node = op_desc->GetInputOffset();
  52. if (input_offset_of_node.size() < i) {
  53. REPORT_INNER_ERROR("E19999", "Input offsets size:%zu of node:%s(%s) < index:%zu, check invalid",
  54. input_offset_of_node.size(), op_desc->GetName().c_str(), op_desc->GetType().c_str(), i);
  55. GELOGE(PARAM_INVALID, "[Check][Param] Input offsets size:%zu of node:%s(%s) < index:%zu",
  56. input_offset_of_node.size(), op_desc->GetName().c_str(), op_desc->GetType().c_str(), i);
  57. return PARAM_INVALID;
  58. }
  59. int64_t input_offset = input_offset_of_node.at(i);
  60. GELOGI("input_offset of %s is %ld.", node->GetName().c_str(), input_offset);
  61. auto in_anchor = node->GetInDataAnchor(i);
  62. GE_IF_BOOL_EXEC(in_anchor == nullptr, continue);
  63. auto peer_out_anchor = in_anchor->GetPeerOutAnchor();
  64. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  65. int out_index = peer_out_anchor->GetIdx();
  66. auto data_op_desc = peer_out_anchor->GetOwnerNode()->GetOpDesc();
  67. GE_CHECK_NOTNULL(data_op_desc);
  68. int64_t out_offset = data_op_desc->GetOutputOffset().at(out_index);
  69. GELOGI("output_offset of %s is %ld.", peer_out_anchor->GetOwnerNode()->GetName().c_str(), out_offset);
  70. vector<int64_t> zero_copy_basic_offset;
  71. vector<int64_t> zero_copy_relative_offset;
  72. (void)ge::AttrUtils::GetListInt(data_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset);
  73. (void)ge::AttrUtils::GetListInt(data_op_desc, ATTR_ZERO_COPY_RELATIVE_OFFSET, zero_copy_relative_offset);
  74. zero_copy_basic_offset.emplace_back(out_offset);
  75. int64_t relative_offset = input_offset - out_offset;
  76. zero_copy_relative_offset.emplace_back(relative_offset);
  77. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(data_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset),
  78. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  79. ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  80. data_op_desc->GetName().c_str(), data_op_desc->GetType().c_str());
  81. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  82. data_op_desc->GetName().c_str(), data_op_desc->GetType().c_str());
  83. return FAILED);
  84. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(data_op_desc, ATTR_ZERO_COPY_RELATIVE_OFFSET,
  85. zero_copy_relative_offset),
  86. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  87. ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  88. data_op_desc->GetName().c_str(), data_op_desc->GetType().c_str());
  89. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  90. data_op_desc->GetName().c_str(), data_op_desc->GetType().c_str());
  91. return FAILED);
  92. }
  93. }
  94. return SUCCESS;
  95. }
  96. Status SetInputOutputOffsetPass::SetInputOffsetForHcom(const ge::NodePtr &node, const vector<int> &connect_input) {
  97. GELOGI("Start SetInputOffsetForHcom for %s.", node->GetName().c_str());
  98. auto op_desc = node->GetOpDesc();
  99. vector<int64_t> input_offset_of_node;
  100. input_offset_of_node = node->GetOpDesc()->GetInputOffset();
  101. for (size_t input_index = 0; input_index < connect_input.size(); ++input_index) {
  102. int connect_input_index = connect_input.at(input_index);
  103. int64_t input_offset = input_offset_of_node.at(connect_input_index);
  104. NodePtr in_data = node->GetInDataNodes().at(connect_input_index);
  105. auto in_op_desc = in_data->GetOpDesc();
  106. GE_CHECK_NOTNULL(in_op_desc);
  107. if (in_op_desc->GetType() == DATA) {
  108. int64_t output_offset = in_op_desc->GetOutputOffset().at(0);
  109. if (output_offset == input_offset) {
  110. continue;
  111. } else {
  112. vector<int64_t> zero_copy_basic_offset;
  113. vector<int64_t> zero_copy_relative_offset;
  114. (void)ge::AttrUtils::GetListInt(in_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset);
  115. (void)ge::AttrUtils::GetListInt(in_op_desc, ATTR_ZERO_COPY_RELATIVE_OFFSET, zero_copy_relative_offset);
  116. GELOGI("input offset from %s to %s is %ld to %ld.", in_op_desc->GetName().c_str(), op_desc->GetName().c_str(),
  117. output_offset, input_offset);
  118. int64_t relative_offset = input_offset - output_offset;
  119. zero_copy_basic_offset.emplace_back(output_offset);
  120. zero_copy_relative_offset.emplace_back(relative_offset);
  121. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(in_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset),
  122. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  123. ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  124. in_op_desc->GetName().c_str(), in_op_desc->GetType().c_str());
  125. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  126. in_op_desc->GetName().c_str(), in_op_desc->GetType().c_str());
  127. return FAILED);
  128. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(in_op_desc,
  129. ATTR_ZERO_COPY_RELATIVE_OFFSET, zero_copy_relative_offset),
  130. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  131. ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  132. in_op_desc->GetName().c_str(), in_op_desc->GetType().c_str());
  133. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  134. in_op_desc->GetName().c_str(), in_op_desc->GetType().c_str());
  135. return FAILED);
  136. }
  137. }
  138. }
  139. return SUCCESS;
  140. }
  141. Status SetInputOutputOffsetPass::SetInputOffset(const NodePtr &node, const vector<int> &connect_input) {
  142. GELOGD("Start to SetInputOffset for %s.", node->GetName().c_str());
  143. std::vector<int64_t> memory_type;
  144. auto op_desc = node->GetOpDesc();
  145. (void)ge::AttrUtils::GetListInt(op_desc, ATTR_NAME_INPUT_MEM_TYPE_LIST, memory_type);
  146. if (!memory_type.empty()) {
  147. Status ret = SetInputOffsetForFusion(memory_type, node);
  148. if (ret != SUCCESS) {
  149. GELOGE(ret, "[Set][InputOffset] For Fusion failed, node:%s.", node->GetName().c_str());
  150. return ret;
  151. }
  152. }
  153. // Data->Hcom
  154. bool is_input_continuous = false;
  155. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_INPUT, is_input_continuous);
  156. if (is_input_continuous) {
  157. Status ret = SetInputOffsetForHcom(node, connect_input);
  158. if (ret != SUCCESS) {
  159. GELOGE(ret, "[Set][InputOffset] For Hcom failed, node:%s.", node->GetName().c_str());
  160. return ret;
  161. }
  162. }
  163. return SUCCESS;
  164. }
  165. Status SetInputOutputOffsetPass::SetOutputOffsetForConcat(const NodePtr &node) {
  166. GELOGI("Start SetOutputOffsetForConcat for %s.", node->GetName().c_str());
  167. auto op_desc = node->GetOpDesc();
  168. std::vector<int64_t> output_offset_of_concat;
  169. output_offset_of_concat = op_desc->GetOutputOffset();
  170. // phony_concat has one output
  171. GE_IF_BOOL_EXEC(output_offset_of_concat.size() != 1,
  172. REPORT_INNER_ERROR("E19999", "Output offsets size:%zu of node:%s(%s) not equal to 1, check invalid",
  173. output_offset_of_concat.size(),
  174. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  175. GELOGE(PARAM_INVALID, "[Check][Param] Output offsets size:%zu of node:%s(%s) not equal to 1.",
  176. output_offset_of_concat.size(), op_desc->GetName().c_str(), op_desc->GetType().c_str());
  177. return PARAM_INVALID);
  178. NodePtr net_output = node->GetOutDataNodes().at(0);
  179. auto out_op_desc = net_output->GetOpDesc();
  180. GE_CHECK_NOTNULL(out_op_desc);
  181. vector<int64_t> zero_copy_basic_offset;
  182. vector<int64_t> zero_copy_relative_offset;
  183. (void)ge::AttrUtils::GetListInt(out_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset);
  184. (void)ge::AttrUtils::GetListInt(out_op_desc, ATTR_ZERO_COPY_RELATIVE_OFFSET, zero_copy_relative_offset);
  185. int64_t basic_offset = output_offset_of_concat.at(0);
  186. GELOGI("output_offset of %s is %ld.", op_desc->GetName().c_str(), basic_offset);
  187. for (InDataAnchorPtr &in_anchor : node->GetAllInDataAnchors()) {
  188. OutDataAnchorPtr peer_out_anchor = in_anchor->GetPeerOutAnchor();
  189. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  190. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  191. auto out_index = peer_out_anchor->GetIdx();
  192. std::vector<int64_t> output_offset_of_in_node;
  193. GE_CHECK_NOTNULL(in_node->GetOpDesc());
  194. output_offset_of_in_node = in_node->GetOpDesc()->GetOutputOffset();
  195. GELOGI("input offset from %s to %s is %ld.", in_node->GetName().c_str(), op_desc->GetName().c_str(),
  196. output_offset_of_in_node.at(out_index));
  197. int64_t relative_offset = output_offset_of_in_node.at(out_index) - basic_offset;
  198. zero_copy_basic_offset.emplace_back(basic_offset);
  199. zero_copy_relative_offset.emplace_back(relative_offset);
  200. }
  201. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(out_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset),
  202. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  203. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  204. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  205. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  206. return FAILED);
  207. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(out_op_desc, ATTR_ZERO_COPY_RELATIVE_OFFSET, zero_copy_relative_offset),
  208. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  209. ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  210. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  211. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  212. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  213. return FAILED);
  214. return SUCCESS;
  215. }
  216. Status SetInputOutputOffsetPass::SetOutputOffsetForHcom(const NodePtr &node, const vector<int> &connect_output) {
  217. GELOGI("Start SetOutputOffsetForHcom, %s connect with %zu output.", node->GetName().c_str(), connect_output.size());
  218. vector<int64_t> output_offset_of_node;
  219. output_offset_of_node = node->GetOpDesc()->GetOutputOffset();
  220. int connect_output_index = connect_output.at(0);
  221. int64_t basic_offset = output_offset_of_node.at(connect_output_index);
  222. GELOGI("basic_offset of %s is %ld.", node->GetName().c_str(), basic_offset);
  223. NodePtr net_output = node->GetOutDataNodes().at(connect_output_index);
  224. auto out_op_desc = net_output->GetOpDesc();
  225. GE_CHECK_NOTNULL(out_op_desc);
  226. vector<int64_t> zero_copy_basic_offset;
  227. vector<int64_t> zero_copy_relative_offset;
  228. (void)ge::AttrUtils::GetListInt(out_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset);
  229. (void)ge::AttrUtils::GetListInt(out_op_desc, ATTR_ZERO_COPY_RELATIVE_OFFSET, zero_copy_relative_offset);
  230. for (auto &out_anchor : node->GetAllOutDataAnchors()) {
  231. GE_IF_BOOL_EXEC(out_anchor == nullptr, continue);
  232. for (auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  233. GE_IF_BOOL_EXEC(in_anchor == nullptr, continue);
  234. if (in_anchor->GetOwnerNode()->GetType() == NETOUTPUT && out_anchor->GetIdx() != connect_output_index) {
  235. continue;
  236. } else {
  237. NodePtr out_node = in_anchor->GetOwnerNode();
  238. auto in_index = in_anchor->GetIdx();
  239. std::vector<int64_t> input_offset_of_out_node;
  240. GE_CHECK_NOTNULL(out_node->GetOpDesc());
  241. input_offset_of_out_node = out_node->GetOpDesc()->GetInputOffset();
  242. GELOGI("input offset from %s to %s is %ld.", node->GetName().c_str(), out_node->GetName().c_str(),
  243. input_offset_of_out_node.at(in_index));
  244. int64_t relative_offset = input_offset_of_out_node.at(in_index) - basic_offset;
  245. zero_copy_basic_offset.emplace_back(basic_offset);
  246. zero_copy_relative_offset.emplace_back(relative_offset);
  247. }
  248. }
  249. }
  250. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(out_op_desc, ATTR_ZERO_COPY_BASIC_OFFSET, zero_copy_basic_offset),
  251. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  252. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  253. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_BASIC_OFFSET.c_str(),
  254. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  255. return FAILED);
  256. GE_CHK_BOOL_EXEC(ge::AttrUtils::SetListInt(out_op_desc, ATTR_ZERO_COPY_RELATIVE_OFFSET, zero_copy_relative_offset),
  257. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  258. ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  259. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  260. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_ZERO_COPY_RELATIVE_OFFSET.c_str(),
  261. out_op_desc->GetName().c_str(), out_op_desc->GetType().c_str());
  262. return FAILED);
  263. return SUCCESS;
  264. }
  265. Status SetInputOutputOffsetPass::SetOutputOffset(const NodePtr &node, const vector<int> &connect_output) {
  266. GELOGD("Start SetOutputOffset of %s.", node->GetName().c_str());
  267. bool attr_no_task = false;
  268. bool get_attr_no_task = ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_NOTASK, attr_no_task);
  269. if (get_attr_no_task && attr_no_task) {
  270. bool is_input_continuous = false;
  271. (void)ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_CONTINUOUS_INPUT, is_input_continuous);
  272. bool buffer_fusion = CheckBufferFusion(node);
  273. // A/B/C -> Phony_concat -> Netoutput : input_continuous
  274. if (is_input_continuous || buffer_fusion) {
  275. Status ret = SetOutputOffsetForConcat(node);
  276. if (ret != SUCCESS) {
  277. GELOGE(ret, "[Set][OutputOffset] For Concat failed, node:%s.", node->GetName().c_str());
  278. return ret;
  279. }
  280. }
  281. }
  282. // allreduce->netoutput : output_continuous
  283. bool is_output_continuous = false;
  284. (void)ge::AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_CONTINUOUS_OUTPUT, is_output_continuous);
  285. if (is_output_continuous) {
  286. Status ret = SetOutputOffsetForHcom(node, connect_output);
  287. if (ret != SUCCESS) {
  288. GELOGE(ret, "[Set][OutputOffset] For Hcom failed, node:%s.", node->GetName().c_str());
  289. return ret;
  290. }
  291. }
  292. return SUCCESS;
  293. }
  294. bool SetInputOutputOffsetPass::CheckBufferFusion(const NodePtr &node) {
  295. for (auto &in_node : node->GetInDataNodes()) {
  296. GE_CHECK_NOTNULL(in_node);
  297. auto op_desc = in_node->GetOpDesc();
  298. GE_CHECK_NOTNULL(op_desc);
  299. if (!op_desc->HasAttr(ATTR_NAME_OUTPUT_OFFSET_FOR_BUFFER_FUSION)) {
  300. GELOGI("The node: %s not have ATTR_NAME_OUTPUT_OFFSET_FOR_BUFFER_FUSION.", node->GetName().c_str());
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. } // namespace ge

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