0元!使用魔搭免费算力,基于Qwen基座模型,复现DeepSeek-R1

本文涉及的产品
模型训练 PAI-DLC,100CU*H 3个月
交互式建模 PAI-DSW,每月250计算时 3个月
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
简介: 近期,随着DeepSeek-R1爆火,学术界掀起一股复现DeepSeek-R1的浪潮,李飞飞等斯坦福大学和华盛顿大学的研究人员以不到50美元的云计算费用,成功训练出了一个名为s1的人工智能推理模型。

01.引言

近期,随着DeepSeek-R1爆火,学术界掀起一股复现DeepSeek-R1的浪潮,李飞飞等斯坦福大学和华盛顿大学的研究人员以不到50美元的云计算费用,成功训练出了一个名为s1的人工智能推理模型。该模型在数学和编码能力测试中的表现,宣称与OpenAI的O1和DeepSeek的R1等尖端推理模型不相上下。

今天,我们使用魔搭社区的免费力算,基于Qwen基础,使用OpenAI的gsm8k数据集,基于Qwen基础模型,复现类似DeepSeek-R1的推理模型。

算力

魔搭社区免费GPU算力:https://modelscope.cn/my/mynotebook

基座模型

Qwen2.5-0.5B-Instruct:https://modelscope.cn/models/Qwen/Qwen2.5-0.5B-Instruct

咖啡

gsm8k:https://modelscope.cn/datasets/modelscope/gsm8k

训练工具

TRL:https://huggingface.co/docs/trl/main/en/grpo_trainer

笔记本分享链接

https://modelscope.cn/notebook/share/ipynb/c4d8363a/Qwen-GRPO.ipynb

02.新技术

本文使用 TRL 的 GRPO Trainer 来训练 Qwen 基础模型(注,本文示例使用 Qwen2.5-0.5B 模型作为示例,但生产场景建议使用 3B 及以上模型),如邵志宏等人论文《DeepSeekMath:在开放语言模型中突破数学推理的极限》中所述。

该论文的摘要如下:

第一步:安装依赖

其他的依赖在ModelScope的笔记本镜像预装好,本次只需要升级vllm和trl到最新版本,安装后请重启ipynb环境。

!pip install vllm -U
!pip install trl -U

第二步:定义提示的结构,需要包含推理标签

import re
import torch
from modelscope.msdatasets import MsDataset
from modelscope import AutoTokenizer, AutoModelForCausalLM
from trl import GRPOConfig, GRPOTrainer
# Load and prep dataset
SYSTEM_PROMPT = """
Respond in the following format:
<reasoning>
...
</reasoning>
<answer>
...
</answer>
"""
XML_COT_FORMAT = """\
<reasoning>
{reasoning}
</reasoning>
<answer>
{answer}
</answer>
"""

第三步:导入gsm8k数据集并重构它以适应对话提示的结构

def extract_xml_answer(text: str) -> str:
    answer = text.split("<answer>")[-1]
    answer = answer.split("</answer>")[0]
    return answer.strip()
def extract_hash_answer(text: str) -> str | None:
    if "####" not in text:
        return None
    return text.split("####")[1].strip()
# uncomment middle messages for 1-shot prompting
def get_gsm8k_questions(split = "train") -> MsDataset:
    data =  MsDataset.load('modelscope/gsm8k', subset_name='main', split=split)
    data = data.map(lambda x: { # type: ignore
        'prompt': [
            {'role': 'system', 'content': SYSTEM_PROMPT},
            {'role': 'user', 'content': x['question']}
        ],
        'answer': extract_hash_answer(x['answer'])
    }) # type: ignore
    return data # type: ignore
dataset = get_gsm8k_questions()

第四步:使用自定义的是Rewarding函数。其中最“正确性”函数 Correctness_reward_func,它充当验证器(比较模型完成情况与答案)。其他三个是格式化函数,论文针对gsm8k数学场景,验证结果是否为int重要类型输出,是否带推理标签等。

# Reward functions
def correctness_reward_func(prompts, completions, answer, **kwargs) -> list[float]:
    responses = [completion[0]['content'] for completion in completions]
    q = prompts[0][-1]['content']
    extracted_responses = [extract_xml_answer(r) for r in responses]
    print('-'*20, f"Question:\n{q}", f"\nAnswer:\n{answer[0]}", f"\nResponse:\n{responses[0]}", f"\nExtracted:\n{extracted_responses[0]}")
    return [2.0 if r == a else 0.0 for r, a in zip(extracted_responses, answer)]
def int_reward_func(completions, **kwargs) -> list[float]:
    responses = [completion[0]['content'] for completion in completions]
    extracted_responses = [extract_xml_answer(r) for r in responses]
    return [0.5 if r.isdigit() else 0.0 for r in extracted_responses]
def strict_format_reward_func(completions, **kwargs) -> list[float]:
    """Reward function that checks if the completion has a specific format."""
    pattern = r"^<reasoning>\n.*?\n</reasoning>\n<answer>\n.*?\n</answer>\n$"
    responses = [completion[0]["content"] for completion in completions]
    matches = [re.match(pattern, r) for r in responses]
    return [0.5 if match else 0.0 for match in matches]
def soft_format_reward_func(completions, **kwargs) -> list[float]:
    """Reward function that checks if the completion has a specific format."""
    pattern = r"<reasoning>.*?</reasoning>\s*<answer>.*?</answer>"
    responses = [completion[0]["content"] for completion in completions]
    matches = [re.match(pattern, r) for r in responses]
    return [0.5 if match else 0.0 for match in matches]
def count_xml(text) -> float:
    count = 0.0
    if text.count("<reasoning>\n") == 1:
        count += 0.125
    if text.count("\n</reasoning>\n") == 1:
        count += 0.125
    if text.count("\n<answer>\n") == 1:
        count += 0.125
        count -= len(text.split("\n</answer>\n")[-1])*0.001
    if text.count("\n</answer>") == 1:
        count += 0.125
        count -= (len(text.split("\n</answer>")[-1]) - 1)*0.001
    return count
def xmlcount_reward_func(completions, **kwargs) -> list[float]:
    contents = [completion[0]["content"] for completion in completions]
    return [count_xml(c) for c in contents]

原理参考:GRPO 是一种在线学习算法,这意味着它通过在训练期间使用训练模型本身生成的数据来迭代改进。GRPO 背后的直觉是最大化生成完成的优势,同时确保模型保持逼近参考策略。要了解 GRPO 的工作原理,可以将其分割为四个主要步骤:生成、计算优势、估计 KL 散度和计算损失。

添加图片注释,不超过 140 字(可选)

图片来源:https://huggingface.co/docs/trl/main/en/grpo_trainer

第五步:设置训练参数,本文是在22G显存力上运行,业务场景计算上建议使用两张卡,一张专门用于vLLM推理,另一张训练用于。

model_name = "Qwen/Qwen2.5-0.5B-Instruct"
output_dir="outputs/Qwen-0.5B-GRPO"
run_name="Qwen-0.5B-GRPO-gsm8k"
training_args = GRPOConfig(
    output_dir=output_dir,
    run_name=run_name,
    learning_rate=5e-6,
    adam_beta1 = 0.9,
    adam_beta2 = 0.99,
    weight_decay = 0.1,
    warmup_ratio = 0.1,
    lr_scheduler_type='cosine',
    logging_steps=1,
    bf16=True,
    per_device_train_batch_size=1,
    gradient_accumulation_steps=4,
    num_generations=8,
    max_prompt_length=256,
    max_completion_length=200,
    num_train_epochs=1,
    save_steps=100,
    max_grad_norm=0.1,
    log_on_each_node=False,
    use_vllm=True,
    vllm_gpu_memory_utilization=.2,
    vllm_device="cuda:0",
    report_to="none" #I'm disabling Wandb.
)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.bfloat16,
    device_map=None
).to("cuda")
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token

第六步:构建训练器,开始训练

trainer = GRPOTrainer(
    model=model,
    processing_class=tokenizer,
    reward_funcs=[
        xmlcount_reward_func,
        soft_format_reward_func,
        strict_format_reward_func,
        int_reward_func,
        correctness_reward_func],
    args=training_args,
    train_dataset=dataset,
    #peft_config=peft_config
)
trainer.train()

第七步:推理效果验证

from modelscope import AutoModelForCausalLM, AutoTokenizer
model_name = "/mnt/workspace/outputs/Qwen-0.5B-GRPO/checkpoint-1868"
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = "Xiao Ming bought 4 apples, ate 1, and gave 1 to his sister. How many apples were left?"
messages = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=256
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)

<理由>

最初小明有4个苹果,吃掉1个苹果后,剩下4-1=3个苹果,然后他把1个苹果给了妹妹,剩下3-1=2个苹果。

</推理>

<答案>

2

</答案>

03.展望

我们可以看到,我们的推理模型已经有了一定的效果——虽然它只是一个例子,肯定还会有一些错误,因为我们只训练了一个很小的数据集——如果我们延长序列长度,选择更坚固的基础模型,如Qwen2.5-32B并训练更长时间,模型的效果会更好。

点击链接,即可领取免费算力~

ModelScope 魔搭社区

相关文章
|
数据可视化 测试技术 PyTorch
智谱ChatGLM3魔搭最佳实践教程来了!
ChatGLM3-6B 是 ChatGLM 系列最新一代的开源模型,在保留了前两代模型对话流畅、部署门槛低等众多优秀特性的基础上
|
5天前
|
自然语言处理 Serverless 测试技术
DeepSeek 模型快速体验,魔搭+函数计算一键部署模型上云
DeepSeek模型近期备受关注,其开源版本DeepSeek-V3和DeepSeek-R1在多个基准测试中表现出色,性能比肩OpenAI顶尖模型。为降低本地部署门槛,Modelscope社区推出DeepSeek-R1-Distill-Qwen模型的一键部署服务,支持函数计算FC平台的闲置GPU实例,大幅降低成本。用户可选择不同参数量的小模型进行快速部署和推理,体验DeepSeek的强大性能。
DeepSeek 模型快速体验,魔搭+函数计算一键部署模型上云
|
12天前
|
人工智能 编解码 JSON
Qwen2.5-VL:阿里通义千问最新开源视觉语言模型,能够理解超过1小时的长视频
Qwen2.5-VL 是阿里通义千问团队开源的视觉语言模型,具有3B、7B和72B三种不同规模,能够识别常见物体、分析图像中的文本、图表等元素,并具备作为视觉Agent的能力。
244 18
Qwen2.5-VL:阿里通义千问最新开源视觉语言模型,能够理解超过1小时的长视频
|
6天前
|
机器学习/深度学习 自然语言处理 Serverless
DeepSeek 快速体验,魔搭+函数计算一键部署模型上云
对于期待第一时间在本地进行使用的用户来说,尽管 DeepSeek 提供了从 1.5B 到 70B 参数的多尺寸蒸馏模型,但本地部署仍需要一定的技术门槛。对于资源有限的用户进一步使用仍有难点。为了让更多开发者第一时间体验 DeepSeek 模型的魅力,Modelscope 社区 DeepSeek-R1-Distill-Qwen 模型现已支持一键部署(SwingDeploy)上函数计算 FC 服务,欢迎开发者立即体验。
177 12
|
5月前
|
编解码 JSON 自然语言处理
通义千问重磅开源Qwen2.5,性能超越Llama
击败Meta,阿里Qwen2.5再登全球开源大模型王座
4213 19
|
3月前
|
机器学习/深度学习 API
重磅!阿里云百炼上线Qwen百万长文本模型
重磅!阿里云百炼上线Qwen百万长文本模型
128 11
昇腾910A部署Qwen2-7B教程
Qwen2-7BS适配昇腾910A教程。
|
7月前
|
人工智能 JSON 自然语言处理
国内大模型LLM选择以及主流大模型快速使用教程[GLM4/Qwen/Baichuan/Coze/Kimi]
【7月更文挑战第7天】国内大模型LLM选择以及主流大模型快速使用教程[GLM4/Qwen/Baichuan/Coze/Kimi]
349 10
国内大模型LLM选择以及主流大模型快速使用教程[GLM4/Qwen/Baichuan/Coze/Kimi]
|
人工智能 安全 Serverless
如何让 Llama2、通义千问开源大语言模型快速跑在函数计算上?
本文是“在Serverless平台上构建AIGC应用”系列文章的第一篇文章。
|
9月前
|
自然语言处理 数据挖掘
Baichuan 3 通用能力评测国内第一,知识百科能力超越GPT-4-Turbo
SuperCLUE发布的《中文大模型基准测评2024年4月报告》显示,Baichuan 3在国内大模型中排名第一,总分73.32,超越文心一言、通义千问等。SuperCLUE是一个综合测评基准,评估大模型在多维度的性能。Baichuan 3在知识百科和逻辑推理上表现出色,分别排名第一和国内领先。此外,它在计算、代码和工具使用方面也名列前茅,适合应用于数学推理、数据分析、智能客服等领域,且能在教育、医疗、金融等行业场景中落地。
161 0
Baichuan 3 通用能力评测国内第一,知识百科能力超越GPT-4-Turbo

热门文章

最新文章