vllm.v1.worker.gpu.ubatch_utils
¶
Microbatching (DBO) helpers for the V2 GPU model runner.
Classes:
-
UBatchRunner–Runs the model on two or more microbatches that overlap each other.
-
UBatchState–Per-microbatch inputs for one dual-batch-overlap step.
Functions:
-
create_ubatch_slices–Split a DP-padded batch into the slices its microbatches run on.
-
maybe_build_ubatch_runner–Build the microbatch runner, or None when DBO is not in use.
-
merge_ubatch_outputs–Reassemble the full-batch model output from the per-microbatch ones.
-
slice_model_inputs–Narrow the model's per-token inputs to one microbatch.
UBatchRunner
¶
Runs the model on two or more microbatches that overlap each other.
Each microbatch gets a thread and its own forward context. Only one thread
holds the GPU at a time; they hand off to each other at the communication
points inside the model (see vllm.v1.worker.ubatching), so one
microbatch's expert all-to-all overlaps the other's compute.
Methods:
-
abort_pending_run–Release and join parked threads after a failed capture.
-
begin_capturable_run–Start microbatch threads and return a callback to run and join them.
-
prepare–Split the batch into the microbatches the step will run on.
_make_forward_contexts(ubatch_slices, attn_metadata, slot_mappings_by_layer, is_padding)
¶
Build one forward context per microbatch.
The expert all-to-all runs once per microbatch, so each context's DP metadata carries that microbatch's token count. Microbatching pads every DP rank alike, so each rank can assume that count for the others too.
abort_pending_run()
¶
Release and join parked threads after a failed capture.
begin_capturable_run(model, model_inputs, ubatch_state, for_capture=False)
¶
Start microbatch threads and return a callback to run and join them.
For capture, start threads outside the graph and call the callback inside.
for_capture selects the capture stream and initializes cuBLAS workspaces
before capture.
Call the callback exactly once on this thread, or call abort_pending_run
on failure to release parked threads.
prepare(input_batch, block_tables, slot_mappings, batch_desc, dp_sync_state=None, for_capture=False)
¶
Split the batch into the microbatches the step will run on.
Attention metadata is built per microbatch and carried in the forward contexts the threads install, not in the caller's.
FULL uses padded sizes; for_capture refreshes metadata from dummy
block tables. Eager steps split by the DP-agreed uniform token count in
dp_sync_state (see create_ubatch_slices); FULL replays the split
its graph captured with.
UBatchState
¶
_slice_input_batch(input_batch, ubatch_slice, query_start_loc_buf, seq_lens_buf, dcp_local_seq_lens_buf=None, *, dcp_size=1, dcp_rank=0, cp_interleave=1)
¶
Build the sub-InputBatch a single microbatch runs on.
A request straddling the boundary appears in both microbatches, its query truncated to the tokens each one owns.
Query offsets and sequence lengths (including DCP-local lengths) are written to the caller's persistent buffers after truncation. Other GPU inputs are views of the full batch's buffers.
The sub-batch describes the forward pass only. Sampling runs once over the
merged batch, so logits_indices, cu_num_logits and the draft-token
fields are carried over unchanged and must not be read off a sub-batch.
_slice_query_start_loc(query_start_loc, out, req_start, req_stop, tok_start, num_tokens_padded)
¶
Rebase query_start_loc onto the microbatch's token range.
The clamp truncates the requests straddling either boundary to the tokens this microbatch owns.
_slice_seq_lens(seq_lens, out, query_start_loc, req_start, req_stop, tok_stop)
¶
Slice seq_lens, shortening the request truncated at the boundary.
Only tokens past the end of this microbatch are missing; tokens it owns in an earlier microbatch are computed before this one reads them, so they still count. Deriving the truncation from this microbatch's own query lengths would subtract them twice. It is computed from tensors, not Python ints, so this stays valid inside a CUDA graph.
create_ubatch_slices(input_batch, num_ubatches, uniform_token_count=None)
¶
Split a DP-padded batch into the slices its microbatches run on.
Splitting at the midpoint of the DP-padded token count leaves the trailing microbatch anywhere from full to entirely inside the padding. Clamping its request slice onto the last request leaves it holding that request with zero query tokens -- the shape a straddling request already takes -- so it stays well-formed with no work to do, like a dummy run.
A uniform batch of multi-token queries (spec decode verification) is split
on a request boundary instead, so each microbatch stays a uniform decode.
uniform_token_count must be agreed across DP ranks.
maybe_build_ubatch_runner(vllm_config, device, model_state, attn_groups, kv_cache_config, max_num_reqs)
¶
Build the microbatch runner, or None when DBO is not in use.
With more than one DP rank, the DP handshake agrees on each split. With one
DP rank, only all-reduce-overlap splits, as a local decision.
merge_ubatch_outputs(outputs)
¶
Reassemble the full-batch model output from the per-microbatch ones.
Models return hidden states, a tuple of those plus a list of auxiliary hidden states (EAGLE3, DFlash, DSpark), or IntermediateTensors on non-final pipeline ranks.
slice_model_inputs(model_inputs, token_slice)
¶
Narrow the model's per-token inputs to one microbatch.