小钢炮进化,MiniCPM 3.0 开源!4B参数超GPT3.5性能,无限长文本,超强RAG三件套!模型推理、微调实战来啦!

简介: 旗舰端侧模型面壁「小钢炮」系列进化为全新 MiniCPM 3.0 基座模型,再次以小博大,以 4B 参数,带来超越 GPT-3.5 的性能。并且,量化后仅 2GB 内存,端侧友好。

旗舰端侧模型面壁「小钢炮」系列进化为全新 MiniCPM 3.0 基座模型,再次以小博大,以 4B 参数,带来超越 GPT-3.5 的性能。并且,量化后仅 2GB 内存,端侧友好。

小编敲黑板,本次发布重点:

  • 无限长文本,榜单性能强,超长文本也不崩;
  • 性能比肩 GPT-4o 的端侧强大 Function Calling;
  • 超强 RAG 三件套,中文检索超强、生成超 Llama3-8B。

MiniCPM 3.0 开源地址:

代码链接:https://github.com/OpenBMB/MiniCPM

模型链接:

  • MiniCPM3-4B:

https://modelscope.cn/models/OpenBMB/MiniCPM3-4B

MiniCPM 3.0 模型性能:

image.png

模型亮点

面壁「无限」长文本

面壁提出 LLMxMapReduce长本文分帧处理技术  ,一举实现「无限」长文本!32, 128, 256, 512K…MiniCPM 3.0 打破大模型记忆限制,可以将上下文长度无限稳定拓展,想多长就多长!

image.png

Function calling ,助力终端Agent应用

MiniCPM 3.0 拥有端侧超强 Function calling 性能 ,在权威评测榜单 Berkeley Function-Calling Leaderboard 上,其性能接近 GPT-4o。

RAG 三件套,生成超 Llama3-8B

MiniCPM 3.0一口气带来超强 RAG 外挂三件套:MiniCPM-Embedding(检索模型)、MiniCPM-Reranker(重排序模型)和面向 RAG 场景的 LoRA 插件(生成模型),款款优秀:

  • MiniCPM-Embedding(检索模型)中英跨语言检索取得 SOTA 性能,在评估模型文本嵌入能力的权威评测集 MTEB 的检索榜单上中文第一、英文第十三 ;
  • MiniCPM-Reranker(重排序模型)在中文、英文、中英跨语言测试上取得 SOTA 性能 ;
  • 经过针对 RAG 场景的 LoRA 训练后,MiniCPM 3.0-RAG-LoRA 在开放域问答(NQ、TQA、MARCO)、多跳问答(HotpotQA)、对话(WoW)、事实核查(FEVER)和信息填充(T-REx)等多项任务上的性能表现,超越 Llama3-8B 和 Baichuan2-13B 等业内优秀模型。

魔搭社区最佳实践

模型下载

  • 模型repo下载:
modelscope download --model=OpenBMB/MiniCPM3-4B --local_dir ./MiniCPM3-4B

模型推理

  • MiniCPM3-4B推理:
from modelscope import AutoModelForCausalLM, AutoTokenizer
import torch
path = "OpenBMB/MiniCPM3-4B"
device = "cuda"
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.bfloat16, device_map=device, trust_remote_code=True)
messages = [
    {"role": "user", "content": "推荐5个北京的景点。"},
]
model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(device)
model_outputs = model.generate(
    model_inputs,
    max_new_tokens=1024,
    top_p=0.7,
    temperature=0.7,
    repetition_penalty=1.02
)
output_token_ids = [
    model_outputs[i][len(model_inputs[i]):] for i in range(len(model_inputs))
]
responses = tokenizer.batch_decode(output_token_ids, skip_special_tokens=True)[0]
print(responses)
  • MiniCPM3-RAG-LoRA推理
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
from modelscope import snapshot_download
base_model_dir = snapshot_download("OpenBMB/MiniCPM3-4B")
lora_model_dir = snapshot_download("OpenBMB/MiniCPM3-RAG-LoRA")
model = AutoModelForCausalLM.from_pretrained(base_model_dir, device_map="auto",torch_dtype=torch.bfloat16).eval()
tokenizer = AutoTokenizer.from_pretrained(lora_model_dir)
model = PeftModel.from_pretrained(model, lora_model_dir)
passages_list = ["In the novel 'The Silent Watcher,' the lead character is named Alex Carter. Alex is a private detective who uncovers a series of mysterious events in a small town.",
"Set in a quiet town, 'The Silent Watcher' follows Alex Carter, a former police officer turned private investigator, as he unravels the town's dark secrets.",
"'The Silent Watcher' revolves around Alex Carter's journey as he confronts his past while solving complex cases in his hometown."]
instruction = "Q: What is the name of the lead character in the novel 'The Silent Watcher'?\nA:"
passages = '\n'.join(passages_list)
input_text = 'Background:\n' + passages + '\n\n' + instruction
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": input_text},
]
prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
outputs = model.chat(tokenizer, prompt, temperature=0.8, top_p=0.8)
print(outputs[0])  # The lead character in the novel 'The Silent Watcher' is named Alex Carter.
  • MiniCPM-Embedding推理
from modelscope import AutoModel, AutoTokenizer
import torch
import torch.nn.functional as F
model_name = "OpenBMB/MiniCPM-Embedding"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name, trust_remote_code=True, attn_implementation="flash_attention_2", torch_dtype=torch.float16).to("cuda")
model.eval()
def weighted_mean_pooling(hidden, attention_mask):
    attention_mask_ = attention_mask * attention_mask.cumsum(dim=1)
    s = torch.sum(hidden * attention_mask_.unsqueeze(-1).float(), dim=1)
    d = attention_mask_.sum(dim=1, keepdim=True).float()
    reps = s / d
    return reps
@torch.no_grad()
def encode(input_texts):
    batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt', return_attention_mask=True).to("cuda")
    
    outputs = model(**batch_dict)
    attention_mask = batch_dict["attention_mask"]
    hidden = outputs.last_hidden_state
    reps = weighted_mean_pooling(hidden, attention_mask)   
    embeddings = F.normalize(reps, p=2, dim=1).detach().cpu().numpy()
    return embeddings
queries = ["中国的首都是哪里?"]
passages = ["beijing", "shanghai"]
INSTRUCTION = "Query: "
queries = [INSTRUCTION + query for query in queries]
embeddings_query = encode(queries)
embeddings_doc = encode(passages)
scores = (embeddings_query @ embeddings_doc.T)
print(scores.tolist())  # [[0.3535913825035095, 0.18596848845481873]]
  • MiniCPM-Reranker推理
from transformers import AutoModel, AutoTokenizer, AutoModelForSequenceClassification
import torch
import numpy as np
#模型下载
from modelscope import snapshot_download
model_name = snapshot_download('OpenBMB/MiniCPM-Reranker')
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
tokenizer.padding_side = "right"
model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True,attn_implementation="flash_attention_2", torch_dtype=torch.float16).to("cuda")
model.eval()
max_len_q, max_len_d = 512, 512
def tokenize_our(query,doc):
    input_id_query = tokenizer.encode(query, add_special_tokens=False, max_length=max_len_q, truncation=True)
    input_id_doc = tokenizer.encode(doc, add_special_tokens=False, max_length=max_len_d, truncation=True)
    pad_input = {"input_ids": [tokenizer.bos_token_id] + input_id_query + [tokenizer.eos_token_id] + input_id_doc}
    return tokenizer.pad(
        pad_input,
        padding="max_length",
        max_length=max_len_q + max_len_d + 2,
        return_tensors="pt",
    )
@torch.no_grad()
def rerank(input_query, input_docs):
    tokenized_inputs = [tokenize_our(input_query, input_doc).to("cuda") for input_doc in input_docs]
    input_ids = {
      "input_ids": [tokenized_input["input_ids"] for tokenized_input in tokenized_inputs],
      "attention_mask": [tokenized_input["attention_mask"] for tokenized_input in tokenized_inputs]
    }
    for k in input_ids:
      input_ids[k] = torch.stack(input_ids[k]).to("cuda")
    outputs = model(**input_ids)
    score = outputs.logits
    return score.float().detach().cpu().numpy()
queries = ["中国的首都是哪里?"]
passages = [["beijing", "shanghai"]]
INSTRUCTION = "Query: "
queries = [INSTRUCTION + query for query in queries]
scores = []
for i in range(len(queries)):
    print(queries[i])
    scores.append(rerank(queries[i],passages[i]))
print(np.array(scores))  # [[[-4.7421875][-8.8515625]]]

模型微调

我们介绍使用ms-swift对minicpm3-4b进行分类任务微调,并对微调后模型进行推理。swift是魔搭社区官方提供的LLM工具箱,支持300+大语言模型和80+多模态大模型的微调到部署。swift开源地址:https://github.com/modelscope/ms-swift

通常,大模型微调通常使用自定义数据集进行微调。在这里,我们将展示可直接运行的demo。我们使用 jd-sentiment-zh 情感分类数据集进行古文翻译腔微调,您可以在 modelscope 上找到该数据集:

https://modelscope.cn/datasets/DAMO_NLP/jd

在开始微调之前,请确保您的环境已正确安装

# 安装ms-swift
git clone https://github.com/modelscope/ms-swift.git
cd swift
pip install -e .[llm]

微调脚本:

# 单卡A10/3090可运行
# 13GB GPU memory
CUDA_VISIBLE_DEVICES=0 swift sft \
    --model_type minicpm3-4b \
    --model_id_or_path OpenBMB/MiniCPM3-4B \
    --dataset jd-sentiment-zh \
    --learning_rate 1e-4 \
    --output_dir output \
    --lora_target_modules ALL \
# 4 * 13GB GPU memory
# Deepspeed-ZeRO2
NPROC_PER_NODE=4 CUDA_VISIBLE_DEVICES=0,1,2,3 \
swift sft \
    --model_type minicpm3-4b \
    --model_id_or_path OpenBMB/MiniCPM3-4B \
    --dataset jd-sentiment-zh \
    --learning_rate 1e-4 \
    --output_dir output \
    --lora_target_modules ALL \
    --deepspeed default-zero2

微调显存消耗:

image.png

微调过程的loss可视化:(这里只微调了200个steps)

image.png

微调后推理脚本如下,这里的ckpt_dir需要修改为训练生成的last checkpoint文件夹。

CUDA_VISIBLE_DEVICES=0 swift infer \
    --ckpt_dir output/minicpm3-4b/vx-xxx/checkpoint-xxx \
    --load_dataset_config true --show_dataset_sample 10 \
    --do_sample false 
    
# merge-lora并推理
CUDA_VISIBLE_DEVICES=0 swift infer \
    --ckpt_dir output/minicpm3-4b/vx-xxx/checkpoint-xxx \
    --load_dataset_config true --show_dataset_sample 10 \
    --merge_lora true --do-sample false

微调后模型对验证集进行推理:

image.png

相关文章
|
2月前
|
数据可视化 API Swift
全模态图像模型Nexus-Gen对齐GPT-4o!同时搞定,数据、训练框架、模型全面开源
OpenAI GPT-4o发布强大图片生成能力后,业界对大模型生图能力的探索向全模态方向倾斜,训练全模态模型成研发重点。
189 17
|
3月前
|
机器学习/深度学习 人工智能 前端开发
SWEET-RL:8B小模型暴打GPT-4?Meta开源强化学习黑科技,多轮任务成功率飙升6%
Meta最新开源的SWEET-RL框架通过优化多轮交互任务的信用分配机制,使Llama-3.1-8B模型在协作推理任务中的表现提升6%,性能达到顶尖大模型水平。
202 33
SWEET-RL:8B小模型暴打GPT-4?Meta开源强化学习黑科技,多轮任务成功率飙升6%
|
2月前
|
人工智能 API 开发者
狂揽7.5k星!这款开源API网关彻底解放开发者:一键聚合GPT-4、Suno、Midjourney,还能在线充值!
New API 是一款基于 One API 二次开发的 AI 模型接口管理与分发系统,支持多种大模型(如 GPT-4、Suno、Midjourney 等)统一封装为 OpenAI 格式接口调用。其核心功能包括多模型统一网关、企业级权限管控、“推理力度”分级、无魔法访问全球 AI 服务、灵活计费体系及开发者友好设计。技术架构采用 Golang + Gin 框架,支持高并发低延迟,适用于企业内部 AI 中台、多模型 SaaS 平台、学术研究协作及个人开发者工具等场景。项目开源地址:https://github.com/kingbug/new-api。
340 6
|
3月前
|
人工智能 搜索推荐 开发者
GPT-4o测评准确率竟不到1%!BrowseComp:OpenAI开源AI代理评测新基准,1266道高难度网络检索问题
OpenAI最新开源的BrowseComp基准包含1266个高难度网络检索问题,覆盖影视、科技、艺术等九大领域,其最新Deep Research模型以51.5%准确率展现复杂信息整合能力,为AI代理的浏览能力评估建立新标准。
158 4
GPT-4o测评准确率竟不到1%!BrowseComp:OpenAI开源AI代理评测新基准,1266道高难度网络检索问题
|
3月前
|
编解码 开发者
ImagePulse图律脉动数据集开源发布:解码GPT-4o级图像生成能力,四大原子数据集+自动生成工具开放
ImagePulse图律脉动数据集开源发布:解码GPT-4o级图像生成能力,四大原子数据集+自动生成工具开放
77 3
|
4月前
|
机器学习/深度学习 人工智能 开发者
GPT-4o-mini-transcribe:OpenAI 推出实时语音秒转文本模型!高性价比每分钟0.003美元
GPT-4o-mini-transcribe 是 OpenAI 推出的语音转文本模型,基于 GPT-4o-mini 架构,采用知识蒸馏技术,适合在资源受限的设备上运行,具有高效、实时和高性价比的特点。
200 2
GPT-4o-mini-transcribe:OpenAI 推出实时语音秒转文本模型!高性价比每分钟0.003美元
|
5月前
|
机器学习/深度学习 人工智能 编解码
R1-Onevision:开源多模态推理之王!复杂视觉难题一键解析,超越GPT-4V
R1-Onevision 是一款开源的多模态视觉推理模型,基于 Qwen2.5-VL 微调,专注于复杂视觉推理任务。它通过整合视觉和文本数据,能够在数学、科学、深度图像理解和逻辑推理等领域表现出色,并在多项基准测试中超越了 Qwen2.5-VL-7B 和 GPT-4V 等模型。
199 0
R1-Onevision:开源多模态推理之王!复杂视觉难题一键解析,超越GPT-4V
|
5月前
|
机器学习/深度学习 人工智能 自然语言处理
通古大模型:古籍研究者狂喜!华南理工开源文言文GPT:AI自动断句+写诗翻译,24亿语料喂出来的学术神器
通古大模型由华南理工大学开发,专注于古籍文言文处理,具备强大的古文句读、文白翻译和诗词创作功能。
626 11
通古大模型:古籍研究者狂喜!华南理工开源文言文GPT:AI自动断句+写诗翻译,24亿语料喂出来的学术神器
|
4月前
清华UCSD提出全新微调方法,8B小模型媲美GPT-4o!科学问题正确率提高28%
清华大学与UCSD研究人员提出了一种创新微调方法,通过“世界知识蒸馏”和“工具使用适应”两组件,显著提升大型语言模型(LLM)解决科学问题的能力,同时保持其基本推理能力。实验结果显示,该方法在多个科学领域基准数据集上大幅提高了答案准确性和工具使用精度。论文地址:https://arxiv.org/abs/2411.00412
73 2
|
6月前
|
人工智能 Python
JoyCaption:开源的图像转提示词生成工具,支持多种风格和场景,性能与 GPT4o 相当
JoyCaption 是一款开源的图像提示词生成工具,支持多种生成模式和灵活的提示选项,适用于社交媒体、图像标注、内容创作等场景,帮助用户快速生成高质量图像描述。
849 21
JoyCaption:开源的图像转提示词生成工具,支持多种风格和场景,性能与 GPT4o 相当

热门文章

最新文章