新闻详情

新闻详情

首页 / 资讯中心 / 详情

MiniCPM-V 1.0 技术解析与端侧部署实战:基于 Perceiver Resampler 的 64 Token 高效多模态大模型

发布时间:2026/9/10 13:24:53来源:尧图网络
MiniCPM-V 1.0 技术解析与端侧部署实战:基于 Perceiver Resampler 的 64 Token 高效多模态大模型
MiniCPM-V 1.0 技术解析与端侧部署实战基于 Perceiver Resampler 的 64 Token 高效多模态大模型【免费下载链接】MiniCPM-VA Pocket-Sized MLLM for Ultra-Efficient Image and Video Understanding on Your Phone项目地址: https://gitcode.com/GitHub_Trending/mi/MiniCPM-VMiniCPM-V 1.0 是 MiniCPM-V 系列中面向端侧部署的高效视觉语言模型版本仓库归档于 2024-05-19对应文档见 docs/minicpm_v1.md它由 SigLIP-400M 视觉编码器与 MiniCPM-2.4B 语言模型通过 perceiver resampler 连接而成将整张图片的视觉表示压缩为仅 64 个 token。本文将以该文档为核心结合当前仓库源码chat.py、omnilmm/model/resampler.py 等逐层拆解其架构设计、评测表现、环境安装、多轮对话推理、MacMPS与手机端部署方法帮助你完整掌握这一「口袋级」多模态模型的原理与实战用法。模型总览SigLIP-400M MiniCPM-2.4B Perceiver ResamplerMiniCPM-V 1.0 采用经典的「视觉编码器 连接器 语言模型」三段式架构视觉编码器SigLIP-400M负责将输入图片编码为视觉特征序列语言模型MiniCPM-2.4B作为多模态对话的推理底座连接器perceiver resampler将视觉特征压缩为固定数量的 query 向量后送入语言模型。当前仓库的omnilmm/子模块中保留了同源的 perceiver resampler 实现omnilmm/model/resampler.py其核心定义如下class Resampler(nn.Module): A 2D perceiver-resampler network with one cross attention layers by (grid_size**2) learnable queries and 2d sincos pos_emb Outputs: A tensor with the shape of (grid_size**2, embed_dim) 从实现可以看到 resampler 的工作机制通过grid_size**2个可学习的 query 与视觉特征做单层交叉注意力cross attention最终输出形状为(grid_size**2, embed_dim)的固定长度特征。其中 query 使用 2D sincos 位置编码注入空间信息get_2d_sincos_pos_embed并通过kv_proj将视觉特征维度对齐到语言模型的隐藏维度。在 omnilmm/model/omnilmm.py 的create_vision_module中resampler 的 query 数量由配置项num_query决定resampler Resampler( grid_sizeint(math.sqrt(config.num_query)), embed_dimembed_dim, num_headsembed_dim // 128, kv_dimvision_tower.embed_dim, )对于 MiniCPM-V 1.0num_query为 64即 8×8 网格这正是文档中所说「将图片压缩为 64 个视觉 token」的由来。核心特性⚡️ 高效率64 Token 带来的推理开销优势在视觉编码阶段MiniCPM-V 1.0 通过 perceiver resampler 将图像表示压缩为64 个 token这一数量显著低于基于 MLP 架构的其他多模态大模型通常超过 512 个 token。更少的视觉 token 意味着语言模型自回归解码时的 KV-Cache 与注意力计算开销更小整体显存占用更低、推理速度更快因此可以高效部署在绝大多数 GPU 显卡、个人电脑甚至手机等端侧设备上。从当前仓库 chat.py 中可见MiniCPM-V 1.0 对应的封装类MiniCPMV仅需AutoModel.from_pretrained(model_path, trust_remote_codeTrue)配合tokenizer即可完成加载与对话说明其在工程上是按轻量级端侧模型设计的class MiniCPMV: def __init__(self, model_path) - None: self.model AutoModel.from_pretrained(model_path, trust_remote_codeTrue).to(dtypetorch.bfloat16) self.tokenizer AutoTokenizer.from_pretrained(model_path, trust_remote_codeTrue) self.model.eval().cuda() def chat(self, input): image Image.open(io.BytesIO(base64.b64decode(input[image]))).convert(RGB) msgs json.loads(input[question]) answer, context, _ self.model.chat( imageimage, msgsmsgs, contextNone, tokenizerself.tokenizer, samplingTrue, temperature0.7) return answer 性能同参数量级中的突出表现MiniCPM-V 1.0 在 MMMU、MME、MMBench 等多个基准上取得了同规模模型中的领先成绩超越基于 Phi-2 构建的既有多模态模型甚至达到或超过9.6B 的 Qwen-VL-Chat的水平。详细评测数据见下一节「评测结果」。 中英双语多模态交互MiniCPM-V 1.0 是首个支持中英双语多模态交互的端侧可部署多模态模型。这一能力来源于将多模态能力跨语言泛化的技术对应 ICLR 2024 spotlight 论文 VisCPM 系列工作使得同一个端侧模型既能理解中文图片内容、也能以中文/英文进行对话。评测结果文档给出了 MiniCPM-V 1.0 与同期主流多模态模型LLaVA-Phi、MobileVLM、Imp-v1、Qwen-VL-Chat、CogVLM在 MME、MMB、MMMU、CMMMU 等基准上的对比结果完整数据如下模型参数量Visual TokensMMEMMB dev (en)MMB dev (zh)MMMU valCMMMU valLLaVA-Phi3B576133559.8---MobileVLM3B144128959.6---Imp-v13B576143466.5---Qwen-VL-Chat9.6B256148760.656.735.930.7CogVLM17.4B1225143863.753.832.1-MiniCPM-V 1.03B64145267.965.337.232.1几个值得注意的要点Visual Tokens 只有 64 个仅为 LLaVA-Phi / Imp-v1576 个的约九分之一、MobileVLM144 个的不到一半这正是其端侧高效的关键在 MMB dev中英双语上以 67.9 / 65.3 领先于表中所有对比模型包括参数量 17.4B 的 CogVLM在 MMMU val / CMMMU val 上分别达到 37.2 / 32.1超过 9.6B 的 Qwen-VL-Chat35.9 / 30.7。端侧部署示例MiniCPM-V 1.0 已在真实端侧设备上完成部署验证演示视频为一加 9R 手机上的原始屏幕录制未做任何剪辑处理见本文开头的两段 GIF中文「蛇」场景与英文「蘑菇」场景。这表明该模型可以脱离 GPU 服务器直接在手机等端侧设备上完成实时图像理解与问答。环境安装文档给出的安装流程如下适用于 Python 3.10 环境克隆仓库并进入源码目录git clone https://gitcode.com/GitHub_Trending/mi/MiniCPM-V cd MiniCPM-V创建 conda 环境conda create -n minicpm-v python3.10 -y conda activate minicpm-v安装依赖pip install -r requirements.txt当前仓库根目录下的 requirements.txt 中已声明模型加载与推理所需的依赖transformers、torch、accelerate 等若需复现 chat.py 中的完整推理链路还依赖omnilmm子模块及其模型实现omnilmm/model/omnilmm.py。注意本文所依据的 docs/minicpm_v1.md 为 2024-05-19 归档版本其中的OmniLMMChat类来自当时的 OmniLMM 代码库当前仓库 chat.py 已演进为统一的MiniCPMVChat入口见下文「多轮对话」调用方式略有差异但核心接口保持一致。推理实战Model Zoo模型说明下载MiniCPM-V 1.0面向端侧部署的高效版本Hugging Faceopenbmb/MiniCPM-VModelScopeOpenBMB/MiniCPM-V多轮对话方式一按归档文档使用OmniLMMChat历史接口from chat import OmniLMMChat, img2base64 chat_model OmniLMMChat(openbmb/MiniCPM-V) im_64 img2base64(./assets/worldmap_ck.jpg) # First round chat msgs [{role: user, content: What is interesting about this image?}] inputs {image: im_64, question: json.dumps(msgs)} answer chat_model.chat(inputs) print(answer) # Second round chat # pass history context of multi-turn conversation msgs.append({role: assistant, content: answer}) msgs.append({role: user, content: Where is China in the image}) inputs {image: im_64, question: json.dumps(msgs)} answer chat_model.chat(inputs) print(answer)输入图片为仓库内的 assets/worldmap_ck.jpg这段代码体现了多轮对话的两个关键约定图片以 base64 字符串传输img2base64读取图片字节并做 Base64 编码对应 chat.py 中img2base64的实现服务端再以Image.open(io.BytesIO(base64.b64decode(...)))还原为 PIL 图像对话历史以 JSON 数组传递msgs中按{role: user/assistant, content: ...}交替追加第二轮对话时将第一轮的answer追加进历史从而保持上下文连贯。方式二当前仓库的MiniCPMVChat统一入口推荐当前仓库 chat.py 提供的统一入口为MiniCPMVChat它会根据模型路径自动路由到对应实现class MiniCPMVChat: def __init__(self, model_path, multi_gpusFalse) - None: if 12B in model_path: self.model OmniLMM12B(model_path) elif MiniCPM-Llama3-V in model_path: self.model MiniCPMV2_5(model_path) elif MiniCPM-V-2_6 in model_path: self.model MiniCPMV2_6(model_path, multi_gpus) else: self.model MiniCPMV(model_path) # openbmb/MiniCPM-V 1.0 走这里 def chat(self, input): return self.model.chat(input)即传入openbmb/MiniCPM-V时自动落到MiniCPMV类chat.py采用trust_remote_codeTrue加载模型权重并在对话时以samplingTrue, temperature0.7采样生成。chat.py底部的__main__分支还给出了一个可直接运行的最小多轮对话示例模型路径为openbmb/OmniLMM-12B时走 OmniLMM 分支改传openbmb/MiniCPM-V即可用于 1.0 版本。在 Mac 上推理MPSMiniCPM-V 1.0 可以在带 MPSApple Silicon 或 AMD GPU的 Mac 上运行。将以下内容保存为test.py# test.py import torch from PIL import Image from transformers import AutoModel, AutoTokenizer model AutoModel.from_pretrained(openbmb/MiniCPM-V, trust_remote_codeTrue, torch_dtypetorch.bfloat16) model model.to(devicemps, dtypetorch.float16) tokenizer AutoTokenizer.from_pretrained(openbmb/MiniCPM-V, trust_remote_codeTrue) model.eval() image Image.open(./assets/worldmap_ck.jpg).convert(RGB) question What is interesting about this image? msgs [{role: user, content: question}] answer, context, _ model.chat( imageimage, msgsmsgs, contextNone, tokenizertokenizer, samplingTrue ) print(answer)运行命令PYTORCH_ENABLE_MPS_FALLBACK1 python test.py注意两点加载后需将模型显式迁移到mps设备并转为float16model.to(devicemps, dtypetorch.float16)因为 MPS 对bfloat16支持有限PYTORCH_ENABLE_MPS_FALLBACK1用于让 MPS 上不支持的算子自动回退到 CPU 实现保证推理链路完整可跑通。手机端部署Android / HarmonyMiniCPM-V 1.0 支持部署在Android 与 Harmony鸿蒙操作系统的手机上。官方通过 mlc-MiniCPM 项目提供手机端 APK 与部署方案可结合 docs/minicpm_v2.md 中「MiniCPM-V 1.0GPU 约 7 GB 显存最轻量、推理最快」的定位选择部署目标设备。这是 MiniCPM-V 系列「端侧可部署」定位的最直接体现——模型先做视觉 token 压缩再由轻量语言模型解码从而让多模态问答真正运行在随身设备上。从源码理解 64 Token 压缩机制视觉编码与 Resampler 的完整数据流结合 omnilmm/model/omnilmm.py 的get_vision_embedding可以还原视觉特征进入语言模型前的完整链路def get_vision_embedding(self, pixel_values): vision_embedding vision_tower.forward_features(pixel_values.type(dtype)) if hasattr(vision_tower, num_prefix_tokens) and vision_tower.num_prefix_tokens 0: vision_embedding vision_embedding[:, vision_tower.num_prefix_tokens:] res self.resampler(vision_embedding) return res即图片 → 视觉编码器提取 patch 特征并去除 cls/prefix token→ resampler 交叉注意力压缩为grid_size**2个 token → 拼接到文本 token 的 embedding 序列中参与语言模型自回归生成。Resampler 的内部结构从 omnilmm/model/resampler.py 可以看到其关键组件self.pos_embed nn.Parameter(torch.from_numpy(get_2d_sincos_pos_embed(embed_dim, grid_size)).float()).requires_grad_(False) self.query nn.Parameter(torch.zeros(self.num_queries, embed_dim)) trunc_normal_(self.query, std.02) if kv_dim is not None and kv_dim ! embed_dim: self.kv_proj nn.Linear(kv_dim, embed_dim, biasFalse) else: self.kv_proj nn.Identity() self.attn nn.MultiheadAttention(embed_dim, num_heads) self.ln_q norm_layer(embed_dim) self.ln_kv norm_layer(embed_dim) self.ln_post norm_layer(embed_dim) self.proj nn.Parameter((embed_dim ** -0.5) * torch.randn(embed_dim, embed_dim))可学习 querygrid_size**2个可学习向量1.0 中为 64 个采用截断正态初始化std0.022D sincos 位置编码pos_embed由get_2d_sincos_pos_embed生成并冻结requires_grad_(False)为 query 和视觉特征注入二维空间位置信息输入分辨率变化时通过get_abs_pos做双三次插值适配KV 投影当视觉维度与语言模型隐藏维度不一致时用kv_proj线性投影对齐单层多头交叉注意力nn.MultiheadAttention(embed_dim, num_heads)其中num_heads embed_dim // 128输出投影经ln_post后再与可学习的proj矩阵相乘得到最终特征。forward 中的核心交叉注意力计算为out self.attn( self._repeat(q, N) self.pos_embed.unsqueeze(1), x pos_embed.unsqueeze(1), x, attn_maskattn_mask)[0]query 与视觉特征均叠加位置编码后参与注意力最终输出(num_queries, embed_dim)的定长表示——这就是「64 个视觉 token」的产生源头。图像预处理与特殊 Token 展开视觉侧与文本侧的衔接同样关键图像预处理推理时采用build_transform(is_trainFalse, input_sizeconfig.image_size, std_modeOPENAI_CLIP)见 omnilmm/model/utils.py即 resize 到固定输入尺寸、转 Tensor 并按 OpenAI CLIP 的 mean/std0.48145466, 0.4578275, 0.40821073/0.26862954, 0.26130258, 0.27577711归一化特殊 token 展开chat.py 中的expand_question_into_multimodal会把问题文本中的image占位符替换为im_start im_patch × image_token_len im_end序列其中image_token_len取自模型配置的num_query即 64。也就是说64 个视觉 token 会以 64 个im_patchtoken 的形式占位在输入序列中模型前向时再被 resampler 输出的真实视觉特征逐位替换对应 omnilmm/model/omnilmm.py 中get_vllm_embedding的 embedding 拼接逻辑。解码参数参考在 chat.py 的OmniLMM12B.decode中可以看到系列模型常用的采样参数同源设计可参考temperature0.6, max_new_tokens1024, do_sampleTrue, repetition_penalty1.1, top_k30, top_p0.9,而 MiniCPM-V 1.0 的MiniCPMV封装在对话时采用samplingTrue, temperature0.7chat.py。实际使用时可根据任务场景调整 temperature越高越发散与 repetition_penalty抑制重复。使用注意事项归档版本说明本文档对应的模型能力与评测数据归档于 2024-05-19当前仓库已演进到 MiniCPM-V 2.x / 4.x 系列见 README.mdMiniCPM-V 1.0 仍保留openbmb/MiniCPM-V权重标识可供加载使用。显存需求结合 docs/minicpm_v2.md 的 Model Zoo 信息MiniCPM-V 1.0 为系列中最轻量版本GPU 推理约需 7 GB 显存适合多数消费级显卡与个人电脑。精度选择NVIDIA GPU 上默认以bfloat16加载chat.py 中MiniCPMV使用torch.bfloat16若显卡不支持 bf16如 V100、T4、RTX 2080可参考 web_demos/web_demo.py 中的做法切换为fp16Mac MPS 上则统一使用fp16并配合PYTORCH_ENABLE_MPS_FALLBACK1。多轮上下文多轮对话时务必把上一轮的answer以assistant角色追加回msgs否则模型无法感知历史对话内容。【免费下载链接】MiniCPM-VA Pocket-Sized MLLM for Ultra-Efficient Image and Video Understanding on Your Phone项目地址: https://gitcode.com/GitHub_Trending/mi/MiniCPM-V创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

GrapesJS Keymaps 模块完全指南:自定义编辑器快捷键 2026/9/10 14:07:02

GrapesJS Keymaps 模块完全指南:自定义编辑器快捷键

GrapesJS Keymaps 模块完全指南:自定义编辑器快捷键 【免费下载链接】grapesjs Free and Open source Web Builder Framework. Next generation tool for building templates without coding 项目地址: https://gitcode.com/GitHub_Trending/gr/grapesjs 导读…

阅读更多 →
格雷厄姆企业估值法:价值投资的核心工具 2026/9/10 14:07:02

格雷厄姆企业估值法:价值投资的核心工具

1. 价值投资的基石:格雷厄姆企业估值法解析在华尔街流传着这样一句话:"每一个价值投资者口袋里都装着一本《证券分析》。"作为价值投资理论的奠基人,本杰明格雷厄姆(Benjamin Graham)提出的企业估值方法&…

阅读更多 →
Python+OpenCV实现人脸识别系统全流程 2026/9/10 14:07:02

Python+OpenCV实现人脸识别系统全流程

1. 项目概述人脸识别作为计算机视觉领域最基础也最实用的技术之一,已经广泛应用于安防监控、身份验证、智能相册等场景。本文将带你从零开始,使用Python和OpenCV实现一个完整的人脸识别系统。不同于简单的调用API,我们会深入底层原理&#xf…

阅读更多 →
使用 Agno 构建文本分类数据标注流水线:从固定标签到置信度与推理依据 2026/9/10 14:07:02

使用 Agno 构建文本分类数据标注流水线:从固定标签到置信度与推理依据

使用 Agno 构建文本分类数据标注流水线:从固定标签到置信度与推理依据 【免费下载链接】agno Build, run, and manage agent platforms. 项目地址: https://gitcode.com/GitHub_Trending/ag/agno 文本分类(Text Classification)是数据…

阅读更多 →
OpenHuman RLM 工作流 Phase 2:为 TinyAgents 补齐宿主嵌入能力(外部取消、实时事件与异步文档化) 2026/9/10 14:07:02

OpenHuman RLM 工作流 Phase 2:为 TinyAgents 补齐宿主嵌入能力(外部取消、实时事件与异步文档化)

OpenHuman RLM 工作流 Phase 2:为 TinyAgents 补齐宿主嵌入能力(外部取消、实时事件与异步文档化) 【免费下载链接】openhuman OpenHuman is an open source personal AI for Mac, Windows and Linux — local-first memory, agent orchestra…

阅读更多 →
10吨葫芦减速器设计与制造关键技术解析 2026/9/10 14:04:01

10吨葫芦减速器设计与制造关键技术解析

1. 10吨葫芦减速器概述10吨葫芦减速器是工业起重设备中的核心传动部件,主要用于桥式起重机、门式起重机等重型物料搬运设备。作为连接电机与卷筒的关键装置,它承担着降低转速、增大扭矩的核心功能,直接决定了起重机的负载能力和运行稳定性。在…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞