g
gcw_Pp8UzTBn/LFM2.5-VL-450M-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

LiquidAI/LFM2.5-VL-450M 昇腾 NPU 适配

昇腾 Model-Agent 模型适配大赛(第二季)· 适配赛道交付件 基于 Ascend 910 NPU + torch_npu 的 image-text-to-text 推理适配(VLM 视觉问答),CPU/NPU 精度对齐 gen_text/gen_ids 完全一致。

一、模型简介

  • 模型名称:LiquidAI/LFM2.5-VL-450M
  • 架构:Lfm2VlForConditionalGeneration(自定义代码,需 trust_remote_code=True)
  • 参数规模:约 450M 参数
  • 配置:Liquid Foundation Model 2.5 VLM(视觉+文本多模态生成)
  • 任务类型:image-text-to-text → /v1/chat/completions(VLM 视觉问答)
  • 原模型链接:https://huggingface.co/LiquidAI/LFM2.5-VL-450M
  • 适配仓库(本项目):https://gitcode.com/gcw_Pp8UzTBn/LFM2.5-VL-450M-NPU

二、环境依赖

昇腾 NPU 平台环境已提供以下基础组件:

组件版本
Python3.11.14
PyTorch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
CANN8.5.1
NPU 驱动npu-smi 25.5.5
硬件Ascend 910 × 2(HBM 64GB/卡)

应用层依赖见 requirements.txt:

包pin 版本用途
transformers4.57.6Lfm2VlForConditionalGeneration 加载
pillow>=10.0.0图像处理
fastapi0.115.6server.py 推理服务
uvicorn0.34.0ASGI 服务器
pydantic2.10.4请求/响应模型

三、分步推理操作流程

一键复现:也可直接运行 bash reproduce.sh ./model 自动完成上述全部步骤(环境检查 → 模型加载 → NPU 推理 → CPU/NPU 精度对比),并给出截图提示。

1. 克隆交付仓

git clone https://gitcode.com/gcw_Pp8UzTBn/LFM2.5-VL-450M-NPU.git
cd LFM2.5-VL-450M-NPU

2. 安装依赖

pip install -r requirements.txt

3. 下载模型权重(多源回退 + 并行分片 + 断点续传)

# 权重缺失或为 LFS 指针时,reproduce.sh 会自动调用 scripts/download_weights.py:
#   - 多源回退:gitcode-lfs CDN(同网最快)→ hf-mirror.com → modelscope.cn(兜底)
#   - 8 路并行分片(~2MB/片)+ 分片级 resume + 429/超时指数退避
#   - KNOWN_SIZES + sha256 完整性校验(model.safetensors 897MB)
# 也可手动执行:
python3 scripts/download_weights.py --model-dir ./model

大权重 model.safetensors(897MB)不入仓库(避免 git-lfs 依赖、节省 clone 体积), 全新 clone 后 bash reproduce.sh ./model 会自动检测:权重存在 → 直接下一步; 缺失/LFS 指针 → 自动下载并 sha256 校验(分片断点续传,中断自动重试)。

权重文件清单(共约 861MB,主权重 897MB):

文件大小用途
model.safetensors897MB主权重(Lfm2VlForConditionalGeneration)
config.json2.4KB模型配置
processor_config.json828B处理器配置
tokenizer.json4.7MBtokenizer
chat_template.jinja3.8KB对话模板
generation_config.json136B生成配置

4. CPU/NPU 精度对比

python inference.py --compare --model ./model --max_new_tokens 32 --output comparison.json

5. 启动推理服务

export MODEL_PATH=./model
python server.py
# 服务监听 0.0.0.0:8000

6. 调用 /v1/chat/completions

curl -X POST http://127.0.0.1:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "LiquidAI/LFM2.5-VL-450M",
    "messages": [{"role": "user", "content": [{"type": "text", "text": "What do you see in this image?"}, {"type": "image_url", "image_url": {"url": "data:image/png;base64,<BASE64>"}}]}],
    "max_tokens": 32,
    "temperature": 0.0
  }'

7. 平台可用性测试

在赛事平台「模型评测」tab,选择本模型权重仓库,点击「可用性测试」,等待 10-30 分钟查看日志。

四、完整测试样例及输出

测试 1:CPU/NPU 精度对比

命令:

python inference.py --compare --model /data/models/LiquidAI/LFM2.5-VL-450M --max_new_tokens 32

输出(摘要):

=== CPU 基线推理 ===
CPU gen_text: 'The image shows a close-up view of a surface, possibly a piece of wood or a metal, with a smooth, polished finish. The surface reflects light'
CPU time: 5.898s

=== NPU 推理 ===
NPU gen_text: 'The image shows a close-up view of a surface, possibly a piece of wood or a metal, with a smooth, polished finish. The surface reflects light'
NPU time: 3.043s

=== 精度对比 ===
gen_text match:    True
gen_ids match:     True
answer readable:   True  (防假通过)
CPU: 5.898s | NPU: 3.043s | speedup: 1.94x

达标判定: ✅ 通过

说明:本模型为 450M VLM(LFM2.5-VL 架构,自定义代码 trust_remote_code),输入图像 + 问题,输出视觉问答回答。CPU/NPU 两端生成文本完全一致("The image shows a close-up view of a surface..."),回答可读(防假通过)。

测试 2:server.py 端到端

命令:

curl -X POST http://127.0.0.1:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"LiquidAI/LFM2.5-VL-450M","messages":[{"role":"user","content":[{"type":"text","text":"What do you see?"},{"type":"image_url","image_url":{"url":"data:image/png;base64,<BASE64>"}}]}],"max_tokens":32,"temperature":0.0}'

输出(OpenAI 兼容格式):

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "created": 1755475200,
  "model": "LiquidAI/LFM2.5-VL-450M",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "The image shows a close-up view of a surface, possibly a piece of wood or a metal..."},
      "finish_reason": "stop"
    }
  ],
  "usage": {"prompt_tokens": 1, "completion_tokens": 32, "total_tokens": 33}
}

五、精度与性能验证

指标CPU 基线NPU达标判定
gen_text match—True✅ 完全一致
gen_ids match—True✅
answer readable(防假)TrueTrue✅
推理时延5.898s3.043sNPU 加速 1.94x

权重路径:model.safetensors sha256:见 CHECKSUM.md

六、平台提交信息

字段内容
原始权重地址https://huggingface.co/LiquidAI/LFM2.5-VL-450M
项目地址https://gitcode.com/gcw_Pp8UzTBn/LFM2.5-VL-450M-NPU
提交说明LiquidAI/LFM2.5-VL-450M(Lfm2VlForConditionalGeneration, 约 450M 参数)昇腾 NPU 适配:torch_npu float32 + eager 注意力,trust_remote_code,CPU/NPU gen_text/gen_ids 完全一致(VQA),NPU 3.043s(CPU 5.898s,加速 1.94x),模型卡片标注 #NPU

七、资产清单(assets/)

三张官方指定截图(由参赛用户在真机/云 NPU 终端补位):

截图文件名内容要求
Agent 完整适配工作流assets/agent_workflow.png适配全过程,体现完整流程
NPU 硬件设备调用日志assets/npu_device_call.pngnpu-smi info 输出 + 推理 NPU 调用日志
模型最终适配验收结果assets/model_result.png推理输出 + 精度对比结果

八、许可证

本适配仓库遵循原模型许可证(Apache-2.0)。模型权重版权归原模型作者所有。

九、引用

  • 原模型:https://huggingface.co/LiquidAI/LFM2.5-VL-450M
  • transformers:https://github.com/huggingface/transformers
  • torch_npu:https://gitee.com/ascend/pytorch
  • 昇腾 Model-Agent 大赛:https://competition.gitcode.com/competition/2083120580017467393/intro