K-ON111111/SmolVLM-256M-Instruct
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SmolVLM-256M-Instruct — 昇腾 NPU 适配模型

赛事: 昇腾 Model Agent 模型适配大赛(第二季) 引擎: torch_npu 硬件: Ascend 910B4 标签: npu ascend pytorch transformers

📖 模型简介

属性值
原始模型HuggingFaceTB/SmolVLM-256M-Instruct
参数量256 M
任务类型多模态视觉语言(图像 + 文本 -> 文本)
输入格式RGB 图像 + 文本提示
输出格式生成文本

SmolVLM 是轻量级开源视觉语言模型(Idefics3 架构),融合了强大的视觉编码器与 SmolLM2 语言主干,支持图像理解与对话式多模态推理。本适配在昇腾 NPU (Ascend 910B4)上完成图像描述推理,31 token 约 7.9 s。

🔧 环境依赖

⚠️ 本仓库不包含模型权重(交付件仅 inference.py / readme.md / requirements.txt / assets/), 推理前必须先从原始模型仓单独下载权重。

# 1. 下载模型权重(仓库不含权重,必须单独下载)
huggingface-cli download HuggingFaceTB/SmolVLM-256M-Instruct --local-dir ./ --local-dir-use-symlinks False

# 2. 安装依赖
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple/

🚀 推理步骤

# 运行 NPU 推理(图像 + 提示 -> 生成文本)
python3 inference.py --model_path . --image test_input.jpg \
    --prompt "Describe this image in one sentence." --device npu:0

🧪 测试用例

用例 1: 图像描述

python3 inference.py --model_path . --image test_input.jpg \
    --prompt "Describe this image in one sentence." --device npu:0 --max_new_tokens 80

预期输出:

[Model Agent] NPU 设备就绪: Ascend910B4 (count=1)
[Model Agent] 模型加载完成 -> npu:0 (Idefics3ForConditionalGeneration)
[Model Agent] 生成文本 (31 tokens):
 In this image I can see two giraffes and a zebra standing on the ground. In the background I can see the trees, plants and the sky.
[Model Agent] 推理时延: 7870.8 ms

📁 目录结构

.
├── inference.py          # NPU 推理脚本
├── readme.md             # 本文件
├── requirements.txt      # 依赖清单
└── assets/               # 截图素材

📝 适配说明

本模型由 昇腾 Model Agent 自动完成 NPU 适配:

  • 代码迁移: AutoModelForImageTextToText(Idefics3)+ AutoProcessor, model.to('npu:0') + torch.no_grad() 自回归生成,attn_implementation="eager" 规避 flash-attn,无 CUDA 依赖。
  • 验证: Ascend 910B4 上 bf16/float32 推理,生成文本描述准确(识别长颈鹿/斑马)。