Skip to content

vllm.model_executor.models.qwen3_vl

Inference-only Qwen3VL model compatible with HuggingFace weights.

Classes:

Functions:

Qwen3VLForConditionalGeneration

Bases: Module, SupportsMultiModal, Qwen2VLEncoderCudaGraphMixin, SupportsLoRA, SupportsPP, SupportsMRoPE, SupportsEagle, SupportsEagle3, SupportsMultiModalPruning

Methods:

_create_final_video_embeddings(video_embeddings, num_tokens_per_frame, timestamps, video_grid_thw, retention_mask)

Create final embeddings that combine video embeddings with text embeddings of indicator tokens.

These final embeddings contain: - Actual video embeddings in positions corresponding to video content - Text embeddings for indicator tokens (, , and frame separation text) in their respective positions

These embeddings will replace the placeholder embeddings to create input_embeds for the LLM.

_init_marker_embed_tokens(vllm_config, prefix)

Keep the token embedding on an encoder-only instance that prunes.

Pruned video outputs include the embeddings of the timestamp and vision marker tokens, which --mm-encoder-only otherwise leaves unloaded with the rest of the language model.

_iter_mm_grid_hw(input_tokens, mm_features, video_token_id, vision_start_token_id, vision_end_token_id, spatial_merge_size) staticmethod

Iterate over multimodal features and yield position info.

Parameters:

  • input_tokens

    (list[int]) –

    List of token IDs in the input sequence.

  • mm_features

    (list[MultiModalFeatureSpec]) –

    List of multimodal feature specifications containing image/video data and position information.

  • video_token_id

    (int) –

    Token ID used for video tokens.

  • vision_start_token_id

    (int) –

    Token ID marking the start of a vision sequence.

  • vision_end_token_id

    (int) –

    Token ID marking the end of a vision sequence.

  • spatial_merge_size

    (int) –

    Size of the spatial merge operation used to compute logical grid dimensions from the original feature grid.

Yields:

  • offset ( int ) –

    Position of the first video/image token in the sequence.

  • llm_grid_h ( int ) –

    Logical grid height (may not match actual token count with EVS).

  • llm_grid_w ( int ) –

    Logical grid width (may not match actual token count with EVS).

  • actual_num_tokens ( int ) –

    Actual number of video/image tokens in the placeholder.

_postprocess_image_embeds_evs(image_embeds_split, image_input)

Append mrope positions for each for images. This is necessary to recover correct mrope positions after video pruning

Parameters:

  • image_embeds_split

    (tuple[Tensor, ...]) –

    Tuple of image embeddings for each image item.

  • image_input

    (Qwen2_5_VLImageInputs) –

    Image input data.

Returns:

  • Tensor –

    Tuple of image embeddings for each image item.

  • ... –

    Resulting embeddings will have extra 5 channels for

  • tuple[Tensor, ...] –

    computed mrope positions, consistent with video embeddings.

_postprocess_video_embeds_evs(video_embeds_split, video_input)

Prunes video embeddings via Efficient Video Sampling (EVS) and then appends mrope positions for each retained embeddings

Parameters:

  • video_embeds_split

    (tuple[Tensor, ...]) –

    Tuple of video embeddings for each video item.

  • video_input

    (Qwen2_5_VLVideoInputs) –

    Video input data.

Returns:

  • Tensor –

    Tuple of video embeddings for each video item.

  • ... –

    Resulting embeddings will have extra 5 channels for computed mrope

  • tuple[Tensor, ...] –

    positions, and whether the index corresponds to a video embedding.

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

Run forward pass for Qwen3VL.

Parameters:

  • input_ids

    (Tensor | None) –

    Flattened (concatenated) input_ids corresponding to a batch.

  • positions

    (Tensor) –

    Flattened (concatenated) position ids corresponding to a batch. NOTE: If mrope is enabled (default setting for Qwen3VL opensource models), the shape will be (3, seq_len), otherwise it will be (seq_len,).

  • intermediate_tensors

    (IntermediateTensors | None, default: None ) –

    Intermediate tensors from previous pipeline stages.

  • inputs_embeds

    (Tensor | None, default: None ) –

    Pre-computed input embeddings.

  • **kwargs

    (object, default: {} ) –

    Additional keyword arguments including: - pixel_values: Pixel values to be fed to a model. None if no images are passed. - image_grid_thw: Tensor (n_images, 3) of image 3D grid in LLM. None if no images are passed. - pixel_values_videos: Pixel values of videos to be fed to a model. None if no videos are passed. - video_grid_thw: Tensor (n_videos, 3) of video 3D grid in LLM. None if no videos are passed.

get_mm_mapping()

Get the module prefix in multimodal models.

recompute_mrope_positions(input_ids, multimodal_embeddings, mrope_positions, num_computed_tokens)

Update part of input mrope positions (starting with num_computed_tokens index). Original mrope_positions are computed for unpruned sequence and becomes incorrect once pruning occurs, so once we prune media tokens we should reflect this in the mrope_positions before we feed it to LLM.

Parameters:

  • input_ids

    (list[int] | Tensor) –

    (N,) All input tokens of the prompt containing entire sequence.

  • multimodal_embeddings

    (Sequence[Tensor]) –

    Sequence of multimodal embeddings that fits into the prefill chunk that is being processed.

  • mrope_positions

    (LongTensor) –

    Existing mrope positions (3, N) for entire sequence

  • num_computed_tokens

    (int) –

    A number of computed tokens so far.

Returns:

Qwen3VLMultiModalProcessor

Bases: BaseMultiModalProcessor[Qwen3VLProcessingInfo]

Methods:

  • get_video_repl –

    Build prompt replacement for a video in Qwen3VL format.

get_video_repl(*, tokens_per_frame, timestamps, tokenizer, vision_start_token_id, vision_end_token_id, video_token_id, select_token_id=False) staticmethod

Build prompt replacement for a video in Qwen3VL format.

The replacement structure for each frame is: timestamp_tokens + vision_start_token + video_tokens + vision_end_token

Parameters:

  • tokens_per_frame

    (list[int]) –

    Number of video tokens per frame (can vary per frame for EVS).

  • timestamps

    (Sequence[float] | Tensor) –

    List of timestamps in seconds for each frame

  • tokenizer

    (TokenizerLike) –

    Tokenizer to encode timestamp strings

  • vision_start_token_id

    (int) –

    Token ID for vision start marker

  • vision_end_token_id

    (int) –

    Token ID for vision end marker

  • video_token_id

    (int) –

    Token ID for video content

  • select_token_id

    (bool, default: False ) –

    Whether to return token ids instead of text

Returns:

Qwen3_VisionTransformer

Bases: Module

Methods:

prepare_encoder_metadata(grid_thw_list, *, max_batch_size=None, max_frames_per_batch=None, max_seqlen_override=None, device=None)

Compute encoder metadata from grid_thw_list.

Shared by the eager forward path, CUDA graph capture, and CUDA graph replay to avoid duplicated implementation.

Parameters:

  • grid_thw_list

    (Sequence[Sequence[int]]) –

    Grid configurations as list of [t, h, w].

  • max_batch_size

    (int | None, default: None ) –

    If set, pad cu_seqlens to this size (needed for CUDA graph capture/replay).

  • max_frames_per_batch

    (int | None, default: None ) –

    If set, overrides max_batch_size for cu_seqlens padding. For video inputs each item contributes T attention sequences (frames); this sizes the buffer to the total frame budget so video replays never overflow.

  • max_seqlen_override

    (int | None, default: None ) –

    If set, use this value for max_seqlen instead of computing from cu_seqlens (needed for CUDA graph capture to cover worst-case replay scenarios).

  • device

    (device | None, default: None ) –

    Device to place tensors on. Defaults to self.device.

_bilinear_pos_embed_kernel(embed_ptr, output_ptr, H, W, h_scale, w_scale, NUM_GRID, M_SIZE, HIDDEN_DIM, BLOCK_D)

Fused bilinear pos-embed interpolation with spatial-merge reorder.

_get_num_sampled_frames(video_processor, metadata, num_frames, fps)

Length of HF's sample_frames output, without allocating the indices.

pos_embed_interpolate_native(embed_weight, t, h, w, num_grid_per_side, m_size, dtype)

Eager PyTorch bilinear position-embedding interpolation.

Returns a tensor of shape (t * h * w, hidden_dim) with the bilinearly-interpolated position embeddings in spatial-merge order.

triton_pos_embed_interpolate(embed_weight, t, h, w, num_grid_per_side, m_size, dtype)

Launch the fused Triton kernel for one (t,h,w) grid.

Returns a tensor of shape (t * h * w, hidden_dim) with the bilinearly-interpolated position embeddings in spatial-merge order.