| 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 | #if _MSC_VER |
| 18 | #define _USE_MATH_DEFINES |
| 19 | #include <math.h> |
| 20 | #endif |
| 21 | |
| 22 | namespace pnnx { |
| 23 | |
| 24 | class F_gelu : public GraphRewriterPass |
| 25 | { |
| 26 | public: |
| 27 | const char* match_pattern_graph() const |
| 28 | { |
| 29 | return R"PNNXIR(7767517 |
| 30 | 3 2 |
| 31 | pnnx.Input input 0 1 input |
| 32 | aten::gelu op_0 1 1 input out |
| 33 | pnnx.Output output 1 0 out |
| 34 | )PNNXIR"; |
| 35 | } |
| 36 | |
| 37 | const char* type_str() const |
| 38 | { |
| 39 | return "F.gelu"; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu, 10) |
| 44 | |
| 45 | class F_gelu_1 : public GraphRewriterPass |
| 46 | { |
| 47 | public: |
| 48 | const char* match_pattern_graph() const |
| 49 | { |
| 50 | return R"PNNXIR(7767517 |
| 51 | 4 3 |
| 52 | pnnx.Input input_0 0 1 input |
| 53 | prim::Constant op_0 0 1 approximate value=%approximate |
| 54 | aten::gelu op_1 2 1 input approximate out |
| 55 | pnnx.Output output 1 0 out |
| 56 | )PNNXIR"; |
| 57 | } |
| 58 | |
| 59 | const char* type_str() const |
| 60 | { |
| 61 | return "F.gelu"; |
| 62 | } |
| 63 | |
| 64 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 65 | { |
| 66 | const std::string& approximate = captured_params.at("approximate").s; |
| 67 | if (approximate != "none") |
| 68 | op->params["approximate"] = approximate; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_1, 10) |
| 73 | |
| 74 | class F_gelu_1_1 : public GraphRewriterPass |
| 75 | { |
| 76 | public: |
| 77 | const char* match_pattern_graph() const |
| 78 | { |
| 79 | return R"PNNXIR(7767517 |
| 80 | 3 2 |
| 81 | pnnx.Input input 0 1 input |
| 82 | aten::gelu_approximate_none op_0 1 1 input out |
| 83 | pnnx.Output output 1 0 out |
| 84 | )PNNXIR"; |
| 85 | } |
| 86 | |
| 87 | const char* type_str() const |
| 88 | { |
| 89 | return "F.gelu"; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_1_1, 10) |
| 94 | |
| 95 | class F_gelu_2 : public GraphRewriterPass |
| 96 | { |
| 97 | public: |
| 98 | // x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) |
| 99 | const char* match_pattern_graph() const |
| 100 | { |
| 101 | return R"PNNXIR(7767517 |
| 102 | 11 10 |
| 103 | pnnx.Input input_0 0 1 input |
| 104 | prim::Constant op_0 0 1 12 value=%0p5 |
| 105 | aten::mul op_1 2 1 input 12 13 |
| 106 | prim::Constant op_2 0 1 15 value=%sqrt2 |
| 107 | aten::div op_3 2 1 input 15 16 |
| 108 | aten::erf op_4 1 1 16 17 |
| 109 | prim::Constant op_5 0 1 20 value=%1 |
| 110 | prim::Constant op_6 0 1 21 value=1 |
| 111 | aten::add op_7 3 1 17 20 21 22 |
| 112 | aten::mul op_8 2 1 13 22 out |
| 113 | pnnx.Output output 1 0 out |
| 114 | )PNNXIR"; |
| 115 | } |
| 116 | |
| 117 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 118 | { |
| 119 | if (captured_params.at("0p5").f != 0.5f) |
| 120 | return false; |
| 121 | |
| 122 | if (fabs(captured_params.at("sqrt2").f - sqrt(2.f)) > 0.0001f) |
| 123 | return false; |
| 124 | |
| 125 | if ((captured_params.at("1").type == 2 && captured_params.at("1").i != 1) || (captured_params.at("1").type == 3 && captured_params.at("1").f != 1.f)) |
| 126 | return false; |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | const char* type_str() const |
| 132 | { |
| 133 | return "F.gelu"; |
| 134 | } |
| 135 | |
| 136 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 137 | { |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_2, 9) |
| 142 | |
| 143 | class F_gelu_3 : public GraphRewriterPass |
| 144 | { |
| 145 | public: |
| 146 | // 0.5 * x * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (x + 0.044715 * torch.pow(x, 3.0)))) |
| 147 | const char* match_pattern_graph() const |
| 148 | { |
| 149 | return R"PNNXIR(7767517 |
| 150 | 17 16 |
| 151 | pnnx.Input input_0 0 1 input |
| 152 | prim::Constant op_0 0 1 60 value=%0p5 |
| 153 | aten::mul op_1 2 1 input 60 26 |
| 154 | prim::Constant op_2 0 1 28 value=%3 |
| 155 | aten::pow op_3 2 1 input 28 29 |
| 156 | prim::Constant op_4 0 1 30 value=%0p044715 |
| 157 | aten::mul op_5 2 1 29 30 31 |
| 158 | prim::Constant op_6 0 1 61 value=1 |
| 159 | aten::add op_7 3 1 input 31 61 35 |
| 160 | prim::Constant op_8 0 1 36 value=%sqrt2dpi |
| 161 | aten::mul op_9 2 1 35 36 37 |
| 162 | aten::tanh op_10 1 1 37 39 |
| 163 | prim::Constant op_11 0 1 62 value=%1 |
| 164 | prim::Constant op_12 0 1 63 value=%1_1 |
| 165 | aten::add op_13 3 1 39 62 63 42 |
| 166 | aten::mul op_14 2 1 26 42 out |
| 167 | pnnx.Output output 1 0 out |
| 168 | )PNNXIR"; |
| 169 | } |
| 170 | |
| 171 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 172 | { |
| 173 | if (captured_params.at("0p5").f != 0.5f) |
| 174 | return false; |
| 175 | |
| 176 | if (fabs(captured_params.at("0p044715").f - 0.044715f) > 0.0001f) |
| 177 | return false; |
| 178 | |
| 179 | if (fabs(captured_params.at("sqrt2dpi").f - sqrt(2.f / M_PI)) > 0.0001f) |
| 180 | return false; |
| 181 | |
| 182 | if ((captured_params.at("1").type == 2 && captured_params.at("1").i != 1) || (captured_params.at("1").type == 3 && captured_params.at("1").f != 1.f)) |
| 183 | return false; |
| 184 | |
| 185 | if ((captured_params.at("3").type == 2 && captured_params.at("3").i != 3) || (captured_params.at("3").type == 3 && captured_params.at("3").f != 3.f)) |
| 186 | return false; |
| 187 | |
| 188 | if ((captured_params.at("1_1").type == 2 && captured_params.at("1_1").i != 1) || (captured_params.at("1_1").type == 3 && captured_params.at("1_1").f != 1.f)) |
| 189 | return false; |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | const char* type_str() const |
| 195 | { |
| 196 | return "F.gelu"; |
| 197 | } |
| 198 | |
| 199 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 200 | { |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_3, 9) |
| 205 | |
| 206 | class F_gelu_4 : public GraphRewriterPass |
| 207 | { |
| 208 | public: |
| 209 | // (x * 0.5) * (tanh((x + (torch.pow(x, 3.0) * 0.044715)) * 7.978846e-01) + 1) |
| 210 | const char* match_pattern_graph() const |
| 211 | { |
| 212 | return R"PNNXIR(7767517 |
| 213 | 15 14 |
| 214 | pnnx.Input input 0 1 input |
| 215 | prim::Constant op_0 0 1 zp5 value=0.5 |
| 216 | aten::mul op_1 2 1 input zp5 pnnx_90 |
| 217 | prim::Constant op_2 0 1 three value=%3 |
| 218 | aten::pow op_3 2 1 input three pnnx_91 |
| 219 | prim::Constant op_4 0 1 zp044715 value=%0p044715 |
| 220 | aten::mul op_5 2 1 pnnx_91 zp044715 pnnx_92 |
| 221 | aten::add op_6 2 1 input pnnx_92 pnnx_93 |
| 222 | prim::Constant op_7 0 1 sqrt2dpi value=%sqrt2dpi |
| 223 | aten::mul op_8 2 1 pnnx_93 sqrt2dpi pnnx_94 |
| 224 | aten::tanh op_9 1 1 pnnx_94 pnnx_95 |
| 225 | prim::Constant op_10 0 1 one value=%1 |
| 226 | aten::add op_11 2 1 pnnx_95 one pnnx_96 |
| 227 | aten::mul op_12 2 1 pnnx_90 pnnx_96 out |
| 228 | pnnx.Output output 1 0 out |
| 229 | )PNNXIR"; |
| 230 | } |
| 231 | |
| 232 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 233 | { |
| 234 | if (fabs(captured_params.at("0p044715").f - 0.044715f) > 0.0001f) |
| 235 | return false; |
| 236 | |
| 237 | if (fabs(captured_params.at("sqrt2dpi").f - sqrt(2.f / M_PI)) > 0.0001f) |
| 238 | return false; |
| 239 | |
| 240 | if ((captured_params.at("1").type == 2 && captured_params.at("1").i != 1) || (captured_params.at("1").type == 3 && captured_params.at("1").f != 1.f)) |
| 241 | return false; |
| 242 | |
| 243 | if ((captured_params.at("3").type == 2 && captured_params.at("3").i != 3) || (captured_params.at("3").type == 3 && captured_params.at("3").f != 3.f)) |
| 244 | return false; |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | const char* type_str() const |
| 250 | { |
| 251 | return "F.gelu"; |
| 252 | } |
| 253 | |
| 254 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 255 | { |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_4, 9) |
| 260 | |
| 261 | class F_gelu_onnx : public GraphRewriterPass |
| 262 | { |
| 263 | public: |
| 264 | // x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) |
| 265 | const char* match_pattern_graph() const |
| 266 | { |
| 267 | return R"PNNXIR(7767517 |
| 268 | 10 9 |
| 269 | pnnx.Input input 0 1 input |
| 270 | prim::Constant op_0 0 1 sqrt2 value=%sqrt2 |
| 271 | aten::div op_1 2 1 input sqrt2 16 |
| 272 | aten::erf op_2 1 1 16 17 |
| 273 | prim::Constant op_3 0 1 one value=%1 |
| 274 | aten::add op_4 2 1 17 one 22 |
| 275 | aten::mul op_5 2 1 input 22 25 |
| 276 | prim::Constant op_6 0 1 0p5 value=%0p5 |
| 277 | aten::mul op_7 2 1 25 0p5 out |
| 278 | pnnx.Output output 1 0 out |
| 279 | )PNNXIR"; |
| 280 | } |
| 281 | |
| 282 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 283 | { |
| 284 | if (captured_params.at("0p5").f != 0.5f) |
| 285 | return false; |
| 286 | |
| 287 | if (fabs(captured_params.at("sqrt2").f - sqrt(2.f)) > 0.0001f) |
| 288 | return false; |
| 289 | |
| 290 | if (captured_params.at("1").f != 1.f) |
| 291 | return false; |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | const char* type_str() const |
| 297 | { |
| 298 | return "F.gelu"; |
| 299 | } |
| 300 | |
| 301 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 302 | { |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx, 9) |
| 307 | |
| 308 | class F_gelu_onnx_1 : public F_gelu_onnx |
| 309 | { |
| 310 | public: |
| 311 | // (x * 0.5) * (1.0 + torch.erf(x / math.sqrt(2.0))) |
| 312 | const char* match_pattern_graph() const |
| 313 | { |
| 314 | return R"PNNXIR(7767517 |
| 315 | 10 9 |
| 316 | pnnx.Input input 0 1 input |
| 317 | prim::Constant op_0 0 1 0p5 value=%0p5 |
| 318 | aten::mul op_1 2 1 input 0p5 15 |
| 319 | prim::Constant op_2 0 1 sqrt2 value=%sqrt2 |
| 320 | aten::div op_3 2 1 input sqrt2 16 |
| 321 | aten::erf op_4 1 1 16 17 |
| 322 | prim::Constant op_5 0 1 one value=%1 |
| 323 | aten::add op_6 2 1 17 one 22 |
| 324 | aten::mul op_7 2 1 15 22 out |
| 325 | pnnx.Output output 1 0 out |
| 326 | )PNNXIR"; |
| 327 | } |
| 328 | }; |
| 329 | |
| 330 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx_1, 9) |
| 331 | |
| 332 | class F_gelu_onnx_2 : public GraphRewriterPass |
| 333 | { |
| 334 | public: |
| 335 | const char* match_pattern_graph() const |
| 336 | { |
| 337 | return R"PNNXIR(7767517 |
| 338 | 3 2 |
| 339 | pnnx.Input input 0 1 input |
| 340 | Gelu op_0 1 1 input out approximate=%approximate |
| 341 | pnnx.Output output 1 0 out |
| 342 | )PNNXIR"; |
| 343 | } |
| 344 | |
| 345 | const char* type_str() const |
| 346 | { |
| 347 | return "F.gelu"; |
| 348 | } |
| 349 | }; |
| 350 | |
| 351 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx_2, 10) |
| 352 | |
| 353 | } // namespace pnnx |