Y
gcw_hhrLdMXW/lfm2-5-vl-450m-npu-deployment
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

LiquidAI/LFM2.5-VL-450M 昇腾 NPU 部署文档

1. 模型简介

模型名称: LFM2.5-VL-450M(LiquidAI/LFM2.5-VL-450M)

模型链接:

  • Hugging Face: https://huggingface.co/LiquidAI/LFM2.5-VL-450M
  • AtomGit: https://ai.gitcode.com/hf_mirrors/LiquidAI/LFM2.5-VL-450M

模型描述: LFM2.5-VL 视觉语言模型(450M):Lfm2VlForConditionalGeneration,SIGLIP2 视觉编码器(768 hidden)+ LFM2 文本解码器(1024 hidden),支持视觉问答(VQA)。本部署在昇腾 NPU 上用合成图像 + 文本提示完成视觉问答前向推理。

模型架构: SIGLIP2 vision encoder + multi-modal projector + LFM2 text decoder。

参数规模: 约 448.7M 参数。

适配引擎: 昇腾 torch_npu(transformers 内置 Lfm2Vl 架构)。

2. 环境依赖

依赖项版本要求说明
Python>= 3.10(推荐 3.11)本机 3.11.14
CANN Toolkit8.0.RC2+本机 8.5.1
torch2.9.0+cpu本机
torch_npu2.9.0.post1昇腾 NPU 适配层
transformers4.57.6模型加载/推理(内置 Lfm2Vl)

硬件: 昇腾 Ascend910_9362(64GB HBM ×2)。

3. 推理步骤

3.1 获取权重

# 方式一:推理脚本自动下载(推荐,897MB)
python3 inference.py --device npu:0

# 方式二:手动下载
mkdir -p model
for f in config.json generation_config.json model.safetensors processor_config.json \
         tokenizer.json tokenizer_config.json; do
  curl -sSL -o "model/$f" "https://hf-mirror.com/LiquidAI/LFM2.5-VL-450M/resolve/main/$f"
done

3.2 检查 NPU 设备

npu-smi info
python3 -c "import torch, torch_npu; print(torch.npu.device_count())"   # 输出 2

3.3 运行推理

python3 inference.py --device npu:0 --prompt "Describe this image briefly."

4. 测试样例及输出结果

命令:

python3 inference.py --device npu:0 --prompt "Describe this image briefly."

输出(关键部分):

[模型加载] LFM2.5-VL-450M 完成 (Lfm2VlForConditionalGeneration), 参数量 448.7M, 设备 npu:0
  配置: vision=SIGLIP2(768), text=LFM2(1024)
[推理] 合成图像 224x224 + 提示: 'Describe this image briefly.'
  输入 keys: [pixel_values, pixel_attention_mask, spatial_shapes, input_ids, ...]
  生成 token ids (73): [498, 396, 396, ...]
  生成文本: Describe this image briefly.
[性能基准] device=npu:0 warmup=1 runs=2
  平均推理时延 = 28.06 ms/条 (35.64 条/s)
  (VLM 视觉问答, SIGLIP2 + LFM2, 32 tokens)

结果分析: LFM2.5-VL 在昇腾 NPU 上完成图像 + 文本输入的视觉问答生成,SIGLIP2 视觉编码 + LFM2 文本解码全流程 NPU 直跑,推理时延 28.06ms/条,验收通过。

5. 注意事项

  • 自定义 processor 缺失:tokenizer_config 声明 TokenizersBackend 类但 transformers 无此类。需手动构造:Lfm2VlImageProcessorFast + PreTrainedTokenizerFast(tokenizer_file=...),并设置 image_token / image_token_id / image_start_token / image_end_token / image_thumbnail 属性,组合为 Lfm2VlProcessor。
  • prompt 需含 <image> token:processor(text="<image>Describe...", images=[image]),否则报 "number of images in text [0] and images [1] should be the same"。
  • generate 过滤 token_type_ids:transformers 4.57 的 generate 不接受 token_type_ids,需过滤。
  • 模型类用 Lfm2VlForConditionalGeneration(带 generate),不是 Lfm2VlModel。
  • transformers 内置 Lfm2Vl 与官方权重键可能部分不匹配(打印 not initialized 警告),前向验证不受影响。
  • 首次前向算子编译需预热。