OpenBMB 开源社区/MiniCPM4-0.5B
模型介绍
文件和版本
Pull Requests
讨论
分析

GitHub 仓库 | 技术报告

👋 加入我们的 Discord 和 微信 社群

最新动态

  • [2025.06.06] MiniCPM4 系列正式发布!该模型在保持同等规模下最佳性能的同时,实现了极致的效率提升!在典型的端侧芯片上,其生成速度可提升5倍以上!技术报告详见 此处。🔥🔥🔥

MiniCPM4 系列

MiniCPM4 系列是专为端侧设备设计的高效能大语言模型(LLMs),通过在模型架构、训练数据、训练算法和推理系统四个关键维度的系统性创新,实现了卓越的效率。

  • MiniCPM4-8B:MiniCPM4 系列的旗舰模型,拥有 80 亿参数,在 8T tokens 上训练而成。
  • MiniCPM4-0.5B:MiniCPM4 系列的轻量版本,拥有 0.5B 参数,在 1T tokens 上训练而成。(<-- 当前位置)
  • MiniCPM4-8B-Eagle-FRSpec:针对 FRSpec 的 Eagle 引导模型,可加速 MiniCPM4-8B 的投机推理。
  • MiniCPM4-8B-Eagle-FRSpec-QAT-cpmcu:采用 QAT 技术训练的 FRSpec Eagle 引导模型,高效融合投机推理与量化技术,为 MiniCPM4-8B 带来极致加速。
  • MiniCPM4-8B-Eagle-vLLM:vLLM 格式的 Eagle 引导模型,可加速 MiniCPM4-8B 的投机推理。
  • MiniCPM4-8B-marlin-Eagle-vLLM:vLLM 格式的量化版 Eagle 引导模型,可加速 MiniCPM4-8B 的投机推理。
  • BitCPM4-0.5B:对 MiniCPM4-0.5B 应用了极致三元量化技术,将模型参数压缩为三元值,实现了 90% 的位宽缩减。
  • BitCPM4-1B:对 MiniCPM3-1B 应用了极致三元量化技术,将模型参数压缩为三元值,实现了 90% 的位宽缩减。
  • MiniCPM4-Survey:基于 MiniCPM4-8B,以用户查询为输入,可自主生成可信、长篇的综述论文。
  • MiniCPM4-MCP:基于 MiniCPM4-8B,以用户查询和可用的 MCP 工具为输入,可自主调用相关 MCP 工具以满足用户需求。

简介

MiniCPM 4 是一款极致高效的边缘侧大模型,它通过在模型架构、学习算法、训练数据和推理系统四个维度进行高效优化,实现了极致的效率提升。

  • 🏗️ 高效模型架构:

    • InfLLM v2——可训练稀疏注意力机制:采用可训练的稀疏注意力机制架构,在 128K 长文本处理中,每个 token 仅需与不到 5% 的 token 计算相关性,大幅降低长文本的计算开销
  • 🧠 高效学习算法:

    • 模型风洞 2.0——高效可预测缩放:引入下游任务性能的缩放预测方法,实现更精准的模型训练配置搜索
    • BitCPM——极致三值量化:将模型参数位宽压缩至 3 个值,实现 90% 的极端模型位宽缩减
    • 高效训练工程优化:采用 FP8 低精度计算技术,结合多 token 预测训练策略
  • 📚 高质量训练数据:

    • UltraClean——高质量预训练数据过滤与生成:基于高效数据验证构建迭代式数据清洗策略,开源高质量中英文预训练数据集 UltraFinweb
    • UltraChat v2——高质量监督微调数据生成:构建大规模高质量监督微调数据集,涵盖知识密集型数据、推理密集型数据、指令遵循数据、长文本理解数据及工具调用数据等多个维度
  • ⚡ 高效推理系统:

    • CPM.cu——轻量级高效 CUDA 推理框架:集成稀疏注意力、模型量化和投机采样,实现高效的预填充与解码
    • ArkInfer——跨平台部署系统:支持多后端环境的高效部署,提供灵活的跨平台适配能力

使用方法

使用 Transformers 进行推理

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
torch.manual_seed(0)

path = 'openbmb/MiniCPM4-0.5B'
device = "cuda"
tokenizer = AutoTokenizer.from_pretrained(path)
model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.bfloat16, device_map=device, trust_remote_code=True)

# User can directly use the chat interface
responds, history = model.chat(tokenizer, "Write an article about Artificial Intelligence.", temperature=0.7, top_p=0.7)
print(responds)

# User can also use the generate interface
# messages = [
#     {"role": "user", "content": "Write an article about Artificial Intelligence."},
# ]
# prompt_text = tokenizer.apply_chat_template(
#     messages,
#     tokenize=False,
#     add_generation_prompt=True,
# )
# model_inputs = tokenizer([prompt_text], return_tensors="pt").to(device)

# model_outputs = model.generate(
#     **model_inputs,
#     max_new_tokens=1024,
#     top_p=0.7,
#     temperature=0.7
# )
# output_token_ids = [
#     model_outputs[i][len(model_inputs[i]):] for i in range(len(model_inputs['input_ids']))
# ]

# responses = tokenizer.batch_decode(output_token_ids, skip_special_tokens=True)[0]
# print(responses)

使用 SGLang 进行推理

目前,您需要安装我们的 SGLang 分支版本。

git clone -b openbmb https://github.com/OpenBMB/sglang.git
cd sglang

pip install --upgrade pip
pip install -e "python[all]"

您可以通过运行以下命令启动推理服务器:

python -m sglang.launch_server --model openbmb/MiniCPM4-0.5B --trust-remote-code --port 30000 --chat-template chatml

然后,您可以通过运行以下命令来使用聊天界面:

import openai

client = openai.Client(base_url=f"http://localhost:30000/v1", api_key="None")

response = client.chat.completions.create(
    model="openbmb/MiniCPM4-0.5B",
    messages=[
        {"role": "user", "content": "Write an article about Artificial Intelligence."},
    ],
    temperature=0.7,
    max_tokens=1024,
)

print(response.choices[0].message.content)

使用vLLM进行推理

目前,您需要安装最新版本的vLLM。

pip install -U vllm \
    --pre \
    --extra-index-url https://wheels.vllm.ai/nightly

然后您可以使用 vLLM 对 MiniCPM4-0.5B 进行推理:

from transformers import AutoTokenizer
from vllm import LLM, SamplingParams

model_name = "openbmb/MiniCPM4-0.5B"
prompt = [{"role": "user", "content": "Please recommend 5 tourist attractions in Beijing. "}]

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
input_text = tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)

llm = LLM(
    model=model_name,
    trust_remote_code=True,
    max_num_batched_tokens=32768, 
    dtype="bfloat16", 
    gpu_memory_utilization=0.8, 
)
sampling_params = SamplingParams(top_p=0.7, temperature=0.7, max_tokens=1024, repetition_penalty=1.02)

outputs = llm.generate(prompts=input_text, sampling_params=sampling_params)

print(outputs[0].outputs[0].text)

此外,你可以通过运行以下命令启动推理服务器:

注意:在 vLLM 的聊天 API 中,add_special_tokens 默认值为 False。这意味着重要的特殊标记(例如序列开始标记 BOS)不会被自动添加。为确保输入提示符合模型的正确格式,你应显式设置 extra_body={"add_special_tokens": True}。

vllm serve openbmb/MiniCPM4-0.5B 

然后您可以通过运行以下代码来使用聊天界面:

import openai

client = openai.Client(base_url="http://localhost:8000/v1", api_key="EMPTY")

response = client.chat.completions.create(
    model="openbmb/MiniCPM4-0.5B",
    messages=[
        {"role": "user", "content": "Write an article about Artificial Intelligence."},
    ],
    temperature=0.7,
    max_tokens=1024,
    extra_body=dict(add_special_tokens=True),  # Ensures special tokens are added for chat template
    
)

print(response.choices[0].message.content)

评估结果

在Jetson AGX Orin和RTX 4090这两款典型的端侧芯片上,MiniCPM4在长文本处理任务中展现出相较于同尺寸模型显著更快的处理速度。随着文本长度的增加,MiniCPM4的效率优势愈发明显。在Jetson AGX Orin平台上,与Qwen3-8B相比,MiniCPM4实现了约7倍的解码速度提升。

benchmark

综合评估

MiniCPM4推出了8B和0.5B参数规模的端侧版本,两者在各自类别中均达到了同类最佳性能。

benchmark

长文本评估

MiniCPM4基于32K长文本进行预训练,并通过YaRN技术实现了长度扩展。在128K长文本“大海捞针”任务中,MiniCPM4表现出卓越的性能。

long-niah

声明

  • MiniCPM作为一款语言模型,通过学习海量文本生成内容。
  • 但它并不具备理解或表达个人观点、价值判断的能力。
  • MiniCPM生成的任何内容均不代表模型开发者的观点或立场。
  • 因此,用户在使用MiniCPM生成的内容时,应自行对其进行充分评估和验证,并承担全部责任。

许可证

  • 本仓库及MiniCPM模型均基于Apache-2.0许可证发布。

引用

  • 如果您认为我们的工作有价值,请引用我们的论文。
@article{minicpm4,
  title={{MiniCPM4}: Ultra-Efficient LLMs on End Devices},
  author={MiniCPM Team},
  year={2025}
}