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
17 #include "tools/optimizer/format/delete_redundant_transpose.h"
18 #include <vector>
19 #include "tools/optimizer/common/format_utils.h"
20 #include "nnacl/op_base.h"
21
22 namespace mindspore {
23 namespace opt {
DeleteNot4DTranspose(const FuncGraphPtr & func_graph)24 STATUS DeleteRedundantTranspose::DeleteNot4DTranspose(const FuncGraphPtr &func_graph) {
25 MS_ASSERT(func_graph != nullptr);
26 MS_ASSERT(manager_ != nullptr);
27 manager_->AddFuncGraph(func_graph);
28 auto node_list = TopoSort(func_graph->get_return());
29 for (auto &node : node_list) {
30 MS_CHECK_TRUE_RET(node != nullptr, lite::RET_NULL_PTR);
31 if (!utils::isa<CNode>(node)) {
32 continue;
33 }
34 auto cnode = node->cast<CNodePtr>();
35 if (CheckPrimitiveType(cnode, prim::kPrimIf) || CheckPrimitiveType(cnode, prim::kPrimWhile)) {
36 auto sub_func_graph = GetValueNode<FuncGraphPtr>(cnode->input(1));
37 if (sub_func_graph == nullptr) {
38 lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(lite::RET_NULL_PTR);
39 return lite::RET_NULL_PTR;
40 }
41 if (DeleteNot4DTranspose(sub_func_graph) != lite::RET_OK) {
42 MS_LOG(ERROR) << "delete transpose failed.";
43 return lite::RET_ERROR;
44 }
45 sub_func_graph = GetValueNode<FuncGraphPtr>(cnode->input(kInputIndexTwo));
46 if (sub_func_graph == nullptr) {
47 lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(lite::RET_NULL_PTR);
48 return lite::RET_NULL_PTR;
49 }
50 if (DeleteNot4DTranspose(sub_func_graph) != lite::RET_OK) {
51 MS_LOG(ERROR) << "delete transpose failed.";
52 return lite::RET_ERROR;
53 }
54 continue;
55 }
56 if (!CheckPrimitiveType(node, prim::kPrimTranspose)) {
57 continue;
58 }
59 auto abstract = GetCNodeInputAbstract(cnode, 1);
60 ShapeVector shape;
61 if (FetchShapeFromAbstract(abstract, &shape) != lite::RET_OK) {
62 MS_LOG(ERROR) << "fetch shape failed.";
63 return lite::RET_ERROR;
64 }
65 std::vector<int> perm;
66 if (GetTransposePerm(cnode, &perm) != lite::RET_OK) {
67 MS_LOG(ERROR) << "fetch transpose perm failed.";
68 return lite::RET_ERROR;
69 }
70 if (!shape.empty() && shape.size() != perm.size() && !(shape.size() == 1 && shape[0] == -1)) {
71 MS_LOG(DEBUG) << "transpose node need to be deleted.";
72 if (UpdateNodeFormat(func_graph, cnode) != lite::RET_OK) {
73 MS_LOG(ERROR) << "update cnode format failed.";
74 return lite::RET_ERROR;
75 }
76 if (!manager_->Replace(node, cnode->input(1))) {
77 MS_LOG(ERROR) << "replace old node failed, please check.";
78 return lite::RET_ERROR;
79 }
80 }
81 }
82 return lite::RET_OK;
83 }
84
TransTransFusion(const FuncGraphPtr & func_graph)85 STATUS DeleteRedundantTranspose::TransTransFusion(const FuncGraphPtr &func_graph) {
86 MS_ASSERT(func_graph != nullptr);
87 MS_ASSERT(manager_ != nullptr);
88 manager_->AddFuncGraph(func_graph);
89 auto node_lite = TopoSort(func_graph->get_return());
90 for (auto &node : node_lite) {
91 MS_CHECK_TRUE_RET(node != nullptr, lite::RET_NULL_PTR);
92 if (!utils::isa<CNode>(node)) {
93 continue;
94 }
95 auto cnode = node->cast<CNodePtr>();
96 if (CheckPrimitiveType(cnode, prim::kPrimIf) || CheckPrimitiveType(cnode, prim::kPrimWhile)) {
97 auto sub_func_graph = GetValueNode<FuncGraphPtr>(cnode->input(1));
98 if (sub_func_graph == nullptr) {
99 lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(lite::RET_NULL_PTR);
100 return lite::RET_NULL_PTR;
101 }
102 if (TransTransFusion(sub_func_graph) != lite::RET_OK) {
103 MS_LOG(ERROR) << "delete transpose failed.";
104 return lite::RET_ERROR;
105 }
106 sub_func_graph = GetValueNode<FuncGraphPtr>(cnode->input(kInputIndexTwo));
107 if (sub_func_graph == nullptr) {
108 lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(lite::RET_NULL_PTR);
109 return lite::RET_NULL_PTR;
110 }
111 if (TransTransFusion(sub_func_graph) != lite::RET_OK) {
112 MS_LOG(ERROR) << "delete transpose failed.";
113 return lite::RET_ERROR;
114 }
115 continue;
116 }
117 if (!CheckPrimitiveType(cnode, prim::kPrimTranspose) ||
118 !CheckPrimitiveType(cnode->input(1), prim::kPrimTranspose)) {
119 continue;
120 }
121 std::vector<int> post_perm;
122 if (GetTransposePerm(cnode, &post_perm) != lite::RET_OK) {
123 MS_LOG(ERROR) << "transpose rm cannot be obtained, " << cnode->fullname_with_scope();
124 return lite::RET_ERROR;
125 }
126 std::vector<int> pre_perm;
127 auto pre_cnode = cnode->input(1)->cast<CNodePtr>();
128 MS_ASSERT(pre_cnode != nullptr);
129 if (GetTransposePerm(pre_cnode, &pre_perm) != lite::RET_OK) {
130 MS_LOG(ERROR) << "transpose rm cannot be obtained, " << pre_cnode->fullname_with_scope();
131 return lite::RET_ERROR;
132 }
133 if ((pre_perm == kNH2NC && post_perm == kNC2NH) || (pre_perm == kNC2NH && post_perm == kNH2NC)) {
134 if (!manager_->Replace(cnode, pre_cnode->input(1))) {
135 MS_LOG(ERROR) << "replace old node failed, please check.";
136 return lite::RET_ERROR;
137 }
138 }
139 }
140 return lite::RET_OK;
141 }
142
UpdateNodeFormat(const FuncGraphPtr & func_graph,const CNodePtr & cnode)143 STATUS DeleteRedundantTranspose::UpdateNodeFormat(const FuncGraphPtr &func_graph, const CNodePtr &cnode) {
144 MS_ASSERT(func_graph != nullptr && cnode != nullptr);
145 MS_ASSERT(manager_ != nullptr);
146 auto prim = GetValueNode<PrimitivePtr>(cnode->input(0));
147 MS_ASSERT(prim != nullptr);
148 if (prim->GetAttr(ops::kFormat) == nullptr) {
149 return lite::RET_OK;
150 }
151 auto format = GetValue<int64_t>(prim->GetAttr(ops::kFormat));
152 auto node_users = manager_->node_users()[cnode];
153 for (auto &node_user : node_users) {
154 if (node_user.second != 1) {
155 continue;
156 }
157 if (!utils::isa<CNode>(node_user.first)) {
158 MS_LOG(ERROR) << "post node is not cnode, which is invalid.";
159 return lite::RET_ERROR;
160 }
161 auto post_cnode = node_user.first->cast<CNodePtr>();
162 auto post_prim = GetValueNode<PrimitivePtr>(post_cnode->input(0));
163 MS_ASSERT(post_prim != nullptr);
164 post_prim->AddAttr(ops::kFormat, MakeValue<int64_t>(format));
165 }
166 return lite::RET_OK;
167 }
168
Run(const FuncGraphPtr & func_graph)169 bool DeleteRedundantTranspose::Run(const FuncGraphPtr &func_graph) {
170 MS_CHECK_TRUE_RET(func_graph != nullptr, false);
171 manager_ = Manage(func_graph, true);
172 if (manager_ == nullptr) {
173 MS_LOG(ERROR) << "manager is nullptr.";
174 return false;
175 }
176 if (TransTransFusion(func_graph) != lite::RET_OK) {
177 MS_LOG(ERROR) << "ranspose and transpose fusion failed.";
178 return false;
179 }
180 if (DeleteNot4DTranspose(func_graph) != lite::RET_OK) {
181 MS_LOG(ERROR) << "delete not 4D transpose failed.";
182 return false;
183 }
184 return true;
185 }
186 } // namespace opt
187 } // namespace mindspore
188