Skip to content

vllm.model_executor.models.granite4_vision

vLLM implementation of Granite 4 Vision.

Uses GraniteForCausalLM as the language backbone with SigLIP vision encoder and deepstack feature injection via WindowQFormer projectors.

LoRA support: use --enable-lora --default-mm-loras for LM-only LoRA adapters.

Classes:

Granite4VisionForConditionalGeneration

Bases: Module, SupportsLoRA, SupportsMultiModal, SupportsPP

vLLM implementation of Granite 4 Vision.

Architecture: - SigLIP vision tower -> WindowQFormerDownsampler projectors - Deepstack: 4 vision layers projected and injected at 4 LLM layers - Spatial: 4 offset groups from last vision layer injected at 4 more LLM layers - Granite language backbone with embedding_multiplier - logits_scaling via LogitsProcessor

The outer model runs the LLM layer loop directly (like HF does) to inject deepstack features. This avoids wrapping the inner model and keeps weight loading simple.

LoRA support: - Full merge: --hf-overrides '{"adapter_path": "path/to/lora"}' merges LM-only LoRA deltas at load time (W += scaling * B @ A). - Native LoRA: --enable-lora --default-mm-loras '{"image": "path/to/lora"}' lets vLLM runtime serve LM LoRA per-request. Both modes expect a LM-only adapter (no modules_to_save).

Methods:

  • embed_input_ids –

    Merge text and vision embeddings, apply embedding_multiplier.

  • embed_multimodal –

    Run vision tower and return per-image packed feature tensors.

_get_all_layer_features(pixel_values, image_sizes)

Extract deepstack + spatial features for all levels.

Returns:

  • llm_layer_indices ( list[int] ) –

    ordered list of target LLM layer indices

  • per_image_packed ( list[Tensor] ) –

    one tensor per image, shape (num_tokens_i, lm_hidden_size * num_levels), all levels packed on dim=-1.

Packing on dim=-1 means the framework's token-level slicing for chunked prefill preserves all levels intact.

_get_vision_hidden_states(pixel_values)

Run vision tower and return all hidden states (including input embeddings).

Uses SiglipEncoder's built-in return_all_hidden_states support. Returns list[Tensor] where index 0 = embeddings, index i = after layer i-1.

_pack_and_unpad_image_features(image_features, image_sizes)

Reshape, unpad, and pack image features.

Matches HF Granite4VisionModel.pack_and_unpad_image_features exactly.

embed_input_ids(input_ids, multimodal_embeddings=None, *, is_multimodal=None, handle_oov_mm_token=True)

Merge text and vision embeddings, apply embedding_multiplier.

HF flow: 1. inputs_embeds = embed_tokens(input_ids) 2. inputs_embeds.masked_fill(vision_mask, 0.0) 3. hidden_states = inputs_embeds * embedding_multiplier 4. layer loop injects deepstack features at target layers

multimodal_embeddings contains packed tensors from embed_multimodal(): shape (num_tokens_i, lm_hidden_size * num_levels). We split on dim=-1 to get per-level features, build batch-sized buffers (zero at text positions), and store in self._ds_features for forward().

embed_multimodal(**kwargs)

Run vision tower and return per-image packed feature tensors.

Each returned tensor has shape (num_tokens_i, lm_hidden_size * num_levels) with all deepstack levels packed on dim=-1. The framework caches these tensors and slices along dim=0 for chunked prefill — all levels survive intact because slicing is token-wise, not feature-wise.

embed_input_ids() splits the packed tensor back into per-level buffers.

Granite4VisionLLMForCausalLM

Bases: GraniteForCausalLM

GraniteForCausalLM backed by Granite4VisionLLMModel.

Granite4VisionLLMModel

Bases: GraniteModel

GraniteModel with deepstack feature injection in the layer loop.

InterpolateDownsampler

Spatial downsampling via area interpolation.

SpatialOffsetDownsampler

Sample one position from each 2x2 block (offset 0-3 = TL/TR/BL/BR).

WindowQFormerDownsampler

Bases: Module

Window-based QFormer downsampler (matches HF downsampling.py exactly).

_unwin(xw, n, win)

(Bnn, winwin, C) → (B, (nwin)^2, C).

_win(x, side, win)

(B, sideside, C) → (Bnn, winwin, C) where n=side//win.