微軟推出兩款功能強大的Phi-4推理模型

微軟推出兩款功能強大的Phi-4推理模型

微軟與 OpenAI、谷歌和 Meta 不同,尤其是在大型語言模型方面。其他科技巨頭喜歡推出多個模型,幾乎讓使用者應接不暇;而微軟只推出幾個模型,但這些模型總能在全球開發者中大受歡迎。在最新的版本中,他們釋出了兩個推理模型: Phi-4-Reasoning 和 Phi-4-Reasoning-plus 都是在基礎 Phi-4 模型上訓練出來的。這兩個 Phi-4-Reasoning 模型可與 o1、o3-mini 和 DeepSeek R1 等強大的模型競爭。在本文中,我們將詳細介紹 Phi-4-Reasoning 模型的技術細節、架構、訓練方法和效能。

讓我們一起來探索 Phi-4 推理模型。

什麼是Phi-4推理模型?

在 LLM 領域,Phi-4 並不陌生。去年,這個小而強大的語言模型一經推出,就在網際網路上引起了轟動。現在,為了滿足對推理模型日益增長的需求,微軟釋出了 Phi-4-Reasoning 模型。這是 14B 引數,擅長執行涉及數學、編碼和 STEM 問題的複雜推理任務。與通用的 Phi-4 系列相比,Phi-4-Reasoning 專門針對長鏈推理進行了最佳化,即能夠將複雜的多步驟問題系統地分解為邏輯步驟。

Phi 4推理模型

微軟釋出的兩種推理模型是

  • Phi-4-Reasoning:在高質量資料集上使用監督微調或 SFT 訓練的推理模型。該模型適用於所有需要更快響應且效能受限的任務。
  • Phi-4-Reasoning-Plus:這是一個增強型推理模型,它透過強化學習或 RL 來提高效能,但與同類模型相比,其生成的標記數量幾乎增加了 50%。該模型的延遲會增加,因此建議用於高精確度任務。

這兩個 14B 模型目前只支援文字輸入,微軟已將它們作為開放重量級模型釋出,開發人員可以根據自己的需求自由測試和微調它們。以下是這些模型的一些主要亮點:

細節 Phi-4-Reasoning模型
開發人員 Microsoft Research
模型變體 Phi-4-Reasoning, Phi-4-Reasoning-Plus
基礎結構 Phi-4(14B 引數),僅密集解碼器變換器
訓練方法 對思維鏈資料進行監督微調;Plus 變體包括額外的強化學習 (RLHF)
訓練時間 在 32× H100-80G GPU 上執行 2.5 天
訓練資料 共 16B 個 tokens(約 83B 個唯一tokens),來自合成提示和過濾後的公共領域資料
訓練週期 2025 年 1 月至 4 月
資料截止 2025 年 3 月
輸入格式 文字輸入,針對聊天式提示進行了最佳化
上下文長度 32,000 tokens
輸出格式 兩個部分:推理思維鏈塊和總結塊
釋出日期 2025 年 4 月 30 日

Phi-4-Reasoning模型的主要特點

對於 Phi-4,團隊在資料選擇、訓練方法和效能方面採取了多項創新措施。他們所做的一些關鍵工作包括

以資料為中心的訓練

為訓練 Phi-4 推理模型而進行的資料整理不僅依賴於資料的數量,也同樣重視資料的質量。他們特別選擇了處於模型能力“邊緣”的資料。這確保了訓練資料可解但不易解。

為 Phi-4 模型建立資料集的主要步驟包括

  • 種子資料庫:微軟團隊從 AIME 和 GPQA 等公開資料集入手。這些資料集涉及多步驟推理的代數和幾何問題。
  • 合成推理鏈:為了獲得全面而詳細的分步推理回答,微軟團隊依賴於 OpenAI 的 o3-mini 模型。

例如,對於“sin(x2) 的導數是多少?”這一問題,o3-mini 的輸出結果如下:

Step 1: Apply the chain rule: d/dx sin(u)=cos(u)*du/dx.  Step 2: Let u=x² ⇒ du/dx=2x. Final Answer: cos(x²) * 2x.

這些人為或合成生成的推理回答鏈為模型如何構建自己的推理回答提供了清晰的藍圖。

  • 選擇“Teachable Moments”:開發團隊有意識地選擇既能解決又能挑戰基礎 Phi-4 模型的提示。這些問題包括 Phi-4 最初顯示出 50%左右準確率的問題。這種方法確保了訓練過程避免使用“簡單”的資料,因為這些資料只會強化現有的模式,而更注重“結構化推理”。

該團隊主要是想讓 Phi-4 推理模型像它們一樣學習,這也是我們人類通常依賴的方法。

監督微調(SFT)

監督微調(SFT)是透過在精心挑選的具有高質量響應的輸入輸出對上進行訓練來改進預訓練語言模型的過程。對於 Phi-4-Reasoning 模型來說,這意味著從基本的 Phi-4 模型開始,然後使用推理任務對其進行改進。從根本上說,Phi-4-Reasoning 是為了學習和遵循 o3-mini 的回答中的逐步推理模式而訓練的。

訓練細節

  • 批次大小:保持在 32 個。這種較小的批次規模允許模型專注於單個示例,而不會被額外的噪音所淹沒。
  • 學習率:學習率為 7e-5,這是一個適中的學習率,可以避免在更新過程中過沖最優權重。
  • 最佳化器:使用了標準的“Adam W”最佳化器。這種深度學習最佳化器兼顧了速度和穩定性。
  • 上下文長度:上下文長度為 32,768 個令牌,是基礎 Phi-4 模型 16K 令牌限制的兩倍。這使得模型能夠處理更長的上下文。

在早期訓練中使用 SFT 可以讓模型使用 <thinking> 和 </thinking> 標記將原始輸入與內部推理分離開來。這種結構使其決策過程變得透明。此外,該模型在 AIME 基準測試中也取得了穩步提高,這證明該模型不僅僅是在複製格式,而是在構建推理邏輯。

強化學習

強化學習是透過對模型生成的所有輸出進行反饋,教導模型如何做得更好。模型每次回答正確都會得到獎勵,每次回答錯誤都會受到懲罰。RL 用於進一步訓練 Phi-4-Reasoning -Plus 模型。這種訓練方法完善了模型的數學解題技巧,評估了回答的準確性和結構化方法。

RL 是如何工作的?

  • 獎勵設計:模型每回答正確一次,獎勵+1;回答錯誤一次,獎勵-0.5。模型因重複“Let’s see.. Let’s see..”等。
  • 演算法:使用 GRPO 演算法或廣義獎勵策略最佳化演算法,這是一種兼顧探索和利用的 RL 變體。
  • 結果:在 AIME 2025 中,Phi-4-Reasoning-Plus 的準確率達到 82.5%,而 Phi-4 Reasoning 僅為 71.4%。它在 Omni-MATH 和 TSP(旅行推銷員問題)上的表現也有所提高。

RL 訓練允許模型反覆改進其步驟,並有助於減少生成輸出中的“幻覺”。

Phi-4-Reasoning模型的架構

Phi-4-Reasoning 模型的主要結構與基本的 Phi-4 模型相似,但為了支援“推理”任務,我們做了一些關鍵的修改。

  1. 我們重新使用了 Phi-4 中的兩個佔位符。這些標記有助於模型區分原始輸入和內部推理。
    • <thinking>:用於標記推理塊的開始。
    • </thinking>:用於標記推理塊的結束。
  2. Phi-4 推理模型有一個 32K 標記的擴充套件上下文視窗,以處理額外的推理鏈。
  3. 模型使用旋轉位置嵌入來更好地跟蹤長序列中標記的位置,以幫助模型保持一致性。
  4. 經過訓練的模型可在消費類硬體(包括手機、平板電腦和桌上型電腦等裝置)上高效執行。

Phi-4-Reasoning模型:基準效能

我們在各種基準上對 Phi-4 推理模型進行了評估,以測試它們在不同任務上與不同模型的效能比較。

Phi-4-Reasoning模型:基準效能

Source: Phi-4-Reasoning

  • AIME 2025:這是一項測試高階數學、推理和近期考試難度的基準測試。Phi-4-Reasoning Plus 的表現優於 o1 和 Claude 3.7 Sonnet 等大多數表現優異的模型,但仍落後於 o3-mini-high。
  • Omni-MATH:評估不同主題和水平的數學推理的基準。Phi-4-Reasoning 和 Phi-4-Reasoning plus 的表現幾乎優於所有模型,僅落後於 DeepSeek R1。
  • GPQA:該基準測試模型在研究生水平的專業質量保證推理中的表現。兩個 Phi 推理模型落後於 o1、o3-mini high 和 DeepSeek R1 等巨頭。
  • SAT:評估美國高中水平學術推理(數學+言語混合)的基準。Phi-4-Reasoning-Plus 模型躋身前三名,Phi-4-Reasoning 緊隨其後。
  • Maze:該基準測試導航和決策尋路推理。在此基準測試中,Phi-4 推理模型落後於 o1 和 Claude 3.7 sonnet 等頂級模型。

在空間地圖、TSP 和 BA 日曆等其他基準測試中,Phi-4 推理模型的表現都還不錯。

如何訪問Phi-4-Reasoning模型?

兩種 Phi-4 推理模型可在 Hugging Face 上獲取:

點選連結進入擁抱面孔頁面,在那裡你可以訪問這些模型。在螢幕右上角點選“Use This Model”,點選“Transformers”並複製以下程式碼:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Use a pipeline as a high-level helper
from transformers import pipeline
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe = pipeline("text-generation", model="microsoft/Phi-4-reasoning")
pipe(messages)
# Use a pipeline as a high-level helper from transformers import pipeline messages = [ {"role": "user", "content": "Who are you?"}, ] pipe = pipeline("text-generation", model="microsoft/Phi-4-reasoning") pipe(messages)
# Use a pipeline as a high-level helper
from transformers import pipeline
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe = pipeline("text-generation", model="microsoft/Phi-4-reasoning")
pipe(messages)

由於這是一個 14B 引數的模型,因此需要大約 40GB 以上的 VRAM(GPU),您可以在“Colab Pro”或“Runpod ”上執行這些模型。在本文中,我們在“Runpod”上執行了模型,並使用了“A100 GPU”。

安裝所需庫

首先,確保已安裝變壓器庫。可以使用 pip 安裝:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install transformers
pip install transformers
pip install transformers

載入模型

安裝完所有庫後,就可以在筆記本上載入 Phi-4-Reasoning 模型了:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="microsoft/Phi-4-reasoning", max_new_tokens=4096)
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/Phi-4-reasoning", max_new_tokens=4096)
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="microsoft/Phi-4-reasoning", max_new_tokens=4096)

請確保設定 max_new_tokens = 4096,因為模型會生成整個推理過程,較少的標記數往往會中途停止輸出。

Phi-4-Reasoning實際應用

現在,我們將在兩個涉及邏輯思維和推理的任務中測試 Phi-4 推理模型。讓我們開始吧。

任務 1:邏輯思維

輸入:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
messages = [
{"role": "user", "content": """A team is to be selected from among ten persons — A, B, C, D, E, F, G, H, I and J — subject to the following conditions.
Exactly two among E, J, l and C must be selected.
If F is selected, then J cannot be selected.
Exactly one among A and C must be selected.
Unless A is selected, E cannot be selected,
If and only if G is selected, D must not be selected.
If D is not selected, then H must be selected.
The size of a team is defined as the number of members in the team. In how many ways can the team of size 6 be selected, if it includes E? and What is the largest possible size of the team?"""
},
]
messages = [ {"role": "user", "content": """A team is to be selected from among ten persons — A, B, C, D, E, F, G, H, I and J — subject to the following conditions. Exactly two among E, J, l and C must be selected. If F is selected, then J cannot be selected. Exactly one among A and C must be selected. Unless A is selected, E cannot be selected, If and only if G is selected, D must not be selected. If D is not selected, then H must be selected. The size of a team is defined as the number of members in the team. In how many ways can the team of size 6 be selected, if it includes E? and What is the largest possible size of the team?""" }, ]
messages = [
    {"role": "user", "content": """A team is to be selected from among ten persons — A, B, C, D, E, F, G, H, I and J — subject to the following conditions.
Exactly two among E, J, l and C must be selected.
If F is selected, then J cannot be selected.
Exactly one among A and C must be selected.
Unless A is selected, E cannot be selected,
If and only if G is selected, D must not be selected.
If D is not selected, then H must be selected.
The size of a team is defined as the number of members in the team. In how many ways can the team of size 6 be selected, if it includes E? and What is the largest possible size of the team?"""
    },
]

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Markdown(pipe(messages)[0]["generated_text"][1]["content"])
Markdown(pipe(messages)[0]["generated_text"][1]["content"])
Markdown(pipe(messages)[0]["generated_text"][1]["content"])

該模型思考透徹。它很好地將整個問題分解成幾個小步驟。問題由兩個任務組成,在給定的標記視窗中,它給出了第一個任務的答案,但無法生成第二個任務的答案。有趣的是該模型在解決給定問題時所採取的方法。首先,它從理解問題開始,找出所有的可能性,然後開始解決每一項任務,有時還會重複它預先建立的邏輯。

任務 2:向一個8歲的孩子解釋LLM工作原理

輸入

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
messages = [
{"role": "user", "content": """Explain How LLMs works by comparing their working to the photosynthesis process in a plant so that an 8 year old kid can actually understand"""
},
]
messages = [ {"role": "user", "content": """Explain How LLMs works by comparing their working to the photosynthesis process in a plant so that an 8 year old kid can actually understand""" }, ]
messages = [
    {"role": "user", "content": """Explain How LLMs works by comparing their working to the photosynthesis process in a plant so that an 8 year old kid can actually understand"""
    },
]

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Markdown(pipe(messages)[0]["generated_text"][1]["content"])
Markdown(pipe(messages)[0]["generated_text"][1]["content"])
Markdown(pipe(messages)[0]["generated_text"][1]["content"])

在生成這個問題的響應時,模型產生了一些幻覺。最後,它生成的答案將 LLM 的工作原理與光合作用過程進行了很好的類比。它的語言非常簡單,最後還加上了免責宣告。

Phi-4-Reasoning與o3-mini:比較

在上一節中,我們看到了 Phi-4 推理模型在處理複雜問題時的表現。現在,讓我們將其效能與 OpenAI 的 o3-mini 進行比較。為此,我們將測試兩個模型在相同任務中的輸出結果。

Phi-4-Reasoning

輸入

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from IPython.display import Markdown
messages = [
{"role": "user", "content": """Suppose players A and B are playing a game with fair coins. To begin the game A and B
both flip their coins simultaneously. If A and B both get heads, the game ends. If A and B both get tails, they both
flip again simultaneously. If one player gets heads and the other gets tails, the player who got heads flips again until he
gets tails, at which point the players flip again simultaneously. What is the expected number of flips until the game ends?"""
},
]
Output = pipe(messages)
from IPython.display import Markdown messages = [ {"role": "user", "content": """Suppose players A and B are playing a game with fair coins. To begin the game A and B both flip their coins simultaneously. If A and B both get heads, the game ends. If A and B both get tails, they both flip again simultaneously. If one player gets heads and the other gets tails, the player who got heads flips again until he gets tails, at which point the players flip again simultaneously. What is the expected number of flips until the game ends?""" }, ] Output = pipe(messages)
from IPython.display import Markdown
messages = [
    {"role": "user", "content": """Suppose players A and B are playing a game with fair coins. To begin the game A and B 
    both flip their coins simultaneously. If A and B both get heads, the game ends. If A and B both get tails, they both 
    flip again simultaneously. If one player gets heads and the other gets tails, the player who got heads flips again until he 
    gets tails, at which point the players flip again simultaneously. What is the expected number of flips until the game ends?"""
    },
]
Output = pipe(messages)

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Markdown(Output[0]["generated_text"][1]["content"])
Markdown(Output[0]["generated_text"][1]["content"])
Markdown(Output[0]["generated_text"][1]["content"])

Phi-4-Reasoning與o3-mini:比較

o3-mini

輸入:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
response = client.responses.create(
model="o3-mini",
input="""Suppose players A and B are playing a game with fair coins. To begin the game A and B
both flip their coins simultaneously. If A and B both get heads, the game ends. If A and B both get tails, they both
flip again simultaneously. If one player gets heads and the other gets tails, the player who got heads flips again until he
gets tails, at which point the players flip again simultaneously. What is the expected number of flips until the game ends?"""
)
response = client.responses.create( model="o3-mini", input="""Suppose players A and B are playing a game with fair coins. To begin the game A and B both flip their coins simultaneously. If A and B both get heads, the game ends. If A and B both get tails, they both flip again simultaneously. If one player gets heads and the other gets tails, the player who got heads flips again until he gets tails, at which point the players flip again simultaneously. What is the expected number of flips until the game ends?""" )
response = client.responses.create(
    model="o3-mini",
    input="""Suppose players A and B are playing a game with fair coins. To begin the game A and B 
    both flip their coins simultaneously. If A and B both get heads, the game ends. If A and B both get tails, they both 
    flip again simultaneously. If one player gets heads and the other gets tails, the player who got heads flips again until he 
    gets tails, at which point the players flip again simultaneously. What is the expected number of flips until the game ends?"""
)

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
print(response.output_text)
print(response.output_text)
print(response.output_text)

Phi-4-Reasoning與o3-mini:比較

要檢視詳細的輸出結果,請參閱以下 Github 連結。

結果評估

兩個模型都給出了準確的答案。另一方面,o3-mini 將思考和最終答案更順暢地結合在一起,使輸出結果清晰易用。它的答案也更加簡潔直接。

Phi-4-Reasoning模型的應用

Phi-4 推理模型開闢了一個充滿可能性的世界。開發人員可以利用這些模型開發智慧系統,以滿足不同行業的需求。以下是Phi-4推理模型真正可以大顯身手的幾個領域:

  • 它們在編碼基準(如 LiveCodeBench)中的強勁表現表明,它們可應用於程式碼生成、除錯、演算法設計和自動化軟體開發。
  • 它們生成詳細推理鏈的能力使其非常適合回答需要多步推理和邏輯演繹的複雜問題。
  • 這些模型在規劃任務方面的能力可用於物流、資源管理、遊戲和需要連續決策的自主系統。
  • 這些模型還有助於設計機器人、自主導航以及涉及空間關係解釋和操作任務的系統。

小結

Phi-4推理模型是開放式的,可與 DeepSeek 和 OpenAI 的 o3-mini 等頂級付費推理模型競爭。由於它們沒有經過指令調整,因此它們的答案可能不像某些流行模型那樣總是遵循清晰、結構化的格式,但隨著時間的推移或自定義微調,這種情況會有所改善。微軟的新模型是功能強大的推理工具,效能卓越,而且只會越來越好。

評論留言