Skip to content

refactor(vllm): share the per-sample rollout pipeline and chat token helpers - #4978

Merged
yuki-97 merged 8 commits into
NVIDIA-NeMo:mainfrom
jthomson04:jthomson04/vllm-request-pipeline
Nov 5, 2026
Merged

yuki-97 merged 8 commits into
NVIDIA-NeMo:mainfrom
jthomson04:jthomson04/vllm-request-pipeline

Conversation

@jthomson04

@jthomson04 jthomson04 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

What

This PR refactors the async vLLM worker without changing what it does. It was split out of #4905 at review request so it can merge on its own. Nothing here imports Dynamo or NeMo Gym.

  • VllmAsyncGenerationWorkerImpl._serve_rollout (new) serves one rollout from this engine:
    1. the token budget (_rollout_max_new_tokens: the context window plus the speculative-decoding lookahead clamp);
    2. the prompt alone when nothing fits;
    3. the SamplingParams;
    4. llm.generate;
    5. the prompt check;
    6. the result.
  • generate_async asserts a single sample, so it no longer fans out per-sample tasks. It formats the prompt, calls _serve_rollout and yields the output. vLLM still aborts the request when the caller cancels.
  • nemo_rl/models/generation/vllm/request_pipeline.py (new) separates building the result from assembling the output:
    • rollout_result builds the result: the sampled logprobs, router-replay (R3) route alignment, and the R3 fallback warning. It returns a RolloutResult.
    • rollout_output assembles the output tensors from that result.
  • New helpers in nemo_rl/models/generation/vllm/utils.py:
    • off_policy_sampling_error holds the chat endpoint's on-policy sampling checks. They are unchanged, still asserted, and run in the same order.
    • chat_routed_experts and chat_generation_tokens are the loop bodies of attach_routed_experts_to_chat_response_choices and attach_token_information_to_chat_response_choices.

Why

#4905 serves rollouts from the same engine through Dynamo's frontend and reuses request_pipeline and _serve_rollout. #4979 serves NeMo Gym's chat calls through it and reuses off_policy_sampling_error, chat_routed_experts and chat_generation_tokens.

Behaviour

Native vLLM behaves the same. The new generate_async and chat-route tests pass against both main's original code and this branch. They cover:

  • the token budget and the speculative-decoding clamp;
  • a full context, with and without speculative decoding;
  • the exact output keys with R3 off;
  • the on-policy assert and the top_k=-1 pin.

There are four small differences, all harmless for real vLLM outputs:

  • Logprob selection no longer guards completion.logprobs with hasattr.
  • The R3 fallback warning now logs under the request_pipeline logger, without the always-0 sample index.
  • output_ids is built with torch.cat, so when no new token fits it is a new tensor rather than a view of the input row.
  • rollout_output takes no pad_token_id; its fill never reached the output.

Tests

  • test_async_vllm_generate_async_* in test_vllm_generation.py (new): the generate_async wiring, with the budget cases in one parametrization.
  • test_chat_handler_rejects_off_policy_sampling_and_pins_top_k in test_vllm_generation.py (new).
  • tests/unit/models/generation/test_vllm_request_pipeline.py (new): R3 alignment and stats with vLLM 0.29's route shape, and R3 missing routes.
  • test_off_policy_sampling_error in test_vllm_utils.py.

Stack

This PR → #4905 (Dynamo core) → #4979 (NeMo Gym through Dynamo).

🤖 Generated with Claude Code

…helpers

Split generate_async's per-sample work into building the result
(request_pipeline.rollout_result: sampled logprobs, router-replay route
alignment and its fallback warning) and assembling the output tensors
(rollout_output). The rollout token budget and SamplingParams become
worker methods (_rollout_max_new_tokens, _rollout_sampling_params).

vllm/utils gains off_policy_sampling_error (the chat endpoint's on-policy
checks, unchanged) and chat_routed_experts / chat_generation_tokens (the
loop bodies of the attach_* response helpers).

No behaviour change for native vLLM: the new generate_async wiring tests
pass against both the old and the new implementation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04 jthomson04 added the CI:L1 Run doctests, unit tests, and functional tests label Oct 29, 2026
@copy-pr-bot

copy-pr-bot Bot commented Oct 29, 2026

Copy link
Copy Markdown

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

Contributors can view more details about this message here.

@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test e0d3789

jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 29, 2026
Restacks onto NVIDIA-NeMo#4954 after NVIDIA-NeMo#4905's split (vLLM refactor NVIDIA-NeMo#4978, Dynamo core
NVIDIA-NeMo#4905, NeMo-Gym through Dynamo NVIDIA-NeMo#4979) and the Gym#4817/#5161 rebase.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
copy-pr-bot Bot pushed a commit that referenced this pull request Oct 29, 2026
#4905 was split at review request: the vLLM refactor is #4978, #4905 is
now Dynamo core, and NeMo-Gym through Dynamo is #4979. This PR now stacks
on #4979. Gym is re-pinned to Gym#5161's head aa808b7, rebased onto Gym
main 5911d628c on top of Gym#4817's b0e681c.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>

@jthomson04 jthomson04 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review with the review team. The refactor preserves behaviour:

  • Base and head generate_async give identical tensors on 53 synthetic vLLM outputs, and bit-identical ones on 8 real vLLM 0.29 rollouts (Qwen3-0.6B).
  • 1152 chat sampling combinations give the same accept/reject and the same message text.
  • Every vLLM 0.29 assumption was checked against the v0.29.0 source.
  • The new generate_async tests also pass against main's original code.
  • All 13 new tests run in the non-vLLM step of the L0 vLLM unit lanes.

Worth keeping: the result/output split (rollout_result / rollout_output), the on-policy check becoming a pure function with its own test, and generate_async going from about 290 lines to about 110.

PR description fix (no line to anchor it on). The "Why" says #4905 reuses these seams, but #4905 reuses only request_pipeline and the two _rollout_* methods (_rollout_max_new_tokens, _rollout_sampling_params). off_policy_sampling_error, chat_routed_experts and chat_generation_tokens get their second caller in #4979 (L529, L791, L794, L808, L814). Suggested replacement text:

#4905 serves rollouts from the same engine through Dynamo's frontend and reuses request_pipeline and the two _rollout_* methods; #4979 serves NeMo Gym's chat calls through it and reuses off_policy_sampling_error, chat_routed_experts and chat_generation_tokens.

Inline comments:

  1. (C1) Move the generate, check and result steps into one worker method that both hosts call, so #4905 stops copying them.
  2. (C2) Add a route test for the on-policy assert and the top_k = -1 pin; no test reaches either line today.
  3. (C3) Drop rollout_output's pad_token_id: it never reaches the output.
  4. (C4) Run the nothing-fits test with speculative decoding too, the only case where the allowed_new_tokens > 0 check matters.
  5. (C5) Say in the docstring that, for top_k=None, None means on-policy only once the caller sends top_k=-1.
  6. (C6) Assert the exact output keys, so R3 stats keys can't leak into non-R3 rollouts unnoticed.
  7. (C7) Give the R3 pipeline test the route shape vLLM 0.29 actually produces.

Generated by Claude Code

Comment thread nemo_rl/models/generation/vllm/utils.py Resolved
Address review on this PR:

- _serve_rollout runs a rollout's whole sequence (budget, nothing-fits,
  SamplingParams, generate, prompt check, result), so every host that
  serves a rollout from this engine calls one method instead of copying
  the steps. It replaces _rollout_sampling_params.
- generate_async asserts a single sample, so it no longer fans out
  per-sample tasks: it formats the prompt, calls _serve_rollout and
  yields the output. vLLM still aborts the request on cancellation.
- rollout_output drops pad_token_id, whose fill never reached the output.
- off_policy_sampling_error documents that vLLM fills unset top_p/top_k
  from generation_config.json, so callers must pin top_k=-1.
- chat_routed_experts drops a dead isinstance check.
- Tests: a route test for the on-policy assert and the top_k pin; the
  budget tests fold into one parametrization that covers a full context
  with speculative decoding; the wiring test asserts the exact output
  keys and a missing logprob; the R3 pipeline test uses vLLM 0.29's route
  shape. Two pipeline tests the wiring tests now cover are removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test 3928525

jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 29, 2026
Brings in NVIDIA-NeMo#4978's review fixes through the stack.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
copy-pr-bot Bot pushed a commit that referenced this pull request Oct 29, 2026
…engine

Brings in #4978's review fixes. The engine's rollout path now calls the
worker's _serve_rollout instead of re-typing its steps, and the driver
drops rollout_output's removed pad_token_id.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
Resolve nemo_rl/models/generation/vllm/utils.py: keep this branch's
chat_generation_tokens and main's
attach_sampler_topk_logprobs_to_chat_response_choices (NVIDIA-NeMo#4818).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 2, 2026
…on04/dynamo-native-engine

Makes NVIDIA-NeMo#4978's head an ancestor of this branch so it can be this PR's base;
the tree is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test c2e4ef9

jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 2, 2026
Restack on NVIDIA-NeMo#4905 6a02b99 (NeMo RL main 6e68b17, NVIDIA-NeMo#4978 c2e4ef9).
The L0 vLLM shard lists keep main's test_score_centering.py and this
branch's test_swe1_dynamo_config.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04
jthomson04 requested a review from yuki-97 November 2, 2026 16:58

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge blockers — please rebase on main; the PR is 11 commits behind and the "Check if PR branch is up to date" job allows at most 10.

Thanks for extracting the shared async vLLM rollout pipeline and chat helpers for reuse by the Dynamo integration.

The before/after comparisons and detailed self-review make the preserved behavior clear; thanks for sharing them.

Scope: Async vLLM rollout and chat-helper refactor, at c2e4ef9

Checked:

  • Request behavior — context budgets, speculative lookahead, stop strings and prompt validation retain their previous behavior.
  • Output contracts — sampled logprobs, truncation, R3 alignment and chat-choice mapping match the original paths.
  • Cancellation — direct awaiting still reaches vLLM's request-abort handler.
  • CI execution — all 13 added test cases passed in the current-head vLLM unit job logs.

nit

  • Shorten the route-encoding and sampling-profile comments.
  • Limit the pipeline docstrings to their actual callers and explain the final output's downstream use.
  • Rename off_policy_sampling_error to check_sampling_param, assert inside the helper and update the affected tests.

Generated by Codex

Comment thread nemo_rl/models/generation/vllm/utils.py Resolved
Comment thread nemo_rl/models/generation/vllm/utils.py Resolved
jthomson04 and others added 2 commits November 3, 2026 09:47
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
- check_sampling_param replaces off_policy_sampling_error and raises
  ValueError itself, with the same messages in the same order. It raises
  rather than asserts so a server that answers an off-policy request with
  a client error can catch it; the native chat handler still fails with a
  500, as its assert did.
- Shorten the route-encoding and sampling-profile comments to one line.
- The pipeline docstrings name their actual callers and what the final
  RequestOutput is for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test e798c86

jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 3, 2026
…on04/dynamo-native-engine

Brings in NVIDIA-NeMo#4978's review fixes and its merge of upstream/main 7b3d9e6.

- L0 vLLM shard lists: this PR's list plus main's
  test_chat_template_parity_common.py.
- test_grpo.py: the legacy backend=dynamo setup tests this PR removes stay
  removed, including main's new turn_checkpointing_enabled line in one.
- test_outdated_config_checks.py: this PR's Dynamo checks plus main's
  rollout-recovery checks.
- _serve_rollout's docstring names the Dynamo engine module rather than
  this PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 3, 2026
…n04/dynamo-gym

Brings in NVIDIA-NeMo#4978's review fixes and upstream/main 7b3d9e6 through NVIDIA-NeMo#4905.

- L0 vLLM shard lists: this PR's swe1 config test plus main's
  test_chat_template_parity_common.py.
- test_nemo_gym_utils.py: this PR's Dynamo dialect test next to main's
  turn-checkpointing tests.
- build_nemo_gym_actors/build_nemo_gym_config now require
  turn_checkpointing_enabled; this PR's two new calls pass False.
- The Gym path calls NVIDIA-NeMo#4978's check_sampling_param, which raises ValueError,
  and still answers an off-policy call with InvalidArgument.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 3, 2026
…on04/dynamo-native-engine

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test d67311f

@jthomson04
jthomson04 requested a review from yuki-97 November 3, 2026 20:55
check_sampling_param raises ValueError since the review rename.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 4, 2026
…on04/dynamo-native-engine

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test d15b6f4

yuki-97
yuki-97 previously approved these changes Nov 4, 2026
@yuki-97
yuki-97 enabled auto-merge (squash) November 4, 2026 07:26
Resolve utils.py: main's encode_for_wire (NVIDIA-NeMo#4345) moves into
chat_routed_experts, which the chat attach loop now calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test 1fe249f

jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 4, 2026
…on04/dynamo-native-engine

Resolve import blocks in nemo_gym.py and routed_experts_codec.py: keep
NVIDIA-NeMo#4905's Dynamo imports beside main's (via NVIDIA-NeMo#4978) new ones.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 4, 2026
…n04/dynamo-gym

Resolve the nemo_gym.py import block: keep main's NEMO_GYM_* keys (via
NVIDIA-NeMo#4978 and NVIDIA-NeMo#4905) beside NVIDIA-NeMo#4979's native_default_chat_template_kwargs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
@terrykong terrykong added CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:L1 Run doctests, unit tests, and functional tests labels Nov 5, 2026
@yuki-97
yuki-97 merged commit aa06ca0 into NVIDIA-NeMo:main Nov 5, 2026
176 of 190 checks passed
jthomson04 added a commit to jthomson04/RL that referenced this pull request Nov 5, 2026
NVIDIA-NeMo#4978 landed on main as aa06ca0. The conflicts in request_pipeline.py,
vllm_worker_async.py and test_vllm_request_pipeline.py keep NVIDIA-NeMo#4905's
versions, which already build on NVIDIA-NeMo#4978's head (1fe249f).

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
isomap added a commit to isomap/RL that referenced this pull request Nov 6, 2026
Conflict in vllm_worker_async.py: main (NVIDIA-NeMo#4978) moved the chat route's
sampling check into check_sampling_param, which forced top_k=-1. Sampler
replay serves the matched profile's top_k, so check_sampling_param now
returns that top_k (top_k or val_top_k, -1 when None) and rejects a
request top_k that differs from it. The route keeps the branch's min_p,
penalty and logit-bias rejections. test_check_sampling_param and the
route's rejection test are updated to match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Hayate Iso <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants