Skip to content

fix(ppo): preserve raw KL so rollout/kl logging is correct - #2114

Merged
zhuzilin merged 1 commit into
THUDM:mainfrom
EazyReal:fix/ppo-kl-inplace-metric
Aug 21, 2026
Merged

fix(ppo): preserve raw KL so rollout/kl logging is correct#2114
zhuzilin merged 1 commit into
THUDM:mainfrom
EazyReal:fix/ppo-kl-inplace-metric

Conversation

@EazyReal

@EazyReal EazyReal commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Problem

compute_advantages_and_returns writes per-token KL into rollout_data["kl"], which is what gets logged as the KL metric.

The PPO branch then did:

for reward, k in zip(old_rewards, kl, strict=False):
    k *= kl_coef
    if cp_rank == 0:
        k[-1] += reward
    rewards.append(k)

k is the same tensor already stored in rollout_data["kl"], so logging sees kl_coef-scaled KL with the scalar reward added on the last token. GRPO / GSPO / CISPO / R++ do not mutate kl this way.

Change

Build token-level rewards out of place:

token_level_rewards = per_token_kl * kl_coef
if cp_rank == 0:
    token_level_rewards[-1] += reward
rewards.append(token_level_rewards)

PPO advantages/returns still use the scaled+reward tensor. rollout_data["kl"] stays the raw metric. Other estimators are untouched.

Test plan

  • tests/test_ppo_kl_metric.py — after PPO, rollout_data["kl"] still matches compute_approx_kl (registered in pr-test.yml.j2)

@EazyReal
EazyReal force-pushed the fix/ppo-kl-inplace-metric branch 3 times, most recently from cbf085d to 0a034ba Compare June 24, 2026 04:17
@EazyReal EazyReal changed the title fix(ppo): stop corrupting the logged rollout/kl metric fix(ppo): preserve raw KL for rollout/kl logging Jun 25, 2026
@EazyReal EazyReal changed the title fix(ppo): preserve raw KL for rollout/kl logging fix(ppo): preserve raw KL so rollout/kl logging is correct Jun 25, 2026
@EazyReal

Copy link
Copy Markdown
Contributor Author

@zhuzilin could you review this now-cleaned version? PPO reward shaping was mutating the raw KL tensor before metrics, so rollout/kl logging could report shaped rewards instead of KL. The fix keeps raw KL separate and mirrors the local-k pattern used by the reinforce loss helpers.

@EazyReal
EazyReal force-pushed the fix/ppo-kl-inplace-metric branch from 9369513 to f343036 Compare June 30, 2026 08:58
EazyReal added a commit to EazyReal/slime that referenced this pull request Jul 7, 2026
@EazyReal

EazyReal commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

@zhuzilin refreshed on latest main and checks are green. This keeps raw KL separate from token-level rewards so rollout/kl logging stays correct while PPO still applies the KL penalty. Could you review when you have bandwidth?

@EazyReal
EazyReal force-pushed the fix/ppo-kl-inplace-metric branch from c271685 to 1eae53b Compare August 18, 2026 21:59
k *= kl_coef wrote through rollout_data['kl'], so logged KL was the
scaled reward term. Other estimators are unchanged.
@EazyReal
EazyReal force-pushed the fix/ppo-kl-inplace-metric branch from 1eae53b to 3e4378a Compare August 21, 2026 00:50
@zhuzilin
zhuzilin merged commit 045310b into THUDM:main Aug 21, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants