Skip to content

perf(checkpoint): pipeline packed prefix writes through a TQ actor - #5176

Merged
terrykong merged 5 commits into
mainfrom
amahishi/pr4508-prefix-write-pipeline-actor
Nov 21, 2026
Merged

terrykong merged 5 commits into
mainfrom
amahishi/pr4508-prefix-write-pipeline-actor

Conversation

@macandro96

@macandro96 macandro96 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What and why

Problem. At a rollout checkpoint, each vLLM engine cuts every unfinished model call it holds: it freezes the call, encodes its new tokens as a row, writes the rows to TransferQueue (TQ), and seals the call once the write is confirmed. Since #4508 this runs on one thread, one inventory at a time: prepare a batch, write it, wait, seal, then prepare the next. Preparation and writing never overlap, and every cut writes separately.

Change. Every generation-prefix cut now goes through a write pipeline, and on TQ the writes run in a separate CPU writer process.

  • Write pipeline (write_pipeline.py). One prepare thread steps each inventory's cut, which keeps every call's freeze/seal on that thread. One write thread does one write at a time. Prepared batches from different inventories of the same checkpoint are merged into one write, within rollout_recovery.prefix_write_max_rows / prefix_write_max_tokens and a byte bound. At most one write is prepared ahead of the write in flight, so only about two batches of calls are frozen at once. A row larger than the byte bound is written alone instead of being dropped.
  • Writer actor (prefix_writer_actor.py). With data_plane.impl: transfer_queue (any backend), prefix PUTs run in a 1-CPU actor pinned to the engine's node. It needs one free Ray CPU there. A local data plane keeps writing in-process. Terminal writes and restore reads keep the worker's own client.
  • Same rows, same guarantees. The sink keeps one encoder; checkpoint records and digests are unchanged. No receipt is sent before TQ confirms a write. A failed merged write retries each row alone exactly once. A dead writer raises PrefixWriterDiedError. Duplicate in-flight inventories share one result; inventories that share calls never prepare at the same time. Router-replay routes and media capture are supported.

There is no new config. The pipeline's bounds are fixed in PREFIX_WRITE_LIMITS: 128 pending inventories, 512 MiB buffered, 128 MiB per merged write, and a 10 ms window for merging. They are documented in single-controller-rollout-checkpointing.md.

flowchart LR
  G[Gym: cut inventories] --> P[prepare thread:<br/>freeze calls, build rows]
  P -->|batches, merged<br/>across inventories| W[write thread:<br/>one write at a time]
  W -->|TQ| A[CPU writer actor<br/>on the engine's node]
  A -->|keys| W
  W -->|keys| P
  P -->|seal calls, receipt| G
Loading

Issues

Follow-up to #4508. Part of #3594.

Convergence / perf / functionality proof

Perf (single runs, earlier revision of this PR). 4 nodes: two TP4 generation engines, one TQ node, one driver node; router replay on, max_num_seqs=512, 630 outstanding calls and 39 inventories per engine. Prefix cut + TQ save time only, not full rollout-checkpoint latency.

Write path Snapshot 1 Snapshot 2
#4508 baseline 52.44 s 107.60 s
pipeline + a direct column encoder, worker's own client 31.65 s 81.20 s
same + writer actor + NumPy packing 25.24 s 72.27 s

These were measured before the direct column encoder was removed and the pipeline became the only path, so no row matches the code as it ships. The shipped path (pipeline + writer actor, one encoder) has not been re-benchmarked.

Functionality.

  • Unit tests cover the pipeline, writer actor, encoder parity, media cut rows, deadlines, failed-write fallback, a dead writer and shutdown: test_prefix_write_pipeline.py, test_prefix_writer_actor.py, test_vllm_token_capture_hosting.py.
  • Local runs on 9d9d34e: 366 passed (default lane) and 320 passed (Gym lane) across those files, test_tq_token_sink.py and test_setup.py. pyrefly 1.4.0: 0 errors over main's baseline.
  • The SC prefix-recovery and router-replay functional tests now check that the pipeline and writer actor started.
  • CI on 9d9d34e: in progress.

Needs upstream changes

None.

PR commands and CI
  • CI label, exactly one:
    • CI:Lfast: the default for most PRs. Runs a fast test subset.
    • CI:L1: dependency or container changes (pyproject.toml, uv.lock, Dockerfile) or broad core changes. Builds a new image and runs unit and functional tests.
    • CI:docs: docs-only changes. Runs doc tests only.
  • /ok to test <full-commit-sha>: start CI for that commit. Post again after each push.
  • /review: automated review comments. Add mode=strict for a stricter review, reset=true for a fresh full review.
  • /resolve-conflict: Autobot merges main into this PR and resolves the conflicts. PRs from forks must allow edits by maintainers.
  • Locally: /review-pr-team <pr-number> for a team review before you ask for human review.
  • Contributor guidelines

🤖 Generated with Claude Code

@macandro96 macandro96 added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Nov 12, 2026
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 981376a

@copy-pr-bot

copy-pr-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Nov 12, 2026
@macandro96
macandro96 added this pull request to stack #5181 November 12, 2026 15:29
@macandro96
macandro96 requested a review from terrykong November 12, 2026 15:33

@macandro96 macandro96 left a comment

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.

The core design is sound, and several choices are worth keeping as they are:

  • One cut generator drives both the synchronous and the pipelined modes, so there is no second copy of the cut loop (the problem the #4508 review removed).
  • No receipt is sent before the rows are durable, and keys that no receipt advertised are cleared on interruption.
  • The writer client has the same put_samples / clear_samples interface as the data-plane client, so the sink does not need to know about actors.
  • The direct encoder writes exactly the same rows as the general one, and 3,600 contended stress runs of the pipeline tests showed no flaky failures.

Must fix before merge

  1. (C1) CI Lint is red: one pyrefly error and import order in 3 test files.
  2. (C2) The 78 new tests run in no CI lane, because they lack the nemo_gym mark.

How to organize this PR and remove the clutter

  1. (C3) Move the new keys into one block under rollout_recovery, next to their siblings. This removes the 2 copied limits, the hand-written bool check, and the 6 YAML hunks.
  2. (C6) Check each setup rule in one place instead of up to four (about 70 lines become about 25).
  3. (C7) Remove the benchmark timing code (about 110 lines) and keep prefix PUTs in the existing data-plane metrics.
  4. (C8) Move the writer actor into a stacked PR, so the encoder and pipeline can land first.

Together these remove about 200 lines and give each rule one home.

Other comments

  • Behavior: (C4) resume fails when the new keys change before the first trainer checkpoint; (C5) the pipeline holds calls frozen for longer; (C10) a dead writer actor is never replaced; (C24) with the pipeline on, a call whose row goes over the byte limit is never checkpointed again.
  • Smaller cleanups: (C9) writer startup timeout and error message, (C11) encoder split in the sink, (C12) copied key prefix, (C13) repeated column code, (C14) byte-estimate constants, (C15) module location.
  • Comments and docs: (C16) and (C17) restore deleted explanations; (C18) name the required keys in the docs.
  • Tests: (C19) expired prepared batch, (C20) byte limit and admission limit, (C21) encoder parity, (C22) writer startup failure, (C23) end-to-end coverage.

PR description

  • Write "Part of #3594" under Issues (not "Closes"; the stack is not complete).
  • Note that #4843 changes the same cut and route code on an older base, so whichever PR lands second needs to be ported onto the new generator.

Visual explainer: https://macandro96.github.io/gh-pages-poc/amahishi/pr-5176-prefix-writes.html

Generated by Claude Code

Comment thread nemo_rl/data_plane/prefix_pipeline.py Resolved
terrykong added a commit to terrykong/RL that referenced this pull request Nov 16, 2026
…ctors

The class-per-everything version cost more than it saved: the prefix writer
differs in owner, count, placement, transport and concurrency, and a pool
and a common process() method had no generic caller. Keep only what the
advantage actor and the rollout reassembler really share:

- DataPlaneDelegate: owns the storage-lending client, names the actor in Ray
  logs, and holds the checkpoint and metrics hooks once.
- start_cpu_actors: create, wait for a ready method, kill all on failure.
- spread_off_head: the advantage actor's placement, now reusable.

run/finalize keep their names, the controller keeps its queues, and the
prefix writer is as in NVIDIA-NeMo#5176.

Signed-off-by: Terry Kong <[email protected]>
@macandro96
macandro96 force-pushed the amahishi/pr4508-prefix-write-pipeline-actor branch from 981376a to d42cf30 Compare November 17, 2026 06:03

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review of #5176 — pipelined prefix writes and the TQ writer actor

Two parts of the design work well: the generator protocol in _checkpoint_steps lets one code path serve both the sync cut and the pipeline, and the service/actor split lets the write order be unit-tested without Ray.

Before merge

  • Two asks are for when the stack moves onto #4979: C14 (run_cut deadline) and the Dynamo half of C10.
  • CI has not run yet (copy-pr-bot vetting is pending), and the new path has no GPU run.

Scope. Written on 981376a3, then re-checked on the rebased head d42cf304 (+2,298/−60 on top of #4508's 46c9ea6c); inline links pin 981376a3. The rebase already fixed the ruff import-order failures.

Size. Together, the asks below remove about 560 lines (≈ 385 source, config and docs, ≈ 175 test), which brings the PR to about +1,720 and keeps every measured win. C17 could remove ≈ 290 more, depending on one measurement. If it is still too big, it splits cleanly: (A) direct encoder + pipeline, the bigger measured win (52 → 32 s, 108 → 81 s), then (B) the writer actor (32 → 25 s, 81 → 72 s).

Inline comments

  1. (C1) CI runs none of the 78 new data-plane tests; add the nemo_gym marker to both files.
  2. (C2) Keep the NumPy byte views but shrink them to two helpers; replace RayPrefixWriterClient with a small wrapper. ≈ −130 source, −75 test.
  3. (C3) Drop the timing fields, per-PUT logs and stats counters that only logs and tests read. ≈ −110 source, −20 test.
  4. (C8) Delete _validate_keys, which can never fire.
  5. (C9) Share one dense-or-jagged helper in direct_prefix_batch.py.
  6. (C10) Make the L1 prefix-recovery run fail if the writer actor never started; same for the Dynamo run after #4979.
  7. (C11) Put back the docstring paragraph that explains deadline.
  8. (C14) Let run_cut take the caller's deadline in both branches, ready for #4979.
  9. (C15) Drop the three settings and pick the new path automatically; keep the old path only for media and non-simple TQ backends. ≈ −120 source, config and docs, −65 test.
  10. (C16) Rename _checkpoint_steps / CutSteps / BatchReleased for what they hold.
  11. (C17) Measure the direct encoder on its own; delete it if it does not pay for itself (≈ −290 more), otherwise make it the only text encoder.
  12. (C18) FYI only: the writer stays its own class; #5256 shares code between the two CPU actors already on main.
  13. (C19) With the actor always on, drop the pipeline's writer thread: send with .remote() and use Ray's future.

Notes

  • The benchmark rows are single runs. timing/rollout_checkpoint/total_save_seconds over 3 or more runs per row would make the table firmer.
  • Cross-inventory merging and the 10 ms coalescing window have no number of their own. With 39 inventories per engine in the benchmark they may matter; could you share a with/without number? If they show no gain, dropping them removes ≈ 45 source + 30 test lines.
  • Not from this PR: a stuck TQ PUT still has no timeout, as before. With the pipeline, more calls stay frozen while it waits.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5176-who-calls-what.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5176-write-paths.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5176-what-can-shrink.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5176-one-actor-pattern.html

Generated by Claude Code

Base automatically changed from amahishi/ckpt-v2-prefix-batched-io to main November 20, 2026 19:49
@macandro96
macandro96 force-pushed the amahishi/pr4508-prefix-write-pipeline-actor branch from a56b00c to 6e59987 Compare November 20, 2026 20:34
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 6e59987

terrykong
terrykong previously approved these changes Nov 20, 2026
@terrykong
terrykong enabled stacked auto-merge (squash) November 20, 2026 21:09
stacked auto-merge was automatically disabled November 21, 2026 01:40

Pull Request is not mergeable

macandro96 and others added 5 commits November 20, 2026 22:56
The second round of prefix batched I/O review dropped `fail_multi_row`
from the shared `_BatchPrefixSink` fake, but the write-pipeline test still
sets it to force a batch failure and a one-row-at-a-time retry. The flag
was silently ignored and `[batch]` failed on `len(sink.batches) > 1`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
- Move the prefix-write settings from policy.generation.vllm_cfg to
  rollout_recovery.prefix_writes (pipeline, writer_actor and the pipeline
  bounds), passed to setup_token_capture as a required argument. The
  pipeline now implies direct column encoding and takes its row and token
  limits from prefix_write_max_rows/prefix_write_max_tokens.
- Check each setup rule once: buffer bound on the config model, token
  capture/prefix target/async vLLM and TQ simple in the SC validator, text-only
  capture in SC setup. Exclude the block from the bootstrap fingerprint.
- Prepare at most one write ahead of the write in flight, so cut calls stay
  frozen for about two writes rather than the whole queue.
- Write a row above max_batch_payload_bytes alone instead of refusing it,
  matching the non-pipeline path's handling of an oversized row.
- Report the writer actor's data-plane counters with the worker's, and the
  pipeline's write counts under generation_prefix/pipeline_*; drop the
  timing-only fields and per-PUT log lines.
- Fail a cut loudly when the writer actor dies, discarding its partial write
  through the local client; name the writer startup timeout and its cause.
- Split the sink's prefix encoders, move the capture-mode codes and the
  generation-cut key prefix to schema.py, and make direct columns a
  TQTokenSink constructor argument.
- Move the pipeline to prefix_recovery/write_pipeline.py.
- Fix the pyrefly bad-return and mark the pipeline and writer tests nemo_gym
  so the Nemo_Gym lane runs them.
- Add tests for expired, oversized, byte-split, admission-limit, mixed-group
  parity, dead-writer and writer-startup-failure paths, and a functional
  variant of the prefix-recovery parity test with the pipeline and writer
  actor on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
…r actor

Address the second round of review on the generation-prefix write path:

- The write pipeline is the only path for generation-prefix cuts; its
  bounds are fixed module constants (PREFIX_WRITE_LIMITS) and the
  rollout_recovery.prefix_writes config is removed.
- Prefix PUTs go through the CPU writer actor whenever the data plane is
  TransferQueue, on any backend; a local data plane writes in-process.
  The actor's client owns no shared-memory segment (segment_size=0).
- Remove the direct column encoder; the sink keeps one prefix encoder.
- Lift the text-only restriction: media capture cuts go through the
  pipeline and writer actor, with a row-parity test.
- A failed batch write falls back to writing each row alone exactly
  once; a dead writer re-raises PrefixWriterDiedError.

Signed-off-by: Anish Mahishi <[email protected]>
…n and its snapshot test

Shutdown can run on a worker whose token-capture state was never built, as
it already does for _prefix_recovery. The frontend-snapshot test's fake
worker now carries _prefix_writer like a real one.

Signed-off-by: Anish Mahishi <[email protected]>
@macandro96
macandro96 force-pushed the amahishi/pr4508-prefix-write-pipeline-actor branch from 6e59987 to 9d9d34e Compare November 21, 2026 07:01
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 9d9d34e

@terrykong
terrykong enabled stacked auto-merge (squash) November 21, 2026 07:42
@terrykong
terrykong merged commit 6331e5b into main Nov 21, 2026
99 of 101 checks passed
@terrykong
terrykong deleted the amahishi/pr4508-prefix-write-pipeline-actor branch November 21, 2026 08:57
yfw added a commit that referenced this pull request Nov 24, 2026
…kind-advantages

Brings in 8 commits, including the BaseModel environment configs (#5234),
the BaseModel EvalConfig (#4909), the pipelined prefix writer (#5176) and the
self-review status check (#5348). No conflicts. The two GRPO configs pick up
main's new math verifier_type key next to this PR's masked_reward_policy
comments.

Main's ledger schema is still 8, so failure_kind stays in schema 9. The Gym
pin is the same on both sides (9e2e5d0ad0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants