| 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 "fuse_convolutiondepthwise1d_activation.h" |
| 16 | |
| 17 | #include "pass_level2.h" |
| 18 | |
| 19 | #include <float.h> |
| 20 | |
| 21 | namespace pnnx { |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | class fuse_convolutiondepthwise1d_relu_pass : public GraphRewriterPass |
| 26 | { |
| 27 | public: |
| 28 | const char* match_pattern_graph() const |
| 29 | { |
| 30 | return R"PNNXIR(7767517 |
| 31 | 4 3 |
| 32 | pnnx.Input input 0 1 input |
| 33 | ConvolutionDepthWise1D op_0 1 1 input a %*=%* |
| 34 | ReLU op_1 1 1 a out %*=%* |
| 35 | pnnx.Output output 1 0 out |
| 36 | )PNNXIR"; |
| 37 | } |
| 38 | |
| 39 | const char* type_str() const |
| 40 | { |
| 41 | return "ConvolutionDepthWise1D"; |
| 42 | } |
| 43 | |
| 44 | const char* name_str() const |
| 45 | { |
| 46 | return "convdw1drelu"; |
| 47 | } |
| 48 | |
| 49 | bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const |
| 50 | { |
| 51 | return captured_params.find("op_0.9") == captured_params.end(); |
| 52 | } |
| 53 | |
| 54 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 55 | { |
| 56 | for (const auto& p : captured_params) |
| 57 | { |
| 58 | const std::string& pkey = p.first; |
| 59 | const Parameter& pp = p.second; |
| 60 | |
| 61 | if (pkey.substr(0, 5) == "op_0.") |
| 62 | op->params[pkey.substr(5)] = pp; |
| 63 | } |
| 64 | |
| 65 | for (const auto& a : captured_attrs) |
| 66 | { |
| 67 | const std::string& akey = a.first; |
| 68 | const Attribute& ap = a.second; |
| 69 | |
| 70 | if (akey.substr(0, 5) == "op_0.") |
| 71 | op->attrs[akey.substr(5)] = ap; |
| 72 | } |
| 73 | |
| 74 | float slope = 0.f; |
| 75 | if (captured_params.find("op_1.0") != captured_params.end()) |
| 76 | { |
| 77 | slope = captured_params.at("op_1.0").f; |
| 78 | } |
| 79 | |
| 80 | if (slope == 0.f) |
| 81 | { |
| 82 | op->params["9"] = 1; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | op->params["9"] = 2; |
| 87 | op->params["10"] = Parameter{slope}; |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | class fuse_convolutiondepthwise1d_clip_pass : public GraphRewriterPass |
| 93 | { |
| 94 | public: |
| 95 | const char* match_pattern_graph() const |
| 96 | { |
| 97 | return R"PNNXIR(7767517 |
| 98 | 4 3 |
| 99 | pnnx.Input input 0 1 input |
| 100 | ConvolutionDepthWise1D op_0 1 1 input a %*=%* |
| 101 | Clip op_1 1 1 a out %*=%* |
| 102 | pnnx.Output output 1 0 out |
| 103 | )PNNXIR"; |
| 104 | } |
| 105 | |
| 106 | const char* type_str() const |
| 107 | { |
| 108 | return "ConvolutionDepthWise1D"; |
| 109 | } |
| 110 | |
| 111 | const char* name_str() const |
| 112 | { |
| 113 | return "convdw1dclip"; |
| 114 | } |
| 115 | |
| 116 | bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const |
| 117 | { |
| 118 | return captured_params.find("op_0.9") == captured_params.end(); |
| 119 | } |
| 120 | |
| 121 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 122 | { |
| 123 | for (const auto& p : captured_params) |
| 124 | { |
| 125 | const std::string& pkey = p.first; |
| 126 | const Parameter& pp = p.second; |
| 127 | |
| 128 | if (pkey.substr(0, 5) == "op_0.") |
| 129 | op->params[pkey.substr(5)] = pp; |
| 130 | } |
| 131 | |
| 132 | for (const auto& a : captured_attrs) |
| 133 | { |
| 134 | const std::string& akey = a.first; |
| 135 | const Attribute& ap = a.second; |
| 136 | |
| 137 | if (akey.substr(0, 5) == "op_0.") |
| 138 | op->attrs[akey.substr(5)] = ap; |
| 139 | } |
| 140 | |
| 141 | float min = -FLT_MAX; |
| 142 | float max = FLT_MAX; |
| 143 | if (captured_params.find("op_1.0") != captured_params.end()) |
| 144 | { |
| 145 | min = captured_params.at("op_1.0").f; |
| 146 | } |
| 147 | if (captured_params.find("op_1.1") != captured_params.end()) |
| 148 | { |
| 149 | max = captured_params.at("op_1.1").f; |
| 150 | } |
| 151 | |
| 152 | op->params["9"] = 3; |
| 153 | op->params["10"] = Parameter{min, max}; |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | class fuse_convolutiondepthwise1d_sigmoid_pass : public GraphRewriterPass |
| 158 | { |
| 159 | public: |
| 160 | const char* match_pattern_graph() const |
| 161 | { |
| 162 | return R"PNNXIR(7767517 |
| 163 | 4 3 |
| 164 | pnnx.Input input 0 1 input |
| 165 | ConvolutionDepthWise1D op_0 1 1 input a %*=%* |
| 166 | Sigmoid op_1 1 1 a out |
| 167 | pnnx.Output output 1 0 out |
| 168 | )PNNXIR"; |
| 169 | } |
| 170 | |
| 171 | const char* type_str() const |
| 172 | { |
| 173 | return "ConvolutionDepthWise1D"; |
| 174 | } |
| 175 | |
| 176 | const char* name_str() const |
| 177 | { |
| 178 | return "convdw1dsigmoid"; |
| 179 | } |
| 180 | |
| 181 | bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const |
| 182 | { |
| 183 | return captured_params.find("op_0.9") == captured_params.end(); |
| 184 | } |
| 185 | |
| 186 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 187 | { |
| 188 | for (const auto& p : captured_params) |
| 189 | { |
| 190 | const std::string& pkey = p.first; |
| 191 | const Parameter& pp = p.second; |
| 192 | |
| 193 | if (pkey.substr(0, 5) == "op_0.") |
| 194 | op->params[pkey.substr(5)] = pp; |
| 195 | } |
| 196 | |
| 197 | for (const auto& a : captured_attrs) |
| 198 | { |
| 199 | const std::string& akey = a.first; |
| 200 | const Attribute& ap = a.second; |
| 201 | |
| 202 | if (akey.substr(0, 5) == "op_0.") |
| 203 | op->attrs[akey.substr(5)] = ap; |
| 204 | } |
| 205 | |
| 206 | op->params["9"] = 4; |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | class fuse_convolutiondepthwise1d_mish_pass : public GraphRewriterPass |
| 211 | { |
| 212 | public: |
| 213 | const char* match_pattern_graph() const |
| 214 | { |
| 215 | return R"PNNXIR(7767517 |
| 216 | 4 3 |
| 217 | pnnx.Input input 0 1 input |
| 218 | ConvolutionDepthWise1D op_0 1 1 input a %*=%* |
| 219 | Mish op_1 1 1 a out |
| 220 | pnnx.Output output 1 0 out |
| 221 | )PNNXIR"; |
| 222 | } |
| 223 | |
| 224 | const char* type_str() const |
| 225 | { |
| 226 | return "ConvolutionDepthWise1D"; |
| 227 | } |
| 228 | |
| 229 | const char* name_str() const |
| 230 | { |
| 231 | return "convdw1dmish"; |
| 232 | } |
| 233 | |
| 234 | bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const |
| 235 | { |
| 236 | return captured_params.find("op_0.9") == captured_params.end(); |
| 237 | } |
| 238 | |
| 239 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 240 | { |
| 241 | for (const auto& p : captured_params) |
| 242 | { |
| 243 | const std::string& pkey = p.first; |
| 244 | const Parameter& pp = p.second; |
| 245 | |
| 246 | if (pkey.substr(0, 5) == "op_0.") |
| 247 | op->params[pkey.substr(5)] = pp; |
| 248 | } |
| 249 | |
| 250 | for (const auto& a : captured_attrs) |
| 251 | { |
| 252 | const std::string& akey = a.first; |
| 253 | const Attribute& ap = a.second; |
| 254 | |
| 255 | if (akey.substr(0, 5) == "op_0.") |
| 256 | op->attrs[akey.substr(5)] = ap; |
| 257 | } |
| 258 | |
| 259 | op->params["9"] = 5; |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | void fuse_convolutiondepthwise1d_activation(Graph& graph) |
| 264 | { |
| 265 | fuse_convolutiondepthwise1d_relu_pass a; |
| 266 | fuse_convolutiondepthwise1d_clip_pass b; |
| 267 | fuse_convolutiondepthwise1d_sigmoid_pass c; |
| 268 | fuse_convolutiondepthwise1d_mish_pass d; |
| 269 | int opindex = 0; |
| 270 | |
| 271 | pnnx_graph_rewrite(graph, &a, opindex); |
| 272 | pnnx_graph_rewrite(graph, &b, opindex); |
| 273 | pnnx_graph_rewrite(graph, &c, opindex); |
| 274 | pnnx_graph_rewrite(graph, &d, opindex); |
| 275 | } |
| 276 | |
| 277 | } // namespace ncnn |
| 278 | |
| 279 | } // namespace pnnx |