2025-05-17 15:25:17 +08:00
# Multi-NPU (QwQ 32B W8A8)
2025-04-28 17:14:26 +08:00
2025-07-11 17:40:17 +08:00
## Run docker container
2025-04-28 17:14:26 +08:00
:::{note}
2025-05-12 22:04:48 +08:00
w8a8 quantization feature is supported by v0.8.4rc2 or higher
2025-04-28 17:14:26 +08:00
:::
```{code-block} bash
:substitutions:
# Update the vllm-ascend image
export IMAGE=m.daocloud.io/quay.io/ascend/vllm-ascend:|vllm_ascend_version|
docker run --rm \
--name vllm-ascend \
--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
```
## Install modelslim and convert model
:::{note}
You can choose to convert the model yourself or use the quantized model we uploaded,
2025-05-17 15:25:17 +08:00
see https://www.modelscope.cn/models/vllm-ascend/QwQ-32B-W8A8
2025-04-28 17:14:26 +08:00
:::
```bash
2025-05-17 15:25:17 +08:00
# (Optional)This tag is recommended and has been verified
2025-05-21 09:12:02 +08:00
git clone https://gitee.com/ascend/msit -b modelslim-VLLM-8.1.RC1.b020_001
2025-04-28 17:14:26 +08:00
cd msit/msmodelslim
# Install by run this script
bash install.sh
pip install accelerate
2025-05-17 15:25:17 +08:00
cd example/Qwen
2025-04-28 17:14:26 +08:00
# Original weight path, Replace with your local model path
2025-05-17 15:25:17 +08:00
MODEL_PATH=/home/models/QwQ-32B
2025-04-28 17:14:26 +08:00
# Path to save converted weight, Replace with your local path
2025-05-17 15:25:17 +08:00
SAVE_PATH=/home/models/QwQ-32B-w8a8
2025-04-28 17:14:26 +08:00
# In this conversion process, the npu device is not must, you can also set --device_type cpu to have a conversion
2025-05-17 15:25:17 +08:00
python3 quant_qwen.py --model_path $MODEL_PATH --save_directory $SAVE_PATH --calib_file ../common/boolq.jsonl --w_bit 8 --a_bit 8 --device_type npu --anti_method m1 --trust_remote_code True
2025-04-28 17:14:26 +08:00
```
## Verify the quantized model
The converted model files looks like:
```bash
.
|-- config.json
2025-05-17 15:25:17 +08:00
|-- configuration.json
2025-04-28 17:14:26 +08:00
|-- generation_config.json
2025-05-17 15:25:17 +08:00
|-- quant_model_description.json
|-- quant_model_weight_w8a8.safetensors
|-- README.md
2025-04-28 17:14:26 +08:00
|-- tokenizer.json
`-- tokenizer_config.json
```
2025-05-29 17:32:42 +08:00
Run the following script to start the vLLM server with quantized model:
2025-05-21 09:12:02 +08:00
:::{note}
The value "ascend" for "--quantization" argument will be supported after [a specific PR ](https://github.com/vllm-project/vllm-ascend/pull/877 ) is merged and released, you can cherry-pick this commit for now.
:::
2025-04-28 17:14:26 +08:00
```bash
2025-05-17 15:25:17 +08:00
vllm serve /home/models/QwQ-32B-w8a8 --tensor-parallel-size 4 --served-model-name "qwq-32b-w8a8" --max-model-len 4096 --quantization ascend
2025-04-28 17:14:26 +08:00
```
Once your server is started, you can query the model with input prompts
```bash
curl http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{
2025-05-17 15:25:17 +08:00
"model": "qwq-32b-w8a8",
"prompt": "what is large language model?",
2025-04-28 17:14:26 +08:00
"max_tokens": "128",
"top_p": "0.95",
"top_k": "40",
"temperature": "0.0"
}'
```
2025-05-29 17:32:42 +08:00
Run the following script to execute offline inference on multi-NPU with quantized model:
:::{note}
To enable quantization for ascend, quantization method must be "ascend"
:::
```python
import gc
import torch
from vllm import LLM, SamplingParams
from vllm.distributed.parallel_state import (destroy_distributed_environment,
destroy_model_parallel)
def clean_up():
destroy_model_parallel()
destroy_distributed_environment()
gc.collect()
torch.npu.empty_cache()
prompts = [
"Hello, my name is",
"The future of AI is",
]
sampling_params = SamplingParams(temperature=0.6, top_p=0.95, top_k=40)
llm = LLM(model="/home/models/QwQ-32B-w8a8",
tensor_parallel_size=4,
distributed_executor_backend="mp",
max_model_len=4096,
quantization="ascend")
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
del llm
clean_up()
```