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

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

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