nemo_rl.algorithms.loss.score_centering#

Score-centering corrections shared by policy-gradient objectives.

Module Contents#

Functions#

compute_score_centering_term

Per-token sum_v sg(c_v) * log p_v whose gradient is the sampler-expected score.

API#

nemo_rl.algorithms.loss.score_centering.compute_score_centering_term(
policy_logprobs_at_sampler_topk: torch.Tensor,
*,
sampler_topk_logprobs: torch.Tensor,
importance_sampling_mode: str,
is_bounds: tuple[float, float] | None,
token_mask: torch.Tensor,
sampler_replay: bool,
adaptive_ppo_clip_advantages: torch.Tensor | None = None,
adaptive_ppo_clip_outer_bound: float | None = None,
) → tuple[torch.Tensor, torch.Tensor]#

Per-token sum_v sg(c_v) * log p_v whose gradient is the sampler-expected score.

Score centering (arXiv:2609.20807, Eq. 14) with the paper’s top-k estimator: the sampler’s head q is exact and its tail is modeled as rho * p with rho = sampler tail mass / trainer tail mass. Without importance sampling the centering mass is q on the head and rho * p on the tail. With token-level TIS, w = clamp(p / q, lo, hi), it is q * w = clamp(p, lo * q, hi * q) on the head and alpha * p on the tail with alpha = clamp(1, lo * rho, hi * rho). Since sum_v p_v grad log p_v is zero, the tail contributes -alpha * p_v on the head ids, so the coefficient is c_v = mass_v - alpha * p_v. Untruncated IS makes this coefficient zero. ICEPOP instead retains p_v only where p_v/q_v is in bounds, and retains the unit tail coefficient only where 1/rho is in bounds. adaptive-ppo-clip follows the same rule with the bounds its filter picks by the sign of the position’s advantage (adaptive_ppo_clip_kept_mask). These rules apply to every head/tail candidate independently of the sampled token’s weight.

Under sampler replay the trainer renormalizes over the sampler’s support S_q, which the head holds: slots the sampler filtered out are -inf for both and get no coefficient, and the head holds all of p, so there is no tail and rho is zero. (miles uses rho = 1; the two differ by a multiple of sum_{S_q} p grad log p = 0.)

This utility leaves the base actor objective, advantage weighting, masking, and normalization to the caller, so PPO and GRPO can share the estimator; adaptive-ppo-clip reads only the advantage’s sign. Adding this term does not center an arbitrary clipped PPO gradient exactly.

Parameters:
  • policy_logprobs_at_sampler_topk – Trainer log-probs at the sampler’s top-k ids, [B, S, k], carrying the gradient; -inf where the sampler filtered the id out.

  • sampler_topk_logprobs – The sampler’s log-probs at those ids, [B, S, k]; -inf where its filters removed the id.

  • importance_sampling_mode – off, is, tis, icepop or adaptive-ppo-clip.

  • is_bounds – (lo, hi) for TIS/ICEPOP; for adaptive-ppo-clip, the negative-advantage lower bound and the positive-advantage upper bound including the controller’s offset k. Otherwise None.

  • token_mask – Active token/sample positions, before IS filtering. A zero sampled-token ICEPOP or adaptive-ppo-clip weight must not mask the correction.

  • sampler_replay – Whether the trainer log-probs are renormalized over the sampler’s support, so the head holds all of p and rho is zero. Otherwise rho models the sampler’s tail on the trainer’s tail, however small that tail is.

  • adaptive_ppo_clip_advantages – Per-token advantages [B, S], whose sign selects adaptive-ppo-clip’s bound. Read by that mode only.

  • adaptive_ppo_clip_outer_bound – adaptive-ppo-clip’s upper bound for either advantage sign. Read by that mode only.

Returns:

The per-token term [B, S] and the sampler mass the head covers [B, S].