| """SOTA reference for W4A16 GEMM. |
| |
| Library survey on RTX PRO 6000 Blackwell (SM120, CC 12.0): |
| |
| - Marlin (IST-DASLab): no SM120 kernels (Ampere/Hopper only). Skip. |
| - GPTQ-Triton (fpgaminer): unmaintained; pure Triton path works on SM120 |
| but is not faster than Marlin on its target HW |
| and has no Blackwell tuning. Skip as primary. |
| - AWQ (mit-han-lab/llm-awq): CUDA kernels not built for SM120 in the wheel. |
| Skip. |
| - bitsandbytes >= 0.49.2: CUDA kernels compile and run on SM120 (verified |
| on this machine). Different quant scheme (NF4, |
| symmetric, blocksize 64) than our reference's |
| AWQ-style asymmetric INT4 with group_size 128, |
| but it occupies the same memory regime and is |
| the only tuned W4A16-class kernel that runs on |
| SM120 today. Used here as an *informational* |
| SOTA line, not as a numerical reference. |
| |
| The benchmark calls `sota_forward(x, ref_model)` and times it; correctness is |
| NOT checked against this path (the quant scheme differs). |
| """ |
| from __future__ import annotations |
|
|
| import torch |
|
|
| _BNB_OK: bool | None = None |
|
|
|
|
| def is_available() -> bool: |
| global _BNB_OK |
| if _BNB_OK is not None: |
| return _BNB_OK |
| try: |
| import bitsandbytes |
| from bitsandbytes.functional import quantize_4bit |
| _BNB_OK = torch.cuda.is_available() |
| except Exception: |
| _BNB_OK = False |
| return _BNB_OK |
|
|
|
|
| _CACHE: dict[tuple[int, int, int], tuple] = {} |
|
|
|
|
| def _prepare(ref_model) -> tuple: |
| """Quantize the reference's bf16-equivalent weight with bnb NF4 once.""" |
| key = (ref_model.M, ref_model.N, ref_model.K) |
| if key in _CACHE: |
| return _CACHE[key] |
| from bitsandbytes.functional import quantize_4bit |
| |
| |
| |
| |
| K, N = ref_model.K, ref_model.N |
| w_packed = ref_model.w_q |
| scales = ref_model.scales |
| zeros = ref_model.zeros |
| g = ref_model.group_size |
|
|
| w_unpacked = torch.empty((K, N), dtype=torch.uint8, device=w_packed.device) |
| w_unpacked[0::2] = w_packed & 0xF |
| w_unpacked[1::2] = (w_packed >> 4) & 0xF |
| s_full = scales.repeat_interleave(g, dim=0) |
| z_full = zeros.repeat_interleave(g, dim=0) |
| w_bf = (w_unpacked.to(torch.bfloat16) - z_full) * s_full |
|
|
| |
| w_for_bnb = w_bf.t().contiguous() |
| qw, qstate = quantize_4bit(w_for_bnb, blocksize=64, quant_type="nf4") |
| _CACHE[key] = (qw, qstate, w_bf) |
| return _CACHE[key] |
|
|
|
|
| def sota_forward(x: torch.Tensor, ref_model) -> torch.Tensor: |
| """W4A16 GEMM via bitsandbytes NF4. x: (M, K) bf16, returns (M, N) bf16.""" |
| from bitsandbytes.functional import dequantize_4bit, gemv_4bit |
| qw, qstate, _ = _prepare(ref_model) |
| M = x.shape[0] |
| if M == 1: |
| |
| out = gemv_4bit(x.view(1, 1, -1).contiguous(), qw.t(), state=qstate) |
| return out.view(1, -1) |
| |
| w_deq = dequantize_4bit(qw, qstate, blocksize=64, quant_type="nf4") |
| return x @ w_deq.t() |
|
|