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#
A restored call whose durable output already reached a terminal limit. |
|
Give vLLM’s parser the complete output while capture keeps tail deltas. |
|
How to serve a call that resumes from its saved generation prefix. |
Functions#
Return output limits for the suffix after restoring generated tokens. |
|
Whether any bad word vLLM enforces spans more than one token. |
|
Sampling settings a resumed tail cannot honor across a generation cut. |
|
Report a restored call’s usage the way the uninterrupted call would. |
|
Classify an exact terminal prefix or reject an incompatible restore. |
|
Full-sequence routes for a restored prefix that needs no more decoding. |
|
Build the final vLLM output for a prefix that needs no more decoding. |
|
Return the text vLLM produced for a restored prefix the first time. |
|
The signature of vLLM’s |
|
Plan a restored call’s engine prompt and remaining budgets. |
|
Tell the engine client this call’s saved prefix already finished it. |
|
The finished restored prefix this request task serves, if any. |
|
Prepend a resumed call’s restored text to its final output’s text. |
|
Count a resumed call’s restored tokens as completion in its reply. |
|
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,
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,
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,
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
seedgives 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’sSamplingType.RANDOM_SEEDis 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,
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
usagein place and returnsprompt_token_idswithout 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,
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,
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,
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,
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_idsentry) 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],
- count_reasoning_tokens(
- token_ids: collections.abc.Sequence[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],
The signature of vLLM’s
chat_completion_full_generator, checked at setup.A restored call wraps
parserso tool calls and reasoning are parsed from the whole output, and writes the corrected usage torequest_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,
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_limitandprefix_token_count).min_tokens – The request’s own
min_tokens.
- nemo_rl.models.generation.prefix_recovery.restore.mark_restored_terminal( ) 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,
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,
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],
Give the response parser the whole output, restored prefix first.