Skip to content

fix(b12x): move TC-GEMV split-K buffers out of global caches, unify 14 PCIe epochs, fix mHC PDL sync and MLA/DSA/MoE P0 bugs - #183

Merged
lukealonso merged 1 commit into
mainfrom
unify-b12x-tcgemv-scratch-pcie-epoch
Nov 29, 2026
Merged

lukealonso merged 1 commit into
mainfrom
unify-b12x-tcgemv-scratch-pcie-epoch

Conversation

@lukealonso

Copy link
Copy Markdown

📌 Description

1. Why

This PR resolves a P0 wave of memory-safety, cross-stream aliasing, and GPU-scope synchronization bugs across flashinfer/experimental/b12x/:

  1. TC-GEMV split-K global dict races (GLM-5.3 Flash KDA NaN bug): All four TC-GEMV families (bf16_gemv, blockscaled MXFP8, blockscaled NVFP4, and wo_projection A16) stored L2 split-K completion flags (and bf16_gemv split-K partials) in process-global dicts (_TC_SPLITK_BUFFERS, _FLAGS_CACHE, _WOA_FLAGS_CACHE) keyed only by (device, shape, split_k). When concurrent streams run identical-shape split-K TC-GEMV projections (e.g., GLM-5.3 KDA g_b_proj / f_b_proj BF16 [2048, 128] with split_k in {2, 4} under ConcurrentProjectionChain), both streams alias the same flags/scratch buffer, causing premature last-CTA arrival and NaN/corrupted outputs.
  2. 14 legacy PCIe epoch kernels missed by fix(b12x/comm/pcie): enforce GPU-scope epoch read-before-advance ordering #154 + perf(b12x/mhc): tune AllReduce cluster sizing and guard SM co-residency #158 mHC PDL sync race:
    • Three legacy PCIe epoch helpers (graph_epoch_arrive, _a2a_graph_epoch_arrive, _advance_graph_epoch) across 14 kernels / 15 call sites still used unscoped atom.global.add.u32 + weak ld/st instead of sync_threads_after_u32 + graph_epoch_arrive_serialized.
    • ClusterMhcAllReducePostYNorm.kernel (merged in perf(b12x/mhc): tune AllReduce cluster sizing and guard SM co-residency #158) called cute.arch.griddepcontrol_launch_dependents() with no preceding cute.arch.sync_threads() and before cluster_arrive()/cluster_wait(), letting dependent kernels launch before all threads/cluster peers finished writing outputs.
  3. Dense MLA >2 GiB Int32 IMA, DSA fused-indexer races, reserve_sms CTA-vs-SM math, and INT64 expert-ID narrowing:
    • attention/dense_mla/_math.py (write_partial_or_final) and _merge.py (output_offset) computed 4D partial_output offsets in 32-bit Int32, overflowing past 2 GiB on long-context split prefill (e.g., Kimi prefill4096) into an illegal memory access (IMA).
    • attention/dsa_indexer/fused_indexer.py elided cute.arch.sync_threads() after split reduction when page_splits == 1 (racing on s_scale reuse across pages in CONTIGUOUS_MLA) and read merge_state[output_offset] with a plain L1-cached load after threadfence() (membar.gl does not invalidate L1).
    • moe/fused_moe/_impl.py subtracted reserve_sms directly from CTA count instead of scaling remaining SMs by blocks_per_sm ((num_sm - reserve_sms) * blocks_per_sm).
    • W4A16 route packing, W4A16 top-k sum reductions, W4A16 direct TC-decode, and MoE calibration narrowed INT64 topk_ids to int32 before bounds-checking, allowing values outside INT32 range (e.g., 1 << 32) to wrap into valid expert IDs during CUDA-graph replay.

2. What changed

  • TC-GEMV Split-K Scratch & Flags (harmonized with perf(b12x): enable consumer PDL for SM120 decode kernels #165 consumer PDL & perf(b12x/gemm): gate SM120 DSMEM split-K by CTA occupancy #169 SM120 DSMEM split-K):
    • flashinfer/experimental/b12x/_lib/utils/device.py: added stream-ordered zero_u32_async (cuMemsetD32Async).
    • bf16_gemv/{_tensor_core.py,_preparation.py,api.py}: deleted _TC_SPLITK_BUFFERS / tc_splitk_buffers; moved non-DSMEM split-K partials + 256B-aligned flags into caller-owned MemoryRequirements.scratch (bf16_gemv.tc_splitk, zero PersistentMemory), added optional workspace to bf16_gemv.mm (with stream-local torch.empty fallback when omitted), and zeroed embedded flags per launch via zero_u32_async. Preserved DSMEM zero-scratch behavior and 16B-aligned dummy pointers (x.data_ptr()).
    • blockscaled/{_mxfp8_tc_gemv.py,_nvfp4_tc_gemv.py,_preparation.py}: deleted _FLAGS_CACHE and cached_*_tc_gemv_flags; embedded 256B-aligned flags into *_tc_gemv_workspace_bytes and sliced/zeroed them per launch when split_k > 1 and not use_dsmem and flags is None (preserving compile_mxfp8_tc_gemv_rope / projection_overlap.py compatibility and DSMEM zero-workspace behavior).
    • wo_projection/{_tc_gemv.py,_preparation.py}: deleted _WOA_FLAGS_CACHE / cached_wo_a16_tc_gemv_flags and PersistentMemory("wo_projection.a16_woa_flags", ...); included aligned flags in wo_a16_tc_gemv_scratch_bytes, sliced flags into binding.tmp_q, and zeroed per launch with zero_u32_async when non-DSMEM split_k > 1.
  • 14 PCIe Epoch Kernels & perf(b12x/mhc): tune AllReduce cluster sizing and guard SM co-residency #158 mHC PDL Sync:
    • Deleted graph_epoch_arrive, _a2a_graph_epoch_arrive, and _advance_graph_epoch; unified all 14 kernels / 15 call sites across _dcp_a2a_cute.py, _ring_gather_cute.py, _phased_gather_cute.py, _island_gather_cute.py, _recursive_gather_cute.py, _kimi_route_cute.py, _oneshot_cute.py, and interleaved_topk/_cute.py onto sync_threads_after_u32 + non-divergent cute.arch.sync_threads() + graph_epoch_arrive_serialized; bumped every modified KernelCompileSpec version.
    • norm/mhc/_split_lagged.py: ordered cluster_arrive()/cluster_wait() (cluster_size > 1) and cute.arch.sync_threads() before griddepcontrol_launch_dependents(); bumped norm.mhc.cluster_allreduce_post_y_norm spec 3 -> 4.
  • Dense MLA, DSA Indexer, reserve_sms, INT64 Expert-IDs:
    • attention/dense_mla/{_math.py,_merge.py,_kernel.py}: switched 4D partial_output stores and output_offset to 64-bit Int64 + get_ptr_as_int64; bumped forward & merge specs 4 -> 5.
    • attention/dsa_indexer/fused_indexer.py: made post-reduce cute.arch.sync_threads() unconditional (page_splits == 1), replaced plain load Int32(merge_state[output_offset]) with ld_global_acquire_i32(get_ptr_as_int64(merge_state, output_offset)), and bumped variant keys v13 -> v14.
    • moe/fused_moe/_impl.py: added _apply_dynamic_reserve_sms scaling remaining SMs by blocks_per_sm (tracking blocks_per_sm = 2 for w4a8_repacked and selected_tile_m <= 32).
    • moe/_shared/kernels/w4a16/{route_pack.py,kernel/sum.py,kernel/sum_launch.py,kernel/execution.py} and moe/calibration.py: validated full-width INT64 expert IDs before narrowing to int32 across all route-pack, top-k sum (spec 8 -> 9), direct TC-decode, and 6 calibration Triton kernels.

3. Claimed benefit

4. Supporting evidence and limitations

Validated at commit 9bf835a2d (rebased onto lil/main 4e6f12cc7) on NVIDIA RTX PRO 6000 Blackwell (SM120, 1–8 GPUs), CUDA 13.5:

  • Post-rebase TC-GEMV / DSMEM / PDL verification:
    • tests/experimental/b12x/test_sm120_dsmem_splitk.py: 13 passed
    • tests/experimental/b12x/test_sm120_decode_pdl.py: 13 passed
    • tests/experimental/b12x/gemm/test_decode_gemm_moe_contracts.py + tests/experimental/b12x/gemm/test_tc_gemv_concurrent_streams.py: 63 passed (default) + 8 passed with B12X_TC_GEMV_DSMEM_SPLIT=0 (100 CUDA-graph dual-stream replays per shape with 0xFF-poisoned workspaces across BF16 KDA [16, 2048] x [128, 2048]^T, MXFP8, MXFP8-RoPE flags=None, NVFP4, and WO-A16)
  • Track 2 & Track 3 unit / GPU regression suites:
    • tests/experimental/b12x/moe/test_w4a16_int64_expert_ids_replay.py + test_calibration.py + test_moe_execution_model.py: 44 passed
    • tests/experimental/b12x/attention/test_dense_mla.py: 17 passed (including 2.50 GiB BF16 & 5.00 GiB FP32 partials)
    • tests/experimental/b12x/attention/test_fused_indexer.py: 108 passed, 1 pre-existing failure (test_dsa_indexer_config_and_merge_thresholds in untouched _tuning.py)
    • tests/experimental/b12x/norm/test_mhc_allreduce_post_y_norm.py (single-GPU) + test_w4a16_{route_pack,route_sum,reference}.py + test_interleaved_topk.py: 259 passed, 103 skipped
    • tests/experimental/b12x/comm/test_kimi_topk16_gpu.py + tests/experimental/b12x/gemm/{test_mxfp8_wide_splitk,test_cute_mxfp8_splitk_pdl,test_mxfp8_linear,test_blockscaled}.py: 155 passed
    • PCIe epoch & mHC PDL contract checks (test_pcie_twoshot_unit.py, test_pcie_dcp_a2a.py, test_mhc_allreduce_post_y_norm.py): 6 passed
    • tests/experimental/b12x/lint/: 1 passed
  • Live multi-GPU (4-GPU and 8-GPU SM120):
    • 4-GPU test_mhc_allreduce_post_y_norm.py -k live_multi_gpu (B12X_RUN_PCIE_ONESHOT_TEST=1): 19 passed
    • 4-GPU test_interleaved_topk_gpu.py + test_paired_gather_gpu.py + test_pcie_dcp_a2a_gpu.py: 10 passed, 8 skipped
    • 4-GPU test_pcie_oneshot_fused_rmsnorm_gpu.py (B12X_RUN_PCIE_ONESHOT_RMS_TEST=1): 2 passed
    • 4-GPU test_pcie_oneshot_torture.py (B12X_RUN_PCIE_ONESHOT_TORTURE=1, 4 ranks): 59 passed, 24 skipped
    • 8-GPU test_interleaved_topk_gpu.py[8-4] + test_pcie_dcp_a2a_gpu.py (WORLD_SIZE=8): 3 passed, 2 skipped
  • compute-sanitizer (SM120):
    • memcheck on >2 GiB test_dense_mla.py: 2 passed, 0 errors
    • synccheck & memcheck on test_fused_indexer.py (page_splits == 1 + serial-merge replay): passed, 0 errors (both tools)
    • memcheck on test_tc_gemv_concurrent_streams.py: 8 passed, 0 errors
    • memcheck on test_w4a16_int64_expert_ids_replay.py + test_calibration.py: 20 passed, 0 errors
  • Pre-commit / lint: All pre-commit hooks (ruff check, ruff format, whitespace/EOL/conflict checks) passed on commit.
  • Limitations: End-to-end 4× DGX Spark (SM121) runs (scripts/repro_glm_batched_speculation.py and Kimi 6112-token serving repro in hyperflare-recipes) were not executed on this SM120 host.

🔍 Related Issues

🚀 Pull Request Checklist

✅ Pre-commit Checks

  • Pre-commit hooks ran on commit and passed (ruff check, ruff format, whitespace/EOL checks).

🧪 Tests

  • Tests have been added or updated as needed.
  • All added and affected tests are passing on SM120 (1–8 GPUs) and under compute-sanitizer.

🔬 Experimental Track

  • This PR is experimental: it changes code only under flashinfer/experimental/b12x/ with tests under tests/experimental/b12x/.
    • Core flashinfer/ is untouched; nothing is registered in flashinfer/aot.py.
    • Tests live in tests/experimental/b12x/ and were validated on SM120 hardware.
    • Test scope declared below.
tests/experimental/b12x/test_sm120_dsmem_splitk.py
tests/experimental/b12x/test_sm120_decode_pdl.py
tests/experimental/b12x/gemm/test_decode_gemm_moe_contracts.py
tests/experimental/b12x/gemm/test_tc_gemv_concurrent_streams.py
tests/experimental/b12x/attention/test_dense_mla.py
tests/experimental/b12x/attention/test_fused_indexer.py
tests/experimental/b12x/norm/test_mhc_allreduce_post_y_norm.py
tests/experimental/b12x/moe/test_w4a16_int64_expert_ids_replay.py
tests/experimental/b12x/moe/test_calibration.py
tests/experimental/b12x/moe/test_moe_execution_model.py
tests/experimental/b12x/lint/

Reviewer Notes

…, fix mHC PDL sync and MLA/DSA/MoE P0 bugs

Signed-off-by: Luke Alonso <lalonso@gmail.com>
@lukealonso
lukealonso merged commit 64a4db0 into main Nov 29, 2026
8 of 11 checks passed
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