Repository navigation
[rl] Optional native rollout timeout - #5512
felipemello1 wants to merge 1 commit into
Conversation
ee23f54 to
740beed
Compare
a9cd35c to
9839941
Compare
740beed to
c81435b
Compare
9839941 to
70a1b3a
Compare
c81435b to
bb79416
Compare
70a1b3a to
a3f5b24
Compare
bb79416 to
67abd3e
Compare
a3f5b24 to
7b48f60
Compare
67abd3e to
e77d87d
Compare
7b48f60 to
8932f4e
Compare
08837ea to
910f98e
Compare
|
Reply from claude: @claude review. This PR was re-cut onto PR 7: it no longer depends on the HTTP generation PRs, so the code and the body changed. |
|
Claude finished @felipemello1's task in 2m 15s —— View job Needs discussion -- review posted. |
There was a problem hiding this comment.
Verdict: Needs discussion -- the code is correct as written, but this adds a config field with no consumer and no observed hang, and its status semantics (train vs not train) need a maintainer decision.
What this PR does. Adds RolloutWorker.Config.rollout_timeout_s (default None). When set, env.init() and every generate call plus env.step in a native rollout run under one asyncio.timeout. When it expires, the rollout ends as ERROR_TIMEOUT and keeps its completed turns, or as ERROR_SYSTEM if it has no turns yet. A TimeoutError raised before the deadline is still ERROR_SYSTEM. The session is released in every case.
Example. rollout_timeout_s=60, 2-turn env, and the generator never replies at turn 1:
before: the rollout, its group, and the group's buffer slot wait forever
after: at t=60s -> Rollout(status=ERROR_TIMEOUT, turns=[turn0]) -> scored, advantaged, trained
same stall at turn 0 -> Rollout(status=ERROR_SYSTEM, turns=[], logs={"error": ...}) -> not trained
Must fix
- The PR description: "Summary (human-written)" is empty.
torchtitan/rl/rollout/rollouter.py:333has no consumer. No recipe sets the field, and the description gives no observed hang ("Who sets it? No in-repo recipe yet"). Please name the run or user that hit a hungenv.init()or a lost generate reply. If nobody has, drop the field until someone does. If the real case is a lost reply, a deadline on the generate call inController._make_generate_fn(next to the existing cancel TODO) fixes the cause instead of bounding the whole rollout.env.init()could get a deadline inTokenEnvnext tostep_timeout_s, which already owns per-call env deadlines (token_env.py:104asks whether timeouts belong in that layer at all).
Should consider
rollouter.py:577-584-- the trained-or-not choice depends onlen(turns). A stall at turn 3, which is a system failure, trains on a partial trajectory with a reward. Making every expiryERROR_SYSTEMis simpler and matches the field's stated purpose (see inline).
Questions
- Should the native and Verifiers paths agree on a zero-turn timeout? On the native path it is
ERROR_SYSTEM(group kept). On the Verifiers path it isERROR(examples/verifiers/rollouter.py:375), and the sample builder then drops the whole group, which is what the comment atrollouter.py:578says this PR avoids. PR 10 moves Verifiers groups into rollout workers, so this difference will show up next to the native one.
Details
- Cancel interaction (PR 8): an external cancel is a
CancelledError, whichexcept Exceptiondoesn't catch, andasyncio.timeoutre-raises it when its own deadline hasn't expired, socancel_groupbehaves the same as before. The nestedasyncio.wait_forinTokenEnv.steponly catches its ownTimeoutError; the outer expiry reaches it as aCancelledErrorand passes through. asyncio.timeout(None)is a no-op, andrequires-python >= 3.11, soasyncio.timeoutandexpired()are available. When the field is None the behavior is unchanged, so the bitwise-numerics claim holds by construction.- Tests: the three parametrized CPU cases are cheap and cover the branches that would regress silently (zero-turn vs n-turn timeout, an early
TimeoutErrornot counted as a timeout). The fourrollout_timeout_s=Noneadditions are needed becauseSimpleNamespaceconfigs don't fall back to defaults.
Not verified: behavior under a real Monarch generator (no GPU run).
| if timeout.expired(): | ||
| # With no turn there are no tokens to train. As ERROR_TIMEOUT it would get a reward, | ||
| # and the sample builder would drop its whole group for having no completion tokens. | ||
| status = ( | ||
| RolloutStatus.ERROR_TIMEOUT if turns else RolloutStatus.ERROR_SYSTEM | ||
| ) | ||
| if not turns: | ||
| logs["error"] = "rollout_timeout_s passed before the first turn" |
There was a problem hiding this comment.
The status depends on len(turns), and that doesn't fit what the field is for. The docstring calls rollout_timeout_s a guard against "a hung env or generator", and PR 7 defines ERROR_SYSTEM as a failure outside the policy. Under this branch, a rollout whose generator stalls at turn 3 ends as ERROR_TIMEOUT: the rubric scores the partial trajectory, it gets an advantage, and it trains. That is the short-rollout bias the docstring warns about, and the policy didn't cause it. The same stall at turn 0 ends as ERROR_SYSTEM and doesn't train.
If every rollout_timeout_s expiry were ERROR_SYSTEM, the turns branch and its comment would go away, the group would still be kept (the sample builder skips ERROR_SYSTEM without dropping the group), and the types.py comment would reduce to "or it passed rollout_timeout_s". TokenEnv.step_timeout_s would keep ERROR_TIMEOUT for a slow env step, which the policy can plausibly cause.
| if timeout.expired(): | |
| # With no turn there are no tokens to train. As ERROR_TIMEOUT it would get a reward, | |
| # and the sample builder would drop its whole group for having no completion tokens. | |
| status = ( | |
| RolloutStatus.ERROR_TIMEOUT if turns else RolloutStatus.ERROR_SYSTEM | |
| ) | |
| if not turns: | |
| logs["error"] = "rollout_timeout_s passed before the first turn" | |
| if timeout.expired(): | |
| # A hung env or generator is not the policy's doing: keep the group, don't train it. | |
| status = RolloutStatus.ERROR_SYSTEM | |
| logs["error"] = f"rollout_timeout_s passed after {len(turns)} turn(s)" |
(and update the logger.warning below to match). If you want ERROR_TIMEOUT to match Verifiers' timeout.rollout, please say so in the docstring. Note that Verifiers sends a zero-turn timeout to ERROR (examples/verifiers/rollouter.py:375), not ERROR_SYSTEM, so the two paths still disagree.
3b01212 to
e83efcf
Compare
6691335 to
ee8da84
Compare
fc51806 to
aac4c28
Compare
55d19be to
6fb7960
Compare
717ad61 to
a9db346
Compare
6fb7960 to
f5e1684
Compare
a9db346 to
eef051a
Compare
f5e1684 to
21f6ee2
Compare
eef051a to
cdbc73a
Compare
21f6ee2 to
c67edf2
Compare
cdbc73a to
38cf105
Compare
c67edf2 to
74a1555
Compare
38cf105 to
5e6d7f4
Compare
74a1555 to
904294c
Compare
5e6d7f4 to
2960536
Compare
|
Reply from claude: On the open question (should a timed-out rollout train?): yes, on purpose.
The other point (no recipe sets |
2960536 to
97a13e7
Compare
|
Reply from claude: @claude please review. This PR now sits on the rebuilt "Cancel a rollout group on its worker". The only code change is the conflict resolution in |
|
Claude finished @felipemello1's task in 1m 30s —— View job Needs discussion -- review posted. |
There was a problem hiding this comment.
Verdict: Needs discussion -- the rebase onto the rebuilt base changes no behavior, and the body's new pointers are accurate. Two questions are still the maintainer's: the field has no user yet, and whether a timed-out rollout should train.
What changed since the last review. I compared HEAD (97a13e7) with e1a8f34:
- The PR now touches 2 files,
rollouter.pyandtest_rollout_worker.py.controller.pyis gone from the diff: the generate-call TODO it used to add now lives in the base, inRolloutWorkerActor.run_group(torchtitan/rl/actors/monarch/rollout_worker.py:78-80). - The
rollouter.pyhunks are the same as before: thedeadline.expired()branch, the docstring table keyed on finished turns, and the turn appended only afterenv.stepreturns. The tests are the same too.
I checked both of the body's new pointers at the base:
- Generate-call TODO. The base wraps each call in
asyncio.shieldand keeps it in_generate_calls. So when the deadline fires, the rollout stops waiting, the request keeps decoding, andwait_for_generate_callscovers its reply at close.closebounds that wait with_SHUTDOWN_DRAIN_TIMEOUT_S(rollouter.py:212), so a reply that never comes cannot hang shutdown. - Release-race NOTE. The
engine_loop.py:345TODO describes this case: a release runs before the cut rollout's generate call is admitted.
No new problems.
Example (unchanged): rollout_timeout_s=60, a 2-turn env.
generator hangs, turn 0 ERROR_INFRA, [], logs["error"] set -> group kept, not trained
env.step hangs, turn 0 ERROR_INFRA, [] (cut turn dropped)
env.step hangs, turn 1 ERROR_TIMEOUT, [t0] -> scored, trained
TimeoutError raised early ERROR_INFRA, turns kept so far
Must fix (still open)
- PR description: "Summary (human-written)" is still empty.
torchtitan/rl/rollout/rollouter.py:345: nothing uses the field yet ("Parked outside the stack: no recipe setsrollout_timeout_syet"). A knob should land together with its first user (CLAUDE.md principle 2,config.md"Few Knobs"). This is the maintainer's call, as the author said, so it stays open until a recipe or run needs it.
Should consider (answered by the author, left to the maintainer)
rollouter.py:647-651: whether a rollout trains depends onlen(turns). The 2026-11-11 reply explains the choice: after a finished turn the policy may have caused the hang, and with no finished turn there is nothing to train. The base'sRolloutStatusdocstring now records it. The reasoning is consistent with the code. I'm not reopening it.
Details
- With this field set, a cut rollout's request keeps decoding during normal training, not just after a shutdown or
cancel_groupcancel. Per the base TODO, it can pin its group's cache salt again. The body discloses this ("The generator still finishes a timed-out request"). - A reply that never arrives (the lost-reply case this field targets) leaves its future in
_generate_callsuntil close. The future is small, and close is bounded, so no action is needed. - The earlier analysis still holds at HEAD:
- An external cancel passes through
except Exception, andasyncio.timeoutre-raises it when its own deadline hasn't expired. - The nested
TokenEnv.stepdeadline doesn't catch the outer expiry. asyncio.timeout(None)is a no-op, so the default gives bitwise-identical numerics by construction.
- An external cancel passes through
Not verified: behavior with a real Monarch generator (no GPU run).
39b031b to
7423b81
Compare
97a13e7 to
2984935
Compare
|
Reply from claude: @claude please review. This push replays the PR onto main: the rollout fakes take main's |
|
Claude finished @felipemello1's task in 1m 11s —— View job Needs discussion -- review posted. |
There was a problem hiding this comment.
Verdict: Needs discussion -- the replay onto main (2984935) changes no behavior, and the fakes match main's Prompt API. Two questions are still the maintainer's: nothing uses the field yet, and whether a timed-out rollout should train.
What changed since the last review. I compared HEAD (2984935) with 97a13e7:
_run_single_rolloutnow callsgenerate_fn(prompt=env_step.next_prompt, ...)and keepsprompt_delta_images/prompt_delta_image_placeholderson each turn. Both come from the base, and this PR only re-indents them underasync with deadline:.- The test fakes (
_TwoTurnTokenEnv,_StepHangsTokenEnv) buildTokenEnvOutput(next_prompt=Prompt(token_ids=...)).generate_then_stall(prompt, **kwargs)takespromptby keyword, which matches the call site.Promptis exported fromtorchtitan/rl/rollout/__init__.py. test_multiturn_images.pyonly gets therollout_timeout_s=Noneline thatSimpleNamespaceconfigs need.- The
deadline.expired()branch, the docstring table and the turn-append order are the same as before.test_rollout_timeouthas 3 cases andtest_a_rollout_timeout_in_an_env_step_drops_the_cut_turnhas 2. Removing the send-time-raise case leaves a gap: no test now checks that aTimeoutErrorbefore the deadline isERROR_INFRAwith 0 turns. The(1, _raise_timeout, ERROR_INFRA, 1)case covers the sameelsebranch, so nothing important goes unguarded.
No new problems.
Example (unchanged): rollout_timeout_s=60, a 2-turn env.
generator hangs, turn 0 ERROR_INFRA, [], logs["error"] set -> group kept, not trained
env.step hangs, turn 0 ERROR_INFRA, [] (cut turn dropped)
env.step hangs, turn 1 ERROR_TIMEOUT, [t0] -> scored, trained
TimeoutError raised before limit ERROR_INFRA, turns kept so far
Must fix (still open)
- PR description: "Summary (human-written)" is still empty.
torchtitan/rl/rollout/rollouter.py:348: nothing uses the field yet ("Parked outside the stack: no recipe setsrollout_timeout_syet"). A knob should land together with its first user (CLAUDE.md principle 2,config.md"Few Knobs"). The author left this to the maintainer, so it stays open until a recipe or run needs it.
Should consider (answered by the author, left to the maintainer)
rollouter.py:653-662: whether a rollout trains depends onlen(turns). The 2026-11-11 reply explains why, and the reasoning matches the code. I'm not reopening it.
Details
- The earlier analysis still holds at HEAD:
- An external cancel passes through
except Exception, andasyncio.timeoutre-raises it when its own deadline hasn't expired. - The nested
TokenEnv.stepdeadline doesn't catch the outer expiry. asyncio.timeout(None)is a no-op, so the default gives bitwise-identical numerics by construction.
- An external cancel passes through
- The PR base is still
felipemello1/http/4-cancel-group(merge-base 7423b81), so "replays onto main" describes the base, which was rebuilt on main.
Not verified: I couldn't run the CPU tests here (the command needed approval), and nothing ran on a real Monarch generator.
2984935 to
f8082cf
Compare
|
Reply from claude: Parking this PR as a draft, outside the stack. Why:
To unpark: a native recipe that needs a cap on one rollout's total time, e.g. a native env with a sandbox or a slow external tool. Fix the shutdown race first: cut only env steps, or track the cut calls. |
Summary (human-written)
Agent details
Parked outside the stack: no recipe sets
rollout_timeout_syet. Its base isfelipemello1/parked-rollout-worker-base, a frozen copy of the stack up to "Rollouter: keep one handle per rollout worker" in its earlier order (Verifiers 0.4.0, with the row-0 router copy). Don't merge it: port it onto main when it is unparked. Delete that branch once #5507 and #5512 are both ported or closed.TLDR:
MessageEnvRolloutWorker.Config.rollout_timeout_sends a native rollout that runs past it, keeps its finished turns, and stops waiting for its generate call. A liveness guard against a hung env or generator, like Verifiers'timeout.rolloutfor the Verifiers path; off by default.Problem
env.init(), then alternates a generate call andenv.stepuntil the env is terminal.TokenEnvstep_timeout_s, 1800 s).env.init()and the generate call have none.Solution
MessageEnvRolloutWorker.Config.rollout_timeout_s(default None) putsenv.init()and every turn, notscore_group, under oneasyncio.timeout.ERROR_TIMEOUTwith its finished turns (a turn finishes when its env step returns), and the session is still released. The turn being cut is dropped: it has no parsed message yet, so a rubric would score an empty reply.step_timeout_sfires after the parse and keeps that turn, so set it belowrollout_timeout_sto train hung env steps.ERROR_INFRA, with the reason inRollout.logs["error"]: with no tokens, anERROR_TIMEOUTwould get a reward and make the sample builder drop its whole group.TimeoutErrorraised byenv.init()or the generate call before the deadline is not a rollout timeout: it ends asERROR_INFRA("Rollouts that fail outside the policy are recorded, not trained").TokenEnv.step_timeout_sstill owns step timeouts.ERROR_TIMEOUT): a timeout can't tell who hung, and the policy can cause the hang (e.g. a command that never returns). A hang in the first env step is the policy's too, but it isERROR_INFRAbecause no turn is left to train. Therollout_timeout_sdocstring says so.rollout_timeout_sdocstring).timeout.rolloutand keep the trace. One difference stays: a Verifiers timeout keeps the cut turn (its trace has the message), while this deadline drops it.rollout_timeout_sis onMessageEnvRolloutWorker.Config, not the base class, so a Verifiers config can't set it.Results
Numerics: bitwise vs main:
rollout_timeout_sdefaults to None, and no recipe sets it. Not run on GPU at this head.You may ask:
env.init(), and a generate call whose reply never comes (a stuck generator or a lost reply). Env steps already havestep_timeout_s.NOTE:
generation_server.py).GenerateFn, so itsfinallyreleases the call's router reservation while the generator still runs the call (look-aside's TODO in_make_generate_fn). Until that call ends, the router counts one call fewer on that generator. This PR is the first in-tree code that cuts a generate call.Rollouter.closeis safe for native rollouts only becauserun_groupreturns after every answer; a cut breaks that. Until Monarch issue 5301 is fixed, each worker would have to keep its unanswered calls andclosewait for them, as "Cancel a rollout group on its worker" does.async with; hide whitespace (?w=1) to read it.asyncio.to_threadwork insideenv.stepkeeps running past the timeout, as withstep_timeout_s.rollout_timeout_sdocstring says so.session_kv_holderon, a rollout cut right after it sent a generate call can release its session before the engine admits that call. This PR adds a TODO inengine_loop.pyfor this race. A Verifiers call sent right before the agent gives up on it (e.g. its rollout timeout) can hit it too, on main as well.Open for the maintainer:
ERROR_INFRA. Today a stall after the first finished turn isERROR_TIMEOUT, so its partial rollout is scored and trained.Test plan
pytest tests/rl/unit_tests/cpu # verifiers 0.4.0, at this position: 707 passed, 1 skippedtest_rollout_timeouton a 2-turn env: stall at turn 0 ->ERROR_INFRA, 0 turns; stall at turn 1 ->ERROR_TIMEOUT, 1 turn; aTimeoutErrorraised at turn 1 before the deadline ->ERROR_INFRA, 1 turn. The session is released in all three, and eachERROR_INFRArollout records why.test_a_rollout_timeout_in_an_env_step_drops_the_cut_turn(2 cases): the env step hangs in turn 0 or turn 1. The cut turn is dropped: turn 0 ->ERROR_INFRA, no turns; turn 1 ->ERROR_TIMEOUT, and the finished turn keeps its env reply.