NVIDIA-NeMo/RL #5176, part 11 of 11 in the partial-checkpointing stack.
Only the PR's own commit counts: 788133a..981376a, +2,281 / −49 (GitHub shows more because the base branch moved).
Permalinks pin the PR head 981376a.
Four pages on this PR. This one: how the new write path runs next to the old one, as a timeline and as pseudocode.
Today, run_cut hands each inventory to one control thread, which runs
checkpoint_cut from start to end.
The PR turns the cut into a generator that stops at each write
(keys = yield PrefixWrite(...)).
With the pipeline off, checkpoint_cut drives that generator itself, so the old behavior stays.
With it on, _advance prepares on one thread and
_write writes on another, merging rows from different inventories into one write.
The cut, before (#4508)
# serving event loop -> one control thread per worker async def run_cut(inventory): return await loop.run_in_executor(control_thread, cut, inventory) # control thread: the whole cut runs here, start to end def cut(inventory): for call in inventory.calls: tx = cut_transaction(call) # generator; holds call.lifecycle_lock pending.append(tx, row) if pending is full: flush() # 256 rows / 4M tokens flush() def flush(): keys = stage(pending) # encode + TQ put, waits for the ack for tx, key in zip(pending, keys): tx.send(key) # seal the row for tx in pending: tx.send(None) # build the ack, release the lock
Real code: run_cut, checkpoint_cut, _cut_batched (at 788133a).
The cut, after (this PR): the same steps, paused at each write
# serving event loop async def run_cut(inventory): if pipeline: return await pipeline.submit(inventory) # pipeline on return await loop.run_in_executor(control_thread, cut, inventory) # pipeline off # whoever drives it: the control thread (off) or the prepare thread (on) def cut_steps(inventory): # now a generator for call in inventory.calls: tx = cut_transaction(call) # holds call.lifecycle_lock pending.append(tx, row) if pending is full: yield from flush() def flush(): keys = stage(pending) keys = yield PrefixWrite(rows, fallback=stage) # pause; locks stay held for tx, key in zip(pending, keys): tx.send(key) # seal, back on this thread for tx in pending: tx.send(None) yield BatchReleased() # locks released def cut(inventory): # pipeline off: drive it inline for req in steps: steps.send(req.fallback()) # fallback = stage()
Real code: run_cut, checkpoint_cut, _checkpoint_steps, flush.
New: the two pipeline threads and the writer actor (all new code)
# prepare thread, one per worker (owns every generator and lock) def _run(): loop: if a write finished: _complete(jobs) # send keys -> seal elif rows waited 10 ms: _writers.submit(_write, _collect()) elif buffer has room: _advance(next inventory) # run to next yield # writer thread, one per worker (one write in flight) def _write(jobs): # rows of several inventories try: keys = sink.stage_generation_prefix_batch(all rows) except: keys = [job.fallback() for job in jobs] # each inventory alone # writer thread, inside the sink (writer actor on) cols = encode_prefix_groups(rows) # one TensorDict ref = ray.put(PrefixPut(pack_columns(cols))) # NumPy byte buffers ray.get(writer_actor.put.remote(ref)) # blocks until TQ acks # PrefixWriterActor process (CPU only, same node) def put(batch): fields = unpack_columns(batch.columns) own_tq_client.put_samples(keys, fields, tags) # the TQ write
Real code: PrefixWritePipeline._run, _write, RayPrefixWriterClient.put_samples, PrefixWriterService.put.
A call's lock is taken and released only on the prepare thread; the writer thread and the actor only move bytes. The generator can pause while it holds locks, which is why the pipeline caps how many prepared batches wait (the byte budget).
| Old path (#4508) | New path (#5176, all three pieces on) | |
|---|---|---|
| Who runs the cut | One control thread, start to end | Prepare thread owns every call's lock; writer thread only writes. A write's rows are sealed back on the prepare thread. |
| Rows per write | One inventory, up to generation_prefix_batch_size (256) | Rows from several inventories, up to the smaller of that and max_batch_rows (also 256) |
| Encoder | _encode_record per row | encode_prefix_groups per batch; same columns and tags (a parity test checks this) |
| Who talks to TQ | The worker's client | A CPU actor on the same node, reached through NumPy byte buffers |
| Oversized single row | Written alone | Fails its cut and regenerates (documented) |
They meet at stage_generation_prefix_batch: same staging keys, same columns, same seal loop.
Before that point the difference is only when rows are prepared and who sends them.
The measured gain (single runs, prefix cut + save): 52.4 s → 31.7 s with the encoder and pipeline, → 25.2 s with the actor added.
🔴 Review comments: C2 · drop packingC15 · no settingsC1 · tests never run in CIC10 · end-to-end run