PPO
Proximal Policy Optimization
A reinforcement learning algorithm for stable policy updates. Mainly used in RLHF.
Proximal Policy Optimization
A reinforcement learning algorithm for stable policy updates. Mainly used in RLHF.
PPO (Proximal Policy Optimization) is an algorithm that updates the policy stably in reinforcement learning (RL). In particular, it is used as a core algorithm in RLHF (Reinforcement Learning from Human Feedback) for large language models (LLMs), and is used in the training of ChatGPT, Claude, and others.
PPO was published in 2017 by John Schulman of OpenAI. Unlike the earlier TRPO (Trust Region Policy Optimization), which required complex second-order optimization, PPO achieves similar performance with a simple clipping technique, making it easier to implement and scale.
The core principle is to limit the size of the policy update. If the ratio between the new policy and the old policy goes outside a certain range (e.g., 0.8~1.2), it is clipped, which prevents too large a change from occurring at once. As a result, training proceeds stably.
In RLHF, PPO trains the LLM to maximize the score of the reward model. It is a key step that makes the model's output more useful and safe through a reward signal that reflects human preferences.
# RLHFμμ PPO νμ΅ μμ (trl λΌμ΄λΈλ¬λ¦¬ μ¬μ©)
from trl import PPOTrainer, PPOConfig, AutoModelForCausalLMWithValueHead
from transformers import AutoTokenizer
import torch
# 1. λͺ¨λΈ λ° ν ν¬λμ΄μ λ‘λ
model_name = "gpt2" # μ€μ λ‘λ λ ν° λͺ¨λΈ μ¬μ©
model = AutoModelForCausalLMWithValueHead.from_pretrained(model_name)
ref_model = AutoModelForCausalLMWithValueHead.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
# 2. PPO μ€μ
ppo_config = PPOConfig(
model_name=model_name,
learning_rate=1e-5,
batch_size=4,
mini_batch_size=2,
ppo_epochs=4, # PPO μ
λ°μ΄νΈ λ°λ³΅ νμ
clip_range=0.2, # ν΄λ¦¬ν λ²μ (ν΅μ¬ νμ΄νΌνλΌλ―Έν°)
target_kl=0.01, # KL λ°μ° μ ν
)
# 3. PPO νΈλ μ΄λ μ΄κΈ°ν
ppo_trainer = PPOTrainer(
config=ppo_config,
model=model,
ref_model=ref_model,
tokenizer=tokenizer,
)
# 4. νμ΅ λ£¨ν (κ°μν)
def compute_reward(response_text):
"""보μ λͺ¨λΈμμ μ μ κ³μ° (μ€μ λ‘λ λ³λ Reward Model μ¬μ©)"""
# μμ: μλ΅ κΈΈμ΄μ λ°λ₯Έ λ¨μ 보μ
return len(response_text) * 0.01
# 5. PPO νμ΅ μ€ν
query_tensors = tokenizer.encode("AIμ μ₯μ μ", return_tensors="pt")
response_tensors = model.generate(query_tensors, max_new_tokens=50)
# 보μ κ³μ°
response_text = tokenizer.decode(response_tensors[0])
reward = torch.tensor([compute_reward(response_text)])
# PPO μ
λ°μ΄νΈ
stats = ppo_trainer.step([query_tensors[0]], [response_tensors[0]], [reward])
print(f"PPO Loss: {stats['ppo/loss/total']:.4f}")
"In the RLHF pipeline, we are optimizing the reward model score with PPO after SFT. We set clip_range=0.2 to control the policy so that it does not change abruptly."
"PPO is a type of policy gradient method that limits the size of the policy update through clipping. Unlike TRPO, it uses only first-order optimization, so it is simple to implement and scales well."
"Recently, DPO (Direct Preference Optimization) sometimes replaces PPO. DPO simplifies the RLHF pipeline by training directly on preference data without a reward model."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
If PPO is applied without a KL divergence constraint, the model may fall into reward hacking and generate strange outputs.
If clip_range is too large (e.g., 0.5), the policy changes abruptly and training becomes unstable. A range of 0.1~0.3 is usually used.
Keep a reference model (ref_model) to monitor the KL divergence, and start PPO training only after sufficiently verifying the quality of the reward model.