Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions release_notes/fragments/23418.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pr: 23418
entries:
- category: bugfix
text: >-
Dense multi-module MTP drafts and plain MTP drafts on 310P can start when
the target uses expert parallelism. The target keeps its expert settings.
- category: bugfix
text: >-
On Ascend 310P, plain MTP speculative decoding now honors the draft's
enforce_eager setting when using Model Runner V2.
45 changes: 44 additions & 1 deletion tests/ut/_310p/spec_decode/test_mtp_mrv2_310.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#
# Lean UTs for 310P MRv2 MTP (rejection offset, capture-safe draft step, RoPE flag).

import runpy
from types import SimpleNamespace
from unittest.mock import patch

import numpy as np
import pytest
import torch
from vllm.config.compilation import CUDAGraphMode
from vllm.config.compilation import CompilationMode, CUDAGraphMode

from tests.ut.base import TestBase
from vllm_ascend._310p.ops.rotary_embedding import AscendRotaryEmbedding310
Expand All @@ -22,6 +24,47 @@
)


def test_310_router_delegates_registered_mtp(monkeypatch):
from vllm.v1.worker.gpu.spec_decode import target_dependent_ar

from vllm_ascend.worker.v2 import model_runner, spec_decode

monkeypatch.setattr(target_dependent_ar, "speculator", SimpleNamespace())
monkeypatch.setattr(model_runner, "init_speculator", spec_decode.init_speculator)
with (
patch.object(spec_decode, "init_speculator") as shared_factory,
patch("vllm_ascend._310p.worker.v2.spec_decode.mtp_speculator.AscendMTPSpeculator310") as plain_mtp,
):
router = runpy.run_module("vllm_ascend.patch.worker.patch_spec_decode_310")["_init_speculator_310p"]
spec = SimpleNamespace(
method="mtp", custom_method="custom_mtp", use_gemma4_mtp=lambda: False, use_multi_module_mtp=lambda: False
)
config = SimpleNamespace(speculative_config=spec)
device, req_states = torch.device("cpu"), object()
assert router(config, device, req_states) is shared_factory.return_value
shared_factory.assert_called_once_with(config, device, req_states)
plain_mtp.assert_not_called()


@pytest.mark.parametrize("eager", [False, True])
def test_plain_mtp_310_draft_compilation_mode(eager):
from vllm_ascend.worker.v2.spec_decode.target_dependent_ar.speculator import AscendTargetDependentARSpeculator

target = SimpleNamespace(
speculative_config=SimpleNamespace(enforce_eager=eager),
compilation_config=SimpleNamespace(mode=CompilationMode.VLLM_COMPILE),
)
with patch.object(AscendTargetDependentARSpeculator, "__init__", autospec=True, return_value=None) as initialize:
AscendMTPSpeculator310(target, torch.device("cpu"))
draft = initialize.call_args.args[1]
if eager:
assert draft.compilation_config.mode == CompilationMode.NONE
assert draft.compilation_config is not target.compilation_config
else:
assert draft is target
assert target.compilation_config.mode == CompilationMode.VLLM_COMPILE


class TestMRv2Mtp310(TestBase):
def test_greedy_rejection_uses_logit_idx_plus_one(self):
# draft_sampled[logit_idx+1] must match argmax(logits[logit_idx]) to accept.
Expand Down
38 changes: 38 additions & 0 deletions tests/ut/worker/test_mtp_pcp_speculator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
from vllm.v1.worker.gpu.spec_decode.mtp.speculator import MTPSpeculator
from vllm.v1.worker.gpu.spec_decode.target_dependent_ar import speculator as target_speculator_module

from vllm_ascend._310p.worker.v2.spec_decode.mtp_speculator import AscendMTPSpeculator310
from vllm_ascend.attention.mla_v1 import AscendMLADecodeMetadata, AscendMLAMetadata
from vllm_ascend.patch.worker import patch_dspark
from vllm_ascend.platform import _validate_eplb_config
from vllm_ascend.worker.v2.spec_decode.eagle.speculator import AscendEagleSpeculator
from vllm_ascend.worker.v2.spec_decode.mtp.speculator import (
AscendMTPSpeculator,
)
from vllm_ascend.worker.v2.spec_decode.multi_module_mtp.speculator import AscendMultiModuleMTPSpeculator
from vllm_ascend.worker.v2.spec_decode.pcp_utils import build_dspark_draft_config
from vllm_ascend.worker.v2.spec_decode.target_dependent_ar import (
speculator as speculator_module,
Expand Down Expand Up @@ -276,6 +278,42 @@ def test_dspark_loader_reconstructs_physical_topology_then_exposes_query_view(ep
patch_dspark.dspark_utils.replace(parallel, prefill_context_parallel_size=1)


@pytest.mark.parametrize("speculator_cls", [AscendMultiModuleMTPSpeculator, AscendMTPSpeculator310])
@pytest.mark.parametrize("is_moe", [False, True], ids=["dense", "moe"])
def test_mtp_loader_uses_draft_expert_parallelism(eplb_worker_config, speculator_cls, is_moe):
target = eplb_worker_config
spec = target.speculative_config
draft = spec.draft_model_config
if not is_moe:
draft.model_arch_config = dataclass_replace(draft.model_arch_config, num_experts=0)
parallel = target.parallel_config
parallel.eplb_config.num_redundant_experts = 8
spec.draft_parallel_config = SpeculativeConfig.create_draft_parallel_config(
parallel, parallel.tensor_parallel_size, draft
)
assert spec.draft_parallel_config.enable_expert_parallel is is_moe
loader = object.__new__(speculator_cls)
loader.vllm_config = target
loader.speculative_config = spec
loader.draft_model_config = draft

loaded = loader._create_draft_vllm_config().parallel_config
assert loaded.enable_expert_parallel is is_moe
assert loaded.enable_eplb is is_moe
assert loaded.pipeline_parallel_size == 1
assert loaded.prefill_context_parallel_size == parallel.prefill_context_parallel_size == 8
assert parallel.enable_expert_parallel and parallel.enable_eplb
assert parallel.eplb_config.num_redundant_experts == 8
if not is_moe:
assert loaded.eplb_config.num_redundant_experts == 0
assert loaded.eplb_config is not parallel.eplb_config
# An invalid dense draft must still reach the upstream TP validator.
target.parallel_config = replace(parallel, tensor_parallel_size=2)
draft.model_arch_config = dataclass_replace(draft.model_arch_config, total_num_attention_heads=3)
with pytest.raises(ValueError, match="divisible by tensor parallel size"):
loader._create_draft_vllm_config()


def test_mtp_draft_config_keeps_target_pcp_topology(eplb_worker_config):
"""Sharded MTP drafts attend with the target's PCP partition (upstream #56107)."""
speculator = object.__new__(AscendMTPSpeculator)
Expand Down
19 changes: 14 additions & 5 deletions vllm_ascend/_310p/worker/v2/spec_decode/mtp_speculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import torch
import torch.nn as nn
from vllm.config import VllmConfig, replace
from vllm.config.compilation import CUDAGraphMode
from vllm.config.compilation import CompilationMode, CUDAGraphMode
from vllm.logger import logger
from vllm.v1.worker.gpu.attn_utils import build_slot_mappings_by_layer
from vllm.v1.worker.gpu.cudagraph_utils import BatchExecutionDescriptor
Expand All @@ -35,6 +35,7 @@
from vllm_ascend._310p.worker.v2.spec_utils import set_draft_step_host
from vllm_ascend.attention.attention_v1 import AscendAttentionState
from vllm_ascend.worker.v2.attn_utils import build_attn_metadata_factory
from vllm_ascend.worker.v2.spec_decode.mtp import get_mtp_draft_parallel_config
from vllm_ascend.worker.v2.spec_decode.target_dependent_ar.speculator import (
AscendTargetDependentARSpeculator,
)
Expand All @@ -43,6 +44,17 @@
class AscendMTPSpeculator310(AscendTargetDependentARSpeculator, MTPSpeculator):
"""Ascend MTP speculator for 310P MRv2 (Triton-free draft loop)."""

def __init__(self, vllm_config: VllmConfig, device: torch.device):
# The 310P router constructs plain MTP directly, before the shared
# factory applies the draft-only compilation mode.
spec = vllm_config.speculative_config
assert spec is not None
if spec.enforce_eager:
vllm_config = copy.copy(vllm_config)
vllm_config.compilation_config = copy.copy(vllm_config.compilation_config)
vllm_config.compilation_config.mode = CompilationMode.NONE
super().__init__(vllm_config, device)

def _create_draft_vllm_config(self) -> VllmConfig:
draft_model_config = self.speculative_config.draft_model_config
# Draft MTP ModelConfig often carries a *callable* hf_overrides composed by
Expand All @@ -55,10 +67,7 @@ def _create_draft_vllm_config(self) -> VllmConfig:
model_config_for_vllm = copy.copy(draft_model_config)
model_config_for_vllm.hf_overrides = {}

parallel_config = replace(
self.vllm_config.parallel_config,
pipeline_parallel_size=1,
)
parallel_config = get_mtp_draft_parallel_config(self.vllm_config)
draft_vllm_config = replace(
self.vllm_config,
model_config=model_config_for_vllm,
Expand Down
1 change: 1 addition & 0 deletions vllm_ascend/patch/worker/patch_spec_decode_310.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _init_speculator_310p(vllm_config, device, req_states=None):
assert speculative_config is not None
if (
speculative_config.method == "mtp"
and speculative_config.custom_method is None
and not speculative_config.use_gemma4_mtp()
and not speculative_config.use_multi_module_mtp()
):
Expand Down
23 changes: 23 additions & 0 deletions vllm_ascend/worker/v2/spec_decode/mtp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,26 @@
# limitations under the License.
# This file is a part of the vllm-ascend project.
#
from copy import copy

from vllm.config import ParallelConfig, VllmConfig, replace


def get_mtp_draft_parallel_config(vllm_config: VllmConfig) -> ParallelConfig:
"""Retain the target worker's topology and use the draft's resolved EP setting."""
target = vllm_config.parallel_config
spec = vllm_config.speculative_config
assert spec is not None and spec.draft_parallel_config is not None
draft = spec.draft_parallel_config
if target.enable_expert_parallel and not draft.enable_expert_parallel:
# A dense draft has no experts to balance. A target's redundant expert
# count or elastic EP flag would also fail ParallelConfig validation.
return replace(
target,
pipeline_parallel_size=1,
enable_expert_parallel=False,
enable_eplb=False,
enable_elastic_ep=False,
eplb_config=copy(draft.eplb_config),
)
return replace(target, pipeline_parallel_size=1)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
build_attn_metadata_wrapper,
)
from vllm_ascend.worker.v2.input_batch import AscendInputBatch, AscendInputBuffers
from vllm_ascend.worker.v2.spec_decode.mtp import get_mtp_draft_parallel_config
from vllm_ascend.worker.v2.spec_decode.target_dependent_ar.aclgraph import (
AutoRegressiveAclGraphManager,
)
Expand Down Expand Up @@ -91,10 +92,7 @@ def load_model(self, target_model: torch.nn.Module) -> None:
super().load_model(target_model)

def _create_draft_vllm_config(self) -> VllmConfig:
parallel_config = replace(
self.vllm_config.parallel_config,
pipeline_parallel_size=1,
)
parallel_config = get_mtp_draft_parallel_config(self.vllm_config)
draft_vllm_config = replace(
self.vllm_config,
parallel_config=parallel_config,
Expand Down
Loading