本仓库为 LiquidAI/LFM2.5-VL-450M 视觉-语言模型 (VLM) 在 昇腾 Ascend NPU 上的推理适配工程。
lfm2_vl · 结构 Lfm2VlForConditionalGenerationnpu:0 (Ascend910, CANN 8.5.1, torch_npu 2.9.0)| 组件 | 版本 |
|---|---|
| 昇腾设备 | Ascend910 64GB HBM (npu-smi 可见, Health=OK) |
| CANN | 8.5.1 |
| Python | 3.11 |
| torch | 2.9.0+cpu (ascend 专用包) |
| torch_npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| fastapi / uvicorn | 0.123.10 / 0.x (服务模式可选) |
说明: 官方仓库的
tokenizer_config.json声明了 transformers 5.x 的TokenizersBackendtokenizer 类 (4.57 中不存在)。本工程内置兼容路径: 用PreTrainedTokenizerFast直接加载tokenizer.json并补齐 processor 所需的 image 特殊 token 属性, 无需修改只读的 site-packages。
# 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 离线加载。
{
"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
}.venv/bin/python inference.py --server --port 8111/ 提供 HTML 上传表单 (图片 + 文本提示词)POST /predict 接受 multipart/form-data:
file: 图片文件 (必填)text: 文本提示词 (可选, 默认 Describe this image in detail.)curl -X POST http://127.0.0.1:8111/predict \
-F file=@sample_input.png \
-F text="What is in this image?"{
"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/ # 权重缓存 (符号链接, 不入库)


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