MCPcopy Create free account
hub / github.com/OpenBitSys/BitDistiller / SteInt3AsymQuantizer

Class SteInt3AsymQuantizer

quantization/quantizer.py:119–151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

117
118
119class SteInt3AsymQuantizer(nn.Module):
120 def __init__(self, q_group_size=128):
121 super().__init__()
122 self.q_group_size = q_group_size
123 self.bit = 3
124 def forward(self, x):
125 org_w_shape = x.shape
126
127 if self.q_group_size > 0:
128 assert org_w_shape[-1] % self.q_group_size == 0
129 x = x.reshape(-1, self.q_group_size)
130 elif self.q_group_size == -1:
131 assert org_w_shape[-1] % self.q_group_size == 0
132 x = x.reshape(-1, x.shape[-1])
133 assert x.dim() == 2
134
135 max_val = x.amax(dim=1, keepdim=True)
136 min_val = x.amin(dim=1, keepdim=True)
137 max_int = 2 ** self.bit - 1
138 min_int = 0
139 scales = (max_val - min_val).clamp(min=1e-5) / max_int
140 zeros = (-torch.round(min_val / scales)).clamp_(min_int, max_int)
141
142 assert torch.isnan(scales).sum() == 0
143 assert torch.isnan(x).sum() == 0
144
145 x = (torch.clamp(Round.apply(x / scales) +
146 zeros, min_int, max_int) - zeros) * scales
147 assert torch.isnan(x).sum() == 0
148
149 x = x.reshape(org_w_shape)
150
151 return x
152
153class SteInt2AsymQuantizer(nn.Module):
154 def __init__(self, q_group_size=64):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected