2025-02-05 10:53:12 +08:00
|
|
|
#
|
|
|
|
|
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
|
|
|
|
|
# Copyright 2023 The vLLM team.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
2025-04-17 14:59:56 +08:00
|
|
|
# This file is a part of the vllm-ascend project.
|
|
|
|
|
# Adapted from vllm/tests/basic_correctness/test_basic_correctness.py
|
2025-02-05 10:53:12 +08:00
|
|
|
#
|
|
|
|
|
"""Compare the short outputs of HF and vLLM when using greedy sampling.
|
|
|
|
|
|
|
|
|
|
Run `pytest tests/test_offline_inference.py`.
|
|
|
|
|
"""
|
|
|
|
|
import os
|
2025-06-05 16:42:18 +08:00
|
|
|
from unittest.mock import patch
|
2025-02-05 10:53:12 +08:00
|
|
|
|
Support multistream of shared experts in FusedMoE (#997)
Contains on #1111 for completeness.
<!-- Thanks for sending a pull request!
BEFORE SUBMITTING, PLEASE READ
https://docs.vllm.ai/en/latest/contributing/overview.html
-->
### What this PR does / why we need it?
Implement multi-stream parallelism for MoE layers with shared experts,
where computation of shared experts will be overlapped with expert token
dispatch and combine. Also, when multi-stream is enabled, weights of
shared experts will be force to replicate across all cards, regardless
of any tensor parallelism configurations, to avoid AllReduce operations.
With the expected overlaping being:
```
| shared gate_up | shared act | | shared down |
| dispatch | routed gate_up, act, down | combine |
```
<!--
- Please clarify what changes you are proposing. The purpose of this
section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster
reviews in your PR.
- Please clarify why the changes are needed. For instance, the use case
and bug description.
- Fixes #
-->
### Does this PR introduce _any_ user-facing change?
No.
<!--
Note that it means *any* user-facing change including all aspects such
as API, interface or other behavior changes.
Documentation-only updates are not considered user-facing changes.
-->
### How was this patch tested?
Tested on 1x16 910 node, with tailored 2 layer DSKv2.
<!--
CI passed with new added/existing test.
If it was tested in a way different from regular unit tests, please
clarify how you tested step by step, ideally copy and paste-able, so
that other reviewers can test and check, and descendants can verify in
the future.
If tests were not added, please describe why they were not added and/or
why it was difficult to add.
-->
---------
Signed-off-by: sdmyzlp <lrwei2@petalmail.com>
2025-06-11 09:18:38 +08:00
|
|
|
from modelscope import snapshot_download # type: ignore
|
2025-06-05 16:42:18 +08:00
|
|
|
from vllm import SamplingParams
|
2025-06-23 09:07:57 +08:00
|
|
|
from vllm.model_executor.models.registry import ModelRegistry
|
[SpecDecode] Add spec decode support (#500)
### What this PR does / why we need it?
Backport: https://github.com/vllm-project/vllm-ascend/pull/252
This support speculative decoding in Ascend, including speculating with
a draft model、by matching n-grams in the prompt、using MLP speculators
and using EAGLE based draft models.
Backport: https://github.com/vllm-project/vllm-ascend/pull/423
spec decode MultiStepWorker support TP1DraftModelRunner fully, support
run the draft_model_runner with multi-step prepare on the NPU directly
and support draft_model_runner use MLA.
1. before this pr, `MultiStepWorker` would not step into the branch
using NPU prepare, but only into the branch using CPU prepare (`line 52`
of `vllm_ascend/patch/patch_multi_step_worker.py`). Although this has
`no effect` on the `correct operation` of speculative decoding and the
performance of the two branches is basically the same as of the current
version, I support entering this branch in this PR. In general, there
are two main changes in `patch_multi_step_worker.py`: first, the
`is_cuda_like()` check is removed and the `TP1DraftModelRunner`
rewritten in vllm_ascend is used; second, the
`supports_gpu_multi_step()` function is made to return true on NPU
devices when outer Multi_step_worker could work correct.
3. before this pr, `TP1DraftModelRunner` only supports Attention on NPU,
but not MLA. The relevant adaptation is in
`vllm_ascend/worker/draft_model_runner.py`. Although I don’t know why
the `input_positions` of `model_input.attn_metadata` in vllm-ascend
needs to be added in `execute_model`, it is done in `model_runner.py`,
so I also made corresponding changes. Otherwise, when atten_backend is
MLA, it will prompt that input_positions cannot be found.
4. I commented out two lines in `draft_model_runner.py` in `line118` to
support the scenario of K>1.
```
# lora_mapping=model_input.lora_mapping,
# lora_requests=model_input.lora_requests,
```
I added comments. In the future, when vllm-ascend supports lora feature,
the changes here can be restored.
TODO:
- [ ] revert the patch when the related issues are addressed in vllm
### How was this patch tested?
CI passed with new added test.
- e2e test for medusa proposer:
tests/singlecard/spec_decode/e2e/test_medusa_correctness.py
- e2e test for mlp proposer:
tests/singlecard/spec_decode/e2e/test_mlp_correctness.py
- e2e test for n-gram proposer:
tests/singlecard/spec_decode/e2e/test_ngram_correctness.py
Tests for patched files:
- tests/singlecard/spec_decode/test_dynamic_spec_decode.py
- tests/singlecard/spec_decode/test_multi_step_worker.py
- tests/singlecard/spec_decode/test_ngram_worker.py
- tests/singlecard/spec_decode/test_spec_decode_worker.py
---------
Signed-off-by: MengqingCao <cmq0113@163.com>
Co-authored-by: mengwei805 <mengwei25@huawei.com>
2025-04-17 20:16:32 +08:00
|
|
|
|
2025-07-15 12:49:57 +08:00
|
|
|
from tests.e2e.conftest import VllmRunner
|
2025-02-05 10:53:12 +08:00
|
|
|
|
2025-03-07 09:47:13 +08:00
|
|
|
os.environ["PYTORCH_NPU_ALLOC_CONF"] = "max_split_size_mb:256"
|
2025-02-05 10:53:12 +08:00
|
|
|
|
|
|
|
|
|
2025-05-28 06:31:35 +08:00
|
|
|
def test_models_distributed_QwQ():
|
2025-04-08 16:52:45 +08:00
|
|
|
example_prompts = [
|
2025-06-05 16:28:01 +08:00
|
|
|
"Hello, my name is",
|
2025-04-08 16:52:45 +08:00
|
|
|
]
|
|
|
|
|
dtype = "half"
|
|
|
|
|
max_tokens = 5
|
2025-04-12 10:24:53 +08:00
|
|
|
with VllmRunner(
|
2025-05-28 06:31:35 +08:00
|
|
|
"Qwen/QwQ-32B",
|
2025-04-08 16:52:45 +08:00
|
|
|
dtype=dtype,
|
|
|
|
|
tensor_parallel_size=4,
|
2025-05-28 06:31:35 +08:00
|
|
|
distributed_executor_backend="mp",
|
2025-04-08 16:52:45 +08:00
|
|
|
) as vllm_model:
|
|
|
|
|
vllm_model.generate_greedy(example_prompts, max_tokens)
|
|
|
|
|
|
|
|
|
|
|
2025-06-25 19:56:49 +08:00
|
|
|
def test_models_distributed_DeepSeek_multistream_moe():
|
|
|
|
|
example_prompts = [
|
|
|
|
|
"Hello, my name is",
|
|
|
|
|
]
|
|
|
|
|
dtype = "half"
|
|
|
|
|
max_tokens = 5
|
|
|
|
|
with VllmRunner(
|
|
|
|
|
"vllm-ascend/DeepSeek-V3-Pruning",
|
|
|
|
|
dtype=dtype,
|
2025-06-30 19:02:29 +08:00
|
|
|
tensor_parallel_size=4,
|
2025-06-25 19:56:49 +08:00
|
|
|
distributed_executor_backend="mp",
|
|
|
|
|
additional_config={
|
|
|
|
|
"torchair_graph_config": {
|
|
|
|
|
"enabled": True,
|
|
|
|
|
"enable_multistream_moe": True,
|
|
|
|
|
},
|
|
|
|
|
"ascend_scheduler_config": {
|
|
|
|
|
"enabled": True,
|
|
|
|
|
},
|
|
|
|
|
"refresh": True,
|
|
|
|
|
},
|
|
|
|
|
enforce_eager=False,
|
|
|
|
|
) as vllm_model:
|
|
|
|
|
vllm_model.generate_greedy(example_prompts, max_tokens)
|
|
|
|
|
|
|
|
|
|
|
2025-06-07 16:46:58 +08:00
|
|
|
@patch.dict(os.environ, {"VLLM_ASCEND_ENABLE_DBO": "1"})
|
|
|
|
|
def test_models_distributed_DeepSeek_dbo():
|
|
|
|
|
example_prompts = ["The president of the United States is"] * 41
|
|
|
|
|
dtype = "half"
|
|
|
|
|
sampling_params = SamplingParams(max_tokens=100, temperature=0.0)
|
|
|
|
|
with VllmRunner(
|
|
|
|
|
"deepseek-ai/DeepSeek-V2-Lite",
|
|
|
|
|
dtype=dtype,
|
|
|
|
|
tensor_parallel_size=4,
|
|
|
|
|
distributed_executor_backend="mp",
|
|
|
|
|
) as vllm_model:
|
2025-06-23 09:07:57 +08:00
|
|
|
model_arch = 'DeepseekV2ForCausalLM'
|
|
|
|
|
registed_models = ModelRegistry.models
|
|
|
|
|
assert registed_models[
|
|
|
|
|
model_arch].module_name == "vllm_ascend.models.deepseek_dbo"
|
|
|
|
|
assert registed_models[
|
|
|
|
|
model_arch].class_name == "CustomDeepseekDBOForCausalLM"
|
|
|
|
|
vllm_model.generate(example_prompts, sampling_params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch.dict(os.environ, {"VLLM_ASCEND_ENABLE_DBO": "1"})
|
|
|
|
|
def test_models_distributed_DeepSeekV3_dbo():
|
|
|
|
|
example_prompts = ["The president of the United States is"] * 41
|
|
|
|
|
dtype = "half"
|
|
|
|
|
sampling_params = SamplingParams(max_tokens=100, temperature=0.0)
|
|
|
|
|
with VllmRunner(
|
|
|
|
|
"vllm-ascend/DeepSeek-V3-Pruning",
|
|
|
|
|
dtype=dtype,
|
|
|
|
|
tensor_parallel_size=4,
|
|
|
|
|
distributed_executor_backend="mp",
|
|
|
|
|
) as vllm_model:
|
|
|
|
|
model_arch = 'DeepseekV3ForCausalLM'
|
|
|
|
|
registed_models = ModelRegistry.models
|
|
|
|
|
assert registed_models[
|
|
|
|
|
model_arch].module_name == "vllm_ascend.models.deepseek_dbo"
|
|
|
|
|
assert registed_models[
|
|
|
|
|
model_arch].class_name == "CustomDeepseekDBOForCausalLM"
|
2025-06-07 16:46:58 +08:00
|
|
|
vllm_model.generate(example_prompts, sampling_params)
|
Support multistream of shared experts in FusedMoE (#997)
Contains on #1111 for completeness.
<!-- Thanks for sending a pull request!
BEFORE SUBMITTING, PLEASE READ
https://docs.vllm.ai/en/latest/contributing/overview.html
-->
### What this PR does / why we need it?
Implement multi-stream parallelism for MoE layers with shared experts,
where computation of shared experts will be overlapped with expert token
dispatch and combine. Also, when multi-stream is enabled, weights of
shared experts will be force to replicate across all cards, regardless
of any tensor parallelism configurations, to avoid AllReduce operations.
With the expected overlaping being:
```
| shared gate_up | shared act | | shared down |
| dispatch | routed gate_up, act, down | combine |
```
<!--
- Please clarify what changes you are proposing. The purpose of this
section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster
reviews in your PR.
- Please clarify why the changes are needed. For instance, the use case
and bug description.
- Fixes #
-->
### Does this PR introduce _any_ user-facing change?
No.
<!--
Note that it means *any* user-facing change including all aspects such
as API, interface or other behavior changes.
Documentation-only updates are not considered user-facing changes.
-->
### How was this patch tested?
Tested on 1x16 910 node, with tailored 2 layer DSKv2.
<!--
CI passed with new added/existing test.
If it was tested in a way different from regular unit tests, please
clarify how you tested step by step, ideally copy and paste-able, so
that other reviewers can test and check, and descendants can verify in
the future.
If tests were not added, please describe why they were not added and/or
why it was difficult to add.
-->
---------
Signed-off-by: sdmyzlp <lrwei2@petalmail.com>
2025-06-11 09:18:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_models_distributed_DeepSeek_W8A8():
|
|
|
|
|
example_prompts = [
|
|
|
|
|
"Hello, my name is",
|
|
|
|
|
]
|
|
|
|
|
max_tokens = 5
|
|
|
|
|
|
|
|
|
|
with VllmRunner(
|
|
|
|
|
snapshot_download("vllm-ascend/DeepSeek-V2-Lite-W8A8"),
|
|
|
|
|
max_model_len=8192,
|
|
|
|
|
enforce_eager=True,
|
|
|
|
|
dtype="auto",
|
|
|
|
|
tensor_parallel_size=4,
|
|
|
|
|
quantization="ascend",
|
|
|
|
|
) as vllm_model:
|
|
|
|
|
vllm_model.generate_greedy(example_prompts, max_tokens)
|
2025-07-03 22:21:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_models_distributed_pangu():
|
|
|
|
|
example_prompts = [
|
|
|
|
|
"Hello, my name is",
|
|
|
|
|
]
|
|
|
|
|
max_tokens = 5
|
|
|
|
|
|
|
|
|
|
with VllmRunner(
|
|
|
|
|
snapshot_download("vllm-ascend/pangu-pro-moe-pruing"),
|
|
|
|
|
max_model_len=8192,
|
|
|
|
|
enforce_eager=True,
|
|
|
|
|
dtype="auto",
|
|
|
|
|
tensor_parallel_size=4,
|
|
|
|
|
distributed_executor_backend="mp",
|
|
|
|
|
) as vllm_model:
|
|
|
|
|
vllm_model.generate_greedy(example_prompts, max_tokens)
|
2025-07-11 15:32:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch.dict(os.environ, {"VLLM_ASCEND_ENABLE_TOPK_TOPP_OPTIMIZATION": "1"})
|
|
|
|
|
def test_models_distributed_topk() -> None:
|
|
|
|
|
example_prompts = [
|
|
|
|
|
"vLLM is a high-throughput and memory-efficient inference and serving engine for LLMs.",
|
|
|
|
|
"Briefly describe the major milestones in the development of artificial intelligence from 1950 to 2020.",
|
|
|
|
|
"Compare and contrast artificial intelligence with human intelligence in terms of processing information.",
|
|
|
|
|
]
|
|
|
|
|
dtype = "half"
|
|
|
|
|
sampling_params = SamplingParams(max_tokens=5,
|
|
|
|
|
temperature=0.0,
|
|
|
|
|
top_k=50,
|
|
|
|
|
top_p=0.9)
|
|
|
|
|
|
|
|
|
|
with VllmRunner(
|
|
|
|
|
"deepseek-ai/DeepSeek-V2-Lite",
|
|
|
|
|
dtype=dtype,
|
|
|
|
|
tensor_parallel_size=4,
|
|
|
|
|
distributed_executor_backend="mp",
|
|
|
|
|
) as vllm_model:
|
|
|
|
|
vllm_model.generate(example_prompts, sampling_params)
|