Skip to content

vllm.model_executor.models.pixtral

Classes:

Functions:

PatchMerger

Bases: Module

Learned merging of spatial_merge_size ** 2 patches.

Methods:

forward_packed(x, merge_indices)

Same as forward, gathering blocks with _make_merge_indices.

permute(x, image_sizes)

Parameters:

  • x

    (Tensor) –

    (N, D) where N is flattened and concatenated patch tokens for all images

  • image_sizes

    (list[tuple[int, int]]) –

    list of tuple of (height, width) in tokens for each image

Returns: image_features: reorders patch tokens so each grid of (spatial_merge_size, spatial_merge_size) is contiguous. now (N / spatial_merge_size ** 2, D * spatial_merge_size ** 2)

PixtralEncoderCudaGraphMixin

Bases: SupportsEncoderCudaGraph

Encoder CUDA graph support for models built on a Pixtral vision tower.

All images of a batch are packed into one patch sequence. Capture buffers are sized for the token budget, and the batch is padded with zero-length sequences up to max_batch_size, so replays only copy fresh values into fixed-shape buffers.

Methods:

Attributes:

encoder_cudagraph_spatial_merge_size property

Patches merged per side by the projector, 1 when not merging.

project_packed_image_features(features, merge_indices)

Map (N_patches, D) tower features to language model embeddings.

PixtralForConditionalGeneration

Bases: Module, SupportsLoRA, SupportsEagle3, SupportsMultiModal, SupportsPP, PixtralEncoderCudaGraphMixin

Methods:

  • forward –

    Run forward pass for pixtral.

forward(input_ids, positions, intermediate_tensors=None, inputs_embeds=None, **kwargs)

Run forward pass for pixtral.

PixtralHFVisionModel

Bases: Module

Methods:

Attributes:

encoder_attention property

Attention of the first layer; all layers share its settings.

forward(pixel_values, *, select_layers=None, feature_select_strategy=None)

Parameters:

  • pixel_values

    (list[Tensor]) –

    Each image to be processed will be a separate tensor in pixel_values. This means it will be a list of tensors because multiple requests batched can have multiple images, each with their own shape potentially

  • select_layers

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

    Layer indices whose features should be concatenated and used as the visual encoder output. If none are provided, the last layer is used.

Returns:

  • image_features ( tuple[Tensor, ...] ) –

    tensor of token features for all tokens of all images of shape (N_toks, D)

forward_packed(patches, positions, cu_seqlens, max_seqlen, sequence_lengths, *, select_layers=None, feature_select_strategy=None)

Encode images packed by pack_image_patches.

patches holds (N_toks, C * patch_size**2) pixel patches (raw pixels when input_norm normalizes on device) and positions their (N_toks, 2) (row, col) coordinates; the attention arguments come from packed_attention_metadata. Returns (N_toks, D) token features.

PixtralImagePixelInputs

Bases: TensorSchema

Dimensions
  • bn: Batch size * number of images
  • c: Number of channels (3)
  • h: Height of each image
  • w: Width of each image

The result of stacking ImageEncoding.tokens from each prompt.

VisionTransformer

Bases: Module

Methods:

Attributes:

encoder_attention property

Attention of the first layer; all layers share its settings.

forward(images)

Parameters:

  • images

    (list[Tensor]) –

    list of N_img images of variable sizes, each of shape (C, H, W)

Returns:

  • image_features ( tuple[Tensor, ...] ) –

    per-image token features, each of shape (N_toks_i, D)

forward_packed(patches, positions, cu_seqlens, max_seqlen, sequence_lengths)

Encode images packed by pack_image_patches.

patches holds (N_toks, C * patch_size**2) pixel patches and positions their (N_toks, 2) (row, col) coordinates; the attention arguments come from packed_attention_metadata. Returns (N_toks, D) token features.

_make_merge_indices(grid_sizes, spatial_merge_size, device)

Indices gathering each spatial_merge_size^2 patch block of the packed sequence into one row, matching the patch mergers' unfold order.

_make_packed_sequence_metadata(sequence_lengths, attn_backend, hidden_size, tp_size, device, *, num_sequences=None, fp8_padded_hidden_size=None)

Build varlen attention metadata for images packed in one sequence.

num_sequences pads the batch with zero-length sequences, which keeps the metadata shapes fixed for encoder CUDA graphs while attention does no work for the padding rows.

_patch_grid_positions(grid_sizes, device)

(row, col) coordinates of every patch, packed as (num_patches, 2).

apply_rotary_emb_vit(xq, xk, freqs_cis)

Rotate xq/xk of shape (bsz, seq_len, n_heads, head_dim).

freqs_cis is the real view (seq_len, head_dim / 2, 2) of the complex frequencies.

pack_image_patches(images, patch_shape)

Pack CHW images into one (num_patches, C * ph * pw) tensor.

Patches are laid out row-major per image, with each row flattened in the (C, ph, pw) order of a patch convolution weight. Returns the packed tensor and the (rows, cols) patch grid of every image.

pixtral_patch_embed(images, weight, input_norm)

Normalize and project all CHW images in one matrix multiplication.

precompute_freqs_cis_2d(dim, height, width, theta)

freqs_cis: 2D complex tensor of shape (height, width, dim // 2) to be indexed by (height, width) position tuples