跳转至

测试

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

搭建测试环境

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

您可以按照以下步骤在 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

在容器内运行剩余命令:

# (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.py → tests/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.yaml、schedule_nightly_test_a3.yaml、pr_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_BASE_PATH=tests/e2e/cases/models/configs/Qwen
export CONFIG_YAML_PATH=Qwen3-32B.yaml
VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/common/single_node/test_single_node.py

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

端到端测试示例

Csrc incremental build cache checks

For changes to native build inputs or the cache adapter, run the cache engine, concurrency, and snapshot-key tests before requesting an NPU build:

pytest -q --confcutdir=tests/ut/_tools \
  tests/ut/_tools/test_build_cache.py \
  tests/ut/_tools/test_build_cache_concurrency.py \
  tests/ut/_tools/test_prepare_csrc_l1_restore.py

On CI, a successful restore step only proves that an outer L1 snapshot was available. Check the inner cache telemetry for action HIT/MISS/BYPASS to establish whether a source build actually reused compiled work. See the persistent csrc build cache design for the identity and failure contracts.

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 矩阵。

新增测试无需修改配置:将 UT 文件放到对应的 tests/ut/<module>[/<npu>] 目录下,或将 E2E 文件放到对应的 tests/e2e/pull_request/<card> 目录下,CI 会自动从测试树中识别。路由元数据(runner 映射、分区)位于 .github/workflows/scripts/test_config.yaml。 用于负载均衡的预估时间位于 .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_PATH、DIST_DTYPE 和 DIST_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)
    

CI 工作流触发方式与 schedule_ 前缀

.github/workflows/ 下名称以 schedule_ 开头的工作流文件 属于周期性 CI 系列(nightly、weekly、doc、coverage 及类似的 周期性任务)。schedule_ 前缀并不保证该工作流 声明了 GitHub Actions 的 schedule:(cron)触发器。

部分 schedule_*.yaml 文件有意省略 schedule:,改由 项目的外部自动化通过 workflow_dispatch 进行调度。这是有意 设计,而非缺少 cron 条目,因为外部调度器能更好地控制 资源密集型 NPU 任务的运行时机。

带有原生 GitHub cron 触发器的工作流:

工作流 其他触发器
schedule_doc_linkcheck.yaml PR 路径过滤、workflow_dispatch
schedule_doc_translate.yaml workflow_dispatch
schedule_e2e_upstream_test.yaml -
schedule_lint_image_build.yaml workflow_dispatch、push
schedule_main2main.yaml workflow_dispatch
schedule_stale_manage.yaml issue_comment

无 cron 触发器、通过 workflow_dispatch 由外部调度的工作流:

工作流 其他触发器
schedule_doc_getting_started_test.yaml PR 路径过滤
schedule_e2e_test.yaml -
schedule_image_build_and_push.yaml PR 标签、tag push
schedule_nightly_test_310p.yaml -
schedule_nightly_test_a2.yaml -
schedule_nightly_test_a3.yaml -
schedule_nightly_test_a3_560t.yaml -
schedule_nightly_test_a5.yaml -
schedule_release_code_and_wheel.yml tag push
schedule_test_coverage.yaml -
schedule_weekly_test_310p.yaml -
schedule_weekly_test_a2.yaml -
schedule_weekly_test_a3.yaml -
schedule_weekly_test_a3_560t.yaml -

运行 doctest

Doctests 验证固定的、已标记的快速开始和安装代码块,而非文档中的每个代码块。快速开始涵盖 A2 和 310P(Atlas 300I DUO),依次运行离线和在线示例。安装涵盖 A2 上的 pip、uv 和 source,随后进行离线推理验证。两者均支持 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_doc_getting_started_test.yaml 显示为 Doc Test。针对 main 或 releases/v* 的相关 PR 变更会自动选择受影响的用例。外部自动化可以按计划调度它,你也可以使用逗号分隔的 quickstart_devices 和/或 installation_methods 手动运行;将某个输入留空即可跳过该测试类型。每个选中的用例都会在两个操作系统上运行。

有关代码块提取、计划预览和选择规则,请参阅对应分支上 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