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

LFM2.5-VL-450M(昇腾 NPU 适配版)

简介

LFM2.5-VL-450M 是 Liquid AI 推出的多模态视觉语言模型,是 LFM2-VL-450M 的升级版本,基于更新后的 LFM2.5-350M 语言骨干与 SigLIP2 视觉编码器构建。模型支持图文对话、多语言视觉理解、目标检测(边界框预测)与函数调用等能力,原生分辨率处理最高 512×512,大图自动切分为 512×512 tile 并叠加缩略图编码。

本仓库基于官方权重在 昇腾 Ascend NPU 上完成部署适配,采用 vLLM-Ascend 服务化推理(OpenAI 兼容接口)对外提供能力。模型核心参数如下:

属性值
架构Lfm2VlForConditionalGeneration(vLLM 注册名 Lfm2VLForConditionalGeneration)
文本骨干LFM2(hidden=1024,16 层:10 层 Mamba conv + 6 层 full_attention,GQA 16/8 heads)
视觉编码器SigLIP2(hidden=768,12 层,patch=16,约 86M 参数)
参数量448,718,848(约 448M,349 个张量)
权重文件model.safetensors(897,484,568 字节,bf16,sha256 2f6deb5d…d412eea)
词表 / 上下文65,536 / 32,768
图像 token<image> = 396;tile=512,max_tiles=10,do_image_splitting=true
部署形态vLLM-Ascend 0.18.0 · chat 任务 · OpenAI 兼容接口

昇腾适配要点

架构 Lfm2VlForConditionalGeneration 在 vLLM 0.18.0 模型注册表中已存在,registry.py 直接映射到 lfm2_vl.Lfm2VLForConditionalGeneration,无需新增模型代码,本次属于「复用 + 排障」策略。适配过程中定位并解决了两处平台侧/环境侧问题:

  1. Mamba 页对齐断言(vllm-ascend 平台补丁):vllm_ascend/patch/platform/patch_mamba_config.py 使用严格 == 断言要求 Mamba 状态页大小与注意力块页大小完全相等。LFM2 的短卷积状态为 (conv_L_cache-1, conv_dim)=(2,1024),bf16 下仅 4096 B/页,远小于最小注意力块页(128 token × 1024 B = 131072 B),导致启动即抛 Cannot align ssm_page_size and attn_page_size。适配方案:在启动入口 run_server.py 中以运行时补丁重写该 verify_and_update_config,将不变量放宽为「注意力页大小 ≥ Mamba 页大小」,随后沿用既有的页填充逻辑(mamba_page_size_padded = attn_page_size + conv_block_page_size)。实测 block_size=128、mamba_page_size_padded=266240,Ascend 缓存连续性要求不变。

  2. tokenizer_class 兼容:官方 tokenizer_config.json 声明 tokenizer_class: TokenizersBackend,当前 transformers 4.57.6 尚无该类,AutoTokenizer 加载失败。适配方案:改为 PreTrainedTokenizerFast(tokenizer.json 为完整 fast 序列化),并将模型专用图像 token(<image>/<|image_start|>/<|image_end|>/<|img_thumbnail|>)写入 extra_special_tokens 字典,使 Lfm2VlProcessor 正确读取 tokenizer.image_token 等属性。图像 token id 与官方一致(<image>=396)。

验证环境

组件版本
操作系统openEuler(Linux 5.10.0,aarch64)
昇腾设备Ascend910 ×2(Atlas 800 A2,单卡 HBM 64GB)
CANN8.5.1(npu-smi 25.5.5)
Python3.11.14
PyTorch / torch_npu2.9.0 / 2.9.0.post1
vLLM / vLLM-Ascend0.18.0 / 0.18.0
transformers4.57.6

权重拉取顺序:GitCode 镜像(LiquidAI/LFM2.5-VL-450M,GitCode hub 拉取成功)→ ModelScope → HuggingFace。本次实测经 GitCode 镜像完成,model.safetensors sha256 与上游一致。

服务启动

1. 安装依赖

pip install -r requirements.txt

2. 启动 vLLM-Ascend 服务

模型目录含 run_server.py(内含 Mamba 页对齐补丁),直接以其启动:

cd /path/to/LFM2.5-VL-450M
ASCEND_RT_VISIBLE_DEVICES=1 python3 run_server.py \
  --model /path/to/LFM2.5-VL-450M \
  --served-model-name LFM2.5-VL-450M \
  --dtype bfloat16 \
  --max-model-len 8192 \
  --max-num-seqs 8 \
  --limit-mm-per-prompt '{"image": 1}' \
  --gpu-memory-utilization 0.5 \
  --port 8130 \
  --mamba-cache-mode align \
  --trust-remote-code \
  --enforce-eager

启动日志关键行:[lfm2-fix] attention block size -> 128 tokens、[lfm2-fix] mamba page size padded -> 266240、Application startup complete.

3. 运行推理

python inference.py --image test.jpg --prompt "Describe this image in detail."

Smoke 验证

服务就绪后最小验证:

# 1. 就绪检查
curl -sf http://127.0.0.1:8130/v1/models
# => {"object":"list","data":[{"id":"LFM2.5-VL-450M",...}]}

# 2. 纯文本对话
curl -s http://127.0.0.1:8130/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"LFM2.5-VL-450M","messages":[{"role":"user","content":"Say hello in one short sentence."}],"temperature":0,"max_tokens":32}'
# => "Hello!"

# 3. 图文推理
python inference.py --image face_lena.jpg --prompt "Describe this image in detail. What is shown?"

实测图文样例(Lenna 图):

prompt  : Describe this image in detail. What is shown?
latency : 1.16s     usage: prompt=279  completion=63  total=342
output  : The image features a person wearing a light-colored straw hat adorned
          with a blue feathered decoration. The hat has a wide brim and a
          ribbon-like band around it. The person has long hair and is looking
          directly at the camera. The background is blurred, with warm tones
          suggesting an indoor setting.

性能参考

下表为 Atlas 800 A2(单 chip)上服务运行期间实测数据,--enforce-eager 模式:

场景prompt tokenscompletion tokens耗时生成速率
图文短答(Lenna 图)279631.16 s~54 tok/s
图文短答(足球图)213220.46 s~48 tok/s
图文长描述2982514.40 s~57 tok/s
纯文本长文(AI 主题)45105617.73 s~60 tok/s

并发 8 路请求(各 512 token)均可正常完成,无超时;单请求平均生成速率约 5560 tok/s。服务进程 NPU 显存稳定约 31.5 GiB(由 --gpu-memory-utilization 0.5 预分配),空闲态 AICore 接近 0%,并发推理时抬升至 6~7%(450M 小模型整体以访存/搬运为瓶颈,AICore 占比天然偏低)。

精度测评

  • 图文正确性:足球图提问 "Who is this person?" 输出 Lionel Messi,目标识别准确;Lenna 图描述的帽子、羽毛、发色、构图与真实内容一致。
  • 语义连贯性:长描述(251 token)与长文(1056 token)输出段落结构完整、无乱码、无 OOM/截断异常。
  • 多模态对齐:图像 token 注入正确(prompt=279 含 64~256 图像 token),<image> 展开为 tile 占位序列后模型可稳定生成。
  • 一致性:同 prompt 多次请求生成结果稳定,未出现权重加载错误或张量形状异常。
  • 端到端:双阶段门禁通过——Stage A(--load-format dummy 随机权重)启动+前向成功;Stage B(真实权重)图文推理输出正确。

注意事项

  1. 多卡共享机型请先 npu-smi info 确认空闲 chip,再通过 ASCEND_RT_VISIBLE_DEVICES 指定,避免与其他任务抢显存;勿 kill 他人进程。
  2. --mamba-cache-mode align 为 Mamba(短卷积)层必需,缺失会触发前缀缓存/页对齐告警;--enforce-eager 用于规避图捕获在 NPU 上的不确定性,生产可尝试去掉后对比性能。
  3. 若更换 transformers 版本,需同步确认 TokenizersBackend 是否原生支持;本仓库固定使用 PreTrainedTokenizerFast 兼容写法。
  4. 官方 chat 模板(chat_template.jinja)已随权重拉取,vLLM 自动使用;自定义 system prompt 时注意 <|im_start|> 语法。
  5. inference.py 仅依赖标准库与 requests,适配任意 OpenAI 兼容端点;图像以 base64 data-URI 上传。