[RFC] Replace shared-memory routed experts with ModelRunnerOutput transfer and HTTP support (#39568)
Signed-off-by: xhx1022 <1737006628@qq.com> Signed-off-by: arlenxu <arlenxu@tencent.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: arlenxu <arlenxu@tencent.com> Co-authored-by: Junjie Zhang <junj.jay.zhang@gmail.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
arlenxu
Junjie Zhang
parent
2317682f95
commit
c7560af424
@@ -109,6 +109,73 @@ class LogprobsTensors(NamedTuple):
|
||||
)
|
||||
|
||||
|
||||
class RoutedExpertsTensors(NamedTuple):
|
||||
"""Device-side snapshot of routed experts data, pending async D2H.
|
||||
|
||||
Produced by :class:`GPUModelRunner` at the end of each async-scheduled
|
||||
step. The copy stream waits on the default stream, then issues
|
||||
non-blocking D2H via :meth:`to_cpu_nonblocking` into a pinned CPU
|
||||
buffer; :class:`AsyncGPUModelRunnerOutput.get_output` synchronizes
|
||||
the copy before the scheduler reads it.
|
||||
|
||||
Sliced to ``total_num_scheduled_tokens`` (step-level, across all
|
||||
requests — NOT per-request). Both ``routing_data`` and
|
||||
``slot_mapping`` must be private clones when sourced from shared
|
||||
capturer / prepare-input buffers, so the next forward pass /
|
||||
``_prepare_inputs`` on the default stream does not race with a
|
||||
D2H still pending on the copy stream.
|
||||
"""
|
||||
|
||||
# (num_scheduled_tokens, num_layers, num_experts_per_tok)
|
||||
routing_data: torch.Tensor
|
||||
# (num_scheduled_tokens,)
|
||||
slot_mapping: torch.Tensor
|
||||
|
||||
def to_cpu_nonblocking(self) -> "RoutedExpertsTensors":
|
||||
"""Issue non-blocking D2H on the current stream.
|
||||
|
||||
NOTE: ``non_blocking=True`` only delivers true overlap when the
|
||||
CPU target is pinned. The current fallback here allocates a
|
||||
new pageable CPU tensor per call, which silently degrades to a
|
||||
synchronous copy; acceptable because the sync happens on the
|
||||
dedicated copy stream, not the default stream.
|
||||
"""
|
||||
if self.routing_data.device.type == "cpu":
|
||||
return self
|
||||
return RoutedExpertsTensors(
|
||||
self.routing_data.to("cpu", non_blocking=True),
|
||||
self.slot_mapping.to("cpu", non_blocking=True),
|
||||
)
|
||||
|
||||
def tolists(self) -> "RoutedExpertsLists":
|
||||
"""Convert to the numpy-backed form consumed by the scheduler.
|
||||
|
||||
``.cpu()`` is a no-op when the tensor is already on CPU, so this
|
||||
is cheap for the post-D2H case; for raw device tensors it will
|
||||
synchronously block, which is only reached in tests.
|
||||
"""
|
||||
return RoutedExpertsLists(
|
||||
self.routing_data.cpu().numpy(),
|
||||
self.slot_mapping.cpu().numpy(),
|
||||
)
|
||||
|
||||
|
||||
class RoutedExpertsLists(NamedTuple):
|
||||
"""CPU-side routed experts, the form :meth:`RoutedExpertsManager.store_batch`
|
||||
consumes.
|
||||
|
||||
Batched per scheduler step: the leading dim is the number of tokens
|
||||
scheduled across all requests in this step (``total_num_scheduled_tokens``),
|
||||
not per-request tokens. ``slot_mapping[i]`` tells the scheduler which
|
||||
physical KV-cache slot row ``i`` of ``routing_data`` belongs to.
|
||||
"""
|
||||
|
||||
# (num_scheduled_tokens, num_layers, num_experts_per_tok)
|
||||
routing_data: np.ndarray
|
||||
# (num_scheduled_tokens,)
|
||||
slot_mapping: np.ndarray
|
||||
|
||||
|
||||
# [num_reqs, <dynamic>]
|
||||
# The shape of each element depends on the pooler used
|
||||
PoolerOutput: TypeAlias = torch.Tensor | list[torch.Tensor] | list[torch.Tensor | None]
|
||||
@@ -201,6 +268,17 @@ class ModelRunnerOutput:
|
||||
# information related to cudagraph execution
|
||||
cudagraph_stats: CUDAGraphStat | None = None
|
||||
|
||||
# Per-step routed experts data captured by the worker.
|
||||
# ``routing_data`` shape: (num_scheduled_tokens, num_layers,
|
||||
# num_experts_per_tok); expert IDs as uint8/uint16.
|
||||
# ``slot_mapping`` shape: (num_scheduled_tokens,); physical KV-cache
|
||||
# slot for each row of routing_data.
|
||||
# ``num_scheduled_tokens`` is step-level (total across all requests
|
||||
# in this step), not per-request. The scheduler persists this into
|
||||
# its slot buffer via ``slot_buffer[slot_mapping] = routing_data``.
|
||||
# ``None`` when ``enable_return_routed_experts`` is off.
|
||||
routed_experts: RoutedExpertsLists | None = None
|
||||
|
||||
|
||||
# ModelRunnerOutput wrapper for async scheduling.
|
||||
class AsyncModelRunnerOutput(ABC):
|
||||
|
||||
Reference in New Issue
Block a user