1: 
2: import torch
3: 
4: 
5: def quant_sym(x: torch.tensor, scaling: torch.tensor, nbits: int):
6:     q_max, q_min = 2 ** (nbits - 1) - 1, -2 ** (nbits - 1)
7:     return torch.round(x / scaling.unsqueeze(1)).clip(q_min, q_max).to(torch.int8)
8: 
9: def dequant_sym(x: torch.tensor, scaling: torch.tensor, target_dtype: torch.dtype):
10:     return x * scaling.unsqueeze(1).to(target_dtype)
11: 
12: def quant_asym(x: torch.tensor, scaling: torch.tensor, zeros: torch.tensor, nbits: int):
13:     q_max, q_min = 2 ** (nbits - 1) - 1, -2 ** (nbits - 1)
14:     return (torch.round(x / scaling.unsqueeze(1) - zeros.unsqueeze(1))).clip(q_min, q_max).to(torch.int8)
15:     
16: def dequant_asym(x: torch.tensor, scaling: torch.tensor, zeros: torch.tensor, target_dtype: torch.dtype):
17:     return (x + zeros.unsqueeze(1)) * scaling.unsqueeze(1).to(target_dtype)
18: 
19: 
20: class VanillaQuantizeMeta:
21:     def __init__(self, nbits, asym, compute_dtype):
22:         self.nbits = nbits
23:         # self.group_size = group_size
24:         # self.axis = axis # 1 for per-channel, 0 for per-token
25:         self.asym = asym
26:         self.compute_dtype = compute_dtype
27: 
28:     
29: class VanillaQuantizedTensor:
30:     def __init__(self, tensor, scaling, zeros, original_shape, axis, meta: VanillaQuantizeMeta):
31:         self.tensor = tensor
32:         self.scaling = scaling
33:         self.zeros = zeros
34:         self.original_shape = original_shape
35:         self.axis = axis
36:         self.meta = meta
37: 
38:     def dequantize(self):
39:         if self.meta.asym:
40:             dequant = dequant_asym(self.tensor, self.scaling, self.zeros, self.meta.compute_dtype)
41:         else:
42:             dequant = dequant_sym(self.tensor, self.scaling, self.meta.compute_dtype)
43:         dequant = dequant.view(self.original_shape)
44:         if self.axis == 1:
45:             max_dim = len(self.original_shape) - 1
46:             dequant = dequant.transpose(max_dim - 1, max_dim)
47:         return dequant
48: 
49: class VanillaQuantizer:
50:     def __init__(self, nbits, asym, compute_dtype):
51:         self.meta = VanillaQuantizeMeta(nbits, asym, compute_dtype)
52:     
53:     def quantize(self, tensor, q_group_size, axis):
54:         if axis == 1:
55:             max_dim = len(tensor.shape) - 1
56:             tensor = tensor.transpose(max_dim - 1, max_dim)
57:         if q_group_size == -1:
58:             assert axis == 0 # must be per-token
59:             q_group_size = tensor.shape[-1] # take the last dimension
60:         rs = tensor.reshape(-1, q_group_size)
61:         
62:         q_max, q_min = 2 ** (self.meta.nbits - 1) - 1, -2 ** (self.meta.nbits - 1)
63:         
64:         if self.meta.asym:
65:             _max, _min = rs.max(dim=1).values, rs.min(dim=1).values
66:             scale = (_max - _min).clamp(min=1e-5).div(q_max - q_min)
67:             zeros = (_min / scale).round() - q_min
68:             quant = quant_asym(rs, scale, zeros, self.meta.nbits)
69:         else:
70:             scale = rs.abs().max(dim=1).values.clamp(min=1e-5).div(q_max)
71:             zeros = None
72:             quant = quant_sym(rs, scale, self.meta.nbits)
73:         
74:         return VanillaQuantizedTensor(quant, scale, zeros, tensor.shape, axis, self.meta)
