模型概述
模型特點
模型能力
使用案例
🚀 Phi-4-reasoning模型
Phi-4-reasoning是基於Phi-4微調的推理模型,通過監督微調與強化學習,使用聚焦高質量和高級推理的數據訓練,適用於對內存、計算、延遲有要求及需推理邏輯的場景。
🚀 快速開始
Phi-4-reasoning模型可用於生成文本,以響應輸入的文本提示。要充分發揮模型的能力,推理時必須使用 temperature=0.8
、top_k=50
、top_p=0.95
並設置 do_sample=True
。對於更復雜的查詢,可設置 max_new_tokens=32768
以支持更長的思維鏈(CoT)。
✨ 主要特性
- 先進架構:基於14B參數的僅解碼器密集Transformer架構,與之前發佈的Phi-4基礎模型相同。
- 長上下文處理:支持32k token的上下文長度。
- 多領域適用:訓練數據涵蓋數學、科學和編碼等領域的問答和聊天格式數據。
- 強大推理能力:在多種推理任務和通用基準測試中表現出色。
- 安全可靠:採用監督微調的安全後訓練方法,並經過多方面的安全評估。
📦 安裝指南
使用 transformers
庫
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-4-reasoning")
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-4-reasoning", device_map="auto", torch_dtype="auto")
使用 vllm
vllm serve microsoft/Phi-4-reasoning --enable-reasoning --reasoning-parser deepseek_r1
Phi-4-reasoning也可直接在Ollama、llama.cpp和任何與Phi-4兼容的框架中使用。
💻 使用示例
基礎用法
由於訓練數據的性質,推理時始終使用ChatML模板和以下系統提示:
<|im_start|>system<|im_sep|>
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:<|im_end|>
<|im_start|>user<|im_sep|>
What is the derivative of x^2?<|im_end|>
<|im_start|>assistant<|im_sep|>
高級用法
使用 transformers
庫進行推理的示例代碼:
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-4-reasoning")
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-4-reasoning", 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_k=50,
top_p=0.95,
do_sample=True,
)
print(tokenizer.decode(outputs[0]))
📚 詳細文檔
模型概述
屬性 | 詳情 |
---|---|
開發者 | Microsoft Research |
描述 | Phi-4-reasoning是一個最先進的開放權重推理模型,基於Phi-4進行監督微調,並使用強化學習。監督微調數據集包括合成提示和來自公共領域網站的高質量過濾數據,重點關注數學、科學和編碼技能,以及用於安全和負責任AI的對齊數據。目標是確保使用專注於高質量和高級推理的數據訓練小型高效模型。 |
架構 | 基礎模型與之前發佈的Phi-4相同,14B參數,僅解碼器密集Transformer模型 |
輸入 | 文本,最適合聊天格式的提示 |
上下文長度 | 32k tokens |
GPU要求 | 32個H100-80G |
訓練時間 | 2.5天 |
訓練數據 | 16B tokens,約83億唯一tokens |
輸出 | 針對輸入生成的文本。模型響應分為兩個部分,即推理思維鏈塊和總結塊 |
日期 | 2025年1月 - 2025年4月 |
狀態 | 基於離線數據集訓練的靜態模型,公開可用數據的截止日期為2025年3月及更早 |
發佈日期 | 2025年4月30日 |
許可證 | MIT |
預期用途
主要用例
該模型旨在加速語言模型的研究,作為生成式AI功能的構建塊。適用於通用AI系統和應用程序(主要為英文),這些應用需要:
- 內存/計算受限的環境。
- 低延遲場景。
- 推理和邏輯能力。
非預期用例
該模型僅針對數學推理進行設計和測試,並非專門為所有下游用途設計或評估。開發者在選擇用例時應考慮語言模型的常見限制,並在特定下游用例中使用之前評估和減輕準確性、安全性和公平性方面的問題,特別是在高風險場景中。開發者應瞭解並遵守與其用例相關的適用法律或法規(包括隱私、貿易合規法等),包括模型對英文的關注。選擇用例時,請參考下面的負責任AI考慮部分以獲取更多指導。本模型卡片中的任何內容均不應被解釋為或視為對模型發佈所依據的許可證的限制或修改。
數據概述
訓練數據集
訓練數據是數學、科學和編碼領域的問答和聊天格式數據的混合。聊天提示來自過濾後的高質量網絡數據,並可選擇通過合成數據生成管道進行重寫和處理。還包括用於提高真實性和安全性的數據。
基準數據集
使用開源的 Eureka 評估套件和內部基準評估Phi-4-reasoning的能力。具體評估任務如下:
推理任務:
- AIME 2025、2024、2023和2022:數學奧林匹克問題。
- GPQA-Diamond:複雜的研究生水平科學問題。
- OmniMath:超過4000個有人類標註的奧林匹克水平數學問題集合。
- LiveCodeBench:從競爭性編程競賽中收集的代碼生成基準。
- 3SAT(3文字可滿足性問題)和TSP(旅行商問題):算法問題解決。
- BA Calendar:規劃。
- Maze and SpatialMap:空間理解。
通用基準:
- Kitab:信息檢索。
- IFEval和ArenaHard:指令遵循。
- PhiBench:內部基準。
- FlenQA:提示長度對模型性能的影響。
- HumanEvalPlus:功能代碼生成。
- MMLU-Pro:流行的多任務語言理解聚合數據集。
安全性
方法
Phi-4-reasoning通過監督微調(SFT)採用了強大的安全後訓練方法。該方法利用了各種開源和內部生成的合成提示,以及符合嚴格Microsoft安全指南的LLM生成響應,例如用戶理解和清晰度、安全和道德指南、限制、免責聲明和知識範圍、處理複雜和敏感主題、安全和尊重互動、指南的保密性和思維鏈的保密性。
安全評估和紅隊測試
在發佈之前,Phi-4-reasoning遵循了多方面的評估方法。使用多個開源安全基準和內部工具進行對抗性對話模擬,進行定量評估。為了進行定性安全評估,與Microsoft的獨立AI紅隊(AIRT)合作,評估Phi-4-reasoning在普通和對抗性用戶場景中的安全風險。在普通用戶場景中,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 |
總體而言,僅具有14B參數的Phi-4-reasoning在各種推理任務中表現出色,顯著優於參數更大的開放權重模型,如DeepSeek-R1蒸餾70B模型,並接近完整DeepSeek R1模型的性能水平。還在多個新的推理基準上測試了模型,包括3SAT、TSP和BA-Calendar等算法問題解決和規劃任務。這些新任務對於模型來說名義上是域外任務,因為訓練過程並未有意針對這些技能,但模型仍然對這些任務表現出強大的泛化能力。此外,在針對標準通用能力基準(如指令遵循或非推理任務)評估性能時,發現新模型相比Phi-4有顯著改進,儘管後訓練主要集中在特定領域的推理技能上。
負責任AI考慮
與其他語言模型一樣,Phi-4-reasoning可能會表現出不公平、不可靠或冒犯性的行為。需要注意的一些限制行為包括:
- 服務質量:模型主要基於英文文本進行訓練,非英文語言的性能會較差。訓練數據中代表性較少的英文變體的性能可能不如標準美式英語。Phi-4-reasoning不支持多語言使用。
- 傷害的代表性和刻板印象的延續:這些模型可能會過度或不足地代表某些人群,抹去某些群體的代表性,或強化貶低性或負面的刻板印象。儘管進行了安全後訓練,但由於不同群體的代表性水平不同或訓練數據中反映現實世界模式和社會偏見的負面刻板印象示例的普遍性,這些限制可能仍然存在。
- 不適當或冒犯性內容:這些模型可能會產生其他類型的不適當或冒犯性內容,在沒有針對特定用例進行額外緩解措施的情況下,可能不適合在敏感環境中部署。
- 信息可靠性:語言模型可能會生成無意義的內容或編造聽起來合理但不準確或過時的內容。
- 選舉信息可靠性:模型在響應選舉關鍵查詢時的缺陷率較高,可能會導致呈現不正確或非權威性的選舉關鍵信息。正在努力改進模型在這方面的性能。用戶應向所在地區的選舉機構核實與選舉相關的信息。
- 代碼範圍有限:Phi-4-reasoning的大部分訓練數據基於Python,並使用常見的包,如
typing
、math
、random
、collections
、datetime
、itertools
。如果模型生成使用其他包的Python腳本或其他語言的腳本,強烈建議用戶手動驗證所有API的使用。
開發者應應用負責任AI的最佳實踐,並負責確保特定用例符合相關法律法規(如隱私、貿易等)。強烈建議使用具有高級防護措施的安全服務,如 Azure AI Content Safety。需要考慮的重要方面包括:
- 分配:在沒有進一步評估和額外去偏技術的情況下,模型可能不適用於對法律地位、資源分配或生活機會(如住房、就業、信貸等)有重大影響的場景。
- 高風險場景:開發者應評估在高風險場景中使用模型的適用性,在這些場景中,不公平、不可靠或冒犯性的輸出可能會造成極大的代價或導致傷害。這包括在準確性和可靠性至關重要的敏感或專業領域提供建議(如法律或健康建議)。應根據部署上下文在應用程序級別實施額外的保障措施。
- 錯誤信息:模型可能會產生不準確的信息。開發者應遵循透明度最佳實踐,並告知最終用戶他們正在與AI系統進行交互。在應用程序級別,開發者可以構建反饋機制和管道,將響應與特定用例的上下文信息相結合,這種技術稱為檢索增強生成(RAG)。
- 有害內容的生成:開發者應根據上下文評估輸出,並使用適合其用例的可用安全分類器或自定義解決方案。
- 濫用:可能存在其他形式的濫用,如欺詐、垃圾郵件或惡意軟件生產,開發者應確保其應用程序不違反適用的法律法規。
🔧 技術細節
Phi-4-reasoning基於之前發佈的Phi-4模型,採用14B參數的僅解碼器密集Transformer架構。通過監督微調(SFT)和強化學習進行訓練,使用了包含數學、科學和編碼等領域的問答和聊天格式數據。訓練過程中,利用了多種開源和內部生成的合成提示,以及符合嚴格Microsoft安全指南的LLM生成響應。在安全評估方面,採用了多方面的評估方法,包括定量評估和定性評估,以確保模型在各種場景下的安全性。
📄 許可證
本項目採用 MIT許可證。



