Skip to content

Normalize the in-loss survivor denominator once instead of rescaling gradients by G/K #4242

Open

Description

@yuki-97

Today

With loss_fn.seq_logprob_error_in_loss (#4171), each microbatch normalizes by the unfiltered global token count G, because the survivor count K is not known until every microbatch has run. The worker then multiplies gradients by G/K before optimizer.step():

# nemo_rl/models/policy/workers/megatron_policy_worker.py:1036-1042
token_factor = float((global_valid_toks / token_denominator).item())
...
self.model.scale_gradients(token_factor)

Proposal

Have the loss emit raw sums and divide by K once after the reduction. Same result, without threading global_valid_toks into the loss as a placeholder and without a second rescale pass (rescale_loss_metrics) that has to stay in sync with the gradient one.

What it needs

Convergence runs, not just an algebraic argument. The two forms are equal on paper, but dropping the per-microbatch division changes what the optimizer sees along the way, so the dynamics have to be compared end to end.

The current shape also buys a cross-check worth giving up deliberately: both paths divide by global_valid_toks, so in-loss can be diff-tested against separate-forward by swapping a single input. If the separate-forward path is retired, that argument goes away on its own.

Scope

  • Loss: emit unnormalized sums where global_normalization_factor is used today.
  • Worker: divide by the reduced survivor counts once, after the DP all-reduce and PP broadcast; drop the G/K rescale and its rescale_loss_metrics pair.
  • Keep the empty-survivor guard at megatron_policy_worker.py:1020.

From review of #4171: #4171 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

accuracyenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions