本文來自Google DeepMind研究員Jimmy關(guān)于PPO & GRPO 可視化介紹
https://yugeten.github.io/posts/2025/01/ppogrpo/
LLM的訓(xùn)練分為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):首先使用監(jiān)督學(xué)習(xí),在少量高質(zhì)量的專家推理數(shù)據(jù)上微調(diào) LLM;instruction-following, question-answering and/or chain-of-thoughts。希望在這個訓(xùn)練階段結(jié)束時,模型已經(jīng)學(xué)會了如何模仿專家演示。
Stage 2:RLHF(Reinforcement Learning from Human Feedback):由于沒有足夠的human expert reasoning data,因此我們需要 RL!RLHF 使用人工反饋來訓(xùn)練獎勵模型,然后獎勵模型通過 RL 指導(dǎo) LLM 的學(xué)習(xí)。
Andrej Karpathy最近對此有個形象的比喻:
Background information / exposition。教科書的核心內(nèi)容,用于解釋概念。當(dāng)你關(guān)注這些信息時,你的大腦正在對這些數(shù)據(jù)進(jìn)行訓(xùn)練。這等同于Pre-training,即模型正在閱讀互聯(lián)網(wǎng)并積累背景知識。
Worked problems with solutions。這些是專家解決問題的具體示例。它們是要被模仿的演示。這等同于有監(jiān)督的微調(diào),即模型在由人類編寫的 “ideal responses” 上進(jìn)行微調(diào)。
Practice problems。這些是給學(xué)生的提示,通常沒有解決方案,但總是有最終答案。通常在每章的末尾有很多很多這樣的練習(xí)題。它們促使學(xué)生通過試錯來學(xué)習(xí) ,他們必須嘗試很多東西才能得到正確答案。這等同于RL。
DeepSeek R1 報告中最令人驚訝的一點(diǎn)是,他們的 R1-zero 模型完全跳過了 SFT 部分,直接將 RL 應(yīng)用于基礎(chǔ)模型(DeepSeek V3)。
benefits:
Computational efficiency:跳過post-training的一個階段可以提高計(jì)算效率;
Open-ended learning:允許模型通過探索“自我進(jìn)化”推理能力;
Alignment:避免人工精選的 SFT 數(shù)據(jù)引入的偏差。
DeepSeek 還引入 GRPO 來取代 PPO來提高 RLHF 部分的效率,相較于原來PPO,減少了 critic 模型(通常與Policy模型一樣大)的需求,從而將內(nèi)存和計(jì)算開銷減少了 ~50%。
PPO vs GRPO
GRPO 對 PPO 的改進(jìn),其動機(jī)是 PPO 需要 4 個大模型,即策略、價值函數(shù)、獎勵模型和參考模型。GRPO 消除了對價值模型的需求。
為此,它首先為每個查詢生成多個響應(yīng)。然后,在計(jì)算advatage時,它將 value 函數(shù)替換為樣本的獎勵,該獎勵由同一查詢的所有響應(yīng)的 mean 和 std 標(biāo)準(zhǔn)化。
此外,它還將 KL 懲罰移動到損失函數(shù)中(RLHF 通常將 KL 懲罰添加到獎勵中),從而簡化了優(yōu)勢的計(jì)算。
GRPO 的缺點(diǎn)是它需要為同一 prompt 生成多個響應(yīng),因此,如果之前每個 prompt 生成的響應(yīng)很少,則 GRPO 可能會增加計(jì)算時間。
接下來了解下如何實(shí)現(xiàn)的:
RLHF
RLHF 的工作流程分解為四個步驟:
Step 1:對于每個 prompt, 從模型中對多個 responses 進(jìn)行采樣;
Step 2: 人類按質(zhì)量對這些 outputs 進(jìn)行排序;
Step 3: 訓(xùn)練reward model以預(yù)測 human preferences / ranking, given any model responses;
Step 4: 使用RL(e.g. PPO, GRPO) 微調(diào)模型以最大化reward model的score
過程相對簡單,有兩個可學(xué)習(xí)的部分,即reward model 和 “the RL”。
現(xiàn)在,讓我們深入了解這兩部分。
Reward Model
實(shí)際上,我們不能讓人類對模型的所有輸出進(jìn)行ranking。一種節(jié)省成本的方法是讓標(biāo)注人員對 LLM 輸出的一小部分進(jìn)行評分,然后train a model to predict these annotators’ preferences,這就是獎勵模型的作用。
獎勵模型的目標(biāo)函數(shù)是最小化以下目標(biāo)
注意,部分響應(yīng)的獎勵始終為 0;只有對于 LLM 的完整響應(yīng),獎勵模型才會返回非零標(biāo)量分?jǐn)?shù)。
“The RL part”: PPO
PPO(proximal policy optimization),包含三部分:
Policy: 已預(yù)先訓(xùn)練/SFT 的 LLM;
Reward model:一個經(jīng)過訓(xùn)練和凍結(jié)的網(wǎng)絡(luò),在對提示做出完全響應(yīng)的情況下提供標(biāo)量獎勵;
Critic:也稱為值函數(shù),它是一個可學(xué)習(xí)的網(wǎng)絡(luò),它接受對提示的部分響應(yīng)并預(yù)測標(biāo)量獎勵。
具體工作流程:
Generate responses: LLM 為給定的prompt生成多個response;
Score responses: reward model 給每個 response 分配reward;
Compute advantages: 使用 GAE 計(jì)算 advantages (it’s used for training the LLM);
Optimise policy: 通過優(yōu)化總目標(biāo)來更新 LLM;
Update critic: 訓(xùn)練 value function以更好地預(yù)測給定部分響應(yīng)的獎勵。
Our policy is updated to optimise advantage,直觀解釋,它定義了一個特定的動作at與policy 在狀態(tài)st決定采取的average action相比 “how much better”。
估計(jì)這種Advantage有兩種主要方法,每種方法各有優(yōu)劣:
Monte-Carlo (MC):使用reward of the full trajectory(完整軌跡的獎勵)(即完整響應(yīng))。由于獎勵稀疏,這種方法具有很高的方差——從 LLM 中獲取足夠的樣本來使用 MC 進(jìn)行優(yōu)化是昂貴的,但它確實(shí)具有低偏差,因?yàn)槲覀兛梢詼?zhǔn)確地對獎勵進(jìn)行建模;
Temporal difference (TD):使用 one-step trajectory reward(一步軌跡獎勵)(即根據(jù)提示測量剛剛生成的單詞有多好)。通過這樣做,我們可以在token級別上計(jì)算獎勵,這大大降低了方差,但與此同時,偏差也會增加,因?yàn)槲覀儫o法準(zhǔn)確地預(yù)測部分生成的響應(yīng)的最終獎勵。
為了綜合這兩種方案,提出GAE,balance the bias and variance through a multi-step TD。
但是,之前我們提到過,如果響應(yīng)不完整,獎勵模型將返回 0:在不知道獎勵在生成單詞之前和之后會如何變化的情況下,我們將如何計(jì)算 TD?
因此,我們引入了一個模型來做到這一點(diǎn),我們稱之為 “the critic”。
The critic (value function)
The critic 受過訓(xùn)練,可以預(yù)期僅給出部分狀態(tài)的最終獎勵,以便我們可以計(jì)算 TD
Training the critic:
critic在訓(xùn)練中對獎勵模型的分?jǐn)?shù)進(jìn)行了簡單的 L2 損失。
雖然獎勵模型R在 PPO 之前進(jìn)行了訓(xùn)練并被凍結(jié),盡管R的工作只是預(yù)測獎勵,但 critic 與 LLM 一起進(jìn)行了訓(xùn)練。
這是因?yàn)?value 函數(shù)必須估計(jì)給定當(dāng)前策略的部分響應(yīng)的獎勵;因此,它必須與 LLM 一起更新,以避免其預(yù)測過時和不一致。這就是actor-critic in RL。
Back to GAE
通過critic V,我們現(xiàn)在有辦法預(yù)測部分狀態(tài)的獎勵。我們繼續(xù)回到GAE,目標(biāo)函數(shù)是computes a multi-step TD。
在 RLHF 中,我們希望最大化這個advantage term,從而最大化 LLM 生成的每個token的reward。
Putting it together – PPO objective
PPO 目標(biāo)有幾個組成部分,即 1) 裁剪的替代目標(biāo),2) 熵獎勵,3) KL 懲罰。
1. The clipped surrogate objective
具體例子:
2. KL divergence penalty
KL 散度,它可以防止當(dāng)前策略 thet 偏離我們正在微調(diào)thet org
KL 只是通過取序列和批次的平均值來估計(jì)的。
# 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目標(biāo)函數(shù):
“The RL part”: GRPO
了解PPO 后就容易理解 GRPO ,關(guān)鍵區(qū)別在于兩種算法如何估計(jì)優(yōu)勢 A:GRPO 不像 PPO 那樣通過批評者來估計(jì)優(yōu)勢,而是使用相同的提示從 LLM 中獲取多個樣本。
在 GRPO 中,優(yōu)勢近似為響應(yīng)組中每個響應(yīng)的標(biāo)準(zhǔn)化獎勵。這消除了評論家網(wǎng)絡(luò)計(jì)算每步獎勵的需要,更不用說數(shù)學(xué)的簡單性和優(yōu)雅性了。
The GRPO objective
More thoughts on R1
DeepSeek-R1 的設(shè)計(jì)反映了 AI 的更廣泛趨勢:規(guī)模和簡單性往往勝過巧妙的工程設(shè)計(jì)。通過無情地偷工減料 — 用規(guī)則替換學(xué)習(xí)的組件、利用大規(guī)模并行采樣以及錨定到預(yù)先訓(xùn)練的基線 — R1 以更少的故障模式實(shí)現(xiàn)了 SOTA 結(jié)果。它并不優(yōu)雅,但很有效。
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

特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺“網(wǎng)易號”用戶上傳并發(fā)布,本平臺僅提供信息存儲服務(wù)。
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.