Skip to content

perf(data_plane): stop the TQ controller rescanning nested shapes on clear - #5141

Merged
ZhiyuLi-Nvidia merged 7 commits into
mainfrom
zhiyul/tq-remove-samples-no-collapse
Nov 18, 2026
Merged

ZhiyuLi-Nvidia merged 7 commits into
mainfrom
zhiyul/tq-remove-samples-no-collapse

Conversation

@ZhiyuLi-Nvidia

@ZhiyuLi-Nvidia ZhiyuLi-Nvidia commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What

Follow-up to #4769. FieldMeta.remove_samples (TQ 0.1.11) rescans every live shape of a nested field to decide whether the field can collapse to non-nested. #4769 throttled that to once per 1/8 of live samples removed, but each rescan is still O(live samples) on the controller's only request thread. Every request type queues behind it.

This PR overrides remove_samples to skip the nested-to-regular relabel. It is exactly the stock method minus that block, so a clear costs O(cleared rows).

Why dropping the collapse is safe

Nothing that needs the collapse reads it:

  • Storage rebuilds tensors from the stored rows, not from the schema flag (storage/managers/base.py process_field, simple_storage_manager.py _pack_field_values).
  • KV backends read shapes through BatchMeta.get_shapes, which returns correct per-sample shapes in both forms.
  • NeMo-RL densifies uniform rows by row shape in transfer_queue.py:_from_wire. It never reads field_schema or is_nested otherwise.

Warning

Behavior change: this drops a documented TQ feature. Stock TQ relabels a nested field as regular once its odd-shaped rows are cleared. This override keeps it nested. TQ's own tests/test_controller_data_partitions.py::test_clear_data_nested_field_becomes_regular fails under the override; the other 48 tests in that file pass. The shapes a client receives do not change. Checked: rows of shape 256, 256 and 128; clear the 128 row; add a 512 row. Both stock and this PR serve [(256,), (256,), (512,)].

Nothing in NeMo-RL reads the label. Readers of row shapes, including NeMo-RL's own Mooncake read path (_get_tensors_thread_worker in transfer_queue.py), get them from BatchMeta.get_shapes. That serves the same shape for every row under either label. Only nemo_rl/data_plane may import transfer_queue, so no other code can read TQ metadata. test_only_the_data_plane_imports_transfer_queue now enforces that rule, which interfaces.py already stated but no test checked.

Benchmarks

Setup: one TQ 0.1.11 controller over real ZMQ, 64 clients, 300k live rows on average, 8 nested + 8 flat fields. Each client puts 16-row groups and clears 16 keys per cycle (KV_RETRIEVE_META → MARK_CLEARING → CLEAR_META). Clears are desynchronized across clients, the measured window is 20 s, and the node is an exclusive CPU node.

Variant Max achieved req/s CLEAR_META p99 / max Max of any request
stock 4 79,997 ms / 79,997 ms 79,997 ms
#4769 ~2,890 1,278 ms / 1,314 ms 1,315 ms
this PR ~4,640 20 ms / 26 ms 1,035 ms (GC, see below)

In-process cost of a 16-key clear at 300k live rows: stock p50 797 ms; #4769 p50 0.14 ms with a 686 ms burst; this PR p50 0.13 ms, max 0.23 ms over 3,000 clears.

Not fixed here: a separate ~1 s stall remains. Its timing matches the controller's own GC pause log (1,013 ms GC vs 1,035 ms worst request). With GC frozen after load it drops to ~130 ms. That needs its own change, because freezing at controller start does not cover rows written later.

Tests

tests/unit/data_plane/test_tq_controller_patch.py, 9 tests:

  • the override never collapses
  • it serves the same per-sample shapes as stock across interleaved clears and updates
  • an emptied field resets like stock
  • the started controller installs it, and a clear_meta through the controller keeps the field nested

Guards against the dropped relabel corrupting a run:

  • test_rows_survive_a_clear_that_stock_would_collapse (simple and mooncake_cpu, real adapter). It puts token rows of length 256, 256 and 128 and clears the 128 row, where stock relabels the field as regular. It then clears down to one row, where stock always relabels, puts a 512 row, and asserts every row comes back unchanged at each step. The same sequence on real Ray + TQ 0.1.11 storage: stock reports is_nested=False and this PR reports True, and the rows are identical in both.
  • test_only_the_data_plane_imports_transfer_queue in test_architecture_invariants.py (AST). It passes on today's tree, where all 6 importers are under nemo_rl/data_plane, and fails on a planted import.

Ran against TQ at the pinned commit. ruff 0.9.9 and pyrefly pass on the changed files.

🤖 Generated with Claude Code

…clear

PR #4769 throttled the nested-shape rescan in FieldMeta.remove_samples to once
per 1/8 of live samples removed. The rescan is still O(live samples) on the
controller's only request thread, so it still stalls every request type:
~1.3 s per 20 s window at 300k live rows.

Override remove_samples to never collapse a nested field to non-nested. The
collapse has no reader that needs it: storage rebuilds tensors from the stored
rows, BatchMeta.get_shapes serves correct per-sample shapes in both forms, and
NeMo-RL densifies by row shape in _from_wire. A clear now costs O(cleared
samples), and the per-sample dict is skipped for non-nested fields.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 9, 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.

Zhiyu Li and others added 4 commits November 9, 2026 14:24
…e drops

The override no longer relabels a nested field as regular once its
odd-shaped rows are cleared, which fails TQ's
test_clear_data_nested_field_becomes_regular. State that in the module
docstring, and that code in this repo must not read a field's is_nested or
shape label from TQ metadata.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
… relabel

Add two guards for the remove_samples override:
- test_rows_survive_a_clear_that_stock_would_collapse puts token rows
  through the real adapter on the simple and mooncake_cpu backends, clears
  the odd-length row where stock TQ would relabel the field as regular, adds
  a new length, and checks every row comes back unchanged.
- test_no_code_reads_the_tq_field_label fails if nemo_rl code reads TQ's
  field_schema, get_shapes or is_nested label, which the override leaves
  nested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
With one row left, stock TQ always relabels a nested field as regular.
Clear down to that one row and check it, then add a row of a new length.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
…ld relabel

State in the module docstring and the single-controller guide what the
remove_samples override skips and why it is safe: stock TQ re-checks every
nested field's label on every clear at O(live rows), the relabel does happen
in NeMo-RL, but nothing in NeMo-RL reads the label and data is rebuilt from
the stored rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Nov 9, 2026
@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test e6f977c

…he label

Replace the regex scan for TQ's field label with an AST test that only
nemo_rl/data_plane imports transfer_queue. NeMo-RL never sees TQ's
BatchMeta outside the adapter, so the boundary is the real guarantee, and
the rule in interfaces.py was not enforced by any test.

Say in the docstring and guide that row-shape readers, including the
Mooncake read path, get shapes from BatchMeta.get_shapes, which serves the
same per-row shapes under either label.

Make remove_samples the stock loop minus the relabel block, compare the
stock-equivalence replay once, trim the wiring test to wiring, and reuse
register_train_partition.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test e9d98d1

@ZhiyuLi-Nvidia ZhiyuLi-Nvidia added the CI:L1 Run doctests, unit tests, and functional tests label Nov 10, 2026
@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test d467335

@zyzhou5 zyzhou5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia merged commit 7bd3c24 into main Nov 18, 2026
96 checks passed
@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia deleted the zhiyul/tq-remove-samples-no-collapse branch November 18, 2026 01:20
jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 18, 2026
…Q clear fix) into jthomson04/dynamo-1.7

Brings in NVIDIA-NeMo#5258, NVIDIA-NeMo#4845, NVIDIA-NeMo#5141 and NVIDIA-NeMo#5201. Git merged the six files both sides changed without a conflict. pyproject.toml, uv.lock and the submodule pins are unchanged.

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

Labels

CI:L1 Run doctests, unit tests, and functional tests Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants