Skip to content

Commit 00294d8

Browse files
committed
[TRTLLM-16622][perf] DeepSeek-V4.1: mHC all-reduce boundaries with the attention O-B in the boundary launch
At attention TP the decode mHC boundaries all-reduce the sublayer's partial output in their own launch (one-shot Lamport protocol). On SM100/SM103, every change bitwise: - ObAllReduceBoundaryKernel runs the attention's small-M MXFP8 O-B projection, its all-reduce and the FFN-entry boundary as two roles of one grid: O-B CTAs compute 8-column tiles as the small-M GEMM does and push them into every rank's slots; token CTAs poll, sum in rank order and finish the boundary. It engages for up to 16 rows when the FFN boundary is split and all-reduces in-kernel and the O-B shard tiles the grid (attention TP8); the decoder layer asks the attention for its O-B input through defer_ob_to_boundary, and defer_ob_projection builds the ObUnprojected. - The CTA counted last advances the Lamport state (a hand-over warp in the cluster form of the all-reduce boundary), so no CTA spins on the arrival counter after its work. - A rank takes its own slot from the words it pushed instead of waiting for their multicast copy. - The 4-CTA cluster form (up to 12 rows) exchanges RMSNorm partials with st.async on an mbarrier; the MoE-finalize rows load right after the routing. - Behind a captured CuTe FC2 the MNNVL MoE boundary reads its routing and clears the previous buffer before its dependency wait. The MoE boundary that enters a layer whose O-B is deferred prefetches that O-B weight into L2. GB300, attention TP8 batch-1 decode with DSpark k=5, per decode step: last-CTA hand-over in the O-B boundary -6..-10 us; routing before the wait, own slot and hand-over warp -16 us together (FC2 end -> MoE boundary end -0.38 / -0.43 us per layer); the O-B weight prefetch together with the CSA2 O-A releasing the deferred boundary after its wait about -29 us. The fused O-B launch and the mbarrier exchange are not measured separately (the exchange with the early MoE rows: 3.688 -> 3.684 ms per step at attention TP4 with 6 rows, within the run-to-run spread). Test: test_mhc_ob_boundary_matches_ob_then_boundary (1, 6 and 16 rows, with and without the NVFP4 copy, MNNVL; 50 graph replays between a MoE-finalize boundary and a library all-reduce on one workspace) equals the O-B GEMM then the boundary byte for byte; test_mhc_moe_boundary_behind_lowm_fc2 (2 GPUs, MNNVL) replays captured layers of attention boundary, FC1-only MoE, CuTe FC2 and MoE boundary (with the O-B prefetch) with the eager bits; test_mhc_allreduce_boundary_matches_allreduce_then_boundary also covers the 6-row MoE input. Signed-off-by: ziyixiong-nv <219238287+ziyixiong-nv@users.noreply.github.com>
1 parent 9ae3494 commit 00294d8

8 files changed

Lines changed: 1759 additions & 112 deletions

File tree

‎tensorrt_llm/_torch/kernels/blackwell/mhc/mhc_ob_boundary.py‎

Lines changed: 708 additions & 0 deletions
Large diffs are not rendered by default.

‎tensorrt_llm/_torch/kernels/blackwell/mhc/mhc_split_boundary.py‎

Lines changed: 521 additions & 75 deletions
Large diffs are not rendered by default.

‎tensorrt_llm/_torch/models/modeling_deepseekv41.py‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
mhc_lamport_allreduce,
6868
mhc_post_collapse_norm,
6969
mhc_split_enabled,
70+
ob_boundary_enabled,
7071
)
7172
from ..modules.multi_stream_utils import do_multi_stream
7273
from ..moe.fused_moe.impl_contract import MOE_DECODE_OPTIONS, MoEDecodeOptions
@@ -1065,6 +1066,16 @@ def _mhc_fused_allreduce(self):
10651066
self._mhc_lamport_resolved = True
10661067
return self._mhc_lamport
10671068

1069+
def _ob_weights(self) -> tuple:
1070+
"""The MXFP8 o_b projection's weight and linear scales, as the deferred projection reads them."""
1071+
from ..modules.low_m_gemm import mxfp8_linear_scale
1072+
1073+
linear = self.self_attn.o_b_proj
1074+
if linear.weight.dtype != torch.float8_e4m3fn:
1075+
return ()
1076+
scale = mxfp8_linear_scale(linear)
1077+
return (linear.weight,) if scale is None else (linear.weight, scale)
1078+
10681079
def _moe_supports_unfinalized(self) -> bool:
10691080
"""Whether the routed experts can hand back their rows before the top-k combine."""
10701081
experts = self.mlp.experts
@@ -1184,6 +1195,8 @@ def forward(
11841195
ffn_split = self._mhc_split(residual.shape[0], ffn=True)
11851196
# Split boundaries can also all-reduce the sublayer output in their own launch.
11861197
lamport = self._mhc_fused_allreduce() if split else None
1198+
# The attention may hand back its o_b input for the FFN boundary to project in its launch.
1199+
defer_ob = lamport is not None and ffn_split and ob_boundary_enabled(residual.shape[0])
11871200
attn_coeff_event = None
11881201
# The entry boundary (split or single-pass) also emits the MXFP8 input of the fused
11891202
# query/SWA projection.
@@ -1209,6 +1222,10 @@ def forward(
12091222
events=self._mhc_attn_events,
12101223
allreduce=entry_allreduce,
12111224
mxfp8=mxfp8,
1225+
# The split FFN boundary reads these coefficients after its dependency wait.
1226+
coefficients_release_after_wait=ffn_split,
1227+
# Its o_b CTAs then read their weights from L2.
1228+
l2_prefetch=self._ob_weights() if defer_ob and entry_allreduce is not None else (),
12121229
)
12131230
residual, attn_pre, post_mix, comb_mix, layer_input, attn_coeff_event = boundary[:6]
12141231
if mxfp8:
@@ -1246,19 +1263,23 @@ def forward(
12461263
residual.reshape(residual.shape[0], -1),
12471264
getattr(attn_metadata, "csa2_replay_query_rows", None),
12481265
)
1249-
x_attn = self.self_attn(
1250-
position_ids=position_ids,
1251-
hidden_states=layer_input,
1252-
attn_metadata=attn_metadata,
1253-
hidden_states_mxfp8=layer_input_mxfp8,
1254-
# The split boundary's critical half prefetches its residual before its PDL wait,
1255-
# so the all-reduce may let it launch at its start.
1256-
all_reduce_params=AllReduceParams(
1257-
enable_allreduce=not self.disable_attn_allreduce and lamport is None,
1258-
trigger_completion_at_end=not split,
1259-
),
1260-
**kwargs,
1261-
)
1266+
self.self_attn.defer_ob_to_boundary = defer_ob
1267+
try:
1268+
x_attn = self.self_attn(
1269+
position_ids=position_ids,
1270+
hidden_states=layer_input,
1271+
attn_metadata=attn_metadata,
1272+
hidden_states_mxfp8=layer_input_mxfp8,
1273+
# The split FFN boundary loads its constant inputs, and after a split entry also
1274+
# its residual, before its PDL wait, so the all-reduce may let it launch at its start.
1275+
all_reduce_params=AllReduceParams(
1276+
enable_allreduce=not self.disable_attn_allreduce and lamport is None,
1277+
trigger_completion_at_end=not ffn_split,
1278+
),
1279+
**kwargs,
1280+
)
1281+
finally:
1282+
self.self_attn.defer_ob_to_boundary = False
12621283
if after_attention_callback is not None:
12631284
after_attention_callback()
12641285
if capture_done is not None:

‎tensorrt_llm/_torch/modules/mhc/hyper_connection.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ def split_hc_lagged(
812812
nvfp4_scale: Optional[torch.Tensor] = None,
813813
residual_after_wait: bool = False,
814814
coefficients_release_after_wait: bool = False,
815+
l2_prefetch: tuple = (),
815816
) -> tuple:
816817
"""Lagged boundary as a critical half and a coefficient half (decode-sized M).
817818
@@ -830,7 +831,8 @@ def split_hc_lagged(
830831
dependency wait (a split boundary), so their launch may release it from its own wait on.
831832
832833
With ``allreduce`` (a :class:`~.mhc_lowm.LamportAllReduce`), ``x_prev`` is this rank's
833-
tensor-parallel partial sum and the critical half all-reduces it in the same launch.
834+
tensor-parallel partial sum and the critical half all-reduces it in the same launch; it
835+
also reads the ``l2_prefetch`` tensors into L2 (see ``mhc_allreduce_post_collapse_norm``).
834836
835837
Returns ``(residual, pre, post, comb, layer_input, coeff_event)``; ``residual`` is
836838
``residual_prev`` itself when ``x_prev`` is None. ``mxfp8`` appends ``layer_input``'s
@@ -873,6 +875,7 @@ def split_hc_lagged(
873875
allreduce,
874876
mxfp8=mxfp8,
875877
nvfp4_scale=nvfp4_scale,
878+
l2_prefetch=l2_prefetch,
876879
)
877880
else:
878881
residual, layer_input, *quantized = mhc_post_collapse_norm(

0 commit comments

Comments
 (0)