跳转至

测试

本文档说明如何编写单元测试、端到端测试和夜间测试,以验证您的功能实现。

搭建测试环境

搭建测试环境最快的方式是使用主分支的容器镜像:

您可以按照以下步骤在 CPU 上运行单元测试:

cd ~/vllm-project/
# ls
# vllm  vllm-ascend

# Use mirror to speed up download
# docker pull m.daocloud.io/quay.io/ascend/cann:9.1.0-910b-ubuntu22.04-py3.12
export IMAGE=quay.io/ascend/cann:9.1.0-910b-ubuntu22.04-py3.12
docker run --rm --name vllm-ascend-ut \
    -v $(pwd):/vllm-project \
    -v ~/.cache:/root/.cache \
    -ti $IMAGE bash

Run the remaining commands inside the container:

# (Optional) Configure mirror to speed up download
sed -i 's|ports.ubuntu.com|mirrors.huaweicloud.com|g' /etc/apt/sources.list
pip config set global.index-url https://mirrors.huaweicloud.com/repository/pypi/simple/

# For TorchNPU dev version or x86 machine
export PIP_EXTRA_INDEX_URL="https://download.pytorch.org/whl/cpu/ https://mirrors.huaweicloud.com/ascend/repos/pypi"

# src path
export SRC_WORKSPACE=/vllm-workspace
mkdir -p $SRC_WORKSPACE
cd $SRC_WORKSPACE

apt-get update -y
apt-get install -y python3-pip git vim wget net-tools gcc g++ cmake libnuma-dev curl gnupg2

git clone -b v0.23.0 --depth 1 https://github.com/vllm-project/vllm-ascend.git
git clone -b v0.23.0 --depth 1 https://github.com/vllm-project/vllm.git

# vllm
cd $SRC_WORKSPACE/vllm
VLLM_TARGET_DEVICE=empty python3 -m pip install .
python3 -m pip uninstall -y triton

# vllm-ascend
cd $SRC_WORKSPACE/vllm-ascend
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/Ascend/ascend-toolkit/latest/$(uname -m)-linux/devlib
# For cpu environment, set SOC_VERSION for different chips.
# See https://github.com/vllm-project/vllm-ascend/blob/3cb0af0bcf3299089ca7e72159fa36e825a470f8/setup.py#L132 for detail.
export SOC_VERSION="ascend910b1"
python3 -m pip install .
python3 -m pip install -r requirements-dev.txt
# Update DEVICE according to your device (/dev/davinci[0-7])
export DEVICE=/dev/davinci0
# A2 Ubuntu image; use nightly-main-a3 for A3 and add -openeuler for openEuler.
export IMAGE=quay.io/ascend/vllm-ascend:nightly-main
docker run --rm \
    --name vllm-ascend \
    --shm-size=1g \
    --device $DEVICE \
    --device /dev/davinci_manager \
    --device /dev/devmm_svm \
    --device /dev/hisi_hdc \
    -v /usr/local/dcmi:/usr/local/dcmi \
    -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
    -v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
    -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
    -v /etc/ascend_install.info:/etc/ascend_install.info \
    -v /root/.cache:/root/.cache \
    -p 8000:8000 \
    -it $IMAGE bash

启动容器后,您需要安装所需的软件包:

# Prepare
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

# Switch to the /vllm-workspace/vllm-ascend directory
cd /vllm-workspace/vllm-ascend/

# Install required packages
pip install -r requirements-dev.txt
# A2 Ubuntu image; use nightly-main-a3 for A3 and add -openeuler for openEuler.
export IMAGE=quay.io/ascend/vllm-ascend:nightly-main
docker run --rm \
    --name vllm-ascend \
    --shm-size=1g \
    --device /dev/davinci0 \
    --device /dev/davinci1 \
    --device /dev/davinci2 \
    --device /dev/davinci3 \
    --device /dev/davinci_manager \
    --device /dev/devmm_svm \
    --device /dev/hisi_hdc \
    -v /usr/local/dcmi:/usr/local/dcmi \
    -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
    -v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
    -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
    -v /etc/ascend_install.info:/etc/ascend_install.info \
    -v /root/.cache:/root/.cache \
    -p 8000:8000 \
    -it $IMAGE bash

启动容器后,您需要安装所需的软件包:

cd /vllm-workspace/vllm-ascend/

# Prepare
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

# Install required packages
pip install -r requirements-dev.txt

运行测试

单元测试

编写单元测试时需要遵循以下几个原则:

  • 测试文件路径应与源文件一致,并以 test_ 前缀开头,例如:vllm_ascend/worker/worker.pytests/ut/worker/test_worker.py
  • vLLM Ascend 测试使用 unittest 框架。请参阅 Python unittest 文档 了解如何编写单元测试。
  • 所有单元测试均可在 CPU 上运行,因此您必须在主机上模拟与设备相关的函数。
  • 示例:tests/ut/test_ascend_config.py
  • 您可以使用 pytest 运行单元测试:
# Run unit tests
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/Ascend/ascend-toolkit/latest/$(uname -m)-linux/devlib
TORCH_DEVICE_BACKEND_AUTOLOAD=0 pytest -sv tests/ut
cd /vllm-workspace/vllm-ascend/
# Run all single-card tests
pytest -sv tests/ut

# Run single test
pytest -sv tests/ut/test_ascend_config.py
cd /vllm-workspace/vllm-ascend/
# Run all multi-card tests
pytest -sv tests/ut

# Run single test
pytest -sv tests/ut/test_ascend_config.py

端到端测试

尽管 vllm-ascend CI 在 Ascend CI 上提供了端到端测试(例如,schedule_nightly_test_a2.yamlschedule_nightly_test_a3.yamlpr_test.yaml),您也可以在本地运行它们。

PR 触发的端到端测试

您也可以使用 pytest 运行测试。典型示例如下:

您无法在 CPU 上运行端到端测试。

cd /vllm-workspace/vllm-ascend/
# Run all single-card tests
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/pull_request/one_card/

# Run a certain test script
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/pull_request/one_card/test_qwen3_0_6b.py

# Run a certain case in test script
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/pull_request/one_card/test_qwen3_0_6b.py::test_dense_default_full_and_piecewise_graph
cd /vllm-workspace/vllm-ascend/
# Run all multi-card tests
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/pull_request/two_card/

# Run a certain test script
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/pull_request/two_card/test_qwen3_moe_eplb.py

# Run a certain case in test script
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/pull_request/two_card/test_qwen3_moe_eplb.py::test_qwen3_moe_w8a8_distributed_tp2_ep_dynamic_eplb

这将重现端到端测试的行为。

夜间触发的端到端测试

您也可以使用 pytest 运行测试。典型示例如下:

您无法在 CPU 上运行端到端测试。

cd /vllm-workspace/vllm-ascend/
# run all single-card op tests
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/nightly/single_node/ops/singlecard_ops/
cd /vllm-workspace/vllm-ascend/
# run all multi-card op tests on A3
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/nightly/single_node/ops/multicard_ops_a3/

如需在本地运行夜间单节点模型测试用例,请参考以下示例。

export CONFIG_YAML_PATH=Qwen3-32B.yaml
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/nightly/single_node/models/scripts/test_single_node.py

如需在本地运行夜间多节点模型测试用例,请参考多节点测试中的“本地运行”部分。

端到端测试示例

PR 选择性测试(CI)

PR CI 工作流(pr_test.yaml)不会在每个 PR 上运行完整的测试套件。它通过基于覆盖率/AST 的精确测试流水线选择测试,并将其路由到 NPU 运行器。当 PR 带有 ready-precise 标签(推荐子集)、ready-all 标签(完整套件)或 main2main 标签(针对已验证的 vLLM main 提交和匹配的 vLLM 发布标签执行的完整套件)时,测试会运行。

测试的选择方式:

  1. test_selector.py 根据历史 CI 覆盖率数据构建测试用例与其覆盖的源代码行的映射,然后推荐受 PR 更改行影响的测试(行 → 函数 → 文件粒度回退)。
  2. select_tests.py 根据目录约定(tests/ut/<module>/ → CPU,tests/ut/<module>/a2/ → A2,tests/e2e/pull_request/{one,two,four,eight}_card/ → A3,_310p → 310P)将每个推荐的测试路径路由到运行器,通过预估时间平衡负载,并生成 CI 矩阵。

Adding a new test requires no configuration change: place the UT file under the matching tests/ut/<module>[/<npu>] directory or the E2E file under the matching tests/e2e/pull_request/<card> directory, and CI picks it up automatically from the test tree. Routing metadata (runner mapping, partitions) lives in .github/workflows/scripts/test_config.yaml. Estimated times used for load balancing live in .github/workflows/scripts/estimated_times.yaml.

您可以在本地预览一组测试将被路由到哪些运行器:

python3 .github/workflows/scripts/select_tests.py \
  --explicit-e2e-tests tests/e2e/pull_request/one_card/test_qwen3_0_6b.py

# Full suite routing (mirrors the ready-all mode)
python3 .github/workflows/scripts/select_tests.py --all-tests

在请求标签之前,要在 CI 硬件上调试特定测试,请参阅 E2E CI 测试

E2E 测试模型资源缩减

CI 资源有限,您可能需要减少模型的层数。以下是如何生成精简层模型的示例:

  1. 在 modelscope 中 fork 原始模型仓库。需要仓库中除权重外的所有文件。
  2. num_hidden_layers 设置为期望的层数,例如 {"num_hidden_layers": 2,}
  3. 复制以下 Python 脚本并命名为 generate_random_weight.py。根据需要设置相关参数 MODEL_LOCAL_PATHDIST_DTYPEDIST_MODEL_PATH

    import torch
    from transformers import AutoTokenizer, AutoConfig
    from modeling_deepseek import DeepseekV3ForCausalLM
    from modelscope import snapshot_download
    
    MODEL_LOCAL_PATH = "~/.cache/modelscope/models/vllm-ascend/DeepSeek-V3-Pruning"
    DIST_DTYPE = torch.bfloat16
    DIST_MODEL_PATH = "./random_deepseek_v3_with_2_hidden_layer"
    
    config = AutoConfig.from_pretrained(MODEL_LOCAL_PATH, trust_remote_code=True)
    model = DeepseekV3ForCausalLM(config)
    model = model.to(DIST_DTYPE)
    model.save_pretrained(DIST_MODEL_PATH)
    

运行 doctest

Doctests 验证固定的、已标记的快速开始和安装代码块,而非文档中的每个代码块。快速开始涵盖 A2 和 310P(Atlas 300I DUO),依次运行离线和在线示例。安装涵盖 A2 上的 pipuvsource,随后进行离线推理验证。两者均支持 Ubuntu 和 openEuler。

在准备好的 NPU 环境中,从仓库根目录运行以下命令之一:

./tests/e2e/doctests/scripts/run_doctests.sh quickstart a2
./tests/e2e/doctests/scripts/run_doctests.sh quickstart 310p

./tests/e2e/doctests/scripts/run_doctests.sh installation pip
./tests/e2e/doctests/scripts/run_doctests.sh installation uv
./tests/e2e/doctests/scripts/run_doctests.sh installation source

该入口点不会创建容器。快速开始请使用匹配的 vLLM Ascend 镜像,安装请使用一次性的 CANN 容器(安装会更改系统和 Python 软件包)。请提前准备示例的模型缓存;worker 会启用 Hugging Face 离线模式。

在 CI 中,.github/workflows/schedule_doctest.yaml 显示为 Doc Test。针对 mainreleases/v* 的相关 PR 变更会自动选择受影响的用例。你也可以通过 quickstart_device 和/或 installation_method 手动运行;none 会跳过该用例。每个选中的用例都会在两个操作系统上运行。没有定时触发。

有关代码块提取、计划预览和选择规则,请参阅对应分支上 tests/e2e/doctests/scripts/doctest_helper.py 中的使用说明和函数注释。

运行文档链接检查

您可以使用以下命令在本地验证 Sphinx 文档中的外部链接:

make -C docs linkcheck SPHINXOPTS="-W --keep-going"

要检查特定 Markdown 文件中的链接,请将该文件传递给 sphinx-build。 例如,仅检查 docs/source/user_guide/release_notes.md

cd docs
sphinx-build -b linkcheck -W --keep-going \
  source _build/linkcheck source/user_guide/release_notes.md

详细报告将写入:

  • docs/_build/linkcheck/output.txt
  • docs/_build/linkcheck/output.json