Skip to content

[rl] Verifiers: one GenerateFn per running rollout group - #5948

Draft
felipemello1 wants to merge 1 commit into
felipemello1/eval/1-validation-dataloaderfrom
felipemello1/eval/2-verifiers-generate-fn
Draft

felipemello1 wants to merge 1 commit into
felipemello1/eval/1-validation-dataloaderfrom
felipemello1/eval/2-verifiers-generate-fn

Conversation

@felipemello1

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: The Verifiers generation server keeps one GenerateFn per running rollout group, keyed by group id, instead of one for all groups. Each HTTP call goes to its own group's function. This is prevention. Its user is the next PR in this stack (the Evaluator), which runs validation groups beside training groups.

main:     run_group_rollouts(group 7, fn=training)   -> server.set_generate_fn(training)
          run_group_rollouts(group -1, fn=validation) -> server.set_generate_fn(validation)
          a call from group 7                         -> validation's fn: logged as validation_generator/*
this PR:  server.generate_fns = {7: training, -1: validation}
          a call from group 7                         -> training's fn
          a call from a group that ended               -> HTTP 400 "rollout group 7 is not running"

Problem

  1. The controller gives each group a GenerateFn. Training's logs generation metrics as generator/*, validation's as validation_generator/*.
  2. VerifiersRollouter.run_group_rollouts stores it with GenerationServer.set_generate_fn: one slot for every group.
  3. So the last group to start rewires the calls of every group still running.
  4. On main this can happen only at a boundary: a cancelled training rollout whose call arrives after the end-of-training validation pass starts is logged as validation (possible, not measured). Once validation runs beside training (next PR), every validation group would rewire every training group.

Solution

  1. GenerationServer.generate_fns: dict[int, GenerateFn] replaces generate_fn and set_generate_fn.
  2. The request handler already parses the group id (sampling_params.torchtitan_group_id); it now looks up that group's function.
  3. run_group_rollouts adds its entry before its rollouts start and removes it in a finally once they end.
  4. A call for a group with no entry gets HTTP 400 naming the id, e.g. a late call from a cancelled group. Main answered 503 only before the first group started.
  5. Unchanged: the native (non-Verifiers) rollout path, request ids, routing and metrics keys.

Test plan

Verifiers tests, in a venv with `verifiers`: 62 passed (one new parametrize case: a call from a group that is not running gets 400)
  python -m pytest tests/rl/unit_tests/cpu/test_verifiers{,_example,_terminal_bench,_terminal_bench_prepare_tmax}.py tests/rl/unit_tests/cpu/test_model_defaults.py
RL CPU suite: 550 passed, 7 skipped (the Verifiers tests skip there without `verifiers`)
pre-commit: passed. pyrefly: 0 errors.

test_a_failing_rollout_fails_its_group_only_after_every_sibling_finished now also checks that a failed group's entry is removed.

@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 8, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 8, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review. This PR is part of a stack. Review only this PR's diff against its base branch. The human-written summary is Felipe's to write.

@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 33s —— View job


Approve, with one optional suggestion (assert that a group id isn't already running before it is added): 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 -- the fix is correct, in the right layer, and makes the server smaller. One small optional change is below.

Reviewed only this PR's diff against felipemello1/eval/1-validation-dataloader. As asked, the empty human-written summary is not flagged.

What this PR does. The Verifiers GenerationServer used to hold a single GenerateFn, and every running group shared it. Now it holds one per running rollout group, keyed by the torchtitan_group_id it already parses from each request. run_group_rollouts adds its group's entry before its rollouts start and removes it in a finally block. A call for a group with no entry gets HTTP 400. set_generate_fn and the 503 "not ready" path are deleted.

Example.

base:    training group 7 running   -> server.generate_fn = training_fn
         validation group -1 starts -> server.generate_fn = validation_fn
         group 7 makes a call       -> validation_fn: metrics go to validation_generator/*
this PR: generate_fns = {7: training_fn, -1: validation_fn}
         group 7 makes a call       -> training_fn
         group -1 ends              -> entry removed; a late call from -1 gets 400 "rollout group -1 is not running"

Should consider

  • torchtitan/rl/examples/verifiers/rollouter.py:259 -- assert that the group_id is not already registered. The dict relies on ids being unique among running groups. That holds today, but validation ids -(i + 1) repeat on every pass. If two passes overlap, the second one silently takes over the first one's calls, and the del in finally then fails with KeyError (details inline).
Details
  • Group ids at HEAD: training groups use IterableRolloutDataLoader / MapStyleRolloutDataLoader._next_index, which only increases, so a training id never repeats while its group is running (torchtitan/rl/rollout/data.py:127,221). Validation uses -(i + 1) within one pass, and _collect_validation_rollouts runs one pass at a time (controller.py:697). So no ids collide in this PR; the assert guards the next PR in the stack.
  • Storing self._generation_server in a local is justified: close() sets the attribute to None, and without the local the finally block would raise AttributeError instead of removing the entry.
  • Deleting the 503 is safe: a request can only arrive from a group that has already registered its entry, so the "not ready" state no longer exists.
  • Tests: the new parametrize case extends an existing test rather than adding one, and it guards the new 400 path. The extra assert in the failing-group test checks that the finally cleanup ran. Both are cheap CPU tests.

),
return_exceptions=True,
)
generation_server.generate_fns[group_id] = generate_fn

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.

The dict assumes no two running groups share a group_id, but nothing checks that. Today the ids are unique: training ids come from the dataloader's increasing _next_index, and validation uses -(i + 1) for one pass at a time. But validation ids repeat on every pass, and the next PR in this stack runs validation beside training. If two passes ever overlap, the failure looks like this:

  1. Group B (id -1) overwrites group A's entry, so A's calls go to B's GenerateFn. That is the wrong-prefix bug this PR fixes, and nothing reports it.
  2. A finishes and its del removes B's entry, so B's remaining calls get "rollout group -1 is not running".
  3. B's finally raises KeyError, which hides B's real result.

An assert at registration turns all three into one clear error:

Suggested change
generation_server.generate_fns[group_id] = generate_fn
assert (
group_id not in generation_server.generate_fns
), f"rollout group {group_id} is already running"
generation_server.generate_fns[group_id] = generate_fn

@felipemello1
felipemello1 force-pushed the felipemello1/eval/2-verifiers-generate-fn branch from 33a8459 to 270fde1 Compare November 8, 2026 23:07
@felipemello1
felipemello1 force-pushed the felipemello1/eval/1-validation-dataloader branch from 0270e15 to 2f3bafe Compare November 8, 2026 23:07
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: rebased onto the new #5944 head (63d82dd) with no code change (git range-diff shows every commit identical). Local checks after the rebase: RL CPU suite 550 passed / 7 skipped, Verifiers tests pass, pre-commit clean. @claude please review again. Review only this PR's diff against its base branch.

@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve. The rebase left this PR's diff unchanged and I found no new problems. One optional suggestion from the last review is still open: assert that a group id isn't already running before adding it. 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 -- the rebase left this PR's diff unchanged, and I found no new problems.

This is a re-review. I looked only at this PR's diff against felipemello1/eval/1-validation-dataloader, at head 270fde1. I compared it with the diff from the last review (33a8459) and they are identical: same 3 files, +61/-37.

What this PR does. The Verifiers GenerationServer now keeps one GenerateFn per running rollout group, stored in generate_fns and keyed by group id. run_group_rollouts adds its group's entry before its rollouts start and removes it in a finally block. A call from a group with no entry gets HTTP 400.

Example.

base:    training group 7 running, validation group -1 starts -> group 7's calls go to validation_fn
this PR: generate_fns = {7: training_fn, -1: validation_fn}  -> group 7's calls go to training_fn
         group -1 ends -> its entry is removed; a late call from -1 gets 400 "rollout group -1 is not running"

Should consider

  • Still open from the last review: torchtitan/rl/examples/verifiers/rollouter.py:259. Assert that group_id is not already in generate_fns before adding it. Group ids are still unique at the new base: training ids come from _next_index (torchtitan/rl/rollout/data.py:127,221), and validation uses -(i + 1) one pass at a time (torchtitan/rl/controller/controller.py:733). So nothing breaks in this PR. Validation ids repeat on every pass, though, and once the next PR runs validation beside training, two overlapping passes would silently send one group's calls to the other group's function. The del in the finally block would then raise KeyError. The inline suggestion from the last review still applies.
Details
  • No callers of the old API are left: set_generate_fn and generate_fn no longer appear in the repo outside this diff.
  • The earlier analysis still holds at the new base. The local generation_server keeps close() from turning the finally cleanup into an AttributeError. Removing the 503 is safe because a request can only come from a group that has already added its entry.

felipemello1 added a commit that referenced this pull request Nov 9, 2026
… own (#5969)

## Summary (human-written)
1) we would like to visualize generatioin metrics for training and
validation separately
2) currently we pass a "metric prefix" and have the generator add the
suffix
3) this doesnt compose well with verifiers
4) we remove this, and let the evaluator add the suffix when they come
back

## Agent details

Stack: main <- #5944 <- this PR <- #5228. Supersedes #5948 (one
`generate_fn` per group, kept in a dict).

**TLDR:** The generate path no longer carries a metrics label.
Generators always name their metrics `generator/*`, and validation
renames its own to `validation_generator/*` after results return, so the
W&B keys stay as on main. #5228 (validation beside training) needs this
first: Verifiers' HTTP server holds one `generate_fn`, so once groups of
both kinds overlap, a request can log under the other kind's label.

```text
main:     validation generate_fn(metrics_prefix="validation_generator") -> router -> generator -> "validation_generator/queue_time_ms"
this PR:  every generate_fn, no label                                    -> router -> generator -> "generator/queue_time_ms"
          validation, after the group returns:  "generator/queue_time_ms" -> "validation_generator/queue_time_ms"
```

**Problem**
1. The controller builds two kinds of `generate_fn`: training's sends
`metrics_prefix="generator"`, validation's `"validation_generator"`.
2. The label travels through 7 layers to name each request's metrics:
controller, `InterGeneratorRouter`, the Monarch router and generator,
`Generator`, engine loop, dispatcher.
3. Verifiers runs its agents against an HTTP server that holds one
`generate_fn` (`GenerationServer.set_generate_fn`). Each group that
starts replaces it.
4. So while a training group and a validation group run at once, a
request uses the function of the group that started last, and logs under
that kind's label. Once validation runs beside training (#5228), that is
most turns of a multi-turn env.
5. On main no logged metric is wrong: validation never overlaps a
running training rollout. A late call from a cancelled rollout lands in
metadata nobody reads. So on main this PR is a cleanup that prevents the
bug.

**Solution**
1. Drop `metrics_prefix` from the generate path. The dispatcher spells
each key: `generator/queue_time_ms`, `generator/num_cached_tokens`, ...
2. `_collect_validation_rollouts` renames its turns' `generator/*`
metrics to `validation_generator/*` after results return.
3. The Verifiers server keeps its one slot. Every group now passes a
function with no label, so replacing it is safe (a comment at the call).
4. Unchanged: the W&B keys (`generator/*` for training,
`validation_generator/*` for validation), the `GenerateFn` contract,
routing and request ids.
5. The logic is in two places: the rename in
`_collect_validation_rollouts` and the spelled keys in
`_prepare_generation_request_metrics`. The other files only delete the
parameter, plus one comment in the Verifiers rollouter and the new test.

You may ask:
- Why not one `generate_fn` per group in the Verifiers server, keyed by
group id (#5948)? It fixes Verifiers only and adds a per-group lifetime.
A function with no label can't be the wrong one, whatever calls it. This
PR replaces #5948.
- Why keep `validation_generator/*` and not `validation/generator/*`?
Dashboards and old runs keep their keys.

NOTE:
- A metric about the whole engine shouldn't ride on one request's
metrics: validation renames every `generator/*` key on its turns, and a
failed group drops its turns' metrics.
- Open PRs that pass `metrics_prefix` need a rebase that drops it.
- The rollout recorder writes no metrics, so `rollout_samples.jsonl` is
unchanged.

**Test plan**
```text
pytest tests/rl/unit_tests/cpu -q --ignore=tests/rl/unit_tests/cpu/test_vllm_wrapper.py
    # 551 passed, 7 skipped (base: 550 passed, 7 skipped)
pytest tests/rl/unit_tests/cpu/test_verifiers*.py tests/rl/unit_tests/cpu/test_model_defaults.py   # in a venv with verifiers
    # 61 passed
pre-commit run --files <changed files>
    # all hooks pass
pyrefly check
    # 1 error, the same on the base: FSDPModule.set_unshard_in_current_stream (torchtitan/distributed/fsdp.py:593)
```
- New:
`test_async_controller.py::test_validation_renames_generator_metrics`.
It fails when the rename is removed.
- `test_generator.py`, `test_engine_loop.py`,
`test_async_controller.py`: the calls drop `metrics_prefix`.
`test_metric_timing_math` now checks `generator/*` keys.

---------

Co-authored-by: Felipe Mello <[email protected]>

This branch has not been deployed

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

Labels

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.

1 participant