Elffuss BraidHub

kikocis/ncnn public

master · 3.4 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_reshape : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
254 3
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 shape
28aten::reshape op_0 2 1 input shape out
29pnnx.Output output 1 0 out
30)PNNXIR";
31 }
32
33 const char* type_str() const
34 {
35 return "Tensor.reshape";
36 }
37};
38
39REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_reshape, 20)
40
41class Tensor_reshape_onnx : public GraphRewriterPass
42{
43public:
44 const char* match_pattern_graph() const
45 {
46 return R"PNNXIR(7767517
475 4
48pnnx.Input input_0 0 1 input
49pnnx.Input input_1 0 1 shape
50aten::cat op_0 1 1 shape cat dim=0
51Reshape op_1 2 1 input cat out %*=%*
52pnnx.Output output 1 0 out
53)PNNXIR";
54 }
55
56 const char* type_str() const
57 {
58 return "Tensor.reshape";
59 }
60
61 void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const
62 {
63 }
64};
65
66REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_reshape_onnx, 19)
67
68class Tensor_reshape_onnx_1 : public Tensor_reshape_onnx
69{
70public:
71 const char* match_pattern_graph() const
72 {
73 return R"PNNXIR(7767517
744 3
75pnnx.Input input_0 0 1 input
76pnnx.Input input_1 0 1 shape
77Reshape op_0 2 1 input shape out %*=%*
78pnnx.Output output 1 0 out
79)PNNXIR";
80 }
81};
82
83REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_reshape_onnx_1, 20)
84
85class Tensor_reshape_onnx_2 : public GraphRewriterPass
86{
87public:
88 const char* match_pattern_graph() const
89 {
90 return R"PNNXIR(7767517
913 2
92pnnx.Input input 0 1 input
93Reshape op_0 1 1 input out %*=%*
94pnnx.Output output 1 0 out
95)PNNXIR";
96 }
97
98 const char* type_str() const
99 {
100 return "Tensor.reshape";
101 }
102
103 bool match(const std::map<std::string, Parameter>& captured_params) const
104 {
105 if (captured_params.find("op_0.shape") == captured_params.end())
106 return false;
107
108 return true;
109 }
110
111 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
112 {
113 if (captured_params.at("op_0.shape").type == 5)
114 {
115 op->params["shape"] = captured_params.at("op_0.shape");
116 }
117 else // if (captured_params.at("op_0.shape").type == 2)
118 {
119 op->params["shape"] = std::vector<int>{captured_params.at("op_0.shape").i};
120 }
121 }
122};
123
124REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_reshape_onnx_2, 20)
125
126} // namespace pnnx