vllm.v1.worker.gpu.cudagraph_utils
¶
Classes:
-
BatchExecutionDescriptor–Describes the shape of the batch and CG mode to run; this is used to make shape
-
CudaGraphManager–Captures and dispatches the CUDA graphs of one runner.
-
ModelCudaGraphManager–CudaGraphManager with model-specific capture and hidden state management.
Functions:
-
has_compiled_submodule–Whether any submodule is an active @support_torch_compile module.
-
profile_cudagraph_memory–Estimate the GPU memory needed for CUDA graph capture.
BatchExecutionDescriptor
dataclass
¶
Describes the shape of the batch and CG mode to run; this is used to make shape matches between the capture and runtime.
CudaGraphManager
¶
Captures and dispatches the CUDA graphs of one runner.
Uniform decode batches of query length q take graphs of n requests, for
each q in decode_query_lens and n in decode_request_sizes (default
decode_cudagraph_capture_request_sizes) up to the requests q maps to.
With varlen_decode (adaptive verification) they take token-keyed graphs of
get_varlen_decode_cudagraph_token_sizes instead. Other batches, and all
batches when one routine runs every batch, take token-keyed graphs of
prefill_cudagraph_capture_token_sizes.
Methods:
-
capture–Capture CUDA graphs.
-
captured_token_counts–Sorted token counts with a captured graph, ignoring LoRA variants.
-
dispatch–Find the graph to run a batch with, else an eager descriptor.
-
release_graphs–Drop the captured graphs so a later capture() can refill them.
-
run_fullgraph–Replay a captured FULL cudagraph.
_build_lora_dispatch_map()
¶
Precompute actual num_active_loras -> effective captured case.
Mirrors the num_tokens candidate expansion in _init_candidates:
every possible active-LoRA count is mapped ahead of time to the
smallest captured case that can serve it, so dispatch is a plain
dict lookup instead of a per-call bisect.
_capture_stream(desc)
¶
Capture on the stream used by the microbatch threads.
_init_candidates()
¶
Build the descriptors to capture and the dispatch tables.
_maybe_ubatch_twin(desc)
¶
Return a microbatched capture candidate when eligible.
Uniform query lengths preserve the captured request split. Use the DP dispatch thresholds so all ranks generate the same candidates.
_resolve_effective_loras(num_active_loras)
¶
Map an actual active-LoRA count to its captured graph case.
_warmup_for_capture(forward_fn)
¶
Run the prepared forward eagerly before capturing this descriptor.
capture(create_forward_fn, progress_bar_desc='Capturing CUDA graphs', stream=None)
¶
Capture CUDA graphs.
Parameters:
-
(create_forward_fn¶CreateForwardFn) –Factory that prepares inputs (OUTSIDE graph) and returns a forward_fn. For FULL and breakable PIECEWISE modes, it is called again after warmup to prepare fresh attention metadata for capture.
-
(progress_bar_desc¶str, default:'Capturing CUDA graphs') –Description shown on the capture progress bar.
-
(stream¶Stream | None, default:None) –Capture on this stream instead of a new one.
captured_token_counts()
¶
Sorted token counts with a captured graph, ignoring LoRA variants.
dispatch(num_reqs, num_tokens, uniform_token_count, num_active_loras, max_query_len=None, num_ubatches=1)
¶
Find the graph to run a batch with, else an eager descriptor.
A uniform decode batch takes the smallest decode graph of its query
length that covers num_tokens, which may be padded (e.g. to the DP
maximum), if that graph holds its requests. Other batches, and decode
batches no decode graph takes, take the smallest compatible token-keyed
graph.
release_graphs()
¶
Drop the captured graphs so a later capture() can refill them.
Elastic EP reallocates the MoE workspace when it grows, which leaves
every captured graph holding a stale data pointer. _capture_descs is
kept, so needs_capture() still reports the work to redo.
run_fullgraph(desc)
¶
Replay a captured FULL cudagraph.
ModelCudaGraphManager
¶
Bases: CudaGraphManager
CudaGraphManager with model-specific capture and hidden state management.
Methods:
-
capture–Capture CUDA graphs for model forward pass.
-
run_fullgraph–Replay a captured FULL cudagraph and return hidden states.
capture(model, model_state, input_buffers, intermediate_tensors, block_tables, attn_groups, kv_cache_config, pcp_manager=None, use_aux_hidden_state_outputs=False, lora_capture_hook=None, progress_bar_desc='Capturing CUDA graphs')
¶
Capture CUDA graphs for model forward pass.
run_fullgraph(desc)
¶
Replay a captured FULL cudagraph and return hidden states.
_extrapolate_full_graph_memory(mem_samples, total_graphs)
¶
Extrapolate the total FULL capture cost from samples of the largest graphs. The first capture allocates the pool baseline; later graphs mostly reuse it, so the second sample is taken as the per-graph cost.
_init_minimal_kv_cache_for_profiling(runner)
¶
Allocate the smallest KV cache that still lets every graph be captured.
_teardown_profiling_state(runner)
¶
Release the profiling KV cache and captured graphs while keeping model
weights, so the real initialize_kv_cache starts from a clean slate.
has_compiled_submodule(model)
¶
Whether any submodule is an active @support_torch_compile module.
profile_cudagraph_memory(runner)
¶
Estimate the GPU memory needed for CUDA graph capture.
Called during memory profiling, before the real KV cache is allocated,
so that Worker.determine_available_memory can reserve headroom for
graph capture. Bootstraps a minimal KV cache, runs capture_model()
once, then releases everything so the real init/capture path starts clean.
FULL graphs bake in KV cache pointers, so only the largest few are captured (into a throwaway pool) and their total cost is extrapolated. PIECEWISE, encoder and speculator graphs are measured in full. All profiling captures are discarded afterwards: replaying graphs recorded against the throwaway profiling state is unsafe (e.g. inductor graph partition reclaims the storages of earlier cudagraph recordings once the real capture records new ones, leading to use-after-free crashes).