MCPcopy Create free account
hub / github.com/OpenGVLab/EfficientQAT / QuantLinear

Class QuantLinear

quantize/int_linear_fake.py:10–54  ·  view source on GitHub ↗

Quantized Module that can perform quantized convolution or normal convolution. To activate quantization, please use set_quant_state function.

Source from the content-addressed store, hash-verified

8
9
10class QuantLinear(nn.Module):
11 """
12 Quantized Module that can perform quantized convolution or normal convolution.
13 To activate quantization, please use set_quant_state function.
14 """
15 def __init__(
16 self,
17 org_module: nn.Linear,
18 wbits=4,
19 group_size=64
20 ):
21 super().__init__()
22 self.fwd_kwargs = dict()
23 self.fwd_func = F.linear
24 self.register_parameter('weight',org_module.weight) # trainable
25 if org_module.bias is not None:
26 self.register_buffer('bias',org_module.bias)
27 else:
28 self.bias = None
29 self.in_features = org_module.in_features
30 self.out_features = org_module.out_features
31 # de-activate the quantized forward default
32 self.use_weight_quant = False
33 # initialize quantizer
34 self.weight_quantizer = UniformAffineQuantizer(wbits, group_size, weight=org_module.weight)
35 self.use_temporary_parameter = False
36
37
38
39 def forward(self, input: torch.Tensor):
40 if self.use_weight_quant:
41 weight = self.weight_quantizer(self.weight)
42 bias = self.bias
43 else:
44 weight = self.weight
45 bias = self.bias
46
47
48 out = self.fwd_func(input, weight, bias, **self.fwd_kwargs)
49
50
51 return out
52
53 def set_quant_state(self, weight_quant: bool = False):
54 self.use_weight_quant = weight_quant
55
56
57

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected