Y
YYPGD/SmolVLM-256M-Instruct
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SmolVLM-256M-Instruct 昇腾NPU部署文档

1. 模型简介

模型名称: SmolVLM-256M-Instruct(HuggingFaceTB/SmolVLM-256M-Instruct) 模型链接: https://ai.gitcode.com/hf_mirrors/HuggingFaceTB/SmolVLM-256M-Instruct 模型描述: SmolVLM-256M-Instruct 是 HuggingFace 发布的超轻量级多模态视觉语言模型(VLM):输入一张图片和一段文本问题,模型输出对图像的理解/描述文本。支持图像问答、图像描述、文档理解等任务,可在消费级设备甚至浏览器端运行,适合端侧与低资源场景。 模型架构: Idefics3 架构——SigLIP-BASE 视觉编码器(约 93M)+ SmolLM2 135M 语言解码器,通过 Pixel Shuffle(4x 下采样)压缩视觉 token 数量后与文本 token 拼接送入 LLM;对话能力经多模态指令微调。 参数规模: 约 256M(model.safetensors 约 515MB,fp32)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch / torch_npu2.1.0+NPU 上需安装 torch_npu
transformers>=4.49, <5需支持 idefics3 架构的 AutoModelForImageTextToText
huggingface_hub<1.0与 transformers 4.x 配套
pillow / numpy最新版图像处理与可视化
safetensors最新版权重加载
昇腾驱动CANN 8.0.RC2+推荐最新版

安装命令:

pip install --target /mnt/workspace/SmolVLM-256M-Instruct/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers>=4.49,<5" "huggingface_hub<1.0" pillow numpy safetensors

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
# 权重已就位
ls /home/developer/models/SmolVLM-256M-Instruct/   # config.json model.safetensors preprocessor_config.json tokenizer.json chat_template.json ...

3.2 运行推理

PYTHONPATH=/mnt/workspace/SmolVLM-256M-Instruct/libs python3 inference.py --model_path /home/developer/models/SmolVLM-256M-Instruct

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/SmolVLM-256M-Instruct本地权重目录
--devicestr自动(npu > cuda > cpu)推理设备
--questionstrDescribe this image briefly.向模型提出的图像问题
--max_new_tokensint48生成的最大新 token 数
--outputstrassets/model_result.png结果图保存路径

4. 测试样例及输出结果

样例 1:内置合成样例(蓝天草地 + 红色太阳)

输入:

python3 inference.py --model_path /home/developer/models/SmolVLM-256M-Instruct

输出:

[INFO] device = npu
[INFO] 模型加载完成 load_time=xx.xs
[INFO] 输入张量: pixel_values=(1, 273, 3, 384, 384) dtype=torch.float32, input_ids=(1, xx)
[INFO] 推理完成 耗时/elapsed=x.xxxs (max_new_tokens=48, device=npu)
[ANSWER] The image shows a red sun in a blue sky over a green field...
[INFO] 结果图已保存: assets/model_result.png
[RESULT] status=ok device=npu 耗时/elapsed=x.xxxs

样例 2:自定义提问 + 限制生成长度

输入:

python3 inference.py --model_path /home/developer/models/SmolVLM-256M-Instruct --question "What colors do you see?" --max_new_tokens 24

输出:

[INFO] 推理完成 耗时/elapsed=x.xxxs ...
[ANSWER] Red, green and blue...
[RESULT] status=ok device=npu 耗时/elapsed=x.xxxs

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果

6. 注意事项

  • 推理脚本通过 torch_npu 调用 NPU,设备自动选择 npu > cuda > cpu;
  • 模型为 transformers 原生 idefics3 架构,通过 AutoProcessor + AutoModelForImageTextToText.from_pretrained(..., local_files_only=True) 从本地目录加载,推理全程离线;
  • 注意力实现使用 eager(NPU 上不使用 flash_attention_2),pixel_values 强制转 float32,全程无 float64 算子;
  • 视觉侧 SigLIP 编码器经 Pixel Shuffle 将每张 384x384 图压缩为 64 个视觉 token,长图会按 processor 配置切分为多个子图(pixel_values 的第 2 维为切分后的总子图数);
  • 对话模板由权重目录内 chat_template.json 提供,用 processor.apply_chat_template 组装图文输入;
  • 生成采用 greedy 解码(do_sample=False),保证结果可复现;
  • 结果图为输入示例图叠加提问文字,模型回答以 [ANSWER] 行打印在终端;
  • 如有精度差异请与官方 pipeline(HuggingFaceTB/SmolVLM-256M-Instruct 示例代码)输出对比确认。