模型简介
模型特点
模型能力
使用案例
🚀 ✨ 使用Unsloth运行并微调Phi - 4!
本项目旨在借助Unsloth工具,实现对Phi - 4模型的运行与微调,为语言模型研究提供助力。用户可以免费使用Google Colab进行Phi - 4 (14B)的微调,还能将微调后的模型导出到Ollama、llama.cpp或HF等平台。
🚀 快速开始
- 点击此处,使用Google Colab免费微调Phi - 4 (14B)。
- 阅读我们关于Phi - 4支持及错误修复的博客:unsloth.ai/blog/phi4。
- 在文档中查看其余的notebook。
- 运行并将微调后的模型导出到Ollama、llama.cpp或HF。
✨ 主要特性
- 多模型支持:支持Phi - 4 (14B)、Qwen3 (14B)、GRPO with Phi - 4 (14B)、Llama - 3.2 (3B)、Llama - 3.2 (11B vision)、Qwen2.5 (7B)等多种模型。
- 高性能:相比其他模型,具有更快的运行速度和更低的内存使用。例如,Phi - 4 (14B)的运行速度快2倍,内存使用减少50%。
- 免费使用:提供免费的Colab notebook进行模型微调。
📦 安装指南
文档未提及具体安装步骤,故跳过此章节。
💻 使用示例
基础用法
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-4-reasoning-plus")
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-4-reasoning-plus", device_map="auto", torch_dtype="auto")
messages = [
{"role": "system", "content": "You are Phi, a language model trained by Microsoft to help users. Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> {Thought section} </think> {Solution section}. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion. Now, try to solve the following question through the above guidelines:"},
{"role": "user", "content": "What is the derivative of x^2?"},
]
inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
outputs = model.generate(
inputs.to(model.device),
max_new_tokens=4096,
temperature=0.8,
top_p=0.95,
do_sample=True,
)
print(tokenizer.decode(outputs[0]))
高级用法
vllm serve microsoft/Phi-4-reasoning-plus --enable-reasoning --reasoning-parser deepseek_r1
📚 详细文档
模型概述
属性 | 详情 |
---|---|
开发者 | Microsoft Research |
描述 | Phi - 4 - reasoning - plus是一款最先进的开放权重推理模型,它基于Phi - 4进行微调。微调过程采用了监督微调(在包含思维链轨迹的数据集上)和强化学习。监督微调数据集包含合成提示和来自公共领域网站的高质量过滤数据,重点关注数学、科学和编码技能,以及用于安全和负责任AI的对齐数据。该方法的目标是确保小而强大的模型使用高质量和高级推理的数据进行训练。Phi - 4 - reasoning - plus还额外进行了强化学习训练,因此具有更高的准确性,但平均生成的令牌数增加了50%,从而导致延迟更高。 |
架构 | 基础模型与之前发布的Phi - 4相同,具有14B参数,是一个仅含解码器的密集Transformer模型 |
输入 | 文本,最适合聊天格式的提示 |
上下文长度 | 32k令牌 |
GPU | 32个H100 - 80G |
训练时间 | 2.5天 |
训练数据 | 16B令牌,约8.3B唯一令牌 |
输出 | 针对输入生成的文本。模型响应分为两个主要部分,即思维链推理块和总结块 |
日期 | 2025年1月 - 2025年4月 |
状态 | 在离线数据集上训练的静态模型,公开可用数据的截止日期为2025年3月及更早 |
发布日期 | 2025年4月30日 |
许可证 | MIT |
预期用途
属性 | 详情 |
---|---|
主要用例 | 我们的模型旨在加速语言模型的研究,作为生成式AI功能的构建块。它适用于通用AI系统和应用程序(主要为英语),这些应用需要:1. 内存/计算受限的环境;2. 低延迟场景;3. 推理和逻辑能力。 |
非预期用例 | 该模型仅针对数学推理进行设计和测试。我们的模型并非专门为所有下游用途设计或评估。开发者在选择用例时应考虑语言模型的常见局限性,并在特定下游用例中使用之前评估和减轻准确性、安全性和公平性方面的问题,特别是在高风险场景中。开发者应了解并遵守与其用例相关的适用法律或法规(包括隐私、贸易合规法律等),包括模型对英语的关注。在选择用例时,请参考下面的负责任AI考虑部分以获取更多指导。本模型卡片中的任何内容均不应被解释为或视为对模型发布所依据的许可证的限制或修改。 |
数据概述
训练数据集
我们的训练数据是数学、科学和编码领域的问答和聊天格式数据的混合。聊天提示来自经过过滤的高质量网络数据,并可选择通过合成数据生成管道进行重写和处理。我们还添加了提高真实性和安全性的数据。
基准数据集
我们使用开源的Eureka评估套件和我们自己的内部基准来评估Phi - 4 - reasoning - plus,以了解模型的能力。具体来说,我们在以下方面评估我们的模型:
- 推理任务:
- AIME 2025、2024、2023和2022:数学奥林匹克问题。
- GPQA - Diamond:复杂的研究生水平科学问题。
- OmniMath:包含4000多个带人工注释的奥林匹克级数学问题的集合。
- LiveCodeBench:从竞争性编码竞赛中收集的代码生成基准。
- 3SAT(3 - 文字可满足性问题)和TSP(旅行商问题):算法问题解决。
- BA Calendar:规划。
- Maze和SpatialMap:空间理解。
- 通用基准:
- Kitab:信息检索。
- IFEval和ArenaHard:指令跟随。
- PhiBench:内部基准。
- FlenQA:提示长度对模型性能的影响。
- HumanEvalPlus:功能代码生成。
- MMLU - Pro:流行的多任务语言理解聚合数据集。
安全性
方法
Phi - 4 - reasoning - plus通过监督微调(SFT)采用了强大的安全后训练方法。该方法利用了各种开源和内部生成的合成提示,以及符合严格Microsoft安全指南的LLM生成响应,例如用户理解和清晰度、安全和道德指南、限制、免责声明和知识范围、处理复杂和敏感主题、安全和尊重参与、指南的机密性和思维链的机密性。
安全评估和红队测试
在发布之前,Phi - 4 - reasoning - plus遵循了多方面的评估方法。定量评估使用多个开源安全基准和内部工具进行,利用对抗性对话模拟。对于定性安全评估,我们与Microsoft的独立AI红队(AIRT)合作,评估Phi - 4 - reasoning - plus在普通和对抗性用户场景中的安全风险。在普通用户场景中,AIRT模拟典型的单轮和多轮交互,以识别潜在的风险行为。在对抗性用户场景中,测试了各种旨在故意破坏模型安全训练的技术,包括基础性、越狱、有害内容(如仇恨和不公平、暴力、性内容或自我伤害)以及受保护材料的版权侵犯。我们还在Toxigen上评估模型,这是一个旨在衡量针对少数群体的偏见和毒性的基准。
请参考技术报告以获取有关安全对齐的更多详细信息。
模型质量
以下是模型在代表性基准上的质量概述。在以下表格中,数值越高表示性能越好:
模型 | AIME 24 | AIME 25 | OmniMath | GPQA - D | LiveCodeBench (8/1/24–2/1/25) |
---|---|---|---|---|---|
Phi - 4 - reasoning | 75.3 | 62.9 | 76.6 | 65.8 | 53.8 |
Phi - 4 - reasoning - plus | 81.3 | 78.0 | 81.9 | 68.9 | 53.1 |
OpenThinker2 - 32B | 58.0 | 58.0 | — | 64.1 | — |
QwQ 32B | 79.5 | 65.8 | — | 59.5 | 63.4 |
EXAONE - Deep - 32B | 72.1 | 65.8 | — | 66.1 | 59.5 |
DeepSeek - R1 - Distill - 70B | 69.3 | 51.5 | 63.4 | 66.2 | 57.5 |
DeepSeek - R1 | 78.7 | 70.4 | 85.0 | 73.0 | 62.8 |
o1 - mini | 63.6 | 54.8 | — | 60.0 | 53.8 |
o1 | 74.6 | 75.3 | 67.5 | 76.7 | 71.0 |
o3 - mini | 88.0 | 78.0 | 74.6 | 77.7 | 69.5 |
Claude - 3.7 - Sonnet | 55.3 | 58.7 | 54.6 | 76.8 | — |
Gemini - 2.5 - Pro | 92.0 | 86.7 | 61.1 | 84.0 | 69.2 |
模型 | FlenQA [3K - token subset] | IFEval Strict | ArenaHard | HumanEvalPlus | MMLUPro | Kitab 无上下文 - 精确率 有上下文 - 精确率 无上下文 - 召回率 有上下文 - 召回率 |
Toxigen Discriminative 有毒类别 中性类别 |
PhiBench 2.21 |
---|---|---|---|---|---|---|---|---|
Phi - 4 | 82.0 | 62.3 | 68.1 | 83.5 | 71.5 | 19.3 88.5 8.2 68.1 |
72.6 90.0 |
58.2 |
Phi - 4 - reasoning | 97.7 | 83.4 | 73.3 | 92.9 | 74.3 | 23.2 91.5 4.9 74.8 |
86.7 84.7 |
70.6 |
Phi - 4 - reasoning - plus | 97.9 | 84.9 | 79.0 | 92.3 | 76.0 | 27.6 93.6 6.3 75.4 |
77.3 90.5 |
74.2 |
o3 - mini | 96.8 | 91.5 | 81.9 | 94.0 | 79.4 | 37.9 94.0 4.2 76.1 |
85.4 88.7 |
78.0 |
GPT - 4o | 90.8 | 81.8 | 75.6 | 88.0 | 73.0 | 53.7 84.7 20.3 69.2 |
87.6 85.1 |
72.4 |
总体而言,Phi - 4 - reasoning和Phi - 4 - reasoning - plus仅具有14B参数,但在广泛的推理任务中表现出色,显著优于一些更大的开放权重模型,如DeepSeek - R1蒸馏70B模型,并接近完整DeepSeek R1模型的性能水平。我们还在多个新的推理基准上测试了这些模型,包括3SAT、TSP和BA - Calendar。这些新任务对于模型来说名义上是域外任务,因为训练过程并未有意针对这些技能,但模型仍然对这些任务表现出强大的泛化能力。此外,在针对标准通用能力基准(如指令跟随或非推理任务)评估性能时,我们发现我们的新模型相比Phi - 4有显著改进,尽管后训练主要集中在特定领域的推理技能上。
使用说明
推理参数
使用temperature = 0.8
、top_p = 0.95
和do_sample = True
进行推理效果更佳。对于更复杂的查询,将最大令牌数设置为32k,以允许更长的思维链(CoT)。
Phi - 4 - reasoning - plus在推理密集型任务中表现出色。在我们的实验中,我们将其最大令牌数扩展到64k,它在处理更长序列时取得了有希望的结果,在扩展输入上保持了连贯性和逻辑一致性。这使其成为需要深度、多步推理或广泛上下文的任务的有吸引力的选择。
输入格式
鉴于训练数据的性质,推理时始终使用ChatML模板和以下系统提示:
<|im_start|>system<|im_sep|>
Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> {Thought section} <\think> {Solution section}. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion. Now, try to solve the following question through the above guidelines:<|im_end|>
<|im_start|>user<|im_sep|>
What is the derivative of x^2?<|im_end|>
<|im_start|>assistant<|im_sep|>
🔧 技术细节
文档未提供足够的技术实现细节(具体说明少于50字),故跳过此章节。
📄 许可证
本项目采用MIT许可证,详情请见此处。



