Elffuss BraidHub

kikocis/ncnn public

master · 2.5 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 "fuse_slice_squeeze_to_select.h"
16
17#include "pass_level2.h"
18
19namespace pnnx {
20
21class fuse_slice_squeeze_to_select_pass : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
274 3
28pnnx.Input input 0 1 input
29Tensor.slice op_0 1 1 input a dim=%dim start=%start end=%end step=1
30torch.squeeze op_1 1 1 a out dim=%squeeze_dim
31pnnx.Output output 1 0 out
32)PNNXIR";
33 }
34
35 const char* type_str() const
36 {
37 return "Tensor.select";
38 }
39
40 const char* name_str() const
41 {
42 return "select";
43 }
44
45 bool match(const std::map<std::string, Parameter>& captured_params) const
46 {
47 const int start = captured_params.at("start").i;
48 const int end = captured_params.at("end").i;
49 if (end != start + 1)
50 return false;
51
52 const int dim = captured_params.at("dim").i;
53 int squeeze_dim;
54 if (captured_params.at("squeeze_dim").type == 2)
55 {
56 squeeze_dim = captured_params.at("squeeze_dim").i;
57 }
58 else // if (captured_params.at("squeeze_dim").type == 5)
59 {
60 if (captured_params.at("squeeze_dim").ai.size() != 1)
61 return false;
62
63 squeeze_dim = captured_params.at("squeeze_dim").ai[0];
64 }
65 if (squeeze_dim != dim)
66 return false;
67
68 return true;
69 }
70
71 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
72 {
73 op->params["dim"] = captured_params.at("dim");
74 op->params["index"] = captured_params.at("start");
75 }
76};
77
78void fuse_slice_squeeze_to_select(Graph& graph)
79{
80 fuse_slice_squeeze_to_select_pass a;
81 int opindex = 0;
82
83 pnnx_graph_rewrite(graph, &a, opindex);
84}
85
86} // namespace pnnx