Repository navigation
[rl] Store each turn's prompt as a delta on the previous turn (linear-size multi-turn rollouts) (1/2) - #5273
Conversation
|
@claude the idea sgtm, please review and give succinct feedback |
|
Claude finished @tianyu-l's task in 1m 7s —— View job Review: delta prompts (1/2)I found no correctness bugs. The builder and both rollouters agree on what "previous" means: the rollouter uses the previous turn's prompt + completion, and the builder uses
The overall design looks good to me: longest common prefix over break-only, bitwise-identical packing, and the TODO for a graph representation. The rename of |
…-size multi-turn rollouts) RolloutTurn stored its full prompt every turn, so a rollout held O(num_turns**2) tokens, all pickled from the rollout worker to the controller. On a 16-rollout chess group at 120 plies that was 236 MB and 1.83 s to unpickle on the controller's event loop, per group. RolloutTurn.prompt_token_ids becomes prompt_prefix_len (tokens shared with the previous turn's prompt + completion) + prompt_delta_token_ids (the rest); split_prompt computes both from the longest common prefix, so continuing and rewritten histories are both linear. The builder rebuilds a branch's prompt from the previous turn's tokens and opens samples exactly where it did before; packed training batches are bitwise identical.
…s not the longest; docstrings The builder now also raises when a turn claims too few shared tokens (e.g. a full prompt stored as the delta), so a hand-migrated rollouter fails loudly instead of opening one sample per turn. prompt_prefix_len's docstring states the longest-prefix rule; split_prompt is a plain loop; the graph TODO names verifiers v1 and what a graph would add.
…elds; TODO without chess numbers
RolloutTurn's docstring draws the delta once, including a turn whose history was rewritten (thinking stripped); the field and Rollout.turns docstrings shrink to one line each. The builder example no longer claims a compacted prompt shares nothing with the previous turn (BOS and the system prompt stay). A new test drives the real RolloutWorker._run_single_rollout over 20 random multi-turn rollouts (continue, strip thinking, shrink to a prefix, reset) and checks the builder opens and fills samples exactly as the full-prompt rule does.
6930add to
c61dfe4
Compare
RolloutTurn shows the rebuild rule as one formula and defines P/Cn/En; split_prompt gets Args and Returns; the builder's example gets a legend; the prefix check lists its two rejected cases; the Rollout.turns TODO says what forking from any earlier turn would buy.
RolloutTurn and split_prompt share one example in token ids (prompt [0, 1, 2], completion [3]). The builder's prefix check becomes two checks with their own example and message: reusing more tokens than the previous turn has, and could_reuse_one_more (the first new token still matches). Same logic as before. The rollout_to_training_samples example is back to main's version; the storage format is explained once, in RolloutTurn.
…checks TokenEnv builds each prompt, so it now also splits it: TokenEnvOutput.next_prompt_prefix_len (0 on the first prompt) and the next_prompt_delta_token_ids property. RolloutWorker copies them into RolloutTurn and no longer handles tokens. The random rollout-worker test drives the real TokenEnv with a renderer that bridges or re-renders a rewritten history; a TokenEnv test checks the bridge and re-render cases. The builder's two prefix checks get a blank line each, and the too-many example says e.g.
next_prompt_prefix_len says it is the longest shared prefix and that a TokenEnv must set it with split_prompt from the tokens it returns; split_prompt is called with kwargs. The Rollout.turns TODO says what forking from an earlier turn buys. Tests: the rewriting renderer keeps one list of prompts and returns copies; the TokenEnv test re-renders a prompt that shares 4 tokens, so a stale previous prompt would give a different split.
…pping the run build_from_group used to pass such a rollout to rollout_to_training_samples, which raises and stops the batcher loop. It now drops the rollout and counts it in training_sample_builder/num_rollouts_dropped_empty_completion. rollout_to_training_samples still raises when called directly.
TLDR: each
RolloutTurnnow stores only the prompt tokens that differ from the previous turn, not its full prompt, so a rollout grows linearly with turns instead of quadratically. On a synthetic 120-ply chess group, the token fields pickle to 17 MB instead of 236 MB and unpickle on the controller in 0.11 s instead of 1.83 s. Today's chess runs (~11 plies) unpickle in under 0.12 s per group, so for chess this is headroom; Terminal-Bench rollouts reach ~45 turns at ~95K-token contexts (not measured here). Packed training batches are bitwise identical.Problem
RolloutTurn.prompt_token_idsis the whole conversation so far, so K turns hold sum(prompt_k) tokens: O(K^2).RolloutGroup, and the controller unpickles it on its event loop.Solution
prompt_token_idsbecomesprompt_prefix_len+prompt_delta_token_ids, whereprompt == previous_turn_tokens[:prompt_prefix_len] + prompt_delta_token_ids.split_prompt(prompt, previous)fills both from the longest common prefix, so a rewritten history (e.g. thinking stripped) also stores only what changed.TokenEnvcalls it, since it builds each prompt (TokenEnvOutput.next_prompt_prefix_len), so the rollout loop never handles tokens; the verifiers example calls it too.token_ids, and opens samples exactly where it did before.prompt_messages(still full).Results
You may ask:
NOTE:
log_tensors=True, the recorder writesprompt_prefix_len+prompt_delta_token_idsinstead ofprompt_token_ids.RolloutTurn.num_prompt_tokensreplaceslen(prompt_token_ids)in the rollout metrics.build_from_groupdrops a rollout whose turn after the first has no completion (e.g. a request aborted before its first token) and counts it intraining_sample_builder/num_rollouts_dropped_empty_completion. Before,rollout_to_training_samplesraised and stopped the run.RolloutTurnby hand must callsplit_prompt. Otherwise the builder raises: a full prompt stored as the delta would silently open one sample per turn.Test plan
pytest tests/rl/unit_tests/cpu(verifiers 0.3.1 installed): 443 passed, 5 skipped, 2 failed. Main: 432 passed and the same 2 failed (test_verifiers_terminal_bench.pystill reads the renamedtrain_dataset).test_delta_prompts_rebuild_the_full_prompt_samples(the real rollout worker andTokenEnvon 20 random multi-turn rollouts vs the full-prompt rule),test_env_returns_each_next_prompt_as_a_delta,test_split_prompt,test_rewrite_that_keeps_a_prefix_branches_then_continues,test_prefix_that_is_not_the_longest_raises,test_verifiers_second_branch_stores_its_prompt_as_a_delta_on_the_first.TokenEnv+RolloutWorkeron 618 rollouts (18 edge cases: empty delta, strict prefix down to an empty prompt, retries, resets, env errors, truncation, abort, NaN logprobs, prompt too long) andVerifiersRollouter.trace_to_rollout_turnson 4,000 random message trees. Every rebuilt prompt, training sample, rollout metric and packed microbatch tensor is identical to main, with and without the stackedTrainingSample-tensor PR. A copy that splits against the prompt without the completion fails the same check.