LLMs之​MiniCPM3-4B:​MiniCPM3-4B的简介、安装和使用方法、案例应用之详细攻略

LLMs之​MiniCPM3-4B:​MiniCPM3-4B的简介、安装和使用方法、案例应用之详细攻略

导读: 2024年9月5日,由 OpenBMB 推出MiniCPM3-4B,这是 MiniCPM 系列的第三代模型,具备 40亿参数,其硬件支持下的推理通过 SGLang 优化后,吞吐量相比 vLLM 提高了 70%。MiniCPM3-4B 使用了包含大规模中英文语料的训练数据,支持 32k 长度上下文,并通过 LLMxMapReduce 技术实现理论上的无限上下文处理。
>> 性能表现:MiniCPM3-4B 的综合表现超越了 Phi-3.5-mini-Instruct 和 GPT-3.5-Turbo-0125,与参数量为 7B~9B 的模型性能相当,如 Llama3.1-8B-Instruct 和 GLM-4-9B-Chat。
● 在工具调用能力(Function Calling)方面,它在 Berkeley Function Calling Leaderboard 上表现优异,超越了多个 9B 参数以下的模型。
数学能力 在 MathBench 上优于 GPT-3.5-Turbo,并在 LiveCodeBench 上超越 Llama3.1-8B-Instruct。
指令遵循能力 在中英文评测中均表现突出,中文 FollowBench-zh 上得分高达 66.8%。
RAG能力:MiniCPM RAG套件在中文、中英跨语言检索测试中取得SOTA表现,MiniCPM3-RAG-LoRA在开放域问答等多项任务上超越Llama3-8B、Baichuan2-13B等模型
>> 数据构成:MiniCPM3-4B 训练于大规模中英文语料,具备强大的多语言处理能力,尤其在跨语言检索和中英文任务上表现优秀。
>> 数据预处理流程及策略:数据经过严格筛选和预处理,确保模型能够在指令遵循和代码生成等复杂任务中表现出色。同时,它支持长文本处理和大规模知识调用的场景。
>> 模型结构:MiniCPM3-4B 采用了高效的 FFN 层稀疏化 和优化技术,进一步减少了计算开销。具备 32k 上下文处理能力,并且使用 LLMxMapReduce 技术在理论上可以处理无限长的上下文。
>> 优化策略:模型在推理时通过 SGLang 优化,提高了推理速度和吞吐量,同时在 vLLM 上也有针对性的优化,提升了推理效率和计算速度。

目录

MiniCPM3-4B的简介

1、更新日志🔥

2、模型下载

3、评测结果

综合评测

工具调用能力

长文本能力

MiniCPM3-4B的安装和使用方法

1、模型推理

T1、Huggingface

SGLang(推荐)

vLLM

llama.cpp

2、模型微调

LLaMA-Factory

3、进阶功能

工具调用

代码解释器

MiniCPM 2.0

评测结果

MiniCPM-2B-128k 模型评测

MiniCPM-MoE-8x2B 模型评测

MiniCPM-S-1B 评测结果

模型推理

HuggingFace、vLLM推理

Powerinfer 推理

MiniCPM 1.0

评测结果

评测设置

部署模式

评测度量

文本模型评测

快速上手

在线体验

基于Gradio的网页版Demo

HuggingFace 推理

MiniCPM-2B

MiniCPM-2B (Llama Format)

vLLM 推理

SGLang 推理

llama.cpp、Ollama、fastllm、mlx_lm推理

模型微调


MiniCPM3-4B的简介

MiniCPM 3.0 是一个 4B 参数量的语言模型,相比 MiniCPM1.0/2.0,功能更加全面,综合能力大幅提升,多数评测集上的效果比肩甚至超越众多 7B-9B 模型。

  • 支持工具调用🛠️(Function Calling)和代码解释器💻(Code Interpreter)Berkeley Function Calling Leaderboard (BFCL) 上取得 9B 规模以下 SOTA,超越 GLM-4-9B-Chat、Qwen2-7B-Instruct。
  • 超强的推理能力🧮:数学能力方面,MathBench 上的效果超越 GPT-3.5-Turbo 以及多个 7B-9B 模型。在非常具有挑战性的 LiveCodeBench 上,效果超越 Llama3.1-8B-Instruct。
  • 出色的中英文指令遵循能力🤖:英文指令遵循 IFEval、中文指令遵循 FollowBench-zh 效果超越 GLM-4-9B-Chat、Qwen2-7B-Instruct。
  • 长文本能力:原生支持 32k 上下文长度,32k 长度内大海捞针全绿。提出 LLM x MapReduce ,理论可处理的上下文长度达到 +∞。
  • RAG能力:我们发布了 MiniCPM RAG 套件。基于 MiniCPM 系列模型的 MiniCPM-EmbeddingMiniCPM-Reranker 在中文、中英跨语言检索测试中取得 SOTA 表现;针对 RAG 场景的 MiniCPM3-RAG-LoRA 在开放域问答等多项任务上超越 Llama3-8B、Baichuan2-13B 等模型。

1、更新日志🔥

  • [2024.09.05] 发布 MiniCPM3-4B!该模型的表现超越 Phi-3.5-mini-instruct 和 GPT-3.5-Turbo-0125,并且能够比肩 Llama3.1-8B-Instruct、Qwen2-7B-Instruct、GLM-4-9B-Chat 等多个 7B-9B 参数量的模型。
  • [2024.07.09] MiniCPM-2B 已经支持使用 SGLang 推理!
  • [2024.07.05] 发布 MiniCPM-S-1B!该模型在保持下游任务性能无损的前提下,FFN 层实现了 87.89% 的平均稀疏度,将 FFN FLOPs 降低了 84%。
  • [2024.04.11] 发布 MiniCPM-2B-128kMiniCPM-MoE-8x2B 和 MiniCPM-1B!点击这里查看技术博客。
  • [2024.03.16] MiniCPM-2B 的 30 余个中间检查点开放了!HuggingFace链接
  • [2024.02.01] 发布 MiniCPM-2B!该模型在公开评测集上与 Mistral-7B 表现相近(中文、数学、代码能力更优),整体性能超越 Llama2-13B、MPT-30B、Falcon-40B 等模型。

2、模型下载

HuggingFaceModelScope
MiniCPM3-4BMiniCPM3-4B
MiniCPM-2B-sftMiniCPM-2B-sft
MiniCPM-2B-dpoMiniCPM-2B-dpo
MiniCPM-2B-128kMiniCPM-2B-128k
MiniCPM-MoE-8x2BMiniCPM-MoE-8x2B
MiniCPM-1BMiniCPM-1B
MiniCPM-S-1BMiniCPM-S-1B

注: 更多模型版本见这里

3、评测结果

综合评测

评测集Qwen2-7B-InstructGLM-4-9B-ChatGemma2-9B-itLlama3.1-8B-InstructGPT-3.5-Turbo-0125Phi-3.5-mini-Instruct(3.8B)MiniCPM3-4B
英文能力
MMLU70.572.472.669.469.268.467.2
BBH64.976.365.267.870.368.670.2
MT-Bench8.418.357.888.288.178.608.41
IFEVAL (Prompt Strict-Acc.)51.064.571.971.558.849.468.4
中文能力
CMMLU80.971.559.555.854.546.973.3
CEVAL77.275.656.755.252.846.173.6
AlignBench v1.17.106.617.105.685.825.736.74
FollowBench-zh (SSR)63.056.457.050.664.658.166.8
数学能力
MATH49.650.646.051.941.846.446.6
GSM8K82.379.679.784.576.482.781.1
MathBench63.459.445.854.348.954.965.6
代码能力
HumanEval+70.167.161.662.866.568.968.3
MBPP+57.162.264.355.371.455.863.2
LiveCodeBench v322.220.219.220.424.019.622.6
工具调用能力
BFCL v271.670.119.273.375.448.476.0
综合能力
平均分65.365.057.960.861.057.266.3

工具调用能力

我们在 Berkeley Function Calling Leaderboard (BFCL) 上测试了模型的工具调用能力,MiniCPM3-4B 在该榜单上的表现超越了多个 7B-9B 参数量的模型,优于 GPT-3.5-Turbo-0125。

模型总体准确率AST SummaryExec SummaryIrrelevance DetectionRelevance Detection
MiniCPM3-4B76.03%68.55%85.54%53.71%90.24%
Llama3.1-8B-Instruct73.28%64.61%86.48%43.12%85.37%
Qwen2-7B-Instruct71.61%65.71%79.57%44.70%90.24%
GLM-4-9B-Chat70.08%60.69%80.02%55.02%82.93%
Phi-3.5-mini-instruct48.44%38.89%54.04%46.78%65.85%
Gemma2-9B-it19.18%5.41%18.50%88.88%7.32%

长文本能力

在 32k 的上下文长度进行大海捞针测试,结果如下图:

MiniCPM3-4B的安装和使用方法

1、模型推理

T1、Huggingface

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

path = 'openbmb/MiniCPM3-4B'
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.7, top_p=0.7)
print(responds)

SGLang(推荐)

  • 安装

参考 SGLang 官方仓库,通过源码安装最新版本。

  • 启动推理服务
python -m sglang.launch_server --model openbmb/MiniCPM3-4B --trust-remote-code --port 30000 --chat-template chatml
  • 使用示例
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams

model_name = "openbmb/MiniCPM3-4B"
prompt = [{"role": "user", "content": "请写一篇关于人工智能的文章,详细介绍人工智能的未来发展和隐患。"}]

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,
    tensor_parallel_size=1
)
sampling_params = SamplingParams(top_p=0.7, temperature=0.7, max_tokens=1024)

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

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

vLLM

安装 vllm

pip install git+https://github.com/OpenBMB/vllm.git@minicpm3

推理

from transformers import AutoTokenizer
from vllm import LLM, SamplingParams

model_name = "openbmb/MiniCPM3-4B"
prompt = [{"role": "user", "content": "请写一篇关于人工智能的文章,详细介绍人工智能的未来发展和隐患。"}]

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,
    tensor_parallel_size=1
)
sampling_params = SamplingParams(top_p=0.7, temperature=0.7, max_tokens=1024)

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

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

llama.cpp

我们提供了 MiniCPM3 的 GGUF 版本,可以直接使用 llama.cpp 推理。

安装 llama.cpp

git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make 

推理

./llama-cli -c 1024 -m minicpm3-4b-fp16.gguf -n 1024 --top-p 0.7 --temp 0.7 --prompt "<|im_start|>user\n请写一篇关于人工智能的文章,详细介绍人工智能的未来发展和隐患。<|im_end|>\n<|im_start|>assistant\n"

2、模型微调

LLaMA-Factory

目前模型微调支持 LLaMA-Factory,使用方法参考 LLaMA-Factory 微调

3、进阶功能

对于以下进阶功能,我们推荐使用 vLLM

工具调用

我们提供了使用 MiniCPM3 调用工具的示例代码:

cd demo/minicpm3/function_call
python function_call.py

如果你想启动一个能够调用工具的推理服务,使用以下代码:

cd demo/minicpm3/function_call
pip install -r requirements.txt
python openai_api_server.py \
    --model openbmb/MiniCPM3-4B \
    --served-model-name MiniCPM3-4B \
    --chat-template chatml.jinja \
    --dtype auto \
    --api-key token-abc123 \
    --tensor-parallel-size 1 \
    --trust-remote-code

下面是一个调用搜索工具回答问题的演示:

代码解释器

我们提供了一个 MiniCPM3 使用代码解释器的示例代码:

cd demo/minicpm3/code_interpreter
pip install -r requirements.txt
python code_interpreter.py openbmb/MiniCPM3-4B

下面是一个使用代码解释器生成二维码的演示:

MiniCPM 2.0

查看 MiniCPM 2.0 的详细信息

MiniCPM 2.0 系列模型对 MiniCPM 进行了多个维度的升级,包括以下模型版本:

  • MiniCPM-2B-128k:将 MiniCPM-2B 的上下文长度从 4k 扩展至 128k,在 InfiniteBench 测试集上优于 ChatGLM3-6B-128k、Yi-6B-200k 等更大参数量的模型。
  • MiniCPM-MoE-8x2B:基于 MiniCPM-2B 进行 MoE 扩展,综合表现相比于 MiniCPM-2B 平均提高 4.5 个百分点。
  • MiniCPM-1B:相比于 MiniCPM-2B 成本下降 60%,综合表现仍然优于 LLaMA2-13B。
  • MiniCPM-S-1B:在保持下游任务性能无损的前提下,FFN 层实现了 87.89% 的平均稀疏度,将 FFN FLOPs 降低了 84%。结合 PowerInfer 推理框架,解码速度提升约 2.8 倍。

评测结果

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全集。

MiniCPM-S-1B 评测结果
  • 代码生成:在 HumanEval(0-shot)和 MBPP(3-shot)上的平均 pass@1 得分。
  • 常识推理:在 PIQA、SIQA、HellaSwag、WinoGrande 和 COPA 上的平均 0-shot 准确率。
  • 阅读理解:在 BoolQ、LAMBADA 和 TyDi QA 上的平均 0-shot 准确率。

其他测试集:我们报告在GSM8K(8-shot)、MMLU(5-shot)、BBH(3-shot)和 AGI-Eval(0-shot)上的平均准确率。

SettingAverage
Sparsity
Average
Performance
Code
Generation
Commonsense
Reasoning
Reading
Comprehension
GSM8KMMLUBBHAGI Eval
LLaMA2-7B-37.9616.3769.5961.8712.9644.4532.9627.53
ReluLLaMA-7B66.9837.6215.8569.6470.545.8438.6435.0727.73
ProSparse-7B*88.1138.3119.4766.2963.3312.7445.2133.5927.55
ProSparse-7B89.3238.4619.4266.2763.5012.1345.4834.9927.46
LLaMA2-13B-44.0620.1972.5871.5522.2154.6937.8929.33
ReluLLaMA-13B71.5642.7420.1970.4473.2918.5050.5837.9728.22
ProSparse-13B*87.9745.0729.0369.7567.5425.4054.7840.2028.76
ProSparse-13B88.8044.9028.4269.7666.9126.3154.3539.9028.67
MiniCPM-1B-44.4436.8563.6760.9035.4850.4435.0328.71
MiniCPM-S-1B*86.2544.7241.3864.5560.6934.7249.3634.0428.27
MiniCPM-S-1B87.8944.7242.0464.3760.7334.5749.5134.0827.77

注:

  1. ReluLLaMA-7B 和 ReluLLaMA-13B 的下载链接分别是 7B and 13B。"ProSparse-7B*"、"ProSparse-13B*" 和 "MiniCPM-S-1B*" 代表没有激活阈值偏移的 ProSparse 版本。
  2. 对于 PIQA、SIQA、HellaSwag、WinoGrande、COPA、BoolQ、LAMBADA、TyDi QA 和 AGI-Eval,我们根据各个选项的 PPL 来进行答案选择。对于 GSM8K、MMLU 和 BBH,我们直接生成答案。

模型推理

HuggingFace、vLLM推理

参考 MiniCPM 1.0 中的模型推理部分。

Powerinfer 推理

针对 MiniCPM-S-1B 模型,我们可以使用 Powerinfer 进行推理加速,使用方法如下:

  1. 保证cmake版本3.17以上,如果已经安装过,则跳过此步骤
  # 下载安装包
  sudo wget https://cmake.org/files/v3.23/cmake-3.23.0.tar.gz
  # 解压安装包
  sudo tar -zxvf cmake-3.23.0.tar.gz
  # 配置安装环境
  sudo ./configure
  sudo make -j8
  # 编译安装
  sudo make install
  # 查看安装后版本
  cmake --version
  # 返回版本号则安装成功
  #cmake version 3.23.0
  1. 安装powerinfer:
  git clone https://github.com/SJTU-IPADS/PowerInfer
  cd PowerInfer
  pip install -r requirements.txt # install Python helpers' dependencies
  1. cpu版本powerinfer编译,如果你的机器只有cpu,或者只想使用cpu进行推理,则运行以下命令:
  cmake -S . -B build
  cmake --build build --config Release
  1. gpu版本powerinfer编译,如果你的机器有gpu,则可以运行以下命令:
  cmake -S . -B build -DLLAMA_CUBLAS=ON
  cmake --build build --config Release
  1. 获取稀疏模型
git clone https://huggingface.co/openbmb/MiniCPM-S-1B-sft-gguf/tree/main
#or
git clone https://modelscope.cn/models/OpenBMB/MiniCPM-S-1B-sft-gguf
  1. 模型推理:
cd PowerInfer
# 以下是命令模版,output_token_count为最大输出tokens,thread_num 为线程数,prompt为输入prompt字符
#./build/bin/main -m /PATH/TO/MODEL -n $output_token_count -t $thread_num -p $prompt
# 以下是示例
./build/bin/main -m /root/ld/ld_model_pretrain/1b-s-minicpm/MiniCPM-S-1B-sft.gguf -n 2048 -t 8 -p '<用户>hello,tell me a story please.<AI>'

MiniCPM 1.0

查看 MiniCPM 1.0 的详细信息

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 进行任何身份认同训练。同时由于我们用 ShareGPT 开源语料作为部分训练数据,模型可能会输出类似 GPT 系列模型的身份认同信息。

评测结果

评测设置
  • 由于大模型评测难以统一,且大量评测也没有公开的prompt和测试代码,对于具体评测方式,我们只能尽量做到适合各类模型。
  • 整体而言,我们测试时采用统一的prompt输入,并按照各模型对应的模板进行输入调整。
  • 评测脚本及prompt已开源在我们的Github仓库中,也欢迎更多开发者来不断改进我们的评测方式。
    • 文本评测部分,采用了我们的开源大模型能力评测框架UltraEval。以下为开源模型复现流程:
      • 安装UltraEval
        git clone https://github.com/OpenBMB/UltraEval.git
        cd UltraEval
        pip install -e .
      • 下载相关数据并解压处理
        wget -O RawData.zip "https://cloud.tsinghua.edu.cn/f/71b5232264ae4833a4d0/?dl=1"
        unzip RawData.zip
        python data_process.py
      • 执行评测脚本(提供了模板,可自定义)
        bash run_eval.sh
部署模式
  • 因为MiniCPM采用Mup的结构,与现有模型在具体计算上有细微差别,我们是基于vllm=0.2.2版本进行了我们模型的实现。
  • 对于非MiniCPM模型,我们采用了vllm=0.2.7的最新版本进行推理。
评测度量
  • 对于QA任务(选择题任务),我们选用两种方式进行测试:
    • PPL:将选项作为题目生成的延续,并根据各个选项的PPL来进行答案选择;
    • 第二种是直接生成答案选项。
  • 对于不同模型,这两种方式得到的结果差异较大。MiniCPM两种模式上的结果较为接近,而Mistral-7B-v0.1等模型在PPL上表现较好,直接生成上效果较差。
  • 在具体评测时,我们以两种评测方式得分的最高者为最终结果,以此保证对比的公平性(以下表格中*号表示采用PPL)。
文本模型评测

越级比较:

模型平均分英文均分中文均分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

快速上手

在线体验

基于Gradio的网页版Demo
  • 使用如下命令启动基于Gradio的网页版demo:
# generation powered by vllm
python demo/minicpm/vllm_based_demo.py --model_path <vllmcpm_repo_path>
# generation powered by huggingface
python demo/minicpm/hf_based_demo.py --model_path <hf_repo_path>
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)
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)
vLLM 推理

安装 vLLM

pip install "vllm>=0.4.1"

具体推理代码见这里

SGLang 推理

安装 SGLang

  • 首先需要启动一个服务:
python -m sglang.launch_server --model-path openbmb/MiniCPM-2B-dpo-fp16 --trust-remote-code --port 30000
  • 下面是一个推理代码的样例:
from sglang import function, gen, set_default_backend, RuntimeEndpoint

@function
def text_qa(s, question):
    s += "<用户>" + question + "<AI>"
    s += gen("answer", max_tokens=1024, temperature=0.7, top_p=0.7)

set_default_backend(RuntimeEndpoint("http://localhost:30000"))

state = text_qa.run(
    question="What is the capital of China?",
)

print(state["answer"])
llama.cpp、Ollama、fastllm、mlx_lm推理

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

请参考 MiniCPM 知识库中的量化指南

模型微调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个处女座的程序猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值