[EPLB] Add EPLB support to dspark draft models (DeepSeekV4) (#57549)
Signed-off-by: Markov Ilya <markovilya197@gmail.com>
This commit is contained in:
@@ -323,39 +323,47 @@ class EplbState:
|
||||
"""
|
||||
if len(self.model_states) > 0:
|
||||
model = next(iter(self.model_states.values())).model
|
||||
if (
|
||||
model.num_routed_experts != new_model.num_routed_experts
|
||||
or model.num_redundant_experts != new_model.num_redundant_experts
|
||||
or model.num_physical_experts != new_model.num_physical_experts
|
||||
or model.num_logical_experts != new_model.num_logical_experts
|
||||
or model.num_expert_groups != new_model.num_expert_groups
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Model: {} "
|
||||
"with config {} "
|
||||
"{} {} {} {} "
|
||||
"mismatch with new model {} "
|
||||
"with config {} "
|
||||
"{} {} {} {}".format(
|
||||
type(model),
|
||||
model.num_routed_experts,
|
||||
model.num_redundant_experts,
|
||||
model.num_physical_experts,
|
||||
model.num_logical_experts,
|
||||
model.num_expert_groups,
|
||||
type(new_model),
|
||||
new_model.num_routed_experts,
|
||||
new_model.num_redundant_experts,
|
||||
new_model.num_physical_experts,
|
||||
new_model.num_logical_experts,
|
||||
new_model.num_expert_groups,
|
||||
)
|
||||
self.assert_confs_equal(model, new_model)
|
||||
|
||||
@staticmethod
|
||||
def assert_confs_equal(
|
||||
model: MixtureOfExperts, new_model: MixtureOfExperts
|
||||
) -> None:
|
||||
"""Raise if the models have different expert parallel configurations."""
|
||||
if (
|
||||
model.num_routed_experts != new_model.num_routed_experts
|
||||
or model.num_redundant_experts != new_model.num_redundant_experts
|
||||
or model.num_physical_experts != new_model.num_physical_experts
|
||||
or model.num_logical_experts != new_model.num_logical_experts
|
||||
or model.num_expert_groups != new_model.num_expert_groups
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Model: {} "
|
||||
"with config {} "
|
||||
"{} {} {} {} "
|
||||
"mismatch with new model {} "
|
||||
"with config {} "
|
||||
"{} {} {} {}".format(
|
||||
type(model),
|
||||
model.num_routed_experts,
|
||||
model.num_redundant_experts,
|
||||
model.num_physical_experts,
|
||||
model.num_logical_experts,
|
||||
model.num_expert_groups,
|
||||
type(new_model),
|
||||
new_model.num_routed_experts,
|
||||
new_model.num_redundant_experts,
|
||||
new_model.num_physical_experts,
|
||||
new_model.num_logical_experts,
|
||||
new_model.num_expert_groups,
|
||||
)
|
||||
)
|
||||
|
||||
def add_model(
|
||||
self,
|
||||
model: MixtureOfExperts,
|
||||
model_config: ModelConfig,
|
||||
model_name: str | None = None,
|
||||
):
|
||||
"""Build the initial EPLB state."""
|
||||
self.validate_ep_configuration(model)
|
||||
@@ -506,7 +514,7 @@ class EplbState:
|
||||
dtype=torch.int64,
|
||||
device=self.device,
|
||||
),
|
||||
model_name=model_config.model,
|
||||
model_name=model_name or model_config.model,
|
||||
model=model,
|
||||
expert_buffer=expert_buffer,
|
||||
rebalanced=False,
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Shared EPLB MoE registration helpers for DeepSeek V4 family models."""
|
||||
|
||||
from collections.abc import Iterable
|
||||
from typing import Any
|
||||
|
||||
import torch.nn as nn
|
||||
|
||||
from vllm.config import ModelConfig
|
||||
|
||||
|
||||
def dspark_draft_supports_eplb(draft_model_config: ModelConfig) -> bool:
|
||||
"""Return whether a DSpark draft can share EPLB state with the target.
|
||||
|
||||
Only DeepSeek-V4 DSpark drafts reuse the target expert layout. V4.1 drafts
|
||||
use a smaller routed-expert count and cannot share EPLB state.
|
||||
"""
|
||||
return getattr(draft_model_config.hf_config, "model_type", None) == "deepseek_v4"
|
||||
|
||||
|
||||
def collect_moe_layers(
|
||||
moe_model: Any,
|
||||
layers: Iterable[nn.Module],
|
||||
config: object,
|
||||
*,
|
||||
decoder_layer_type: type[nn.Module],
|
||||
moe_type: type[nn.Module],
|
||||
) -> None:
|
||||
"""Populate ``MixtureOfExperts`` fields from decoder layers for EPLB.
|
||||
|
||||
Works for both target backbones and DSpark draft stacks. Draft models pass
|
||||
their shorter ``model.layers`` list; pipeline-parallel placeholder layers
|
||||
are skipped via ``decoder_layer_type``.
|
||||
"""
|
||||
moe_model.num_expert_groups = getattr(config, "n_group", 1)
|
||||
moe_model.moe_layers = []
|
||||
moe_model.moe_mlp_layers = []
|
||||
example_moe = None
|
||||
for layer in layers:
|
||||
if not isinstance(layer, decoder_layer_type):
|
||||
continue
|
||||
if not isinstance(layer.ffn, moe_type):
|
||||
continue
|
||||
example_moe = layer.ffn
|
||||
moe_model.moe_mlp_layers.append(layer.ffn)
|
||||
moe_model.moe_layers.append(layer.ffn.experts)
|
||||
|
||||
moe_model.num_moe_layers = len(moe_model.moe_layers)
|
||||
moe_model.extract_moe_parameters(example_moe)
|
||||
@@ -52,11 +52,14 @@ from vllm.models.common.ops.sequence_parallel import (
|
||||
sp_padding_mask,
|
||||
sp_shard,
|
||||
)
|
||||
from vllm.models.deepseek_v4.common.eplb_util import collect_moe_layers
|
||||
from vllm.models.deepseek_v4.common.mm_preprocess import IMAGE_SENTINEL_BASE_ID
|
||||
|
||||
from .model import (
|
||||
DeepseekV4DecoderLayer,
|
||||
DeepseekV4MixtureOfExperts,
|
||||
DeepseekV4Model,
|
||||
DeepseekV4MoE,
|
||||
_use_sequence_parallel,
|
||||
make_deepseek_v4_expert_params_mapping,
|
||||
prepare_mega_gate_routing_metadata,
|
||||
@@ -346,7 +349,7 @@ def _insert_context_kv(
|
||||
)
|
||||
|
||||
|
||||
class DSparkDeepseekV4ForCausalLM(nn.Module):
|
||||
class DSparkDeepseekV4ForCausalLM(nn.Module, DeepseekV4MixtureOfExperts):
|
||||
# Draft weights ship in the target checkpoint (mtp.*) without embed/head, so
|
||||
# load_dspark_model always aliases the target's.
|
||||
has_own_embed_tokens = False
|
||||
@@ -373,6 +376,16 @@ class DSparkDeepseekV4ForCausalLM(nn.Module):
|
||||
prefix=maybe_prefix(prefix, "lm_head"),
|
||||
)
|
||||
self.logits_processor = LogitsProcessor(self.config.vocab_size)
|
||||
self.set_moe_parameters()
|
||||
|
||||
def set_moe_parameters(self) -> None:
|
||||
collect_moe_layers(
|
||||
self,
|
||||
self.model.layers,
|
||||
self.config,
|
||||
decoder_layer_type=DeepseekV4DecoderLayer,
|
||||
moe_type=DeepseekV4MoE,
|
||||
)
|
||||
|
||||
# --- Hooks used by the speculator -------------------------------------
|
||||
|
||||
@@ -451,6 +464,7 @@ class DSparkDeepseekV4ForCausalLM(nn.Module):
|
||||
ckpt_down_proj_name="w2",
|
||||
ckpt_up_proj_name="w3",
|
||||
num_experts=self.config.n_routed_experts,
|
||||
num_redundant_experts=first_layer.ffn.n_redundant_experts,
|
||||
)
|
||||
expert_scale_suffix = (
|
||||
".weight_scale"
|
||||
|
||||
@@ -81,6 +81,7 @@ from vllm.models.common.ops.sequence_parallel import (
|
||||
sp_shard,
|
||||
)
|
||||
from vllm.models.deepseek_v4.attention import DeepseekV4Attention
|
||||
from vllm.models.deepseek_v4.common.eplb_util import collect_moe_layers
|
||||
from vllm.models.deepseek_v4.nvidia.flashinfer_sparse import (
|
||||
DeepseekV4FlashInferMLAAttention,
|
||||
DeepseekV4FlashInferSM120Attention,
|
||||
@@ -1841,23 +1842,13 @@ class DeepseekV4ForCausalLM(
|
||||
self.set_moe_parameters()
|
||||
|
||||
def set_moe_parameters(self) -> None:
|
||||
self.num_expert_groups = getattr(self.config, "n_group", 1)
|
||||
self.num_moe_layers = self.config.num_hidden_layers
|
||||
self.moe_layers: list[nn.Module] = []
|
||||
self.moe_mlp_layers: list[DeepseekV4MoE] = []
|
||||
example_moe: DeepseekV4MoE | None = None
|
||||
for layer in self.model.layers:
|
||||
if isinstance(layer, PPMissingLayer):
|
||||
continue
|
||||
if not isinstance(layer, DeepseekV4DecoderLayer):
|
||||
continue
|
||||
if isinstance(layer.ffn, DeepseekV4MoE):
|
||||
example_moe = layer.ffn
|
||||
self.moe_mlp_layers.append(layer.ffn)
|
||||
self.moe_layers.append(layer.ffn.experts)
|
||||
|
||||
self.num_moe_layers = len(self.moe_layers)
|
||||
self.extract_moe_parameters(example_moe)
|
||||
collect_moe_layers(
|
||||
self,
|
||||
self.model.layers,
|
||||
self.config,
|
||||
decoder_layer_type=DeepseekV4DecoderLayer,
|
||||
moe_type=DeepseekV4MoE,
|
||||
)
|
||||
|
||||
def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor:
|
||||
return self.model.embed_input_ids(input_ids)
|
||||
|
||||
@@ -13,8 +13,10 @@ from vllm.config import ModelConfig
|
||||
from vllm.distributed.eplb.eplb_state import EplbState
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.models.interfaces import (
|
||||
MixtureOfExperts,
|
||||
get_mixture_of_experts_model,
|
||||
)
|
||||
from vllm.models.deepseek_v4.common.eplb_util import dspark_draft_supports_eplb
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.v1.worker.gpu.model_runner import GPUModelRunner
|
||||
@@ -22,6 +24,25 @@ if TYPE_CHECKING:
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def eplb_draft_model_name(model_config: ModelConfig) -> str:
|
||||
"""Return the EPLB step-log display name for a draft model."""
|
||||
return f"{model_config.model} (draft)"
|
||||
|
||||
|
||||
def draft_model_supports_eplb(
|
||||
speculative_config: Any | None,
|
||||
draft_moe_model: MixtureOfExperts | None,
|
||||
) -> bool:
|
||||
"""Return whether a draft MoE model should register with EPLB."""
|
||||
if draft_moe_model is None:
|
||||
return False
|
||||
if speculative_config is None or speculative_config.draft_model_config is None:
|
||||
return False
|
||||
if getattr(speculative_config, "method", None) == "dspark":
|
||||
return dspark_draft_supports_eplb(speculative_config.draft_model_config)
|
||||
return True
|
||||
|
||||
|
||||
def step_eplb_after(*, is_dummy: bool = False) -> Callable:
|
||||
"""Step EPLB after a model runner method completes successfully."""
|
||||
|
||||
@@ -93,16 +114,26 @@ class EPLBController:
|
||||
|
||||
draft_model = speculator.model
|
||||
draft_moe_model = get_mixture_of_experts_model(draft_model)
|
||||
if draft_moe_model is None:
|
||||
if not draft_model_supports_eplb(speculative_config, draft_moe_model):
|
||||
return False
|
||||
assert draft_moe_model is not None
|
||||
|
||||
assert not self.parallel_config.enable_elastic_ep, (
|
||||
"Elastic EP is not supported with draft model."
|
||||
)
|
||||
assert speculative_config is not None
|
||||
assert speculative_config.draft_model_config is not None
|
||||
draft_model_config = speculative_config.draft_model_config
|
||||
assert draft_model_config is not None
|
||||
logger.info_once(
|
||||
"EPLB is enabled for MoE part of drafter model %s.",
|
||||
draft_model_config.model,
|
||||
)
|
||||
assert self.state is not None
|
||||
self.state.add_model(draft_moe_model, speculative_config.draft_model_config)
|
||||
self.state.add_model(
|
||||
draft_moe_model,
|
||||
draft_model_config,
|
||||
model_name=eplb_draft_model_name(draft_model_config),
|
||||
)
|
||||
speculator.set_eplb_state(self.state)
|
||||
self._has_registered_models = True
|
||||
return True
|
||||
|
||||
@@ -6,6 +6,7 @@ import torch.nn as nn
|
||||
from vllm.config import ModelConfig, ParallelConfig, VllmConfig, replace
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.model_loader.utils import get_draft_load_config
|
||||
from vllm.models.deepseek_v4.common.eplb_util import dspark_draft_supports_eplb
|
||||
from vllm.v1.attention.backends.registry import AttentionBackendEnum
|
||||
from vllm.v1.worker.gpu.spec_decode.utils import get_pp_safe_draft_load_config
|
||||
|
||||
@@ -35,11 +36,24 @@ def _resolve_dspark_attention_backend(
|
||||
def _get_dspark_parallel_config(
|
||||
parallel_config: ParallelConfig,
|
||||
tensor_parallel_size: int,
|
||||
draft_model_config: ModelConfig,
|
||||
) -> ParallelConfig:
|
||||
if parallel_config.enable_eplb:
|
||||
enable_draft_eplb = parallel_config.enable_eplb and dspark_draft_supports_eplb(
|
||||
draft_model_config
|
||||
)
|
||||
if parallel_config.enable_eplb and not enable_draft_eplb:
|
||||
logger.warning_once(
|
||||
"EPLB is disabled for the DSpark draft model. EPLB remains enabled "
|
||||
"for the target model."
|
||||
"EPLB is disabled for the DSpark draft model (%s). EPLB remains "
|
||||
"enabled for the target model.",
|
||||
draft_model_config.hf_config.model_type,
|
||||
)
|
||||
|
||||
if enable_draft_eplb:
|
||||
return replace(
|
||||
parallel_config,
|
||||
pipeline_parallel_size=1,
|
||||
tensor_parallel_size=tensor_parallel_size,
|
||||
enable_elastic_ep=False,
|
||||
)
|
||||
|
||||
return replace(
|
||||
@@ -81,6 +95,7 @@ def load_dspark_model(target_model: nn.Module, vllm_config: VllmConfig) -> nn.Mo
|
||||
parallel_config=_get_dspark_parallel_config(
|
||||
vllm_config.parallel_config,
|
||||
speculative_config.draft_parallel_config.tensor_parallel_size,
|
||||
draft_model_config,
|
||||
),
|
||||
attention_config=replace(
|
||||
vllm_config.attention_config,
|
||||
|
||||
@@ -206,6 +206,10 @@ from vllm.v1.worker.cp_utils import (
|
||||
)
|
||||
from vllm.v1.worker.dp_utils import coordinate_batch_across_dp
|
||||
from vllm.v1.worker.ec_connector_model_runner_mixin import ECConnectorModelRunnerMixin
|
||||
from vllm.v1.worker.gpu.eplb_utils import (
|
||||
draft_model_supports_eplb,
|
||||
eplb_draft_model_name,
|
||||
)
|
||||
from vllm.v1.worker.gpu_input_batch import CachedRequestState, InputBatch
|
||||
from vllm.v1.worker.gpu_ubatch_wrapper import UBatchWrapper
|
||||
from vllm.v1.worker.kv_connector_model_runner_mixin import KVConnectorModelRunnerMixin
|
||||
@@ -5237,25 +5241,34 @@ class GPUModelRunner(
|
||||
logger.info_once("Loading drafter model...")
|
||||
if hasattr(self.drafter, "load_model"):
|
||||
self.drafter.load_model(self.model)
|
||||
drafter_moe_model = None
|
||||
if self.parallel_config.enable_eplb and hasattr(
|
||||
self.drafter, "model"
|
||||
):
|
||||
drafter_moe_model = get_mixture_of_experts_model(
|
||||
self.drafter.model
|
||||
)
|
||||
if not draft_model_supports_eplb(
|
||||
self.vllm_config.speculative_config,
|
||||
drafter_moe_model,
|
||||
):
|
||||
drafter_moe_model = None
|
||||
|
||||
if (
|
||||
self.parallel_config.enable_eplb
|
||||
and hasattr(self.drafter, "model")
|
||||
and (
|
||||
drafter_moe_model := get_mixture_of_experts_model(
|
||||
self.drafter.model
|
||||
)
|
||||
)
|
||||
is not None
|
||||
and drafter_moe_model is not None
|
||||
):
|
||||
assert not self.parallel_config.enable_elastic_ep, (
|
||||
"Elastic EP is not supported with drafter model."
|
||||
)
|
||||
spec_config = self.vllm_config.speculative_config
|
||||
assert spec_config is not None
|
||||
assert spec_config.draft_model_config is not None
|
||||
draft_model_config = spec_config.draft_model_config
|
||||
assert draft_model_config is not None
|
||||
logger.info_once(
|
||||
"EPLB is enabled for MoE part of drafter model %s.",
|
||||
spec_config.draft_model_config.model,
|
||||
draft_model_config.model,
|
||||
)
|
||||
if self.eplb_state is None:
|
||||
self.eplb_state = EplbState(
|
||||
@@ -5263,7 +5276,8 @@ class GPUModelRunner(
|
||||
)
|
||||
self.eplb_state.add_model(
|
||||
drafter_moe_model,
|
||||
spec_config.draft_model_config,
|
||||
draft_model_config,
|
||||
model_name=eplb_draft_model_name(draft_model_config),
|
||||
)
|
||||
assert hasattr(self.drafter, "set_eplb_state")
|
||||
self.drafter.set_eplb_state(self.eplb_state)
|
||||
|
||||
Reference in New Issue
Block a user