| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // 2022 Xiaomi Corp. (author: Fangjun Kuang) |
| 5 | // |
| 6 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 7 | // in compliance with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // https://opensource.org/licenses/BSD-3-Clause |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software distributed |
| 12 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 13 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 14 | // specific language governing permissions and limitations under the License. |
| 15 | |
| 16 | #include "pass_ncnn.h" |
| 17 | |
| 18 | namespace pnnx { |
| 19 | |
| 20 | namespace ncnn { |
| 21 | |
| 22 | class F_glu : public GraphRewriterPass |
| 23 | { |
| 24 | public: |
| 25 | const char* match_pattern_graph() const |
| 26 | { |
| 27 | return R"PNNXIR(7767517 |
| 28 | 3 2 |
| 29 | pnnx.Input input 0 1 input |
| 30 | F.glu op_0 1 1 input out dim=%dim |
| 31 | pnnx.Output output 1 0 out |
| 32 | )PNNXIR"; |
| 33 | } |
| 34 | |
| 35 | const char* type_str() const |
| 36 | { |
| 37 | return "GLU"; |
| 38 | } |
| 39 | |
| 40 | const char* name_str() const |
| 41 | { |
| 42 | return "glu"; |
| 43 | } |
| 44 | |
| 45 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 46 | { |
| 47 | const int batch_index = op->inputs[0]->params["__batch_index"].i; |
| 48 | |
| 49 | int axis = captured_params.at("dim").i; |
| 50 | if (axis == batch_index) |
| 51 | { |
| 52 | fprintf(stderr, "glu along batch axis %d is not supported\n", batch_index); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (axis < 0) |
| 57 | { |
| 58 | int input_rank = op->inputs[0]->shape.size(); |
| 59 | axis = input_rank + axis; |
| 60 | } |
| 61 | |
| 62 | if (axis > batch_index) |
| 63 | axis -= 1; |
| 64 | |
| 65 | op->params["0"] = axis; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_glu, 20) |
| 70 | |
| 71 | } // namespace ncnn |
| 72 | |
| 73 | } // namespace pnnx |