Elffuss BraidHub

kikocis/ncnn public

master · 5.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 "pass_level2.h"
16
17namespace pnnx {
18
19class torch_arange : 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 end
27prim::Constant op_0 0 1 dtype value=%dtype
28prim::Constant op_1 0 1 layout value=*
29prim::Constant op_2 0 1 device value=*
30prim::Constant op_3 0 1 requires_grad value=*
31aten::arange op_4 5 1 end dtype layout device requires_grad out
32pnnx.Output output 1 0 out
33)PNNXIR";
34 }
35
36 const char* type_str() const
37 {
38 return "torch.arange";
39 }
40
41 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
42 {
43 if (captured_params.at("dtype").type == 0)
44 {
45 op->params["dtype"] = Parameter();
46 }
47 else // if (captured_params.at("dtype").type == 2)
48 {
49 if (captured_params.at("dtype").i == 0) op->params["dtype"] = "torch.uint8";
50 if (captured_params.at("dtype").i == 1) op->params["dtype"] = "torch.int8";
51 if (captured_params.at("dtype").i == 2) op->params["dtype"] = "torch.short";
52 if (captured_params.at("dtype").i == 3) op->params["dtype"] = "torch.int";
53 if (captured_params.at("dtype").i == 4) op->params["dtype"] = "torch.long";
54 if (captured_params.at("dtype").i == 5) op->params["dtype"] = "torch.half";
55 if (captured_params.at("dtype").i == 6) op->params["dtype"] = "torch.float";
56 if (captured_params.at("dtype").i == 7) op->params["dtype"] = "torch.double";
57 if (captured_params.at("dtype").i == 8) op->params["dtype"] = "torch.complex32";
58 if (captured_params.at("dtype").i == 9) op->params["dtype"] = "torch.complex64";
59 if (captured_params.at("dtype").i == 10) op->params["dtype"] = "torch.complex128";
60 if (captured_params.at("dtype").i == 11) op->params["dtype"] = "torch.bool";
61 if (captured_params.at("dtype").i == 15) op->params["dtype"] = "torch.bfloat16";
62 }
63 }
64};
65
66REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_arange, 20)
67
68class torch_arange_1 : public torch_arange
69{
70public:
71 const char* match_pattern_graph() const
72 {
73 return R"PNNXIR(7767517
749 8
75pnnx.Input input_0 0 1 start
76pnnx.Input input_1 0 1 end
77pnnx.Input input_2 0 1 step
78prim::Constant op_0 0 1 dtype value=%dtype
79prim::Constant op_1 0 1 layout value=*
80prim::Constant op_2 0 1 device value=*
81prim::Constant op_3 0 1 requires_grad value=*
82aten::arange op_4 7 1 start end step dtype layout device requires_grad out
83pnnx.Output output 1 0 out
84)PNNXIR";
85 }
86};
87
88REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_arange_1, 20)
89
90class torch_arange_2 : public torch_arange
91{
92public:
93 const char* match_pattern_graph() const
94 {
95 return R"PNNXIR(7767517
968 7
97pnnx.Input input_0 0 1 start
98pnnx.Input input_1 0 1 end
99prim::Constant op_0 0 1 dtype value=%dtype
100prim::Constant op_1 0 1 layout value=*
101prim::Constant op_2 0 1 device value=*
102prim::Constant op_3 0 1 pin_memory value=*
103aten::arange op_4 6 1 start end dtype layout device pin_memory out
104pnnx.Output output 1 0 out
105)PNNXIR";
106 }
107};
108
109REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_arange_2, 20)
110
111class torch_arange_3 : public torch_arange
112{
113public:
114 const char* match_pattern_graph() const
115 {
116 return R"PNNXIR(7767517
1177 6
118pnnx.Input input_0 0 1 start
119pnnx.Input input_1 0 1 end
120prim::Constant op_0 0 1 dtype value=%dtype
121prim::Constant op_1 0 1 layout value=*
122prim::Constant op_2 0 1 pin_memory value=*
123aten::arange op_3 6 1 start end dtype layout layout pin_memory out
124pnnx.Output output 1 0 out
125)PNNXIR";
126 }
127};
128
129REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_arange_3, 20)
130
131class torch_arange_onnx : public GraphRewriterPass
132{
133public:
134 const char* match_pattern_graph() const
135 {
136 return R"PNNXIR(7767517
1375 4
138pnnx.Input input_0 0 1 start
139pnnx.Input input_1 0 1 end
140pnnx.Input input_2 0 1 step
141Range op_0 3 1 start end step out
142pnnx.Output output 1 0 out
143)PNNXIR";
144 }
145
146 const char* type_str() const
147 {
148 return "torch.arange";
149 }
150
151 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
152 {
153 op->params["dtype"] = Parameter();
154 }
155};
156
157REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_arange_onnx, 20)
158
159} // namespace pnnx