Lfm2VlForConditionalGeneration 三段式结构——SigLIP-B16 视觉编码器 + 像素重排下采样(downsample_factor=2)+ MLP projector + LFM2.5 混合架构语言模型(conv 与 full_attention 层交替,block_dim=1024)。
参数规模: 约 450M(model.safetensors 约 0.9GB),最大图像 token 数 256,最多 10 tiles 切图。| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch / torch_npu | 2.1.0+ | NPU 上需安装 torch_npu |
| transformers | >= 4.57 | Lfm2VlForConditionalGeneration / AutoProcessor 加载 |
| accelerate | 随 transformers 安装 | 权重加载加速 |
| pillow | 任意较新版本 | 图像读取与内置示例图生成 |
| 昇腾驱动 | CANN 8.0.RC2+ | 推荐最新版 |
安装命令:
pip install --target /mnt/workspace/LFM2.5-VL-450M/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers>=4.57" accelerate pillow# 检查 NPU 设备
npu-smi info
# 权重已就位
ls /home/developer/models/LFM2.5-VL-450M/ # config.json model.safetensors tokenizer.json processor_config.json ...PYTHONPATH=/mnt/workspace/LFM2.5-VL-450M/libs python3 inference.py --model_path /home/developer/models/LFM2.5-VL-450M脚本自动选择设备(npu > cuda > cpu),不传图片时使用内置合成的示例图(红矩形/蓝圆形/绿条形图案)进行图文问答。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model_path | str | /home/developer/models/LFM2.5-VL-450M | 本地权重目录 |
| --device | str | 自动(npu > cuda > cpu) | 推理设备 |
| --image | str | 内置合成示例图 | 输入图片路径 |
| --prompt | str | Describe this image briefly. | 提问文本 |
| --max_new_tokens | int | 64 | 最大新生成 token 数 |
输入:
python3 inference.py --model_path /home/developer/models/LFM2.5-VL-450M
# 提问: Describe this image briefly.
# 图像: 内置合成图(白底红色矩形 + 蓝色圆形 + 绿色长条)输出:
[INFO] device = npu
[INFO] 模型加载完成 load_time=xx.xxs
[INFO] 模型回答: The image shows a red square, a blue circle, and a green rectangle on a white background.
[RESULT] status=ok device=npu 耗时/elapsed=x.xxs输入:
python3 inference.py --model_path /home/developer/models/LFM2.5-VL-450M \
--image ./assets/sample_input.png --prompt "这张图片里有哪些形状和颜色?"输出:
[INFO] device = npu
[INFO] 模型回答: 图片中有红色的正方形、蓝色的圆形和绿色的长方形。
[RESULT] status=ok device=npu 耗时/elapsed=x.xxsassets/model_result.png)


/home/developer/models/LFM2.5-VL-450M/,仓库内 .gitignore 已排除 *.safetensors 等大文件;/mnt/workspace/LFM2.5-VL-450M/libs(pip install --target),通过 PYTHONPATH 叠加使用,勿污染共享环境;transformers 需 >= 4.57 才包含 lfm2_vl 架构;torch_dtype=torch.float32 加载模型(原生 bf16 权重会自动上转型转),注意力实现优先 sdpa、失败时回退 eager,未使用 flash_attention_2;HF_HUB_OFFLINE=1 且 from_pretrained(..., local_files_only=True),仅从本地路径加载,推理过程不联网;max_image_tokens=256 限制视觉 token 上限。