[Model Runner V2] batch-sharded sample (#50465)

Signed-off-by: Giancarlo Delfin <gdelfin@inferact.ai>
This commit is contained in:
Giancarlo Delfin
2026-08-24 15:49:57 -07:00
committed by GitHub
parent 0d7d5ed0b2
commit d154d90d6c
19 changed files with 1781 additions and 22 deletions
+72 -4
View File
@@ -127,6 +127,11 @@ from vllm.v1.worker.gpu.mm.lora import set_active_mm_loras
from vllm.v1.worker.gpu.model_states import init_model_state
from vllm.v1.worker.gpu.pool.pooling_runner import PoolingRunner
from vllm.v1.worker.gpu.pp_utils import PPHandler
from vllm.v1.worker.gpu.sample.batch_shard import (
BatchSharder,
all_to_all_logits,
gather_sampler_output,
)
from vllm.v1.worker.gpu.sample.output import SamplerOutput
from vllm.v1.worker.gpu.sample.prompt_logprob import PromptLogprobsWorker
from vllm.v1.worker.gpu.sample.sampler import Sampler
@@ -310,6 +315,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
# model_state exists (num_new_sampled_tokens_per_step from ModelState).
self.sampler: Sampler | None = None
self.rejection_sampler: RejectionSampler | None = None
self.batch_sharder: BatchSharder | None = None
self.prompt_logprobs_worker: PromptLogprobsWorker | None = None
self.structured_outputs_worker: StructuredOutputsWorker | None = None
self.cudagraph_manager: ModelCudaGraphManager | None = None
@@ -404,6 +410,21 @@ class GPUModelRunner(LoRAModelRunnerMixin):
+ self.model_state.num_new_sampled_tokens_per_step
)
if self.parallel_config.enable_batch_sharded_sampling:
if hasattr(self.model, "compute_logits_local"):
self.batch_sharder = BatchSharder(
max_num_reqs=self.max_num_reqs,
max_num_logits_per_req=self.decode_query_len,
device=self.device,
)
logger.info("Batch-sharded sampling enabled.")
else:
logger.warning_once(
"Disabling batch-sharded sampling: %s does not implement "
"compute_logits_local",
type(self.model).__name__,
)
# Initialize samplers. Model states may override via custom_sampler().
if self.is_last_pp_rank and not self.is_pooling_model:
self.sampler = Sampler(
@@ -935,7 +956,10 @@ class GPUModelRunner(LoRAModelRunnerMixin):
preempted_req_ids = scheduler_output.preempted_req_ids
if preempted_req_ids:
finished_req_ids = finished_req_ids.union(preempted_req_ids)
for req_id in finished_req_ids:
# Sorted so every TP rank frees request slots in the same order.
# Features like batch-sharded sampling derive rank request ownership
# from the slot index.
for req_id in sorted(finished_req_ids):
self._remove_request(req_id)
def free_states(self, scheduler_output: SchedulerOutput) -> None:
@@ -1335,8 +1359,25 @@ class GPUModelRunner(LoRAModelRunnerMixin):
input_batch: InputBatch,
grammar_output: GrammarOutput | None,
) -> tuple[SamplerOutput, torch.Tensor, torch.Tensor]:
sample_hidden_states = hidden_states[input_batch.logits_indices]
logits = self.model.compute_logits(sample_hidden_states)
shard_metadata = None
global_input_batch = input_batch
if self.batch_sharder is not None:
# Shard the inputs along the batch dimension to sample in parallel
# across TP ranks.
input_batch, sorted_logits_indices, grammar_output, shard_metadata = (
self.batch_sharder.shard_sampler_inputs(input_batch, grammar_output)
)
# The hidden states must be gathered in rank-owner-sorted order
# before computing the partial-vocab logits, so that the all-to-all
# produces full-vocab logits for just the locally-owned requests.
sample_hidden_states = hidden_states[sorted_logits_indices]
local_logits = self.model.compute_logits_local(sample_hidden_states)
logits = all_to_all_logits(local_logits, shard_metadata)
logits = logits[:, : self.vocab_size]
else:
sample_hidden_states = hidden_states[input_batch.logits_indices]
logits = self.model.compute_logits(sample_hidden_states)
if grammar_output is not None:
# Apply grammar bitmask to the logits in-place.
assert self.structured_outputs_worker is not None
@@ -1347,7 +1388,12 @@ class GPUModelRunner(LoRAModelRunnerMixin):
grammar_output.grammar_bitmask,
)
if input_batch.num_draft_tokens == 0 or self.rejection_sampler is None:
sampler_output: SamplerOutput | None
if input_batch.num_reqs == 0:
# This rank owns no requests this step. It contributes an
# all-padding block to the gather below.
sampler_output = None
elif input_batch.num_draft_tokens == 0 or self.rejection_sampler is None:
assert self.sampler is not None
sampler_output = self.sampler(logits, input_batch)
else:
@@ -1361,6 +1407,28 @@ class GPUModelRunner(LoRAModelRunnerMixin):
self.speculator.draft_logits,
)
if shard_metadata is not None:
# Gather the sharded sampler outputs from the TP ranks into a single
# sampler output.
assert self.sampler is not None
sampler_output = gather_sampler_output(
sampler_output,
shard_metadata,
device=self.device,
global_batch=global_input_batch,
local_batch=input_batch,
gather_num_nans=self.sampler.compute_nans,
logprobs_dims=self.sampler.get_logprobs_dims(
global_input_batch.idx_mapping_np,
# Rejection sampler does not return logprob token ids.
include_token_ids=(
global_input_batch.num_draft_tokens == 0
or self.rejection_sampler is None
),
),
)
assert sampler_output is not None
return sampler_output, sampler_output.num_sampled, sampler_output.num_rejected
def postprocess_sampled(