| 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_hardsigmoid : 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::hardsigmoid 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.hardsigmoid"; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid, 10) |
| 39 | |
| 40 | class F_hardsigmoid_2 : public GraphRewriterPass |
| 41 | { |
| 42 | public: |
| 43 | const char* match_pattern_graph() const |
| 44 | { |
| 45 | return R"PNNXIR(7767517 |
| 46 | 10 9 |
| 47 | pnnx.Input input 0 1 input |
| 48 | prim::Constant op_0 0 1 410 value=3 |
| 49 | prim::Constant op_1 0 1 412 value=1 |
| 50 | aten::add op_2 3 1 input 410 412 a |
| 51 | prim::Constant op_3 0 1 413 value=0 |
| 52 | prim::Constant op_4 0 1 414 value=6 |
| 53 | aten::clamp op_5 3 1 a 413 414 b |
| 54 | prim::Constant op_6 0 1 409 value=6 |
| 55 | aten::div op_7 2 1 b 409 out |
| 56 | pnnx.Output output 1 0 out |
| 57 | )PNNXIR"; |
| 58 | } |
| 59 | |
| 60 | const char* type_str() const |
| 61 | { |
| 62 | return "F.hardsigmoid"; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_2, 9) |
| 67 | |
| 68 | class F_hardsigmoid_2_1 : public F_hardsigmoid_2 |
| 69 | { |
| 70 | public: |
| 71 | const char* match_pattern_graph() const |
| 72 | { |
| 73 | return R"PNNXIR(7767517 |
| 74 | 9 8 |
| 75 | pnnx.Input input 0 1 input |
| 76 | prim::Constant op_0 0 1 410 value=3 |
| 77 | aten::add op_1 2 1 input 410 a |
| 78 | prim::Constant op_2 0 1 413 value=0 |
| 79 | prim::Constant op_3 0 1 414 value=6 |
| 80 | aten::clamp op_4 3 1 a 413 414 b |
| 81 | prim::Constant op_5 0 1 409 value=6 |
| 82 | aten::div op_6 2 1 b 409 out |
| 83 | pnnx.Output output 1 0 out |
| 84 | )PNNXIR"; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_2_1, 9) |
| 89 | |
| 90 | class F_hardsigmoid_3 : public GraphRewriterPass |
| 91 | { |
| 92 | public: |
| 93 | const char* match_pattern_graph() const |
| 94 | { |
| 95 | return R"PNNXIR(7767517 |
| 96 | 10 9 |
| 97 | pnnx.Input input 0 1 input |
| 98 | prim::Constant op_0 0 1 12 value=3 |
| 99 | prim::Constant op_1 0 1 13 value=1 |
| 100 | aten::add op_2 3 1 input 12 13 a |
| 101 | prim::Constant op_3 0 1 16 value=0 |
| 102 | prim::Constant op_4 0 1 17 value=6 |
| 103 | aten::hardtanh op_5 3 1 a 16 17 b |
| 104 | prim::Constant op_6 0 1 19 value=6 |
| 105 | aten::div op_7 2 1 b 19 out |
| 106 | pnnx.Output output 1 0 out |
| 107 | )PNNXIR"; |
| 108 | } |
| 109 | |
| 110 | const char* type_str() const |
| 111 | { |
| 112 | return "F.hardsigmoid"; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_3, 9) |
| 117 | |
| 118 | class F_hardsigmoid_4 : public GraphRewriterPass |
| 119 | { |
| 120 | public: |
| 121 | const char* match_pattern_graph() const |
| 122 | { |
| 123 | return R"PNNXIR(7767517 |
| 124 | 8 7 |
| 125 | pnnx.Input input 0 1 input |
| 126 | prim::Constant op_0 0 1 12 value=3 |
| 127 | prim::Constant op_1 0 1 13 value=1 |
| 128 | aten::add op_2 3 1 input 12 13 a |
| 129 | aten::relu6 op_3 1 1 a b |
| 130 | prim::Constant op_4 0 1 19 value=6 |
| 131 | aten::div op_5 2 1 b 19 out |
| 132 | pnnx.Output output 1 0 out |
| 133 | )PNNXIR"; |
| 134 | } |
| 135 | |
| 136 | const char* type_str() const |
| 137 | { |
| 138 | return "F.hardsigmoid"; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_4, 9) |
| 143 | |
| 144 | class F_hardsigmoid_5 : public GraphRewriterPass |
| 145 | { |
| 146 | public: |
| 147 | const char* match_pattern_graph() const |
| 148 | { |
| 149 | return R"PNNXIR(7767517 |
| 150 | 8 7 |
| 151 | pnnx.Input input 0 1 input |
| 152 | prim::Constant op_1 0 1 22 value=1 |
| 153 | prim::Constant op_2 0 1 23 value=3 |
| 154 | aten::add op_3 3 1 input 23 22 a |
| 155 | nn.ReLU6 op_4 1 1 a b |
| 156 | prim::Constant op_0 0 1 21 value=6 |
| 157 | aten::div op_5 2 1 b 21 out |
| 158 | pnnx.Output output 1 0 out |
| 159 | )PNNXIR"; |
| 160 | } |
| 161 | |
| 162 | const char* type_str() const |
| 163 | { |
| 164 | return "F.hardsigmoid"; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_5, 9) |
| 169 | |
| 170 | static bool NearlyEqual(float a, float b, float epsilon) |
| 171 | { |
| 172 | if (a == b) |
| 173 | return true; |
| 174 | |
| 175 | float diff = (float)fabs(a - b); |
| 176 | if (diff <= epsilon) |
| 177 | return true; |
| 178 | |
| 179 | // relative error |
| 180 | return diff < epsilon * std::max(fabs(a), fabs(b)); |
| 181 | } |
| 182 | |
| 183 | class F_hardsigmoid_onnx : public GraphRewriterPass |
| 184 | { |
| 185 | public: |
| 186 | const char* match_pattern_graph() const |
| 187 | { |
| 188 | return R"PNNXIR(7767517 |
| 189 | 3 2 |
| 190 | pnnx.Input input 0 1 input |
| 191 | HardSigmoid op_0 1 1 input out alpha=%alpha |
| 192 | pnnx.Output output 1 0 out |
| 193 | )PNNXIR"; |
| 194 | } |
| 195 | |
| 196 | const char* type_str() const |
| 197 | { |
| 198 | return "F.hardsigmoid"; |
| 199 | } |
| 200 | |
| 201 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 202 | { |
| 203 | float alpha = captured_params.at("alpha").f; |
| 204 | return NearlyEqual(alpha, 1.f / 6, 0.001); |
| 205 | } |
| 206 | |
| 207 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 208 | { |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_onnx, 10) |
| 213 | |
| 214 | class F_hardsigmoid_onnx_1 : public GraphRewriterPass |
| 215 | { |
| 216 | public: |
| 217 | const char* match_pattern_graph() const |
| 218 | { |
| 219 | return R"PNNXIR(7767517 |
| 220 | 3 2 |
| 221 | pnnx.Input input 0 1 input |
| 222 | HardSigmoid op_0 1 1 input out alpha=%alpha beta=%beta |
| 223 | pnnx.Output output 1 0 out |
| 224 | )PNNXIR"; |
| 225 | } |
| 226 | |
| 227 | const char* type_str() const |
| 228 | { |
| 229 | return "F.hardsigmoid"; |
| 230 | } |
| 231 | |
| 232 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 233 | { |
| 234 | float alpha = captured_params.at("alpha").f; |
| 235 | float beta = captured_params.at("beta").f; |
| 236 | return NearlyEqual(alpha, 1.f / 6, 0.001) && NearlyEqual(beta, 0.5f, 0.001); |
| 237 | } |
| 238 | |
| 239 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 240 | { |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_onnx_1, 10) |
| 245 | |
| 246 | class F_hardsigmoid_onnx_2 : public GraphRewriterPass |
| 247 | { |
| 248 | public: |
| 249 | const char* match_pattern_graph() const |
| 250 | { |
| 251 | return R"PNNXIR(7767517 |
| 252 | 3 2 |
| 253 | pnnx.Input input 0 1 input |
| 254 | HardSigmoid op_0 1 1 input out alpha=%alpha |
| 255 | pnnx.Output output 1 0 out |
| 256 | )PNNXIR"; |
| 257 | } |
| 258 | |
| 259 | const char* replace_pattern_graph() const |
| 260 | { |
| 261 | return R"PNNXIR(7767517 |
| 262 | 5 4 |
| 263 | pnnx.Input input 0 1 input |
| 264 | prim::Constant alpha2 0 1 alpha2 |
| 265 | aten::mul mul 2 1 input alpha2 a |
| 266 | F.hardsigmoid hs 1 1 a out |
| 267 | pnnx.Output output 1 0 out |
| 268 | )PNNXIR"; |
| 269 | } |
| 270 | |
| 271 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 272 | { |
| 273 | float alpha = captured_params.at("alpha").f; |
| 274 | return !NearlyEqual(alpha, 1.f / 6, 0.001); |
| 275 | } |
| 276 | |
| 277 | void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params) const |
| 278 | { |
| 279 | const float alpha = captured_params.at("alpha").f; |
| 280 | ops.at("alpha2")->params["value"] = alpha / (1.f / 6); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_onnx_2, 10) |
| 285 | |
| 286 | class F_hardsigmoid_onnx_3 : public F_hardsigmoid_onnx_2 |
| 287 | { |
| 288 | public: |
| 289 | const char* match_pattern_graph() const |
| 290 | { |
| 291 | return R"PNNXIR(7767517 |
| 292 | 3 2 |
| 293 | pnnx.Input input 0 1 input |
| 294 | HardSigmoid op_0 1 1 input out alpha=%alpha beta=%beta |
| 295 | pnnx.Output output 1 0 out |
| 296 | )PNNXIR"; |
| 297 | } |
| 298 | |
| 299 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 300 | { |
| 301 | float alpha = captured_params.at("alpha").f; |
| 302 | float beta = captured_params.at("beta").f; |
| 303 | return !NearlyEqual(alpha, 1.f / 6, 0.001) && NearlyEqual(beta, 0.5f, 0.001); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_onnx_3, 10) |
| 308 | |
| 309 | } // namespace pnnx |