MiniCPM-V 4.5
MiniCPM-V 多模态系列
用更小的权重换取更低的显存与存储占用。GGUF 适用于本地与 CPU / Metal 运行,BNB / AWQ / GPTQ 面向 GPU 推理。可直接使用官方预量化权重,也可按本页脚本自行量化。
| 格式 | 精度 | 工具 | 适用场景 |
|---|---|---|---|
| GGUF | Q4_K_M 等多档 | llama.cpp | llama.cpp / Ollama 本地运行 |
| BNB | int4 | bitsandbytes | GPU 推理,脚本最简单 |
| AWQ | 4-bit | AutoAWQ | GPU 推理,需校准数据 |
| GPTQ | 4-bit | AutoGPTQ | GPU 推理,需校准数据 |
GGUF
本指南将引导您完成将 MiniCPM-V 4.5 PyTorch 模型转换为 GGUF 格式并进行量化的过程。
本指南涉及的模型需要基于 llama.cpp/ollama 使用。
1. 下载 PyTorch 模型
首先,请从以下任一来源获取原始的 PyTorch 模型文件:
- HuggingFace: https://huggingface.co/openbmb/MiniCPM-V-4_5
- 魔搭社区: https://modelscope.cn/models/OpenBMB/MiniCPM-V-4_5
2. 将 PyTorch 模型转换为 GGUF 格式
请依次执行以下命令,以完成模型结构调整、视觉编码器转换和语言模型转换。
Bash
# 步骤 1: 对模型结构进行预处理
python ./tools/mtmd/legacy-models/minicpmv-surgery.py -m ../MiniCPM-V-4_5
# 步骤 2: 将视觉编码器转换为 GGUF 格式
python ./tools/mtmd/legacy-models/minicpmv-convert-image-encoder-to-gguf.py -m ../MiniCPM-V-4_5 --minicpmv-projector ../MiniCPM-V-4_5/minicpmv.projector --output-dir ../MiniCPM-V-4_5/ --minicpmv_version 6
# 步骤 3: 将语言模型转换为 GGUF 格式
python ./convert-hf-to-gguf.py ../MiniCPM-V-4_5/model
3. 执行 INT4 量化
转换完成后,使用 llama-quantize 工具对 F16 精度的 GGUF 模型进行 INT4 量化。
Bash
./llama-quantize ../MiniCPM-V-4_5/model/ggml-model-f16.gguf ../MiniCPM-V-4_5/model/ggml-model-Q4_K_M.gguf Q4_K_M
BNB
支持版本: MiniCPM-V 4.5 / MiniCPM-V 4.0 / MiniCPM-V 2.6 / MiniCPM-V 2.5
1. 下载模型
从 HuggingFace 下载 MiniCPM-V-4_5 模型,解压到本地目录。
2. 量化脚本
下方脚本会加载原始模型,使用 bitsandbytes 量化为 4-bit,并保存量化后的模型。
Python
import torch
from transformers import AutoModel, AutoTokenizer, BitsAndBytesConfig
from PIL import Image
import time
import GPUtil
import os
assert torch.cuda.is_available(), "CUDA is not available, but this code requires a GPU."
device = 'cuda' # 选择 GPU
model_path = '/model/MiniCPM-V-4_5' # 模型路径
save_path = './model/MiniCPM-V-4_5-int4' # 量化模型保存路径
image_path = './assets/airplane.jpeg'
# 量化配置
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
load_in_8bit=False,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_quant_storage=torch.uint8,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
llm_int8_enable_fp32_cpu_offload=False,
llm_int8_has_fp16_weight=False,
llm_int8_skip_modules=["out_proj", "kv_proj", "lm_head"],
llm_int8_threshold=6.0
)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(
model_path,
device_map=device,
quantization_config=quantization_config,
trust_remote_code=True
)
gpu_usage = GPUtil.getGPUs()[0].memoryUsed
start = time.time()
response = model.chat(
image=Image.open(image_path).convert("RGB"),
msgs=[
{
"role": "user",
"content": "图中是什么?"
}
],
tokenizer=tokenizer
)
print('量化后输出:', response)
print('量化后推理耗时:', time.time() - start)
print(f"量化后 GPU 显存占用:{round(gpu_usage/1024, 2)}GB")
# 保存量化模型与 tokenizer
os.makedirs(save_path, exist_ok=True)
model.save_pretrained(save_path, safe_serialization=True)
tokenizer.save_pretrained(save_path)
3. 预期输出
量化完成后大致会看到:
Plain Text
量化后输出: The image depicts an Airbus A380-800 aircraft in mid-flight against a clear blue sky. The airplane is predominantly white with a distinctive blue tail fin featuring a red and white logo. The fuselage has the text "Airbus A380-800" written on it, and there are Chinese characters along the side of the aircraft. The landing gear is partially extended, indicating that the plane is either taking off or preparing to land. The engines are visible under the wings, and the overall design showcases the large size and advanced engineering of the Airbus A380 model.
量化后推理耗时: 9.316158771514893
量化后 GPU 显存占用:18.97GB
量化模型会保存到 save_path 指定的目录,可用于继续微调或推理。
要自定义模型路径、图片或保存路径,修改脚本中对应的变量即可。
AWQ
支持版本: MiniCPM-V 4.5