Ascend-SACT/Qwen2.5-Coder-7B-Instruct
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Qwen2.5-Coder-7B-Instruct

本文介绍使用 vLLM-Ascend 在昇腾单卡上部署 Qwen2.5-Coder-7B-Instruct,提供代码生成、代码修复和对话接口。

部署环境

类别项目配置
硬件NPUAscend 910B4,单卡 32 GB HBM
硬件卡数1 张
软件系统架构Linux,ARM64
软件驱动26.1.1
软件Docker 镜像quay.nju.edu.cn/ascend/vllm-ascend:v0.18.0rc1
模型权重BF16,未量化

1 下载模型

在宿主机执行以下命令,通过镜像内的 ModelScope 下载模型。

docker pull quay.nju.edu.cn/ascend/vllm-ascend:v0.18.0rc1

mkdir -p /opt/qwen25-coder-7b-deploy/model /opt/qwen25-coder-7b-deploy/cache

docker run --rm --network host \
  -v /opt/qwen25-coder-7b-deploy:/task-runtime \
  -e TORCH_DEVICE_BACKEND_AUTOLOAD=0 \
  -e MODELSCOPE_CACHE=/task-runtime/cache/modelscope \
  --entrypoint /bin/bash \
  quay.nju.edu.cn/ascend/vllm-ascend:v0.18.0rc1 \
  -c 'modelscope download --model Qwen/Qwen2.5-Coder-7B-Instruct --local_dir /task-runtime/model'

模型权重及配套文件约 15.2 GB。

2 启动 Docker

先执行 npu-smi info 查看设备占用情况。下面示例使用宿主机 7 号卡。使用其他卡时,同时修改 /dev/davinci7 和 ASCEND_RT_VISIBLE_DEVICES=7。该环境通过 --privileged 访问 NPU 驱动;可见设备变量使用宿主机物理编号,推理进程内对应逻辑设备 npu:0。

docker run -d --privileged \
  --name qwen25-coder-7b \
  --shm-size=4g \
  --device=/dev/davinci7 \
  --device=/dev/davinci_manager \
  --device=/dev/devmm_svm \
  --device=/dev/hisi_hdc \
  -v /usr/local/dcmi:/usr/local/dcmi:ro \
  -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi:ro \
  -v /usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64:ro \
  -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info:ro \
  -v /etc/ascend_install.info:/etc/ascend_install.info:ro \
  -v /opt/qwen25-coder-7b-deploy:/task-runtime \
  -e ASCEND_RT_VISIBLE_DEVICES=7 \
  -e VLLM_WORKER_MULTIPROC_METHOD=spawn \
  -e PYTORCH_NPU_ALLOC_CONF=expandable_segments:True \
  -e HF_HOME=/task-runtime/cache/huggingface \
  -e XDG_CACHE_HOME=/task-runtime/cache/xdg \
  -e VLLM_CACHE_ROOT=/task-runtime/cache/vllm \
  -e TORCHINDUCTOR_CACHE_DIR=/task-runtime/cache/torchinductor \
  -p 127.0.0.1:18527:8000 \
  --entrypoint /bin/bash \
  quay.nju.edu.cn/ascend/vllm-ascend:v0.18.0rc1 \
  -c 'sleep infinity'

进入容器:

docker exec -it qwen25-coder-7b bash

3 启动服务

在容器内执行:

for environment in /usr/local/Ascend/ascend-toolkit/set_env.sh /usr/local/Ascend/cann/set_env.sh; do
  if [ -f "$environment" ]; then
    source "$environment"
    break
  fi
done
if [ -f /usr/local/Ascend/nnal/atb/set_env.sh ]; then
  source /usr/local/Ascend/nnal/atb/set_env.sh
fi

nohup python3 -m vllm.entrypoints.openai.api_server \
  --model /task-runtime/model \
  --served-model-name qwen2.5-coder-7b-instruct \
  --host 0.0.0.0 \
  --port 8000 \
  --tensor-parallel-size 1 \
  --dtype bfloat16 \
  --max-model-len 2048 \
  --max-num-batched-tokens 2048 \
  --max-num-seqs 2 \
  --gpu-memory-utilization 0.75 \
  --enforce-eager \
  --seed 1024 \
  > /task-runtime/vllm.log 2>&1 &

查看日志,等待服务就绪:

tail -f /task-runtime/vllm.log

按 Ctrl+C 退出日志查看,再执行 exit 返回宿主机。

参数说明
--tensor-parallel-size 1单卡推理
--dtype bfloat16BF16 权重精度
--max-model-len 2048输入和输出合计最多 2048 token
--max-num-seqs 2最大同时处理 2 条序列
--gpu-memory-utilization 0.75vLLM 显存预算比例为 75%
--enforce-eager使用 eager 执行模式

4 接口调用

在宿主机执行:

curl --fail http://127.0.0.1:18527/health

curl --fail http://127.0.0.1:18527/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "qwen2.5-coder-7b-instruct",
    "messages": [
      {"role": "user", "content": "请用Python编写一个add(a, b)函数,返回两个数的和。"}
    ],
    "temperature": 0,
    "max_tokens": 256
  }'

返回 JSON 中的 choices[0].message.content 为模型生成的内容。服务端口绑定宿主机本地地址,调用命令在同一台服务器执行。

5 停止与清理

使用结束后,在宿主机执行,停止服务并删除本次下载的模型、日志和缓存:

docker rm -f qwen25-coder-7b
rm -rf -- /opt/qwen25-coder-7b-deploy