Skip to content

[rl] Rollout turns as a tree: a turn's prompt is a delta on any earlier turn - #5303

Draft
felipemello1 wants to merge 1 commit into
mainfrom
felipemello1/rollout-turn-tree
Draft

felipemello1 wants to merge 1 commit into
mainfrom
felipemello1/rollout-turn-tree

Conversation

@felipemello1

Copy link
Copy Markdown
Contributor

TLDR: A RolloutTurn names its parent, any earlier turn, and stores its prompt (tokens and now messages) as a delta on it. Today this shrinks the recorded rollouts: one 30-turn rollout's rollout_samples.jsonl line drops from 3.38 MB to 0.22 MB, because prompt messages stop repeating the whole conversation. Forks (retries, resamples, subagents) store only their new tokens. No rollouter on main forks yet, so that part is headroom. The default Rollouter's packed batches are bitwise unchanged.

Problem

  1. Today. A turn's prompt tokens are a delta on the previous turn in Rollout.turns; its prompt_messages are stored in full.
  2. The parent is implicit. A subagent turn between two main-agent turns, or a retry of an older turn, shares little with the previous turn in the list, so it stores its prompt again and the builder re-forwards it as context.
  3. When. No rollouter on main spawns subagents or retries yet, so no run hits this today.
  4. Messages are O(K^2) for K turns. prompt_messages repeats the conversation on every turn: 3.38 MB for one 30-turn rollout's recorder line.
turn  prompt                                              stored today (delta on the previous turn)
0     [SYS USER_Q GEN]                                    3 tokens
1     [SYS USER_Q GEN THINK_1 TOOL_CALL TOOL_RESULT GEN]  2
2     [SUB_SYS SUB_TASK GEN]                  (subagent)  3
3     [... the 9 tokens of turn 1 ... SUB_RESULT GEN]     11
-> samples [0 1], [2] and [3], which re-forwards turn 1's 9 tokens as context

Solution

  1. Explicit parent. RolloutTurn.parent_turn_id: any earlier turn, or None for a root. The prompt is the parent's first prompt_prefix_len tokens of prompt + completion, then prompt_delta_token_ids.
  2. Messages the same way. prompt_message_prefix_len + prompt_delta_messages, measured against the parent's prompt messages + completion + env reply. TokenEnv returns the prefix next to the token split.
  3. Builder. A turn continues its parent's sample if its prompt holds the parent's whole prompt + completion and the parent is still that sample's last turn; otherwise it opens a sample with the parent's prefix as untrained context. Samples stay open until the rollout ends, so a turn can resume an earlier one.
  4. Producers. The default Rollouter sets parent = the previous turn. Verifiers sets parent = the last turn before it on its branch, with that turn's end as the prefix.
  5. Unchanged: GenerateFn, TrainingSample, the batcher, rubrics and metrics.
turn  parent  prefix  delta                    completion
0     None    0       [SYS USER_Q GEN]         [THINK_1 TOOL_CALL]
1     0       5       [TOOL_RESULT GEN]        [THINK_2 SPAWN]
2     None    0       [SUB_SYS SUB_TASK GEN]   [SUB_ANSWER]          subagent: a second root
3     1       9       [SUB_RESULT GEN]         [THINK_3 ANSWER]      the main agent resumes
-> 10 prompt tokens stored (19 today); samples [0 1 3] and [2], nothing re-forwarded

Results

CPU, real RolloutTurn / builder / recorder, groups built as the default Rollouter builds them. main -> this PR.
recorder line, one 30-turn rollout with messages     3.38 -> 0.22 MB
120-ply chess group (16 x 60 turns)
  pickle                                              11.3 -> 11.0 MB
  Monarch flatten + unflatten                         0.041 -> 0.030 s
  build_from_group                                    0.009 -> 0.008 s

Correctness:

  • Default Rollouter (the real TokenEnv + RolloutWorker, 618 rollouts including 18 edge cases): every turn, rollout metric, training sample and packed microbatch tensor is identical to main.
  • Verifiers (4,000 random message trees): every rebuilt prompt, completion, logprob and message is identical to main. 2.3% fewer samples, because a branch that resumes a turn now continues that turn's sample. Every completion still trains once, after its full prompt, with its own logprob.

You may ask:

  • Why not a message graph, a node per message? Two record types that must stay consistent, an ancestor walk on every add and every build, and a fork inside a completion would re-store the node's prefix. Storage is the same for our shapes.
  • Why not one store per group, so siblings share their first prompt? It saves 1.4% of a chess group's tokens, and rollouts would stop being self-contained (metrics, the recorder and filters take groups).
  • Why is parent_turn_id required? A default of None would silently make every turn a root: one sample per turn and quadratic trainer compute.

NOTE:

  • rollout_samples.jsonl keys change: prompt_messages becomes parent_turn_id + prompt_message_prefix_len + prompt_delta_messages. RolloutSampleRecorder._encode_turn documents how to rebuild a turn's full conversation and tokens.
  • Verifiers traces that resume a turn pack into fewer samples: the per-token signal is the same, but a loss curve on such a run is not a bitwise comparison.
  • Also fixes test_metrics.py's RolloutTurn fixture, which still used the removed prompt_token_ids and fails on main today.
  • Not run: a GPU run. Forks and subagent shapes are tested on CPU only.

Follow up

  • Per-token side data (router replay rows, top-k rows, forced-token loss mask): TODOs mark the spots in RolloutTurn and the builder.
  • Verifiers prompt messages as deltas, so the recorder can rebuild Verifiers conversations (a TODO in trace_to_rollout_turns).

Test plan

# verifiers 0.3.1, torch 2.16.0.dev20261011
pytest -q tests/rl/unit_tests/cpu   # 456 passed, 5 skipped, 2 failed; the 2 (test_verifiers_terminal_bench.py) also fail on main

New tests:

  • test_rollout_utils.py: fork from an older turn, fork from the middle of a sample, resample + branch inside a completion, interleaved subagent turns, a parent that is not an earlier turn.
  • test_verifiers.py: the Terminus-2 fork shape (a new root), a resumed turn continuing its sample.
  • test_rollout_worker.py: parent = the previous turn, and prompt messages rebuild from their deltas through the real TokenEnv.

The correctness numbers above come from scripts outside the repo: the same seeded rollouts and traces run on main and on this branch, compared field by field (floats as bits), plus an independent check that every completion trains once after its full prompt with its own logprob.

…er turn

A RolloutTurn names its parent (parent_turn_id: any earlier turn, or None for a root) and stores only
what its prompt adds to the parent's prompt + completion. Prompt messages become deltas the same way
(prompt_message_prefix_len + prompt_delta_messages), so the recorder no longer repeats the whole
conversation on every turn. Forks from older turns, resamples and interleaved subagents can store
only their new tokens.

- TokenEnv returns the message prefix next to the token split; its messages only grow, so the message
  delta is empty. RolloutWorker sets parent = the previous turn.
- The builder extends a turn's parent sample when the prompt holds the parent's whole prompt +
  completion and the parent is still that sample's last turn. Otherwise it opens a sample with the
  parent's first prompt_prefix_len tokens as untrained context. Samples stay open until the rollout
  ends, so a turn can resume an earlier one. For the default Rollouter, packed batches are bitwise
  unchanged.
- Verifiers: a turn's parent is the last turn before it on its branch, so a branch that resumes a
  turn continues that turn's sample. Turns still record only env replies.
- Recorder: prompt_messages becomes parent_turn_id + prompt_message_prefix_len +
  prompt_delta_messages; _encode_turn documents the rebuild rule.
- test_metrics: its RolloutTurn fixture still used the removed prompt_token_ids.

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