| 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_linear : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 5 4 |
| 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 | aten::linear op_0 3 1 input weight bias out |
| 30 | pnnx.Output output 1 0 out |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* type_str() const |
| 35 | { |
| 36 | return "F.linear"; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear, 10) |
| 41 | |
| 42 | class F_linear_1 : public GraphRewriterPass |
| 43 | { |
| 44 | public: |
| 45 | const char* match_pattern_graph() const |
| 46 | { |
| 47 | return R"PNNXIR(7767517 |
| 48 | 8 7 |
| 49 | pnnx.Input input_0 0 1 input |
| 50 | pnnx.Input input_1 0 1 weight |
| 51 | pnnx.Input input_2 0 1 bias |
| 52 | aten::t op_0 1 1 weight 9 |
| 53 | aten::matmul op_1 2 1 input 9 a |
| 54 | prim::Constant op_2 0 1 19 value=1 |
| 55 | aten::add op_3 3 1 a bias 19 out |
| 56 | pnnx.Output output 1 0 out |
| 57 | )PNNXIR"; |
| 58 | } |
| 59 | |
| 60 | const char* type_str() const |
| 61 | { |
| 62 | return "F.linear"; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_1, 9) |
| 67 | |
| 68 | class F_linear_2 : public GraphRewriterPass |
| 69 | { |
| 70 | public: |
| 71 | const char* match_pattern_graph() const |
| 72 | { |
| 73 | return R"PNNXIR(7767517 |
| 74 | 5 4 |
| 75 | pnnx.Input input_0 0 1 input |
| 76 | pnnx.Input input_1 0 1 weight |
| 77 | aten::t op_0 1 1 weight 9 |
| 78 | aten::matmul op_1 2 1 input 9 out |
| 79 | pnnx.Output output 1 0 out |
| 80 | )PNNXIR"; |
| 81 | } |
| 82 | |
| 83 | const char* type_str() const |
| 84 | { |
| 85 | return "F.linear"; |
| 86 | } |
| 87 | |
| 88 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 89 | { |
| 90 | op->params["bias"] = Parameter(); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_2, 10) |
| 95 | |
| 96 | class F_linear_3 : public GraphRewriterPass |
| 97 | { |
| 98 | public: |
| 99 | const char* match_pattern_graph() const |
| 100 | { |
| 101 | return R"PNNXIR(7767517 |
| 102 | 8 7 |
| 103 | pnnx.Input input_0 0 1 input |
| 104 | pnnx.Input input_1 0 1 weight |
| 105 | pnnx.Input input_2 0 1 bias |
| 106 | aten::t op_0 1 1 weight 14 |
| 107 | prim::Constant op_1 0 1 15 value=1 |
| 108 | prim::Constant op_2 0 1 30 value=1 |
| 109 | aten::addmm op_3 5 1 bias input 14 15 30 out |
| 110 | pnnx.Output output 1 0 out |
| 111 | )PNNXIR"; |
| 112 | } |
| 113 | |
| 114 | const char* type_str() const |
| 115 | { |
| 116 | return "F.linear"; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_3, 10) |
| 121 | |
| 122 | class F_linear_onnx : public GraphRewriterPass |
| 123 | { |
| 124 | public: |
| 125 | const char* match_pattern_graph() const |
| 126 | { |
| 127 | return R"PNNXIR(7767517 |
| 128 | 5 4 |
| 129 | pnnx.Input input_0 0 1 input |
| 130 | pnnx.Input input_1 0 1 weight |
| 131 | pnnx.Input input_2 0 1 bias |
| 132 | Gemm gemm 3 1 input weight bias out %*=%* |
| 133 | pnnx.Output output 1 0 out |
| 134 | )PNNXIR"; |
| 135 | } |
| 136 | |
| 137 | const char* type_str() const |
| 138 | { |
| 139 | return "F.linear"; |
| 140 | } |
| 141 | |
| 142 | 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 |
| 143 | { |
| 144 | if (captured_params.find("gemm.alpha") != captured_params.end()) |
| 145 | { |
| 146 | if (captured_params.at("gemm.alpha").type != 3 || captured_params.at("gemm.alpha").f != 1.f) |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if (captured_params.find("gemm.beta") != captured_params.end()) |
| 151 | { |
| 152 | if (captured_params.at("gemm.beta").type != 3 || captured_params.at("gemm.beta").f != 1.f) |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | if (captured_params.find("gemm.transA") != captured_params.end()) |
| 157 | { |
| 158 | if (captured_params.at("gemm.transA").type != 2 || captured_params.at("gemm.transA").i != 0) |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if (captured_params.find("gemm.transB") == captured_params.end()) |
| 163 | return false; |
| 164 | |
| 165 | if (captured_params.at("gemm.transB").type != 2 || captured_params.at("gemm.transB").i != 1) |
| 166 | return false; |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 172 | { |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx, 10) |
| 177 | |
| 178 | class F_linear_onnx_1 : public GraphRewriterPass |
| 179 | { |
| 180 | public: |
| 181 | const char* match_pattern_graph() const |
| 182 | { |
| 183 | return R"PNNXIR(7767517 |
| 184 | 5 4 |
| 185 | pnnx.Input input_0 0 1 input |
| 186 | pnnx.Input input_1 0 1 bias |
| 187 | pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32 |
| 188 | Gemm gemm 3 1 input weight bias out %*=%* |
| 189 | pnnx.Output output 1 0 out |
| 190 | )PNNXIR"; |
| 191 | } |
| 192 | |
| 193 | const char* replace_pattern_graph() const |
| 194 | { |
| 195 | return R"PNNXIR(7767517 |
| 196 | 5 4 |
| 197 | pnnx.Input input_0 0 1 input |
| 198 | pnnx.Input input_1 0 1 bias |
| 199 | pnnx.Attribute weight 0 1 weight |
| 200 | F.linear linear 3 1 input weight bias out $weight=weight |
| 201 | pnnx.Output output 1 0 out |
| 202 | )PNNXIR"; |
| 203 | } |
| 204 | |
| 205 | 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 |
| 206 | { |
| 207 | if (captured_params.find("gemm.alpha") != captured_params.end()) |
| 208 | { |
| 209 | if (captured_params.at("gemm.alpha").type != 3 || captured_params.at("gemm.alpha").f != 1.f) |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | if (captured_params.find("gemm.beta") != captured_params.end()) |
| 214 | { |
| 215 | if (captured_params.at("gemm.beta").type != 3 || captured_params.at("gemm.beta").f != 1.f) |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | if (captured_params.find("gemm.transA") != captured_params.end()) |
| 220 | { |
| 221 | if (captured_params.at("gemm.transA").type != 2 || captured_params.at("gemm.transA").i != 0) |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | if (captured_params.find("gemm.transB") != captured_params.end()) |
| 226 | { |
| 227 | if (captured_params.at("gemm.transB").type != 2 || captured_params.at("gemm.transB").i != 0) |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 235 | { |
| 236 | const int in_features = captured_params.at("in_features").i; |
| 237 | const int out_features = captured_params.at("out_features").i; |
| 238 | |
| 239 | auto weight = captured_attrs.at("weight.data").get_float32_data(); |
| 240 | std::vector<float> transposed_weight(in_features * out_features); |
| 241 | for (int i = 0; i < out_features; i++) |
| 242 | { |
| 243 | float* wptr = (float*)transposed_weight.data() + in_features * i; |
| 244 | for (int j = 0; j < in_features; j++) |
| 245 | { |
| 246 | wptr[j] = ((const float*)weight.data())[out_features * j + i]; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | Operator* op_weight = ops.at("weight"); |
| 251 | op_weight->attrs["data"] = Attribute({out_features, in_features}, transposed_weight); |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_1, 9) |
| 256 | |
| 257 | class F_linear_onnx_2 : public F_linear_onnx_1 |
| 258 | { |
| 259 | public: |
| 260 | const char* match_pattern_graph() const |
| 261 | { |
| 262 | return R"PNNXIR(7767517 |
| 263 | 4 3 |
| 264 | pnnx.Input input_0 0 1 input |
| 265 | pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32 |
| 266 | MatMul matmul 2 1 input weight out |
| 267 | pnnx.Output output 1 0 out |
| 268 | )PNNXIR"; |
| 269 | } |
| 270 | |
| 271 | const char* replace_pattern_graph() const |
| 272 | { |
| 273 | return R"PNNXIR(7767517 |
| 274 | 4 3 |
| 275 | pnnx.Input input_0 0 1 input |
| 276 | pnnx.Attribute weight 0 1 weight |
| 277 | F.linear linear 2 1 input weight out bias=None $weight=weight |
| 278 | pnnx.Output output 1 0 out |
| 279 | )PNNXIR"; |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_2, 9) |
| 284 | |
| 285 | class F_linear_onnx_3 : public F_linear_onnx_1 |
| 286 | { |
| 287 | public: |
| 288 | const char* match_pattern_graph() const |
| 289 | { |
| 290 | return R"PNNXIR(7767517 |
| 291 | 6 5 |
| 292 | pnnx.Input input_0 0 1 input |
| 293 | pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32 |
| 294 | pnnx.Attribute bias 0 1 bias @data=(%out_features)f32 |
| 295 | MatMul matmul 2 1 input weight mm |
| 296 | aten::add add 2 1 mm bias out |
| 297 | pnnx.Output output 1 0 out |
| 298 | )PNNXIR"; |
| 299 | } |
| 300 | |
| 301 | const char* replace_pattern_graph() const |
| 302 | { |
| 303 | return R"PNNXIR(7767517 |
| 304 | 5 4 |
| 305 | pnnx.Input input_0 0 1 input |
| 306 | pnnx.Attribute weight 0 1 weight |
| 307 | pnnx.Attribute bias 0 1 bias |
| 308 | F.linear linear 3 1 input weight bias out $weight=weight $bias=bias |
| 309 | pnnx.Output output 1 0 out |
| 310 | )PNNXIR"; |
| 311 | } |
| 312 | |
| 313 | void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 314 | { |
| 315 | F_linear_onnx_1::write(ops, captured_params, captured_attrs); |
| 316 | |
| 317 | Operator* op_bias = ops.at("bias"); |
| 318 | op_bias->attrs["data"] = captured_attrs.at("bias.data"); |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_3, 8) |
| 323 | |
| 324 | class F_linear_onnx_4 : public F_linear_onnx_3 |
| 325 | { |
| 326 | public: |
| 327 | const char* match_pattern_graph() const |
| 328 | { |
| 329 | return R"PNNXIR(7767517 |
| 330 | 6 5 |
| 331 | pnnx.Input input_0 0 1 input |
| 332 | pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32 |
| 333 | pnnx.Attribute bias 0 1 bias @data=(%out_features)f32 |
| 334 | MatMul matmul 2 1 input weight mm |
| 335 | aten::add add 2 1 bias mm out |
| 336 | pnnx.Output output 1 0 out |
| 337 | )PNNXIR"; |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_4, 8) |
| 342 | |
| 343 | } // namespace pnnx |