Elffuss BraidHub

kikocis/ncnn public

master · 5.1 KiBRawPermalink
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
17namespace pnnx {
18
19class torch_min : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
255 5
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 dim
28prim::Constant op_0 0 1 keepdim value=%keepdim
29aten::min op_1 3 2 input dim keepdim out indices
30pnnx.Output output 2 0 out indices
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "torch.min";
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
52REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_min, 20)
53
54class torch_min_1 : public GraphRewriterPass
55{
56public:
57 const char* match_pattern_graph() const
58 {
59 return R"PNNXIR(7767517
603 2
61pnnx.Input input_0 0 1 input
62aten::min op_1 1 1 input out
63pnnx.Output output 1 0 out
64)PNNXIR";
65 }
66
67 const char* type_str() const
68 {
69 return "torch.min";
70 }
71};
72
73REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_min_1, 20)
74
75class torch_min_onnx : public GraphRewriterPass
76{
77public:
78 const char* match_pattern_graph() const
79 {
80 return R"PNNXIR(7767517
813 2
82pnnx.Input input 0 1 input
83ReduceMin op_0 1 1 input out %*=%*
84pnnx.Output output 1 0 out
85)PNNXIR";
86 }
87
88 const char* type_str() const
89 {
90 return "torch.min";
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
126REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_min_onnx, 20)
127
128class torch_min_onnx_1 : public GraphRewriterPass
129{
130public:
131 const char* match_pattern_graph() const
132 {
133 return R"PNNXIR(7767517
1344 3
135pnnx.Input input 0 1 input
136ReduceMin op_0 1 1 input out %*=%*
137ArgMin op_1 1 1 input indices %*=%*
138pnnx.Output output 2 0 out indices
139)PNNXIR";
140 }
141
142 const char* type_str() const
143 {
144 return "torch.min";
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
183REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_min_onnx_1, 19)
184
185} // namespace pnnx