MiniCPM-V-4.6 是 OpenBMB 发布的端侧多模态大模型,采用 MiniCPMV4_6 架构(1.3B 参数), 支持图文对话、OCR、视觉问答等任务,主打高推理效率与端侧可部署性。
本项目完成其在**华为昇腾 NPU(Ascend 910B)**上的推理适配与验证。
| 项目 | 说明 |
|---|---|
| 模型架构 | MiniCPMV4_6ForConditionalGeneration(minicpmv4_6) |
| 参数量 | 1.3B |
| 视觉编码器 | 27 层 ViT(hidden 1152,patch 14,image_size 980) |
| 文本模型 | MiniCPM4(linear_attention + full_attention 混合) |
| 权重格式 | safetensors(BF16) |
| 支持任务 | 图文对话、OCR、视觉问答 |
| 输入 | 图片 + 文本问题 |
| 输出 | 文本回答 |
| 项目 | 要求 |
|---|---|
| 硬件 | Ascend 910B 系列(≥1 卡) |
| CANN | ≥ 8.0(source /usr/local/Ascend/ascend-toolkit/set_env.sh) |
| Python | 3.10 – 3.11 |
| torch / torch_npu | 2.9.0 / 2.9.0.post1 |
| transformers | ≥ 5.0(含 minicpmv4_6 架构) |
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install torch_npu==2.9.0.post1 transformers>=5.0 pillow safetensors \
-i https://repo.huaweicloud.com/repository/pypi/simple/权重通过 HuggingFace 镜像获取:
git clone --depth 1 https://hf-mirror.com/openbmb/MiniCPM-V-4.6 model_src权重目录包含:model.safetensors(2.5GB)/ tokenizer.json / preprocessor_config.json /
chat_template.jinja 等。
cd /workspace/MiniCPM-V-4.6
python3 inference.py --model_path ./model_src \
--image 图片.png --question "What is in the image?" --device npu:0| 参数 | 说明 | 默认值 |
|---|---|---|
--model_path | 模型权重目录 | ./model_src |
--image | 输入图片(真实图片,必选) | - |
--question | 文本问题 | Describe this image in detail. |
--device | 推理设备(npu:0 / cpu) | npu:0 |
--max_new_tokens | 最大生成 token 数 | 512 |
--output | 结果保存文件(可选) | stdout |
设备解析:NPU 优先,自动回退 CPU。
输入:真实手写表单图片(980×506)+ 问题
"What type of document is this? Describe what you see briefly."
模型回答:
This is a code or programming document, likely containing HTML or a similar markup.
It shows some text and a code block or syntax highlighting.模型基于真实图片正确描述了文档内容,NPU 推理链路完整跑通(1.3B 模型,BF16)。
.to(npu) 迁移即可。processor.apply_chat_template(..., downsample_mode="16x", max_slice_nums=36)
生成输入——vit_merger 依赖该方式产生的 patch 数量;
若用普通 processor(text=..., images=...) 调用会报
shape '[b, seq, dim]' is invalid for input of size ... 形状错误。torch.no_grad() + model.generate(..., downsample_mode="resample")。resolve_device():NPU 可用性检测,失败自动回退 CPU。*.safetensors / *.bin 已加入 .gitignore,
仓库仅包含推理脚本、模型卡片与截图素材。.
├── README.md # 模型卡片(本文件)
├── inference.py # NPU 推理脚本
└── assets/ # 适配过程截图素材