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

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

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