Skip to content

[BugFix][SpecDecode] Honor 310P MTP eager mode and preserve draft EP settings - #23418

Closed
zhenwenqi2024 wants to merge 4 commits into
vllm-project:mainfrom
zhenwenqi2024:refactor/spec-draft-dispatch
Closed

zhenwenqi2024 wants to merge 4 commits into
vllm-project:mainfrom
zhenwenqi2024:refactor/spec-draft-dispatch

Conversation

@zhenwenqi2024

@zhenwenqi2024 zhenwenqi2024 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it?

Two Ascend MTP setup paths still need local handling in Model Runner V2.

  • The 310P wrapper constructs plain MTP directly. Give that draft its own compilation config when enforce_eager is set. Delegate registered MTP-based methods to the existing shared factory with their request state.
  • The 310P and multi-module MTP loaders reconstruct a VllmConfig from the target worker's parallel config. This can re-enable EP for a dense draft and fail upstream model validation at startup. Honor the EP decision from upstream's draft_parallel_config in both loaders. Preserve the worker topology and clear target-only EPLB, redundant-expert and elastic-EP settings for the dense draft.

#22804 owns the shared speculator dispatch and the DFlash2 declaration. This revision applies directly to main after #23075 and uses upstream model validation.

Does this PR introduce any user-facing change?

Yes. On 310P, plain MTP honors draft eager mode without changing the target's compilation mode. A dense draft in either affected loader can pass EP validation when the target uses EP. MoE drafts retain their target worker's expert settings.

How was this patch tested?

Ran these CPU unit tests on Linux at 5f20134a4 with vLLM v0.34.0 and c410543d9. Each run passed all 92 cases.

TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -q --tb=short \
  tests/ut/_310p/spec_decode/test_mtp_mrv2_310.py \
  tests/ut/worker/test_mtp_pcp_speculator_v2.py \
  tests/ut/worker/v2/test_spec_decode.py
  • Applying just the seven new regression cases to Ascend main at 5da436cf7 reproduced four failures. They covered registered MTP routing on 310P, draft eager mode on 310P and dense-draft EP validation in both loaders.
  • The same three files passed all 109 cases with [Refactor][SpecDecode] Align MRV2 init_speculator dispatch with upstream vLLM #22804 at 6b05e4580 applied alongside this change. The tests exercise dense and MoE drafts, target EPLB with redundant experts, preserved PCP topology and upstream rejection of an invalid TP configuration.
  • Ruff lint and formatting passed on all six changed Python files. The repository-wide lint run stopped while the Windows runner was downloading the Go toolchain for actionlint. 310P device execution and end-to-end inference remain unverified.

Checklist

Testing

  • Boundary / edge-case coverage
  • Regression test
  • Error / failure path coverage
  • Meaningful & reliable tests
  • Performance impact (startup configuration only)

AI assistance was used for implementation and validation.

Problem
MRV2 duplicates upstream draft selection and 310P reroutes MTP in a
second factory. Dense drafts inheriting target EP skip all parallel
checks, including tensor and decode context parallelism.

Fix
Map the resolved speculator class to its NPU implementation, declare
DFlash2 on the Ascend draft model, and override only plain MTP on 310P.
Validate the effective draft PP and EP settings on a copy so upstream
still checks the rest of the configuration.

Validation
All 200 tests passed on both v0.34.0 and c410543d9, A3 passed n-gram
generation and DFlash2 initialization, and targeted upstream main checks
passed while full collection remains blocked by the KVCacheGroupSpec import.

Signed-off-by: zhenwenqi2024 <zhenwenqi_2022@qq.com>
Keep the DSpark tests from main after vllm-project#23075 removed the legacy EP bypass.

Signed-off-by: zhenwenqi2024 <zhenwenqi_2022@qq.com>
Signed-off-by: zhenwenqi2024 <zhenwenqi_2022@qq.com>
@github-actions

Copy link
Copy Markdown
Contributor

👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:‌‌

  • A PR should do only one thing, smaller PRs enable faster reviews.
  • Every PR should include unit tests and end-to-end tests ‌to ensure it works and is not broken by other future PRs.
  • Write the commit message by fulfilling the PR description to help reviewer and future developers understand.

If CI fails, you can run linting and testing checks locally according Contributing and Testing.

🤖 How to trigger CI

CI runs automatically when the PR is opened or reopened. Pushing a new commit no longer re-runs the E2E CI (to save NPU runner resources), so use the comment commands below when needed. They are available to the PR author and users with triage permission or above:

  • /run — run the full PR CI gate on the latest commit.
  • /rerun — re-run the failed CI jobs (e.g. after a flaky failure).
  • /e2e — run selected E2E tests instead of the full gate.
  • /nightly / /presmoke / /weekly — run the corresponding large-scale tests (triage permission or above; blocked during the nightly maintenance window).
  • /cancel — cancel a running CI on this PR.

Note

Until the CI gate has run on a commit, the required ci-gate check stays pending and the PR cannot merge — remember to comment /run after pushing.


Tip

💡 Consider Linking a Related Issue or RFC

Your PR title contains the [BugFix] tag, indicating a bug fix or new feature.

Linking a related issue or RFC in the PR description is strongly encouraged — it gives reviewers helpful context and speeds up the review. You can use any of these keywords:

  • Fixes #<issue_number>
  • Closes #<issue_number>
  • Resolves #<issue_number>
  • Refs #<rfc_or_issue_number> (for RFCs)

🙏 Thanks for helping us keep the project well-organized!

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request refactors the speculator initialization and dispatch mechanism in vllm-ascend to align with upstream vLLM by mapping speculator config classes directly to their Ascend implementations, replacing hardcoded conditional checks. It also updates related unit tests and refactors parallel configuration validation for dense drafts. The review feedback focuses on enhancing backward compatibility and robustness across different vLLM versions by wrapping speculator config imports in try-except blocks and defensively using getattr to prevent potential ImportError and AttributeError exceptions.

Comment on lines +24 to +53
from vllm.v1.worker.gpu.spec_decode.dflash.config import DFlashSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.dflash2.config import DFlash2SpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.dspark.config import DSparkSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.eagle.config import EagleSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.extract_hidden_states_config import ExtractHiddenStatesSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.gemma4.config import Gemma4MTPSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.lilicorr.config import LiLiCorrSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.mtp.config import MTPSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.multi_module_mtp.config import MultiModuleMTPSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.ngram.config import NgramSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.standalone_ar.config import StandaloneARSpeculatorConfig

if TYPE_CHECKING:
from vllm.v1.worker.gpu.states import RequestState


# Keep backend imports lazy. The draft checkpoint and method are resolved by vLLM.
_ASCEND_SPECULATORS = {
DFlashSpeculatorConfig.speculator_class: f"{__name__}.dflash.speculator.AscendDFlashSpeculator",
DFlash2SpeculatorConfig.speculator_class: f"{__name__}.dflash2.speculator.AscendDFlash2Speculator",
DSparkSpeculatorConfig.speculator_class: f"{__name__}.dspark.speculator.AscendDSparkSpeculator",
EagleSpeculatorConfig.speculator_class: f"{__name__}.eagle.speculator.AscendEagleSpeculator",
Gemma4MTPSpeculatorConfig.speculator_class: f"{__name__}.gemma4.speculator.AscendGemma4Speculator",
LiLiCorrSpeculatorConfig.speculator_class: f"{__name__}.lilicorr.speculator.AscendLiLiCorrSpeculator",
MTPSpeculatorConfig.speculator_class: f"{__name__}.mtp.speculator.AscendMTPSpeculator",
MultiModuleMTPSpeculatorConfig.speculator_class: (
f"{__name__}.multi_module_mtp.speculator.AscendMultiModuleMTPSpeculator"
),
StandaloneARSpeculatorConfig.speculator_class: f"{__name__}.standalone_ar.speculator.AscendStandaloneARSpeculator",
}

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.

high

Unconditional imports of speculator config classes from vLLM can cause ImportError on older or different vLLM versions where some of these speculator configs might not exist. Wrapping these imports and registrations in try-except blocks ensures backward and forward compatibility across different vLLM versions.

_ASCEND_SPECULATORS = {}

def _register(config_import_path: str, config_class_name: str, impl_path: str):
    try:
        mod = __import__(config_import_path, fromlist=[config_class_name])
        config_cls = getattr(mod, config_class_name)
        _ASCEND_SPECULATORS[config_cls.speculator_class] = impl_path
    except (ImportError, AttributeError):
        pass

_register("vllm.v1.worker.gpu.spec_decode.dflash.config", "DFlashSpeculatorConfig", f"{__name__}.dflash.speculator.AscendDFlashSpeculator")
_register("vllm.v1.worker.gpu.spec_decode.dflash2.config", "DFlash2SpeculatorConfig", f"{__name__}.dflash2.speculator.AscendDFlash2Speculator")
_register("vllm.v1.worker.gpu.spec_decode.dspark.config", "DSparkSpeculatorConfig", f"{__name__}.dspark.speculator.AscendDSparkSpeculator")
_register("vllm.v1.worker.gpu.spec_decode.eagle.config", "EagleSpeculatorConfig", f"{__name__}.eagle.speculator.AscendEagleSpeculator")
_register("vllm.v1.worker.gpu.spec_decode.gemma4.config", "Gemma4MTPSpeculatorConfig", f"{__name__}.gemma4.speculator.AscendGemma4Speculator")
_register("vllm.v1.worker.gpu.spec_decode.lilicorr.config", "LiLiCorrSpeculatorConfig", f"{__name__}.lilicorr.speculator.AscendLiLiCorrSpeculator")
_register("vllm.v1.worker.gpu.spec_decode.mtp.config", "MTPSpeculatorConfig", f"{__name__}.mtp.speculator.AscendMTPSpeculator")
_register("vllm.v1.worker.gpu.spec_decode.multi_module_mtp.config", "MultiModuleMTPSpeculatorConfig", f"{__name__}.multi_module_mtp.speculator.AscendMultiModuleMTPSpeculator")
_register("vllm.v1.worker.gpu.spec_decode.standalone_ar.config", "StandaloneARSpeculatorConfig", f"{__name__}.standalone_ar.speculator.AscendStandaloneARSpeculator")

try:
    from vllm.v1.worker.gpu.spec_decode.ngram.config import NgramSpeculatorConfig
    _NGRAM_CLASS = NgramSpeculatorConfig.speculator_class
except ImportError:
    _NGRAM_CLASS = None

try:
    from vllm.v1.worker.gpu.spec_decode.extract_hidden_states_config import ExtractHiddenStatesSpeculatorConfig
    _EXTRACT_HIDDEN_STATES_CLASS = ExtractHiddenStatesSpeculatorConfig.speculator_class
except ImportError:
    _EXTRACT_HIDDEN_STATES_CLASS = None

if TYPE_CHECKING:
    from vllm.v1.worker.gpu.states import RequestState

vllm_config.compilation_config = copy.copy(vllm_config.compilation_config)
vllm_config.compilation_config.mode = CompilationMode.NONE
if speculative_config.method in ("ngram", "ngram_gpu"):
qualname = speculative_config.speculator_config.speculator_class

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.

high

Accessing speculator_config directly can raise an AttributeError if it is None or missing in certain vLLM configurations. Using getattr defensively prevents unexpected crashes.

Suggested change
qualname = speculative_config.speculator_config.speculator_class
speculator_config = getattr(speculative_config, "speculator_config", None)
qualname = getattr(speculator_config, "speculator_class", None) if speculator_config is not None else None
if qualname is None:
raise ValueError(
"Failed to determine speculator class. Please ensure "
"speculative_config.speculator_config is properly resolved."
)

Comment on lines +69 to +74
if qualname == NgramSpeculatorConfig.speculator_class:
if req_states is None:
raise ValueError("MRV2 n-gram speculative decoding requires request states")
# Older vLLM pins lack this module; MRV2 n-gram requires upstream #40704.
from vllm.v1.worker.gpu.spec_decode.ngram.speculator import ( # type: ignore[import-not-found]
NgramGPUSpeculator,
)

# MRV2 postprocess has already updated history. The MRV1 NPU kernel
# appends sampled tokens itself and cannot be used here unchanged.
return NgramGPUSpeculator(vllm_config, device, req_states)
if speculative_config.method == "extract_hidden_states":
# No Ascend-specific behavior beyond update_stream assignment in
# NPUModelRunner; reuse upstream ExtractHiddenStatesSpeculator as-is.
from vllm.v1.worker.gpu.spec_decode.extract_hidden_states import (
ExtractHiddenStatesSpeculator,
)

return ExtractHiddenStatesSpeculator(vllm_config, device)
if speculative_config.use_dspark():
from vllm_ascend.worker.v2.spec_decode.dspark.speculator import (
AscendDSparkSpeculator,
)

return AscendDSparkSpeculator(vllm_config, device)
if speculative_config.use_dflash():
if "LiLiCorrDraftModel" in speculative_config.draft_model_config.architectures:
from vllm_ascend.worker.v2.spec_decode.lilicorr.speculator import (
AscendLiLiCorrSpeculator,
)

return AscendLiLiCorrSpeculator(vllm_config, device)
if "DFlash2DraftModel" in speculative_config.draft_model_config.architectures:
from vllm_ascend.worker.v2.spec_decode.dflash2.speculator import (
AscendDFlash2Speculator,
)

return AscendDFlash2Speculator(vllm_config, device)
from vllm_ascend.worker.v2.spec_decode.dflash.speculator import (
AscendDFlashSpeculator,
)

return AscendDFlashSpeculator(vllm_config, device)
if speculative_config.use_gemma4_mtp():
from vllm_ascend.worker.v2.spec_decode.gemma4.speculator import (
AscendGemma4Speculator,
)

return AscendGemma4Speculator(vllm_config, device)
if speculative_config.use_multi_module_mtp():
from vllm_ascend.worker.v2.spec_decode.multi_module_mtp.speculator import AscendMultiModuleMTPSpeculator

return AscendMultiModuleMTPSpeculator(vllm_config, device)
if speculative_config.method == "mtp":
from vllm_ascend.worker.v2.spec_decode.mtp.speculator import (
AscendMTPSpeculator,
)

return AscendMTPSpeculator(vllm_config, device)
if speculative_config.use_eagle():
from vllm_ascend.worker.v2.spec_decode.eagle.speculator import AscendEagleSpeculator

return AscendEagleSpeculator(vllm_config, device)
if speculative_config.uses_draft_model():
from vllm_ascend.worker.v2.spec_decode.standalone_ar.speculator import (
AscendStandaloneARSpeculator,
)
return resolve_obj_by_qualname(qualname)(vllm_config, device, req_states)
if qualname == ExtractHiddenStatesSpeculatorConfig.speculator_class:
return resolve_obj_by_qualname(qualname)(vllm_config, device)

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.

high

Update the speculator class checks to use the safely imported class names, ensuring compatibility when these classes are missing in the installed vLLM version.

Suggested change
if qualname == NgramSpeculatorConfig.speculator_class:
if req_states is None:
raise ValueError("MRV2 n-gram speculative decoding requires request states")
# Older vLLM pins lack this module; MRV2 n-gram requires upstream #40704.
from vllm.v1.worker.gpu.spec_decode.ngram.speculator import ( # type: ignore[import-not-found]
NgramGPUSpeculator,
)
# MRV2 postprocess has already updated history. The MRV1 NPU kernel
# appends sampled tokens itself and cannot be used here unchanged.
return NgramGPUSpeculator(vllm_config, device, req_states)
if speculative_config.method == "extract_hidden_states":
# No Ascend-specific behavior beyond update_stream assignment in
# NPUModelRunner; reuse upstream ExtractHiddenStatesSpeculator as-is.
from vllm.v1.worker.gpu.spec_decode.extract_hidden_states import (
ExtractHiddenStatesSpeculator,
)
return ExtractHiddenStatesSpeculator(vllm_config, device)
if speculative_config.use_dspark():
from vllm_ascend.worker.v2.spec_decode.dspark.speculator import (
AscendDSparkSpeculator,
)
return AscendDSparkSpeculator(vllm_config, device)
if speculative_config.use_dflash():
if "LiLiCorrDraftModel" in speculative_config.draft_model_config.architectures:
from vllm_ascend.worker.v2.spec_decode.lilicorr.speculator import (
AscendLiLiCorrSpeculator,
)
return AscendLiLiCorrSpeculator(vllm_config, device)
if "DFlash2DraftModel" in speculative_config.draft_model_config.architectures:
from vllm_ascend.worker.v2.spec_decode.dflash2.speculator import (
AscendDFlash2Speculator,
)
return AscendDFlash2Speculator(vllm_config, device)
from vllm_ascend.worker.v2.spec_decode.dflash.speculator import (
AscendDFlashSpeculator,
)
return AscendDFlashSpeculator(vllm_config, device)
if speculative_config.use_gemma4_mtp():
from vllm_ascend.worker.v2.spec_decode.gemma4.speculator import (
AscendGemma4Speculator,
)
return AscendGemma4Speculator(vllm_config, device)
if speculative_config.use_multi_module_mtp():
from vllm_ascend.worker.v2.spec_decode.multi_module_mtp.speculator import AscendMultiModuleMTPSpeculator
return AscendMultiModuleMTPSpeculator(vllm_config, device)
if speculative_config.method == "mtp":
from vllm_ascend.worker.v2.spec_decode.mtp.speculator import (
AscendMTPSpeculator,
)
return AscendMTPSpeculator(vllm_config, device)
if speculative_config.use_eagle():
from vllm_ascend.worker.v2.spec_decode.eagle.speculator import AscendEagleSpeculator
return AscendEagleSpeculator(vllm_config, device)
if speculative_config.uses_draft_model():
from vllm_ascend.worker.v2.spec_decode.standalone_ar.speculator import (
AscendStandaloneARSpeculator,
)
return resolve_obj_by_qualname(qualname)(vllm_config, device, req_states)
if qualname == ExtractHiddenStatesSpeculatorConfig.speculator_class:
return resolve_obj_by_qualname(qualname)(vllm_config, device)
if _NGRAM_CLASS is not None and qualname == _NGRAM_CLASS:
if req_states is None:
raise ValueError("MRV2 n-gram speculative decoding requires request states")
return resolve_obj_by_qualname(qualname)(vllm_config, device, req_states)
if _EXTRACT_HIDDEN_STATES_CLASS is not None and qualname == _EXTRACT_HIDDEN_STATES_CLASS:
return resolve_obj_by_qualname(qualname)(vllm_config, device)

Comment on lines +61 to +63
dense_draft_ep = (
getattr(self, "runner_type", None) == "draft" and parallel_config.enable_expert_parallel and not self.is_moe
)

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.

high

Accessing self.is_moe directly can raise an AttributeError if the attribute or property is missing or if model_arch_config is not fully initialized. Using getattr(self, "is_moe", False) is safer and more robust.

Suggested change
dense_draft_ep = (
getattr(self, "runner_type", None) == "draft" and parallel_config.enable_expert_parallel and not self.is_moe
)
dense_draft_ep = (
getattr(self, "runner_type", None) == "draft" and parallel_config.enable_expert_parallel and not getattr(self, "is_moe", False)
)

Comment thread vllm_ascend/models/qwen3_dflash2.py Outdated
DFlashQwen3Model,
)
from vllm.model_executor.models.utils import maybe_prefix
from vllm.v1.worker.gpu.spec_decode.dflash2.config import DFlash2SpeculatorConfig

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.

high

Wrapping the import of DFlash2SpeculatorConfig in a try-except block prevents ImportError on older vLLM versions where this speculator config might not exist.

try:
    from vllm.v1.worker.gpu.spec_decode.dflash2.config import DFlash2SpeculatorConfig
except ImportError:
    DFlash2SpeculatorConfig = None

Comment on lines 9 to 10
from vllm.v1.worker.gpu.spec_decode.mtp.config import MTPSpeculatorConfig
from vllm.v1.worker.gpu.spec_decode.target_dependent_ar import speculator as ar_speculator

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.

high

Wrapping the import of MTPSpeculatorConfig in a try-except block prevents ImportError on older vLLM versions where this speculator config might not exist.

try:
    from vllm.v1.worker.gpu.spec_decode.mtp.config import MTPSpeculatorConfig
except ImportError:
    MTPSpeculatorConfig = None
from vllm.v1.worker.gpu.spec_decode.target_dependent_ar import speculator as ar_speculator

Comment on lines +26 to +28
ascend_spec_decode._ASCEND_SPECULATORS[MTPSpeculatorConfig.speculator_class] = (
"vllm_ascend._310p.worker.v2.spec_decode.mtp_speculator.AscendMTPSpeculator310"
)

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.

high

Only register the 310P MTP speculator if MTPSpeculatorConfig was successfully imported, preventing runtime crashes on older vLLM versions.

Suggested change
ascend_spec_decode._ASCEND_SPECULATORS[MTPSpeculatorConfig.speculator_class] = (
"vllm_ascend._310p.worker.v2.spec_decode.mtp_speculator.AscendMTPSpeculator310"
)
if MTPSpeculatorConfig is not None:
ascend_spec_decode._ASCEND_SPECULATORS[MTPSpeculatorConfig.speculator_class] = (
"vllm_ascend._310p.worker.v2.spec_decode.mtp_speculator.AscendMTPSpeculator310"
)

Signed-off-by: zhenwenqi2024 <zhenwenqi_2022@qq.com>
@zhenwenqi2024 zhenwenqi2024 changed the title [BugFix][ModelRunner] Honor resolved draft routing on 310P and validate inherited parallel settings [BugFix][SpecDecode] Honor 310P MTP eager mode and preserve draft EP settings Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant