[6/N][warmup][DSv4] Migrate sampling, and DFlash JIT kernels (#56323)

Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com>
Signed-off-by: LopezCastroRoberto <rocastro@redhat.com>
Co-authored-by: Lucas Wilkinson <lwilkins@redhat.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Roberto L. Castro
2026-09-12 17:24:24 -07:00
committed by GitHub
co-authored by Lucas Wilkinson OpenAI Codex
parent e19a3e172e
commit aed894c190
27 changed files with 2288 additions and 558 deletions
+3 -4
View File
@@ -80,16 +80,15 @@ class CPUModelRunner(GPUModelRunner):
# Speculative decoding fallbacks
import vllm.v1.sample.rejection_sampler
import vllm.v1.spec_decode.llm_base_proposer
import vllm.v1.spec_decode.utils as spec_decode_utils
vllm.v1.spec_decode.llm_base_proposer.eagle_prepare_inputs_padded_kernel = (
spec_decode_utils._eagle_prepare_inputs_padded.kernel = (
cpu_tl.eagle_prepare_inputs_padded_kernel
)
vllm.v1.spec_decode.llm_base_proposer.eagle_prepare_next_token_padded_kernel = (
spec_decode_utils._eagle_prepare_next_token_padded.kernel = (
cpu_tl.eagle_prepare_next_token_padded_kernel
)
vllm.v1.spec_decode.llm_base_proposer.copy_and_expand_eagle_inputs_kernel = (
spec_decode_utils._copy_and_expand_eagle_inputs.kernel = (
cpu_tl.copy_and_expand_eagle_inputs_kernel
)
spec_decode_utils.copy_and_expand_dflash_inputs_kernel = (
+52 -43
View File
@@ -188,6 +188,7 @@ logger = init_logger(__name__)
class GPUModelRunner(LoRAModelRunnerMixin):
@JitWarmupRegistry.capture
def __init__(self, vllm_config: VllmConfig, device: torch.device):
self.vllm_config = vllm_config
self.model_config = vllm_config.model_config
@@ -202,7 +203,6 @@ class GPUModelRunner(LoRAModelRunnerMixin):
self.speculative_config is not None and self.speculative_config.use_dspark()
)
self.observability_config = vllm_config.observability_config
self.jit_warmup_registry = JitWarmupRegistry(vllm_config)
self.device = device
self.dtype = self.model_config.dtype
@@ -399,7 +399,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
logger.info_once("Loading model from scratch...")
# Capture warmup providers selected while constructing the model.
with self.jit_warmup_registry.activate():
with self.jit_warmup_registry.activate(): # type: ignore[attr-defined]
self.model = model_loader.load_model(
vllm_config=self.vllm_config,
model_config=self.vllm_config.model_config,
@@ -461,47 +461,50 @@ class GPUModelRunner(LoRAModelRunnerMixin):
# Initialize samplers. Model states may override via custom_sampler().
if self.is_last_pp_rank and not self.is_pooling_model:
sampler_kwargs: dict[str, Any] = {
"max_num_reqs": self.max_num_reqs,
"vocab_size": self.vocab_size,
"device": self.device,
"req_states": self.req_states,
"logprobs_mode": self.model_config.logprobs_mode,
"num_speculative_tokens": self.decode_query_len,
"use_fp64_gumbel": self.model_config.use_fp64_gumbel,
"enable_trace_replay": self.model_config.enable_trace_replay,
"reasoning_config": self.vllm_config.reasoning_config,
"return_sampling_mask": self.model_config.return_sampling_mask,
}
if self.vllm_config.watermark_config is None:
self.sampler = Sampler(**sampler_kwargs)
else:
wm_config = self.vllm_config.watermark_config
watermarker = create_watermarker(wm_config)
if self.speculative_config is not None:
watermarker = create_speculative_target_watermarker(watermarker)
self.sampler = GPUWatermarkSampler(
watermarker,
deduplicate_contexts=wm_config.deduplicate_contexts,
deduplicate_contexts_max_history=(
wm_config.deduplicate_contexts_max_history
),
**sampler_kwargs,
)
custom = self.model_state.custom_sampler(self.sampler)
with self.jit_warmup_registry.activate(): # type: ignore[attr-defined]
sampler_kwargs: dict[str, Any] = {
"max_num_reqs": self.max_num_reqs,
"vocab_size": self.vocab_size,
"device": self.device,
"req_states": self.req_states,
"logprobs_mode": self.model_config.logprobs_mode,
"num_speculative_tokens": self.decode_query_len,
"use_fp64_gumbel": self.model_config.use_fp64_gumbel,
"enable_trace_replay": self.model_config.enable_trace_replay,
"reasoning_config": self.vllm_config.reasoning_config,
"return_sampling_mask": self.model_config.return_sampling_mask,
}
if self.vllm_config.watermark_config is None:
self.sampler = Sampler(**sampler_kwargs)
else:
wm_config = self.vllm_config.watermark_config
watermarker = create_watermarker(wm_config)
if self.speculative_config is not None:
watermarker = create_speculative_target_watermarker(watermarker)
self.sampler = GPUWatermarkSampler(
watermarker,
deduplicate_contexts=wm_config.deduplicate_contexts,
deduplicate_contexts_max_history=(
wm_config.deduplicate_contexts_max_history
),
**sampler_kwargs,
)
custom = self.model_state.custom_sampler(self.sampler)
if custom:
self.vllm_config._check_watermarking_unsupported(custom_sampler=True)
self.sampler, self.rejection_sampler = custom
elif self.speculative_config is not None:
self.rejection_sampler = RejectionSampler(
self.sampler,
self.speculative_config,
self.device,
watermark_key=speculative_target_watermark_key(
self.vllm_config.watermark_config
),
)
if custom:
self.vllm_config._check_watermarking_unsupported(
custom_sampler=True
)
self.sampler, self.rejection_sampler = custom
elif self.speculative_config is not None:
self.rejection_sampler = RejectionSampler(
self.sampler,
self.speculative_config,
self.device,
watermark_key=speculative_target_watermark_key(
self.vllm_config.watermark_config
),
)
self.prompt_logprobs_worker = PromptLogprobsWorker(
self.max_num_reqs,
logprobs_mode=self.model_config.logprobs_mode,
@@ -738,7 +741,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
self.speculator.init_cudagraph_manager(cudagraph_mode)
# Capture warmup providers that depend on allocated KV-cache strides.
with self.jit_warmup_registry.activate():
with self.jit_warmup_registry.activate(): # type: ignore[attr-defined]
kv_caches_dict = init_kv_cache(
self.compilation_config.static_forward_context,
self.kv_cache_config,
@@ -924,6 +927,12 @@ class GPUModelRunner(LoRAModelRunnerMixin):
# during actual execution.
assert self.sampler is not None
self.sampler(logits, dummy_input_batch)
if self.rejection_sampler is not None:
# Warm head-dtype logits; scheduler warmup covers processed FP32 logits.
assert self.speculator is not None
self.rejection_sampler(
logits, dummy_input_batch, self.speculator.draft_logits
)
@torch.inference_mode()
def _dummy_pooler_run(self, hidden_states: torch.Tensor) -> None:
+5 -1
View File
@@ -4,7 +4,10 @@ import numpy as np
import torch
from vllm.sampling_params import SamplingParams
from vllm.v1.sample.ops.topk_topp_sampler import apply_top_k_top_p
from vllm.v1.sample.ops.topk_topp_sampler import (
apply_top_k_top_p,
register_top_k_top_p_warmups,
)
from vllm.v1.worker.gpu.buffer_utils import UvaBackedTensor
from vllm.v1.worker.gpu.sample.gumbel import apply_temperature
from vllm.v1.worker.gpu.sample.min_p import apply_min_p
@@ -18,6 +21,7 @@ class SamplingStates:
def __init__(self, max_num_reqs: int, vocab_size: int):
self.max_num_reqs = max_num_reqs
self.vocab_size = vocab_size
register_top_k_top_p_warmups()
self.temperature = UvaBackedTensor(max_num_reqs, dtype=torch.float32)
self.top_k = UvaBackedTensor(max_num_reqs, dtype=torch.int32)
@@ -1,8 +1,10 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import copy
from types import SimpleNamespace
from typing import Any
import numpy as np
import torch
import torch.nn as nn
@@ -10,6 +12,15 @@ from vllm.config import VllmConfig, replace
from vllm.config.compilation import CUDAGraphMode
from vllm.forward_context import BatchDescriptor, set_forward_context
from vllm.logger import init_logger
from vllm.model_executor.warmup.jit_warmup import (
WarmupChoices,
WarmupIntRange,
)
from vllm.model_executor.warmup.jit_warmup_triton_helper import (
DispatchSpec,
TritonWarmupTensor,
triton_kernel_dispatcher_with_warmup,
)
from vllm.triton_utils import tl, triton
from vllm.v1.attention.backend import AttentionCGSupport
from vllm.v1.attention.backends.utils import PAD_SLOT_ID
@@ -108,6 +119,7 @@ class DFlashSpeculator(DraftModelSpeculator):
self.query_cudagraph_manager: DFlashCudaGraphManager | None = None
self.draft_kv_cache_group_id: int = -1
prepare_dflash_inputs.register_warmup(speculator=self)
@property
def attn_vllm_config(self) -> VllmConfig:
@@ -682,6 +694,64 @@ def _prepare_dflash_inputs_kernel(
tl.store(out_query_slot_mapping_ptr + block, PAD_SLOT_ID, mask=mask)
def _prepare_dflash_warmup_inputs(*, speculator: DFlashSpeculator) -> dict[str, Any]:
int32 = TritonWarmupTensor(torch.int32)
int64 = TritonWarmupTensor(torch.int64)
float32 = TritonWarmupTensor(torch.float32)
input_buffers = SimpleNamespace(
input_ids=int32, positions=int64, query_start_loc=int32, seq_lens=int32
)
gid: Any = WarmupChoices(*speculator.draft_kv_cache_group_ids)
max_target_query_len: Any = WarmupIntRange(1, 257, advance=lambda value: value * 2)
block_table_stride = int(speculator.block_tables.input_block_tables[gid].stride(0))
block_table = TritonWarmupTensor(
torch.int32,
shape=(1, block_table_stride),
strides=(block_table_stride, 1),
)
input_batch = SimpleNamespace(
num_reqs=1,
num_scheduled_tokens=np.array([max_target_query_len]),
positions=int64,
query_start_loc=int32,
idx_mapping=int64,
)
return dict(
input_buffers=input_buffers,
query_slot_mapping=int64,
context_positions=int64,
context_slot_mapping=int64,
sample_indices=int64,
sample_pos=int64,
sample_idx_mapping=int32,
temperature=float32,
seeds=int64,
input_batch=input_batch,
num_sampled=int32,
num_rejected=int32,
last_sampled=int64,
next_prefill_tokens=int32,
input_temperature=float32,
input_seeds=int64,
block_table=block_table,
block_size=speculator.block_tables.kernel_block_sizes[gid],
cp_rank=speculator.block_tables.cp_rank,
cp_size=speculator.block_tables.cp_size,
cp_interleave=speculator.block_tables.cp_interleave,
parallel_drafting_token_id=speculator.parallel_drafting_token_id,
num_query_per_req=speculator.num_query_per_req,
num_speculative_steps=speculator.num_speculative_steps,
max_num_reqs=speculator.max_num_reqs,
max_num_tokens=speculator.max_num_tokens,
max_model_len=speculator.max_model_len,
sample_from_anchor=speculator.sample_from_anchor,
)
@triton_kernel_dispatcher_with_warmup(
kernel=_prepare_dflash_inputs_kernel,
warmup_inputs=_prepare_dflash_warmup_inputs,
)
def prepare_dflash_inputs(
input_buffers: InputBuffers,
query_slot_mapping: torch.Tensor,
@@ -718,50 +788,33 @@ def prepare_dflash_inputs(
max_num_tokens: int,
max_model_len: int,
sample_from_anchor: bool = False,
) -> None:
) -> DispatchSpec:
num_reqs = input_batch.num_reqs
assert num_reqs > 0
# Cover the longest possible per-request span (ctx + query). Use the max
# per-request query length, not the total token count across the batch.
max_target_query_len = int(input_batch.num_scheduled_tokens.max())
max_tokens_per_req = max_target_query_len + num_query_per_req
BLOCK_SIZE = min(256, triton.next_power_of_2(max(1, max_tokens_per_req)))
num_blocks = triton.cdiv(max_tokens_per_req, BLOCK_SIZE)
_prepare_dflash_inputs_kernel[(num_reqs, num_blocks)](
input_buffers.input_ids,
input_buffers.positions,
input_buffers.query_start_loc,
input_buffers.seq_lens,
query_slot_mapping,
context_positions,
context_slot_mapping,
sample_indices,
sample_pos,
sample_idx_mapping,
temperature,
seeds,
input_batch.positions,
input_batch.query_start_loc,
input_batch.idx_mapping,
last_sampled,
next_prefill_tokens,
num_sampled,
num_rejected,
input_temperature,
input_seeds,
block_table,
block_table.stride(0),
parallel_drafting_token_id,
block_size,
num_query_per_req,
num_speculative_steps,
max_num_reqs,
max_num_tokens,
max_model_len,
cp_rank,
SAMPLE_FROM_ANCHOR=sample_from_anchor,
block = min(256, triton.next_power_of_2(max(1, max_tokens_per_req)))
num_blocks = triton.cdiv(max_tokens_per_req, block)
return (num_reqs, num_blocks), dict(
out_input_ids_ptr=input_buffers.input_ids,
out_query_positions_ptr=input_buffers.positions,
out_query_start_loc_ptr=input_buffers.query_start_loc,
out_seq_lens_ptr=input_buffers.seq_lens,
out_query_slot_mapping_ptr=query_slot_mapping,
out_context_positions_ptr=context_positions,
out_context_slot_mapping_ptr=context_slot_mapping,
out_sample_indices_ptr=sample_indices,
out_sample_pos_ptr=sample_pos,
out_sample_idx_mapping_ptr=sample_idx_mapping,
out_temperature_ptr=temperature,
out_seeds_ptr=seeds,
target_positions_ptr=input_batch.positions,
target_query_start_loc_ptr=input_batch.query_start_loc,
idx_mapping_ptr=input_batch.idx_mapping,
temperature_ptr=input_temperature,
seeds_ptr=input_seeds,
PAD_SLOT_ID=PAD_SLOT_ID,
CP_SIZE=cp_size,
CP_INTERLEAVE=cp_interleave,
BLOCK_SIZE=BLOCK_SIZE,
BLOCK_SIZE=block,
)
@@ -9,6 +9,11 @@ from vllm.config import VllmConfig
from vllm.config.compilation import CUDAGraphMode
from vllm.forward_context import BatchDescriptor, set_forward_context
from vllm.logger import init_logger
from vllm.model_executor.warmup.jit_warmup_triton_helper import (
DispatchSpec,
TritonWarmupTensor,
triton_kernel_dispatcher_with_warmup,
)
from vllm.triton_utils import tl, triton
from vllm.v1.attention.backends.utils import PAD_SLOT_ID
from vllm.v1.worker.gpu.attn_utils import build_slot_mappings_by_layer
@@ -69,6 +74,9 @@ class MultiModuleMTPSpeculator(DraftModelSpeculator):
self.cudagraph_manager: SpeculatorCudaGraphManager | None = None
_shift_input_ids.register_warmup()
_shift_input_embeds.register_warmup(speculator=self)
def load_draft_model(
self,
target_model: nn.Module,
@@ -1008,6 +1016,32 @@ def _shift_input_ids_kernel(
tl.store(input_ids_ptr + last_token_index, draft_token)
def _shift_input_ids_warmup_inputs() -> dict[str, Any]:
return dict(
input_ids=TritonWarmupTensor(torch.int32),
idx_mapping=TritonWarmupTensor(torch.int32),
query_start_loc=TritonWarmupTensor(torch.int32),
last_token_indices=TritonWarmupTensor(torch.int64),
draft_tokens=TritonWarmupTensor(torch.int64),
num_reqs=1,
)
@triton_kernel_dispatcher_with_warmup(
kernel=_shift_input_ids_kernel,
warmup_inputs=_shift_input_ids_warmup_inputs,
)
def _shift_input_ids(
input_ids: torch.Tensor,
idx_mapping: torch.Tensor,
query_start_loc: torch.Tensor,
last_token_indices: torch.Tensor,
draft_tokens: torch.Tensor,
num_reqs: int,
) -> DispatchSpec:
return (num_reqs,), dict(BLOCK_SIZE=1024)
@triton.jit
def _shift_input_embeds_kernel(
input_embeds_ptr,
@@ -1065,6 +1099,44 @@ def _shift_input_embeds_kernel(
)
def _shift_input_embeds_warmup_inputs(
*, speculator: MultiModuleMTPSpeculator
) -> dict[str, Any]:
hidden = TritonWarmupTensor(
speculator.dtype,
shape=(1, speculator.hidden_size),
strides=(speculator.hidden_size, 1),
)
return dict(
input_embeds=hidden,
draft_embeds=hidden,
idx_mapping=TritonWarmupTensor(torch.int32),
query_start_loc=TritonWarmupTensor(torch.int32),
last_token_indices=TritonWarmupTensor(torch.int64),
num_reqs=1,
)
@triton_kernel_dispatcher_with_warmup(
kernel=_shift_input_embeds_kernel,
warmup_inputs=_shift_input_embeds_warmup_inputs,
)
def _shift_input_embeds(
input_embeds: torch.Tensor,
draft_embeds: torch.Tensor,
idx_mapping: torch.Tensor,
query_start_loc: torch.Tensor,
last_token_indices: torch.Tensor,
num_reqs: int,
) -> DispatchSpec:
hidden_size = input_embeds.shape[-1]
return (num_reqs, triton.cdiv(hidden_size, 256)), dict(
hidden_size=hidden_size,
BLOCK_SIZE_Q=16,
BLOCK_SIZE_H=256,
)
def update_draft_inputs(
draft_tokens: torch.Tensor,
draft_embeds: torch.Tensor | None,
@@ -1074,29 +1146,21 @@ def update_draft_inputs(
idx_mapping: torch.Tensor,
num_reqs: int,
) -> None:
_shift_input_ids_kernel[(num_reqs,)](
_shift_input_ids(
input_buffers.input_ids,
idx_mapping,
input_buffers.query_start_loc,
last_token_indices,
draft_tokens,
BLOCK_SIZE=1024,
num_reqs=num_reqs,
)
if input_embeds is not None:
assert draft_embeds is not None
hidden_size = input_embeds.shape[-1]
hidden_block_size = 256
_shift_input_embeds_kernel[
(num_reqs, triton.cdiv(hidden_size, hidden_block_size))
](
_shift_input_embeds(
input_embeds,
input_embeds.stride(0),
draft_embeds,
draft_embeds.stride(0),
idx_mapping,
input_buffers.query_start_loc,
last_token_indices,
hidden_size,
BLOCK_SIZE_Q=16,
BLOCK_SIZE_H=hidden_block_size,
num_reqs=num_reqs,
)
+33 -34
View File
@@ -498,6 +498,7 @@ class ExecuteModelState(NamedTuple):
class GPUModelRunner(
LoRAModelRunnerMixin, KVConnectorModelRunnerMixin, ECConnectorModelRunnerMixin
):
@JitWarmupRegistry.capture
def __init__(
self,
vllm_config: VllmConfig,
@@ -514,7 +515,6 @@ class GPUModelRunner(
self.scheduler_config = vllm_config.scheduler_config
self.speculative_config = vllm_config.speculative_config
self.observability_config = vllm_config.observability_config
self.jit_warmup_registry = JitWarmupRegistry(vllm_config)
model_config = self.model_config
cache_config = self.cache_config
@@ -750,36 +750,35 @@ class GPUModelRunner(
self.parallel_config.cp_kv_cache_interleave_size
)
# Capture warmup providers registered by the initial placeholder InputBatch
with self.jit_warmup_registry.activate():
self.input_batch = InputBatch(
max_num_reqs=self.max_num_reqs,
# We need to use the encoder length for encoder-decoder
# because of KV cache for cross-attention.
max_model_len=max(self.max_model_len, self.max_encoder_len),
max_num_batched_tokens=self.max_num_tokens,
device=self.device,
vocab_size=self.model_config.get_vocab_size(),
block_sizes=[placeholder_block_size],
kernel_block_sizes=[placeholder_block_size],
max_num_blocks_per_req=[placeholder_max_num_blocks],
num_spec_tokens=self.num_spec_tokens,
logitsprocs=build_logitsprocs(
self.vllm_config,
self.device,
PIN_MEMORY,
self.is_pooling_model,
custom_logitsprocs,
),
# We currently don't know whether a particular custom logits processor
# uses output token ids so we set this conservatively. Thinking-budget
# tracking is requested dynamically when a budgeted request is in the
# batch.
logitsprocs_need_output_token_ids=bool(custom_logitsprocs),
is_pooling_model=self.is_pooling_model,
cp_kv_cache_interleave_size=self.parallel_config.cp_kv_cache_interleave_size,
reasoning_config=self.vllm_config.reasoning_config,
use_replayssm=self.cache_config.use_replayssm,
)
self.input_batch = InputBatch(
max_num_reqs=self.max_num_reqs,
# We need to use the encoder length for encoder-decoder
# because of KV cache for cross-attention.
max_model_len=max(self.max_model_len, self.max_encoder_len),
max_num_batched_tokens=self.max_num_tokens,
device=self.device,
vocab_size=self.model_config.get_vocab_size(),
block_sizes=[placeholder_block_size],
kernel_block_sizes=[placeholder_block_size],
max_num_blocks_per_req=[placeholder_max_num_blocks],
num_spec_tokens=self.num_spec_tokens,
logitsprocs=build_logitsprocs(
self.vllm_config,
self.device,
PIN_MEMORY,
self.is_pooling_model,
custom_logitsprocs,
),
# We currently don't know whether a particular custom logits processor
# uses output token ids so we set this conservatively. Thinking-budget
# tracking is requested dynamically when a budgeted request is in the
# batch.
logitsprocs_need_output_token_ids=bool(custom_logitsprocs),
is_pooling_model=self.is_pooling_model,
cp_kv_cache_interleave_size=self.parallel_config.cp_kv_cache_interleave_size,
reasoning_config=self.vllm_config.reasoning_config,
use_replayssm=self.cache_config.use_replayssm,
)
# Separate cuda stream for overlapping transfer of sampled token ids from
# GPU to CPU when async scheduling is enabled.
@@ -5332,7 +5331,7 @@ class GPUModelRunner(
self.load_config.load_format = "dummy"
model_loader = get_model_loader(self.load_config)
# Capture warmup providers selected while constructing the model.
with self.jit_warmup_registry.activate():
with self.jit_warmup_registry.activate(): # type: ignore[attr-defined]
self.model = model_loader.load_model(
vllm_config=self.vllm_config, model_config=self.model_config
)
@@ -7304,7 +7303,7 @@ class GPUModelRunner(
self.parallel_config.cp_kv_cache_interleave_size
)
# Capture warmup providers registered after final KV-cache geometry is known
with self.jit_warmup_registry.activate():
with self.jit_warmup_registry.activate(): # type: ignore[attr-defined]
self.input_batch = InputBatch(
max_num_reqs=self.max_num_reqs,
max_model_len=max_model_len,
@@ -7454,7 +7453,7 @@ class GPUModelRunner(
# Reinitialize need to after initialize_attn_backend
self.may_reinitialize_input_batch(kv_cache_config, kernel_block_sizes)
# Capture warmup providers that depend on allocated KV-cache strides.
with self.jit_warmup_registry.activate():
with self.jit_warmup_registry.activate(): # type: ignore[attr-defined]
kv_caches = self.initialize_kv_cache_tensors(
kv_cache_config,
kernel_block_sizes,