vllm.v1.worker.encoder_cudagraph
¶
CUDA graph manager for vision encoder budget-batch execution.
Classes:
-
BudgetGraphMetadata–Metadata for a single budget graph.
-
EncoderCudaGraphManager–Budget-based CUDA graph capture/replay for vision encoders.
BudgetGraphMetadata
dataclass
¶
Metadata for a single budget graph.
CUDA graph replay pattern: * Copy precomputed values into input_buffers * Replay graph * Read encoder outputs from output_buffer
EncoderCudaGraphManager
¶
Budget-based CUDA graph capture/replay for vision encoders.
Methods:
-
__init__–Initialize CUDA graph manager with provided token budgets
-
capture–Capture CUDA graphs for every configured path and token budget.
-
clear–Release captured encoder CUDA graphs and the manager-local pool.
-
execute–Execute encoder using CUDA graph with optional DP.
-
get_cumulative_stats–Get cumulative CUDA graph statistics.
-
is_captured–Return whether a CUDA graph pool is active.
-
supports_batch–Check if a batch can run through the captured encoder graphs.
-
supports_modality–Check if a modality is supported by this manager.
__init__(vllm_config, device, dtype, model)
¶
Initialize CUDA graph manager with provided token budgets and max batch size.
_capture_budget_graph(token_budget, path='default', axis_keys=())
¶
Capture CUDA graph for a single token budget.
_dp_gather(local_outputs, per_item_out_tokens, image_rank_assignment, images_per_rank, max_output_tokens_per_rank)
¶
Gather outputs from all TP ranks and reorder to original sequence.
Assumes 2D output tensors [tokens, hidden]. Follows the same pad -> all_gather -> unpad -> reorder algorithm as run_dp_sharded_mrope_vision_model() in the eager path.
_dp_shard(mm_kwargs, per_item_out_tokens)
¶
Distribute items across TP ranks for data-parallel execution.
Uses get_load_balance_assignment() to balance load by input size, then select_encoder_cudagraph_items() to extract each rank's inputs.
Returns:
-
local_mm_kwargs(dict[str, Any]) –Inputs for this rank.
-
image_rank_assignment(list[int]) –Flattened assignment order across all ranks.
-
images_per_rank(list[int]) –Number of items per rank.
-
max_output_tokens_per_rank(int) –Max output tokens across all ranks (for padding during all_gather).
_execute_local(mm_kwargs)
¶
Execute the local batch with at most one replay per path.
_find_smallest_fitting_budget_given_tokens(total_tokens, budgets=None)
¶
Find smallest budget >= total_tokens, or None if none fits.
_generate_budgets(min_budget, max_budget, cap=None)
staticmethod
¶
Generate token budgets from min_budget up to max_budget or cap.
Quarter-octave steps up to 1024 tokens, steps of 128 up to 2560, then
1/8-octave steps (rounded up to 128), so a replay pads at most ~25%
below 1024, ~12% up to 2560 and ~9% above. Capture buffers are shared
across budgets (see _capture_budget_graph), so extra graphs cost
no buffer memory. Batches above the largest budget run eagerly.
_get_graph_set(path='default')
¶
Return the captured graphs for one encoder path.
_get_item_specs(mm_kwargs)
¶
Get item specs from the model.
_get_per_item_out_tokens(mm_kwargs)
¶
Get per-item output token counts as plain ints.
_item_multiple_budgets(item_tokens, max_budget)
staticmethod
¶
Budgets for a path whose items all have item_tokens tokens.
Every item count up to 16, then 1/8-octave steps, so batches of up to 16 items fit their budget exactly.
_path_fitting_budget(path, tokens)
¶
Smallest captured budget on a path; 0 when idle, None if oversized.
_replay_exceeds_capture(input_buffers, replay_values)
staticmethod
¶
Return True when any replay tensor does not fit its capture buffer.
Capture buffers have a fixed leading dimension. Copying a longer replay tensor raises in the padded copy path; callers must fall back to eager execution instead.
_run_budget_graph(mm_kwargs, token_budget, path='default', axis_keys=())
¶
Execute budget graph.
Parameters:
-
(mm_kwargs¶dict[str, Any]) –Multimodal inputs for the batch.
-
(token_budget¶int) –Token budget to use.
-
(path¶str, default:'default') –Configured encoder path.
-
(axis_keys¶CaptureAxisKeys, default:()) –Resolved capture-axis keys for this batch.
Returns:
_select_items(mm_kwargs, indices)
¶
Select the mm kwargs for indices from the model.
Returns the sliced mm_kwargs together with the resolved capture-axis keys (popped out of the returned dict; empty without capture axes).
_share_prefix_compatible(probe, shared)
staticmethod
¶
View prefix-compatible probe buffers into the shared buffers.
For each probe buffer that is a row-prefix of its shared counterpart (same trailing dims and dtype, no more rows), copy the probe contents into the shared buffer and return the prefix view instead. Anything else, including 0-dim buffers (which hold per-budget values and save no memory when shared), keeps its own per-budget allocation.
capture(graph_pool)
¶
Capture CUDA graphs for every configured path and token budget.
clear()
¶
Release captured encoder CUDA graphs and the manager-local pool.
execute(mm_kwargs)
¶
get_cumulative_stats()
¶
Get cumulative CUDA graph statistics.
is_captured()
¶
Return whether a CUDA graph pool is active.
supports_batch(modality, mm_kwargs)
¶
Check if a batch can run through the captured encoder graphs.
Precomputed embeddings (<modality>_embeds) do not run the encoder,
so the caller must send them to embed_multimodal instead. A batch
never mixes them with pixel inputs: group_and_batch_mm_kwargs only
batches items that have the same keys.
supports_modality(modality)
¶
Check if a modality is supported by this manager.