| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch / torch_npu | 2.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# 检查 NPU 设备
npu-smi info
# 权重已就位
ls /home/developer/models/SmolVLM-256M-Instruct/ # config.json model.safetensors preprocessor_config.json tokenizer.json chat_template.json ...PYTHONPATH=/mnt/workspace/SmolVLM-256M-Instruct/libs python3 inference.py --model_path /home/developer/models/SmolVLM-256M-Instruct| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model_path | str | /home/developer/models/SmolVLM-256M-Instruct | 本地权重目录 |
| --device | str | 自动(npu > cuda > cpu) | 推理设备 |
| --question | str | Describe this image briefly. | 向模型提出的图像问题 |
| --max_new_tokens | int | 48 | 生成的最大新 token 数 |
| --output | str | assets/model_result.png | 结果图保存路径 |
输入:
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输入:
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


AutoProcessor + AutoModelForImageTextToText.from_pretrained(..., local_files_only=True) 从本地目录加载,推理全程离线;processor.apply_chat_template 组装图文输入;[ANSWER] 行打印在终端;