| 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_ncnn.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | class nn_Linear_0 : public GraphRewriterPass |
| 22 | { |
| 23 | public: |
| 24 | const char* match_pattern_graph() const |
| 25 | { |
| 26 | return R"PNNXIR(7767517 |
| 27 | 3 2 |
| 28 | pnnx.Input input 0 1 input #input=(1,%m,%in_features)f32 |
| 29 | nn.Linear op_0 1 1 input out in_features=%in_features out_features=%out_features bias=%bias |
| 30 | pnnx.Output output 1 0 out |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* type_str() const |
| 35 | { |
| 36 | return "Gemm"; |
| 37 | } |
| 38 | |
| 39 | const char* name_str() const |
| 40 | { |
| 41 | return "gemm"; |
| 42 | } |
| 43 | |
| 44 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 45 | { |
| 46 | int m = captured_params.at("m").i; |
| 47 | if (m == -1) |
| 48 | m = 0; |
| 49 | |
| 50 | op->params["2"] = 0; |
| 51 | op->params["3"] = 1; |
| 52 | op->params["4"] = 0; |
| 53 | op->params["5"] = 1; |
| 54 | op->params["6"] = 1; |
| 55 | op->params["7"] = m; |
| 56 | op->params["8"] = captured_params.at("out_features"); |
| 57 | op->params["9"] = captured_params.at("in_features"); |
| 58 | op->params["10"] = captured_params.at("bias").b ? 4 : -1; |
| 59 | |
| 60 | op->attrs["0"] = Attribute(); |
| 61 | op->attrs["0"].data = {0, 0, 0, 0}; |
| 62 | op->attrs["1"] = captured_attrs.at("op_0.weight"); |
| 63 | if (captured_params.at("bias").b) |
| 64 | { |
| 65 | op->attrs["2"] = Attribute(); |
| 66 | op->attrs["2"].data = {0, 0, 0, 0}; |
| 67 | op->attrs["3"] = captured_attrs.at("op_0.bias"); |
| 68 | } |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_0, 19) |
| 73 | |
| 74 | class nn_Linear_01 : public nn_Linear_0 |
| 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 #input=(%m,%in_features)f32 |
| 82 | nn.Linear op_0 1 1 input out in_features=%in_features out_features=%out_features bias=%bias |
| 83 | pnnx.Output output 1 0 out |
| 84 | )PNNXIR"; |
| 85 | } |
| 86 | |
| 87 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 88 | { |
| 89 | const int m = captured_params.at("m").i; |
| 90 | |
| 91 | if (m == 1) |
| 92 | return false; |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_01, 19) |
| 99 | |
| 100 | class nn_Linear_10 : public GraphRewriterPass |
| 101 | { |
| 102 | public: |
| 103 | const char* match_pattern_graph() const |
| 104 | { |
| 105 | return R"PNNXIR(7767517 |
| 106 | 4 3 |
| 107 | pnnx.Input input_0 0 1 input #input=(1,%m,%in_features)f32 |
| 108 | pnnx.Input input_1 0 1 bias |
| 109 | nn.Linear op_0 2 1 input bias out in_features=%in_features out_features=%out_features bias=False |
| 110 | pnnx.Output output 1 0 out |
| 111 | )PNNXIR"; |
| 112 | } |
| 113 | |
| 114 | const char* type_str() const |
| 115 | { |
| 116 | return "Gemm"; |
| 117 | } |
| 118 | |
| 119 | const char* name_str() const |
| 120 | { |
| 121 | return "gemm"; |
| 122 | } |
| 123 | |
| 124 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 125 | { |
| 126 | int m = captured_params.at("m").i; |
| 127 | if (m == -1) |
| 128 | m = 0; |
| 129 | |
| 130 | op->params["2"] = 0; |
| 131 | op->params["3"] = 1; |
| 132 | op->params["4"] = 0; |
| 133 | op->params["5"] = 1; |
| 134 | op->params["6"] = 0; |
| 135 | op->params["7"] = m; |
| 136 | op->params["8"] = captured_params.at("out_features"); |
| 137 | op->params["9"] = captured_params.at("in_features"); |
| 138 | op->params["10"] = 4; |
| 139 | |
| 140 | op->attrs["0"] = Attribute(); |
| 141 | op->attrs["0"].data = {0, 0, 0, 0}; |
| 142 | op->attrs["1"] = captured_attrs.at("op_0.weight"); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_10, 19) |
| 147 | |
| 148 | class nn_Linear_11 : public nn_Linear_10 |
| 149 | { |
| 150 | public: |
| 151 | const char* match_pattern_graph() const |
| 152 | { |
| 153 | return R"PNNXIR(7767517 |
| 154 | 4 3 |
| 155 | pnnx.Input input_0 0 1 input #input=(%m,%in_features)f32 |
| 156 | pnnx.Input input_1 0 1 bias |
| 157 | nn.Linear op_0 2 1 input bias out in_features=%in_features out_features=%out_features bias=False |
| 158 | pnnx.Output output 1 0 out |
| 159 | )PNNXIR"; |
| 160 | } |
| 161 | |
| 162 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 163 | { |
| 164 | const int m = captured_params.at("m").i; |
| 165 | |
| 166 | if (m == 1) |
| 167 | return false; |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_11, 19) |
| 174 | |
| 175 | class nn_Linear : public GraphRewriterPass |
| 176 | { |
| 177 | public: |
| 178 | const char* match_pattern_graph() const |
| 179 | { |
| 180 | return R"PNNXIR(7767517 |
| 181 | 3 2 |
| 182 | pnnx.Input input 0 1 input |
| 183 | nn.Linear op_0 1 1 input out in_features=%in_features out_features=%out_features bias=%bias |
| 184 | pnnx.Output output 1 0 out |
| 185 | )PNNXIR"; |
| 186 | } |
| 187 | |
| 188 | const char* type_str() const |
| 189 | { |
| 190 | return "InnerProduct"; |
| 191 | } |
| 192 | |
| 193 | const char* name_str() const |
| 194 | { |
| 195 | return "linear"; |
| 196 | } |
| 197 | |
| 198 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 199 | { |
| 200 | op->params["0"] = captured_params.at("out_features"); |
| 201 | op->params["1"] = captured_params.at("bias").b ? 1 : 0; |
| 202 | op->params["2"] = captured_attrs.at("op_0.weight").elemcount(); |
| 203 | |
| 204 | op->attrs["0"] = Attribute(); |
| 205 | op->attrs["0"].data = {0, 0, 0, 0}; |
| 206 | op->attrs["1"] = captured_attrs.at("op_0.weight"); |
| 207 | if (captured_params.at("bias").b) |
| 208 | op->attrs["2"] = captured_attrs.at("op_0.bias"); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear, 20) |
| 213 | |
| 214 | class nn_Linear_1 : public GraphRewriterPass |
| 215 | { |
| 216 | public: |
| 217 | const char* match_pattern_graph() const |
| 218 | { |
| 219 | return R"PNNXIR(7767517 |
| 220 | 4 3 |
| 221 | pnnx.Input input_0 0 1 input |
| 222 | pnnx.Input input_1 0 1 bias |
| 223 | nn.Linear op_0 2 1 input bias out in_features=%in_features out_features=%out_features bias=False |
| 224 | pnnx.Output output 1 0 out |
| 225 | )PNNXIR"; |
| 226 | } |
| 227 | |
| 228 | const char* replace_pattern_graph() const |
| 229 | { |
| 230 | return R"PNNXIR(7767517 |
| 231 | 5 4 |
| 232 | pnnx.Input input_0 0 1 input |
| 233 | pnnx.Input input_1 0 1 bias |
| 234 | InnerProduct linear 1 1 input a |
| 235 | BinaryOp bias 2 1 a bias out 0=0 |
| 236 | pnnx.Output output 1 0 out |
| 237 | )PNNXIR"; |
| 238 | } |
| 239 | |
| 240 | 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 |
| 241 | { |
| 242 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 243 | |
| 244 | const int batch_index = ops.at("linear")->inputs[0]->params["__batch_index"].i; |
| 245 | |
| 246 | ops.at("linear")->params["0"] = captured_params.at("out_features"); |
| 247 | ops.at("linear")->params["1"] = 0; |
| 248 | ops.at("linear")->params["2"] = captured_attrs.at("op_0.weight").elemcount(); |
| 249 | |
| 250 | ops.at("linear")->attrs["0"] = Attribute(); |
| 251 | ops.at("linear")->attrs["0"].data = {0, 0, 0, 0}; |
| 252 | ops.at("linear")->attrs["1"] = captured_attrs.at("op_0.weight"); |
| 253 | |
| 254 | ops.at("linear")->outputs[0]->params["__batch_index"] = batch_index; |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_1, 20) |
| 259 | |
| 260 | } // namespace ncnn |
| 261 | |
| 262 | } // namespace pnnx |