Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion tensorrt_llm/_torch/kernels/blackwell/moe_router_gemm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cutlass._mlir.dialects import llvm
from cutlass.cute.nvgpu import cpasync, tcgen05
from cutlass.cutlass_dsl import T, dsl_user_op
from cutlass.experimental import primitives as prims

from ...cute_dsl_utils import smem_optin_bytes
from .moe_decode_front import (
Expand Down Expand Up @@ -108,6 +109,24 @@ def _tcgen05_after_sync(*, loc=None, ip=None) -> None:
_asm(None, [], "tcgen05.fence::after_thread_sync;", "~{memory}", loc=loc, ip=ip)


@cute.jit
def _mbar_wait_cluster_acquire(mbar_ptr: cute.Pointer, phase: Int32):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this helper is now duplicated in three files. A shared helper next to the other CuTe DSL utilities would keep the memory-ordering logic in one place and make it easy to reuse for the remaining sites listed in #21843.

"""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' ``cp.async.bulk ...
mbarrier::complete_tx`` (``_bulk_push``) 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 MoeRouterGemmKernel:
"""logits [M, E] (BF16, row-major, contiguous) = x [M, K] (BF16, row stride ``a_stride``) @ w [E, K].T.

Expand Down Expand Up @@ -538,7 +557,8 @@ class SharedStorage:
self._quant_phase(
sB, full_mbar, gs_ptr, xq_ptr, xsf_ptr, s, e_tile, t_tile, tidx, phase
)
cute.arch.mbarrier_wait(recv_mbar, 0)
# The split CTAs' bulk copies complete `recv_mbar` with a cluster-scope release.
_mbar_wait_cluster_acquire(recv_mbar, 0)
_cluster_arrive_relaxed() # this CTA's slices have arrived (waited at the end)

# Reduce this CTA's slice (tokens [s tps, (s + 1) tps) of the tile, all 128 experts) in split order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from cutlass.cute.nvgpu import cpasync, tcgen05
from cutlass.cute.runtime import from_dlpack
from cutlass.cutlass_dsl import T, dsl_user_op
from cutlass.experimental import primitives as prims
from cutlass.pipeline import pipeline_init_arrive, pipeline_init_wait

from tensorrt_llm._torch.flashinfer_utils import get_env_enable_pdl
Expand Down Expand Up @@ -118,6 +119,24 @@ def _lds_v4_f32(addr, *, loc=None, ip=None):
return [cutlass.Float32(llvm.extractvalue(ty, res, [i], loc=loc, ip=ip)) for i in range(4)]


@cute.jit
def _mbar_wait_cluster_acquire(mbar_ptr: cute.Pointer, phase: cutlass.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' ``cp.async.bulk ...
mbarrier::complete_tx`` (``_bulk_to_peer``) 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 _bf16x2(lo, hi, *, loc=None, ip=None):
"""Two fp32 values rounded (RN, as `.to(BFloat16)`) into one bf16x2 word, `lo` low."""
Expand Down Expand Up @@ -540,7 +559,8 @@ class SharedStorage:

# Every K slice's partial of this CTA's rows is local once `recv_full` completes;
# no DSMEM access follows (a CTA may exit once its own copies have read their source).
cute.arch.mbarrier_wait(storage.recv_full, 0)
# The peers' bulk copies complete it with a cluster-scope release: acquire at cluster scope.
_mbar_wait_cluster_acquire(storage.recv_full, 0)

# This CTA's rows [kr * rows_per, ...) of the tile's real rows, in 4-column groups,
# all 6 warps; an element's `KS` partials summed in K-slice order (deterministic).
Expand Down
21 changes: 20 additions & 1 deletion tensorrt_llm/_torch/models/musespark_open_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from cutlass._mlir.dialects import llvm
from cutlass.cute.runtime import make_ptr
from cutlass.cutlass_dsl import T, dsl_user_op
from cutlass.experimental import primitives as prims

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this import, the module needs a CuTe DSL release that has cutlass.experimental.primitives; with an older one, _HAS_CUTE_DSL silently becomes False and the cluster boundary kernel is disabled. The comment above the import block ("this one needs no minimum DSL release") should be updated.

except ImportError:
_HAS_CUTE_DSL = False
else:
Expand All @@ -58,6 +59,23 @@ def _float_bits(value, *, loc=None, ip=None):
)
return Int32(bits)

@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`` (``store_async_dsmem``) 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 _ClusterBoundaryKernel:
def __init__(
self,
Expand Down Expand Up @@ -123,7 +141,8 @@ def reduce(
bars.iterator + phase,
lane,
)
cute.arch.mbarrier_wait(bars.iterator + phase, 0)
# The peers' st.async complete-tx releases at cluster scope.
_mbar_wait_cluster_acquire(bars.iterator + phase, 0)
else:
if lane == 0:
partials[(phase * self.slots + warp) * 4] = value
Expand Down
Loading