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_level1.h"
16
17#include "../utils.h"
18
19namespace pnnx {
20
21class QuantizedLinear : public FuseModulePass
22{
23public:
24 const char* match_type_str() const
25 {
26 return "__torch__.torch.nn.quantized.modules.linear.Linear";
27 }
28
29 const char* type_str() const
30 {
31 return "nn.quantized.Linear";
32 }
33
34 void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const
35 {
36 // mod.dump(true, false, false);
37
38 // graph->dump();
39
40 const torch::jit::Node* quantized_linear = find_node_by_kind(graph, "quantized::linear");
41
42 // for (auto aa : quantized_linear->schema().arguments())
43 // {
44 // fprintf(stderr, "arg %s\n", aa.name().c_str());
45 // }
46
47 // torch::jit::Node* packed_params_node = 0;
48 // for (const auto& n : graph->nodes())
49 // {
50 // if (n->kind() == c10::prim::GetAttr && n->s(torch::jit::attr::name) == "_packed_params")
51 // {
52 // packed_params_node = n;
53 // break;
54 // }
55 // }
56
57 const auto& packed_params = mod.attr("_packed_params").toObject();
58
59 // for (auto aa : torch::jit::script::Object(packed_params).get_methods())
60 // {
61 // fprintf(stderr, "method %s\n", aa.name().c_str());
62 // }
63
64 auto x = torch::jit::script::Object(packed_params).run_method("_weight_bias").toTuple();
65
66 auto weight = x->elements()[0].toTensor();
67 auto bias = x->elements()[1].toTensor();
68
69 op->attrs["weight"] = weight;
70 op->attrs["bias"] = bias;
71
72 if (weight.qscheme() == c10::kPerChannelAffine)
73 {
74 op->attrs["weight.q_per_channel_scales"] = weight.q_per_channel_scales();
75 op->attrs["weight.q_per_channel_zero_points"] = weight.q_per_channel_zero_points();
76 // op->params["weight.q_per_channel_axis"] = weight.q_per_channel_axis();
77 }
78
79 op->params["in_features"] = weight.size(1);
80 op->params["out_features"] = weight.size(0);
81
82 op->params["scale"] = quantized_linear->namedInput("Y_scale_i");
83 op->params["zero_point"] = quantized_linear->namedInput("Y_zero_point_i");
84 }
85};
86
87REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(QuantizedLinear)
88
89class QuantizedLinearReLU : public FuseModulePass
90{
91public:
92 const char* match_type_str() const
93 {
94 return "__torch__.torch.nn.intrinsic.quantized.modules.linear_relu.LinearReLU";
95 }
96
97 const char* type_str() const
98 {
99 return "nn.intrinsic.quantized.LinearReLU";
100 }
101
102 void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const
103 {
104 // mod.dump(true, false, false);
105
106 graph->dump();
107
108 const torch::jit::Node* quantized_linear = find_node_by_kind(graph, "quantized::linear_relu");
109
110 // for (auto aa : quantized_linear->schema().arguments())
111 // {
112 // fprintf(stderr, "arg %s\n", aa.name().c_str());
113 // }
114
115 // torch::jit::Node* packed_params_node = 0;
116 // for (const auto& n : graph->nodes())
117 // {
118 // if (n->kind() == c10::prim::GetAttr && n->s(torch::jit::attr::name) == "_packed_params")
119 // {
120 // packed_params_node = n;
121 // break;
122 // }
123 // }
124
125 const auto& packed_params = mod.attr("_packed_params").toObject();
126
127 // for (auto aa : torch::jit::script::Object(packed_params).get_methods())
128 // {
129 // fprintf(stderr, "method %s\n", aa.name().c_str());
130 // }
131
132 auto x = torch::jit::script::Object(packed_params).run_method("_weight_bias").toTuple();
133
134 auto weight = x->elements()[0].toTensor();
135 auto bias = x->elements()[1].toTensor();
136
137 op->attrs["weight"] = weight;
138 op->attrs["bias"] = bias;
139
140 if (weight.qscheme() == c10::kPerChannelAffine)
141 {
142 op->attrs["weight.q_per_channel_scales"] = weight.q_per_channel_scales();
143 op->attrs["weight.q_per_channel_zero_points"] = weight.q_per_channel_zero_points();
144 // op->params["weight.q_per_channel_axis"] = weight.q_per_channel_axis();
145 }
146
147 op->params["in_features"] = weight.size(1);
148 op->params["out_features"] = weight.size(0);
149
150 op->params["scale"] = quantized_linear->namedInput("Y_scale_i");
151 op->params["zero_point"] = quantized_linear->namedInput("Y_zero_point_i");
152 }
153};
154
155REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(QuantizedLinearReLU)
156
157} // namespace pnnx