Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion vllm/model_executor/layers/fused_moe/modular_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,8 +1030,19 @@ def set_capture_fn(
if capture_fn is None:
self._routing_replay_buffer = None
return
# Naive DP and DP+EP prepare paths all-gather per-rank batches before
# invoking the monolithic kernel. ``max_num_tokens`` is the scheduler
# limit for one rank, so the replay output must cover the largest

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A bit verbose.

# dispatch group rather than only the local batch. Under EP that group
# can also contain sequence-parallel shards flattened into the EP
# group, including padding.
dispatch_group_size = max(

@aoshen02 aoshen02 Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be more clearer to change to

 if self.moe_config.use_ep:
      self.moe_config.ep_size
else:
     self.moe_config.dp_size

self.moe_config.dp_size,
self.moe_config.ep_size,
)
max_num_replay_tokens = self.moe_config.max_num_tokens * dispatch_group_size
self._routing_replay_buffer = torch.empty(
(self.moe_config.max_num_tokens, self.moe_config.experts_per_token),
(max_num_replay_tokens, self.moe_config.experts_per_token),
dtype=torch.int16,
device=self.moe_config.device,
)
Expand Down
26 changes: 22 additions & 4 deletions vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,20 @@ def __init__(
def capture(self, layer_id: int, topk_ids: torch.Tensor) -> None:
"""Capture expert routing decisions for a specific layer.

Under data parallelism, ``topk_ids`` may have three different batch
Under data parallelism, ``topk_ids`` may have four different batch
layouts depending on where the DP combine happens and whether
Sequence Parallelism (SP) is active for the MoE layer:
Expert Parallelism (EP) or Sequence Parallelism (SP) is active for the
MoE layer:
- ``n == total`` (naive dispatch): all DP ranks' tokens are
concatenated before routing; we slice out this rank's span
using the cumulative per-rank counts.
- ``n == token_num_per_dp`` (modular-kernel path): DP combine
happens inside ``quant_method.apply``; ``select_experts`` only
ever sees this rank's tokens, so we take the whole tensor.
- ``n == sum(dp_metadata.local_sizes)`` (naive DP+EP dispatch):
sequence-parallel shards from every DP rank are gathered through
the flattened EP group. The shard sizes include CUDA-graph / SP
padding, so we use them to locate this DP rank's unpadded rows.
- ``n == ceil(token_num_per_dp / tp_size)`` (SP + modular-kernel
path): tokens were split along dim=0 across the TP group by
``_sequence_parallel_context``
Expand All @@ -155,6 +160,8 @@ def capture(self, layer_id: int, topk_ids: torch.Tensor) -> None:
token_num_per_dp = int(num_tokens_dp[self.dp_rank].item())
total = int(num_tokens_dp.sum().item())
n = topk_ids.shape[0]
shard_sizes = getattr(ctx.dp_metadata, "local_sizes", None)
gathered_size = sum(shard_sizes) if shard_sizes is not None else -1

if n == total:
# Naive dispatch: all DP ranks' tokens concatenated
Expand All @@ -169,6 +176,17 @@ def capture(self, layer_id: int, topk_ids: torch.Tensor) -> None:
# rank's tokens, take the whole tensor.
start_loc = 0
end_loc = token_num_per_dp
elif shard_sizes is not None and n == gathered_size:
# Naive DP+EP dispatch gathers the sequence-parallel shards in
# flattened DP-then-TP order. ``local_sizes`` is the exact
# all-gatherv layout, including padding. Select this DP rank's
# contiguous shard group, then trim its trailing padding.
num_dp_ranks = len(num_tokens_dp)
assert len(shard_sizes) % num_dp_ranks == 0
shards_per_dp_rank = len(shard_sizes) // num_dp_ranks
first_shard = self.dp_rank * shards_per_dp_rank
start_loc = sum(shard_sizes[:first_shard])
end_loc = start_loc + token_num_per_dp
elif (
self.tp_size > 1
and n != token_num_per_dp
Expand Down Expand Up @@ -201,8 +219,8 @@ def capture(self, layer_id: int, topk_ids: torch.Tensor) -> None:
raise AssertionError(
"RoutedExpertsCapturer: unexpected topk_ids batch "
f"dim {n} (expected {total}, {token_num_per_dp}, "
f"or {sp_expected} for dp_rank={self.dp_rank}, "
f"tp_size={self.tp_size})"
f"{gathered_size}, or {sp_expected} for "
f"dp_rank={self.dp_rank}, tp_size={self.tp_size})"
)

# Defensive: model may expose more layers than the capture buffer
Expand Down
Loading