Teo Wiki RL NOTE
Article References RL note

DeepSeekMath, PPO, and GRPO

핵심: PPO/GRPO는 좋은 답변 확률을 올리고 나쁜 답변 확률을 내리되, rollout을 만든 old policy에서 너무 멀어지지 않게 probability ratio와 clipping으로 제어한다.

Metadata

Paper
DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models
Authors
Zhihong Shao, Peiyi Wang, Qihao Zhu, Runxin Xu, Junxiao Song, Xiao Bi, Haowei Zhang, Mingchuan Zhang, Y. K. Li, Y. Wu, Daya Guo
arXiv
2402.03300 · 2024
Source material
Teo's uploaded Korean study note: deepseekmath_ppo_grpo_notes.md
Category
RL for LLMs / Post-training · Mathematical reasoning · GRPO
Keywords
DeepSeekMath, CoT, PoT, PPO, GRPO, reward model, value model, reference model, old policy, probability ratio, clipping, KL penalty

DeepSeekMath pipeline

DeepSeekMath에서 CoTPoT는 새로운 RL 알고리즘이라기보다, SFT 단계에서 모델이 따라 배울 출력 형식에 가깝다.

DeepSeek-Coder-Base-v1.5 7B
→ math-heavy continual pretraining
→ DeepSeekMath-Base 7B
→ CoT / PoT / tool-integrated reasoning SFT
→ DeepSeekMath-Instruct 7B
→ GRPO reinforcement learning
→ DeepSeekMath-RL 7B
  • CoT: 자연어 단계별 풀이를 target sequence로 둔다.
  • PoT: Python 등 프로그램 형태의 풀이를 target sequence로 둔다.
  • Tool-integrated reasoning: 자연어 reasoning과 tool 실행을 섞은 풀이를 target으로 둔다.

SFT objective는 여전히 next-token prediction이다. 즉 모델에게 “문제를 보면 이런 형식의 풀이 과정을 생성하라”를 imitation learning으로 가르친다.

LLM RL mapping

State

Prompt plus generated prefix

LLM에서 state는 물리적 환경 상태가 아니라 s_t = (x, y_<t), 즉 문제 prompt와 지금까지 생성한 prefix다.

Action

Next token

Action은 다음 token 선택이다. Policy model은 πθ(y_t | x, y_<t) 확률분포를 출력한다.

Reward / return

Usually sequence-level and delayed

수학 문제에서는 완성된 response 전체에 대해 정답 checker나 reward model이 점수를 주는 경우가 많다. 그래서 어떤 token이 최종 실패를 만들었는지 credit assignment가 어렵다.

Reward, value, and reference models

Model Role LLM example
Policy model Generates the answer 수학 문제 풀이를 작성하는 모델
Reward model / reward function Scores a completed response 정답이면 1, 틀리면 0; 또는 learned RM 점수
Value model Predicts future reward from a prefix 현재 풀이 prefix가 최종적으로 좋은 점수를 받을 가능성 예측
Reference model Frozen anchor for KL penalty RL 중인 policy가 SFT 모델에서 너무 멀어지지 않게 잡아주는 기준

중요한 구분: GRPO가 없애는 것은 reward signal이 아니라 value model / critic이다. 수학에서는 neural reward model 대신 rule-based answer checker가 reward function 역할을 할 수 있다.

PPO vs GRPO

PPO와 GRPO의 차이는 advantage를 어떻게 baseline과 비교하느냐에서 가장 잘 보인다.

PPO

“이 token은 현재 prefix의 기대값보다 좋은가?”

PPO는 A_t = G_t - V(s_t)를 사용한다. 현재 prefix의 기대 reward를 value model이 예측하고, 실제 return이 그 기대보다 좋으면 해당 token/action 확률을 올린다.

GRPO

“이 response는 같은 prompt의 다른 response보다 좋은가?”

GRPO는 같은 prompt에서 여러 response를 샘플링한 뒤, group reward의 mean/std로 상대 advantage를 만든다. Baseline이 value model이 아니라 group mean이므로 critic이 필요 없다.

GRPO advantage for response i:
A_i = (r_i - mean(r_1, ..., r_G)) / std(r_1, ..., r_G)

따라서 PPO는 actor-critic에 가깝고, GRPO는 같은 문제에서 나온 여러 답변 간 상대 비교를 이용해 critic 없이 policy를 업데이트한다.

Probability ratio: current / old

PPO/GRPO objective의 ratio는 current policy probability / old policy probability다. Reference model이 분모가 아니다.

r_t(θ) = π_current(a_t | s_t) / π_old(a_t | s_t)
  • old policy: 이번 rollout batch를 실제로 생성한 policy snapshot. Ratio의 denominator다.
  • current policy: 지금 optimizer step으로 업데이트되는 모델. Ratio의 numerator다.
  • reference policy: RL 시작 전 SFT 모델. KL penalty의 기준이다.

Rollout 직후에는 current와 old가 같아서 ratio가 1이다. 하지만 같은 rollout batch로 여러 optimizer step을 밟으면 current가 바뀌고 old는 고정되므로 ratio가 1에서 멀어진다.

Reference를 ratio 분모로 쓰면, RL이 진행되어 이미 SFT 모델보다 좋아진 policy는 새 batch 업데이트 전부터 ratio가 크게 튀어 clipping이 잘못 걸릴 수 있다. 그래서 current / old는 “이번 batch 기준 변화량”을 보고, current vs reference는 KL penalty로 따로 제어한다.

Clipped objective intuition

L_clip(θ) = E[min(r_t(θ) A_t,
                  clip(r_t(θ), 1-ε, 1+ε) A_t)]
  • Positive advantage: 좋은 token/response의 확률을 올린다. 단, old 대비 너무 많이 올리면 추가 이득을 막는다.
  • Negative advantage: 나쁜 token/response의 확률을 내린다. 단, old 대비 너무 많이 내리면 추가 이득을 막는다.
  • Why multiple steps: rollout 생성과 reward 채점은 비싸기 때문에 같은 batch를 여러 번 재사용한다. Ratio와 clipping은 이 재사용 중 policy drift를 제한한다.

한 줄로 말하면, 좋은 답변은 강화하고 나쁜 답변은 약화하되, 한 번 만든 rollout batch를 과하게 쥐어짜서 policy가 급격히 움직이는 것을 막는 장치다.

Toy code mapping

CartPole용 PPO/GRPO toy code를 볼 때는 DeepSeekMath의 LLM GRPO와 완전히 같다고 보기보다, 어떤 부품이 대응되는지 확인하는 학습 예제로 보면 좋다.

  • PPO toy code: PolicyAndValueNetwork가 actor와 critic을 함께 가진다. Trajectory에는 state, action, reward, value, old probability가 저장되고, advantage 계산에 value가 들어간다.
  • GRPO-like toy code: value head 없이 PolicyNetwork만 둔다. Group return이나 group weight로 policy를 업데이트하는 구조를 통해 “critic 없이 상대 비교를 쓴다”는 직관을 보여준다.
  • DeepSeekMath GRPO 핵심: 같은 math prompt에서 여러 response를 샘플링하고, reward의 group mean/std로 advantage를 만든다.