nemo_rl.models.generation.prefix_recovery.restore#

Serve a restored call: plan its prompt and budgets, and assemble its reply.

Plain functions over a call’s saved tokens, so any serving path can resume a cut call: the native vLLM chat server today, and the Dynamo frontend (#4979). The vLLM-specific pieces are the detokenizer replay, the finished-call output and the parser wrap, which follow vLLM’s own types.

Module Contents#

Classes#

_RestoredPrefixTerminal

A restored call whose durable output already reached a terminal limit.

_RestoredPrefixParser

Give vLLM’s parser the complete output while capture keeps tail deltas.

RestorePlan

How to serve a call that resumes from its saved generation prefix.

Functions#

_remaining_generation_limits_after_prefix

Return output limits for the suffix after restoring generated tokens.

_bad_words_can_straddle_a_cut

Whether any bad word vLLM enforces spans more than one token.

_generation_cut_blockers

Sampling settings a resumed tail cannot honor across a generation cut.

_count_restored_prefix_as_completion

Report a restored call’s usage the way the uninterrupted call would.

_classify_restored_prefix_terminal

Classify an exact terminal prefix or reject an incompatible restore.

_restored_prefix_terminal_routes

Full-sequence routes for a restored prefix that needs no more decoding.

_build_restored_prefix_terminal_output

Build the final vLLM output for a prefix that needs no more decoding.

_detokenize_restored_prefix

Return the text vLLM produced for a restored prefix the first time.

_full_generator_signature

The signature of vLLM’s chat_completion_full_generator, checked at setup.

plan_restore

Plan a restored call’s engine prompt and remaining budgets.

mark_restored_terminal

Tell the engine client this call’s saved prefix already finished it.

restored_terminal

The finished restored prefix this request task serves, if any.

restore_reply_text

Prepend a resumed call’s restored text to its final output’s text.

restore_reply_usage

Count a resumed call’s restored tokens as completion in its reply.

wrap_parser_for_restore

Give the response parser the whole output, restored prefix first.

Data#

API#

nemo_rl.models.generation.prefix_recovery.restore._remaining_generation_limits_after_prefix(
*,
max_tokens: int,
min_tokens: int | None,
prefix_token_count: int,
) → tuple[int, int | None]#

Return output limits for the suffix after restoring generated tokens.

nemo_rl.models.generation.prefix_recovery.restore._bad_words_can_straddle_a_cut(
sampling_params: Any,
tokenizer: Any,
) → bool#

Whether any bad word vLLM enforces spans more than one token.

vLLM bans a one-token bad word at every step, whatever came before it, so a restored tail bans it exactly as the original call did. A longer one is banned only after its leading tokens appear in the call’s own output, which after a restore no longer holds the prefix. The expansion of each word into token sequences (with and without a leading space) is vLLM’s own.

nemo_rl.models.generation.prefix_recovery.restore._generation_cut_blockers(
sampling_params: Any,
tokenizer: Any,
) → list[str]#

Sampling settings a resumed tail cannot honor across a generation cut.

After a restore the prefix is part of the prompt, but vLLM matches stop strings and multi-token bad words over the output tokens only. A resumed tail would therefore miss a stop string or bad word that straddles the cut. (The chat route refuses presence and frequency penalties outright.)

A request-level seed gives the call its own random generator, seeded when vLLM admits it. The restored call would start that generator over and reuse the draws that chose its prefix. Greedy decoding draws nothing, and an unseeded call draws from the engine’s shared generator, so neither is affected; vLLM’s SamplingType.RANDOM_SEED is exactly the affected case.

nemo_rl.models.generation.prefix_recovery.restore._RESTORED_PREFIX_TERMINAL: contextvars.ContextVar[Optional[nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal]]#

‘ContextVar(…)’

class nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal#

A restored call whose durable output already reached a terminal limit.

prompt_token_ids: tuple[int, ...]#

None

prefix_token_count: int#

None

reason: str#

None

finish_reason: Literal[stop, length]#

None

stop_reason: str | int | None#

None

routed_experts: torch.Tensor | None#

‘field(…)’

property original_prompt_token_count: int#
nemo_rl.models.generation.prefix_recovery.restore._count_restored_prefix_as_completion(
usage: Any,
prompt_token_ids: collections.abc.Sequence[int] | None,
*,
restored_token_count: int,
terminal: nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal | None,
) → list[int] | None#

Report a restored call’s usage the way the uninterrupted call would.

The engine served the restored prefix as part of the prompt, so vLLM counts it as prompt tokens and only the regenerated tail as completion tokens, while the reasoning count covers prefix and tail. Gym adds usage across turns, so the prefix moves back to the completion side. Updates usage in place and returns prompt_token_ids without the prefix.

nemo_rl.models.generation.prefix_recovery.restore._classify_restored_prefix_terminal(
*,
prompt_token_ids: list[int],
prefix_token_count: int,
requested_output_tokens: int | None,
model_max_tokens: int,
terminal_finish_reason: Literal[stop, length] | None = None,
terminal_stop_reason: str | int | None = None,
) → nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal | None#

Classify an exact terminal prefix or reject an incompatible restore.

Gym’s continuation model already guarantees a non-negative prefix count and a stop reason only alongside a finish reason.

nemo_rl.models.generation.prefix_recovery.restore._restored_prefix_terminal_routes(
restored: torch.Tensor,
*,
prev_len: int,
) → torch.Tensor#

Full-sequence routes for a restored prefix that needs no more decoding.

The engine never runs, so nothing routes the positions before the restored delta (the parent’s last token included) or the final token: those rows are missing-route placeholders.

nemo_rl.models.generation.prefix_recovery.restore._build_restored_prefix_terminal_output(
request_id: str,
terminal: nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal,
) → Any#

Build the final vLLM output for a prefix that needs no more decoding.

nemo_rl.models.generation.prefix_recovery.restore._detokenize_restored_prefix(
tokenizer: Any,
*,
original_prompt_token_ids: collections.abc.Sequence[int],
prefix_token_ids: collections.abc.Sequence[int],
terminal: nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal | None,
stop: str | collections.abc.Sequence[str] | None,
min_tokens: int,
include_stop_str_in_output: bool,
skip_special_tokens: bool,
spaces_between_special_tokens: bool,
) → str#

Return the text vLLM produced for a restored prefix the first time.

The prefix is replayed through vLLM’s own incremental detokenizer, primed on the call’s original prompt: a standalone decode drops the leading space of SentencePiece tokenizers. Replaying with the call’s stop settings also reapplies what vLLM stripped from an output that had already stopped: the final stop token (EOS or a stop_token_ids entry) and a matched stop string.

class nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixParser#

Give vLLM’s parser the complete output while capture keeps tail deltas.

delegate: Any#

None

prefix_token_ids: tuple[int, ...]#

None

parse(
model_output: str,
request: Any,
*,
enable_auto_tools: bool,
model_output_token_ids: list[int],
) → Any#
count_reasoning_tokens(
token_ids: collections.abc.Sequence[int],
) → int#
__getattr__(name: str) → Any#
nemo_rl.models.generation.prefix_recovery.restore._FULL_GENERATOR_RESTORE_PARAMETERS#

(‘parser’, ‘request_metadata’)

nemo_rl.models.generation.prefix_recovery.restore._full_generator_signature(
method: collections.abc.Callable[..., Any],
) → inspect.Signature#

The signature of vLLM’s chat_completion_full_generator, checked at setup.

A restored call wraps parser so tool calls and reasoning are parsed from the whole output, and writes the corrected usage to request_metadata. Both are bound by name, so a vLLM that renames or drops either fails here instead of silently on restored calls.

class nemo_rl.models.generation.prefix_recovery.restore.RestorePlan#

How to serve a call that resumes from its saved generation prefix.

engine_prefix_token_ids: list[int]#

None

resumed_generation_token_ids: tuple[int, ...]#

None

output_limit: int#

None

max_tokens: int | None#

None

min_tokens: int | None#

None

nemo_rl.models.generation.prefix_recovery.restore.plan_restore(
*,
prefix_token_ids: collections.abc.Sequence[int],
saved: Any,
continuation: Any,
min_tokens: int | None,
) → nemo_rl.models.generation.prefix_recovery.restore.RestorePlan#

Plan a restored call’s engine prompt and remaining budgets.

Parameters:
  • prefix_token_ids – The call’s context, resolved as for any captured call.

  • saved – The verified saved rows (token_ids_delta/token_mask_delta).

  • continuation – The admission’s generation cut (effective_output_limit and prefix_token_count).

  • min_tokens – The request’s own min_tokens.

nemo_rl.models.generation.prefix_recovery.restore.mark_restored_terminal(
terminal: nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal,
) → None#

Tell the engine client this call’s saved prefix already finished it.

nemo_rl.models.generation.prefix_recovery.restore.restored_terminal() → nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal | None#

The finished restored prefix this request task serves, if any.

nemo_rl.models.generation.prefix_recovery.restore.restore_reply_text(
request: Any,
request_output: Any,
*,
prefix_token_ids: collections.abc.Sequence[int],
engine_prompt_token_ids: collections.abc.Sequence[int],
tokenizer: Any,
) → None#

Prepend a resumed call’s restored text to its final output’s text.

The engine already detokenized the tail with the request’s settings and stripped EOS and any matched stop string from it. Re-decoding the whole sequence would restore both, so only the prefix is replayed, with the same settings, and prepended to the processed tail. A call that had already stopped has no tail, so its stop is stripped here.

nemo_rl.models.generation.prefix_recovery.restore.restore_reply_usage(
response: Any,
*,
restored_token_count: int,
terminal: nemo_rl.models.generation.prefix_recovery.restore._RestoredPrefixTerminal | None,
) → bool#

Count a resumed call’s restored tokens as completion in its reply.

Returns whether the usage changed, so the caller can pass it on.

nemo_rl.models.generation.prefix_recovery.restore.wrap_parser_for_restore(
parser: Any,
prefix_token_ids: collections.abc.Sequence[int],
) → Any#

Give the response parser the whole output, restored prefix first.