| 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_prelu : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 4 3 |
| 26 | pnnx.Input input_0 0 1 input |
| 27 | pnnx.Input input_1 0 1 weight |
| 28 | aten::prelu op_0 2 1 input weight out |
| 29 | pnnx.Output output 1 0 out |
| 30 | )PNNXIR"; |
| 31 | } |
| 32 | |
| 33 | const char* type_str() const |
| 34 | { |
| 35 | return "F.prelu"; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu, 10) |
| 40 | |
| 41 | class F_prelu_onnx : public GraphRewriterPass |
| 42 | { |
| 43 | public: |
| 44 | const char* match_pattern_graph() const |
| 45 | { |
| 46 | // clang-format off |
| 47 | // *INDENT-OFF* |
| 48 | |
| 49 | return R"PNNXIR(7767517 |
| 50 | 4 3 |
| 51 | pnnx.Input input_0 0 1 input |
| 52 | pnnx.Input input_1 0 1 weight #weight=(?)f32 |
| 53 | PRelu op_0 2 1 input weight out |
| 54 | pnnx.Output output 1 0 out |
| 55 | )PNNXIR"; |
| 56 | |
| 57 | // *INDENT-ON* |
| 58 | // clang-format on |
| 59 | } |
| 60 | |
| 61 | const char* type_str() const |
| 62 | { |
| 63 | return "F.prelu"; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx, 10) |
| 68 | |
| 69 | class F_prelu_onnx_1 : public GraphRewriterPass |
| 70 | { |
| 71 | public: |
| 72 | const char* match_pattern_graph() const |
| 73 | { |
| 74 | return R"PNNXIR(7767517 |
| 75 | 4 3 |
| 76 | pnnx.Input input 0 1 input |
| 77 | pnnx.Attribute slope 0 1 weight @data |
| 78 | PRelu op_0 2 1 input weight out |
| 79 | pnnx.Output output 1 0 out |
| 80 | )PNNXIR"; |
| 81 | } |
| 82 | |
| 83 | const char* replace_pattern_graph() const |
| 84 | { |
| 85 | return R"PNNXIR(7767517 |
| 86 | 4 3 |
| 87 | pnnx.Input input 0 1 input |
| 88 | pnnx.Attribute slope 0 1 weight @data=%slope.data |
| 89 | F.prelu prelu 2 1 input weight out |
| 90 | pnnx.Output output 1 0 out |
| 91 | )PNNXIR"; |
| 92 | } |
| 93 | |
| 94 | 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 |
| 95 | { |
| 96 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 97 | |
| 98 | Operator* op_slope = ops.at("slope"); |
| 99 | |
| 100 | // hack slope shape |
| 101 | int num_slope = op_slope->attrs["data"].shape[0]; |
| 102 | op_slope->attrs["data"].shape = {num_slope}; |
| 103 | |
| 104 | op_slope->outputs[0]->shape = {num_slope}; |
| 105 | op_slope->outputs[0]->type = op_slope->attrs["data"].type; |
| 106 | |
| 107 | Operator* op_prelu = ops.at("prelu"); |
| 108 | op_prelu->inputnames = {"input", "weight"}; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx_1, 10) |
| 113 | |
| 114 | class F_prelu_onnx_2 : public GraphRewriterPass |
| 115 | { |
| 116 | public: |
| 117 | const char* match_pattern_graph() const |
| 118 | { |
| 119 | // clang-format off |
| 120 | // *INDENT-OFF* |
| 121 | |
| 122 | return R"PNNXIR(7767517 |
| 123 | 5 4 |
| 124 | pnnx.Input input_0 0 1 input |
| 125 | pnnx.Input input_1 0 1 weight #weight=(?)f32 |
| 126 | Unsqueeze uqz 1 1 weight w2 axes=%axes |
| 127 | PRelu op_0 2 1 input w2 out |
| 128 | pnnx.Output output 1 0 out |
| 129 | )PNNXIR"; |
| 130 | |
| 131 | // *INDENT-ON* |
| 132 | // clang-format on |
| 133 | } |
| 134 | |
| 135 | const char* type_str() const |
| 136 | { |
| 137 | return "F.prelu"; |
| 138 | } |
| 139 | |
| 140 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 141 | { |
| 142 | if (captured_params.at("axes").type == 5) |
| 143 | { |
| 144 | // 1 2 ... N |
| 145 | const std::vector<int>& axes = captured_params.at("axes").ai; |
| 146 | for (int i = 0; i < (int)axes.size(); i++) |
| 147 | { |
| 148 | if (axes[i] != i + 1) |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | int axes = captured_params.at("axes").i; |
| 155 | if (axes != 1) |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 163 | { |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx_2, 10) |
| 168 | |
| 169 | } // namespace pnnx |