| 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 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | class F_layer_norm : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 8 7 |
| 26 | pnnx.Input input_0 0 1 input |
| 27 | pnnx.Input input_1 0 1 weight |
| 28 | pnnx.Input input_2 0 1 bias |
| 29 | pnnx.Input input_3 0 1 normalized_shape |
| 30 | prim::Constant op_0 0 1 eps value=%eps |
| 31 | prim::Constant op_1 0 1 cudnn_enabled value=* |
| 32 | aten::layer_norm op_2 6 1 input normalized_shape weight bias eps cudnn_enabled out |
| 33 | pnnx.Output output 1 0 out |
| 34 | )PNNXIR"; |
| 35 | } |
| 36 | |
| 37 | const char* type_str() const |
| 38 | { |
| 39 | return "F.layer_norm"; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm, 10) |
| 44 | |
| 45 | class F_layer_norm_onnx : public GraphRewriterPass |
| 46 | { |
| 47 | public: |
| 48 | const char* match_pattern_graph() const |
| 49 | { |
| 50 | return R"PNNXIR(7767517 |
| 51 | 5 4 |
| 52 | pnnx.Input input_0 0 1 input |
| 53 | pnnx.Input input_1 0 1 weight |
| 54 | pnnx.Input input_2 0 1 bias |
| 55 | LayerNormalization op_0 3 1 input weight bias out axis=%axis epsilon=%epsilon |
| 56 | pnnx.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 | |
| 86 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx, 10) |
| 87 | |
| 88 | class F_layer_norm_onnx_1 : public F_layer_norm_onnx |
| 89 | { |
| 90 | public: |
| 91 | const char* match_pattern_graph() const |
| 92 | { |
| 93 | return R"PNNXIR(7767517 |
| 94 | 5 6 |
| 95 | pnnx.Input input_0 0 1 input |
| 96 | pnnx.Input input_1 0 1 weight |
| 97 | pnnx.Input input_2 0 1 bias |
| 98 | LayerNormalization op_0 3 3 input weight bias out Mean InvStdDev axis=%axis epsilon=%epsilon stash_type=%stash_type |
| 99 | pnnx.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 | |
| 117 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_1, 10) |
| 118 | |
| 119 | class F_layer_norm_onnx_2 : public F_layer_norm_onnx |
| 120 | { |
| 121 | public: |
| 122 | const char* match_pattern_graph() const |
| 123 | { |
| 124 | return R"PNNXIR(7767517 |
| 125 | 5 4 |
| 126 | pnnx.Input input_0 0 1 input |
| 127 | pnnx.Input input_1 0 1 weight |
| 128 | pnnx.Input input_2 0 1 bias |
| 129 | LayerNormalization op_0 3 1 input weight bias out axis=%axis epsilon=%epsilon stash_type=%stash_type |
| 130 | pnnx.Output output 1 0 out |
| 131 | )PNNXIR"; |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_2, 10) |
| 136 | |
| 137 | class F_layer_norm_onnx_3 : public GraphRewriterPass |
| 138 | { |
| 139 | public: |
| 140 | const char* match_pattern_graph() const |
| 141 | { |
| 142 | return R"PNNXIR(7767517 |
| 143 | 11 10 |
| 144 | pnnx.Input input 0 1 input |
| 145 | torch.mean mean 1 1 input mean dim=%dim keepdim=True |
| 146 | aten::sub op_1 2 1 input mean pnnx_1 |
| 147 | prim::Constant op_2 0 1 two value=2.000000e+00 |
| 148 | aten::pow op_3 2 1 pnnx_1 two pnnx_2 |
| 149 | torch.mean op_4 1 1 pnnx_2 var dim=%dim keepdim=True |
| 150 | prim::Constant op_5 0 1 eps value=%eps |
| 151 | aten::add op_6 2 1 var eps pnnx_4 |
| 152 | aten::sqrt op_7 1 1 pnnx_4 pnnx_5 |
| 153 | aten::div op_8 2 1 pnnx_1 pnnx_5 out |
| 154 | pnnx.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 | |
| 214 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_3, 30) |
| 215 | |
| 216 | class F_layer_norm_onnx_4 : public GraphRewriterPass |
| 217 | { |
| 218 | public: |
| 219 | const char* match_pattern_graph() const |
| 220 | { |
| 221 | return R"PNNXIR(7767517 |
| 222 | 15 14 |
| 223 | pnnx.Input input_0 0 1 input |
| 224 | pnnx.Input input_1 0 1 weight |
| 225 | pnnx.Input input_2 0 1 bias |
| 226 | torch.mean mean 1 1 input mean dim=%dim keepdim=True |
| 227 | aten::sub op_1 2 1 input mean pnnx_1 |
| 228 | prim::Constant op_2 0 1 two value=2.000000e+00 |
| 229 | aten::pow op_3 2 1 pnnx_1 two pnnx_2 |
| 230 | torch.mean op_4 1 1 pnnx_2 var dim=%dim keepdim=True |
| 231 | prim::Constant op_5 0 1 eps value=%eps |
| 232 | aten::add op_6 2 1 var eps pnnx_4 |
| 233 | aten::sqrt op_7 1 1 pnnx_4 pnnx_5 |
| 234 | aten::div op_8 2 1 pnnx_1 pnnx_5 pnnx_6 |
| 235 | aten::mul mul 2 1 pnnx_6 weight pnnx_7 |
| 236 | aten::add add 2 1 pnnx_7 bias out |
| 237 | pnnx.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 | |
| 307 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_layer_norm_onnx_4, 29) |
| 308 | |
| 309 | } // namespace pnnx |