nemo_gym.token_id_capture.builder
nemo_gym.token_id_capture.builder
Build trainable trajectories from a frozen token capture.
The consumer freezes a TokenCaptureSnapshot before passing its TokenEntry records here.
The builder can return multiple trajectories when a rollout contains independent call chains.
Each current record states whether the call starts a root, continues a verified parent, or has unresolved ancestry. The builder verifies every resolved parent against the child’s prompt tokens before joining the calls. An unresolved call begins a separate fragment. The builder never infers a parent across that boundary.
prefix_merging uses token-prefix matching only when a verified parent is absent from the frozen snapshot.
For example, capture can filter a parent that generated no tokens.
In that case, prefix matching may reconnect the child to a verified surviving ancestor.
The build does not depend on capture order.
prefix_merging processes entries by increasing prompt length because a parent’s prompt is shorter than its child’s.
Loss masks follow token provenance. Policy-generated tokens have a mask value of 1 and retain their captured log probabilities. Prompt tokens have a mask value of 0.
Module Contents
Classes
Functions
Data
API
Describe what the build kept, dropped, or could not resolve.
The consumer converts these fields into run metrics. Typed fields prevent renamed keys from appearing as zero values on dashboards.
Require one log probability for each generated token.
A trainer cannot use a chain with mismatched token and log-probability counts.
Avoid building a token-prefix index when every parent is present.
Every supported entry carries a request-time parent decision. Only missing-parent recovery needs the trie.
Index cumulative token sequences for linear-time parent lookup.
Rebuild full prompts for delta records by walking their parent chains.
Return full-prompt entries and call ids with broken chains.
Find this call’s parent.
New records preserve the request-time parent decision.
Token-prefix matching recovers a resolved link whose parent is absent from this build.
This can happen when the parent had an empty generation and was filtered out.
note reports why the recorded link was not used as recorded.
Require each generated item to extend all preceding tokens.
The preceding tokens include the prompt and all prior generations.
Raise AssertionError when the response is not contiguous.
Project a chain into Responses output items with contiguous prompts.
Preserve captured assistant text and tool calls. Put the contiguous prompt on the item that carries each generation. Each generated item’s prompt extends the previous generated item. Preserve text for downstream scoring. Create a token-only item only when a call has no captured content items.
Rebuild the main chain as a Responses object whose output items are contiguous.
The result is a Gym-native Responses payload.
It contains object: "response", output items, and usage.
Token fields describe one unbroken sequence across the rollout.
The sequence combines items from multiple model calls.
Chain frozen snapshot entries with the named strategy.
terminal_call_id anchors chain selection for prefix_merging.