Skip to content

feat: add vllm shared prefix rollout sampler - #34

Merged
Flink-ddd merged 3 commits into
RL-Align:mainfrom
inaniloquentee:issue-11-vllm-shared-prefix-caching
May 30, 2026
Merged

Flink-ddd merged 3 commits into
RL-Align:mainfrom
inaniloquentee:issue-11-vllm-shared-prefix-caching

Conversation

@inaniloquentee

Copy link
Copy Markdown
Collaborator

Summary

This PR completes issue #11:

[FEAT][executors]: optimize advanced vLLM sampler with shared prefix caching

It adds a vLLM-backed rollout sampler path that explicitly enables vLLM prefix caching by default, preserves shared prompt prefixes across GRPO multi-candidate generation, and returns both raw vLLM outputs and a stable normalized Kernel-Align rollout schema.

The PR also includes mocked unit tests, a real WSL2 vLLM smoke test, and a steady-state vLLM A/B benchmark comparing prefix caching disabled vs enabled.

Motivation

GRPO rollout commonly generates multiple candidate completions for the same prompt. Those candidates share the same prompt prefix. Without prefix reuse, the inference backend can repeatedly perform prompt prefill and rebuild equivalent KV cache state for each candidate.

vLLM supports Automatic Prefix Caching through:

LLM(..., enable_prefix_caching=True)

Kernel-Align should not manage vLLM KV blocks directly. The executor's responsibility is to:

  • request vLLM prefix caching explicitly;
  • keep candidate prompts byte-identical or token-identical across each prompt group;
  • preserve candidate grouping for downstream RL logic;
  • provide a stable output shape for trainer/loss consumers;
  • keep tests runnable without a real vLLM/GPU runtime.

Implementation

New vLLM Sampler Module

Added:

rl_engine/executors/vllm_sampler.py

Main types:

VLLMSamplerConfig
VLLMSharedPrefixSampler
NormalizedRolloutCandidate

VLLMSamplerConfig

VLLMSamplerConfig normalizes rollout sampler configuration from the executor's loose model_config dictionary.

Important defaults:

enable_prefix_caching=True
num_generations=1
backend="vllm"

Supported config fields:

  • model
  • num_generations
  • enable_prefix_caching
  • sampling_params
  • engine_kwargs
  • backend

The config validates:

  • num_generations >= 1
  • only the "vllm" backend is currently accepted

Lazy vLLM Construction

vLLM imports and engine construction are lazy.

This keeps CPU-only tests and kernel-only development paths from requiring vLLM at import time.

The vLLM engine is created only when generation is first requested:

LLM(
    model=...,
    enable_prefix_caching=True,
    **engine_kwargs,
)

Explicit disable remains supported:

enable_prefix_caching=False

Shared Prefix Candidate Expansion

For each input prompt, the sampler expands requests like this:

prompt-a, prompt-a, prompt-a, prompt-b, prompt-b, prompt-b

for num_generations=3.

This preserves identical prefixes within each GRPO candidate group and gives vLLM the request pattern needed for prefix-cache reuse.

Both text prompts and token prompt mappings are supported:

"text prompt"
{"prompt_token_ids": [1, 2, 3, ...]}

Token prompt support was added so real vLLM smoke tests can run with skip_tokenizer_init=True and avoid external tokenizer downloads.

Output Grouping

vLLM returns one output per expanded request. The sampler regroups flat output back into:

[prompt][candidate]

Returned shape:

{
    "backend": "vllm",
    "prefix_cache_enabled": True,
    "num_prompts": 2,
    "num_generations": 3,
    "outputs": [[raw_candidate, ...], ...],
    "normalized_outputs": [[NormalizedRolloutCandidate, ...], ...],
}

The raw outputs field is kept for backward compatibility and debugging.

Stable Normalized Output Schema

Added NormalizedRolloutCandidate as a stable Kernel-Align view over vLLM RequestOutput objects.

Fields:

prompt_index
candidate_index
request_id
prompt_token_ids
token_ids
text
finish_reason
cumulative_logprob
logprobs

The object provides:

candidate.to_dict()

This lets downstream trainer/loss code consume a predictable schema without depending directly on vLLM's internal output object layout.

RolloutExecutor Changes

Updated:

rl_engine/executors/rollout.py

Added:

RolloutExecutor.generate_candidates(...)

The existing execute_rollout(...) smoke path remains intact.

RolloutExecutor now owns:

  • sampler_config
  • lazy sampler
  • generate_candidates(...) delegation to VLLMSharedPrefixSampler

Tests

Added:

tests/test_vllm_rollout_sampler.py

Mocked tests cover:

  • prefix caching enabled by default;
  • explicit disable path;
  • vLLM lazy import behavior;
  • vLLM engine constructor receives enable_prefix_caching=True;
  • vLLM engine constructor receives enable_prefix_caching=False;
  • sampling params are passed through;
  • shared prefix prompt expansion;
  • output regrouping as [prompt][candidate];
  • token prompt mapping support;
  • stable normalized output schema for mapping-style fake outputs;
  • stable normalized output schema for object-style fake RequestOutput objects;
  • RolloutExecutor.generate_candidates(...) uses the vLLM sampler config.

Validation Results

Focused Unit Tests

Command:

python -m pytest tests/test_vllm_rollout_sampler.py -q

Result:

8 passed in 0.05s

Coverage:

  • default enable_prefix_caching=True;
  • explicit enable_prefix_caching=False;
  • lazy vLLM import and engine construction;
  • vLLM constructor receives the expected prefix-cache flag;
  • sampling params and engine kwargs are passed through;
  • shared-prefix prompt expansion for multi-candidate generation;
  • regrouping flat vLLM responses into [prompt][candidate];
  • text prompt and token prompt support;
  • normalized output schema for mapping-style and object-style outputs;
  • RolloutExecutor.generate_candidates(...) integration.

Real vLLM Test Environment

Runtime used for local validation:

OS/runtime: Ubuntu WSL2
GPU: NVIDIA GeForce RTX 3050
VRAM: 6144 MiB
compute capability: (8, 6)
vLLM 0.10.2
torch 2.8.0+cu126
CUDA runtime compatible with host CUDA 12.6 driver
model: local tiny GPT-2 test model
prompt mode: token prompts with skip_tokenizer_init=True

The real vLLM checks were run with a local validation harness outside the PR source tree. The relevant PR code path under test is RolloutExecutor.generate_candidates(...), which delegates to VLLMSharedPrefixSampler.

Real vLLM Smoke A/B Test

Workload:

local tiny GPT-2 model
token prompts
skip_tokenizer_init=True
num_generations=2
prompt_tokens=112
new_tokens=8

Result:

prefix_cache=false: 118827.93 ms
prefix_cache=true:   56470.65 ms

Interpretation:

This smoke test proves real vLLM execution through RolloutExecutor.generate_candidates(...) with both prefix-cache modes. The timings include engine startup/model load, so they should not be used as steady-state throughput claims.

Real vLLM Steady-State Benchmark

Workload:

local tiny GPT-2 model
token prompts
skip_tokenizer_init=True
num_generations=2
prompt_tokens=112
new_tokens=8
warmup=2
iterations=6

Result:

prefix_cache=false
  avg_ms: 16.59
  p50_ms: 15.87
  p90_ms: 18.74
  output_tokens_per_s: 964.28

prefix_cache=true
  avg_ms: 14.22
  p50_ms: 14.28
  p90_ms: 15.63
  output_tokens_per_s: 1124.95

Interpretation:

This benchmark reuses each vLLM engine after warmup and excludes engine startup/model load from measured iterations. On this small local RTX 3050 workload, prefix caching improved average measured latency by about 14.3%.

This is integration-grade steady-state evidence, not a production-scale model benchmark.

Real vLLM Normalized Output Smoke Test

Verified normalized fields from real vLLM RequestOutput:

request_id
prompt_token_ids
token_ids
finish_reason
prompt_index
candidate_index

The local validation artifacts also recorded benchmark rows equivalent to:

issue-11-real-vllm,vllm,false,2,112,8,118827.93,...
issue-11-real-vllm,vllm,true,2,112,8,56470.65,...
issue-11-real-vllm-steady-state,vllm,false,2,112,8,16.59,...
issue-11-real-vllm-steady-state,vllm,true,2,112,8,14.22,...

Notes And Limitations

  • The local GPU is an RTX 3050 with 6 GB VRAM, so the real benchmark uses a small generated GPT-2 model.
  • The steady-state benchmark is meaningful for integration and relative behavior, but it is not a production model performance claim.
  • FlashInfer was not available in the vLLM 0.10.2 environment, so vLLM logged fallback to PyTorch-native top-k/top-p sampling.
  • skip_tokenizer_init=True and token prompts are used to avoid external model/tokenizer downloads.

These limits are intentionally kept out of the blocking scope for this PR. The implementation goal is to prove the executor integration, default prefix-cache configuration, shared-prefix request shape, output grouping, and normalized result schema. A production-scale throughput claim should be made in a follow-up benchmark with larger model artifacts and suitable benchmark hardware.

Completion Status

Issue #11 is complete for the current scope.

Completed:

  • vLLM sampler integration;
  • default shared prefix caching;
  • explicit disable path;
  • shared prefix candidate expansion;
  • raw and normalized grouped outputs;
  • text prompt and token prompt support;
  • mocked unit tests;
  • real WSL2 vLLM smoke test;
  • real WSL2 vLLM steady-state benchmark;
  • real vLLM output normalization smoke test;
  • documentation and benchmark evidence.

Remaining non-blocking future work:

  • rerun the benchmark on a production-scale model/GPU when suitable hardware and model artifacts are available.

@Flink-ddd

Copy link
Copy Markdown
Collaborator

Thank you for contribution, This is an absolutely phenomenal PR, I'm approving this immediately. Outstanding work, @inaniloquentee Let's get this merged into main. if has real user report issue in the future and we will further update by case by case. but please pull latest code and push again and trigger completely CI workflow.

Signed-off-by: inaniloquentee <[email protected]>
@Flink-ddd
Flink-ddd merged commit 1788be5 into RL-Align:main May 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants