Skip to content

perf(b12x/moe): optimize W4A8 inline 1-bit CSF staging and register-direct decode - #191

Open
voipmonitor wants to merge 1 commit into
mainfrom
perf/csf-w4a8-opt
Open

voipmonitor wants to merge 1 commit into
mainfrom
perf/csf-w4a8-opt

Conversation

@voipmonitor

Copy link
Copy Markdown

Summary

Eliminates the decode regression and narrows prefill overhead for W4A8 MXFP4 inline 1-bit CSF (--expert-scale-compression csf, destination == 3, bits == 1) on SM120, making inline CSF 3–5% faster than native scales on decode (M=4..128) and at parity on 16K prefill while preserving 100% bitwise lossless parity (max_err = 0.00000) and the full -3.41 GiB/GPU / +24% KV cache savings.

Key Changes

  1. Zero-STS cp.async staging + register-direct decode (b12x/_lib/quant/csf_inline.py):
    • Replace SMEM-buffered warp expansion (STS + sync_threads + LDS) with stage_compact_1bit (256B-aligned slot: cp.async 1-bit codes + sparse exception positions/values up to 32/group, overlapping bounds_smem LDS.64 ahead of code staging) and register-direct decode_compact_1bit_words / decode_compact_1bit_pair_words (PRMT + IMAD + LOP3 + single zero-exception skip branch + shared _patch_compact_1bit_exceptions staged + >32 overflow fallback).
    • Cooperative 32/64-thread prefetch_column_bounds_smem + packed invariant lane row bases (load_lane_bases_packed / load_lane_pair_bases_packed) hoisted outside K-loops.
  2. Compact W4A8 decode kernels (w4a8_compact_projection.py):
    • 256B-aligned CSF slots eliminate shared-memory bank conflicts (MicroProjection: 88 regs vs 96 native; CoalescedFC1: 118 vs 124; CoalescedFC2: 96 regs, o128=5, STACK:0).
  3. Materialized Phase1 & Phase2 prefill/dynamic kernels (w4a8_phase1.py, w4a8_phase2.py):
    • Phase1: unified 128-thread gate+up bounds prefetch piggybacked on existing tok_smem sync_threads(), unified 64-thread gate+up producer, register-direct interleaved pair decode (REG:255, STACK:0, LOCAL:0), no evict_first hint on reused prefill scale blocks.
    • Phase2: pack column bounds inside the unused half of the SFB buffer (zero extra SMEM, 1024B multi-group alignment), piggyback bounds prefetch on existing mbarrier_init sync_threads() (REG:254, STACK:0, LOCAL:0).
  4. Autotune & tests (_tuning.py, tests/experimental/b12x/moe/test_mxfp4_csf.py):
    • Bump TUNING.candidate_contract_version (74 -> 75) so cached plans re-race csf_inline_scales=(False, True).
    • Add test_w4a8_inline_compact_1bit_exception_overflow_parity verifying bitwise parity (torch.equal) with >32 exceptions/group (up to ~350/group), n64_tail (nr=64, nc=2), odd topk (fused_groups=2), and poisoned scale scratch.

Verification (SM120, RTX PRO 6000 Blackwell)

1. GPU Microbenchmarks — Real DeepSeek-V4.1-Flash Layer-10 Weights (h=5120, n=576, E=384, topk=6, L2-flushed CUDA graphs, max_err = 0.00000)

Shape / Config native Pre-opt csf_inl This PR csf_inl Delta vs native
M=1 micro:64 41.0 us 49.2 us (+20.0%) 43.0 us +5.00%
M=4 micro:64 69.6 us — 67.6 us -2.99% faster
M=8 micro:64 126.9 us — 122.9 us -3.20% faster
M=16..64 micro:64 219–586 us — 209–561 us -3.9% to -4.7% faster
M=128 micro:64 823.3 us — 782.3 us -4.98% faster
M=128 dynamic:16 849.9 us — 815.1 us -4.09% faster
M=4096 dynamic:128 1,354.7 us — 1,355.7 us +0.07% (Phase2 -6.0 us)
M=8192 dynamic:128 2,222.0 us — 2,244.1 us +0.99% (Phase2 -10.2 us)

2. End-to-End Same-GPU A/B — DeepSeek-V4.1-Flash 16K TP4 C32 (4x RTX PRO 6000, GPUs 4–7)

  • VRAM / KV Cache: 80.10 -> 76.69 GiB/GPU (-3.41 GiB/GPU), KV capacity 17.03M -> 21.16M tokens (+24.2%).
  • qualify_release_16k:
    • C=1 step latency: 5.734 -> 5.489 ms (-4.27% faster, vs +2.87% pre-opt).
    • by_shape step latency: B1xW8 -3.73%, B2xW8 -0.62%, B4xW8 -1.91%, B6xW8 -8.96%, B8xW8 -1.63%, B12xW8 -10.04%, B24xW8 -1.02%.
    • Balanced decode: C24_P256 +2.02%, C32_P256 +4.05% (1,581.7 vs 1,520.1 tok/s), C32_P2048 +0.72% (req +3.15%, step -1.29%).
    • Cold NIAH prefill (8K–128K, 10/10 needles): 8K -0.31%, 16K +0.00%, 32K +0.05%, 64K +0.21%, 128K +0.20% GPU / +1.40% wall (vs -1.3% to -2.0% pre-opt).
  • llm_decode_bench (max_tokens=8192, 30s):
    • Sustained decode wins 6/6 cells: ctx=0 C1 +3.11% / C8 +1.00% / C32 +2.42% (3,018.3 vs 2,946.9 tok/s); ctx=32k C1 +3.54% / C8 +1.97% / C32 +1.12%.
    • Standalone prefill 8K–128K: -1.2% to -1.6%.

3. Tests

  • pytest tests/experimental/b12x/moe/test_mxfp4_csf.py (including new test_w4a8_inline_compact_1bit_exception_overflow_parity): PASS
  • pytest tests/experimental/b12x/moe/test_w4a8_compact_coalesced.py: 74/74 PASS
  • pytest tests/experimental/b12x/quantization/test_mxfp4_csf.py tests/experimental/b12x/quantization/test_csf_warp_rows.py: PASS
  • ruff check & git diff --check: clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant