Repository navigation
feat(token-capture): low-overhead metrics for capture operations, outcomes, digests, and caches - #4218
Open
ananthsub wants to merge 2 commits into
Open
feat(token-capture): low-overhead metrics for capture operations, outcomes, digests, and caches#4218ananthsub wants to merge 2 commits into
ananthsub wants to merge 2 commits into
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
ananthsub
force-pushed
the
ananthsub/token-capture-telemetry
branch
from
October 8, 2026 16:41
9a32a09 to
e8d7525
Compare
ananthsub
marked this pull request as ready for review
October 8, 2026 16:48
Contributor
ananthsub
force-pushed
the
ananthsub/token-capture-telemetry
branch
from
October 8, 2026 17:53
e8d7525 to
8d65063
Compare
Contributor
Author
|
/ok to test 8d65063 |
ananthsub
force-pushed
the
ananthsub/token-capture-telemetry
branch
from
October 8, 2026 18:07
8d65063 to
6a2b334
Compare
ananthsub
force-pushed
the
ananthsub/token-capture-telemetry
branch
from
October 14, 2026 18:39
64f2ab8 to
c0c1498
Compare
3 of 5 tasks
This was referenced Oct 22, 2026
7 tasks done
…comes, digests, and caches Time token-capture operations at protocol call sites and inside Gym's file stores, count capture outcomes and digest work in process memory, and report cache sizes at export. Recording is off unless a metrics provider exists. Rebased onto main after the package moved to nemo_gym.models.token_capture and rollout ids became capture keys. The ledger failure sites added on main (engine refusals, uncommitted calls in model-call capture) go through record_ledger_failure, and framework-owned admission times its ledger manifest read and call-intent write. Cache owners are registered and copied under a lock, so a store built on a capture thread during an export no longer blanks the cache metric for that export. Squashes the four reviewed commits: - feat(token-capture): low-overhead metrics for capture operations, outcomes, digests, and caches - refactor(token-capture): align metric names with the capture vocabulary - feat(token-capture): time manifest, retire, and delete, and address review - fix(token-capture): record through any meter provider in the process Signed-off-by: Ananth Subramaniam <[email protected]>
ananthsub
force-pushed
the
ananthsub/token-capture-telemetry
branch
from
November 9, 2026 23:23
0025f42 to
3f6f65b
Compare
…e the remaining sites Address the team review of the token-capture metrics: - Count Gym's own tool_call_turn resolution reason instead of folding it into "other". - Count writes refused for a retired rollout, by the token store or a ledger, as late_write_dropped/retired, and count poisoned where Gym's ledgers write the failure row, so replays and dropped rows are not counted. - Add an optional TokenSource.drop_with_reason extension, implemented by the token store, so retire_skipped says stale_snapshot or not_frozen; sources without it are counted with no reason. - Stage records through the private builder, so ordinary staging is no longer also timed as a checkpoint cut, and restore the public builders' signatures and docstrings. - Count the unclassified-request incomplete mark, and time the checkpoint ledger retire and the file ledger's in-thread failure write. - Label ledger and lineage lock waits with the store that waited, add the implementation to the token totals, and copy the lineage index before summing it at export. - Bring the metrics docs and module docstring in line with the code. Signed-off-by: Ananth Subramaniam <[email protected]>
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed and why
Training-token capture runs on every captured model call, yet it emits no OpenTelemetry. In aggregate across a run there is no way to see where its time goes, how often calls are poisoned or masked, how much hashing it repeats, or how large its caches grow. Performance work such as #4213 has to be measured with an external harness, and a regression in it would go unnoticed.
This PR adds a small set of token-capture metrics next to Gym's other attributed metrics, built so they cannot hurt training throughput.
gym.token_capture.operation.duration_mslt_4k,lt_32k,lt_128k,ge_128k; absent without a record),error(errororcancelled, only when the operation did not complete)gym.token_capture.operation.tokens_totalgym.token_capture.outcome_totalgym.token_capture.digest.calls_total,.bytes_totalgym.token_capture.cache.sizeOperations timed:
TokenSink,TokenSource,LineageResolver,CaptureLedger,StagingSink):lineage.resolve,sink.begin_call,sink.put,ledger.record,ledger.record_failure,staging.build_record,staging_sink.stage,source.freeze,source.drop, the control routes' and checkpoint retirement'sledger.manifest,ledger.retire, andledger.delete, and, when a training framework selects the parent call,ledger.manifestandledger.begin_callat admission. Each is labelled with the implementing class, so framework-provided implementations (for example NeMo RL's staging sink and source) are measured without changes to them. These timings include waiting for a worker thread.cut.build_prefix_recordandcut.build_chunk_record. Ordinary staging goes through the private builder and is timed only asstaging.build_record.sink.put.in_thread,ledger.record.in_thread,ledger.record_failure.in_thread,ledger.manifest.in_thread,ledger.retire.in_thread, andledger.delete.in_thread, named after the call-site operation they pair with (the difference is thread-pool queueing), andfile_lock.waitonly when acquiring the lock took at least 0.1 ms, labelled with the store that waited (a ledger sharing the token store's lock file reports as the ledger).Operations are named
<role>.<method>after the protocol they time. Outcomes reuse the names the capture code already uses, each with a reason from a fixed set:parent_root,parent_resolved(tool_call_turnwhen matched on the turn's tool calls), andparent_unresolved(the resolver's reason).poisoned, with the ledger failure reason. Gym's ledgers count it when they write the failure row, so a replayed row or one dropped for a retired rollout is not counted.capture_failed:writeorbuildon the model server;build,staging_sink_error, orstaging_sink_rejectedon the inference worker, matching the worker'scapture_faileddisposition.late_write_dropped:frozen, orretiredwhen the token store or a ledger refused a write for a retired rollout. A dropped ledger row leaves staged tokens no manifest names.marked_incomplete: no reason after a capture failure,unclassified_requestfor a request the model server could not classify.retire_skipped: why the source refused the drop,stale_snapshotornot_frozen, from sources that implement the optionaldrop_with_reasonextension (Gym's token store does). Other sources are counted with no reason.lock_acquired:sharedorexclusive.gym.token_capture.operation.tokens_totalcarries the operation and the implementation, so milliseconds per token can be compared per implementation. The call sites reach the instruments throughnemo_gym.models.token_capture.capture_metrics; the name avoids the per-rolloutmetricsdictionaries this package already uses.Every ledger poisoning site, including the engine-refusal and uncommitted-call sites on
main, now goes through onerecord_ledger_failurehelper inmodels/token_capture/sink.py, which times the write; the ledger counts the row it writes.Docs: the metrics table and a "Token-Capture Metrics" section in
observability/metrics.mdx.How it works
flowchart LR A["Agent"] --> M["Policy model server<br/>lineage.resolve, sink.put,<br/>ledger.record, ledger.record_failure"] M --> W["Inference worker<br/>staging.build_record, staging_sink.stage,<br/>digest counts"] C["Consumer<br/>source.freeze, source.drop"] --> F M --> F[("Capture files and caches<br/>sink.put.in_thread, ledger.record.in_thread,<br/>ledger.manifest.in_thread, file_lock.wait, cache.size")] W --> T[("Framework token store")] M -. "OTLP" .-> O["Per-job collector<br/>to the dashboard backend"] W -. "OTLP" .-> O C -. "OTLP" .-> OKeeping it off the critical path:
nemo_gym.telemetryuntil a process has set telemetry up, so importing capture code stays cheap and the package stays a leaf.other. Implementation labels are read from the class on each call, so no reference to a class is kept. Cache owners are held weakly, and one owner failing to report does not hide the others.error.Measured per call on a workstation:
A captured model call records about 5 to 10 measurements, well under 0.1 ms against model calls of 100 ms or more.
Measuring #4213 with these metrics
As a check that the metrics show real optimizations, #4213's parent (
ba18d3dae) and #4213 (b2fde6b54) were each run with this commit applied, plus timing of #4213's checkpoint-cut builders and prefix cache, on a synthetic workload: 24 calls with 2,048 to 65,536 prompt tokens, six checkpoint cuts each, then a completion, three repetitions. Every number came from these instruments through an in-memory reader.#4213 has since landed, and the tests now assert one staging digest per record, the behavior on
main. They also show that a staged record still computes its extras digest twice, once in the builder and once in the record's validator.What callers and operators should know
ledger.manifestminusledger.manifest.in_threadis time waiting for a worker thread,file_lock.waitis time waiting for the ledger lock, and the rest of the in-thread time is the read. The framework-side wait for a pooled connection isgym.http.connection_pool.queue_duration_ms, already onmain.Compatibility
nemo_gym.models.token_capture.sink.record_ledger_failure; the internal ledger poisoning sites call it.TokenCaptureStore.appendandFileLineageStore._recordnow delegate to_appendand_record_row; behavior is unchanged.TokenSourceextensiondrop_with_reason(rollout_id, *, snapshot_id, version) -> tuple[bool, str], whichTokenCaptureStoreimplements anddropdelegates to. It is opt-in: a source without it keeps working and its refusals are counted with no reason. ATokenCaptureStoresubclass that overridesdropmust also overridedrop_with_reason, as its docstring says.RolloutTokenCapture.build_prefix_recordandbuild_generation_chunk_recordkeep their public keyword-only signatures and docstrings; they now time themselves as checkpoint cuts.TokenCaptureStore._lockedtakes an optionalwaiterfor the lock-wait label.Issue
No tracking issue. Motivated by the need to measure token-capture performance work such as #4213 in aggregate.
Validation
Rebased onto
mainatf49b582a10, after the capture package moved tonemo_gym/models/token_capture/and rollout ids became capture keys. The four reviewed commits are squashed into one, because every one of them touched the call sites the move rewrote. The imports use the new paths, not the deprecation shim. Ledger poisoning sites added onmainsince the PR opened go throughrecord_ledger_failure: engine refusals insink.py, and uncommitted calls inmodel_call_capture.py, which replaced the oldbase_responses_api_model.pysite. Framework-owned admission, also new onmain, times its ledger manifest read and its call-intent write.The tests run against a real OpenTelemetry in-memory metric reader, and each capture method is exercised end to end for every metric family:
file_lock.waitsample of the hold's length in milliseconds.file_lock.waitsample.Each new test fails on the code without this PR. On
mainthe test module does not import. With only the two metric modules added tomain's capture code, every new end-to-end test fails on its first missing measurement. The redundant-freeze test fails without its timer.Fixed in this round:
source.freezeon the path for a rollout that already carries token ids is timed like the main path.Commands, in one venv for this worktree synced with the full-suite extras (nemo-lens and the OpenTelemetry SDK installed, so the gated telemetry code runs and the lens tests do not skip):
Results:
mainatf49b582a10with this branch's tests: the harbor run-summary tests, the Docker PTY pid-record tests (no user namespaces here), and the benchmark listing (the host name resolves to 127.0.1.1).pre-commit runon the changed files passed, including the import-linter layering check.Team review round
A team review of the rebased commit found eleven issues and three follow-ups, all fixed in the second commit:
tool_call_turnparent resolution was counted asother.poisoned.frozen.retire_skippedcalled every refused drop a stale snapshot (now the opt-in extension above).Each fix has a test that fails on the first commit and passes on the second. Five mutations that the first commit's tests let through, such as the size-class bound and the slow-operation threshold, now fail a test. The reviewer re-ran its mutations of the fixes against the second commit, and every one failed a test.
A note on one existing test: the disabled-site cost test compares two timings in one process. On this host it failed once in six runs under six test workers while other sessions held the load average near 90, and it passes alone under the same load.
Rollout evidence
N/A: the metrics are off unless an exporter is configured and do not change capture behavior. The synthetic #4213 measurement above exercises the instrumented paths end to end through the OpenTelemetry SDK.