nemo_rl.algorithms.loss.score_centering#
Score-centering corrections shared by policy-gradient objectives.
Module Contents#
Functions#
Per-token |
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,
Per-token
sum_v sg(c_v) * log p_vwhose gradient is the sampler-expected score.Score centering (arXiv:2609.20807, Eq. 14) with the paper’s top-k estimator: the sampler’s head
qis exact and its tail is modeled asrho * pwithrho= sampler tail mass / trainer tail mass. Without importance sampling the centering mass isqon the head andrho * pon the tail. With token-level TIS,w = clamp(p / q, lo, hi), it isq * w = clamp(p, lo * q, hi * q)on the head andalpha * pon the tail withalpha = clamp(1, lo * rho, hi * rho). Sincesum_v p_v grad log p_vis zero, the tail contributes-alpha * p_von the head ids, so the coefficient isc_v = mass_v - alpha * p_v. Untruncated IS makes this coefficient zero. ICEPOP instead retainsp_vonly wherep_v/q_vis in bounds, and retains the unit tail coefficient only where1/rhois 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
-inffor both and get no coefficient, and the head holds all of p, so there is no tail andrhois zero. (miles usesrho = 1; the two differ by a multiple ofsum_{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;-infwhere the sampler filtered the id out.sampler_topk_logprobs – The sampler’s log-probs at those ids,
[B, S, k];-infwhere its filters removed the id.importance_sampling_mode –
off,is,tis,icepoporadaptive-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 offsetk. OtherwiseNone.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
rhois zero. Otherwiserhomodels 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].