liuhongwei-2026/SmolVLM-256M-Instruct
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SmolVLM-256M-Instruct 昇腾 NPU 部署与适配

使用 vLLM-Ascend 将 HuggingFaceTB/SmolVLM-256M-Instruct 多模态模型部署到华为昇腾 NPU,并通过 OpenAI 兼容 API 开启服务化推理。

1. 项目简介

模型来源:HuggingFaceTB/SmolVLM-256M-Instruct(或 ATOM 镜像 hf_mirrors/HuggingFaceTB/SmolVLM-256M-Instruct)

模型类型:多模态(图像 + 文本)→ 文本生成

参数量:约 256M(视觉编码器 SigLIP 9.3M ×实际 ~93M + 文本骨干 VLlama3)

架构:Idefics3ForConditionalGeneration(vLLM registry 以 SmolVLMForConditionalGeneration 为入口,继承自 Idefics3)

  • 视觉编码器:SigLIP(12 层 Transformer,hidden_size=768,patch_size=16,512×512 输入),将图像编码为视觉特征,每 512×512 图像块产生 64 个视觉 token
  • 连接器:Idefics3Connector(pixel_shuffle + 模态投影),将视觉特征下采样并与文本 embedding 对齐
  • 文本骨干:VLlama3ForCausalLM(30 层,hidden_size=576,基于 Llama 架构的自回归语言模型)
  • 词表:49,280 tokens(含图像占位符及特殊 token)
  • 权重:model.safetensors 约 513MB(bfloat16),共 471 个张量

适配状态:✔ 成功(走 vLLM registry 直接复用路径,无需新增 adapter)

2. 验证环境

组件版本
操作系统Linux aarch64
Python3.11
NPU昇腾 910(Ascend910_9362)
NPU 驱动npu-smi 25.5.5
CANN8.5.1
torch2.9.0
torch-npu2.9.0.post1
transformers4.57.6
vLLM0.18.0
vLLM-Ascend0.18.0

3. 环境安装

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 运行所必需的依赖。

4. 模型权重获取

方式一: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")

5. 启动 vLLM-Ascend 服务化推理

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 选择空闲卡。

6. 服务化推理验证

模型通过 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?"

7. 精度与性能参考

使用相同权重对比昇腾 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
  • 生成内容前缀完全一致,昇腾适配未引入数值偏差
  • 昇腾 NPU 推理较 CPU 约 10 倍加速
  • 视觉编码器(SigLIP)在多模态推理中同样通过验证:图像识别内容语义正确

8. 项目结构

SmolVLM-256M-Instruct/
├── inference.py        # vLLM-Ascend 服务化推理脚本(OpenAI 兼容)
├── README.md           # 部署说明文档
├── requirements.txt    # 环境依赖清单
└── assets/             # 截图素材文件夹
          ├── agent_workflow.png
          ├── npu_device_call.png
          └── model_result.png

9. 关键点总结

  1. 架构已存在于 vLLM registry,属于直接复用场景,无需新增 adapter;核心是算子兼容性验证与真实权重加载。
  2. --enforce-eager 对 SmolVLM 多模态模型必需。
  3. 单张 512×512 图像约产生 1149 个 prompt tokens(含视觉特征),生成前注意 max_model_len 预算。
  4. 图像通过 image_url + base64 data URI 传入(详见 inference.py)。

10. 贡献者

  • liuhongwei-2026 | 🏷 NPU 标签