Elffuss BraidHub

kikocis/ncnn public

master · 10.0 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_level2.h"
16
17namespace pnnx {
18
19class F_layer_norm : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
258 7
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 weight
28pnnx.Input input_2 0 1 bias
29pnnx.Input input_3 0 1 normalized_shape
30prim::Constant op_0 0 1 eps value=%eps
31prim::Constant op_1 0 1 cudnn_enabled value=*
32aten::layer_norm op_2 6 1 input normalized_shape weight bias eps cudnn_enabled out
33pnnx.Output output 1 0 out
34)PNNXIR";
35 }
36
37 const char* type_str() const
38 {
39 return "F.layer_norm";
40 }
41};
42
43REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm, 10)
44
45class F_layer_norm_onnx : public GraphRewriterPass
46{
47public:
48 const char* match_pattern_graph() const
49 {
50 return R"PNNXIR(7767517
515 4
52pnnx.Input input_0 0 1 input
53pnnx.Input input_1 0 1 weight
54pnnx.Input input_2 0 1 bias
55LayerNormalization op_0 3 1 input weight bias out axis=%axis epsilon=%epsilon
56pnnx.Output output 1 0 out
57)PNNXIR";
58 }
59
60 const char* type_str() const
61 {
62 return "F.layer_norm";
63 }
64
65 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
66 {
67 const int input_rank = op->inputs[0]->shape.size();
68
69 int axis = captured_params.at("axis").i;
70 if (axis < 0)
71 {
72 axis = input_rank + axis;
73 }
74
75 std::vector<int> normalized_shape;
76 for (int i = axis; i < input_rank; i++)
77 {
78 normalized_shape.push_back(op->inputs[0]->shape[i]);
79 }
80
81 op->params["normalized_shape"] = normalized_shape;
82 op->params["eps"] = captured_params.at("epsilon");
83 }
84};
85
86REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx, 10)
87
88class F_layer_norm_onnx_1 : public F_layer_norm_onnx
89{
90public:
91 const char* match_pattern_graph() const
92 {
93 return R"PNNXIR(7767517
945 6
95pnnx.Input input_0 0 1 input
96pnnx.Input input_1 0 1 weight
97pnnx.Input input_2 0 1 bias
98LayerNormalization op_0 3 3 input weight bias out Mean InvStdDev axis=%axis epsilon=%epsilon stash_type=%stash_type
99pnnx.Output output 3 0 out Mean InvStdDev
100)PNNXIR";
101 }
102
103 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
104 {
105 F_layer_norm_onnx::write(op, captured_params);
106
107 // drop Mean and InvStdDev if not used
108 if (op->outputs[1]->consumers.empty() && op->outputs[2]->consumers.empty())
109 {
110 op->outputs[1]->producer = 0;
111 op->outputs[2]->producer = 0;
112 op->outputs.resize(1);
113 }
114 }
115};
116
117REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_1, 10)
118
119class F_layer_norm_onnx_2 : public F_layer_norm_onnx
120{
121public:
122 const char* match_pattern_graph() const
123 {
124 return R"PNNXIR(7767517
1255 4
126pnnx.Input input_0 0 1 input
127pnnx.Input input_1 0 1 weight
128pnnx.Input input_2 0 1 bias
129LayerNormalization op_0 3 1 input weight bias out axis=%axis epsilon=%epsilon stash_type=%stash_type
130pnnx.Output output 1 0 out
131)PNNXIR";
132 }
133};
134
135REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_2, 10)
136
137class F_layer_norm_onnx_3 : public GraphRewriterPass
138{
139public:
140 const char* match_pattern_graph() const
141 {
142 return R"PNNXIR(7767517
14311 10
144pnnx.Input input 0 1 input
145torch.mean mean 1 1 input mean dim=%dim keepdim=True
146aten::sub op_1 2 1 input mean pnnx_1
147prim::Constant op_2 0 1 two value=2.000000e+00
148aten::pow op_3 2 1 pnnx_1 two pnnx_2
149torch.mean op_4 1 1 pnnx_2 var dim=%dim keepdim=True
150prim::Constant op_5 0 1 eps value=%eps
151aten::add op_6 2 1 var eps pnnx_4
152aten::sqrt op_7 1 1 pnnx_4 pnnx_5
153aten::div op_8 2 1 pnnx_1 pnnx_5 out
154pnnx.Output output 1 0 out
155)PNNXIR";
156 }
157
158 const char* type_str() const
159 {
160 return "F.layer_norm";
161 }
162
163 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
164 {
165 const Operator* op_mean = matched_operators.at("mean");
166 const std::vector<int>& inputshape = op_mean->inputs[0]->shape;
167 if (inputshape.empty())
168 return false;
169
170 // dim must be the last N dimensions
171 std::vector<int> dim = captured_params.at("dim").ai;
172
173 const int input_rank = (int)inputshape.size();
174 const int dim_count = (int)dim.size();
175
176 for (int i = 0; i < dim_count; i++)
177 {
178 if (dim[i] < 0)
179 dim[i] += input_rank;
180
181 if (dim[i] < input_rank - dim_count)
182 return false;
183 }
184
185 std::vector<int> normalized_shape(dim_count);
186 for (int i = 0; i < dim_count; i++)
187 {
188 normalized_shape[i] = inputshape[input_rank - dim_count + i];
189 }
190
191 return true;
192 }
193
194 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
195 {
196 const std::vector<int>& inputshape = op->inputs[0]->shape;
197 const std::vector<int>& dim = captured_params.at("dim").ai;
198 const int input_rank = (int)inputshape.size();
199 const int dim_count = (int)dim.size();
200
201 std::vector<int> normalized_shape(dim_count);
202 for (int i = 0; i < dim_count; i++)
203 {
204 normalized_shape[i] = inputshape[input_rank - dim_count + i];
205 }
206
207 op->params["normalized_shape"] = normalized_shape;
208 op->params["eps"] = captured_params.at("eps");
209 op->params["weight"] = Parameter();
210 op->params["bias"] = Parameter();
211 }
212};
213
214REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_3, 30)
215
216class F_layer_norm_onnx_4 : public GraphRewriterPass
217{
218public:
219 const char* match_pattern_graph() const
220 {
221 return R"PNNXIR(7767517
22215 14
223pnnx.Input input_0 0 1 input
224pnnx.Input input_1 0 1 weight
225pnnx.Input input_2 0 1 bias
226torch.mean mean 1 1 input mean dim=%dim keepdim=True
227aten::sub op_1 2 1 input mean pnnx_1
228prim::Constant op_2 0 1 two value=2.000000e+00
229aten::pow op_3 2 1 pnnx_1 two pnnx_2
230torch.mean op_4 1 1 pnnx_2 var dim=%dim keepdim=True
231prim::Constant op_5 0 1 eps value=%eps
232aten::add op_6 2 1 var eps pnnx_4
233aten::sqrt op_7 1 1 pnnx_4 pnnx_5
234aten::div op_8 2 1 pnnx_1 pnnx_5 pnnx_6
235aten::mul mul 2 1 pnnx_6 weight pnnx_7
236aten::add add 2 1 pnnx_7 bias out
237pnnx.Output output 1 0 out
238)PNNXIR";
239 }
240
241 const char* type_str() const
242 {
243 return "F.layer_norm";
244 }
245
246 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
247 {
248 const Operator* op_mean = matched_operators.at("mean");
249 const std::vector<int>& inputshape = op_mean->inputs[0]->shape;
250 if (inputshape.empty())
251 return false;
252
253 // dim must be the last N dimensions
254 std::vector<int> dim = captured_params.at("dim").ai;
255
256 const int input_rank = (int)inputshape.size();
257 const int dim_count = (int)dim.size();
258
259 for (int i = 0; i < dim_count; i++)
260 {
261 if (dim[i] < 0)
262 dim[i] += input_rank;
263
264 if (dim[i] < input_rank - dim_count)
265 return false;
266 }
267
268 std::vector<int> normalized_shape(dim_count);
269 for (int i = 0; i < dim_count; i++)
270 {
271 normalized_shape[i] = inputshape[input_rank - dim_count + i];
272 }
273
274 // check weight and bias shape
275 const Operator* op_mul = matched_operators.at("mul");
276 const Operator* op_add = matched_operators.at("add");
277 const std::vector<int>& weight_shape = op_mul->inputs[1]->shape;
278 const std::vector<int>& bias_shape = op_add->inputs[1]->shape;
279
280 if (weight_shape != normalized_shape)
281 return false;
282
283 if (bias_shape != normalized_shape)
284 return false;
285
286 return true;
287 }
288
289 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
290 {
291 const std::vector<int>& inputshape = op->inputs[0]->shape;
292 const std::vector<int>& dim = captured_params.at("dim").ai;
293 const int input_rank = (int)inputshape.size();
294 const int dim_count = (int)dim.size();
295
296 std::vector<int> normalized_shape(dim_count);
297 for (int i = 0; i < dim_count; i++)
298 {
299 normalized_shape[i] = inputshape[input_rank - dim_count + i];
300 }
301
302 op->params["normalized_shape"] = normalized_shape;
303 op->params["eps"] = captured_params.at("eps");
304 }
305};
306
307REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_4, 29)
308
309} // namespace pnnx