Elffuss BraidHub

kikocis/ncnn public

master · 4.9 KiBRawPermalink
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
17namespace pnnx {
18
19class Tensor_slice : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
257 6
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 dim
28pnnx.Input input_2 0 1 start
29pnnx.Input input_3 0 1 end
30pnnx.Input input_4 0 1 step
31aten::slice op_0 5 1 input dim start end step out
32pnnx.Output output 1 0 out
33)PNNXIR";
34 }
35
36 const char* type_str() const
37 {
38 return "Tensor.slice";
39 }
40};
41
42REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_slice, 20)
43
44class Tensor_slice_onnx : public GraphRewriterPass
45{
46public:
47 const char* match_pattern_graph() const
48 {
49 return R"PNNXIR(7767517
503 2
51pnnx.Input input 0 1 input
52Slice op_0 1 1 input out axes=%axes starts=%starts ends=%ends
53pnnx.Output output 1 0 out
54)PNNXIR";
55 }
56
57 const char* type_str() const
58 {
59 return "Tensor.slice";
60 }
61
62 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
63 {
64 if (captured_params.at("axes").type == 2)
65 {
66 op->params["dim"] = captured_params.at("axes");
67 op->params["start"] = captured_params.at("starts");
68 op->params["end"] = captured_params.at("ends");
69 op->params["step"] = 1;
70 }
71 else // if (captured_params.at("axes").type == 5)
72 {
73 const std::vector<int>& axes = captured_params.at("axes").ai;
74 const std::vector<int>& starts = captured_params.at("starts").ai;
75 const std::vector<int>& ends = captured_params.at("ends").ai;
76
77 if (axes.size() == 1)
78 {
79 op->params["dim"] = axes[0];
80 op->params["start"] = starts[0];
81 op->params["end"] = ends[0];
82 op->params["step"] = 1;
83 }
84 else
85 {
86 op->params["dims"] = axes;
87 op->params["starts"] = starts;
88 op->params["ends"] = ends;
89 op->params["steps"] = std::vector<int>(axes.size(), 1);
90 op->params["selects"] = std::vector<int>(axes.size(), INT_MAX);
91 }
92 }
93 }
94};
95
96REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_slice_onnx, 20)
97
98class Tensor_slice_onnx_1 : public GraphRewriterPass
99{
100public:
101 const char* match_pattern_graph() const
102 {
103 return R"PNNXIR(7767517
1043 2
105pnnx.Input input 0 1 input
106Slice op_0 1 1 input out axes=%axes starts=%starts ends=%ends steps=%steps
107pnnx.Output output 1 0 out
108)PNNXIR";
109 }
110
111 const char* type_str() const
112 {
113 return "Tensor.slice";
114 }
115
116 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
117 {
118 if (captured_params.at("axes").type == 2)
119 {
120 op->params["dim"] = captured_params.at("axes");
121 op->params["start"] = captured_params.at("starts");
122 op->params["end"] = captured_params.at("ends");
123 op->params["step"] = captured_params.at("steps");
124 }
125 else // if (captured_params.at("axes").type == 5)
126 {
127 const std::vector<int>& axes = captured_params.at("axes").ai;
128 const std::vector<int>& starts = captured_params.at("starts").ai;
129 const std::vector<int>& ends = captured_params.at("ends").ai;
130 const std::vector<int>& steps = captured_params.at("steps").ai;
131
132 if (axes.size() == 1)
133 {
134 op->params["dim"] = axes[0];
135 op->params["start"] = starts[0];
136 op->params["end"] = ends[0];
137 op->params["step"] = steps[0];
138 }
139 else
140 {
141 op->params["dims"] = axes;
142 op->params["starts"] = starts;
143 op->params["ends"] = ends;
144 op->params["steps"] = steps;
145 op->params["selects"] = std::vector<int>(axes.size(), INT_MAX);
146 }
147 }
148 }
149};
150
151REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_slice_onnx_1, 20)
152
153} // namespace pnnx