本文來自Google DeepMind研究員Jimmy關于PPO & GRPO 可視化介紹
https://yugeten.github.io/posts/2025/01/ppogrpo/
LLM的訓練分為pre-training and post-training
Pre-training: using large scale web data training the model with next token prediction
Post-training: 用來提高模型推理能力,分為兩個階段
Stage 1:SFT(Supervised Finetuning):首先使用監督學習,在少量高質量的專家推理數據上微調 LLM;instruction-following, question-answering and/or chain-of-thoughts。希望在這個訓練階段結束時,模型已經學會了如何模仿專家演示。
Stage 2:RLHF(Reinforcement Learning from Human Feedback):由于沒有足夠的human expert reasoning data,因此我們需要 RL!RLHF 使用人工反饋來訓練獎勵模型,然后獎勵模型通過 RL 指導 LLM 的學習。
Andrej Karpathy最近對此有個形象的比喻:
Background information / exposition。教科書的核心內容,用于解釋概念。當你關注這些信息時,你的大腦正在對這些數據進行訓練。這等同于Pre-training,即模型正在閱讀互聯網并積累背景知識。
Worked problems with solutions。這些是專家解決問題的具體示例。它們是要被模仿的演示。這等同于有監督的微調,即模型在由人類編寫的 “ideal responses” 上進行微調。
Practice problems。這些是給學生的提示,通常沒有解決方案,但總是有最終答案。通常在每章的末尾有很多很多這樣的練習題。它們促使學生通過試錯來學習 ,他們必須嘗試很多東西才能得到正確答案。這等同于RL。
DeepSeek R1 報告中最令人驚訝的一點是,他們的 R1-zero 模型完全跳過了 SFT 部分,直接將 RL 應用于基礎模型(DeepSeek V3)。
benefits:
Computational efficiency:跳過post-training的一個階段可以提高計算效率;
Open-ended learning:允許模型通過探索“自我進化”推理能力;
Alignment:避免人工精選的 SFT 數據引入的偏差。
DeepSeek 還引入 GRPO 來取代 PPO來提高 RLHF 部分的效率,相較于原來PPO,減少了 critic 模型(通常與Policy模型一樣大)的需求,從而將內存和計算開銷減少了 ~50%。
PPO vs GRPO
GRPO 對 PPO 的改進,其動機是 PPO 需要 4 個大模型,即策略、價值函數、獎勵模型和參考模型。GRPO 消除了對價值模型的需求。
為此,它首先為每個查詢生成多個響應。然后,在計算advatage時,它將 value 函數替換為樣本的獎勵,該獎勵由同一查詢的所有響應的 mean 和 std 標準化。
此外,它還將 KL 懲罰移動到損失函數中(RLHF 通常將 KL 懲罰添加到獎勵中),從而簡化了優勢的計算。
GRPO 的缺點是它需要為同一 prompt 生成多個響應,因此,如果之前每個 prompt 生成的響應很少,則 GRPO 可能會增加計算時間。
接下來了解下如何實現的:
RLHF
RLHF 的工作流程分解為四個步驟:
Step 1:對于每個 prompt, 從模型中對多個 responses 進行采樣;
Step 2: 人類按質量對這些 outputs 進行排序;
Step 3: 訓練reward model以預測 human preferences / ranking, given any model responses;
Step 4: 使用RL(e.g. PPO, GRPO) 微調模型以最大化reward model的score
過程相對簡單,有兩個可學習的部分,即reward model 和 “the RL”。
現在,讓我們深入了解這兩部分。
Reward Model
實際上,我們不能讓人類對模型的所有輸出進行ranking。一種節省成本的方法是讓標注人員對 LLM 輸出的一小部分進行評分,然后train a model to predict these annotators’ preferences,這就是獎勵模型的作用。
獎勵模型的目標函數是最小化以下目標
注意,部分響應的獎勵始終為 0;只有對于 LLM 的完整響應,獎勵模型才會返回非零標量分數。
“The RL part”: PPO
PPO(proximal policy optimization),包含三部分:
Policy: 已預先訓練/SFT 的 LLM;
Reward model:一個經過訓練和凍結的網絡,在對提示做出完全響應的情況下提供標量獎勵;
Critic:也稱為值函數,它是一個可學習的網絡,它接受對提示的部分響應并預測標量獎勵。
具體工作流程:
Generate responses: LLM 為給定的prompt生成多個response;
Score responses: reward model 給每個 response 分配reward;
Compute advantages: 使用 GAE 計算 advantages (it’s used for training the LLM);
Optimise policy: 通過優化總目標來更新 LLM;
Update critic: 訓練 value function以更好地預測給定部分響應的獎勵。
Our policy is updated to optimise advantage,直觀解釋,它定義了一個特定的動作at與policy 在狀態st決定采取的average action相比 “how much better”。
估計這種Advantage有兩種主要方法,每種方法各有優劣:
Monte-Carlo (MC):使用reward of the full trajectory(完整軌跡的獎勵)(即完整響應)。由于獎勵稀疏,這種方法具有很高的方差——從 LLM 中獲取足夠的樣本來使用 MC 進行優化是昂貴的,但它確實具有低偏差,因為我們可以準確地對獎勵進行建模;
Temporal difference (TD):使用 one-step trajectory reward(一步軌跡獎勵)(即根據提示測量剛剛生成的單詞有多好)。通過這樣做,我們可以在token級別上計算獎勵,這大大降低了方差,但與此同時,偏差也會增加,因為我們無法準確地預測部分生成的響應的最終獎勵。
為了綜合這兩種方案,提出GAE,balance the bias and variance through a multi-step TD。
但是,之前我們提到過,如果響應不完整,獎勵模型將返回 0:在不知道獎勵在生成單詞之前和之后會如何變化的情況下,我們將如何計算 TD?
因此,我們引入了一個模型來做到這一點,我們稱之為 “the critic”。
The critic (value function)
The critic 受過訓練,可以預期僅給出部分狀態的最終獎勵,以便我們可以計算 TD
Training the critic:
critic在訓練中對獎勵模型的分數進行了簡單的 L2 損失。
雖然獎勵模型R在 PPO 之前進行了訓練并被凍結,盡管R的工作只是預測獎勵,但 critic 與 LLM 一起進行了訓練。
這是因為 value 函數必須估計給定當前策略的部分響應的獎勵;因此,它必須與 LLM 一起更新,以避免其預測過時和不一致。這就是actor-critic in RL。
Back to GAE
通過critic V,我們現在有辦法預測部分狀態的獎勵。我們繼續回到GAE,目標函數是computes a multi-step TD。
在 RLHF 中,我們希望最大化這個advantage term,從而最大化 LLM 生成的每個token的reward。
Putting it together – PPO objective
PPO 目標有幾個組成部分,即 1) 裁剪的替代目標,2) 熵獎勵,3) KL 懲罰。
1. The clipped surrogate objective
具體例子:
2. KL divergence penalty
KL 散度,它可以防止當前策略 thet 偏離我們正在微調thet org
KL 只是通過取序列和批次的平均值來估計的。
# Compute KL divergence between original and current policy/model logits_orig = original_model(states) # Original model's logits logits_current = current_model(states) # Current model's logits probs_orig = F.softmax(logits_orig, dim=-1) log_probs_orig = F.log_softmax(logits_orig, dim=-1) log_probs_current = F.log_softmax(logits_current, dim=-1) kl_div = (probs_orig * (log_probs_orig - log_probs_current)).sum(dim=-1) kl_penalty = kl_div.mean() # Average over sequence and batch
3. Entropy bonus熵獎勵通過懲罰低熵來鼓勵探索 LLM 的生成
# Compute entropy of current policy probs_current = F.softmax(logits_current, dim=-1) log_probs_current = F.log_softmax(logits_current, dim=-1) entropy = -(probs_current * log_probs_current).sum(dim=-1) entropy_bonus = entropy.mean() # Average over sequence and batch
Finally, the PPO objectivePPO目標函數:
“The RL part”: GRPO
了解PPO 后就容易理解 GRPO ,關鍵區別在于兩種算法如何估計優勢 A:GRPO 不像 PPO 那樣通過批評者來估計優勢,而是使用相同的提示從 LLM 中獲取多個樣本。
在 GRPO 中,優勢近似為響應組中每個響應的標準化獎勵。這消除了評論家網絡計算每步獎勵的需要,更不用說數學的簡單性和優雅性了。
The GRPO objective
More thoughts on R1
DeepSeek-R1 的設計反映了 AI 的更廣泛趨勢:規模和簡單性往往勝過巧妙的工程設計。通過無情地偷工減料 — 用規則替換學習的組件、利用大規模并行采樣以及錨定到預先訓練的基線 — R1 以更少的故障模式實現了 SOTA 結果。它并不優雅,但很有效。
GRPO Workflow
How GRPO works: 1 ? model generates a group of answers 2 ? compute score for each answer 3 ? compute avg score for entire group 4 ? compare each answer score to avg score 5 ? reinforce model to favor higher scores
Other methods like PPO, use a value function model to do reinforcement learning.
GRPO does not, which reduces memory and computational overhead when training.
A concrete example of GRPO in action:
Query: “What is 2 + 3?” Step 1: LLM generates three answers. 1. “5” 2. “6” 3. “2 + 3 = 5” Step 2: Each answer is scored. 1. “5” → 1 points (correct, no reasoning) 2. “6” → 0 points (incorrect) 3. “2 + 3 = 5” → 2 points (correct, w/ reasoning) Step 3: Compute avg score for entire group. Avg score = (1 + 0 + 2) / 3 = 1 Step 4: Compare each answer score to avg. 1. “5” → 0 (same as avg) 2. “6” → -1 (below avg) 3. “2 + 3 = 5” → 1 (above avg) Step 5: Reinforce LLM to favor higher scores. 1. Favor responses like #3 (positive) 2. Maintain responses like #1 (neutral) 3. Avoid responses like #2 (negative) This process is repeated, allowing the model to learn and improve over time.
How use GRPO in TRL

特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.