Skip to content

docs: document token_level_loss in the GRPO guide - #3656

Merged
terrykong merged 3 commits into
mainfrom
terryk/docs-token-level-loss
Aug 18, 2026
Merged

terrykong merged 3 commits into
mainfrom
terryk/docs-token-level-loss

Conversation

@terrykong

Copy link
Copy Markdown
Collaborator

What

Closes #1274.

token_level_loss had no explanation anywhere in the docs — it appeared only as a bare line in config blocks (dapo.md, cispo.md, ppo.md, prorlv2.md) and in ~30 example YAMLs. This adds a Loss Normalization section to docs/guides/grpo.md, placed under ## Loss before the "Improvements to the GRPO Loss Formulation" subsections — it is not an optional stability add-on like those, it is a property of the baseline loss that the existing $E_t[\cdot]$ notation leaves unspecified.

Contents of the new section

  • What it controls. The reduction applied to the per-token loss, not the per-token quantity itself. The same switch also normalizes the KL penalty, and both denominators are global-batch counts rather than per-microbatch.
  • The two reductions, written out once, plus a worked example: a 10-token and a 1000-token response give a 99%/1% split under true and 50%/50% under false, so short-response tokens carry ~100× the per-token weight under sequence-level.
  • Objectives table. Full objective for GRPO/PPO/DAPO, CISPO, GSPO and REINFORCE/RLOO under both settings. The guide never wrote these objectives out anywhere, so the table does double duty. CISPO and GSPO each have one cell marked unavailable — those combinations are rejected by assertions in ClippedPGLossFn.__init__.
  • Losses without this parameter. token_level_loss is a field of ClippedPGLossConfig only; SFT, DPO/RM, distillation, the PPO critic, cross-tokenizer distillation and Eagle3 draft training all fix loss_type as a class attribute. Setting it in an SFT config does nothing, which is worth saying out loud.
  • Metric normalization. Which reported metrics follow the flag (loss, kl_penalty), which are always token-normalized, which key off a different parameter (sampling_importance_ratio, is_oob_ratio), and which are not normalized at all. Relevant when comparing runs across the setting.
  • Choosing a setting. DAPO §3.3's argument for token-level in long-CoT, quoted rather than paraphrased, and explicitly framed as one reported result rather than a rule — the original GRPO paper formulates the loss at the sequence level, and this repo has no benchmark comparing the two.

Also in this PR

  • Docstring fix (loss_functions.py:206): the REINFORCE/RLOO objective was written as $E_t[\pi_\theta(a_t|s_t) \cdot A_t]$, but :541 sets ratios = curr_logprobs — the raw log-probability, not exp(). The new table is derived from the code, so leaving the docstring disagreeing with it would have been confusing. Corrected to $\log \pi_\theta$.
  • Cross-links from dapo.md:86 and cispo.md:56 to the new section. Those are the two pages where a reader meets this parameter first and currently gets no explanation — the same gap the issue reports.

No changes to ppo.md (it already defers wholesale to the GRPO guide) or docs/index.md (no new file; grpo.md is already in the toctree).

Verification

The docs build could not be run locally — uv.lock only supports Linux and this is macOS — so the MyST-level checks were done directly:

  • Heading anchors confirmed against mdit_py_plugins.anchors.slugify: Loss Normalization (\token_level_loss`)→loss-normalization-token_level_loss, matching the links from dapo.mdandcispo.md. myst_heading_anchors = 5inconf.py:68covers the###/####` levels used.
  • Parsed the file with markdown-it-py + dollarmath: all 3 tables parse with the intended cell counts, and all inline/display math tokenizes. Table cells use \lvert/\rvert rather than bare |, which would otherwise split the rows.
  • Every formula in the objectives table was read back off the implementation rather than from the papers — GSPO's $s_i$ against :528-534, CISPO against :545, the clipped surrogate against :547-551, REINFORCE against :541-542.
  • Every row of the "losses without this parameter" table was checked against its actual consumer (sft.py:229, dpo.py:284, rm.py:263, distillation.py:641, ppo.py:377, xtoken_off_policy_distillation.py:479, wrapper.py:251).
  • The DAPO quotes were pulled from the paper, not recalled.
  • ruff format --check and ruff check pass on the modified Python file.

The permalink to metric_normalizations is SHA-pinned rather than blob/main, since a line-anchored link to main breaks silently as code shifts.

@terrykong
terrykong requested review from a team as code owners August 14, 2026 21:35
@copy-pr-bot

copy-pr-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Aug 14, 2026
Comment thread docs/guides/grpo.md Resolved
Comment thread docs/guides/grpo.md Resolved
@terrykong terrykong added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Aug 17, 2026
@terrykong

Copy link
Copy Markdown
Collaborator Author

/ok to test 8a6f950

@terrykong
terrykong enabled auto-merge (squash) August 17, 2026 20:40
Adds a Loss Normalization section to docs/guides/grpo.md covering what
token_level_loss controls: the reduction applied to the per-token loss
(and the KL penalty), not the per-token quantity itself.

The section includes the two reduction formulas, a worked example
contrasting a 10-token and a 1000-token response, a table writing out the
full objective for GRPO/PPO/DAPO, CISPO, GSPO and REINFORCE/RLOO under
both settings, a table of the losses that fix their normalization as a
class attribute, and a table of which reported metrics follow the flag.

Also fixes the REINFORCE/RLOO formula in the ClippedPGLossFn docstring,
which wrote the objective in terms of pi_theta where the code uses
log pi_theta, and links to the new section from the dapo and cispo
guides, where the parameter was previously mentioned without
explanation.

Closes #1274

Signed-off-by: Terry Kong <[email protected]>
- Separate the two epsilon symbols with a comma instead of a slash.
  GitHub's math renderer does not close an inline span when the next
  character is a slash immediately followed by another '$', so
  $...$/$...$ rendered the second symbol as literal text.

- Name $\ell_{i,t}$ the per-token objective rather than a per-token
  quantity, and drop the leading minus from the two reductions and the
  objective table so the sign convention matches the L(theta) formula
  earlier in the same section, which is written without one.

Signed-off-by: Terry Kong <[email protected]>
The link was pinned to e983576, a commit this branch was never based
on. That SHA is in main's history and the line range happens to be
correct there, but pinning to the branch's actual base is verifiable.
Re-pinned to 4a1454b; confirmed L335-L371 is the metric_normalizations
table at that commit and that the ref resolves.

Signed-off-by: Terry Kong <[email protected]>
@terrykong
terrykong force-pushed the terryk/docs-token-level-loss branch from 8a6f950 to 44cdeae Compare August 17, 2026 21:54
@terrykong

Copy link
Copy Markdown
Collaborator Author

/ok to test 44cdeae

@terrykong
terrykong merged commit 42f4491 into main Aug 18, 2026
83 checks passed
@terrykong
terrykong deleted the terryk/docs-token-level-loss branch August 18, 2026 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add token_level_loss in grpo guide

2 participants