Repository navigation
perf(b12x/mla): SM120 register-recycled MLA prefill for DeepSeek-V4.1 and GLM-5.3 - #175
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
…loop base Signed-off-by: Brandon M. Music <266531604+brandonmmusic-max@users.noreply.github.com>
…taging
Optimize the SM120 GLM_NEXT ARBITRARY_FP32 FP8 multi-group prefill path
(UnifiedPrefillMGKernel) while retaining FP32 accumulation and exact
per-head, per-V-chunk dynamic FP16 weight scaling:
- Load 16x16 E4M3 V tiles directly via ldmatrix.m16n16.x1.trans.b8 with
matching K-order weight staging, shift-free 4-byte E4M3-to-FP16x2
unpacking, K16 and cross-V-chunk V-load pipelining, and contiguous
16-dim warp epilogue output indexing in s7_epilogue.
- Replace shared-memory atomic_max contention in s6_xv_nope_glm_next_mg2
with lock-free packed BF16x2 quad reduction into XOR-swizzled 32 B
per-slot w_head_sc scratch (99,456 B total SMEM <= 101,376 B carveout),
LDS.128 + HMNMX2.BF16_V2 warp reduction, and exponent-subtracted packed
BF16x2 scale broadcast.
- Fold sm_scale_log2 into Q block scales once during S0 quantization (when
vectorized S0 runs) to eliminate per-tile QK scale multiplies in S3, and
defer the 4-lane softmax row-sum butterfly reduction from each tile to
s4_finalize_row_sum_mg2.
Validation (600 W SM120 RTX PRO 6000, M=4096, H=64, D=512, W=2051,
L2-flushed CUDA graph, 44 slices):
- contiguous_2051: 4.199 -> 3.426 ms/slice (184.77 -> 150.76 ms, -34.01 ms)
- scattered_32K_2051: 4.324 -> 3.512 ms/slice (190.27 -> 154.54 ms, -35.73 ms)
- half_valid_1024: 2.189 -> 1.808 ms/slice (96.33 -> 79.57 ms, -16.76 ms)
- Parity: rel_l2 0.16-0.21% / cos >= 0.9999977 across H={8,16,24,32,64},
0.0006% / cos 1.0000000 on sink+1e6 outlier, 0.0 empty-row error,
bit-exact zero-alloc CUDA-graph replay, 89/89 targeted MLA pytest pass.
Signed-off-by: Brandon M. Music <266531604+brandonmmusic-max@users.noreply.github.com>
… BF16 prefill Optimize the SM120 DeepSeek-V4.1 (ModelType.DSV41, ComputeMode.BF16, ScaleFormat.NVFP4_E4M3) multi-group prefill path in UnifiedPrefillMGKernel while keeping exact lossless BF16 QK and BF16 PV arithmetic and bit-exact mg_n_hg=2 vs mg_n_hg=1 equivalence: - Expand DSV4.1 BF16 shared-memory staging in smem_mg.py to three 64x304 B indexed NVFP4 buffers (58,368 B; total 99,968 B <= 101,376 B carveout at mg_n_hg=2; 69,632 B dual-SWA / 3-indexed at mg_n_hg=1) with 10 dedicated mbarriers (SWA full/empty and 3-stage indexed full/empty). Replace blocking 384-thread BAR 0x1 barriers in the indexed loop (and mg_n_hg=1 SWA loop) with lock-free empty-mbarrier signaling and disjoint SWA-to-indexed tile-0 prefetch. - Recycle dead FP32 score registers (qk0_frag/qk1_frag) immediately after s4_exp_rescale_pack_dsv41_mg2 packs BF16 weights into sm_p. Run ks_pv=0..1 of PV(t) before waiting on tile t+1, then interleave ks_pv=2..3 (16 PV steps) 1-to-1 with 16 bank-conflict-free LDS.128 QK(t+1) pairs (32 QK k-steps) in s6_s1_pv_qk_interleaved_indexed_mg2. - Reorder s6_xv_dsv41_source_mg2 to outer-ks / inner-vc (cutting sm_p LDSM.x4 by 87.5%, 64 -> 8 at mg2), accumulate HMMA directly into acc0/acc1, hoist PV scales via LDS.128 with fused row-0/row-8 bf16x2 scale conversion, and vectorize QK K loads via LDS.128 with batch 4-scale unpacking. - Replace global index loads in DSV4.1 S3 with register-only warp-ballot masking from SMEM source tags, overlap MUFU.EX2 with accumulator rescaling, defer 4-lane quad row-sum reduction to s4_finalize_row_sum_mg2, and bump attention.mla.sm120.prefill_mg compile spec from v6 to v7. Validation (RTX PRO 6000 Blackwell Workstation SM120 @ 450 W, L2-flushed CUDA graph replay, vs parent 358c16d (pre-recycling baseline)): - swa128_idx512_h16_m4096 (mg1): 612.35 -> 477.22 us (1.283x, 180.0 TFLOPS) - swa128_idx512_h64_m1024 (mg2): 436.22 -> 346.11 us (1.260x, 248.2 TFLOPS) - swa128_idx512_h64_m2048 (mg2): 854.02 -> 679.92 us (1.256x, 252.7 TFLOPS) - swa128_idx512_h64_m4096 (mg2): 1689.28 -> 1345.54 us (1.255x, 255.4 TFLOPS) - swa128_idx2048_h64_m1024 (mg2): 1255.42 -> 990.22 us (1.268x, 294.9 TFLOPS) - swa128_idx2048_h64_m2048 (mg2): 2836.50 -> 1991.68 us (1.424x, 293.3 TFLOPS) - swa128_idx2048_h128_m1024 (mg2): 2819.58 -> 2063.87 us (1.366x, 283.0 TFLOPS) - swa128_only_h64_m4096 (mg2): 542.72 -> 487.42 us (1.113x, 141.0 TFLOPS) - Numerical parity: max_abs 0.000122-0.000488, rel_err 0.00218-0.00228, lse_max_abs <= 2e-6 (matching baseline to 6 decimals); 92/92 pass across test_compressed_sparse_mla_v41.py and test_benchmark_deepseek_v41_mla.py (including bit-exact mg2 == mg1), 5/5 DSV4.1 binding tests, 2/2 DSV4 unified prefill live-graph smoke tests. Signed-off-by: Brandon M. Music <266531604+brandonmmusic-max@users.noreply.github.com>
…efill Add a dedicated register-recycled FP8 prefill loop (_run_glm_next_fp8_pipelined) for the GLM_NEXT ARBITRARY_FP32 path (D_QK=512, D_V=512, 4 inline FP32 scales) in UnifiedPrefillMGKernel while preserving lossless FP8/E4M3 + FP32 scale numerics and leaving DSV4/DSV41_SOURCE/GLM_NSA paths unchanged: - Preload per-head Q block scales into registers once before the tile loop via 128-bit shared loads (eliminating 16 SMEM scale loads per tile in QK). - Prefetch per-lane candidate indices ahead of QK so global/L2 index latency overlaps with all FP8 QK MMAs, and carry hoisted inline K/V scales through bitwise NaN-safe S3 masking directly into XV-NoPE (zero V-scale reloads). - Pipeline ldmatrix across all 16 k32 steps of FP8 QK-NoPE. - Pre-pack Pair-1 FP16x2 weights into 8 Uint32 registers before Pair-0 PV on mg_n_hg=2 (reclaiming all 32 pv and 16 inv registers prior to PV and reducing the inter-pair barrier window to 8 STS.32 stores), and expand single-head-group (mg_n_hg=1) w_fp8 staging to 8 slots (88,704 B / 101,376 B SMEM) so all four V-chunks stage simultaneously (reducing math-loop BAR.SYNC from 5 to 3/tile). - Release empty_mbar on vc=3, k16=3 immediately after unpacking the final V words from kv_fp8, ahead of the final HMMAs and accumulator scale-accumulate FFMAs, and apply 240/24 math/IO dynamic register budgets (0 spills). - Retain attention.mla.sm120.prefill_mg KernelCompileSpec version 7 (introduced in the preceding commit). Validation on RTX PRO 6000 Blackwell (SM120): - SASS (cuobjdump/nvdisasm -plr): STACK=0 LOCAL=0 STL=0 LDL=0 across mg2 (SMEM 99,456 B, peak 200 live GPRs / 240, 5 loop BAR.SYNC) and mg1 (SMEM 88,704 B, peak 132 live GPRs / 240, 3 loop BAR.SYNC). - Parity: 12/12 GLM_NEXT shapes/edge cases (mg2 H64/H32, mg1 H16/H8, empty, 1-tile, all-invalid first tile, strided Q, outlier + NaN unselected) match baseline within <=4.88e-4 abs / <=2e-6 LSE and match FP32 reference; mg2 and mg1 CUDA-graph replay match eager output identically (0.0 diff). - Interleaved clock-matched (2610 MHz) L2-flushed CUDA-graph benchmarks: 1.003x-1.010x across all 9 mg2 shapes (M=1024/2048/4096, contiguous/scattered/ half-valid) and 1.000x at the ~4.0 TB/s SM120 L2-TMA ceiling on mg1 (H=16). Signed-off-by: Brandon M. Music <266531604+brandonmmusic-max@users.noreply.github.com>
5e1764c to
2e6a92f
Compare
|
Rebased onto
|
|
Sharing end-to-end SM120 TP4 DeepSeek-V4.1-Flash A/B numbers measured sequentially on the same 4× RTX PRO 6000 Blackwell (600 W) GPUs with Setup
Correctness & VRAM
Prefill A/B (DeepSeek-V4.1-Flash BF16 MLA, 16K chunk)Calibrated 1-message NIAH (Wall / Prometheus GPU tok/s, 2-rep mean):
Key observation for DeepSeek-V4.1Current |
|
Thank you for running the 4x RTX PRO 6000 Blackwell end-to-end A/B and pointing out the post- Following up on your suggestion, direct L2-flushed CUDA-graph SM120 microbenchmarks comparing
Checking the rest of the combined |
|
Re-tested combined The earlier 16K–128K prefill dip and
Full same-GPU breakdown posted on #176: #176 (comment) |
📌 Description
General SM120 (
gb202)b12xunified MLA prefill kernel (UnifiedPrefillMGKernelinflashinfer/experimental/b12x/attention/_shared/mla/) optimization through register-recycled QK/PV pipelining and reduced shared-memory / barrier overhead. This change benefits all DeepSeek-V4.1 and GLM-5.3-Flash models (BF16, NVFP4, FP8, and TrellisMX) across bothDSV41_SOURCEandGLM_NEXT(ARBITRARY_FP32) MLA prefill paths, with zero TrellisMX dependency.Why this change is needed
On SM120 GPUs (99 KiB addressable shared memory per SM, 255 max registers per thread), the existing
UnifiedPrefillMGKernelstaged QK and PV tiles serially across barriers and issued repeatedLDSM.x4reloads of V/W fragments across the innervcdimension. For compact NVFP4 KV sources (DSV41_SOURCE, 304 B/token), execution was bound by barrier stalls, shared-memory load bandwidth, and sequential QK/PV phases. For widerARBITRARY_FP32KV sources (GLM_NEXT, 33,792 B/tile), L2 read bandwidth saturates at ~4.0 TB/s, so optimizations must preserve full IO prefetch depth while trimming register-file and barrier overhead.What changed
prefill_mg.py,smem_mg.py, anddecode_math.pyfor tighter SM120 mainloop scheduling, XV-NoPE handling, and softmax staging.GLM_NEXT/ARBITRARY_FP32) XV-NoPE and softmax staging (358c16db): Replaces shared-memoryatomic_maxcontention with lock-free packed BF16x2 quad reduction in XOR-swizzled scratch, loads 16x16 E4M3 V tiles directly vialdmatrix.m16n16.x1.trans.b8, foldssm_scale_log2into Q block scales in S0, and defers the 4-lane softmax row-sum butterfly reduction to finalization.DSV41_SOURCE) register-recycled PV+QK pipeline (f1649fd7):99,968 B <= 101,376 B).PV(t)andQK(t+1)using mid-PV deferred waits and register recycling across the attention tile loop.ks/ inner-vcdirect accumulation, eliminating 87.5% ofLDSM.x4shared-memory loads (64 -> 8atmg2).LDS.128) and performs S3 index handling with register-only warp ballots.GLM_NEXT/ARBITRARY_FP32) IO-preserving register-recycled staging (5e1764ca):QK(t+1)||PV(t)shrinks prefetch buffering onARBITRARY_FP32).ldmatrix, adds pre-packed Pair-1 W staging (mg2, 99,456 B, 0 register spills) and 8-slot simultaneous W staging (mg1, 88,704 B, reducingBAR.SYNCper tile from 5 to 3), releasesempty_mbarearly, and uses a 240/24 warp-group register split.Measured performance (isolated kernel microbenchmarks on SM120)
DeepSeek-V4.1 (
DSV41_SOURCE) BF16 prefill (RTX PRO 6000 Blackwell Workstation, 450 W, L2-flushed CUDA graph vs pre-recycling baseline (358c16db))mg2/mg1)us)us)swa128_idx2048_h64_m2048(mg2)swa128_idx2048_h64_m1024(mg2)swa128_idx2048_h128_m1024(mg2)swa128_idx512_h64_m4096(mg2)swa128_idx512_h16_m4096(mg1)GLM-5.3 (
GLM_NEXT/ARBITRARY_FP32) FP8 prefill358c16db, 600 W RTX PRO 6000 Blackwell,M=4096, H=64, D=512, W=2051, 44 slices, L2-flushed CUDA graph):contiguous_20514.199 -> 3.426 ms/slice(-34.01 ms),scattered_32K_20514.324 -> 3.512 ms/slice(-35.73 ms),half_valid_10242.189 -> 1.808 ms/slice(-16.76 ms).5e1764ca, interleaved clock-matched 2610 MHz L2-flushed CUDA graph): additional1.003x–1.010xacross 9/9mg2shapes (M=1024/2048/4096, contiguous/scattered/half-valid) and1.000xat the ~4.0 TB/s SM120 L2-TMA ceiling onmg1(H=16), with 0 register spills (STACK=0 LOCAL=0 STL=0 LDL=0) and bit-exact CUDA graph replay.Combined-stack end-to-end serving validation (separate from isolated kernel microbenchmarks; not attributable solely to this PR)
--mla-compute-mode bf16,--expert-scale-compression native,--embedding-table-storage ram, built onNVIDIA/hyperflare-inference@99339606+ mixed-GPU keying,local-inference-lab/hyperflare-recipes@d954b2f+ general GLM-5.3 recipe PR, andlocal-inference-lab/flashinfer@335459b2+ this PR, zero TrellisMX and zero environment-variable flags):76.20 GiBloaded/GPU (72.8 GiB/21,058,816KV tokens,62 sstartup,135CUDA graphs/rank,38,944captured kernels across 4 ranks), cold prefill37,671(8K,0.217 s) /42,053(32K,0.767 s) /42,780(64K,1.504 s) /43,134 tok/s(128K,2.979 s), sustainedctx=0decode377.4 tok/sat C1 (159.90 steps/s,6.25 ms/rd,2.67 msITL) /619.8 tok/sat C2 (135.59 steps/s,7.38 ms/rd,3.28 msITL) /920.7 tok/sat C4 (100.65 steps/s,9.94 ms/rd,4.49 msITL) /1,185.8 tok/sat C8 (72.67 steps/s,13.76 ms/rd,6.71 msITL,0loops,0errors), HTML Tetris coding681.92med /715.68 tok/sbest (reasoning_effort: "low", 5 runs,0/5CJK),6/6first-token prefix parity checks PASS (6/6 exact reference first tokens),21/21multi-turn chat & tool-calling trials PASS (3/3tool calls on all 5 tool suites) +5/5DeepSeek edge-case checks PASS, image OCR vision check (4871) PASS,0server or CUDA errors. Relative to the prior native-scale build onNVIDIA/hyperflare-inference@96fd11fc+local-inference-lab/flashinfer@0d48af53(16.41MKV tokens;37,630 / 40,881 / 41,386 / 41,941 tok/sprefill;156.37 / 133.50 / 102.63 / 72.03 steps/sat C1/C2/C4/C8), this build delivers+2.8%to+3.4%faster 32K–128K prefill (+1,193 tok/sat 128K),+28.3%KV cache capacity (+4.65Mtokens), and+2.3%faster C1 step cadence (6.39 -> 6.25 ms/rd); these whole-model deltas include upstreammainruntime/memory improvements between96fd11fc/0d48af53and99339606/335459b2in addition to this PR's MLA prefill kernel (1.26x–1.42xisolated).gb202, 4x RTX PRO 6000 Blackwell PCIe, 300/450/300/450 W,brandonmusic/glm-5.3-flash-exl3-tr3-2.375bpw-flare): paired with the companion SM120 decode/prefill/norm/router/MXFP8 BMM kernels PR, the companion TrellisMX P8 MoE runtime PR, and thehyperflare-recipesGLM-5.3 integration:40.75 GiBloaded/GPU (214.5 GiB/36.21MGPU KV cache), cold prefill12,547(8K) /20,330(32K) /20,781(64K) /20,805 tok/s(128K), sustainedctx=0decode303.4(C1) /899.5(C8) /1,241.2(C16) /1,666.1 tok/s(C32), HTML Tetris coding507.7med /549.0 tok/sbest (enable_thinking: false, 0/5 stray CJK) and288.8med /316.2 tok/sbest (thinking on, 0/5 stray CJK), 28-window 57,316-pos contiguous KLD0.045665 nats(95% BCa CI [0.039007, 0.053480],+0.001985vs0.04368carrier-only ref,92.90%Top-1,0.997412PPL ratio),2/2vision PASS,24/24multi-turn chat & tool-calling trials PASS,0server or CUDA errors.gb202-tp2, 2x RTX PRO 6000 Workstation @ 600 W, 17,001 MHz GDDR7, 7-token DFlash2,--online-mxfp8):66.0 GiB / 10,382,336 tokens (10.38M)GPU KV cache (60.86-61.41 GiBloaded/GPU,16 sstartup), cold prefill14,264(8K,0.57 s) /16,588(32K,1.95 s) /16,524(64K,3.90 s) /16,329 tok/s(128K,7.89 s), sustainedctx=0decode256.9(C1, 4 ms ITL) /525.4(C2, 262.7/user) /510.7(C4) /729.4(C8) /1,012.6(C16, 63.3/user) /1,428.9 tok/s(C32, 44.7/user, 22 ms ITL, 0 loops), HTML Tetris coding478.5med /493.8 tok/sbest (reasoning_effort: "low", 0/5 stray CJK) and247.7med /256.4 tok/sbest (thinking on, 0/5 stray CJK),2/2vision PASS,24/24multi-turn chat & tool-calling trials PASS (3/3tool calls on all 5 tool cases),4/4GLM-5.3 edges PASS,0server or CUDA errors.🔍 Related Issues
Standalone SM120 MLA prefill optimization in
flashinfer/experimental/b12x/attention/_shared/mla/.🚀 Pull Request Checklist
✅ Pre-commit Checks
pre-commitby runningpip install pre-commit(or used your preferred method).pre-commit install.pre-commit run --all-filesand fixed any reported issues.🧪 Tests
DSV41_SOURCEMLA prefill CUDA test suite on SM120: 99/99 PASS (92/92test_compressed_sparse_mla_v41.py+test_benchmark_deepseek_v41_mla.py,5/5DSV4.1 binding tests,2/2DSV4 live-graph smoke tests; numerical parity to 6 decimal places).GLM_NEXTMLA prefill CUDA test suite on SM120: 101/101 PASS (89/89targeted MLA pytest +12/12GLM_NEXT parity/edge cases; bit-exact CUDA graph replay).300/450/300/450 W, cleanorigin/main+ this PR + mixed-GPU keying + general recipe PR):21.06MKV cache (72.8 GiB), cold prefill37,671–43,134 tok/s(8K–128K), sustained decode377.4(C1) to1,185.8 tok/s(C8,0loops), HTML Tetris681.92med /715.68best (0/5CJK),6/6first-token prefix parity checks PASS,21/21multi-turn chat & tool-calling trials +5/5DeepSeek edge-case checks PASS, image OCR vision check (4871) PASS; (2) GLM-5.3-Flash TP4 (gb202) & TP2 (gb202-tp2, 2x RTX PRO 6000 Workstation @ 600 W) onbrandonmusic/glm-5.3-flash-exl3-tr3-2.375bpw-flare: TP4 28-window contiguous KLD0.045665nats across 57,316 positions (92.90%Top-1,0.997412PPL ratio); TP2 cold prefill14,264–16,588 tok/s, sustained decode256.9(C1) to1,428.9 tok/s(C32),2/2vision PASS,24/24multi-turn chat & tool-calling trials PASS,4/4GLM-5.3 edges PASS,0server or CUDA errors.Suggested CI test targets:
tests/experimental/b12x/attention/🔬 Experimental Track
flashinfer/experimental/b12x/attention/_shared/mla/(decode_math.py,prefill_mg.py,smem_mg.py). Scope and validation are tracked in this PR.flashinfer/experimental/b12x/attention/_shared/mla/(zero non-experimental files modified).tests/experimental/b12x/attention/and were validated on SM120 hardware.flashinfer/aot.py, and no experimental backend is reachable frombackend="auto"withoutFLASHINFER_ALLOW_EXPERIMENTAL_AUTO_BACKENDS=1.Reviewer Notes
origin/main(335459b2) with zero file overlap against the companion SM120 decode/prefill/norm/router/MXFP8 BMM kernels PR or the companion TrellisMX P8 MoE runtime PR.gb202TP4 &gb202-tp2TP2, RTX PRO 6000 Blackwell); SM103/SM121 hardware was not tested locally.Discord: Brandonmusic