πŸ€– AI/ML

PPO

Proximal Policy Optimization

A reinforcement learning algorithm for stable policy updates. Mainly used in RLHF.

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

# 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}")

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"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."
πŸ’¬ In an Interview
"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."
πŸ’¬ In a Technical Discussion
"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

⚠️ Common Mistakes & Cautions

❌
Training without a KL penalty

If PPO is applied without a KL divergence constraint, the model may fall into reward hacking and generate strange outputs.

❌
Setting clip_range too large

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.

βœ…
The right way

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.

πŸ”— Related Terms

πŸ“š Learn More