Skip to content

[rl] Validation samples at temperature 1; optional n samples per prompt with best_of_n - #6266

Merged
felipemello1 merged 3 commits into
pytorch:mainfrom
felipemello1:rl/validation-temperature-best-of-n
Nov 20, 2026
Merged

felipemello1 merged 3 commits into
pytorch:mainfrom
felipemello1:rl/validation-temperature-best-of-n

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

changes validation temperate default to 1
exposes the arg
enables best-of-n

Agent details

TLDR: Validation now samples at temperature 1.0 instead of greedily, still with 1 rollout per prompt by default. 6 of the 8 RL training libraries we surveyed validate by sampling by default. num_samples_per_prompt runs more rollouts per prompt. With n > 1, validation_reward/best_of_n logs the best reward per prompt, which is pass@n when rewards are 0/1.

main runs one greedy rollout per validation prompt, and this PR samples num_samples_per_prompt rollouts at temperature 1.0, so the unit test's rewards [[1, 0], [0, 0]] log validation_reward/_mean 0.25 and best_of_2 0.5.

Problem

  1. Controller.validate runs each held-out prompt once, greedily (temperature=0.0).
  2. Training and inference sample from the policy, and most of our recipes train at temperature 1.0. Greedy decoding scores a different policy: the argmax.
  3. Of the 8 RL training libraries we surveyed, 6 validate by sampling at the training temperature by default. The other 2 default to greedy, but their main math recipes switch to temperature 1.0.
  4. Validation had no way to average several samples per prompt. The surveyed libraries support it, and their AIME-style recipes use 16-32 samples.

Solution

  1. ValidationConfig gains temperature: float = 1.0 and num_samples_per_prompt: int = 1.
  2. Each validation prompt runs as one rollout group of n. Group ids and release_groups don't change, and the best-of-1 TODO goes away.
  3. validation_reward keeps its name and meaning: the mean over all scored rollouts.
  4. When n > 1, validation_reward/best_of_{n} logs each prompt's best scored reward, averaged over prompts; unscored rollouts (reward None) are skipped, as in validation_reward, and a prompt with none scored is left out. It needs no pass threshold (alphabet_sort's reward is graded), and it equals pass@n for 0/1 rewards.
  5. max_num_seqs is unchanged: main sizes it from training rollouts only. With n > 1, a validation sends prompts x n requests, and the generators serve validation requests first (vLLM priority -1).
  6. Config checks raise ValueError on temperature < 0, on num_samples_per_prompt < 1, and on greedy with n > 1, which would run n identical rollouts.
  7. Unchanged: top_p stays 1.0 (SamplingConfig requires it), and validation keeps main's schedule (ValidationConfig.freq, beside training).

Recipes
No recipe sets validation sampling, so they all move from greedy to temperature 1.0 with the same number of rollouts. One sampled rollout per prompt is noisier than greedy, and validation_reward curves are not comparable with earlier greedy runs.

Results
2-GPU smoke, H100: Qwen3-0.6B on alphabet_sort, 2 training steps, 8 validation prompts x 4 rollouts. It exercises the n > 1 path.

                                  pre-training   post-training
validation_reward/_mean           0.187          0.181    (32 rollouts per pass)
validation_reward/best_of_4/mean  0.263          0.285

You may ask:

  • Why not inherit the training temperature? A fixed validation temperature keeps runs comparable when a recipe changes the training temperature (alphabet_sort trains at 0.8).
  • Why the max reward per prompt? That is what best-of-n means: the best of the prompt's n tries. "Pass" would need a threshold, and our rewards can be graded. For 0/1 rewards the max is 1 when any try passed, so it equals pass@n.
  • Why default to 1 sample? It costs the same number of rollouts as greedy validation did; recipes opt into more.
  • How do I get greedy back? ValidationConfig(temperature=0.0).

NOTE: The H100 smoke above ran on an earlier revision that defaulted to n=4. The GB300 smoke in the test plan ran on 7c4c678, this commit before a clean rebase onto main.

Test plan

  • pytest tests/rl/unit_tests/cpu/test_async_controller.py -k validat: 15 passed.
  • pytest tests/rl/unit_tests/cpu: 689 passed, 7 skipped (main b1c259cd3: 685 passed, 7 skipped).
  • New tests: test_validate_rolls_out_each_prompt_n_times (n=1, n=2, and n=2 with unscored rollouts) and test_validation_config_rejects_bad_sampling.
  • test_validation_renames_turn_metrics_and_uses_fresh_group_ids passes num_samples_per_prompt=1: validate() now hands it to _collect_validation_rollouts next to sampling.
  • 2-GPU smoke on 7c4c678: Qwen3-0.6B on alphabet_sort, 2 GB300 GPUs, 2 training steps, ValidationConfig(steps=8, num_samples_per_prompt=4, validate_after_training=True). rc=0, validation/group_failures 0, 32 rollouts per validation. validation_reward/best_of_4/mean 0.24 vs validation_reward/_mean 0.12 at step 0, and 0.23 vs 0.14 after training.
  • pre-commit run on the changed files.

…pt with best_of_n

ValidationConfig gains temperature (default 1.0) and num_samples_per_prompt (default 1). Each
validation prompt runs as one group of n rollouts. validation_reward keeps its meaning (the mean
over all scored rollouts). With n > 1, validation_reward/best_of_{n} logs each prompt's best scored
reward (pass@n for 0/1 rewards); a prompt with no scored rollout is left out. Config checks reject
temperature < 0, num_samples_per_prompt < 1, and greedy with n > 1.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 2m 36s —— View job


Approve -- please address the items before merging (missing human summary, stale "greedy" docstring in slake.py): review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the items below before merging. The change is small, fits how validation already works (one rollout group per prompt, fresh negative group ids, release_groups), and the new metric is correct. What's left: a missing human summary and docs that still say validation is greedy. A maintainer still needs to review, because this touches the RL controller.

What this PR does. Validation used to decode greedily with 1 rollout per prompt. Now it samples at ValidationConfig.temperature (default 1.0) and runs num_samples_per_prompt rollouts per prompt (default 1). With n > 1 it also logs validation_reward/best_of_{n}: each prompt's best scored reward, averaged over prompts.

Example. 2 prompts, ValidationConfig(num_samples_per_prompt=2), training sampling T=0.8, seed=7:

main:  run_group_rollouts(group_size=1, sampling=T=0.0)       -> validation_reward/_mean over 2 rollouts
PR:    run_group_rollouts(group_size=2, sampling=T=1.0, seed=7)
       rewards [[1, 0], [0, 0]] -> validation_reward/_mean 0.25, validation_reward/best_of_2/mean 0.5
       rewards [[None, 1], [None, None]] -> _mean 1.0, best_of_2 1.0 (the second prompt is left out)

The rollouter seeds siblings with seed + rollout_id (rollouter.py:456, verifiers/rollouter.py:268), so n > 1 gives n distinct samples when debug.seed is set.

Must fix

  1. PR description: the "Summary (human-written)" section is empty. The author needs to fill it in (see "AI-assisted contributions" in CONTRIBUTING.md).
  2. torchtitan_recipes/rl/slake.py:112: the docstring still says "Validation: greedy answers on the English test questions". After this PR, the slake recipe samples at T=1.0. Either set ValidationConfig(..., temperature=0.0) there, if exact-match short answers should stay greedy, or update the docstring.

Should consider

  • torchtitan/rl/controller/controller.py:903-905: pass num_samples_per_prompt into _collect_validation_rollouts as an argument, like sampling. Then the existing test doesn't need a stubbed controller.config (details inline).
  • torchtitan/rl/examples/search_r1/README.md:81: the results say the curves are "greedy decoding". That is still true of those snapshots, but new runs will validate at T=1.0 and won't be comparable with them. Add one clause saying so, or pin the search_r1 recipe to temperature=0.0.

Questions

  • Item 3 of the description cites libraries that validate "at the training temperature". The PR instead fixes validation at 1.0, which is the training temperature for every recipe except alphabet_sort (0.8). The "You may ask" answer (comparability across runs) is a reasonable call. A maintainer should confirm they prefer a fixed default over inheriting generator.sampling.temperature.
Details
  • top_p=1.0 is no longer forced. That's fine: SamplingConfig.__post_init__ already rejects top_p < 1.0 (generator/types.py:44).
  • Unscored rollouts: best_of_n filters reward is None the same way compute_rollout_metrics does for validation_reward (metrics.py:262), so the two metrics count the same rollouts.
  • Every in-repo ValidationConfig(...) caller (recipes, torchtitan_recipes/tests/rl, tests/rl) uses defaults for the new fields. The loss-guard configs use steps=0, so no golden file depends on validation numerics.
  • The extra load with n > 1 runs at vLLM priority -1, as the description says (generator/types.py:60). The existing TODO at controller.py:991 about reading every prompt up front already covers the case where prompts x n gets large (e.g. steps=-1 on slake or geometry3k).

Not verified: the GPU smoke results in the description.

Comment thread torchtitan/rl/controller/controller.py Outdated
Comment on lines +903 to +905
num_samples_per_prompt = (
self.config.async_loop.validation.num_samples_per_prompt
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should consider: validate() works out the validation sampling and passes it in as sampling=, but _collect_validation_rollouts reads num_samples_per_prompt from self.config itself. That split is the only reason test_validation_renames_turn_metrics_and_uses_fresh_group_ids now has to stub controller.config with a ValidationConfig(). Pass it in as a keyword argument next to sampling (num_samples_per_prompt=validation.num_samples_per_prompt in validate()). Then the helper gets everything from its caller, and the existing test needs no change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reply from claude: applied in 7df4fb8. validate() reads the ValidationConfig once and passes num_samples_per_prompt next to sampling; the old test passes num_samples_per_prompt=1 and no longer stubs controller.config.

…ollouts; docs no longer say greedy

validate() reads the ValidationConfig once and passes the sampling and the sample count, so
the helper takes everything from its caller. The SLAKE recipe docstring and the search_r1
README say validation now samples at temperature 1.0.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: 7df4fb8 addresses the review: num_samples_per_prompt is passed into _collect_validation_rollouts; the SLAKE docstring and the search_r1 README no longer call validation greedy (the README says its curves are greedy snapshots). Fixed vs inherited temperature is a maintainer call; the description's "You may ask" gives the reason for a fixed 1.0. @claude please review

@claude

claude Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 37s —— View job


Approve -- please address the item below before merging (the human-written summary is still empty; all earlier code and doc items are fixed): review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the item below before merging. 7df4fb8 fixes the code and doc items from the last review. The new commit adds no new problems. The one open item is the empty human-written summary. A maintainer still needs to review, because this touches the RL controller.

What this PR does. Validation used to decode greedily, with 1 rollout per prompt. Now it samples at ValidationConfig.temperature (default 1.0) and runs num_samples_per_prompt rollouts per prompt (default 1). With n > 1 it also logs validation_reward/best_of_{n}.

Example.

ValidationConfig(num_samples_per_prompt=2), 2 prompts, training sampling T=0.8 seed=7
main: run_group_rollouts(group_size=1, sampling=T=0.0)
PR:   run_group_rollouts(group_size=2, sampling=T=1.0, seed=7)
      rewards [[1, 0], [0, 0]] -> validation_reward/_mean 0.25, validation_reward/best_of_2/mean 0.5

Status of earlier items

  • Resolved: torchtitan_recipes/rl/slake.py:112. The docstring now says one sampled answer at T=1.0.
  • Resolved: controller.py. validate() passes num_samples_per_prompt next to sampling, so the old test no longer stubs controller.config.
  • Resolved: search_r1/README.md:83-85. It says the curves are greedy snapshots and newer runs aren't comparable. The terminal_bench README is updated too. No other in-repo doc still calls validation greedy.

Must fix

  1. PR description: "Summary (human-written)" is still empty. The author has to fill it in (see "AI-assisted contributions" in CONTRIBUTING.md).

Questions

  • Still open for a maintainer: should validation use a fixed default of 1.0, or inherit generator.sampling.temperature? The "You may ask" section explains why the PR picks a fixed value (runs stay comparable). Today the only recipe where the two differ is alphabet_sort, which trains at 0.8.

Not verified: the GPU smoke results in the description.

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: 83c22ea shortens the ValidationConfig docstrings and drops the sampling details from the SLAKE docstring and the READMEs (search_r1's README is back to main's text): the knobs are documented on ValidationConfig. @claude please review

@claude

claude Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 36s —— View job


Approve. Please fill in the empty human-written summary before merging. The doc trims in 83c22ea add no new problems: review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the item below before merging. 83c22ea only trims docs, and none of it is wrong. The one open item is still the empty human-written summary. A maintainer still needs to review, because this touches the RL controller.

What this PR does. Validation used to decode greedily, with 1 rollout per prompt. Now it samples at ValidationConfig.temperature (default 1.0) and runs num_samples_per_prompt rollouts per prompt (default 1). With n > 1 it also logs validation_reward/best_of_{n}.

Example.

ValidationConfig(num_samples_per_prompt=2), 2 prompts, training sampling T=0.8 seed=7
main: run_group_rollouts(group_size=1, sampling=T=0.0)
PR:   run_group_rollouts(group_size=2, sampling=T=1.0, seed=7)
      rewards [[1, 0], [0, 0]] -> validation_reward/_mean 0.25, validation_reward/best_of_2/mean 0.5

Status of the new commit

  • controller.py:168-173: the docstrings are shorter now. The greedy-needs-n=1 rule used to be in the docstring and is now enforced only by the __post_init__ error (controller.py:207-211). The error message is clear enough on its own.
  • slake.py:112 and the terminal_bench README no longer describe the validation sampling. They still say nothing incorrect.
  • search_r1/README.md is back to main's text. Its "greedy decoding" describes the linked snapshot curves, and those really were greedy, so it stays accurate. No in-repo doc still says current validation is greedy.

Must fix

  1. PR description: "Summary (human-written)" is still empty. The author has to fill it in (see "AI-assisted contributions" in CONTRIBUTING.md).

Questions

  • Still open for a maintainer: should validation use a fixed default of 1.0, or inherit generator.sampling.temperature? Today the only recipe where the two differ is alphabet_sort, which trains at 0.8.

Not verified: the GPU smoke results in the description.

@felipemello1
felipemello1 marked this pull request as ready for review November 20, 2026 03:51
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 20, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 20, 2026 03:51

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@felipemello1
felipemello1 merged commit 33cc5af into pytorch:main Nov 20, 2026
26 checks passed
felipemello1 pushed a commit that referenced this pull request Nov 25, 2026
## Summary (human-written)
Reject non-positive training sampling temperatures
(`generator.sampling.temperature <= 0`) at config time in
`Controller.Config.__post_init__`. Previously, setting `0.0` (greedy)
built without error and produced `NaN` logprobs, loss, and gradients
when `compute_logprobs` divided logits by temperature, while also
repeating identical rollouts with all-zero group-relative advantages.
Keeping the check in `Controller.Config` rather than `SamplingConfig`
leaves greedy validation (`async_loop.validation.temperature=0`)
unaffected, and `test_greedy_training_sampling_raises` guards against
regressions.

## Why
`SamplingConfig.temperature` documents `0.0 = greedy`, and
`Controller.Config` accepts it as the training sampling temperature. The
controller hands `generator.sampling.temperature` to the `Batcher`,
which fills every microbatch's `temperature[T]` with it, and
`compute_logprobs` scores tokens under `softmax(logits / temperature)`.
At 0 the trainer's logprobs, loss and gradients become NaN. A greedy
group also repeats one rollout, so its group-relative advantages are all
0.

Validation already rejects greedy sampling with more than one sample per
prompt (#6266), and the trainer-vs-vLLM parity test in #5928 scores
greedy tokens with `temperature=None` rather than 0. Training has no
such check.

**Example.** `rl_grpo_qwen3_5_debug_varlen(seq_len=128)` with
`generator.sampling.temperature=0.0`:

```
main:     the config builds; DAPOLoss with temperature[T] = 0 gives loss=nan,
          non-finite logits gradients and entropy nan (T = 0.8: loss 0.0964, finite)
this PR:  ValueError: generator.sampling.temperature must be positive, got 0.0: the
          trainer divides the logits by it. For greedy validation, set
          async_loop.validation.temperature=0.
```

## What changed
- `Controller.Config.__post_init__` raises `ValueError` when
`generator.sampling.temperature <= 0`.
- `test_greedy_training_sampling_raises` builds the CI recipe with
temperature 0 and expects the error. It catches the check being dropped,
which would let a greedy training config train on NaN again.

## Alternatives considered
- Check in `SamplingConfig.__post_init__`: validation reuses it through
`dataclasses.replace(self._sampling,
temperature=validation.temperature)`, so greedy validation would break.
- Score greedy tokens without the divide, as #5928's test does: removes
the NaN, but a greedy group still trains on all-zero advantages.

## User-facing changes
| Before | After |
| --- | --- |
| `generator.sampling.temperature=0.0` builds and trains on NaN loss |
`ValueError` at config time |
| `async_loop.validation.temperature=0` (greedy validation) | unchanged
|

## Testing
WSL2 Ubuntu 24.04, Python 3.12, torch `2.16.0.dev20261123+cpu`, vLLM
`1.0.0.dev20261107+cu132` with `VLLM_TARGET_DEVICE=cpu`, base `3f09e7b`
and this branch.

- `pytest tests/rl/unit_tests/cpu/test_async_controller.py -k
greedy_training_sampling`: fails on `main` with `DID NOT RAISE <class
'ValueError'>`, passes on this branch.
- `pytest tests/rl/unit_tests/cpu
--ignore=tests/rl/unit_tests/cpu/test_dapo_math.py
--ignore=tests/rl/unit_tests/cpu/test_geometry3k.py`: `main` 702 passed,
1 failed, 10 skipped; this branch 703 passed, 1 failed, 10 skipped. The
failure on both is
`test_controller_requires_a_vision_processor_exactly_for_multimodal_models`
(`No module named 'math_verify'` in my environment). The two ignored
modules need their example dependencies to collect.
- Every zero-argument recipe in `torchtitan_recipes/rl` and
`torchtitan_recipes/tests/rl` that imports without optional packages
(43, at temperature 0.8 or 1.0) builds on both `main` and this branch.
Training temperatures 1.0, 0.8 and 1e-3, and greedy validation with a
training temperature of 0.8, still build.
- On this branch merged with current `main` (`8919259`):
`test_async_controller.py` 55 passed. With `main`'s `controller.py`, the
new test fails with `DID NOT RAISE`.
- `pre-commit run --files` on the two changed files: flake8, ufmt,
pydoclint and codespell pass. Pyrefly reports the same diagnostics on
these files on `main` and this branch (RL packages missing on my Windows
checkout).

**AI assistance disclosure:** AI-assisted development tools were used
during investigation, implementation, and regression testing. I
personally reviewed the final diff, verified the correctness of the
changes and validation results, and take full responsibility for the
contribution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants