fix: keep bounded sampling masks sparse - #3464
Open
mikasenghaas wants to merge 1 commit into
Open
Conversation
vLLM expands every sampling mask to a full-vocabulary bitset before the asynchronous host copy. The engine then unpacks that bitset on the CPU for every decode step. This stalls sampling replay even though training caps each kept set at 512 tokens. Capture bounded masks as fixed-width token IDs instead. Preserve the existing scheduler and response formats, and use vLLM's native bitset for wider masks.
mikasenghaas
marked this pull request as ready for review
September 2, 2026 07:53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mainVerification
uv run --no-sync pytest -q tests/unit/inference(3 passed)uv run --no-sync ruff check src/prime_rl/inference/patches.pyuv run --no-sync ruff format --check src/prime_rl/inference/patches.pyThe paired live-training benchmark ran 10 steps per revision with the same
Qwen3-0.6B model, Reverse Text environment, V2 runner, sampling replay
(
top_p = 0.95, bounded totop_k = 512), nodes, and warm caches.mainranfirst and this PR ran immediately afterward. Request- and token-normalized
metrics account for the different trajectories sampled during live training.
mainmainW&B runA targeted batch-384 sampling-mask benchmark reduced device-to-host data from
6.94 MiB to 0.75 MiB and CPU mask materialization from 211 ms to 1.20 ms. This
isolates the regression to vLLM's dense vocabulary-width bitset conversion;
the HTTP payload, router, environment server, and trainer are downstream of
that work.
Note
Medium Risk
Monkey-patches core vLLM v1 sampler/mask conversion on every worker; incorrect sparse logic could break sampling replay or mask payloads, though behavior is gated to bounded top-k and falls back to upstream.
Overview
Restores a sparse path for vLLM sampling masks when
return_sampling_maskis on and effectivetop_kis at most 512, avoiding the dense full-vocabulary bitset that regressed device-to-host transfer and CPU materialization.A new
monkey_patch_bounded_sampling_mask()is wired intoapply_shared_vllm_patches(). It wrapsSampler.__call__(via aContextVar) and replacesSamplingMaskTensors.from_logitsto buildSparseSamplingMaskTensorsfrom a boundedtopkover logits, withto_cpu_nonblocking/tolistsstill emitting vLLM’sSamplingMaskListsso scheduler and response formats stay unchanged. Wider masks or disabled sparse mode fall back to stock vLLM behavior; the patch is idempotent via_prime_rl_uses_sparse_sampling_masks.Reviewed by Cursor Bugbot for commit ae64d7f. Bugbot is set up for automated code reviews on this repo. Configure here.