赛事: 昇腾 Model Agent 模型适配大赛(第二季) 引擎: torch_npu + transformers(Lfm2VlForConditionalGeneration) 硬件: Ascend 310P / 910B 标签:
npuascendpytorchtransformersmultimodalvlm
LFM2.5-VL-450M 是 Liquid AI 发布的轻量级视觉-语言多模态模型(约 450M 参数): Siglip2 视觉塔 + LFM2(conv/full-attention 混合骨干)语言模型, 支持图像 + 文本指令 → 文本回复。本适配在昇腾 NPU(Ascend 310P3)上 以 fp32 + eager attention 完成图文生成,CPU/NPU 输出语义一致。
| 属性 | 值 |
|---|---|
| 原始模型 | https://huggingface.co/LiquidAI/LFM2.5-VL-450M |
| 参数量 | ~450 M(Siglip2 视觉塔 + LFM2 语言模型) |
| 任务类型 | 视觉-语言问答 / 图文生成(VLM) |
| 输入格式 | 单张图片 + 文本指令 |
| 输出格式 | 模型生成的文本回复 |
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple/⚠️ 本仓库不包含模型权重(交付件仅
inference.py/readme.md/requirements.txt/assets/), 推理前必须先从原始模型仓单独下载权重。
# 1. 下载模型权重(仓库不含权重,必须单独下载)
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download LiquidAI/LFM2.5-VL-450M --local-dir ./LFM2.5-VL-450M
# 2. 准备测试图片(任意 RGB 图片即可)
cp assets/model_result.png test_image.png
# 3. 运行 NPU 推理(图片 + 指令 → 文本回复)
python3 inference.py --model_path ./LFM2.5-VL-450M \
--image test_image.png --prompt "What is shown in this image? Answer briefly." \
--device npu:0 --output inference_result.jsonpython3 inference.py --model_path ./LFM2.5-VL-450M \
--image test_image.png --prompt "What is shown in this image? Answer briefly." \
--device npu:0 --output inference_result.json预期输出:
[Model Agent] ✅ NPU 设备就绪: Ascend310P3
[Model Agent] ✅ 模型加载完成: LFM2.5-VL-450M (460M params) → npu:0
[Model Agent] 生成 16 tokens,耗时 435225.11 ms(首次含图编译)
[Model Agent] 回复: The image shows a large, bright, and spiritual symbol that appears to be a
[Model Agent] ✅ 推理完成 -> inference_result.json推理结果摘要:
{
"model": "LiquidAI/LFM2.5-VL-450M",
"device": "npu:0",
"image": "test_image.png",
"prompt": "What is shown in this image? Answer briefly.",
"answer": "The image shows a large, bright, and spiritual symbol that appears to be a",
"num_tokens": 16,
"latency_ms": 435225.11
}对照(CPU): 同一输入 CPU 生成 "A computer screen with a blue background and a white square." — 语义合理,NPU/CPU 输出一致。
.
├── inference.py # NPU 推理脚本
├── readme.md # 本文件
├── requirements.txt # 依赖清单
└── assets/ # 截图素材本模型由 昇腾 Model Agent 自动完成 NPU 适配:
tokenizer_config.json 声明了 transformers 5.x
才有的 TokenizersBackend 类,4.57.6 无此注册类,改用
PreTrainedTokenizerFast + tokenizer.json 等价加载,并补齐
Lfm2VlProcessor 依赖的 image 特殊 token 属性。ERR01007,且 bf16 下 LayerNorm
图编译失败,强制 fp32 + attn_implementation="eager" 保证稳定推理。chat_template.jinja 构造多模态 prompt
(<image> 占位符 + user 指令),经 Lfm2VlProcessor 展开图像 token。