Elffuss BraidHub

kikocis/ncnn public

master · 8.1 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_level1.h"
16
17#include "../utils.h"
18
19namespace pnnx {
20
21class QuantizedConv2d : public FuseModulePass
22{
23public:
24 const char* match_type_str() const
25 {
26 return "__torch__.torch.nn.quantized.modules.conv.Conv2d";
27 }
28
29 const char* type_str() const
30 {
31 return "nn.quantized.Conv2d";
32 }
33
34 void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const
35 {
36 // graph->dump();
37
38 const torch::jit::Node* quantized_convolution = find_node_by_kind(graph, "quantized::conv2d");
39
40 // for (auto aa : quantized_convolution->schema().arguments())
41 // {
42 // fprintf(stderr, "arg %s\n", aa.name().c_str());
43 // }
44
45 // torch::jit::Node* packed_params_node = 0;
46 // for (const auto& n : graph->nodes())
47 // {
48 // if (n->kind() == c10::prim::GetAttr && n->s(torch::jit::attr::name) == "_packed_params")
49 // {
50 // packed_params_node = n;
51 // break;
52 // }
53 // }
54
55 // quantized_convolution->namedInput("output_scale");
56
57 const auto& packed_params = mod.attr("_packed_params").toObject();
58
59 // auto x = torch::jit::script::Object(packed_params).run_method("__getstate__");
60 auto x = torch::jit::script::Object(packed_params).run_method("unpack").toTuple();
61 // std::cout << x->elements()[0].toTensor() << std::endl;
62 // std::cout << x->elements()[0].toTensor().quantizer() << std::endl;
63 // std::cout << x->elements()[1] << std::endl;
64 // at::Tensor dequantize() const;
65 // double q_scale() const;
66 // int64_t q_zero_point() const;
67 // at::Tensor q_per_channel_scales() const;
68 // at::Tensor q_per_channel_zero_points() const;
69 // int64_t q_per_channel_axis() const;
70
71 // auto quantizer = x->elements()[0].toTensor().quantizer();
72
73 auto weight = x->elements()[0].toTensor();
74 auto bias = x->elements()[1].toTensor();
75
76 op->attrs["weight"] = weight;
77 op->attrs["bias"] = bias;
78
79 if (weight.qscheme() == c10::kPerChannelAffine)
80 {
81 op->attrs["weight.q_per_channel_scales"] = weight.q_per_channel_scales();
82 op->attrs["weight.q_per_channel_zero_points"] = weight.q_per_channel_zero_points();
83 // op->params["weight.q_per_channel_axis"] = weight.q_per_channel_axis();
84 }
85
86 op->params["in_channels"] = mod.attr("in_channels").toInt();
87 op->params["out_channels"] = mod.attr("out_channels").toInt();
88 op->params["kernel_size"] = Parameter{mod.attr("kernel_size").toTuple()->elements()[0].toInt(), mod.attr("kernel_size").toTuple()->elements()[1].toInt()};
89 op->params["stride"] = Parameter{mod.attr("stride").toTuple()->elements()[0].toInt(), mod.attr("stride").toTuple()->elements()[1].toInt()};
90 op->params["padding"] = Parameter{mod.attr("padding").toTuple()->elements()[0].toInt(), mod.attr("padding").toTuple()->elements()[1].toInt()};
91 op->params["dilation"] = Parameter{mod.attr("dilation").toTuple()->elements()[0].toInt(), mod.attr("dilation").toTuple()->elements()[1].toInt()};
92 op->params["groups"] = mod.attr("groups").toInt();
93 op->params["padding_mode"] = "zeros";
94 op->params["bias"] = mod.hasattr("bias");
95
96 op->params["scale"] = quantized_convolution->namedInput("output_scale");
97 op->params["zero_point"] = quantized_convolution->namedInput("output_zero_point");
98 }
99};
100
101REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(QuantizedConv2d)
102
103class QuantizedConvReLU2d : public FuseModulePass
104{
105public:
106 const char* match_type_str() const
107 {
108 return "__torch__.torch.nn.intrinsic.quantized.modules.conv_relu.ConvReLU2d";
109 }
110
111 const char* type_str() const
112 {
113 return "nn.intrinsic.quantized.ConvReLU2d";
114 }
115
116 void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const
117 {
118 // graph->dump();
119
120 const torch::jit::Node* quantized_convolution = find_node_by_kind(graph, "quantized::conv2d_relu");
121
122 // for (auto aa : quantized_convolution->schema().arguments())
123 // {
124 // fprintf(stderr, "arg %s\n", aa.name().c_str());
125 // }
126
127 // torch::jit::Node* packed_params_node = 0;
128 // for (const auto& n : graph->nodes())
129 // {
130 // if (n->kind() == c10::prim::GetAttr && n->s(torch::jit::attr::name) == "_packed_params")
131 // {
132 // packed_params_node = n;
133 // break;
134 // }
135 // }
136
137 // quantized_convolution->namedInput("output_scale");
138
139 const auto& packed_params = mod.attr("_packed_params").toObject();
140
141 // auto x = torch::jit::script::Object(packed_params).run_method("__getstate__");
142 auto x = torch::jit::script::Object(packed_params).run_method("unpack").toTuple();
143 // std::cout << x->elements()[0].toTensor() << std::endl;
144 // std::cout << x->elements()[0].toTensor().quantizer() << std::endl;
145 // std::cout << x->elements()[1] << std::endl;
146 // at::Tensor dequantize() const;
147 // double q_scale() const;
148 // int64_t q_zero_point() const;
149 // at::Tensor q_per_channel_scales() const;
150 // at::Tensor q_per_channel_zero_points() const;
151 // int64_t q_per_channel_axis() const;
152
153 // auto quantizer = x->elements()[0].toTensor().quantizer();
154
155 auto weight = x->elements()[0].toTensor();
156 auto bias = x->elements()[1].toTensor();
157
158 op->attrs["weight"] = weight;
159 op->attrs["bias"] = bias;
160
161 if (weight.qscheme() == c10::kPerChannelAffine)
162 {
163 op->attrs["weight.q_per_channel_scales"] = weight.q_per_channel_scales();
164 op->attrs["weight.q_per_channel_zero_points"] = weight.q_per_channel_zero_points();
165 // op->params["weight.q_per_channel_axis"] = weight.q_per_channel_axis();
166 }
167
168 op->params["in_channels"] = mod.attr("in_channels").toInt();
169 op->params["out_channels"] = mod.attr("out_channels").toInt();
170 op->params["kernel_size"] = Parameter{mod.attr("kernel_size").toTuple()->elements()[0].toInt(), mod.attr("kernel_size").toTuple()->elements()[1].toInt()};
171 op->params["stride"] = Parameter{mod.attr("stride").toTuple()->elements()[0].toInt(), mod.attr("stride").toTuple()->elements()[1].toInt()};
172 op->params["padding"] = Parameter{mod.attr("padding").toTuple()->elements()[0].toInt(), mod.attr("padding").toTuple()->elements()[1].toInt()};
173 op->params["dilation"] = Parameter{mod.attr("dilation").toTuple()->elements()[0].toInt(), mod.attr("dilation").toTuple()->elements()[1].toInt()};
174 op->params["groups"] = mod.attr("groups").toInt();
175 op->params["padding_mode"] = "zeros";
176 op->params["bias"] = mod.hasattr("bias");
177
178 op->params["scale"] = quantized_convolution->namedInput("output_scale");
179 op->params["zero_point"] = quantized_convolution->namedInput("output_zero_point");
180 }
181};
182
183REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(QuantizedConvReLU2d)
184
185} // namespace pnnx