nemo_gym.token_id_capture.builder
nemo_gym.token_id_capture.builder
Build trainable trajectories from a frozen token capture.
The builder consumes TokenEntry records from a TokenCaptureSnapshot.
The snapshot is frozen before the consumer passes its entries to the builder.
per_request creates one training sequence per call.
It does not infer relationships between calls.
It can return multiple trajectories.
prefix_merging chains calls by token-prefix relationships.
Each call uses the earlier call with the longest matching cumulative sequence as its parent.
The cumulative sequence contains the prompt and generation.
This strategy rebuilds an append-only rollout as one chain.
A rewritten or compacted prompt starts a new root.
Identical candidate parents are ambiguous.
The builder quarantines the ambiguous subtree.
Both strategies are independent of capture order.
prefix_merging processes entries by increasing prompt length.
This order comes from the tokens.
A parent’s prompt is shorter than its child’s prompt.
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.
Index cumulative token sequences for linear-time parent lookup.
Infer the parent from the longest cumulative prefix.
This is the fallback when no verified parent link exists. Identical cumulative sequences are ambiguous. The caller quarantines an ambiguous subtree.
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.