Skip to content

perf(vllm): derive required prefix token IDs in Gym - #2836

Draft
jthomson04 wants to merge 2 commits into
NVIDIA-NeMo:mainfrom
jthomson04:perf/native-vllm-prefix-pinned
Draft

jthomson04 wants to merge 2 commits into
NVIDIA-NeMo:mainfrom
jthomson04:perf/native-vllm-prefix-pinned

Conversation

@jthomson04

@jthomson04 jthomson04 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add an opt-in derive_prefix_token_ids_from_messages flag (default false) to the vLLM model config.
  • When it's on, send the newest assistant message's prompt_token_ids + generation_token_ids as required_prefix_token_ids before forwarding a native vLLM request.
  • Only the newest assistant message counts. If it has no token bundle, no prefix is sent; Gym never falls back to an older turn.
  • Keep a prefix the caller supplied explicitly, and omit the field on the first turn.
  • Reuse the existing strict token-bundle validation, so partial assistant metadata still fails loudly.

This is the Gym side of a native NeMo-RL vLLM request-path optimization. It does not change stored session messages or response token metadata.

Linked NeMo-RL PR: NVIDIA-NeMo/RL#3892. #1784 stacks on this PR.

Why message-derived prefixes alongside supply_prefix_token_ids

  • Prefix supply resolves a verified parent from Gym's capture lineage ledger and needs generation-time proof.
  • Here, the caller already sends exact token IDs on the assistant messages it replays (return_token_id_information), so no ledger lookup is needed.
  • This removes NeMo-RL's per-request message scan and gives the Dynamo wrapper the exact prefix.

Rules

  • Off by default. Stock vLLM ignores unknown request fields (it logs them at debug level since v0.18). Dynamo's OpenAI frontend rejects them with Unsupported parameter(s). So the field is only sent when the backend splices the prefix. NeMo-RL's vLLM server and Dynamo token wrapper do.
  • Config validation requires return_token_id_information=true. It rejects the flag together with supply_prefix_token_ids (that mechanism owns the field when enabled) or with use_completions_api=true.
  • External staging: the field is never placed in an external-staging body, and it never sets prefix_requested. Prefix-supply verification is unchanged.

Tests

  • Rebased on current main; responses_api_models/vllm_model/tests/ gives 418 passed.
  • The 7 new tests cover:
    • the flag is off by default;
    • no fallback to an older assistant's prefix;
    • the prefix reaches both chat completions and /tokenize over HTTP;
    • the field is never put in an external-staging body;
    • the three config rejections.
  • pre-commit passes on the changed range.

Validation

Before the rebase, the combined NeMo-RL and Gym change passed a real Nano V3.5 TP4 smoke with first-turn and multi-turn requests. The full 48-GPU native vLLM 0.26 workload completed four optimizer steps with 144 trajectories per step and no prefix fallback.

@copy-pr-bot

copy-pr-bot Bot commented Aug 28, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

jthomson04 and others added 2 commits October 12, 2026 09:08
…newest turn

Gate required_prefix_token_ids derivation behind a new
derive_prefix_token_ids_from_messages flag (default false). Stock vLLM
ignores the field, but strict frontends such as Dynamo reject unknown
request fields, and the field is also forwarded to /tokenize.

Keep the flag apart from supply_prefix_token_ids, which owns the field
when enabled, and never put the token array in an external-staging body.

Derive only from the newest assistant message. If it has no token
bundle, send no prefix instead of falling back to an older turn's
tokens, which may no longer be a prefix of the prompt.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jthomson04
jthomson04 force-pushed the perf/native-vllm-prefix-pinned branch from 0ebcb5b to 9691c00 Compare October 12, 2026 16:34
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 12, 2026 16:34
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 12, 2026
Pin 3rdparty/Gym-workspace/Gym to 9691c00178df (NVIDIA-NeMo/Gym#2836,
two commits on Gym main 75bb53e6d4). That PR adds the opt-in
VLLMModelConfig.derive_prefix_token_ids_from_messages flag, which sends
the newest assistant turn's exact prompt + generation tokens as
required_prefix_token_ids.

Relocked with uv 0.11.28, the version CI installs. Every lock change
comes from Gym's own pyproject: aiohttp>=3.14.4 (sglang keeps 3.14.3,
so aiohttp forks), anyio>=4.15.1, PyJWT>=2.15.1, jinja2, jsonschema, and
the dropped mlflow/mlflow-skinny dependencies.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 12, 2026
NVIDIA-NeMo/Gym#2836 makes Gym's message-derived required_prefix_token_ids
opt-in through VLLMModelConfig.derive_prefix_token_ids_from_messages
(default false). NeMo-RL's native vLLM HTTP server implements that field,
so setup_nemo_gym_config now turns the flag on in the env.nemo_gym
policy_model overlay when the generation backend is vllm. Dynamo is left
to its own PR, and megatron is unchanged.

- An explicit derive_prefix_token_ids_from_messages in the overlay wins.
- The flag stays off when the overlay sets supply_prefix_token_ids, which
  owns the same field and which Gym rejects alongside it.
- A sharded job without a policy_model overlay gets the flag through
  common_overrides. A new top-level overlay would have no shard owner and
  fail the shard plan.
- Token capture turns off return_token_id_information on the actor, and
  Gym rejects derivation without it, so the actor now turns the flag off
  there too. External staging supplies the prefix from the ledger.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 12, 2026
When a Gym request has no required_prefix_token_ids, the native server
scanned back for any message carrying prompt_token_ids. If the newest
assistant message had no tokens (it was injected, edited, or
regenerated), the scan used an older turn's tokens, which are no longer a
prefix of the rendered prompt. NVIDIA-NeMo/Gym#2836 fixed the same bug in
Gym's derivation.

Match Gym's rule: look only at the newest assistant message, and send no
prefix when it has no token information. The rule lives in
_message_prefix_token_ids so it can be tested without the vLLM request
classes.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 13, 2026
Pin 3rdparty/Gym-workspace/Gym to 9691c00178df (NVIDIA-NeMo/Gym#2836,
two commits on Gym main 75bb53e6d4). That PR adds the opt-in
VLLMModelConfig.derive_prefix_token_ids_from_messages flag, which sends
the newest assistant turn's exact prompt + generation tokens as
required_prefix_token_ids.

Relocked with uv 0.11.28, the version CI installs. Every lock change
comes from Gym's own pyproject: aiohttp>=3.14.4 (sglang keeps 3.14.3,
so aiohttp forks), anyio>=4.15.1, PyJWT>=2.15.1, jinja2, jsonschema, and
the dropped mlflow/mlflow-skinny dependencies.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 13, 2026
NVIDIA-NeMo/Gym#2836 makes Gym's message-derived required_prefix_token_ids
opt-in through VLLMModelConfig.derive_prefix_token_ids_from_messages
(default false). NeMo-RL's native vLLM HTTP server implements that field,
so setup_nemo_gym_config now turns the flag on in the env.nemo_gym
policy_model overlay when the generation backend is vllm. Dynamo is left
to its own PR, and megatron is unchanged.

- An explicit derive_prefix_token_ids_from_messages in the overlay wins.
- The flag stays off when the overlay sets supply_prefix_token_ids, which
  owns the same field and which Gym rejects alongside it.
- A sharded job without a policy_model overlay gets the flag through
  common_overrides. A new top-level overlay would have no shard owner and
  fail the shard plan.
- Token capture turns off return_token_id_information on the actor, and
  Gym rejects derivation without it, so the actor now turns the flag off
  there too. External staging supplies the prefix from the ledger.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 13, 2026
When a Gym request has no required_prefix_token_ids, the native server
scanned back for any message carrying prompt_token_ids. If the newest
assistant message had no tokens (it was injected, edited, or
regenerated), the scan used an older turn's tokens, which are no longer a
prefix of the rendered prompt. NVIDIA-NeMo/Gym#2836 fixed the same bug in
Gym's derivation.

Match Gym's rule: look only at the newest assistant message, and send no
prefix when it has no token information. The rule lives in
_message_prefix_token_ids so it can be tested without the vLLM request
classes.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:model Model servers, inference providers, and model adapters feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer labels Oct 20, 2026
@yaoyu-33
yaoyu-33 marked this pull request as draft November 18, 2026 02:16
@yaoyu-33

Copy link
Copy Markdown
Contributor

We’re doing some cleanup of the open PR list. This PR has had no activity for over a month, so I’ve moved it back to draft.

If this change is still needed, please resolve any merge conflicts and mark the PR ready for review again when it’s ready to merge. Thanks!

@github-actions github-actions Bot removed the sla:triage-overdue Review assignment is over the one-business-day SLA label Nov 18, 2026

This branch was previously deployed

1 inactive deployment
public — 9691c001 Deployed Oct 12, 2026 by copy-pr-bot[bot] via release / finalize / notify #5057
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model servers, inference providers, and model adapters feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants