| 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_hardswish : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 3 2 |
| 26 | pnnx.Input input 0 1 input |
| 27 | aten::hardswish op_0 1 1 input out |
| 28 | pnnx.Output output 1 0 out |
| 29 | )PNNXIR"; |
| 30 | } |
| 31 | |
| 32 | const char* type_str() const |
| 33 | { |
| 34 | return "F.hardswish"; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish, 10) |
| 39 | |
| 40 | class F_hardswish_1 : public GraphRewriterPass |
| 41 | { |
| 42 | public: |
| 43 | const char* match_pattern_graph() const |
| 44 | { |
| 45 | return R"PNNXIR(7767517 |
| 46 | 11 10 |
| 47 | pnnx.Input input 0 1 input |
| 48 | prim::Constant op_0 0 1 392 value=3 |
| 49 | prim::Constant op_1 0 1 393 value=1 |
| 50 | aten::add op_2 3 1 input 392 393 a |
| 51 | prim::Constant op_3 0 1 394 value=0 |
| 52 | prim::Constant op_4 0 1 395 value=6 |
| 53 | aten::hardtanh op_5 3 1 a 394 395 b |
| 54 | aten::mul op_6 2 1 input b c |
| 55 | prim::Constant op_7 0 1 391 value=6 |
| 56 | aten::div op_8 2 1 c 391 out |
| 57 | pnnx.Output output 1 0 out |
| 58 | )PNNXIR"; |
| 59 | } |
| 60 | |
| 61 | const char* type_str() const |
| 62 | { |
| 63 | return "F.hardswish"; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_1, 8) |
| 68 | |
| 69 | class F_hardswish_2 : public GraphRewriterPass |
| 70 | { |
| 71 | public: |
| 72 | const char* match_pattern_graph() const |
| 73 | { |
| 74 | return R"PNNXIR(7767517 |
| 75 | 4 3 |
| 76 | pnnx.Input input 0 1 input |
| 77 | aten::hardsigmoid op_0 1 1 input a |
| 78 | aten::mul op_1 2 1 input a out |
| 79 | pnnx.Output output 1 0 out |
| 80 | )PNNXIR"; |
| 81 | } |
| 82 | |
| 83 | const char* type_str() const |
| 84 | { |
| 85 | return "F.hardswish"; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_2, 9) |
| 90 | |
| 91 | class F_hardswish_3 : public GraphRewriterPass |
| 92 | { |
| 93 | public: |
| 94 | const char* match_pattern_graph() const |
| 95 | { |
| 96 | return R"PNNXIR(7767517 |
| 97 | 11 10 |
| 98 | pnnx.Input input 0 1 input |
| 99 | prim::Constant op_0 0 1 12 value=3 |
| 100 | prim::Constant op_1 0 1 13 value=1 |
| 101 | aten::add op_2 3 1 input 12 13 a |
| 102 | prim::Constant op_3 0 1 17 value=0 |
| 103 | prim::Constant op_4 0 1 18 value=6 |
| 104 | aten::hardtanh op_5 3 1 a 17 18 b |
| 105 | aten::mul op_6 2 1 input b c |
| 106 | prim::Constant op_7 0 1 22 value=6 |
| 107 | aten::div op_8 2 1 c 22 out |
| 108 | pnnx.Output output 1 0 out |
| 109 | )PNNXIR"; |
| 110 | } |
| 111 | |
| 112 | const char* type_str() const |
| 113 | { |
| 114 | return "F.hardswish"; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_3, 8) |
| 119 | |
| 120 | class F_hardswish_4 : public GraphRewriterPass |
| 121 | { |
| 122 | public: |
| 123 | const char* match_pattern_graph() const |
| 124 | { |
| 125 | return R"PNNXIR(7767517 |
| 126 | 11 10 |
| 127 | pnnx.Input input 0 1 input |
| 128 | prim::Constant op_0 0 1 25 value=3 |
| 129 | prim::Constant op_1 0 1 47 value=1 |
| 130 | aten::add op_2 3 1 input 25 47 a |
| 131 | prim::Constant op_3 0 1 48 value=0 |
| 132 | prim::Constant op_4 0 1 49 value=6 |
| 133 | aten::hardtanh op_5 3 1 a 48 49 b |
| 134 | prim::Constant op_6 0 1 50 value=6 |
| 135 | aten::div op_7 2 1 b 50 c |
| 136 | aten::mul op_8 2 1 c input out |
| 137 | pnnx.Output output 1 0 out |
| 138 | )PNNXIR"; |
| 139 | } |
| 140 | |
| 141 | const char* type_str() const |
| 142 | { |
| 143 | return "F.hardswish"; |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_4, 8) |
| 148 | |
| 149 | class F_hardswish_4_1 : public F_hardswish_4 |
| 150 | { |
| 151 | public: |
| 152 | const char* match_pattern_graph() const |
| 153 | { |
| 154 | return R"PNNXIR(7767517 |
| 155 | 10 9 |
| 156 | pnnx.Input input 0 1 input |
| 157 | prim::Constant op_0 0 1 25 value=3 |
| 158 | aten::add op_2 2 1 input 25 a |
| 159 | prim::Constant op_3 0 1 48 value=0 |
| 160 | prim::Constant op_4 0 1 49 value=6 |
| 161 | aten::clamp op_5 3 1 a 48 49 b |
| 162 | prim::Constant op_6 0 1 50 value=6 |
| 163 | aten::div op_7 2 1 b 50 c |
| 164 | aten::mul op_8 2 1 c input out |
| 165 | pnnx.Output output 1 0 out |
| 166 | )PNNXIR"; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_4_1, 8) |
| 171 | |
| 172 | class F_hardswish_5 : public GraphRewriterPass |
| 173 | { |
| 174 | public: |
| 175 | const char* match_pattern_graph() const |
| 176 | { |
| 177 | return R"PNNXIR(7767517 |
| 178 | 9 8 |
| 179 | pnnx.Input input 0 1 input |
| 180 | prim::Constant op_0 0 1 25 value=3 |
| 181 | prim::Constant op_1 0 1 48 value=1 |
| 182 | aten::add op_2 3 1 input 25 48 a |
| 183 | aten::relu6 op_3 1 1 a b |
| 184 | prim::Constant op_4 0 1 49 value=6 |
| 185 | aten::div op_5 2 1 b 49 c |
| 186 | aten::mul op_6 2 1 c input out |
| 187 | pnnx.Output output 1 0 out |
| 188 | )PNNXIR"; |
| 189 | } |
| 190 | |
| 191 | const char* type_str() const |
| 192 | { |
| 193 | return "F.hardswish"; |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_5, 8) |
| 198 | |
| 199 | class F_hardswish_6 : public GraphRewriterPass |
| 200 | { |
| 201 | public: |
| 202 | const char* match_pattern_graph() const |
| 203 | { |
| 204 | return R"PNNXIR(7767517 |
| 205 | 9 8 |
| 206 | pnnx.Input input 0 1 input |
| 207 | prim::Constant op_0 0 1 25 value=3 |
| 208 | prim::Constant op_1 0 1 48 value=1 |
| 209 | aten::add op_2 3 1 input 25 48 a |
| 210 | aten::relu6 op_3 1 1 a b |
| 211 | aten::mul op_4 2 1 input b c |
| 212 | prim::Constant op_5 0 1 49 value=6 |
| 213 | aten::div op_6 2 1 c 49 out |
| 214 | pnnx.Output output 1 0 out |
| 215 | )PNNXIR"; |
| 216 | } |
| 217 | |
| 218 | const char* type_str() const |
| 219 | { |
| 220 | return "F.hardswish"; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_6, 8) |
| 225 | |
| 226 | class F_hardswish_7 : public GraphRewriterPass |
| 227 | { |
| 228 | public: |
| 229 | const char* match_pattern_graph() const |
| 230 | { |
| 231 | return R"PNNXIR(7767517 |
| 232 | 10 9 |
| 233 | pnnx.Input input 0 1 input |
| 234 | prim::Constant op_0 0 1 25 value=3 |
| 235 | aten::add op_1 2 1 input 25 a |
| 236 | prim::Constant op_2 0 1 min value=0 |
| 237 | prim::Constant op_3 0 1 max value=6 |
| 238 | aten::clamp op_4 3 1 a min max b |
| 239 | aten::mul op_5 2 1 input b c |
| 240 | prim::Constant op_6 0 1 49 value=6 |
| 241 | aten::div op_7 2 1 c 49 out |
| 242 | pnnx.Output output 1 0 out |
| 243 | )PNNXIR"; |
| 244 | } |
| 245 | |
| 246 | const char* type_str() const |
| 247 | { |
| 248 | return "F.hardswish"; |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_7, 8) |
| 253 | |
| 254 | class F_hardswish_onnx : public GraphRewriterPass |
| 255 | { |
| 256 | public: |
| 257 | const char* match_pattern_graph() const |
| 258 | { |
| 259 | return R"PNNXIR(7767517 |
| 260 | 3 2 |
| 261 | pnnx.Input input 0 1 input |
| 262 | HardSwish op_0 1 1 input out |
| 263 | pnnx.Output output 1 0 out |
| 264 | )PNNXIR"; |
| 265 | } |
| 266 | |
| 267 | const char* type_str() const |
| 268 | { |
| 269 | return "F.hardswish"; |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx, 10) |
| 274 | |
| 275 | static bool NearlyEqual(float a, float b, float epsilon) |
| 276 | { |
| 277 | if (a == b) |
| 278 | return true; |
| 279 | |
| 280 | float diff = (float)fabs(a - b); |
| 281 | if (diff <= epsilon) |
| 282 | return true; |
| 283 | |
| 284 | // relative error |
| 285 | return diff < epsilon * std::max(fabs(a), fabs(b)); |
| 286 | } |
| 287 | |
| 288 | class F_hardswish_onnx_1 : public GraphRewriterPass |
| 289 | { |
| 290 | public: |
| 291 | const char* match_pattern_graph() const |
| 292 | { |
| 293 | return R"PNNXIR(7767517 |
| 294 | 4 3 |
| 295 | pnnx.Input input 0 1 input |
| 296 | HardSigmoid op_0 1 1 input h alpha=%alpha |
| 297 | aten::mul op_1 2 1 input h out |
| 298 | pnnx.Output output 1 0 out |
| 299 | )PNNXIR"; |
| 300 | } |
| 301 | |
| 302 | const char* type_str() const |
| 303 | { |
| 304 | return "F.hardswish"; |
| 305 | } |
| 306 | |
| 307 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 308 | { |
| 309 | float alpha = captured_params.at("alpha").f; |
| 310 | return NearlyEqual(alpha, 1.f / 6, 0.001); |
| 311 | } |
| 312 | |
| 313 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 314 | { |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx_1, 9) |
| 319 | |
| 320 | class F_hardswish_onnx_2 : public GraphRewriterPass |
| 321 | { |
| 322 | public: |
| 323 | const char* match_pattern_graph() const |
| 324 | { |
| 325 | return R"PNNXIR(7767517 |
| 326 | 8 7 |
| 327 | pnnx.Input input 0 1 input |
| 328 | prim::Constant op_0 0 1 20 value=3 |
| 329 | aten::add op_1 2 1 input 20 8 |
| 330 | aten::clamp op_2 1 1 8 9 max=6 min=0 |
| 331 | prim::Constant op_3 0 1 23 value=6 |
| 332 | aten::div op_4 2 1 9 23 10 |
| 333 | aten::mul op_5 2 1 input 10 out |
| 334 | pnnx.Output output 1 0 out |
| 335 | )PNNXIR"; |
| 336 | } |
| 337 | |
| 338 | const char* type_str() const |
| 339 | { |
| 340 | return "F.hardswish"; |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx_2, 9) |
| 345 | |
| 346 | class F_hardswish_onnx_3 : public GraphRewriterPass |
| 347 | { |
| 348 | public: |
| 349 | const char* match_pattern_graph() const |
| 350 | { |
| 351 | return R"PNNXIR(7767517 |
| 352 | 8 7 |
| 353 | pnnx.Input input 0 1 input |
| 354 | prim::Constant op_0 0 1 v3 value=3 |
| 355 | aten::add op_1 2 1 input v3 a |
| 356 | aten::clamp op_2 1 1 a b max=6 min=0 |
| 357 | aten::mul op_3 2 1 input b c |
| 358 | prim::Constant op_4 0 1 v6 value=6 |
| 359 | aten::div op_5 2 1 c v6 out |
| 360 | pnnx.Output output 1 0 out |
| 361 | )PNNXIR"; |
| 362 | } |
| 363 | |
| 364 | const char* type_str() const |
| 365 | { |
| 366 | return "F.hardswish"; |
| 367 | } |
| 368 | }; |
| 369 | |
| 370 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx_3, 9) |
| 371 | |
| 372 | } // namespace pnnx |