L
Lumiire/SmolVLM-256M-Instruct
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SmolVLM-256M-Instruct(昇腾 NPU 适配版)

简介

SmolVLM-256M-Instruct 是 HuggingFaceTB 发布的一系列轻量级视觉-语言模型(VLM)中的最小可部署成员。它以 Idefics3 架构为基础:视觉侧采用 SigLIP 风格的视觉塔(输入 512×512、patch 16×16,12 层 Transformer,hidden 768),文本侧采用 SmolLM2 风格的稠密自回归解码器(30 层、hidden 576、9 个注意力头、vocab 49280),通过一个 modality projection 连接器把图像 patch 特征映射进文本语义空间。scale_factor=4 表示每个图像 patch 展开为 4 个图像 token,image_token_id=49190 用于在输入序列中标记图像位置。

本仓库将官方权重(HuggingFaceTB/SmolVLM-256M-Instruct,单文件 safetensors,513,028,808 字节)在 昇腾 Ascend NPU 上完成部署适配,采用 vLLM-Ascend 服务化推理 形态,对外暴露 OpenAI 兼容的 /v1/chat/completions 接口,支持单图多轮图文对话。

属性值
模型名SmolVLM-256M-Instruct
架构Idefics3ForConditionalGeneration(model_type=idefics3)
文本骨干hidden=576 / layers=30 / heads=9 / kv_heads=3 / max_len=8192
视觉骨干hidden=768 / layers=12 / patch=16 / image_size=512
连接器modality_projection(proj.weight)
图像 tokenimage_token_id=49190,scale_factor=4,单图≈4096 token
权重文件model.safetensors(513,028,808 字节,单文件)
加载后权重0.48 GB(bf16)
部署形态vLLM-Ascend 0.18.0 · chat/completions · OpenAI 兼容接口

昇腾 NPU 适配说明

SmolVLM-256M-Instruct 的 config.json 声明 architectures=["Idefics3ForConditionalGeneration"]、model_type=idefics3。在 vLLM 0.18.0 的模型注册表中,Idefics3ForConditionalGeneration 已映射到 idefics3 模型实现(vllm/model_executor/models/idefics3.py),架构原生受支持,无需新增或改写模型代码,本次工作属于「权重拉取 + 服务化 + 验证」的完整适配链路:

  • 算子兼容性:Idefics3 实现仅依赖 nn.Linear、nn.LayerNorm、nn.Embedding、视觉塔的 nn.Conv2d(patch embedding)以及标准 attention 算子,均为昇腾原生算子,无 CUDA/Triton 专属内核,算子门禁通过。
  • 框架侧:vllm-ascend 未对 idefics3 建模做 override,attention 走 Ascend 后端,图编译采用 PIECEWISE(ACL Graph)模式。
  • 权重拉取顺序:按 GitCode 镜像(ai.gitcode.com/hf_mirrors/HuggingFaceTB/SmolVLM-256M-Instruct)→ ModelScope → HuggingFace 的顺序尝试;本次实测 GitCode 镜像对连续请求触发 429 限流,遂按既定回退顺序从 ModelScope(HuggingFaceTB/SmolVLM-256M-Instruct)完成拉取,本地单文件 safetensors 校验通过。
  • 设备映射:本机 npu-smi 物理设备 Phy-ID 为 12/13,torch_npu 通过 ASCEND_RT_VISIBLE_DEVICES(逻辑索引)做映射;本次指定逻辑索引 1 即物理 davinci13,避开同机其他任务占用的 davinci12。

验证环境

组件版本
操作系统openEuler(Linux 5.10.0,aarch64)
昇腾设备Ascend910 ×2(Atlas 800,单卡 HBM 64GB)
CANN8.5.1(npu-smi 25.5.5)
Python3.11.14
PyTorch2.9.0
torch_npu2.9.0.post1
vLLM0.18.0
vLLM-Ascend0.18.0
transformers4.57.6

服务启动

1. 安装依赖

pip install -r requirements.txt

2. 启动 vLLM-Ascend 服务

export ASCEND_RT_VISIBLE_DEVICES=1      # 逻辑索引 1 = 物理 davinci13(空闲卡)
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
vllm serve /path/to/SmolVLM-256M-Instruct \
  --host 0.0.0.0 \
  --port 8383 \
  --served-model-name SmolVLM-256M-Instruct \
  --tensor-parallel-size 1 \
  --max-model-len 8192 \
  --max-num-seqs 8 \
  --gpu-memory-utilization 0.2 \
  --limit-mm-per-prompt '{"image": 1}' \
  --trust-remote-code \
  --seed 42

启动后暴露的接口:

  • GET /v1/models —— 就绪检查与模型信息
  • POST /v1/chat/completions —— 图文对话推理(消息 content 支持 text 与 image_url 多模态条目)

3. 运行推理

python inference.py --prompt "Describe this picture." --image scene.png

Smoke 验证

服务启动后的最小验证:

# 1. 就绪检查
curl -sf http://127.0.0.1:8383/v1/models
# {"object":"list","data":[{"id":"SmolVLM-256M-Instruct",...,"max_model_len":8192,...}]}

# 2. 纯文本对话
curl -s -X POST http://127.0.0.1:8383/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"SmolVLM-256M-Instruct","messages":[{"role":"user","content":"What is a large language model? Answer in one sentence."}],"max_tokens":80}'

# 3. 图文对话(图片以 base64 data-URI 传入)
python inference.py --prompt "What is in this image?" --image sunset_scene.png

实测一次图文推理:

prompt  : Describe what you see in this picture, including the colors, objects and scene.
response: A rectangular piece of art depicting two mountains and the sun in the background.
prompt_tokens: 1151   completion_tokens: 16   total_tokens: 1167
latency : 267.0 ms

模型正确识别出图像中的「太阳」与「两座山」及其相对关系,图文理解链路完整可用。

性能参考

在 Atlas 800(Ascend910,davinci13 单卡)上对已部署服务的实测(n=10,热调用,单图 512×512 + 文本提示,max_tokens=120):

指标数值
TTFT P5058.0 ms
TTFT P9059.4 ms
总延迟 P50228.6 ms
总延迟 P90260.6 ms
吞吐44.39 req/s
  • NPU 侧进程(VLLMEngineCor)HBM 占用约 12,912 MB(由 --gpu-memory-utilization 0.2 预分配决定),空闲态 AICore ≈ 0%。
  • 长文本生成(300 tokens)期间实测 AICore 利用率约 11%–13%,生成结束后回落至 0%。模型规模仅 256M,对 910B 算力而言计算负载很小,利用率低属正常现象。
  • 首次请求包含图编译/图像编码器预热开销,后续回到稳态;解码速率约 50–60 tokens/s。

精度测评

SmolVLM-256M-Instruct 为 256M 量级的轻量图文模型,vLLM-Ascend 端以 bf16 运行。功能/精度验证采用图文理解定性比对(模型输出与图像内容语义一致性检查):

测试输入模型输出判定
落日场景图(太阳+两座山+湖面)"A rectangular piece of art depicting two mountains and the sun in the background."正确识别主要对象与空间关系
含 "Ascend NPU" 文字图"Ascent NPU."正确识别图中文字(OCR 语义)
纯文本 "What is a large language model?""A large language model is a computer program that can understand and generate text..."语义正确、语句通顺

说明:受模型参数量限制,生成文本偏简洁,偶有同义改写(如 "Ascent NPU"),不影响理解正确性;如需更高精度,建议选用 SmolVLM 更大尺寸版本。

注意事项

  1. 设备映射:torch_npu 通过 ASCEND_RT_VISIBLE_DEVICES(逻辑索引 0/1)映射物理卡(本机 Phy-ID 12/13),启动前请用 npu-smi info 确认空闲卡,勿与同机其他任务冲突。
  2. 端口:多会话共用机器时先检查端口占用;本仓库默认 8383,可用 --port 调整。
  3. 图像 token 开销:单张 512×512 图像经 patch16 + scale_factor=4 展开约 4096 个图像 token,会显著占用 max-model-len;--max-model-len 8192 下单图 + 文本约 1150 输入 token。
  4. --limit-mm-per-prompt 格式:vLLM-Ascend 需使用 JSON 字符串格式 '{"image": 1}',旧写法 image=1 会被拒绝。
  5. 首次延迟:首次请求含算子编译与图像编码器预热,延迟偏高,属一次性开销。
  6. AICore 利用率:256M 模型在 910B 上利用率低(峰值约 12%),属预期,不应据此误判为性能问题。
  7. 许可证:模型权重遵循上游 Apache-2.0 许可,请遵守原始仓库的使用条款。

致谢

感谢 HuggingFaceTB 团队开源 SmolVLM 系列模型;本仓库的部署与验证基于昇腾 CANN / vLLM-Ascend 生态完成。