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.

transpose_strategy.cc 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * Copyright 2021 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 "tools/optimizer/graph/transpose_strategy.h"
  17. #include <algorithm>
  18. #include <memory>
  19. #include <vector>
  20. #include <string>
  21. #include <utility>
  22. #include "ops/crop.h"
  23. #include "ops/fusion/activation.h"
  24. #include "ops/fusion/slice_fusion.h"
  25. #include "ops/op_utils.h"
  26. #include "ops/strided_slice.h"
  27. namespace mindspore {
  28. namespace opt {
  29. namespace {
  30. constexpr size_t kFirstInput = 1;
  31. constexpr size_t kTransposePerm = 2;
  32. constexpr size_t kOnnxStridedSlice = 6;
  33. const std::vector<int> NH2NC = {0, 3, 1, 2};
  34. const std::vector<int> NC2NH = {0, 2, 3, 1};
  35. STATUS GetPostNodes(const FuncGraphPtr &func_graph, const CNodePtr &cnode, std::vector<AnfNodePtr> *out_nodes) {
  36. auto manager = func_graph->manager();
  37. if (manager == nullptr) {
  38. manager = Manage(func_graph, true);
  39. }
  40. if (manager == nullptr) {
  41. MS_LOG(ERROR) << "manager is nullptr.";
  42. return lite::RET_ERROR;
  43. }
  44. auto node_users = manager->node_users()[cnode];
  45. if (node_users.empty()) {
  46. MS_LOG(ERROR) << "cnode is isolated.";
  47. return lite::RET_ERROR;
  48. }
  49. std::transform(node_users.begin(), node_users.end(), std::back_inserter(*out_nodes),
  50. [](const std::pair<AnfNodePtr, int> &node_user) { return node_user.first; });
  51. return lite::RET_OK;
  52. }
  53. } // namespace
  54. AnfNodePtr TransposeStrategy::TransposePairFuseWhenInsert(const FuncGraphPtr &func_graph, const CNodePtr &cnode,
  55. const std::vector<int> &perm, bool before, size_t index) {
  56. MS_ASSERT(func_graph != nullptr && cnode != nullptr);
  57. AnfNodePtr trans_input_node = before ? cnode->input(index) : cnode;
  58. // judge pair transpose after insert.
  59. if (CheckPrimitiveType(trans_input_node, prim::kPrimTranspose)) {
  60. std::vector<int> trans_perm;
  61. auto input_cnode = trans_input_node->cast<CNodePtr>();
  62. if (input_cnode == nullptr) {
  63. MS_LOG(ERROR) << "input node is invalid.";
  64. return nullptr;
  65. }
  66. if (GetTransposePerm(input_cnode, &trans_perm) != lite::RET_OK) {
  67. MS_LOG(ERROR) << "transpose perm get failed.";
  68. return nullptr;
  69. }
  70. if ((perm == NH2NC && trans_perm == NC2NH) || (perm == NC2NH && trans_perm == NH2NC)) {
  71. return input_cnode->input(kFirstInput);
  72. }
  73. }
  74. // insert depend on shape
  75. return TransposeDependOnShape(func_graph, cnode, perm, before, index);
  76. }
  77. AnfNodePtr TransposeStrategy::TransposeDependOnShape(const FuncGraphPtr &func_graph, const CNodePtr &cnode,
  78. const std::vector<int> &perm, bool before, size_t index) {
  79. MS_ASSERT(func_graph != nullptr && cnode != nullptr);
  80. AnfNodePtr trans_input_node = before ? cnode->input(index) : cnode;
  81. auto status = TransposeInsertDependOnShape(func_graph, cnode, before, index);
  82. if (status == lite::RET_ERROR) {
  83. return nullptr;
  84. } else if (status == lite::RET_NO_CHANGE) {
  85. return before ? cnode->input(index) : cnode;
  86. }
  87. // insert tranpsoe
  88. std::string trans_name =
  89. before ? cnode->fullname_with_scope() + "_pre" + std::to_string(index - 1) : cnode->fullname_with_scope() + "_post";
  90. auto trans_insert_node = GenTransposeNode(func_graph, trans_input_node, perm, trans_name);
  91. return trans_insert_node;
  92. }
  93. bool TransposeStrategy::CanFusionIfInsert(const FuncGraphPtr &func_graph, const CNodePtr &cnode,
  94. TransTypePair *trans_info, TransTypePair *trans_insert_info) {
  95. MS_ASSERT(func_graph != nullptr && cnode != nullptr);
  96. MS_ASSERT(pre_type != nullptr && post_type != nullptr);
  97. size_t trans_count = 0;
  98. std::vector<AnfNodePtr> in_nodes;
  99. for (size_t i = 1; i < cnode->size(); ++i) {
  100. if (utils::isa<CNodePtr>(cnode->input(i))) {
  101. in_nodes.push_back(cnode->input(i));
  102. }
  103. }
  104. if (!IsInOutCanFuison(func_graph, in_nodes, &trans_count, &trans_info->pre_)) {
  105. return false;
  106. }
  107. std::vector<AnfNodePtr> out_nodes;
  108. if (GetPostNodes(func_graph, cnode, &out_nodes) != lite::RET_OK) {
  109. return false;
  110. }
  111. if (!IsInOutCanFuison(func_graph, out_nodes, &trans_count, &trans_info->post_)) {
  112. return false;
  113. }
  114. if (trans_info->pre_ == trans_info->post_) {
  115. return false;
  116. }
  117. auto total_node_count = in_nodes.size() + out_nodes.size();
  118. bool can_insert = trans_count > total_node_count / 2;
  119. if (CheckPrimitiveType(cnode, prim::kPrimActivation)) {
  120. auto prim_act = GetValueNode<std::shared_ptr<ops::Activation>>(cnode->input(0));
  121. MS_ASSERT(prim_act != nullptr);
  122. if (prim_act->get_activation_type() == mindspore::ActivationType::LEAKY_RELU) {
  123. can_insert = trans_count >= total_node_count / 2;
  124. }
  125. }
  126. if (CheckPrimitiveType(cnode, prim::kPrimSplit) || CheckPrimitiveType(cnode, prim::kPrimQuantDTypeCast)) {
  127. can_insert = trans_count >= total_node_count / 2;
  128. }
  129. if (!can_insert) {
  130. return can_insert;
  131. }
  132. DecidePreAndPostTransType(trans_info, trans_insert_info);
  133. return can_insert;
  134. }
  135. bool TransposeStrategy::CanChangeOpAxis(const FuncGraphPtr &func_graph, const CNodePtr &cnode) {
  136. MS_ASSERT(func_graph != nullptr && cnode != nullptr);
  137. auto shape = node_infer_shape_.GetInputShape(cnode, 1);
  138. if (shape.size() != 4) {
  139. if (cnode->size() > 2) {
  140. shape = node_infer_shape_.GetInputShape(cnode, 2);
  141. if (shape.size() != 4 && !shape.empty()) {
  142. return false;
  143. }
  144. } else {
  145. return false;
  146. }
  147. }
  148. if (CheckPrimitiveType(cnode, prim::kPrimConcat) || CheckPrimitiveType(cnode, prim::kPrimSplit)) {
  149. auto prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  150. if (prim->GetAttr(ops::kAxis) == nullptr) {
  151. return false;
  152. }
  153. }
  154. if (CheckPrimitiveType(cnode, prim::kPrimSliceFusion) || CheckPrimitiveType(cnode, prim::kPrimStridedSlice)) {
  155. for (size_t i = 2; i < cnode->size(); ++i) {
  156. if (utils::isa<CNodePtr>(cnode->input(i))) {
  157. return false;
  158. }
  159. }
  160. if (CheckPrimitiveType(cnode, prim::kPrimStridedSlice) && cnode->size() != kOnnxStridedSlice) {
  161. return false;
  162. }
  163. }
  164. return true;
  165. }
  166. STATUS TransposeStrategy::ChangeOpAxis(const FuncGraphPtr &func_graph, const CNodePtr &cnode) {
  167. MS_ASSERT(func_graph != nullptr && cnode != nullptr);
  168. auto shape = node_infer_shape_.GetInputShape(cnode, 1);
  169. if (shape.size() != 4) {
  170. if (cnode->size() > 2) {
  171. shape = node_infer_shape_.GetInputShape(cnode, 2);
  172. if (shape.size() != 4 && !shape.empty()) {
  173. return lite::RET_NOT_SUPPORT;
  174. }
  175. } else {
  176. return lite::RET_NOT_SUPPORT;
  177. }
  178. }
  179. auto axis_map = GetNC2NHAxisMap();
  180. if (CheckPrimitiveType(cnode, prim::kPrimConcat) || CheckPrimitiveType(cnode, prim::kPrimSplit)) {
  181. auto prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  182. if (prim->GetAttr(ops::kAxis) == nullptr) {
  183. return lite::RET_NOT_SUPPORT;
  184. }
  185. auto axis = GetValue<int64_t>(prim->GetAttr(ops::kAxis));
  186. auto new_axis = axis_map[axis < 0 ? axis + 4 : axis];
  187. prim->AddAttr(ops::kAxis, MakeValue<int64_t>(new_axis));
  188. }
  189. if (CheckPrimitiveType(cnode, prim::kPrimCrop)) {
  190. auto crop_prim = GetValueNode<std::shared_ptr<ops::Crop>>(cnode->input(0));
  191. if (crop_prim == nullptr) {
  192. return lite::RET_NULL_PTR;
  193. }
  194. auto axis = crop_prim->get_axis();
  195. auto offsets = crop_prim->get_offsets();
  196. auto new_axis = axis_map[axis < 0 ? axis + 4 : axis];
  197. if (new_axis == 0) {
  198. offsets = {offsets[0], offsets[2], offsets[3], offsets[1]};
  199. } else if (new_axis == 3) {
  200. offsets = {offsets[1], offsets[2], offsets[0]};
  201. } else {
  202. offsets.push_back(0);
  203. }
  204. crop_prim->set_axis(new_axis);
  205. crop_prim->set_offsets(offsets);
  206. }
  207. if (CheckPrimitiveType(cnode, prim::kPrimSliceFusion)) {
  208. return ChangeOpSlice(func_graph, cnode);
  209. }
  210. if (CheckPrimitiveType(cnode, prim::kPrimStridedSlice)) {
  211. return ChangeOpStrideSlice(func_graph, cnode);
  212. }
  213. return lite::RET_OK;
  214. }
  215. STATUS TransposeStrategy::TransposeInsertDependOnShape(const FuncGraphPtr &func_graph, const CNodePtr &cnode,
  216. bool before, size_t index) {
  217. MS_ASSERT(func_graph != nullptr && cnode != nullptr);
  218. auto manager = func_graph->manager();
  219. if (manager == nullptr) {
  220. manager = Manage(func_graph, true);
  221. }
  222. if (manager == nullptr) {
  223. MS_LOG(ERROR) << "manager is nullptr.";
  224. return lite::RET_ERROR;
  225. }
  226. auto node_users = manager->node_users()[cnode];
  227. if (node_users.empty()) {
  228. MS_LOG(ERROR) << "cnode is isolated.";
  229. return lite::RET_ERROR;
  230. }
  231. if (!utils::isa<CNodePtr>(node_users.front().first)) {
  232. return lite::RET_ERROR;
  233. }
  234. CNodePtr base_node = before ? cnode : node_users.front().first->cast<CNodePtr>();
  235. size_t input_index = before ? index : node_users.front().second;
  236. auto shape = node_infer_shape_.GetInputShape(base_node, input_index);
  237. if (!shape.empty() && shape.size() != NH2NC.size()) {
  238. return lite::RET_NO_CHANGE;
  239. }
  240. return lite::RET_OK;
  241. }
  242. bool TransposeStrategy::IsInOutCanFuison(const FuncGraphPtr &func_graph, const std::vector<AnfNodePtr> &nodes,
  243. size_t *trans_count, FormatTransNodeType *trans_type) {
  244. MS_ASSERT(func_graph != nullptr);
  245. MS_ASSERT(trans_count != nullptr && trans_type != nullptr);
  246. for (auto &node : nodes) {
  247. if (CheckPrimitiveType(node, prim::kPrimTranspose)) {
  248. FormatTransNodeType cur_type;
  249. std::vector<int> perm;
  250. auto cnode = node->cast<CNodePtr>();
  251. if (cnode == nullptr) {
  252. return false;
  253. }
  254. if (GetTransposePerm(cnode, &perm) != lite::RET_OK) {
  255. return false;
  256. }
  257. if (perm == NH2NC) {
  258. cur_type = kNHWC2NCHW;
  259. } else if (perm == NC2NH) {
  260. cur_type = kNCHW2NHWC;
  261. } else {
  262. return false;
  263. }
  264. if (*trans_type == kNONE) {
  265. *trans_type = cur_type;
  266. } else if (*trans_type != cur_type) {
  267. return false;
  268. }
  269. *trans_count += 1;
  270. }
  271. }
  272. return true;
  273. }
  274. void TransposeStrategy::DecidePreAndPostTransType(TransTypePair *trans_info, TransTypePair *trans_insert_info) {
  275. if (trans_info->pre_ == trans_info->post_) {
  276. return;
  277. }
  278. if (trans_info->pre_ != kNONE && trans_info->post_ != kNONE) {
  279. trans_insert_info->pre_ = trans_info->pre_ == kNHWC2NCHW ? kNCHW2NHWC : kNHWC2NCHW;
  280. trans_insert_info->post_ = trans_info->post_ == kNHWC2NCHW ? kNCHW2NHWC : kNHWC2NCHW;
  281. } else if (trans_info->pre_ == kNONE) {
  282. trans_insert_info->pre_ = trans_info->post_ == kNHWC2NCHW ? kNHWC2NCHW : kNCHW2NHWC;
  283. trans_insert_info->post_ = trans_info->post_ == kNHWC2NCHW ? kNCHW2NHWC : kNHWC2NCHW;
  284. } else {
  285. trans_insert_info->pre_ = trans_info->pre_ == kNHWC2NCHW ? kNCHW2NHWC : kNHWC2NCHW;
  286. trans_insert_info->post_ = trans_info->pre_ == kNHWC2NCHW ? kNHWC2NCHW : kNCHW2NHWC;
  287. }
  288. }
  289. STATUS TransposeStrategy::ChangeOpSlice(const FuncGraphPtr &func_graph, const CNodePtr &cnode) {
  290. MS_ASSERT(cnode != nullptr);
  291. for (size_t i = 2; i < cnode->size(); ++i) {
  292. if (utils::isa<CNodePtr>(cnode->input(i))) {
  293. return lite::RET_NOT_SUPPORT;
  294. }
  295. }
  296. auto shape = node_infer_shape_.GetInputShape(cnode, 2);
  297. if (shape.empty()) {
  298. return lite::RET_NOT_SUPPORT;
  299. }
  300. int element_num = shape.front();
  301. auto prim = GetValueNode<std::shared_ptr<ops::SliceFusion>>(cnode->input(0));
  302. std::vector<int> axes;
  303. if (prim->GetAttr(ops::kAxes) == nullptr || prim->get_axes().empty()) {
  304. for (int index = 0; index < element_num; ++index) {
  305. axes.push_back(index);
  306. }
  307. } else {
  308. auto origin_axes = prim->get_axes();
  309. std::transform(origin_axes.begin(), origin_axes.end(), std::back_inserter(axes),
  310. [](int64_t v) { return static_cast<int>(v); });
  311. }
  312. for (size_t i = 2; i < cnode->size(); ++i) {
  313. TransformAttrByAxes(func_graph, cnode, i, axes);
  314. }
  315. auto tmp_axes = TransformOpAxesAttr(axes);
  316. std::vector<int64_t> new_axes(tmp_axes.begin(), tmp_axes.end());
  317. prim->set_axes(new_axes);
  318. return lite::RET_OK;
  319. }
  320. STATUS TransposeStrategy::ChangeOpStrideSlice(const FuncGraphPtr &func_graph, const CNodePtr &cnode) {
  321. if (cnode->size() != kOnnxStridedSlice) {
  322. return lite::RET_NOT_SUPPORT;
  323. }
  324. for (size_t i = 2; i < cnode->size(); ++i) {
  325. if (utils::isa<CNodePtr>(cnode->input(i))) {
  326. return lite::RET_NOT_SUPPORT;
  327. }
  328. }
  329. std::vector<int> axes = node_infer_shape_.GetIntVecInput(cnode, kOnnxStridedSlice - 2);
  330. if (axes.empty()) {
  331. MS_LOG(ERROR) << "strided slice input invalid.";
  332. return lite::RET_ERROR;
  333. }
  334. for (size_t index = 2; index < cnode->size(); ++index) {
  335. if (index == 4) {
  336. continue;
  337. }
  338. TransformAttrByAxes(func_graph, cnode, index, axes);
  339. }
  340. auto cur_axes = TransformOpAxesAttr(axes);
  341. auto param_node = BuildIntVecParameterNode(func_graph, cur_axes, cnode->input(4)->fullname_with_scope());
  342. func_graph->manager()->Replace(cnode->input(4), param_node);
  343. return lite::RET_OK;
  344. }
  345. void TransposeStrategy::TransformAttrByAxes(const FuncGraphPtr &func_graph, const CNodePtr &cnode, size_t input_index,
  346. const std::vector<int> &axes) {
  347. if (cnode == nullptr || input_index >= cnode->size() || axes.empty()) {
  348. return;
  349. }
  350. auto axis_map = GetNC2NHAxisMap();
  351. auto origin_input = node_infer_shape_.GetIntVecInput(cnode, input_index);
  352. if (origin_input.size() != axes.size()) {
  353. return;
  354. }
  355. std::vector<int> cur_input;
  356. for (int dim = 0; dim < 4; ++dim) {
  357. for (size_t index = 0; index < axes.size(); ++index) {
  358. int nhwc_dim = axis_map[axes[index] < 0 ? axes[index] + 4 : axes[index]];
  359. if (nhwc_dim == dim) {
  360. cur_input.push_back(origin_input[index]);
  361. }
  362. }
  363. }
  364. auto param_node = BuildIntVecParameterNode(func_graph, cur_input, cnode->input(input_index)->fullname_with_scope());
  365. func_graph->manager()->Replace(cnode->input(input_index), param_node);
  366. }
  367. std::vector<int> TransposeStrategy::TransformOpAxesAttr(const std::vector<int> &origin_axes) {
  368. auto axis_map = GetNC2NHAxisMap();
  369. std::vector<int> cur_axis;
  370. for (size_t i = 0; i < origin_axes.size(); ++i) {
  371. cur_axis.push_back(axis_map[origin_axes[i] < 0 ? origin_axes[i] + 4 : origin_axes[i]]);
  372. }
  373. std::sort(cur_axis.begin(), cur_axis.end());
  374. return cur_axis;
  375. }
  376. } // namespace opt
  377. } // namespace mindspore