利用OpenVINO™ 快速部署端侧可用的MiniCPM-V4.0视觉大模型

简介: MiniCPM-V4.0是MiniCPM-V系列中最新的高效模型,参数总量为4B。该模型在 OpenCompass评测中图像理解能力超越了GPT-4.1-mini-20250414、Qwen2.5-VL-3B-Instruct和InternVL2.5-8B。凭借小巧的参数规模和高效的架构,MiniCPM-V4.0是移动端部署的理想选择。

MiniCPM-V4.0是MiniCPM-V系列中最新的高效模型,参数总量为4B。该模型在 OpenCompass评测中图像理解能力超越了GPT-4.1-mini-20250414、Qwen2.5-VL-3B-Instruct和InternVL2.5-8B。凭借小巧的参数规模和高效的架构,MiniCPM-V4.0是移动端部署的理想选择。

OpenVINO作为一个跨平台的深度学习模型部署工具,可以极大优化大语言的模型的推理性能,在充分激活硬件算力同时,降低对于内存资源的占用。本文将介绍如何利用OpenVINO工具套件Day0在本地部署MiniCPM-V4.0模型。

内容列表

1. 环境准备

2. 模型下载和转换

3. 模型部署

第一步,环境准备

通过以下命令可以搭建基于Python的模型部署环境。

python -m venv py_venv 
./py_venv/Scripts/activate.bat 
pip install -q "torch>=2.1" "torchvision" "timm>=0.9.2" "transformers>=4.45" "Pillow" "gradio>=4.40" "tqdm" "sentencepiece" "peft" "huggingface-hub>=0.24.0" --extra-index-url https://download.pytorch.org/whl/cpu
pip install -q "nncf>=2.14.0"
pip install -q "git+https://github.com/openvino-dev-samples/optimum-intel.git@minicpm4v" --extra-index-url https://download.pytorch.org/whl/cpu
pip install -q "git+https://github.com/openvino-dev-samples/optimum.git@minicpm4v" --extra-index-url https://download.pytorch.org/whl/cpu
pip install -q -U --pre "openvino>=2025.0" "openvino-tokenizers>=2025.0" "openvino-genai>=2025.0" --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly

第二步,模型下载和转换

在部署模型之前,我们首先需要将原始的PyTorch模型转换为OpenVINO的IR静态图格式,并对其进行压缩,以实现更轻量化的部署和最佳的性能表现。借助Optimum提供的命令行工具optimum-cli,我们可以一键完成模型的格式转换任务:

optimum-cli export openvino --model openbmb/MiniCPM-V-4 MiniCPM-V-4-ov --trust-remote-code --weight-format fp16 --task image-text-to-text

其中,“—model” 为模型在HuggingFace上的model id,这里我们也提前下载原始模型,并将model id替换为原始模型的本地路径,针对国内开发者,推荐使用ModelScope魔搭社区作为原始模型的下载渠道,具体加载方式可以参考ModelScope官方指南:https://www.modelscope.cn/docs/models/download

转换完成后,optimum-cli工具会把原始模型拆分在4个子模型。他们分别是: language_model,resampler_model,text_embedding_model和vision_embedding_model, 其中作为基座模型的language_model资源占用最大,因此在模型转换后,我们可以单独对其进行低精度量化,以追求更高的推理性能。通过OpenVINO的NNCF量化工具,可以快速进行模型文件里的离线量化,量化后,我们可以得到INT4精度的OpenVINO模型,包含.xml和.bin两个文件,相较FP16精度的模型文件,INT4精度模型尺寸仅为其1/4左右。

  • NNCF模型量化示例
import nncf
import openvino as ov
compression_configuration = {"mode": nncf.CompressWeightsMode.INT4_SYM, "group_size": 64, "ratio": 1.0, "all_layers": True}
ov_model_path = model_dir / "openvino_language_model.xml"
ov_int4_model_path = model_dir / "openvino_language_model_int4.xml"
ov_model = ov.Core().read_model(ov_model_path)
ov_compressed_model = nncf.compress_weights(ov_model, **compression_configuration)

第三步,模型部署

目前我们推荐是用openvino-genai来部署大语言以及生成式AI任务,它同时支持Python和C++两种编程语言,安装容量不到200MB,支持流式输出以及多种采样策略。

  • GenAI API部署示例
import openvino_genai as ov_genai
ov_model = ov_genai.VLMPipeline(model_dir, device=device.value)config = ov_genai.GenerationConfig()config.max_new_tokens = 100
def load_image(image_file):    if isinstance(image_file, str) and (image_file.startswith("http") or image_file.startswith("https")):        response = requests.get(image_file)        image = Image.open(BytesIO(response.content)).convert("RGB")    else:        image = Image.open(image_file).convert("RGB")    image_data = np.array(image.getdata()).reshape(1, image.size[1], image.size[0], 3).astype(np.byte)    return image, ov.Tensor(image_data)
def streamer(subword: str) -> bool:    """        Args:        subword: sub-word of the generated text.    Returns: Return flag corresponds whether generation should be stopped.
    """    print(subword, end="", flush=True)
image, image_tensor = load_image(image_path)
question = "What is unusual on this image?"ov_model.start_chat()output = ov_model.generate(question, image=image_tensor, generation_config=config, streamer=streamer)

openvino-genai提供了chat模式的构建方法,通过声明pipe.start_chat()以及pipe.finish_chat(),多轮聊天中的历史数据将被以kvcache的形态,在内存中进行管理,从而提升运行效率。

以下为该模型在图像理解任务中的输出示例:

The unusual aspect of this image is the cat's relaxed and vulnerable position. Typically, cats avoid exposing their bellies, which are sensitive and vulnerable areas, to potential threats. In this image, the cat is lying on its back in a cardboard box, exposing its belly and hindquarters, which is not a common sight. This behavior could indicate that the cat feels safe and comfortable in its environment, suggesting a strong bond with its owner and a sense of security in its home.

完整示例代码可以查看该notebook PR:https://github.com/openvinotoolkit/openvino_notebooks/pull/3047

其中附带的Gradio demo演示效果如下:

可以看到在仅需不到5GB内存占用的情况下,该模型便可以在Intel iGPU平台上流畅运行。
📎DM_20250812150618_001.mp4

总结

利用OpenVINO工具套件,开发者可以非常轻松地将转换后的MiniCP-V系列模型部署在Intel的硬件平台上,从而进一步在本地构建起各类基于LLM的服务和应用。

参考资料

OpenVINO

---------------------------------------

*OpenVINO and the OpenVINO logo are trademarks of Intel Corporation or its subsidiaries.

-----------------------------

目录
相关文章
|
7月前
|
缓存 API 调度
70_大模型服务部署技术对比:从框架到推理引擎
在2025年的大模型生态中,高效的服务部署技术已成为连接模型能力与实际应用的关键桥梁。随着大模型参数规模的不断扩大和应用场景的日益复杂,如何在有限的硬件资源下实现高性能、低延迟的推理服务,成为了所有大模型应用开发者面临的核心挑战。
949 0
|
7月前
|
监控 安全 数据安全/隐私保护
55_大模型部署:从云端到边缘的全场景实践
随着大型语言模型(LLM)技术的飞速发展,从实验室走向产业化应用已成为必然趋势。2025年,大模型部署不再局限于传统的云端集中式架构,而是向云端-边缘协同的分布式部署模式演进。这种转变不仅解决了纯云端部署在延迟、隐私和成本方面的痛点,还为大模型在各行业的广泛应用开辟了新的可能性。本文将深入剖析大模型部署的核心技术、架构设计、工程实践及最新进展,为企业和开发者提供从云端到边缘的全场景部署指南。
2065 1
|
7月前
|
人工智能 监控 安全
06_LLM安全与伦理:部署大模型的防护指南
随着大型语言模型(LLM)在各行业的广泛应用,其安全风险和伦理问题日益凸显。2025年,全球LLM市场规模已超过6400亿美元,年复合增长率达30.4%,但与之相伴的是安全威胁的复杂化和伦理挑战的多元化
861 0
|
8月前
|
人工智能 云栖大会
2025云栖大会大模型应用开发与部署|门票申领
2025云栖大会大模型应用开发与部署门票申领
730 9
|
8月前
|
算法 安全 开发者
大模型部署指南:从个人玩转到企业级应用,这4款工具必看!
本文介绍了五款主流大语言模型部署工具,帮助用户根据需求选择合适的方案。包括适合个人使用的 Ollama 和 LM Studio、优化低配设备运行的 llama.cpp、企业级部署的 vLLM,以及 Hugging Face 推出的 TGI 框架,覆盖从本地体验到高性能服务的多种场景。
|
9月前
|
人工智能 JavaScript 前端开发
​​大模型开发从入门到部署
本内容系统讲解大语言模型技术,涵盖BERT、GPT等主流架构,深入Transformer原理与自注意力机制,结合PyTorch实战,详解张量操作、自动求导与模型训练,并介绍RAG、Agent等典型应用场景,助你掌握AI核心技术。
1259 0
|
10月前
|
人工智能 搜索推荐 Linux
ollama部署本地DeepSeek大模型
本地部署大模型具有省钱省心、数据安全、使用自由、无需联网、量身定制及响应高效等优势。DeepSeek 提供满血版与多种蒸馏版模型,适配不同硬件条件。通过 Ollama 可便捷部署,并结合客户端工具如 AnythingLLM 提升交互体验,打造个性化本地 AI 助手。
1270 0
|
11月前
|
人工智能 弹性计算 自然语言处理
从0到1部署大模型,计算巢模型市场让小白秒变专家
阿里云计算巢模型市场依托阿里云弹性计算资源,支持私有化部署,集成通义千问、通义万象、Stable Diffusion等领先AI模型,覆盖大语言模型、文生图、多模态、文生视频等场景。模型部署在用户云账号下,30分钟极速上线,保障数据安全与权限自主控制,适用于企业级私有部署及快速原型验证场景。

热门文章

最新文章