f
gcw_pCMO8DfP/LFM2.5-VL-450M
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

LiquidAI/LFM2.5-VL-450M · 昇腾 NPU 视觉-语言推理适配

本仓库为 LiquidAI/LFM2.5-VL-450M 视觉-语言模型 (VLM) 在 昇腾 Ascend NPU 上的推理适配工程。

  • 模型类型: lfm2_vl · 结构 Lfm2VlForConditionalGeneration
  • 视觉主干: SigLIP2 (hidden 768, patch 16, 12 层)
  • 文本主干: LFM2-350M (hidden 1024, 16 层, conv + full_attention 混合层, vocab 65536)
  • 参数量: 448.7M · 精度: bfloat16
  • 输入: 图片 + 文本提示 → 输出: 生成文本
  • 推理设备: npu:0 (Ascend910, CANN 8.5.1, torch_npu 2.9.0)

环境要求

组件版本
昇腾设备Ascend910 64GB HBM (npu-smi 可见, Health=OK)
CANN8.5.1
Python3.11
torch2.9.0+cpu (ascend 专用包)
torch_npu2.9.0.post1
transformers4.57.6
fastapi / uvicorn0.123.10 / 0.x (服务模式可选)

说明: 官方仓库的 tokenizer_config.json 声明了 transformers 5.x 的 TokenizersBackend tokenizer 类 (4.57 中不存在)。本工程内置兼容路径: 用 PreTrainedTokenizerFast 直接加载 tokenizer.json 并补齐 processor 所需的 image 特殊 token 属性, 无需修改只读的 site-packages。

快速开始 (CLI)

# 1) 初始化环境 (一次性)
python -m venv .venv
.venv/bin/pip install torch==2.9.0+cpu torch_npu==2.9.0.post1 \
    transformers==4.57.6 fastapi uvicorn python-multipart pillow safetensors

# 2) 运行推理 (首次会自动从镜像下载权重, 约 897MB)
cd /opt/atomgit/npu_adapt/LiquidAI/LFM2.5-VL-450M
.venv/bin/python inference.py --img sample_input.png --text "Describe this image in detail."

权重缓存目录: ./model_weights/LiquidAI/LFM2.5-VL-450M/ 缺失文件自动从 ai.gitcode.com/hf_mirrors → hf-mirror.com 依次下载, 下载完成后自动切换 HF_HUB_OFFLINE=1 / TRANSFORMERS_OFFLINE=1 离线加载。

CLI 输出示例

{
  "model": "LiquidAI/LFM2.5-VL-450M",
  "device": "npu:0",
  "image": "sample_input.png",
  "image_size": [540, 360],
  "prompt": "<image>\nDescribe this image in detail.",
  "response": "The image depicts a group of animals in a grassy savanna. In the foreground, there are zebras grazing. Behind them, a giraffe is standing, and to the left, a giraffe is bending its neck towards a tree. Further in the background, there are several other animals, including giraffes and zebras, scattered across the landscape. The scene is set against a backdrop of trees and a grassy plain, typical of a savanna ecosystem.",
  "inference_time_s": 2.618,
  "generated_tokens": 97
}

FastAPI 服务模式

.venv/bin/python inference.py --server --port 8111
  • 根路径 / 提供 HTML 上传表单 (图片 + 文本提示词)
  • POST /predict 接受 multipart/form-data:
    • file: 图片文件 (必填)
    • text: 文本提示词 (可选, 默认 Describe this image in detail.)
  • 返回与 CLI 相同结构的 JSON
curl -X POST http://127.0.0.1:8111/predict \
  -F file=@sample_input.png \
  -F text="What is in this image?"

FastAPI 返回示例

{
  "model": "LiquidAI/LFM2.5-VL-450M",
  "device": "npu:0",
  "image": "/opt/atomgit/npu_adapt/LiquidAI/LFM2.5-VL-450M/_uploads/upload_1787216344077.png",
  "image_size": [540, 360],
  "prompt": "<image>\nWhat is in this image?",
  "response": "There are many animals in this image.\n\nThe image shows a scene of a wildlife scene.\n\nThe image depicts a scene of a wildlife scene.\n...",
  "inference_time_s": 4.699,
  "generated_tokens": 200,
  "filename": "sample_input.png"
}

说明: 文本中缺失 <image> 占位符时, 脚本会自动补前缀 (数量=图片数), 处理器才会正确扩展图片 token 序列。

工程结构

LFM2.5-VL-450M/
├── inference.py            # 昇腾 NPU 推理脚本 (CLI + FastAPI 双模式)
├── README.md               # 本说明文档
├── sample_input.png        # 测试图片 (自然野生动物场景)
├── .gitignore              # 忽略大权重 / 虚拟环境
├── assets/                 # 截图与日志
│   ├── agent_workflow.png  # NPU 环境部署 · 依赖安装 · 任务启动
│   ├── npu_device_call.png # CLI 推理运行成功 (npu:0)
│   ├── model_result.png    # FastAPI 推理服务返回 JSON
│   └── _logs/              # 真实日志: agent_workflow.log / npu_device_call.log / predict_result.json / server.log
├── .venv -> ...            # Python 虚拟环境 (符号链接)
└── model_weights/
    └── LiquidAI/LFM2.5-VL-450M/   # 权重缓存 (符号链接, 不入库)

截图

NPU 环境部署 · 依赖安装 · 任务启动

agent_workflow

LFM2.5-VL-450M · CLI 推理运行成功

npu_device_call

LFM2.5-VL-450M NPU 推理服务

model_result

适配要点

  1. 设备初始化: init_npu() try-except 捕获, 校验 torch.npu.is_available(), 失败输出排查提示并退出。
  2. 零 CUDA: 全工程无 .cuda() / cuda 字符串, 模型与输入一律 .to("npu:0")。
  3. 权重保障: _is_valid_file 校验 JSON/safetensors 头, 拒绝限流 HTML 错误页; 双镜像 + 断点续传 + 3 次重试, 已验证缓存文件永不重复下载。
  4. 算子兼容: _patch_npu_ops() 对 torch.bucketize 做 bf16→fp32 保底, 并提供 Tensor.masked_scatter 失败兜底。
  5. 权重不匹配兼容: 官方 5.x checkpoint 的 projector 不含 layer_norm (config projector_use_layernorm=false), 而 4.57 建模代码无条件创建 LayerNorm; 加载后按 config 将 multi_modal_projector.layer_norm 替换为 nn.Identity()。
  6. 生成兜底: model.generate 失败时自动降级 use_cache=False, 仍失败则转手动逐 token argmax 解码。