使用 vLLM-Ascend 将 HuggingFaceTB/SmolVLM-256M-Instruct 多模态模型部署到华为昇腾 NPU,并通过 OpenAI 兼容 API 开启服务化推理。
模型来源:HuggingFaceTB/SmolVLM-256M-Instruct(或 ATOM 镜像 hf_mirrors/HuggingFaceTB/SmolVLM-256M-Instruct)
模型类型:多模态(图像 + 文本)→ 文本生成
参数量:约 256M(视觉编码器 SigLIP 9.3M ×实际 ~93M + 文本骨干 VLlama3)
架构:Idefics3ForConditionalGeneration(vLLM registry 以 SmolVLMForConditionalGeneration 为入口,继承自 Idefics3)
hidden_size=768,patch_size=16,512×512 输入),将图像编码为视觉特征,每 512×512 图像块产生 64 个视觉 tokenIdefics3Connector(pixel_shuffle + 模态投影),将视觉特征下采样并与文本 embedding 对齐VLlama3ForCausalLM(30 层,hidden_size=576,基于 Llama 架构的自回归语言模型)model.safetensors 约 513MB(bfloat16),共 471 个张量适配状态:✔ 成功(走 vLLM registry 直接复用路径,无需新增 adapter)
| 组件 | 版本 |
|---|---|
| 操作系统 | Linux aarch64 |
| Python | 3.11 |
| NPU | 昇腾 910(Ascend910_9362) |
| NPU 驱动 | npu-smi 25.5.5 |
| CANN | 8.5.1 |
| torch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| vLLM | 0.18.0 |
| vLLM-Ascend | 0.18.0 |
pip install vllm==0.18.0 vllm-ascend==0.18.0 \
torch==2.9.0 torch-npu==2.9.0.post1 \
transformers==4.57.6 safetensors accelerate \
Pillow num2words说明:
num2words为 SmolVLM processor 运行所必需的依赖。
方式一:ATOM 镜像克隆
git lfs install
git clone https://ai.gitcode.com/hf_mirrors/HuggingFaceTB/SmolVLM-256M-Instruct.git方式二:HuggingFace 直连(如网络可达)
from huggingface_hub import snapshot_download
snapshot_download("HuggingFaceTB/SmolVLM-256M-Instruct", local_dir="./SmolVLM-256M-Instruct")方式三:ModelScope
from modelscope import snapshot_download
snapshot_download("HuggingFaceTB/SmolVLM-256M-Instruct", local_dir="./SmolVLM-256M-Instruct")vllm serve ./SmolVLM-256M-Instruct \
--dtype bfloat16 \
--tensor-parallel-size 1 \
--max-model-len 8192 \
--max-num-seqs 8 \
--port 8025 \
--enforce-eager \
--host 0.0.0.0 \
--served-model-name smolvlm-256m-instruct启动成功关键日志:
Loading safetensors checkpoint shards: 100% Completed | 1/1
Loading weights took 0.57 seconds
Multi-modal warmup completed in 0.961s
Starting vLLM server on http://0.0.0.0:8025
Application startup complete.注意事项:
--enforce-eager必需:绕过 torch.compile/CUDAGraph 在昇腾上的编译路径(多模态视觉编码器需禁用高阶优化)。--tensor-parallel-size 1单卡部署即可(权重仅约 0.48GB)。- 可通过
--gpu-memory-utilization 0.2收紧 vLLM 默认预分配的大 KV cache(默认约 54GB),以便共享 NPU。ASCEND_VISIBLE_DEVICES环境变量决定可见 NPU,多卡时可用npu-smi info选择空闲卡。
模型通过 vLLM-Ascend 提供 OpenAI 兼容的 /v1/chat/completions 接口,支持图像 + 文本多模态输入。图像以 image_url(base64 data URI)方式传入。
python inference.py \
--api http://0.0.0.0:8025/v1 \
--model smolvlm-256m-instruct \
--image path/to/image.jpg \
--prompt "Can you describe this image?"文本 + 图像(多模态)示例:
python inference.py \
--image https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg \
--prompt "Can you describe this image?"使用相同权重对比昇腾 NPU(vLLM-Ascend)与 CPU(transformers Idefics3)在相同文本输入下的生成结果:
| 平台 | 输出 | 耗时 |
|---|---|---|
| CPU (transformers, bf16) | The capital of Japan is Tokyo.(+完整句子) | 3.1 s |
| 昇腾 NPU (vLLM-Ascend, bf16) | The capital of Japan is Tokyo. | 0.3 s |
SmolVLM-256M-Instruct/
├── inference.py # vLLM-Ascend 服务化推理脚本(OpenAI 兼容)
├── README.md # 部署说明文档
├── requirements.txt # 环境依赖清单
└── assets/ # 截图素材文件夹
├── agent_workflow.png
├── npu_device_call.png
└── model_result.png--enforce-eager 对 SmolVLM 多模态模型必需。max_model_len 预算。image_url + base64 data URI 传入(详见 inference.py)。