Repository navigation
[BugFix][SpecDecode] Honor 310P MTP eager mode and preserve draft EP settings - #23418
zhenwenqi2024 wants to merge 4 commits into
Conversation
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>
|
👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:
If CI fails, you can run linting and testing checks locally according Contributing and Testing. 🤖 How to trigger CI
Note Until the CI gate has run on a commit, the required Tip 💡 Consider Linking a Related Issue or RFCYour 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:
🙏 Thanks for helping us keep the project well-organized! |
There was a problem hiding this comment.
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.
| 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", | ||
| } |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Accessing speculator_config directly can raise an AttributeError if it is None or missing in certain vLLM configurations. Using getattr defensively prevents unexpected crashes.
| 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." | |
| ) |
| 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) |
There was a problem hiding this comment.
Update the speculator class checks to use the safely imported class names, ensuring compatibility when these classes are missing in the installed vLLM version.
| 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) |
| dense_draft_ep = ( | ||
| getattr(self, "runner_type", None) == "draft" and parallel_config.enable_expert_parallel and not self.is_moe | ||
| ) |
There was a problem hiding this comment.
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.
| 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) | |
| ) |
| DFlashQwen3Model, | ||
| ) | ||
| from vllm.model_executor.models.utils import maybe_prefix | ||
| from vllm.v1.worker.gpu.spec_decode.dflash2.config import DFlash2SpeculatorConfig |
There was a problem hiding this comment.
| 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 |
There was a problem hiding this comment.
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| ascend_spec_decode._ASCEND_SPECULATORS[MTPSpeculatorConfig.speculator_class] = ( | ||
| "vllm_ascend._310p.worker.v2.spec_decode.mtp_speculator.AscendMTPSpeculator310" | ||
| ) |
There was a problem hiding this comment.
Only register the 310P MTP speculator if MTPSpeculatorConfig was successfully imported, preventing runtime crashes on older vLLM versions.
| 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>
What this PR does / why we need it?
Two Ascend MTP setup paths still need local handling in Model Runner V2.
enforce_eageris set. Delegate registered MTP-based methods to the existing shared factory with their request state.VllmConfigfrom 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'sdraft_parallel_configin 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
mainafter #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
5f20134a4with vLLMv0.34.0andc410543d9. Each run passed all 92 cases.mainat5da436cf7reproduced four failures. They covered registered MTP routing on 310P, draft eager mode on 310P and dense-draft EP validation in both loaders.6b05e4580applied 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.Checklist
Testing
AI assistance was used for implementation and validation.