|
| 1 | +# Testing |
| 2 | + |
| 3 | +This secition explains how to write e2e tests and unit tests to verify the implementation of your feature. |
| 4 | + |
| 5 | +## Setup test environment |
| 6 | + |
| 7 | +The fastest way to setup test environment is to use the main branch container image: |
| 8 | + |
| 9 | +:::::{tab-set} |
| 10 | +:sync-group: e2e |
| 11 | + |
| 12 | +::::{tab-item} Single card |
| 13 | +:selected: |
| 14 | +:sync: single |
| 15 | + |
| 16 | +```{code-block} bash |
| 17 | + :substitutions: |
| 18 | +
|
| 19 | +# Update DEVICE according to your device (/dev/davinci[0-7]) |
| 20 | +export DEVICE=/dev/davinci0 |
| 21 | +# Update the vllm-ascend image |
| 22 | +export IMAGE=quay.io/ascend/vllm-ascend:main |
| 23 | +docker run --rm \ |
| 24 | + --name vllm-ascend \ |
| 25 | + --device $DEVICE \ |
| 26 | + --device /dev/davinci_manager \ |
| 27 | + --device /dev/devmm_svm \ |
| 28 | + --device /dev/hisi_hdc \ |
| 29 | + -v /usr/local/dcmi:/usr/local/dcmi \ |
| 30 | + -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \ |
| 31 | + -v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \ |
| 32 | + -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \ |
| 33 | + -v /etc/ascend_install.info:/etc/ascend_install.info \ |
| 34 | + -v /root/.cache:/root/.cache \ |
| 35 | + -p 8000:8000 \ |
| 36 | + -it $IMAGE bash |
| 37 | +``` |
| 38 | + |
| 39 | +:::: |
| 40 | + |
| 41 | +::::{tab-item} Multi cards |
| 42 | +:sync: multi |
| 43 | + |
| 44 | +```{code-block} bash |
| 45 | + :substitutions: |
| 46 | +# Update the vllm-ascend image |
| 47 | +export IMAGE=quay.io/ascend/vllm-ascend:main |
| 48 | +docker run --rm \ |
| 49 | + --name vllm-ascend \ |
| 50 | + --device /dev/davinci0 \ |
| 51 | + --device /dev/davinci1 \ |
| 52 | + --device /dev/davinci2 \ |
| 53 | + --device /dev/davinci3 \ |
| 54 | + --device /dev/davinci_manager \ |
| 55 | + --device /dev/devmm_svm \ |
| 56 | + --device /dev/hisi_hdc \ |
| 57 | + -v /usr/local/dcmi:/usr/local/dcmi \ |
| 58 | + -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \ |
| 59 | + -v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \ |
| 60 | + -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \ |
| 61 | + -v /etc/ascend_install.info:/etc/ascend_install.info \ |
| 62 | + -v /root/.cache:/root/.cache \ |
| 63 | + -p 8000:8000 \ |
| 64 | + -it $IMAGE bash |
| 65 | +``` |
| 66 | +:::: |
| 67 | + |
| 68 | +::::: |
| 69 | + |
| 70 | +After starting the container, you should install the required packages: |
| 71 | + |
| 72 | +```bash |
| 73 | +# Prepare |
| 74 | +pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple |
| 75 | + |
| 76 | +# Install required packages |
| 77 | +pip install -r requirements-dev.txt |
| 78 | +``` |
| 79 | + |
| 80 | +## Running tests |
| 81 | + |
| 82 | +### Unit test |
| 83 | + |
| 84 | +There are several principles to follow when writing unit tests: |
| 85 | + |
| 86 | +- The test file path should be consistent with source file and start with `test_` prefix, such as: `vllm_ascend/worker/worker_v1.py` --> `tests/ut/worker/test_worker_v1.py` |
| 87 | +- The vLLM Ascend test are using unittest framework, see [here](https://docs.python.org/3/library/unittest.html#module-unittest) to understand how to write unit tests. |
| 88 | +- All unit tests can be run on CPU, so you must mock the device-related function to host. |
| 89 | +- Example: [tests/ut/test_ascend_config.py](https://github.com/vllm-project/vllm-ascend/blob/main/tests/ut/test_ascend_config.py). |
| 90 | +- You can run the unit tests using `pytest`: |
| 91 | + |
| 92 | + ```bash |
| 93 | + cd /vllm-workspace/vllm-ascend/ |
| 94 | + # Run all single card the tests |
| 95 | + pytest -sv tests/ut |
| 96 | + |
| 97 | + # Run |
| 98 | + pytest -sv tests/ut/test_ascend_config.py |
| 99 | + ``` |
| 100 | + |
| 101 | +### E2E test |
| 102 | + |
| 103 | +Although vllm-ascend CI provide [e2e test](https://github.com/vllm-project/vllm-ascend/blob/main/.github/workflows/vllm_ascend_test.yaml) on Ascend CI, you can run it |
| 104 | +locally. |
| 105 | + |
| 106 | +:::::{tab-set} |
| 107 | +:sync-group: e2e |
| 108 | + |
| 109 | +::::{tab-item} Single card |
| 110 | +:selected: |
| 111 | +:sync: single |
| 112 | + |
| 113 | +```bash |
| 114 | +cd /vllm-workspace/vllm-ascend/ |
| 115 | +# Run all single card the tests |
| 116 | +VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/singlecard/ |
| 117 | +
|
| 118 | +# Run a certain test script |
| 119 | +VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/singlecard/test_offline_inference.py |
| 120 | +
|
| 121 | +# Run a certain case in test script |
| 122 | +VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/singlecard/test_offline_inference.py::test_models |
| 123 | +``` |
| 124 | +:::: |
| 125 | + |
| 126 | +::::{tab-item} Multi cards test |
| 127 | +:sync: multi |
| 128 | +```bash |
| 129 | +cd /vllm-workspace/vllm-ascend/ |
| 130 | +# Run all single card the tests |
| 131 | +VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/multicard/ |
| 132 | +
|
| 133 | +# Run a certain test script |
| 134 | +VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/multicard/test_dynamic_npugraph_batchsize.py |
| 135 | +
|
| 136 | +# Run a certain case in test script |
| 137 | +VLLM_USE_MODELSCOPE=true pytest -sv tests/e2e/multicard/test_offline_inference.py::test_models |
| 138 | +``` |
| 139 | +:::: |
| 140 | + |
| 141 | +::::: |
| 142 | + |
| 143 | +This will reproduce e2e test: [vllm_ascend_test.yaml](https://github.com/vllm-project/vllm-ascend/blob/main/.github/workflows/vllm_ascend_test.yaml). |
| 144 | + |
| 145 | +#### E2E test example: |
| 146 | + |
| 147 | +- Offline test example: [`tests/e2e/singlecard/test_offline_inference.py`](https://github.com/vllm-project/vllm-ascend/blob/main/tests/e2e/singlecard/test_offline_inference.py) |
| 148 | +- Online test examples: [`tests/e2e/singlecard/test_prompt_embedding.py`](https://github.com/vllm-project/vllm-ascend/blob/main/tests/e2e/singlecard/test_prompt_embedding.py) |
| 149 | +- Correctness test example: [`tests/e2e/singlecard/test_aclgraph.py`](https://github.com/vllm-project/vllm-ascend/blob/main/tests/e2e/singlecard/test_aclgraph.py) |
| 150 | +- Reduced Layer model test example: [test_torchair_graph_mode.py - DeepSeek-V3-Pruning](https://github.com/vllm-project/vllm-ascend/blob/20767a043cccb3764214930d4695e53941de87ec/tests/e2e/multicard/test_torchair_graph_mode.py#L48) |
| 151 | + |
| 152 | + The CI resource is limited, you might need to reduce layer number of the model, below is an example of how to generate a reduced layer model: |
| 153 | + 1. Fork the original model repo in modelscope, we need all the files in the repo except for weights. |
| 154 | + 2. Set `num_hidden_layers` to the expected number of layers, e.g., `{"num_hidden_layers": 2,}` |
| 155 | + 3. Copy the following python script as `generate_random_weight.py`. Set the relevant parameters `MODEL_LOCAL_PATH`, `DIST_DTYPE` and `DIST_MODEL_PATH` as needed: |
| 156 | + |
| 157 | + ```python |
| 158 | + import torch |
| 159 | + from transformers import AutoTokenizer, AutoConfig |
| 160 | + from modeling_deepseek import DeepseekV3ForCausalLM |
| 161 | + from modelscope import snapshot_download |
| 162 | +
|
| 163 | + MODEL_LOCAL_PATH = "~/.cache/modelscope/models/vllm-ascend/DeepSeek-V3-Pruning" |
| 164 | + DIST_DTYPE = torch.bfloat16 |
| 165 | + DIST_MODEL_PATH = "./random_deepseek_v3_with_2_hidden_layer" |
| 166 | +
|
| 167 | + config = AutoConfig.from_pretrained(MODEL_LOCAL_PATH, trust_remote_code=True) |
| 168 | + model = DeepseekV3ForCausalLM(config) |
| 169 | + model = model.to(DIST_DTYPE) |
| 170 | + model.save_pretrained(DIST_MODEL_PATH) |
| 171 | + ``` |
| 172 | + |
| 173 | +### Run doctest |
| 174 | + |
| 175 | +vllm-ascend provides a `vllm-ascend/tests/e2e/run_doctests.sh` command to run all doctests in the doc files. |
| 176 | +The doctest is a good way to make sure the docs are up to date and the examples are executable, you can run it locally as follows: |
| 177 | + |
| 178 | +```bash |
| 179 | +# Run doctest |
| 180 | +/vllm-workspace/vllm-ascend/tests/e2e/run_doctests.sh |
| 181 | +``` |
| 182 | + |
| 183 | +This will reproduce the same environment as the CI: [vllm_ascend_doctest.yaml](https://github.com/vllm-project/vllm-ascend/blob/main/.github/workflows/vllm_ascend_doctest.yaml). |
0 commit comments