| 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_ncnn.h" |
| 16 | |
| 17 | #include "pass_ncnn/convert_attribute.h" |
| 18 | #include "pass_ncnn/convert_custom_op.h" |
| 19 | #include "pass_ncnn/convert_module_op.h" |
| 20 | #include "pass_ncnn/convert_half_to_float.h" |
| 21 | #include "pass_ncnn/convert_input.h" |
| 22 | #include "pass_ncnn/convert_torch_cat.h" |
| 23 | #include "pass_ncnn/convert_torch_chunk.h" |
| 24 | #include "pass_ncnn/convert_torch_einsum.h" |
| 25 | #include "pass_ncnn/convert_torch_split.h" |
| 26 | #include "pass_ncnn/convert_torch_stack.h" |
| 27 | #include "pass_ncnn/convert_torch_tensor_split.h" |
| 28 | #include "pass_ncnn/convert_torch_unbind.h" |
| 29 | #include "pass_ncnn/convert_Tensor_select.h" |
| 30 | #include "pass_ncnn/convert_Tensor_slice.h" |
| 31 | #include "pass_ncnn/convert_Tensor_slice_copy.h" |
| 32 | #include "pass_ncnn/eliminate_output.h" |
| 33 | #include "pass_ncnn/expand_expression.h" |
| 34 | #include "pass_ncnn/fuse_convert_shufflechannel_slice.h" |
| 35 | #include "pass_ncnn/insert_split.h" |
| 36 | #include "pass_ncnn/chain_multi_output.h" |
| 37 | #include "pass_ncnn/solve_batch_index.h" |
| 38 | |
| 39 | #include "pass_ncnn/eliminate_noop.h" |
| 40 | #include "pass_ncnn/eliminate_tail_reshape_permute.h" |
| 41 | #include "pass_ncnn/fuse_convolution_activation.h" |
| 42 | #include "pass_ncnn/fuse_convolution1d_activation.h" |
| 43 | #include "pass_ncnn/fuse_convolutiondepthwise_activation.h" |
| 44 | #include "pass_ncnn/fuse_convolutiondepthwise1d_activation.h" |
| 45 | #include "pass_ncnn/fuse_deconvolution_activation.h" |
| 46 | #include "pass_ncnn/fuse_deconvolutiondepthwise_activation.h" |
| 47 | #include "pass_ncnn/fuse_innerproduct_activation.h" |
| 48 | #include "pass_ncnn/fuse_transpose_matmul.h" |
| 49 | #include "pass_ncnn/fuse_binaryop_eltwise.h" |
| 50 | #include "pass_ncnn/insert_reshape_numpy_binaryop_broadcast.h" |
| 51 | #include "pass_ncnn/insert_reshape_linear.h" |
| 52 | #include "pass_ncnn/insert_reshape_pooling.h" |
| 53 | #include "pass_ncnn/insert_reshape_global_pooling.h" |
| 54 | |
| 55 | #include "pass_level4/dead_code_elimination.h" |
| 56 | #include "pass_level4/canonicalize.h" |
| 57 | #include "pass_level5/eliminate_maxpool_indices.h" |
| 58 | #include "pass_level5/unroll_rnn_op.h" |
| 59 | |
| 60 | namespace pnnx { |
| 61 | |
| 62 | static std::map<int, std::vector<const GraphRewriterPass*> > g_global_pnnx_ncnn_graph_rewriter_passes; |
| 63 | |
| 64 | NcnnGraphRewriterPassRegister::NcnnGraphRewriterPassRegister(const GraphRewriterPass* _pass, int priority) |
| 65 | : pass(_pass) |
| 66 | { |
| 67 | if (g_global_pnnx_ncnn_graph_rewriter_passes.find(priority) == g_global_pnnx_ncnn_graph_rewriter_passes.end()) |
| 68 | { |
| 69 | g_global_pnnx_ncnn_graph_rewriter_passes[priority] = std::vector<const GraphRewriterPass*>(); |
| 70 | } |
| 71 | |
| 72 | g_global_pnnx_ncnn_graph_rewriter_passes[priority].push_back(pass); |
| 73 | } |
| 74 | |
| 75 | NcnnGraphRewriterPassRegister::~NcnnGraphRewriterPassRegister() |
| 76 | { |
| 77 | delete pass; |
| 78 | } |
| 79 | |
| 80 | void pass_ncnn(Graph& g, const std::vector<std::string>& module_operators) |
| 81 | { |
| 82 | unroll_rnn_op(g); |
| 83 | |
| 84 | eliminate_maxpool_indices(g); |
| 85 | |
| 86 | ncnn::expand_expression(g); |
| 87 | |
| 88 | ncnn::chain_multi_output(g); |
| 89 | |
| 90 | ncnn::solve_batch_index(g); |
| 91 | |
| 92 | ncnn::convert_half_to_float(g); |
| 93 | |
| 94 | ncnn::insert_reshape_numpy_binaryop_broadcast(g); |
| 95 | ncnn::insert_reshape_pooling(g); |
| 96 | ncnn::insert_reshape_linear(g); |
| 97 | ncnn::insert_reshape_global_pooling(g); |
| 98 | |
| 99 | ncnn::fuse_convert_shufflechannel_slice(g); |
| 100 | |
| 101 | ncnn::convert_torch_cat(g); |
| 102 | ncnn::convert_torch_chunk(g); |
| 103 | ncnn::convert_torch_stack(g); |
| 104 | ncnn::convert_torch_split(g); |
| 105 | ncnn::convert_torch_unbind(g); |
| 106 | ncnn::convert_torch_tensor_split(g); |
| 107 | ncnn::convert_torch_einsum(g); |
| 108 | |
| 109 | ncnn::convert_Tensor_select(g); |
| 110 | ncnn::convert_Tensor_slice(g); |
| 111 | ncnn::convert_Tensor_slice_copy(g); |
| 112 | |
| 113 | // slice -> crop + reshape |
| 114 | // slice_copy -> reshape + copyto |
| 115 | |
| 116 | int opindex = 0; |
| 117 | for (auto x : g_global_pnnx_ncnn_graph_rewriter_passes) |
| 118 | { |
| 119 | for (auto rewriter : x.second) |
| 120 | { |
| 121 | pnnx_graph_rewrite(g, rewriter, opindex); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | ncnn::eliminate_noop(g); |
| 126 | |
| 127 | ncnn::insert_split(g); |
| 128 | |
| 129 | ncnn::fuse_transpose_matmul(g); |
| 130 | ncnn::fuse_binaryop_eltwise(g); |
| 131 | ncnn::fuse_convolution_activation(g); |
| 132 | ncnn::fuse_convolution1d_activation(g); |
| 133 | ncnn::fuse_convolutiondepthwise_activation(g); |
| 134 | ncnn::fuse_convolutiondepthwise1d_activation(g); |
| 135 | ncnn::fuse_deconvolution_activation(g); |
| 136 | ncnn::fuse_deconvolutiondepthwise_activation(g); |
| 137 | ncnn::fuse_innerproduct_activation(g); |
| 138 | ncnn::eliminate_tail_reshape_permute(g); |
| 139 | |
| 140 | dead_code_elimination(g); |
| 141 | |
| 142 | canonicalize(g); |
| 143 | |
| 144 | ncnn::convert_custom_op(g); |
| 145 | ncnn::convert_module_op(g, module_operators); |
| 146 | |
| 147 | ncnn::convert_attribute(g); |
| 148 | |
| 149 | ncnn::convert_input(g); |
| 150 | |
| 151 | ncnn::eliminate_output(g); |
| 152 | } |
| 153 | |
| 154 | } // namespace pnnx |