Skip to content

vllm.model_executor.models.vision

Classes:

Functions:

InterpolatedPosEmbedCache

Bases: Module

Pre-allocated per-module cache for interpolated positional embeddings.

Allocates a fixed non-persistent byte buffer at module initialization so its memory footprint is accounted for during model loading and profiling with zero growth during inference. Safe across weight loading/reloads (process_weights_after_loading), encoder graph capture (refreshes slots in place and never evicts live slots), and torch.compile (bypasses cache).

Methods:

  • refresh –

    Refresh cached entries in place after weights are loaded or updated.

refresh(weight, compute_fn)

Refresh cached entries in place after weights are loaded or updated.

_get_vit_attn_backend(head_size, dtype, *, attn_backend_override=None)

Get the available attention backend for Vision Transformer.

get_fp8_padded_hidden_size(num_heads, head_dim)

Return the padded hidden size for FP8 ViT encoder attention, or None when FP8 is not enabled.

cuDNN FP8 prefill attention requires head_dim to be a multiple of 16. For non-aligned head_dim (e.g. 72), Q/K/V are padded to the nearest multiple of 16.

get_load_balance_assignment(sizes, num_gpus=2)

Generate load balancing assignment and metadata for distributing data across GPUs. The load is determined by the total image sizes, not the number of images.

Parameters:

  • sizes

    (list[int]) –

    The size of each image

  • num_gpus

    (int, default: 2 ) –

    Number of GPUs to balance across

Returns:

  • shuffle_indices ( list[int] ) –

    Indices to reorder data for balanced loading

  • gpu_sample_counts ( list[int] ) –

    Number of samples assigned to each GPU

  • grouped_sizes_per_gpu ( list[int] ) –

    Total size assigned to each GPU

Example
sizes = [1000, 100, 200, 50]
num_gpus = 2

get_multimodal_config()

Return the current MultiModalConfig, or None when no engine config context is active (e.g., during unit tests) or when the current model_config does not carry a multimodal_config (e.g., minimal stubs used in tests).

get_vit_attn_backend(head_size, dtype)

Get the attention backend for Vision Transformer.

interpolate_cls_abs_pos(abs_pos, tgt_grid)

Bicubic-interpolate 2-D patch embeddings while keeping a leading CLS token.

is_vit_use_data_parallel(num_heads=None)

Get the tensor parallel type for Vision Transformer.

resolve_visual_encoder_outputs(encoder_outputs, post_layer_norm, *, select_layers=None, max_possible_layers=None, last_hs_proc=None, feature_select_strategy=None)

Given the outputs a visual encoder module that may correspond to the output of the last layer, or a list of hidden states to be stacked, handle post normalization and resolve it into a single output tensor.

Parameters:

  • encoder_outputs

    (Tensor | list[Tensor]) –

    Output of encoder's last layer or all hidden states.

  • post_layer_norm

    (LayerNorm | None) –

    Post norm to apply to the output of the encoder.

  • select_layers

    (list[int] | None, default: None ) –

    Optional layer indices to grab from the encoder outputs; if provided, encoder outputs must be a list.

  • max_possible_layers

    (int | None, default: None ) –

    Total layers in the fully loaded visual encoder.

  • last_hs_proc

    (Callable[[Tensor], Tensor] | None, default: None ) –

    Optional callable to be applied to the last layer if it is used, e.g., pooling head for Siglip. This is done prior to feature selection and layer normalization. If select_layers are provided, the output of last_hs_proc must be able to be concatenated with the other select_layers along the last dimension.

  • feature_select_strategy

    (VisionFeatureSelectStrategy | None, default: None ) –

    Defines how to select the hidden states from each layer.

run_dp_sharded_mrope_vision_model(vision_model, pixel_values, grid_thw_list, *, rope_type)

Run a vision model with data parallelism (DP) sharding. The function will shard the input image tensor on the first dimension and run the vision model. This function is used to run the vision model with mrope.

Parameters:

  • vision_model

    (Module) –

    Vision model.

  • pixel_values

    (Tensor) –

    Image/Video input tensor.

  • grid_thw_list

    (list[list[int]]) –

    List of grid dimensions for each image

  • rope_type

    (Literal['rope_3d', 'rope_2d']) –

    Type of rope used in the vision model. Different rope types have different dimension to do ViT. "rope_3d" for 3D rope (e.g., Qwen2.5-VL) "rope_2d" for 2D rope (e.g., Kimi-VL)

Returns:

  • tuple[Tensor, ...] –

    torch.Tensor: Output image embeddings

Example
vision_model.out_hidden_size = 64
vision_model.spatial_merge_size = 2
pixel_values.shape = (1350, channel)
grid_thw_list = [[1, 10, 100], [1, 10, 10], [1, 10, 20], [1, 50]]
tp_size = 2

run_dp_sharded_vision_model(image_input, vision_model)

Run a vision model with data parallelism (DP) sharding. The function will shard the input image tensor on the first dimension and run the vision model

Parameters:

  • image_input

    (Tensor) –

    Image input tensor.

  • vision_model

    (Module) –

    Vision model.

Returns:

  • Tensor –

    torch.Tensor: Output image embeddings