From 23f4fba25e55db7b756451fd9ac247cc92359e68 Mon Sep 17 00:00:00 2001 From: Bastian Hagedorn Date: Wed, 2 Dec 2026 10:09:37 +0100 Subject: [PATCH] [None][fix] Acquire at cluster scope on the DSMEM mailbox waits of the Kimi K3.1 Rubin router and push GEMMs Three Kimi K3.1 SM107 kernels read shared memory that a peer CTA of the cluster delivered with st.async ... mbarrier::complete_tx, after waiting for the mailbox mbarrier with cute.arch.mbarrier_wait, which acquires at CTA scope. The peers' complete-tx releases at cluster scope, so the CTA-scope acquire does not order their payload before the reads that follow the wait. - kernels/moe/rubin_router_exact.py: CTA 1 waits for CTA 0's running sums. - kimi_k31_pro_nvfp4__sm_107__tep8/kernels/spec/rubin_draft_gemm_push.py and kimi_k31_pro_nvfp4__sm_107__tep8/kernels/moe/rubin_ull_push_gemm.py: the owner CTA waits for its peers' split-K partials. Each wait becomes a relaxed cluster-scope mbarrier.try_wait loop followed by an acquire fence restricted to shared::cluster (_mbar_wait_cluster_acquire). Signed-off-by: Bastian Hagedorn --- .../kernels/moe/rubin_router_exact.py | 21 ++++++++++++++++++- .../kernels/moe/rubin_ull_push_gemm.py | 4 +++- .../kernels/spec/rubin_draft_gemm_push.py | 21 ++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kernels/moe/rubin_router_exact.py b/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kernels/moe/rubin_router_exact.py index 26a84772b89e..1c9b20795fe6 100644 --- a/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kernels/moe/rubin_router_exact.py +++ b/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kernels/moe/rubin_router_exact.py @@ -38,6 +38,7 @@ from cutlass.cute.nvgpu import cpasync from cutlass.cute.runtime import make_ptr from cutlass.cutlass_dsl import T, dsl_user_op +from cutlass.experimental import primitives as prims from .rubin_fused_router import ( _EVICT_FIRST, @@ -111,6 +112,23 @@ def _st_async_v4_f32(local_addr, v0, v1, v2, v3, local_mbar, peer, *, loc=None, ) +@cute.jit +def _mbar_wait_cluster_acquire(mbar_ptr: cute.Pointer, phase: Int32): + """Wait for ``phase`` of this CTA's mbarrier at ``mbar_ptr``, acquiring at cluster scope. + + ``cute.arch.mbarrier_wait`` acquires at CTA scope. The peers' ``st.async ... + mbarrier::complete_tx`` that completes this barrier releases at cluster scope, and only a + cluster-scope acquire orders their payload before the reads that follow the wait: a relaxed + cluster-scope try_wait loop and an acquire fence restricted to shared::cluster (the ISA's + acquire pattern for it; unlike ``.acquire.cluster`` on the wait it adds no L1 invalidate). + """ + while not prims.mbarrier_try_wait_parity( + mbar_ptr, phase, scope=prims.MBarrierScope.CLUSTER, order=prims.MemOrder.RELAXED + ): + pass + prims.fence_sync_restrict(prims.MemOrder.ACQUIRE) + + @dsl_user_op def _ldg_v4_b32_if(valid, addr, *, loc=None, ip=None): """16 bytes of global memory as four 32-bit words if ``valid != 0``, else zeros.""" @@ -319,7 +337,8 @@ def kernel( off = tidx * 16 tot = [Float32(0.0)] * 4 if rank == 1: - cute.arch.mbarrier_wait(mbox_full, 0) + # CTA 0's st.async complete-tx releases at cluster scope. + _mbar_wait_cluster_acquire(mbox_full, 0) tot = list(_lds_v4_f32(mbox_base + off)) for w in cutlass.range_constexpr(NUM_WARPS): v = _lds_v4_f32(red_base + w * _FRAG_BYTES + off) diff --git a/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/moe/rubin_ull_push_gemm.py b/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/moe/rubin_ull_push_gemm.py index b2971cd84e1f..ef088f91666a 100644 --- a/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/moe/rubin_ull_push_gemm.py +++ b/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/moe/rubin_ull_push_gemm.py @@ -55,6 +55,7 @@ _bmm_push, _lds_v4, _make_layout_tensor, + _mbar_wait_cluster_acquire, _swapped, ensure_cute_runtime, validate_push_tactic, @@ -199,7 +200,8 @@ def push_epilog_warp( cute.arch.mbarrier_arrive_and_expect_tx( bar_reduce, self.expected_transaction_bytes ) - cute.arch.mbarrier_wait(bar_reduce, 0) + # The peers' st.async complete-tx releases at cluster scope. + _mbar_wait_cluster_acquire(bar_reduce, Int32(0)) for peer in cutlass.range_constexpr(self.split_k - 1): for value_idx in cutlass.range_constexpr(values_per_thread): rAcc[value_idx] = ( diff --git a/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/spec/rubin_draft_gemm_push.py b/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/spec/rubin_draft_gemm_push.py index 637bb0012f7e..5e2324507d5f 100644 --- a/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/spec/rubin_draft_gemm_push.py +++ b/tensorrt_llm/_torch/modeling_v2/models/kimi_k31/kimi_k31_pro_nvfp4__sm_107__tep8/kernels/spec/rubin_draft_gemm_push.py @@ -38,6 +38,7 @@ from cutlass import Int32, Int64 from cutlass.cute import experimental as cute_ext from cutlass.cutlass_dsl import T, dsl_user_op +from cutlass.experimental import primitives as prims from flashinfer.gemm.kernels.dense_bf16_gemm_sm100_splitk import ( _AB_BUFFER_ALIGN_BYTES, _MAILBOX_ALIGN_BYTES, @@ -126,6 +127,23 @@ def _lds_v4(addr, *, loc=None, ip=None): return _unpack(res, T.i32(), Int32, 4, loc=loc, ip=ip) +@cute.experimental.jit +def _mbar_wait_cluster_acquire(mbar_ptr: cute.Pointer, phase: Int32): + """Wait for ``phase`` of this CTA's mbarrier at ``mbar_ptr``, acquiring at cluster scope. + + ``cute.arch.mbarrier_wait`` acquires at CTA scope. The peers' ``st.async ... + mbarrier::complete_tx`` that completes this barrier releases at cluster scope, and only a + cluster-scope acquire orders their payload before the reads that follow the wait: a relaxed + cluster-scope try_wait loop and an acquire fence restricted to shared::cluster (the ISA's + acquire pattern for it; unlike ``.acquire.cluster`` on the wait it adds no L1 invalidate). + """ + while not prims.mbarrier_try_wait_parity( + mbar_ptr, phase, scope=prims.MBarrierScope.CLUSTER, order=prims.MemOrder.RELAXED + ): + pass + prims.fence_sync_restrict(prims.MemOrder.ACQUIRE) + + class SplitKArPushGemmKernel(SplitKDenseGemmKernel): """:class:`SplitKDenseGemmKernel` with the operands of a push into the Lamport slots of an all-reduce of ``world`` ranks over ``hidden`` columns and a token-major staging tile (see the @@ -480,7 +498,8 @@ def push_epilog_warp( cute.arch.mbarrier_arrive_and_expect_tx( bar_reduce, self.expected_transaction_bytes ) - cute.arch.mbarrier_wait(bar_reduce, 0) + # The peers' st.async complete-tx releases at cluster scope. + _mbar_wait_cluster_acquire(bar_reduce, Int32(0)) for peer in cutlass.range_constexpr(self.split_k - 1): for value_idx in cutlass.range_constexpr(values_per_thread): rAcc[value_idx] = (