MCPcopy Create free account
hub / github.com/OpenMOSS/MOSS / pack

Method pack

models/quantization.py:315–363  ·  view source on GitHub ↗
(self, linear, scales, zeros, g_idx=None)

Source from the content-addressed store, hash-verified

313 self.bias = None
314
315 def pack(self, linear, scales, zeros, g_idx=None):
316 self.g_idx = g_idx.clone() if g_idx is not None else self.g_idx
317
318 scales = scales.t().contiguous()
319 zeros = zeros.t().contiguous()
320 scale_zeros = zeros * scales
321 self.scales = scales.clone().half()
322 if linear.bias is not None:
323 self.bias = linear.bias.clone().half()
324
325 intweight = []
326 for idx in range(self.infeatures):
327 intweight.append(torch.round(
328 (linear.weight.data[:, idx] + scale_zeros[self.g_idx[idx]]) / self.scales[self.g_idx[idx]]).to(
329 torch.int)[:, None])
330 intweight = torch.cat(intweight, dim=1)
331 intweight = intweight.t().contiguous()
332 intweight = intweight.numpy().astype(np.uint32)
333 qweight = np.zeros((intweight.shape[0] // 32 * self.bits, intweight.shape[1]), dtype=np.uint32)
334 i = 0
335 row = 0
336 while row < qweight.shape[0]:
337 if self.bits in [2, 4, 8]:
338 for j in range(i, i + (32 // self.bits)):
339 qweight[row] |= intweight[j] << (self.bits * (j - i))
340 i += 32 // self.bits
341 row += 1
342 else:
343 raise NotImplementedError("Only 2,4,8 bits are supported.")
344
345 qweight = qweight.astype(np.int32)
346 self.qweight = torch.from_numpy(qweight)
347
348 zeros -= 1
349 zeros = zeros.numpy().astype(np.uint32)
350 qzeros = np.zeros((zeros.shape[0], zeros.shape[1] // 32 * self.bits), dtype=np.uint32)
351 i = 0
352 col = 0
353 while col < qzeros.shape[1]:
354 if self.bits in [2, 4, 8]:
355 for j in range(i, i + (32 // self.bits)):
356 qzeros[:, col] |= zeros[:, j] << (self.bits * (j - i))
357 i += 32 // self.bits
358 col += 1
359 else:
360 raise NotImplementedError("Only 2,4,8 bits are supported.")
361
362 qzeros = qzeros.astype(np.int32)
363 self.qzeros = torch.from_numpy(qzeros)
364
365 def forward(self, x):
366 out_shape = x.shape[:-1] + (self.outfeatures,)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected