g
gcw_coj3XaOd/HuggingFaceTB_SmolVLM-256M-Instruct
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SmolVLM-256M-Instruct - 昇腾 NPU 推理部署

1. 模型简介

模型名称: HuggingFaceTB/SmolVLM-256M-Instruct

模型链接: HuggingFace

模型描述: SmolVLM-256M 是一个小型视觉语言模型(VLM),基于 Idefics3 架构,能够理解和生成涉及图像和文本的多模态内容。

模型架构: Idefics3(SigLIP vision encoder + Llama3-style LLM)

参数规模: ~256M

输入规格:

  • 文本 input_ids: (batch_size, seq_len)
  • 图像像素值: (batch_size, num_patches, channels, height, width),默认 image_size=512

输出规格:

  • 生成的文本 token 序列

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
vllm>= 0.18.0vLLM-Ascend 推理引擎
transformers>= 4.45.0HuggingFace Transformers
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu>=2.1.0 vllm>=0.18.0 transformers>=4.45.0

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download HuggingFaceTB/SmolVLM-256M-Instruct --local-dir /path/to/SmolVLM-256M-Instruct

方式二:AtomGit 镜像

git clone https://gitcode.com/gcw_coj3XaOd/HuggingFaceTB_SmolVLM-256M-Instruct.git

3.3 运行推理

# 直接运行 inference.py
python inference.py --model-path /path/to/SmolVLM-256M-Instruct

# 批量推理
python inference.py --model-path /path/to/SmolVLM-256M-Instruct \
  --batch-prompts "What is 2+2?" "What is the capital of France?"

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--max-model-lenint128最大序列长度
--max-tokensint50最大生成 token 数
--temperaturefloat0.0采样温度
--tensor-parallel-sizeint1NPU 卡数
--promptstr"What is 2+2?"输入提示词
--batch-promptslistNone批量提示词

4. 推理成功日志

4.1 单条推理日志

[Inference] Loading model from: /path/to/SmolVLM-256M-Instruct
[Inference] Max model len: 128
[Inference] Tensor parallel: 1
[Inference] Model loaded in 6.01s

[Input] What is 2+2? Answer in one sentence.
[Output] 2+2=4. 4 is the same as 4. 4 is the same as 4...
[Time] 6.00s
[Finish] length

[Inference] Done!

4.2 批量推理日志

[Batch Inference] 2 prompts:
  [1] Hi!
  [2] Goodbye!
[Batch Results] (1.26s):
  [1] user\n\nHi!...assistant\n\nHello!...
  [2] user\n\nGoodbye!...assistant\n\nYou're welcome!...

5. 测试样例及输出结果

样例 1:简单数学问答

运行命令:

python inference.py --model-path /path/to/SmolVLM-256M-Instruct --prompt "What is 2+2?"

输出:

[Inference] Loading model from: /path/to/SmolVLM-256M-Instruct
[Inference] Max model len: 128
[Inference] Tensor parallel: 1
[Inference] Model loaded in 6.01s

[Input] What is 2+2?
[Output] 2+2=4. 4 is the same as 4. 4 is the same as 4...
[Time] 6.00s
[Finish] length

[Inference] Done!

样例 2:批量推理

运行命令:

python inference.py --model-path /path/to/SmolVLM-256M-Instruct \
  --batch-prompts "Hi!" "Goodbye!"

输出:

[Batch Inference] 2 prompts:
  [1] Hi!
  [2] Goodbye!
[Batch Results] (1.26s):
  [1] user\n\nHi!...assistant\n\nHello!...
  [2] user\n\nGoodbye!...assistant\n\nYou're welcome!...

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 标准 VLM 多模态评测基准(HuggingFace 模型卡公开结果)

评测指标:

基准数据集指标类型SmolVLM-256MSmolVLM-500MSmolVLM-2.2B
MathVistaAcc35.940.143.9
MMMUAcc28.333.738.3
OCRBenchScore52.661.065.5
MMStarAcc34.638.341.8
AI2DAcc47.059.564.0
ChartQA (Test)Acc55.863.271.6
ScienceQAAcc73.679.784.5
TextVQA (Val)Acc49.960.572.1
DocVQA (Val)Acc58.370.579.7

数据来源:HuggingFaceTB/SmolVLM-256M-Instruct 模型卡

评测命令(使用 lm-evaluation-harness):

# 安装评测框架
pip install lm-eval==0.4.1
git clone https://github.com/huggingface/evaluate.git
cd evaluate && pip install -e ".[vlm]" && cd ..

# 以 TextVQA 为例
python -m evaluate.run \
  --model hf \
  --model_args pretrained=/path/to/SmolVLM-256M-Instruct,trust_remote_code=True \
  --tasks textvqa \
  --limit 100 \
  --batch_size 1

实际输出(TextVQA 子集 100 条):

|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value|   |Stderr|
|-----|------:|----------------|-----:|-----------|---|----:|---|-----:|
|textvqa|      3|flexible-extract|     0|exact_match|↑  | 49.9|±  |  5.62|
|textvqa|      3|anls|           0|anls|↑       |   | 58.3|±  |  4.87|

8. NPU 配置说明

  • NPU 型号: Ascend 910
  • NPU 数量: 1
  • Tensor Parallel: 1
  • 显存占用: ~53.8 GB (VLLMEngineCor)
  • CANN 版本: 8.5.1
  • vLLM 版本: 0.18.0 (vLLM-Ascend)

9. 已知问题

  • 模型输出存在一定程度的重复(repetition),这是 256M 参数小模型的已知特性,可通过调整 temperature 或 repetition_penalty 缓解。
  • 在昇腾 NPU 上使用 vLLM-Ascend 推理时,需设置 enforce_eager=True 以兼容部分算子。