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

Method forward

quantization/quantizer.py:189–222  ·  view source on GitHub ↗
(self, x)

Source from the content-addressed store, hash-verified

187 self.q_group_size = q_group_size
188
189 def forward(self, x):
190 org_w_shape = x.shape
191
192 # reshape to groupsize
193 if self.q_group_size > 0:
194 assert org_w_shape[-1] % self.q_group_size == 0
195 qx = x.reshape(-1, self.q_group_size)
196 elif self.q_group_size == -1:
197 qx = x.reshape(-1, x.shape[-1])
198 assert qx.dim() == 2
199
200 # Get the Min Max
201 max_val = qx.amax(dim=1, keepdim=True)
202 min_val = qx.amin(dim=1, keepdim=True)
203
204
205 scale_pos = torch.abs(max_val)
206 scale_neg = torch.abs(min_val)
207
208 dev = qx.device
209 x_pos = torch.zeros_like(qx)
210 x_neg = torch.zeros_like(qx)
211 x_pos = torch.where(qx >= 0, qx, x_pos)
212 x_neg = torch.where(qx < 0, qx, x_neg)
213 q_pos = x_pos / scale_pos
214 q_neg = x_neg / scale_neg
215
216 q_pos, q_neg = self.round_pass(q_pos, q_neg, dev)
217
218 qx = q_pos * scale_pos + q_neg * scale_neg
219
220 qx = qx.reshape(org_w_shape)
221
222 return qx
223
224 def round_n2f3(self, q_pos, q_neg, dev):
225 q_pos = torch.where(q_pos >= 0.8114928305149078, torch.tensor(1.0).to(dev), q_pos)

Callers

nothing calls this directly

Calls 1

round_passMethod · 0.95

Tested by

no test coverage detected