| 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 | // |
| 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 torch_max : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 5 5 |
| 26 | pnnx.Input input_0 0 1 input |
| 27 | pnnx.Input input_1 0 1 dim |
| 28 | prim::Constant op_0 0 1 keepdim value=%keepdim |
| 29 | aten::max op_1 3 2 input dim keepdim out indices |
| 30 | pnnx.Output output 2 0 out indices |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* type_str() const |
| 35 | { |
| 36 | return "torch.max"; |
| 37 | } |
| 38 | |
| 39 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 40 | { |
| 41 | GraphRewriterPass::write(op, captured_params); |
| 42 | |
| 43 | // drop indices if not used |
| 44 | if (op->outputs[1]->consumers.empty()) |
| 45 | { |
| 46 | op->outputs[1]->producer = 0; |
| 47 | op->outputs.resize(1); |
| 48 | } |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_max, 20) |
| 53 | |
| 54 | class torch_max_1 : public GraphRewriterPass |
| 55 | { |
| 56 | public: |
| 57 | const char* match_pattern_graph() const |
| 58 | { |
| 59 | return R"PNNXIR(7767517 |
| 60 | 3 2 |
| 61 | pnnx.Input input_0 0 1 input |
| 62 | aten::max op_1 1 1 input out |
| 63 | pnnx.Output output 1 0 out |
| 64 | )PNNXIR"; |
| 65 | } |
| 66 | |
| 67 | const char* type_str() const |
| 68 | { |
| 69 | return "torch.max"; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_max_1, 20) |
| 74 | |
| 75 | class torch_max_onnx : public GraphRewriterPass |
| 76 | { |
| 77 | public: |
| 78 | const char* match_pattern_graph() const |
| 79 | { |
| 80 | return R"PNNXIR(7767517 |
| 81 | 3 2 |
| 82 | pnnx.Input input 0 1 input |
| 83 | ReduceMax op_0 1 1 input out %*=%* |
| 84 | pnnx.Output output 1 0 out |
| 85 | )PNNXIR"; |
| 86 | } |
| 87 | |
| 88 | const char* type_str() const |
| 89 | { |
| 90 | return "torch.max"; |
| 91 | } |
| 92 | |
| 93 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 94 | { |
| 95 | if (captured_params.find("op_0.axes") != captured_params.end()) |
| 96 | { |
| 97 | if (captured_params.at("op_0.axes").type != 5 || captured_params.at("op_0.axes").ai.size() != 1) |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 105 | { |
| 106 | if (captured_params.find("op_0.axes") != captured_params.end()) |
| 107 | { |
| 108 | op->params["dim"] = captured_params.at("op_0.axes").ai[0]; |
| 109 | |
| 110 | if (captured_params.find("op_0.keepdims") != captured_params.end()) |
| 111 | { |
| 112 | op->params["keepdim"] = captured_params.at("op_0.keepdims").i ? true : false; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | op->params["keepdim"] = true; |
| 117 | } |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | // reduce all |
| 122 | } |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_max_onnx, 20) |
| 127 | |
| 128 | class torch_max_onnx_1 : public GraphRewriterPass |
| 129 | { |
| 130 | public: |
| 131 | const char* match_pattern_graph() const |
| 132 | { |
| 133 | return R"PNNXIR(7767517 |
| 134 | 4 3 |
| 135 | pnnx.Input input 0 1 input |
| 136 | ReduceMax op_0 1 1 input out %*=%* |
| 137 | ArgMax op_1 1 1 input indices %*=%* |
| 138 | pnnx.Output output 2 0 out indices |
| 139 | )PNNXIR"; |
| 140 | } |
| 141 | |
| 142 | const char* type_str() const |
| 143 | { |
| 144 | return "torch.max"; |
| 145 | } |
| 146 | |
| 147 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 148 | { |
| 149 | if (captured_params.find("op_0.axes") == captured_params.end()) |
| 150 | return false; |
| 151 | |
| 152 | if (captured_params.find("op_0.keepdims") == captured_params.end()) |
| 153 | return false; |
| 154 | |
| 155 | if (captured_params.find("op_1.axis") == captured_params.end()) |
| 156 | return false; |
| 157 | |
| 158 | if (captured_params.find("op_1.keepdims") == captured_params.end()) |
| 159 | return false; |
| 160 | |
| 161 | if (captured_params.at("op_0.axes").type != 5 || captured_params.at("op_0.axes").ai.size() != 1) |
| 162 | return false; |
| 163 | |
| 164 | if (captured_params.at("op_1.axis").type != 2) |
| 165 | return false; |
| 166 | |
| 167 | if (captured_params.at("op_0.axes").ai[0] != captured_params.at("op_1.axis").i) |
| 168 | return false; |
| 169 | |
| 170 | if (captured_params.at("op_0.keepdims").i != captured_params.at("op_1.keepdims").i) |
| 171 | return false; |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 177 | { |
| 178 | op->params["dim"] = captured_params.at("op_0.axes").ai[0]; |
| 179 | op->params["keepdim"] = captured_params.at("op_0.keepdims").i ? true : false; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_max_onnx_1, 19) |
| 184 | |
| 185 | } // namespace pnnx |