| 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 torch_clone : 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 1 input |
| 27 | prim::Constant op_0 0 1 memory_format value=%memory_format |
| 28 | aten::clone op_1 2 1 input memory_format out |
| 29 | pnnx.Output output 1 0 out |
| 30 | )PNNXIR"; |
| 31 | } |
| 32 | |
| 33 | const char* type_str() const |
| 34 | { |
| 35 | return "torch.clone"; |
| 36 | } |
| 37 | |
| 38 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 39 | { |
| 40 | if (captured_params.at("memory_format").i == 0) |
| 41 | op->params["memory_format"] = "torch.contiguous_format"; |
| 42 | if (captured_params.at("memory_format").i == 1) |
| 43 | op->params["memory_format"] = "torch.preserve_format"; |
| 44 | if (captured_params.at("memory_format").i == 2) |
| 45 | op->params["memory_format"] = "torch.channels_last"; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_clone, 20) |
| 50 | |
| 51 | class torch_clone_1 : public GraphRewriterPass |
| 52 | { |
| 53 | public: |
| 54 | const char* match_pattern_graph() const |
| 55 | { |
| 56 | return R"PNNXIR(7767517 |
| 57 | 3 2 |
| 58 | pnnx.Input input 0 1 input |
| 59 | aten::clone op_1 1 1 input out memory_format=%memory_format |
| 60 | pnnx.Output output 1 0 out |
| 61 | )PNNXIR"; |
| 62 | } |
| 63 | |
| 64 | const char* type_str() const |
| 65 | { |
| 66 | return "torch.clone"; |
| 67 | } |
| 68 | |
| 69 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 70 | { |
| 71 | if (captured_params.at("memory_format").type == 4 && captured_params.at("memory_format").s.empty()) |
| 72 | { |
| 73 | op->params["memory_format"] = "torch.contiguous_format"; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | if (captured_params.at("memory_format").i == 0) |
| 78 | op->params["memory_format"] = "torch.contiguous_format"; |
| 79 | if (captured_params.at("memory_format").i == 1) |
| 80 | op->params["memory_format"] = "torch.preserve_format"; |
| 81 | if (captured_params.at("memory_format").i == 2) |
| 82 | op->params["memory_format"] = "torch.channels_last"; |
| 83 | } |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_clone_1, 20) |
| 88 | |
| 89 | } // namespace pnnx |