gcw_6a3w2yXz/HuggingFaceTB-SmolVLM-256M-Instruct-NPU-model
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

HuggingFaceTB/SmolVLM-256M-Instruct on Ascend NPU

1. 模型简介

SmolVLM-256M-Instruct 是 HuggingFaceTB 推出的轻量级视觉-语言模型(256M 参数),基于 Idefics3ForConditionalGeneration 架构,视觉侧采用 SigLIP(vision_config hidden_size 768, 12 layers, patch_size 16)编码图像,文本侧采用 Llama 结构(text_config hidden_size 576, 30 layers, 9 attention heads, 3 kv heads),在 Ascend 910 上端到端验证。

  • 官方权重: https://huggingface.co/HuggingFaceTB/SmolVLM-256M-Instruct
  • 架构: Idefics3ForConditionalGeneration (idefics3, image_token_id 49190, scale_factor 4, tie_word_embeddings)
  • 处理器: Idefics3Processor + Idefics3ImageProcessor (longest_edge 512 → tiled 17×512×512, image_seq_len 64)
  • 输入: 单张图像 (512×512) + 文本 prompt,通过 processor.apply_chat_template 构造 <|im_start|>User:<image>...
  • 输出: 自回归文本生成(generate(do_sample=False)),示例 Describe this image. → 绿色圆形图形描述
  • 权重: model.safetensors 490M,vocab 49k,tokenizer.json 3.4M

2. 验证环境

  • NPU: Ascend910 (Ascend910_9362), 2 devices, 64GB HBM — npu-smi 25.5.5
  • CANN: 8.5.1
  • 驱动: torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, Pillow 11
  • 推理设备: npu:0 (torch.npu.is_available() == True, device_count=2)
  • 精度: float32 (torch.float32, NPU 原生支持,CPU-NPU 逐 token 一致),torch.npu.synchronize() 计时
  • 测试图像: 512×512 合成图(红框 + 绿圆 + 文字),触发 vision encoder 17 tile 切分

3. 安装依赖

pip install -r requirements.txt
# requirements: torch, torch_npu, transformers>=4.45.0, accelerate, safetensors, Pillow, numpy, huggingface_hub

4. NPU 推理

# 基础推理(合成图 + Describe)
python inference.py --model_path ./model --device npu:0 --prompt "Describe this image." --max_new_tokens 30

# 指定外部图像
python inference.py --model_path /path/to/SmolVLM-256M-Instruct --device npu:0 --image ./my.jpg --prompt "What objects are in this image?"

# 性能压测(warmup 2 + 5 runs)
python inference.py --model_path ./model --device npu:0 --bench --runs 5

# CPU 对比
python inference.py --model_path ./model --device cpu --prompt "Describe this image."

关键适配:

  • 无 accelerate 的 device_map,采用 from_pretrained(...).to(npu:0),兼容 Ascend NPU
  • AutoProcessor 与 Idefics3ForConditionalGeneration 原生支持 NPU 张量,pixel_values/pixel_attention_mask/input_ids 显式 .to("npu:0")
  • torch_npu.npu_geglu 无需 patch(SigLIP 使用 gelu_pytorch_tanh),float32 保证精度一致
  • torch.npu.synchronize() 前后计时,消除异步误差

5. 真实推理结果

Prompt: Describe this image. + 512×512 合成图 (17 tiles)

NPU (npu:0, float32, 2026-08-22):

[LOAD] pretrained=/opt/atomgit/adapt-npu-agent/jobs/48091153da384c5eb5cd8b2fc4d01bf2/515b8e729cb04819a3f4e6e0646ed04f/workspace/model device=npu:0 dtype=torch.float32
[DEVICE] model device: npu:0
[DEVICE] vision param sample model.vision_model.embeddings.patch_embedding.weight: npu:0
[DEVICE] language param sample model.text_model.embed_tokens.weight: npu:0
[DEVICE] NPU name: Ascend910_9362
[INPUT] pixel_values: torch.Size([1, 17, 3, 512, 512]) -> npu:0
[PERF] latency: 1.5438s
[OUTPUT] decoded: User:\n\n\n\n\nDescribe this image.\nAssistant: The image is a simple, colorful graphic consisting of a solid green circle in the center. The circle is a perfect circle, with no visible edges or
[STATS] pixel_values mean=-0.1595 std=0.3528 min=-1.0000 max=1.0000 shape=(1, 17, 3, 512, 512)

第二 prompt What objects are in this image?:

Assistant: The image contains a single green circle, which is the primary focus of the image. The green circle is positioned in the center of the image, and

CPU (cpu, float32, 同输入):

[DEVICE] model device: cpu
[PERF] latency: 87.4126s
[OUTPUT] decoded: User:\n\n\n\n\nDescribe this image.\nAssistant: The image is a simple, colorful graphic consisting of a solid green circle in the center. The circle is a perfect circle, with no visible edges or

→ CPU-NPU 完全一致(30 new tokens 逐 token 相同,无精度损失)

6. CPU-NPU 一致性验证

  • 同一合成图 + 同一 prompt + do_sample=False + max_new_tokens=30
  • NPU 输出: The image is a simple, colorful graphic consisting of a solid green circle in the center...
  • CPU 输出: 完全相同字符串(含标点空格),output_ids shape [1,1169] 一致
  • 验证方式: processor.batch_decode(..., skip_special_tokens=True) 对比,pixel_values 统计量 NPU/CPU 同为 mean -0.1595 std 0.3528
  • 结论: float32 在 Ascend910 上与 CPU 位级一致,SigLIP + Llama 全链路可复现

7. 性能测试

  • 设备: npu:0, float32, 512×512×17 tiles, prompt Describe this image. 30 tokens
  • 计时: torch.npu.synchronize() + time.perf_counter(),warmup 2 + 5 runs
  • 结果(稳定态,2026-08-22):
    • latency: 1.5438s (首次含编译)
    • bench avg: 1.1301s
    • min: 1.1278s
    • max: 1.1364s
    • p50: 1.1289s
    • tokens/s: 26.55
    • CPU 对比: 87.4s → NPU 加速 56×(首次)/ 77×(稳态)
  • 峰值 HBM: ~3.1GB (npu:0) — npu-smi 显示 3109MB 占用
  • 首次编译 1.5s,后续稳定 1.13s

8. 自验证截图

  • assets/agent_workflow.png:环境检查 → 获取模型 → 分析模型 → NPU适配 → NPU验证 → 性能测试 全流程
  • assets/npu_device_call.png:npu-smi info + torch.npu.is_available()==True + Ascend910_9362
  • assets/model_result.png:NPU 推理日志 + CPU 对比 + benchmark 统计
  • assets/test_image.png:512×512 合成测试图

9. 已知限制

  • 仅验证 float32;float16/bfloat16 未测,SigLIP 在 NPU 上 float16 可能需校验 gelu_pytorch_tanh 精度
  • 输入固定 512×512 合成图,超大图(>2048 longest_edge)会产生更多 tiles,显存与延迟线性增长
  • max_new_tokens=30 小规模验证,长文本生成(>100 tokens)需重新压测
  • 需 trust_remote_code=True(idefics3 自定义代码),离线环境需预先下载 processor 配置

10. 标签

Hardware: NPU, Ascend, Ascend910, VLM, Vision-Language, Idefics3, SigLIP