[Model] Support R3 capture with DeepGEMM MegaMoE (#51831)

Signed-off-by: aoshen02 <aoshen@inferact.ai>
This commit is contained in:
aoshen02
2026-08-12 01:07:07 -07:00
committed by GitHub
parent 10b7766a90
commit 4eef91c03c
4 changed files with 82 additions and 0 deletions
@@ -6,6 +6,9 @@
from __future__ import annotations
import logging
from collections.abc import Callable
from functools import partial
from typing import Protocol, runtime_checkable
import numpy as np
import torch
@@ -20,6 +23,12 @@ from vllm.v1.outputs import RoutedExpertsTensors
logger = logging.getLogger(__name__)
@runtime_checkable
class RoutedExpertsCaptureSource(Protocol):
layer_id: int
capture_fn: Callable[[torch.Tensor], None] | None
def _get_routed_experts_shape(vllm_config: VllmConfig) -> tuple[int, int, int]:
model_config = vllm_config.model_config
num_layers = model_config.get_total_num_hidden_layers()
@@ -234,6 +243,10 @@ def bind_routed_experts_capturer(
num_bound = 0
for module in model.modules():
if isinstance(module, RoutedExpertsCaptureSource):
module.capture_fn = partial(capturer.capture, module.layer_id)
num_bound += 1
continue
if not isinstance(module, MoERunner):
continue
layer_id = module.layer_id
+8
View File
@@ -183,6 +183,7 @@ class DeepseekV4MegaMoEExperts(nn.Module):
):
super().__init__()
self.prefix = prefix
self.capture_fn: Callable[[torch.Tensor], None] | None = None
self.num_experts = num_experts
self.num_local_experts = num_local_experts
self.experts_start_idx = experts_start_idx
@@ -436,6 +437,10 @@ class DeepseekV4MegaMoEExperts(nn.Module):
def update_expert_map(self) -> None:
pass
@property
def layer_id(self) -> int:
return extract_layer_index(self.prefix)
def forward(
self,
hidden_states: torch.Tensor,
@@ -464,6 +469,9 @@ class DeepseekV4MegaMoEExperts(nn.Module):
if is_padding is not None:
is_padding = is_padding[:num_tokens]
if self.capture_fn is not None:
self.capture_fn(topk_ids)
# EPLB: map logical expert IDs to physical replicas and record load.
eplb_state = self.eplb_state
if eplb_state.logical_to_physical_map is not None:
+3
View File
@@ -383,6 +383,9 @@ class KimiK3MegaMoEExperts(DeepseekV4MegaMoEExperts):
if is_padding is not None:
is_padding = is_padding[:num_tokens]
if self.capture_fn is not None:
self.capture_fn(topk_ids)
eplb_state = self.eplb_state
if eplb_state.logical_to_physical_map is not None:
assert eplb_state.expert_load_view is not None