eardoi/SmolVLM-256M-Instruct-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

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

SmolVLM-256M-Instruct 是 HuggingFaceTB 发布的轻量级多模态视觉语言模型(VLM), 基于 Idefics3 架构(256M 参数)。支持图文对话:输入真实图片 + 文本问题, 输出文本回答。适合边缘/轻量部署场景。

本项目完成其在**华为昇腾 NPU(Ascend 910B)**上的推理适配与验证。

模型规格

项目说明
模型架构Idefics3ForConditionalGeneration(idefics3)
参数量256M
文本模型VLlama3ForCausalLM
权重格式safetensors(BF16/FP16)
支持任务图文对话(image-text-to-text)
输入图片 + 文本问题
输出文本回答

环境要求

项目要求
硬件Ascend 910B 系列(≥1 卡)
CANN≥ 8.0(source /usr/local/Ascend/ascend-toolkit/set_env.sh)
Python3.10 – 3.11
torch / torch_npu2.9.0 / 2.9.0.post1
transformers≥ 5.0

环境准备

source /usr/local/Ascend/ascend-toolkit/set_env.sh
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install torch_npu==2.9.0.post1 transformers>=5.0 pillow safetensors \
    -i https://repo.huaweicloud.com/repository/pypi/simple/

模型权重

权重通过 HuggingFace 镜像获取:

git clone --depth 1 https://hf-mirror.com/HuggingFaceTB/SmolVLM-256M-Instruct model_src

权重目录包含:model.safetensors(490MB)/ tokenizer.json / processor_config.json / preprocessor_config.json 等。

推理

cd /workspace/SmolVLM-256M-Instruct
python3 inference.py --model_path ./model_src \
    --image 图片.png --question "What is in the image?" --device npu:0

参数

参数说明默认值
--model_path模型权重目录./model_src
--image输入图片(真实图片,必选)-
--question文本问题Describe this image in detail.
--device推理设备(npu:0 / cpu)npu:0
--max_new_tokens最大生成 token 数512
--output结果保存文件(可选)stdout

设备解析:NPU 优先,自动回退 CPU。

验证结果(Ascend 910B, npu:0)

输入:真实手写表单图片(2400×1240)+ 问题 "What type of document is this? Describe what you see briefly."

模型回答:

This is a copy of a document.

模型正确识别出输入为文档图片。轻量 256M 模型回答简洁,NPU 推理链路完整跑通。

适配说明

  • NPU 算子适配要点:Idefics3 的 torch.bucketize(图像位置编码,内部调用 aclnnSearchSorted)在 NPU 上不支持 BF16(报 DT_BFLOAT16 not implemented), 需以 FP16 加载模型(FP16 在支持列表内),即可正常推理。
  • 无其他 CUDA 特有 API,直接 .to(npu) 迁移即可。
  • 推理使用 torch.no_grad() + model.generate()。
  • 设备解析 resolve_device():NPU 可用性检测,失败自动回退 CPU。
  • 模型权重不推送到仓库:*.safetensors / *.bin 已加入 .gitignore, 仓库仅包含推理脚本、模型卡片与截图素材。

目录结构

.
├── README.md          # 模型卡片(本文件)
├── inference.py       # NPU 推理脚本
└── assets/            # 适配过程截图素材

引用

  • 模型:https://huggingface.co/HuggingFaceTB/SmolVLM-256M-Instruct