Elffuss BraidHub

kikocis/ncnn public

master · 5.2 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2023 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
17namespace pnnx {
18namespace ncnn {
19
20class F_grid_sample : public GraphRewriterPass
21{
22public:
23 const char* match_pattern_graph() const
24 {
25 return R"PNNXIR(7767517
264 3
27pnnx.Input input_0 0 1 input0
28pnnx.Input input_1 0 1 input1
29F.grid_sample op_0 2 1 input0 input1 out mode=%mode padding_mode=%padding_mode align_corners=%align_corners
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "GridSample";
37 }
38
39 const char* name_str() const
40 {
41 return "gridsample";
42 }
43
44 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
45 {
46 const std::string& mode = captured_params.at("mode").s;
47 if (mode == "bilinear")
48 op->params["0"] = 1;
49 if (mode == "nearest")
50 op->params["0"] = 2;
51 if (mode == "bicubic")
52 op->params["0"] = 3;
53
54 const std::string& padding_mode = captured_params.at("padding_mode").s;
55 if (padding_mode == "zeros")
56 op->params["1"] = 1;
57 if (padding_mode == "border")
58 op->params["1"] = 2;
59 if (padding_mode == "reflection")
60 op->params["1"] = 3;
61
62 op->params["2"] = captured_params.at("align_corners").b ? 1 : 0;
63 op->params["3"] = 0;
64 }
65};
66
67REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_grid_sample, 20)
68
69class F_grid_sample_1 : public GraphRewriterPass
70{
71public:
72 const char* match_pattern_graph() const
73 {
74 return R"PNNXIR(7767517
755 4
76pnnx.Input input_a 0 1 a
77pnnx.Input input_b 0 1 b
78Tensor.permute op_0 1 1 b b1 dims=%dims
79F.grid_sample op_1 2 1 a b1 out mode=%mode padding_mode=%padding_mode align_corners=%align_corners
80pnnx.Output output 1 0 out
81)PNNXIR";
82 }
83
84 const char* type_str() const
85 {
86 return "GridSample";
87 }
88
89 const char* name_str() const
90 {
91 return "permutegridsample";
92 }
93
94 bool match(const std::map<std::string, Parameter>& captured_params) const
95 {
96 const std::vector<int>& dims = captured_params.at("dims").ai;
97
98 if ((dims == std::vector<int>{1, 2, 0}) || (dims == std::vector<int>{1, 2, 3, 0}))
99 return true;
100 if ((dims == std::vector<int>{0, 2, 3, 1}) || (dims == std::vector<int>{0, 2, 3, 4, 1}))
101 return true;
102 return false;
103 }
104
105 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
106 {
107 const std::string& mode = captured_params.at("mode").s;
108 if (mode == "bilinear")
109 op->params["0"] = 1;
110 if (mode == "nearest")
111 op->params["0"] = 2;
112 if (mode == "bicubic")
113 op->params["0"] = 3;
114
115 const std::string& padding_mode = captured_params.at("padding_mode").s;
116 if (padding_mode == "zeros")
117 op->params["1"] = 1;
118 if (padding_mode == "border")
119 op->params["1"] = 2;
120 if (padding_mode == "reflection")
121 op->params["1"] = 3;
122
123 op->params["2"] = captured_params.at("align_corners").b ? 1 : 0;
124
125 const int batch_index = op->inputs[1]->params["__batch_index"].i;
126
127 const std::vector<int>& dims = captured_params.at("dims").ai;
128
129 int input_rank = (int)op->inputs[0]->shape.size();
130
131 if (input_rank == 0)
132 {
133 // assume input is fine
134 input_rank = (int)dims.size();
135 }
136
137 if (batch_index >= 0 && batch_index < input_rank)
138 input_rank -= 1;
139
140 if (input_rank > 4)
141 {
142 fprintf(stderr, "permute %d-rank tensor is not supported yet!\n", input_rank);
143 return;
144 }
145
146 // drop permute batch index
147 std::vector<int> new_dims;
148 for (int i = 0; i < (int)dims.size(); i++)
149 {
150 if (dims[i] == batch_index)
151 continue;
152
153 int new_dim = dims[i] > batch_index ? dims[i] - 1 : dims[i];
154 new_dims.push_back(new_dim);
155 }
156
157 if (input_rank != (int)new_dims.size())
158 {
159 fprintf(stderr, "permute %d-rank tensor with %d-rank dims is not possible\n", input_rank, (int)new_dims.size());
160 return;
161 }
162
163 if ((input_rank == 3 && new_dims == std::vector<int>{1, 2, 0}) || (input_rank == 4 && new_dims == std::vector<int>{1, 2, 3, 0}))
164 op->params["3"] = 1;
165 }
166};
167
168REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_grid_sample_1, 19)
169
170} // namespace ncnn
171
172} // namespace pnnx