|
| 1 | +"""Phase M2: fused native gemv_4bit vs the Phase-M1 baseline (dequant -> F.linear). |
| 2 | +
|
| 3 | +Per shape, times: |
| 4 | + - fused torch.ops.bitsandbytes.gemv_4bit (routes to the fused Metal kernel) |
| 5 | + - baseline native dequant of B + F.linear (what gemv_4bit did before Phase M2) |
| 6 | +and reports ms/iter plus the fused kernel's effective read bandwidth |
| 7 | +(packed B + absmax + A, i.e. the memory the fused kernel actually touches). |
| 8 | +""" |
| 9 | + |
| 10 | +import time |
| 11 | + |
| 12 | +import torch |
| 13 | + |
| 14 | +from bitsandbytes.backends.mps import ops as mps_ops |
| 15 | +import bitsandbytes.functional as F |
| 16 | + |
| 17 | +DEV = "mps" |
| 18 | +ITERS = 50 |
| 19 | +WARMUP = 10 |
| 20 | + |
| 21 | +assert mps_ops._native_available(), "native MPS library required for this benchmark" |
| 22 | +assert hasattr(mps_ops._mps_native._lib, "bnb_mps_gemv_4bit"), "fused gemv kernel missing" |
| 23 | + |
| 24 | + |
| 25 | +def sync(): |
| 26 | + torch.mps.synchronize() |
| 27 | + |
| 28 | + |
| 29 | +def timed(fn): |
| 30 | + for _ in range(WARMUP): |
| 31 | + fn() |
| 32 | + sync() |
| 33 | + t0 = time.perf_counter() |
| 34 | + for _ in range(ITERS): |
| 35 | + fn() |
| 36 | + sync() |
| 37 | + return (time.perf_counter() - t0) / ITERS * 1e3 # ms/iter |
| 38 | + |
| 39 | + |
| 40 | +def bench(N, K, dtype, quant_type="nf4", blocksize=64): |
| 41 | + A = torch.randn(1, 1, K, dtype=dtype, device=DEV) |
| 42 | + B = torch.randn(N, K, dtype=dtype, device=DEV) |
| 43 | + B_q, absmax = torch.ops.bitsandbytes.quantize_4bit(B, blocksize, quant_type, torch.uint8) |
| 44 | + code = F.get_4bit_type(quant_type, device=DEV, blocksize=blocksize) |
| 45 | + |
| 46 | + def fused(): |
| 47 | + return torch.ops.bitsandbytes.gemv_4bit(A, B_q, B.shape, absmax, code, blocksize) |
| 48 | + |
| 49 | + def baseline(): |
| 50 | + B_dq = torch.ops.bitsandbytes.dequantize_4bit(B_q, absmax, blocksize, quant_type, list(B.shape), dtype) |
| 51 | + return torch.nn.functional.linear(A, B_dq) |
| 52 | + |
| 53 | + # Sanity: fused output matches the baseline it replaces. |
| 54 | + ref = baseline() |
| 55 | + got = fused() |
| 56 | + max_err = (got.float() - ref.float()).abs().max().item() |
| 57 | + |
| 58 | + t_fused = timed(fused) |
| 59 | + t_base = timed(baseline) |
| 60 | + |
| 61 | + # Memory the fused kernel reads: packed B (N*K/2 bytes) + absmax (N*K/blocksize fp32) |
| 62 | + # + A (K fp32); writes out (N fp32). |
| 63 | + bytes_moved = N * K // 2 + (N * K // blocksize) * 4 + K * 4 + N * 4 |
| 64 | + gbps = bytes_moved / (t_fused * 1e-3) / 1e9 |
| 65 | + |
| 66 | + print( |
| 67 | + f" N={N:>6} K={K:>6} {str(dtype).replace('torch.', ''):>8} " |
| 68 | + f"fused={t_fused:7.3f}ms baseline={t_base:7.3f}ms " |
| 69 | + f"speedup={t_base / t_fused:5.1f}x fused-read={gbps:6.1f} GB/s max|err|={max_err:.2e}" |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + print(f"iters={ITERS} warmup={WARMUP} device={DEV}\n") |
| 75 | + for dtype in (torch.float16, torch.bfloat16, torch.float32): |
| 76 | + print(f"=== gemv_4bit (M=1), {str(dtype).replace('torch.', '')} ===") |
| 77 | + for N, K in [(4096, 4096), (11008, 4096), (4096, 11008)]: |
| 78 | + bench(N, K, dtype) |
| 79 | + print() |
0 commit comments