|
| 1 | +"""Phase M1 baseline + Phase M3 comparison for gemm_4bit. |
| 2 | +
|
| 3 | +M1 question: as M grows, does the GEMM cost overtake dequant? (Answer: crossover near |
| 4 | +M~512; below it gemm is dequant-bound.) |
| 5 | +
|
| 6 | +M3 question: what does the native path (chunked dequant -> scratch -> MPSMatrixMultiplication |
| 7 | +-> bias, ONE command buffer / ONE sync) buy over the dequant + F.linear fallback (native |
| 8 | +dequant wait + torch GEMM + second sync)? `gemm_4bit` routes native automatically when built, |
| 9 | +so `native` here is just the op; `fallback` reproduces the old tail verbatim. |
| 10 | +""" |
| 11 | + |
| 12 | +import time |
| 13 | + |
| 14 | +import torch |
| 15 | + |
| 16 | +import bitsandbytes.backends.mps.ops as mps_ops |
| 17 | +import bitsandbytes.functional as F |
| 18 | + |
| 19 | +DEV = "mps" |
| 20 | +ITERS = 30 |
| 21 | +WARMUP = 8 |
| 22 | + |
| 23 | + |
| 24 | +def sync(): |
| 25 | + torch.mps.synchronize() |
| 26 | + |
| 27 | + |
| 28 | +def timed(fn): |
| 29 | + for _ in range(WARMUP): |
| 30 | + fn() |
| 31 | + sync() |
| 32 | + t0 = time.perf_counter() |
| 33 | + for _ in range(ITERS): |
| 34 | + fn() |
| 35 | + sync() |
| 36 | + return (time.perf_counter() - t0) / ITERS * 1e3 |
| 37 | + |
| 38 | + |
| 39 | +def bench(M, N, K, dtype, quant_type="nf4", blocksize=64): |
| 40 | + A = torch.randn(1, M, K, dtype=dtype, device=DEV) |
| 41 | + B = torch.randn(N, K, dtype=dtype, device=DEV) |
| 42 | + B_q, qs = F.quantize_4bit(B, blocksize=blocksize, quant_type=quant_type) |
| 43 | + |
| 44 | + def native(): |
| 45 | + # Routes through bnb_mps_gemm_4bit when the native library is built (fp32/fp16). |
| 46 | + return torch.ops.bitsandbytes.gemm_4bit(A, B_q, list(B.shape), qs.absmax, blocksize, quant_type) |
| 47 | + |
| 48 | + def dequant_only(): |
| 49 | + return torch.ops.bitsandbytes.dequantize_4bit(B_q, qs.absmax, blocksize, quant_type, list(B.shape), dtype) |
| 50 | + |
| 51 | + def fallback(): |
| 52 | + # The pre-M3 tail: native dequant (its own sync) + torch F.linear (torch's queue). |
| 53 | + B_dq = mps_ops._dequantize_4bit_impl(B_q, qs.absmax, blocksize, quant_type, list(B.shape), dtype) |
| 54 | + return torch.nn.functional.linear(A, B_dq) |
| 55 | + |
| 56 | + B_dq = dequant_only() |
| 57 | + |
| 58 | + def linear_only(): |
| 59 | + return torch.nn.functional.linear(A, B_dq) |
| 60 | + |
| 61 | + t_nat, t_fb, t_deq, t_lin = timed(native), timed(fallback), timed(dequant_only), timed(linear_only) |
| 62 | + print( |
| 63 | + f" M={M:>4} N={N:>5} K={K:>5} {str(dtype).replace('torch.', ''):>8} " |
| 64 | + f"native={t_nat:7.3f}ms fallback={t_fb:7.3f}ms ({t_fb / t_nat:4.2f}x) " |
| 65 | + f"[fallback = dequant {t_deq:6.3f} + linear {t_lin:6.3f}]" |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + native = "native" if mps_ops._native_available() else "FALLBACK-ONLY (no native build)" |
| 71 | + print(f"iters={ITERS} warmup={WARMUP} device={DEV} lib={native}\n") |
| 72 | + for dtype in (torch.float16, torch.float32): |
| 73 | + print(f"=== gemm_4bit, N=K=4096, {str(dtype).replace('torch.', '')} ===") |
| 74 | + for M in (8, 64, 512, 2048): |
| 75 | + bench(M, 4096, 4096, dtype) |
| 76 | + print() |
0 commit comments