昇腾 Model-Agent 模型适配大赛(第二季)· 适配赛道交付件 基于 Ascend 910 NPU + torch_npu 的 image-text-to-text 推理适配(VLM 视觉问答),CPU/NPU 精度对齐 gen_text/gen_ids 完全一致。
trust_remote_code=True)/v1/chat/completions(VLM 视觉问答)昇腾 NPU 平台环境已提供以下基础组件:
| 组件 | 版本 |
|---|---|
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| CANN | 8.5.1 |
| NPU 驱动 | npu-smi 25.5.5 |
| 硬件 | Ascend 910 × 2(HBM 64GB/卡) |
应用层依赖见 requirements.txt:
| 包 | pin 版本 | 用途 |
|---|---|---|
| transformers | 4.57.6 | Lfm2VlForConditionalGeneration 加载 |
| pillow | >=10.0.0 | 图像处理 |
| fastapi | 0.115.6 | server.py 推理服务 |
| uvicorn | 0.34.0 | ASGI 服务器 |
| pydantic | 2.10.4 | 请求/响应模型 |
一键复现:也可直接运行
bash reproduce.sh ./model自动完成上述全部步骤(环境检查 → 模型加载 → NPU 推理 → CPU/NPU 精度对比),并给出截图提示。
git clone https://gitcode.com/gcw_Pp8UzTBn/LFM2.5-VL-450M-NPU.git
cd LFM2.5-VL-450M-NPUpip install -r requirements.txt# 权重缺失或为 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.safetensors | 897MB | 主权重(Lfm2VlForConditionalGeneration) |
| config.json | 2.4KB | 模型配置 |
| processor_config.json | 828B | 处理器配置 |
| tokenizer.json | 4.7MB | tokenizer |
| chat_template.jinja | 3.8KB | 对话模板 |
| generation_config.json | 136B | 生成配置 |
python inference.py --compare --model ./model --max_new_tokens 32 --output comparison.jsonexport MODEL_PATH=./model
python server.py
# 服务监听 0.0.0.0:8000/v1/chat/completionscurl -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
}'在赛事平台「模型评测」tab,选择本模型权重仓库,点击「可用性测试」,等待 10-30 分钟查看日志。
命令:
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..."),回答可读(防假通过)。
命令:
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(防假) | True | True | ✅ |
| 推理时延 | 5.898s | 3.043s | NPU 加速 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 |
三张官方指定截图(由参赛用户在真机/云 NPU 终端补位):
| 截图 | 文件名 | 内容要求 |
|---|---|---|
| Agent 完整适配工作流 | assets/agent_workflow.png | 适配全过程,体现完整流程 |
| NPU 硬件设备调用日志 | assets/npu_device_call.png | npu-smi info 输出 + 推理 NPU 调用日志 |
| 模型最终适配验收结果 | assets/model_result.png | 推理输出 + 精度对比结果 |
本适配仓库遵循原模型许可证(Apache-2.0)。模型权重版权归原模型作者所有。