LLMs之MiniCPM:MiniCPM(揭示端侧大语言模型的无限潜力)的简介、安装和使用方法、案例应用之详细攻略

LLMs之MiniCPM:MiniCPM(揭示端侧大语言模型的无限潜力)的简介、安装和使用方法、案例应用之详细攻略

目录

MiniCPM的简介

0、更新日志

1、公开的模型

2、局限性

3、文本模型评测

越级比较:

同级比较:

Chat模型比较:

DPO后模型比较:

MiniCPM-2B-128k 模型评测

MiniCPM-MoE-8x2B模型评测

多模态模型评测

MiniCPM的安装和使用方法

1、 模型下载

语言模型

多模态模型

2、模型推理

T1、Huggingface 模型

MiniCPM-2B

MiniCPM-2B (Llama Format)

MiniCPM-V

T2、vLLM 推理

安装vLLM

测试样例

期望输出

T3、llama.cpp、Ollama、fastllm、mlx_lm推理

llama.cpp

ollama

fastllm

mlx_lm

3、模型部署

手机部署

部署步骤

部署性能

Demo & API 部署

基于Gradio的网页版Demo

MiniCPM的案例应用


MiniCPM的简介

MiniCPM 是面壁智能与清华大学自然语言处理实验室共同开源的系列端侧大模型,主体语言模型 MiniCPM-2B 仅有 24亿(2.4B)的非词嵌入参数量, 总计2.7B参数量。

  • 经过 SFT 后,MiniCPM-2B 在公开综合性评测集上与 Mistral-7B 表现相近(中文、数学、代码能力更优),整体性能超越 Llama2-13B、MPT-30B、Falcon-40B 等模型。
  • 经过 DPO 后,MiniCPM-2B 在当前最接近用户体感的评测集 MTBench 上也超越了 Llama2-70B-Chat、Vicuna-33B、Mistral-7B-Instruct-v0.1、Zephyr-7B-alpha 等众多代表性开源大模型。
  • 以 MiniCPM-2B 为基础构建端侧多模态大模型 MiniCPM-V 2.0,在多个测试基准中实现了 7B 以下模型的最佳性能,在 OpenCompass 榜单上超过了 Qwen-VL-Chat 9.6B、CogVLM-Chat 17.4B 和 Yi-VL 34B 等更大参数规模的模型。MiniCPM-V 2.0 还展现出领先的 OCR 能力,在场景文字识别能力上接近 Gemini Pro。
  • 经过 Int4 量化后,MiniCPM 可在手机上进行部署推理,流式输出速度略高于人类说话速度。MiniCPM-V 也直接跑通了多模态大模型在手机上的部署。
  • 一张1080/2080可高效参数微调,一张3090/4090可全参数微调,一台机器可持续训练 MiniCPM,二次开发成本较低。

GitHub地址:GitHub - OpenBMB/MiniCPM: MiniCPM-2B: An end-side LLM outperforms Llama2-13B.

0、更新日志

1、公开的模型

我们完全开源MiniCPM系列的模型参数供学术研究和有限商用。 具体而言,我们目前已公开以下模型,地址详见 模型下载 部分

  • 基于MiniCPM-2B的指令微调与人类偏好对齐版本MiniCPM-2B-SFT/DPO
  • 基于MiniCPM-2B的多模态模型MiniCPM-V 2.0
  • MiniCPM-2B-SFT/DPO的Int4量化版MiniCPM-2B-SFT/DPO-Int4
  • MiniCPM-2B的128k长文本版本MiniCPM-2B-128k
  • MiniCPM-2B的MoE版本MiniCPM-MoE-8x2B
  • 更轻量级的MiniCPM-1B指令微调版本MiniCPM-1B-SFT
  • 基于MLC-LLM、LLMFarm开发的MiniCPM手机端程序,文本及多模态模型均可在手机端进行推理
  • MiniCPM-2B训练过程中的30个Checkpoints供模型机理研究。

2、局限性

  • 受限于模型规模,模型可能出现幻觉性问题。其中由于DPO模型生成的回复内容更长,更容易出现幻觉。我们也将持续进行MiniCPM模型的迭代改进。
  • 为了保证在学术研究用途上模型的通用性,我们未对模型进行任何身份认同训练。同时由于我们用ShareGPT开源语料作为部分训练数据,模型可能会输出类似GPT系列模型的身份认同信息。
  • 受限于模型规模,模型的输出受到提示词(prompt)的影响较大,可能多次尝试产生不一致的结果。
  • 受限于模型容量,模型的知识记忆较不准确,后续我们将结合RAG方法来增强模型的知识记忆能力。

3、文本模型评测

越级比较:

模型平均分英文均分中文均分C-EvalCMMLUMMLUHumanEvalMBPPGSM8KMATHBBHARC-EARC-CHellaSwag
Llama2-7B35.4036.2131.76532.4231.1144.3212.227.1713.571.833.2375.2542.7575.62*
Qwen-7B49.4647.1959.65558.9660.3557.6517.0742.1541.245.3437.7583.4264.7675.32*
Deepseek-7B39.9639.1543.6442.8244.4547.8220.1241.4515.851.5333.3874.58*42.15*75.45*
Mistral-7B48.9749.9644.5446.1242.9662.6927.4445.233.135.041.0683.9270.7380.43*
Llama2-13B41.4842.4437.1937.3237.0654.7117.0732.5521.152.2537.9278.87*58.1979.23*
MPT-30B38.1739.8230.7229.3432.0946.5621.9535.3610.311.5638.2278.66*46.08*79.72*
Falcon-40B43.6244.2140.9340.2941.5753.5324.3936.5322.441.9236.2481.94*57.6883.26*
MiniCPM-2B52.3352.651.151.1351.0753.4650.0047.3153.8310.2436.8785.4468.0068.25

同级比较:

模型平均分英文均分中文均分C-EvalCMMLUMMLUHumanEvalMBPPGSM8KMATHBBHARC-EARC-CHellaSwag
TinyLlama-1.1B25.3625.5524.52525.0224.0324.36.7119.912.270.7428.7860.77*28.15*58.33*
Qwen-1.8B34.7231.8747.5749.8145.3243.377.9317.8019.262.4229.0763.97*43.6959.28*
Gemini Nano-3B-------27.2(report)22.8(report)-42.4(report)---
StableLM-Zephyr-3B43.4646.3130.6230.3430.8945.935.3731.8552.5412.4937.6873.7855.3871.87*
Phi-2-2B48.8454.4123.7823.3724.1852.6647.5655.0457.163.543.3986.1171.2573.07*
MiniCPM-2B52.3352.651.1051.1351.0753.4650.0047.3153.8310.2436.8785.4468.0068.25

Chat模型比较:

模型平均分英文均分中文均分C-EvalCMMLUMMLUHumanEvalMBPPGSM8KMATHBBHARC-EARC-CHellaSwag
ChatGLM2-6B37.9835.1750.6352.0549.2145.7710.379.3822.745.9632.674.4556.8258.48*
Mistral-7B-Instruct-v0.144.3645.8937.5138.0636.9653.5629.2739.3428.733.4839.5281.6163.9973.47*
Mistral-7B-Instruct-v0.250.9152.8342.23542.5541.9260.5136.5948.9540.494.9539.8186.2873.3884.55*
Qwen-7B-Chat44.9342.0557.958.5757.2356.0315.8540.5242.238.337.3464.44*39.25*74.52*
Yi-6B-Chat50.4645.8970.99570.8871.1162.9514.0228.3436.543.8837.4384.8970.3974.6*
Baichuan2-7B-Chat44.6842.7453.3953.2853.55321.3432.3225.256.3237.4679.6360.1569.23*
Deepseek-7B-chat49.3449.5648.33546.9549.7251.6740.8548.4848.524.2635.776.8563.0576.68*
Llama2-7B-Chat38.1639.1733.5934.5432.6447.6414.0227.421.152.0835.5474.2854.7875.65*
MiniCPM-2B52.3352.651.1051.1351.0753.4650.0047.3153.8310.2436.8785.4468.0068.25

DPO后模型比较:

模型MT-bench
GPT-4-turbo9.32
GPT-3.5-turbo8.39
Mistral-8*7b-Instruct-v0.18.30
Claude-2.18.18
Zephyr-7B-beta7.34
MiniCPM-2B7.25
Vicuna-33B7.12
Zephyr-7B-alpha6.88
LLaMA-2-70B-chat6.86
Mistral-7B-Instruct-v0.16.84
MPT-34B-instruct6.39

MiniCPM-2B-128k 模型评测

Modelavgavg w/o code&mathpasskeynumber_stringkv_retrievallongbook_choice_englongbook_qa_chnlongbook_qa_englongbook_sum_englongdialogue_qa_engmath_calcmath_findcode_debugcode_run
LWM-Text-128k24.4533.6210097.80.628.8215.9314.319.991.503.4320.051
Yarn-Mistral-7b-128k19.8427.3692.71027.9515.499.559.067.5017.140.761.25
Mistral-7B-Instruct-v0.2(ABF 1000w)27.7536.910078.983.637.1211.7417.3721.129.5029.4317.510
Yi-6B-200k22.1532.5410094.92036.6815.079.20.923.504.290.510.75
chatglm3-6b-128k25.5836.5789.9399.665.246.2910.78.3825.916.5085.331
MiniCPM-2.4B-128k27.3237.6898.3199.83929.6923.0616.3315.739.504.2922.080

MiniCPM-MoE-8x2B模型评测

ModelBBHMMLUCEvalCMMLUHumanEvalMBPP†GSM8KMATH
Llama2-34B*44.162.6--22.633.042.26.24
Mistral-7B-Instruct-v0.239.8160.5142.5541.9236.5939.6340.494.95
Gemma-7B*55.164.3--32.344.446.424.3
Qwen1.5-7B*40.26174.173.13637.462.520.3
Deepseek-MoE(16B)*-45.040.642.526.839.218.84.3
MiniCPM-2.4B36.8753.4651.1351.0750.0035.9353.8310.24
MiniCPM-MoE-8x2B39.2258.9058.1158.8055.4941.6861.5610.52

注:* 表示结果取自技术报告。† 表示评测集为MBPP全集。

多模态模型评测

ModelSizeTextVQA valDocVQA testOCRBenchOpenCompassMMEMMB dev(en)MMB dev(zh)MMMU valMathVistaLLaVA BenchObject HalBench
Proprietary models
Gemini Pro Vision-74.688.168063.82148.975.274.048.945.879.9-
GPT-4V-78.088.464563.21771.575.175.053.847.893.186.4 / 92.7
Open-source models 6B~34B
Yi-VL-6B6.7B45.5*17.1*29049.31915.168.668.340.328.851.9-
Qwen-VL-Chat9.6B61.562.648852.11860.060.656.737.033.867.756.2 / 80.0
Yi-VL-34B34B43.4*16.9*29052.62050.271.171.445.130.762.3-
DeepSeek-VL-7B7.3B64.7*47.0*43555.61765.474.172.838.336.877.8-
TextMonkey9.7B64.366.7558--------
CogVLM-Chat17.4B70.433.3*59052.51736.663.753.837.334.773.973.6 / 87.4
Open-source models 1B~3B
DeepSeek-VL-1.3B1.7B58.4*37.9*41346.01531.664.061.233.829.451.1-
MobileVLM V23.1B57.519.4*--1440.5(P)63.2-----
Mini-Gemini2.2B56.234.2*--1653.059.8-31.7---
MiniCPM-V2.8B60.638.236647.61650.267.965.338.328.951.378.4 / 88.5
MiniCPM-V 2.02.8B74.171.960555.01808.669.668.138.238.769.285.5 / 92.2

* 我们自己评测了正式开源的模型权重。

MiniCPM的安装和使用方法

1、 模型下载

语言模型

HuggingFaceModelScopeWiseModel
MiniCPM-2B-sft-bf16MiniCPM-2B-sft-bf16MiniCPM-2B-sft-bf16
MiniCPM-2B-dpo-bf16MiniCPM-2B-dpo-bf16MiniCPM-2B-dpo-bf16
MiniCPM-2B-128kMiniCPM-2B-128k
MiniCPM-MoE-8x2BMiniCPM-MoE-8x2B
MiniCPM-1B-sft-bf16MiniCPM-1B-sft-bf16

注: 更多模型版本见这里

多模态模型

HuggingFaceModelScopeWiseModel
MiniCPM-V 2.0MiniCPM-V 2.0
MiniCPM-VMiniCPM-VMiniCPM-V
OmniLMM-12BOmniLMM-12BOmniLMM-12B

2、模型推理

在线colab体验地址https://colab.research.google.com/drive/1tJcfPyWGWA5HezO7GKLeyeIso0HyOc0l?usp=sharing

T1、Huggingface 模型
MiniCPM-2B
  • 安装transformers>=4.36.0以及accelerate后,运行以下代码
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
torch.manual_seed(0)

path = 'openbmb/MiniCPM-2B-dpo-bf16'
tokenizer = AutoTokenizer.from_pretrained(path)
model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.bfloat16, device_map='cuda', trust_remote_code=True)

responds, history = model.chat(tokenizer, "山东省最高的山是哪座山, 它比黄山高还是矮?差距多少?", temperature=0.5, top_p=0.8, repetition_penalty=1.02)
print(responds)

期望输出

山东省最高的山是泰山,海拔1545米。
相对于黄山(海拔1864米),泰山海拔较低,相差约319米。

MiniCPM-2B (Llama Format)

我们将MiniCPM的模型权重转化成了Llama代码可以直接调用的格式,以便大家尝试:

import torch
from transformers import LlamaTokenizerFast, LlamaForCausalLM
model_path = "openbmb/MiniCPM-2B-dpo-bf16-llama-format"
tokenizer = LlamaTokenizerFast.from_pretrained(model_path)
model = LlamaForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map='cuda', trust_remote_code=True)

prompt="Now you act like a terminal situated within a beginner's C++ practice repository folder, please provide the output for the command: `ls -l`"
input_ids = tokenizer.encode("<用户>{}<AI>".format(prompt), return_tensors='pt', add_special_tokens=True).cuda()
responds = model.generate(input_ids, temperature=0.3, top_p=0.8, repetition_penalty=1.02, max_length=1024)
responds = tokenizer.decode(responds[0], skip_special_tokens=True)
print(responds)
MiniCPM-V
import torch
from PIL import Image
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True)
model.eval().cuda()

image = Image.open('xx.jpg').convert('RGB')
question = 'What is in the image?'
msgs = [{'role': 'user', 'content': question}]

res, context, _ = model.chat(
    image=image,
    msgs=msgs,
    context=None,
    tokenizer=tokenizer,
    sampling=True,
    temperature=0.7
)
print(res)
 

T2、vLLM 推理

安装vLLM
pip install "vllm>=0.4.1"
测试样例
python inference/inference_vllm.py --model_path <hf_repo_path> --prompt_path prompts/prompt_demo.txt
期望输出
<用户>: Which city is the capital of China?
<AI>:
 The capital city of China is Beijing. Beijing is a major political, cultural, and economic center in China, and it is known for its rich history, beautiful architecture, and vibrant nightlife. It is also home to many of China's most important cultural and historical sites, including the Forbidden City, the Great Wall of China, and the Temple of Heaven. Beijing is a popular destination for tourists from around the world, and it is an important hub for international business and trade.

T3、llama.cpp、Ollama、fastllm、mlx_lm推理

MiniCPM支持llama.cpp 、ollamafastllmmlx_lm推理。感谢@runfuture对llama.cpp和ollama的适配。

llama.cpp
  1. 安装llama.cpp
  2. 下载gguf形式的模型。下载链接-fp16格式 下载链接-q4km格式
  3. 在命令行运行示例代码:
./main -m ../../model_ckpts/download_from_hf/MiniCPM-2B-dpo-fp16-gguf.gguf --prompt "<用户>写藏头诗,藏头是龙年大吉<AI>" --temp 0.3 --top-p 0.8 --repeat-penalty 1.05

更多参数调整详见

ollama
  1. 安装ollama
  2. 在命令行运行:
ollama run modelbest/minicpm-2b-dpo
fastllm
import torch
from transformers import AutoTokenizer, LlamaTokenizerFast, AutoModelForCausalLM
path = 'openbmb/MiniCPM-2B-dpo-fp16'
tokenizer = AutoTokenizer.from_pretrained(path)
model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.float16, device_map='cuda', trust_remote_code=True)
from fastllm_pytools import llm
llm.set_device_map("cpu")
model = llm.from_hf(model, tokenizer, dtype = "float16") # dtype支持 "float16", "int8", "int4"
print(model.response("<用户>山东省最高的山是哪座山, 它比黄山高还是矮?差距多少?<AI>", top_p=0.8, temperature=0.5, repeat_penalty=1.02))
  1. 编译安装fastllm
  2. 模型推理
 
mlx_lm

安装mlx_lm库

pip install mlx_lm

下载转换后的模型权重MiniCPM-2B-sft-bf16-llama-format-mlx

模型推理

python -m mlx_lm.generate --model mlx-community/MiniCPM-2B-sft-bf16-llama-format-mlx --prompt "hello, tell me a joke." --trust-remote-code

3、模型部署

手机部署

部署步骤
  • 进行Int4量化后,MiniCPM只占2GB空间,具备在端侧手机进行模型部署的条件。
  • 对于不同的操作系统,我们进行了不同的适配。
  • 注意:当前开源框架对手机支持还在完善,并非所有芯片与操作系统版本均能成功运行MLC-LLM或LLMFarm。
  • Android、HarmonyOS
    • 使用开源框架MLC-LLM进行模型适配。
    • 支持文本模型、多模态模型。
    • 适用于MiniCPM-2B-SFT-INT4、MiniCPM-2B-DPO-INT4、MiniCPM-V。
    • 编译安装MiniCPM指南
  • iOS
    • 使用开源框架LLMFarm进行模型适配。
    • 支持文本模型。
    • 适用于MiniCPM-2B-SFT-INT4、MiniCPM-2B-DPO-INT4。
    • 编译安装MiniCPM指南
部署性能
  • 我们未针对手机推理模型进行深度优化和系统测试,仅验证MiniCPM使用手机芯片进行推理的可行性。我们也欢迎更多开发者进一步调优并更新下面的测试列表,不断提升端侧大模型在手机上的推理性能
手机型号操作系统处理器Memory(GB)文本吞吐(token/s)
OPPO Find N3Android 13snapdragon 8 Gen2126.5
Samsung S23 UltraAndroid 14snapdragon 8 Gen2126.4
Meizu M182QAndroid 11snapdragon 888Plus83.7
Xiaomi 12 ProAndroid 13snapdragon 8 Gen18+33.7
Xiaomi Redmi K40Android 11snapdragon 87083.5
Oneplus LE 2100Android 13snapdragon 870123.5
Oneplus HD1900Android 11snapdragon 86583.2
Oneplus HD1900Android 11snapdragon 85583.0
Oneplus HD1905Android 10snapdragon 85583.0
Oneplus HD1900Android 11snapdragon 85583.0
Xiaomi MI 8Android 9snapdragon 84562.3
Huawei Nova 11SEHarmonyOS 4.0.0snapdragon 778121.9
Xiaomi MIX 2Android 9snapdragon 83561.3
iPhone 15 ProiOS 17.2.1A17 pro818.0
iPhone 15iOS 17.2.1A16615.0
iPhone 12 ProiOS 16.5.1A1465.8
iPhone 12iOS 17.2.1A1445.8
iPhone 11iOS 16.6A1344.6
Xiaomi Redmi K50HyperOS 1.0.2MediaTek Dimensity 8100123.5
  • 我们也使用MLC-LLM验证了在手机上部署MiniCPM-V系列模型的可行性,能够正常输入输出,但也存在图片处理时间较长的问题,需要进一步优化,兼容性问题也需要进一步解决。下面的动图是使用小米14 Pro运行MiniCPM-V 2.0的屏幕录像,没有进行任何编辑。

Demo & API 部署

基于Gradio的网页版Demo
  • 使用如下命令启动基于Gradio的网页版demo:
# generation powered by vllm
python demo/vllm_based_demo.py --model_path <vllmcpm_repo_path>
# generation powered by huggingface
python demo/hf_based_demo.py --model_path <hf_repo_path>

MiniCPM的案例应用

持续更新中……

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个处女座的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值