| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 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 torch_roll : 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 |
| 29 | torch.roll op_0 1 1 input out dims=%dims shifts=%shifts |
| 30 | pnnx.Output output 1 0 out |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* replace_pattern_graph() const |
| 35 | { |
| 36 | return R"PNNXIR(7767517 |
| 37 | 4 3 |
| 38 | pnnx.Input input 0 1 input |
| 39 | Slice slice 1 2 input a b |
| 40 | Concat concat 2 1 b a out |
| 41 | pnnx.Output output 1 0 out |
| 42 | )PNNXIR"; |
| 43 | } |
| 44 | |
| 45 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 46 | { |
| 47 | if (captured_params.at("dims").type != 5) |
| 48 | return false; |
| 49 | |
| 50 | if (captured_params.at("dims").ai.size() != 1) |
| 51 | return false; |
| 52 | |
| 53 | if (captured_params.at("shifts").type != 5) |
| 54 | return false; |
| 55 | |
| 56 | if (captured_params.at("shifts").ai.size() != 1) |
| 57 | return false; |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | 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 |
| 63 | { |
| 64 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 65 | |
| 66 | const Operand* in = ops.at("slice")->inputs[0]; |
| 67 | |
| 68 | const int batch_index = in->params.at("__batch_index").i; |
| 69 | |
| 70 | int axis = captured_params.at("dims").ai[0]; |
| 71 | if (axis == batch_index) |
| 72 | { |
| 73 | fprintf(stderr, "roll along batch axis %d is not supported\n", batch_index); |
| 74 | } |
| 75 | |
| 76 | if (axis < 0) |
| 77 | { |
| 78 | int input_rank = in->shape.size(); |
| 79 | axis = input_rank + axis; |
| 80 | } |
| 81 | |
| 82 | if (axis > batch_index) |
| 83 | axis -= 1; |
| 84 | |
| 85 | ops.at("slice")->params["1"] = axis; |
| 86 | |
| 87 | ops.at("concat")->params["0"] = axis; |
| 88 | |
| 89 | const int shift = captured_params.at("shifts").ai[0]; |
| 90 | ops.at("slice")->params["2"] = std::vector<int>{-shift}; |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_roll, 20) |
| 95 | |
| 96 | class torch_roll_1 : public GraphRewriterPass |
| 97 | { |
| 98 | public: |
| 99 | const char* match_pattern_graph() const |
| 100 | { |
| 101 | return R"PNNXIR(7767517 |
| 102 | 3 2 |
| 103 | pnnx.Input input 0 1 input |
| 104 | torch.roll op_0 1 1 input out dims=%dims shifts=%shifts |
| 105 | pnnx.Output output 1 0 out |
| 106 | )PNNXIR"; |
| 107 | } |
| 108 | |
| 109 | const char* replace_pattern_graph() const |
| 110 | { |
| 111 | return R"PNNXIR(7767517 |
| 112 | 8 7 |
| 113 | pnnx.Input input 0 1 input |
| 114 | Slice slice 1 2 input a b |
| 115 | Slice slice_a 1 2 a a0 a1 |
| 116 | Slice slice_b 1 2 b b0 b1 |
| 117 | Concat concat_a 2 1 a1 a0 a10 |
| 118 | Concat concat_b 2 1 b1 b0 b10 |
| 119 | Concat concat 2 1 b10 a10 out |
| 120 | pnnx.Output output 1 0 out |
| 121 | )PNNXIR"; |
| 122 | } |
| 123 | |
| 124 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 125 | { |
| 126 | if (captured_params.at("dims").type != 5) |
| 127 | return false; |
| 128 | |
| 129 | if (captured_params.at("dims").ai.size() != 2) |
| 130 | return false; |
| 131 | |
| 132 | if (captured_params.at("shifts").type != 5) |
| 133 | return false; |
| 134 | |
| 135 | if (captured_params.at("shifts").ai.size() != 2) |
| 136 | return false; |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | 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 |
| 142 | { |
| 143 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 144 | |
| 145 | const Operand* in = ops.at("slice")->inputs[0]; |
| 146 | |
| 147 | const int batch_index = in->params.at("__batch_index").i; |
| 148 | |
| 149 | int axis0 = captured_params.at("dims").ai[0]; |
| 150 | int axis1 = captured_params.at("dims").ai[1]; |
| 151 | if (axis0 == batch_index || axis1 == batch_index) |
| 152 | { |
| 153 | fprintf(stderr, "roll along batch axis %d is not supported\n", batch_index); |
| 154 | } |
| 155 | |
| 156 | if (axis0 < 0) |
| 157 | { |
| 158 | int input_rank = in->shape.size(); |
| 159 | axis0 = input_rank + axis0; |
| 160 | } |
| 161 | |
| 162 | if (axis0 > batch_index) |
| 163 | axis0 -= 1; |
| 164 | |
| 165 | if (axis1 < 0) |
| 166 | { |
| 167 | int input_rank = in->shape.size(); |
| 168 | axis1 = input_rank + axis1; |
| 169 | } |
| 170 | if (axis1 > batch_index) |
| 171 | axis1 -= 1; |
| 172 | |
| 173 | ops.at("slice")->params["1"] = axis0; |
| 174 | ops.at("slice_a")->params["1"] = axis1; |
| 175 | ops.at("slice_b")->params["1"] = axis1; |
| 176 | |
| 177 | ops.at("concat_a")->params["0"] = axis1; |
| 178 | ops.at("concat_b")->params["0"] = axis1; |
| 179 | ops.at("concat")->params["0"] = axis0; |
| 180 | |
| 181 | const int shift0 = captured_params.at("shifts").ai[0]; |
| 182 | const int shift1 = captured_params.at("shifts").ai[1]; |
| 183 | ops.at("slice")->params["2"] = std::vector<int>{-shift0}; |
| 184 | ops.at("slice_a")->params["2"] = std::vector<int>{-shift1}; |
| 185 | ops.at("slice_b")->params["2"] = std::vector<int>{-shift1}; |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_roll_1, 20) |
| 190 | |
| 191 | } // namespace ncnn |
| 192 | |
| 193 | } // namespace pnnx |