Elffuss BraidHub

kikocis/ncnn public

master · 5.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "pass_ncnn.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21class Tensor_permute : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
273 2
28pnnx.Input input 0 1 input
29Tensor.permute op_0 1 1 input out dims=%dims
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "Permute";
37 }
38
39 const char* name_str() const
40 {
41 return "permute";
42 }
43
44 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
45 {
46 op->params["0"] = 0;
47
48 const int batch_index = op->inputs[0]->params["__batch_index"].i;
49
50 const std::vector<int>& dims = captured_params.at("dims").ai;
51
52 int input_rank = (int)op->inputs[0]->shape.size();
53
54 if (input_rank == 0)
55 {
56 // assume input is fine
57 input_rank = (int)dims.size();
58 }
59
60 if (batch_index >= 0 && batch_index < input_rank)
61 input_rank -= 1;
62
63 if (input_rank > 4)
64 {
65 fprintf(stderr, "permute %d-rank tensor is not supported yet!\n", input_rank);
66 return;
67 }
68
69 // drop permute batch index
70 std::vector<int> new_dims;
71 for (int i = 0; i < (int)dims.size(); i++)
72 {
73 if (dims[i] == batch_index)
74 continue;
75
76 int new_dim = dims[i] > batch_index ? dims[i] - 1 : dims[i];
77 new_dims.push_back(new_dim);
78 }
79
80 if (input_rank != (int)new_dims.size())
81 {
82 fprintf(stderr, "permute %d-rank tensor with %d-rank dims is not possible\n", input_rank, (int)new_dims.size());
83 return;
84 }
85
86 if (input_rank == 1)
87 {
88 // noop
89 op->type = "Noop";
90 }
91 if (input_rank == 2)
92 {
93 if (new_dims == std::vector<int>{0, 1})
94 op->type = "Noop";
95 else if (new_dims == std::vector<int>{1, 0})
96 op->params["0"] = 1;
97 }
98 if (input_rank == 3)
99 {
100 if (new_dims == std::vector<int>{0, 1, 2})
101 op->type = "Noop";
102 else if (new_dims == std::vector<int>{0, 2, 1})
103 op->params["0"] = 1;
104 else if (new_dims == std::vector<int>{1, 0, 2})
105 op->params["0"] = 2;
106 else if (new_dims == std::vector<int>{1, 2, 0})
107 op->params["0"] = 3;
108 else if (new_dims == std::vector<int>{2, 0, 1})
109 op->params["0"] = 4;
110 else if (new_dims == std::vector<int>{2, 1, 0})
111 op->params["0"] = 5;
112 }
113 if (input_rank == 4)
114 {
115 if (new_dims == std::vector<int>{0, 1, 2, 3})
116 op->type = "Noop";
117 else if (new_dims == std::vector<int>{0, 1, 3, 2})
118 op->params["0"] = 1;
119 else if (new_dims == std::vector<int>{0, 2, 1, 3})
120 op->params["0"] = 2;
121 else if (new_dims == std::vector<int>{0, 2, 3, 1})
122 op->params["0"] = 3;
123 else if (new_dims == std::vector<int>{0, 3, 1, 2})
124 op->params["0"] = 4;
125 else if (new_dims == std::vector<int>{0, 3, 2, 1})
126 op->params["0"] = 5;
127 else if (new_dims == std::vector<int>{1, 0, 2, 3})
128 op->params["0"] = 6;
129 else if (new_dims == std::vector<int>{1, 0, 3, 2})
130 op->params["0"] = 7;
131 else if (new_dims == std::vector<int>{1, 2, 0, 3})
132 op->params["0"] = 8;
133 else if (new_dims == std::vector<int>{1, 2, 3, 0})
134 op->params["0"] = 9;
135 else if (new_dims == std::vector<int>{1, 3, 0, 2})
136 op->params["0"] = 10;
137 else if (new_dims == std::vector<int>{1, 3, 2, 0})
138 op->params["0"] = 11;
139 else if (new_dims == std::vector<int>{2, 0, 1, 3})
140 op->params["0"] = 12;
141 else if (new_dims == std::vector<int>{2, 0, 3, 1})
142 op->params["0"] = 13;
143 else if (new_dims == std::vector<int>{2, 1, 0, 3})
144 op->params["0"] = 14;
145 else if (new_dims == std::vector<int>{2, 1, 3, 0})
146 op->params["0"] = 15;
147 else if (new_dims == std::vector<int>{2, 3, 0, 1})
148 op->params["0"] = 16;
149 else if (new_dims == std::vector<int>{2, 3, 1, 0})
150 op->params["0"] = 17;
151 else if (new_dims == std::vector<int>{3, 0, 1, 2})
152 op->params["0"] = 18;
153 else if (new_dims == std::vector<int>{3, 0, 2, 1})
154 op->params["0"] = 19;
155 else if (new_dims == std::vector<int>{3, 1, 0, 2})
156 op->params["0"] = 20;
157 else if (new_dims == std::vector<int>{3, 1, 2, 0})
158 op->params["0"] = 21;
159 else if (new_dims == std::vector<int>{3, 2, 0, 1})
160 op->params["0"] = 22;
161 else if (new_dims == std::vector<int>{3, 2, 1, 0})
162 op->params["0"] = 23;
163 }
164 }
165};
166
167REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(Tensor_permute, 20)
168
169} // namespace ncnn
170
171} // namespace pnnx