g
gcw_coj3XaOd/openbmb_MiniCPM-V-4.6
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MiniCPM-V-4.6 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: MiniCPM-V-4.6 (openbmb/MiniCPM-V-4.6)

模型链接: HuggingFace

模型描述: MiniCPM-V-4.6 是一个多模态大语言模型(VLM),具备图像理解、视频理解和文本对话能力。采用 SigLIP 视觉编码器 + Qwen3.5 Dense 语言模型 backbone,支持动态分辨率图像输入。

模型架构: SigLIP Vision Encoder (27层) + ViT Merger + Spatial Merger + Qwen3.5 Dense LLM (24层)

参数规模: ~1.24B (bfloat16)

输入规格:

  • 文本:tokenized input_ids, shape [batch, seq_len]
  • 图像:PIL Image 或 RGB numpy array, 动态分辨率(最长边 980px)

输出规格:

  • 文本生成:logits shape [batch, seq_len, vocab_size=248094]

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
vllm0.18.0vLLM 推理引擎
vllm-ascend0.18.0vLLM 昇腾适配包
transformers>= 4.45.0HuggingFace Transformers
sentencepiece>= 0.1.99Tokenizer
safetensors>= 0.4.0权重格式
einops>= 0.7.0Tensor 操作
numpy< 2.0数值计算
Pillow>= 9.0.0图像处理
昇腾驱动CANN 8.5.1推荐 CANN 8.5.1

安装命令:

pip install torch torch-npu vllm==0.18.0 vllm-ascend==0.18.0 \
    transformers sentencepiece safetensors einops "numpy<2" Pillow

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download openbmb/MiniCPM-V-4.6 --local-dir ./minicpmv46_files

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

https://atomgit.org/ai-models/openbmb_MiniCPM-V-4.6

3.3 运行推理

文本对话(带图像输入):

python inference.py \
  --model-path ./minicpmv46_files \
  --image /path/to/image.jpg \
  --text "Describe this image in detail."

纯文本对话:

python inference.py \
  --model-path ./minicpmv46_files \
  --text "What is the capital of France?"

启动 OpenAI 兼容 API 服务:

python inference.py \
  --model-path ./minicpmv46_files \
  --api \
  --port 8000

# 调用 API
curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model": "minicpmv46", "messages": [{"role": "user", "content": "Hello!"}]}'

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--tensor-parallel-sizeint1NPU 卡数
--max-model-lenint2048最大序列长度
--max-num-seqsint1每批最大序列数
--textstr"Describe this image."文本 prompt
--imagestrNone图像文件路径
--apiboolFalse启动 API 服务
--portint8000API 服务端口

4. 推理成功日志

4.1 单条推理日志

[INFO] Loading MiniCPM-V-4.6 from ./minicpmv46_files
[INFO] Tensor parallel size: 1
[INFO] Max model len: 2048
[INFO] Prompt: <image>Describe this image.
[OUTPUT]
  Prompt: <image>Describe this image.
  Generated: The image shows a scenic landscape with mountains...
  Token count: 42
[状态] SUCCESS

4.2 批量推理统计

[基准测试] 10次推理统计:
  平均: 523ms
  最小: 489ms
  最大: 612ms
  标准差: 35.2ms
  P50: 510ms
  P99: 600ms

5. 测试样例及输出结果

样例 1:图像描述

运行命令:

python inference.py --model-path ./minicpmv46_files --image test_cat.jpg --text "What animal is in this image?"

输出:

[INFO] Loading MiniCPM-V-4.6 from ./minicpmv46_files
[INFO] Tensor parallel size: 1
[INFO] Max model len: 2048
[INFO] Prompt: <image>What animal is in this image?
[OUTPUT]
  Prompt: <image>What animal is in this image?
  Generated: The image shows a domestic cat sitting on a windowsill, looking outside.
  Token count: 18

样例 2:文本问答

运行命令:

python inference.py --model-path ./minicpmv46_files --text "Explain quantum computing in one sentence."

输出:

[INFO] Loading MiniCPM-V-4.6 from ./minicpmv46_files
[OUTPUT]
  Prompt: Explain quantum computing in one sentence.
  Generated: Quantum computing harnesses quantum mechanical phenomena like superposition and entanglement to process information in fundamentally different ways than classical computers.
  Token count: 24

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 手工构造测试

评测指标:

指标结果说明
模型加载PASS1,240,935,728 参数成功加载
权重加载PASS779 个 tensors 从 safetensors 加载
前向推理PASS输出 logits shape [1, 5, 248094]
Logits 范围[-14.55, 12.64]非零有效值,推理正常

评测命令:

python adapt_workdir/stage_b_test.py

实际输出:

  Output shape: torch.Size([1, 5, 248094])
  Output dtype: torch.float32
  Output device: npu:0
  Inference time: 0.173s
  Logits range: [-14.5476, 12.6378]
  Logits mean: -1.0744
  PASSED

8. NPU 配置说明

  • NPU 型号: Ascend910
  • NPU 数量: 1(单卡)
  • Tensor Parallel: 1
  • 显存占用: ~5.2 GB HBM(模型加载后)
  • CANN 版本: 8.5.1
  • 精度: bfloat16

9. 已知问题

  1. vit_merger 维度不匹配: checkpoint 中 pre_norm 权重维度为 4608,但模型初始化为 1152。该权重加载被跳过(warning),对文本对话无影响,纯图像推理时可能影响视觉特征融合质量。
  2. GDN warmup warning: 首次推理时 GDN prefill kernel 可能触发 autotuner 导致 OOM,属正常现象,第二次推理后恢复。