Dequant INT4 weights to input dtype and call F.linear.
(x, qdata, scale, zero, group_size)
| 54 | |
| 55 | |
| 56 | def _dequant_matmul(x, qdata, scale, zero, group_size): |
| 57 | """Dequant INT4 weights to input dtype and call F.linear.""" |
| 58 | N, K_half = qdata.shape |
| 59 | K = K_half * 2 |
| 60 | n_groups = K // group_size |
| 61 | gs_half = group_size // 2 |
| 62 | dtype = x.dtype |
| 63 | |
| 64 | p = qdata.to(torch.uint8).reshape(N, n_groups, gs_half) |
| 65 | low = (p & 0x0F).to(dtype) |
| 66 | high = ((p >> 4) & 0x0F).to(dtype) |
| 67 | data = torch.stack([low, high], dim=-1).reshape(N, n_groups, group_size) |
| 68 | |
| 69 | s = scale.to(dtype).t().unsqueeze(-1) |
| 70 | z = zero.to(dtype).t().unsqueeze(-1) |
| 71 | w_deq = ((data - z) * s).reshape(N, K) |
| 72 | |
| 73 | return F.linear(x, w_deq) |
| 74 | |
| 75 | |
| 76 | # --------------------------------------------------------------------------- |