nemo_rl.models.generation.prefix_recovery.state#

Per-call capture state and the registry of a worker’s live captured calls.

Module Contents#

Classes#

_CallProgress

A consistent copy of what one call has recorded, taken under its lock.

_FrozenCut

The tokens one cut froze: the chunk to stage and the whole prefix.

_RequestCaptureState

Append-only generated tokens for one in-flight captured request.

_CallLifecycle

Lifecycle changes to one call, usable only while its lifecycle is held.

_CompletedCaptureState

Time-bounded terminal evidence retained across the response/cut race.

CaptureRegistry

In-flight captured calls, indexed by request object and by model call ID.

API#

class nemo_rl.models.generation.prefix_recovery.state._CallProgress#

A consistent copy of what one call has recorded, taken under its lock.

generated_token_ids: list[int]#

None

generated_logprobs: list[float]#

None

staged_token_count: int#

None

staging_keys: tuple[str, ...]#

None

cut_in_progress: bool#

None

terminal_started: bool#

None

effective_output_limit: int | None#

None

terminal_finish_reason: Literal[stop, length] | None#

None

terminal_stop_reason: str | int | None#

None

observation_error: str | None#

None

uncuttable_reason: str | None#

None

terminal_routed_experts: torch.Tensor | None#

None

class nemo_rl.models.generation.prefix_recovery.state._FrozenCut#

The tokens one cut froze: the chunk to stage and the whole prefix.

chunk_key: str#

None

chunk_sequence: int#

None

chunk_token_ids: list[int]#

None

chunk_logprobs: list[float]#

None

generated_token_ids: list[int]#

None

generated_logprobs: list[float]#

None

class nemo_rl.models.generation.prefix_recovery.state._RequestCaptureState(
call: Any,
prompt_token_ids: collections.abc.Sequence[int],
*,
media: Any | None = None,
resumed_generation_token_ids: collections.abc.Sequence[int] = (),
effective_output_limit: int | None = None,
staging_keys: collections.abc.Sequence[str] = (),
routed_experts_dtype: torch.dtype | None = None,
restored_routed_experts: torch.Tensor | None = None,
restored_sampler_topk: tuple[torch.Tensor, torch.Tensor] | None = None,
)#

Append-only generated tokens for one in-flight captured request.

Tokens are recorded lazily: a checkpoint cut reads the tokens vLLM decoded since the last record, and the finished output records the rest. record keeps each position once whichever source reaches it first.

Two locks guard the rest, and callers never take either directly. Every method here takes _lock itself. Lifecycle changes (freezing, sealing or rolling back a cut, starting the terminal write) exist only on the _CallLifecycle that lifecycle() yields while it holds _lifecycle_lock, which a cut keeps across its TQ write. call, prompt_token_ids, media, resumed_generation_token_ids and the router-replay settings never change after begin_call.

Each successful cut stages only the tokens past the last sealed one. The ordered TQ keys remain in Gym lineage; the full list is kept locally to compute the cumulative digest advertised by the latest cut.

Initialization

__slots__#

(‘_call’, ‘_prompt_token_ids’, ‘_media’, ‘_resumed_generation_token_ids’, ‘_routed_experts_dtype’, ‘…

property call: Any#
property prompt_token_ids: list[int]#
property media: Any | None#
property resumed_generation_token_ids: list[int]#
property routed_experts_dtype: torch.dtype | None#
property restored_routed_experts: torch.Tensor | None#
property restored_sampler_topk: tuple[torch.Tensor, torch.Tensor] | None#
property sampler_logprobs: Any#

The logprobs container the call’s recorded tokens were read from.

property recorded_token_count: int#
record(
generated_token_ids: collections.abc.Sequence[int],
generated_logprobs: collections.abc.Sequence[float],
*,
start: int | None = None,
finish_reason: Any = None,
stop_reason: Any = None,
logprobs_container: Any = None,
) → None#

Record tokens read at position start (default: the end).

A cut and the finished output both read from vLLM, so positions already recorded by the other are skipped rather than appended twice. Callers read start from recorded_token_count, and tokens are never removed, so it never lies past the recorded end. finish_reason is how vLLM finished the call, if it has. logprobs_container is the engine container the tokens were read from, kept for sampler_logprobs.

record_terminal_routes(routed_experts: torch.Tensor) → None#

Keep the full-sequence routes of the call’s final output.

record_error(error: str) → None#

Keep the call out of cuts: its recorded tokens disagree with vLLM’s.

mark_uncuttable(reason: str) → None#
set_effective_output_limit(limit: int) → None#

Keep vLLM’s resolved output budget; a restored cut’s own one wins.

snapshot() → nemo_rl.models.generation.prefix_recovery.state._CallProgress#
lifecycle() → collections.abc.Iterator[nemo_rl.models.generation.prefix_recovery.state._CallLifecycle]#

Own this call’s lifecycle until the block ends.

try_lifecycle() → collections.abc.Iterator[nemo_rl.models.generation.prefix_recovery.state._CallLifecycle | None]#

Like lifecycle, but yield None at once if a cut or write owns it.

_owned() → collections.abc.Iterator[nemo_rl.models.generation.prefix_recovery.state._CallLifecycle]#
_progress() → nemo_rl.models.generation.prefix_recovery.state._CallProgress#
class nemo_rl.models.generation.prefix_recovery.state._CallLifecycle(
state: nemo_rl.models.generation.prefix_recovery.state._RequestCaptureState,
)#

Lifecycle changes to one call, usable only while its lifecycle is held.

_RequestCaptureState.lifecycle is the only way to get one, and it stops working when that block ends, like DataPlaneMutationCut.

Initialization

__slots__#

(‘_state’, ‘_live’)

_invalidate() → None#
_require_live() → nemo_rl.models.generation.prefix_recovery.state._RequestCaptureState#
property terminal_started: bool#
property staging_keys: tuple[str, ...]#
begin_terminal() → tuple[str, ...]#

Start the call’s terminal write; return the chunk rows it retires.

freeze(
checkpoint_id: str,
) → tuple[nemo_rl.models.generation.prefix_recovery.state._CallProgress, nemo_rl.models.generation.prefix_recovery.state._FrozenCut | None]#

Freeze the recorded length for a cut, if the call can be cut.

Returns what the call had recorded, and the frozen chunk and prefix, or None when the call is uncuttable, its record is inconsistent, or vLLM has not resolved its output limit.

seal(checkpoint_id: str, staged_key: str | None) → None#

Count a staged cut’s tokens as written to TQ and keep its row key.

absorb_engine_route_pieces(
pieces: collections.abc.Sequence[Any],
) → torch.Tensor#

Join the engine’s new route pieces; return the request’s routes so far.

rollback(checkpoint_id: str) → None#

Abandon a cut; its tokens stay unstaged for the next one.

_frozen_count(checkpoint_id: str) → int#
class nemo_rl.models.generation.prefix_recovery.state._CompletedCaptureState#

Time-bounded terminal evidence retained across the response/cut race.

coords: Any#

None

generation_token_count: int#

None

effective_output_limit: int | None#

None

terminal_finish_reason: Literal[stop, length] | None#

None

terminal_stop_reason: str | int | None#

None

completed_at_monotonic: float#

None

class nemo_rl.models.generation.prefix_recovery.state.CaptureRegistry#

In-flight captured calls, indexed by request object and by model call ID.

The worker’s request path uses the first index; a checkpoint cut uses the second, because Gym’s inventory names calls by model call ID.

Initialization

add(
request: Any,
state: nemo_rl.models.generation.prefix_recovery.state._RequestCaptureState,
) → None#
get(
request: Any,
) → nemo_rl.models.generation.prefix_recovery.state._RequestCaptureState | None#
__contains__(request: Any) → bool#
__len__() → int#
pop(
request: Any,
) → nemo_rl.models.generation.prefix_recovery.state._RequestCaptureState | None#
get_by_model_call_id(
model_call_id: str,
) → nemo_rl.models.generation.prefix_recovery.state._RequestCaptureState | None#