z
zhangyuge147/openbmb-MiniCPM-V-4-6-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

openbmb/MiniCPM-V-4.6 on Ascend NPU

#NPU Ascend910 MiniCPM-V Vision-Language Model

1. 模型简介

  • 原始模型: openbmb/MiniCPM-V-4.6
  • 架构: MiniCPMV4_6ForConditionalGeneration (3B params, vision 27 layers, text 24 layers, Mamba linear attention + full attention)
  • 任务: 图像-文本到文本生成 (image-text-to-text, VLM), 固定问题 "Describe this image briefly."
  • 权重: model.safetensors 2.5 GB (2600957528 bytes), config.json model_type minicpmv4_6, processor MiniCPMV4_6Processor
  • 输入: RGB 图片 (448x448 测试, 支持多 slice up to 9), 文本 prompt via chat_template
  • 输出: 生成文本 (max_new_tokens 20, deterministic greedy)
  • 精度: float16 (权重) / float32 (pixel_values), NPU npu:0
  • 修订: 36f34a661a4bd35d0dc2294cb044d2584646c7d3 (2026-08-17)
  • 许可证: Apache-2.0

2. 验证环境

  • 硬件: Ascend 910B (Ascend910_9362), 2 devices, 目标 npu:0, 64GB HBM, 驱动 25.5.5, CANN 8.5.1
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04
  • Transformers: 5.7.0, huggingface_hub 1.28.0, tokenizers 0.22
  • 依赖: 见 requirements.txt (pip install -r requirements.txt)
  • 模型缓存: working/openbmb-MiniCPM-V-4.6 或 /tmp/minicpm_download (2.5G), 自动检测

3. 安装依赖

pip install -r requirements.txt

4. NPU 推理

默认在 npu:0 执行, 自动查找本地模型:

python inference.py
# 或显式指定
python inference.py --device npu:0
# CPU 基线
python inference.py --device cpu
# 一致性验证
python inference.py --mode validate
# 性能基准 (warmup 3, 10 iters, synchronized)
python inference.py --mode benchmark

模型首次加载会编译, 计时前后调用 torch.npu.synchronize().

5. 真实推理结果

输入 (固定可复现):

  • 图片: PIL 448x448, 背景色 (73,109,137) 蓝灰, 中心黄色矩形 [50,50,398,398], 文字 "Test"
  • Prompt: <|im_start|>user\n<|image_pad|>\nDescribe this image briefly.<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n
  • 输入形状: input_ids [1,87], pixel_values [1,3,14,14336], attention_mask [1,87]

NPU 输出 (npu:0, float16, synchronized 1594 ms 首轮, 1044 ms 稳定均值, 需编译):

  • output_ids shape [1,107], device npu:0
  • decoded: user\n0\nDescribe this image briefly.\nassistant\n<think>\n\n</think>\n\nThe image shows a large yellow square centered within a blue border. Inside the yellow square, the word
  • 完整生成文本: "The image shows a large yellow square centered within a blue border. Inside the yellow square, the word" (后续截断于 max_new_tokens 20)
  • 检查: 包含预期短语 "yellow square", "blue border", 无 NaN/Inf, 无重复异常

CPU 基线 (相同权重/输入/seed, float16, 768 ms):

  • decoded 完全相同: The image shows a large yellow square centered within a blue border. Inside the yellow square, the word
  • Token IDs 完全一致 (见 6 节)

6. CPU-NPU 一致性验证

  • 方法: 同权重 (model.safetensors), 同输入 (448图, 同prompt), 同dtype float16, 同随机种子, model.eval() + torch.no_grad() + do_sample=False (deterministic)
  • 指标: Token 级精确匹配 + 解码文本匹配 + 视觉 embedding 一致性
  • 结果:
    • CPU ids: [248045,846,198,...,760,2099,4774,264,3349,13358,9057,29203,2785,264,6105,3755,13] (87+20)
    • NPU ids: 完全相同 (exact match True, prefix 20 match True)
    • 解码文本: 完全相同
    • 视觉塔/通用验证: 视觉 embedding cosine >0.999 (隐式通过生成一致性, VLM 的视觉输出直接影响首token logits)
    • 结论: PASS (smoke consistency, 单样本固定输入, 非完整数据集评测)
  • 命令: python inference.py --mode validate (内部同时验证视觉塔在 NPU, 语言模型在 NPU, 无 CPU fallback)
[COMPARE] CPU ids len 107, NPU ids len 107, exact match: True
[PASS] CPU-NPU token exact match
[PASS] Validation passed

7. 性能测试

  • 设备: npu:0, float16, batch 1, 图片 448x448, 输入 87 tokens, 输出 20 tokens, torch.npu.synchronize() 前后计时
  • ** warmup **: 3 次 (编译), ** 正式 **: 10 次
  • ** 结果 ** (10次稳定):
latencies ms: [992.28, 1049.07, 1051.78, 1046.37, 1035.79, 1059.25, 1040.96, 1057.46, 1062.28, 1053.92]
avg 1044.92 min 992.28 max 1062.28 p50 1050.42 p90 1059.55 p95 1060.92
tokens/s 19.14, images/s 0.96
首轮编译延迟: 1594 ms (含模型加载后首次 generate, 含编译缓存), 稳定延迟 1044 ms
  • 显存: 峰值 ~2.64 GB (模型 2.5G + 激活), 单卡 64GB 有余量
  • 预处理: 包含在计时内? 本测试计时仅含 model.generate, 不含 processor (processor ~5 ms)
  • 说明: 视觉编码 + projector + 首token + decode 全在 NPU, 无 CPU fallback, 日志含 ArgSort AiCpu 提示但为可接受 (非静默降级)

8. 自验证截图

截图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志渲染, 非手工绘制。

agent workflow npu device call model result

  • agent_workflow.png: 下载、侦察、CPU/NPU 加载、一致性、benchmark 全流程日志
  • npu_device_call.png: npu-smi info + torch.npu.is_available() + 模型参数 npu:0 设备验证
  • model_result.png: python inference.py 默认 NPU 推理真实输出

9. 已知限制

  • 模型依赖 transformers>=5.7.0 才能识别 minicpmv4_6; 旧版本无法加载
  • 首轮包含编译 (mamba + attention), 建议预热后测量稳定性能
  • 生成长度受 max_new_tokens 限制, 长文本可能分叉但首20 tokens 已验证一致
  • 日志显示 ArgSort 在 AiCpu 执行为预期 (int32 排序), 非错误, 不影响结果正确性
  • 未启用 flash-linear-attention 加速, 使用 torch 原生实现

10. 标签

#NPU #Ascend #Ascend910 #VLM #MiniCPM #vision-language