Skip to content

vllm.multimodal.utils

Functions:

argsort_mm_positions(mm_positions)

Given a MultiModalPlaceholders, output a sequence of keys to sort the dictionary by offset (starting index in the input sequence) in ascending order.

Returns:

copy_mm_embedding_modality(src, dst)

Copy modality and mm_placement from src onto dst if present.

encode_audio_base64(audio, sampling_rate, *, format='WAV')

Encode audio as base64.

encode_audio_url(audio, sampling_rate, *, format='WAV')

Encode audio as a data URL.

encode_image_base64(image, *, image_mode='RGB', format='PNG')

Encode a pillow image to base64 format.

By default, the image is converted into RGB format before being encoded. Pass image_mode=None to keep the original image mode.

encode_image_url(image, *, image_mode='RGB', format='PNG')

Encode a pillow image as a data URL.

By default, the image is converted into RGB format before being encoded. Pass image_mode=None to keep the original image mode.

fetch_audio(audio_url, audio_io_kwargs=None)

Parameters:

  • audio_url

    (str) –

    URL of the audio file to fetch.

  • audio_io_kwargs

    (dict[str, Any] | None, default: None ) –

    Additional kwargs passed to handle audio IO.

Warning

This method has direct access to local files and is only intended to be called by user code. Never call this from the online server!

fetch_image(image_url, image_io_kwargs=None)

Parameters:

  • image_url

    (str) –

    URL of the image file to fetch.

  • image_io_kwargs

    (dict[str, Any] | None, default: None ) –

    Additional kwargs passed to handle image IO.

Warning

This method has direct access to local files and is only intended to be called by user code. Never call this from the online server!

fetch_video(video_url, video_io_kwargs=None)

Parameters:

  • video_url

    (str) –

    URL of the video file to fetch.

  • video_io_kwargs

    (dict[str, Any] | None, default: None ) –

    Additional kwargs passed to handle video IO.

Warning

This method has direct access to local files and is only intended to be called by user code. Never call this from the online server!

get_load_audio_from_video(kwargs)

Whether each video's audio track is loaded and fused into the video, or None if the processor kwargs don't say.

load_audio_from_video is the name Transformers uses for this processor setting, replacing the deprecated use_audio_in_video, which is still read as a fallback until vLLM moves to the new name everywhere.

get_mm_embedding_modalities(multimodal_embeddings)

Collect per-embedding modalities previously set on the tensors.

get_mm_embedding_modality_masks(multimodal_embeddings, is_multimodal)

Build a position mask per modality from each embedding's mm_placement.

Returns None if any embedding lacks a modality or placement, in which case callers must derive the masks from input_ids instead.

get_mm_features_in_window(mm_features, start, end)

Return (lo, hi) indices for features overlapping [start, end).

Assumes mm_features are sorted by offset and non-overlapping, so offset + length is also sorted.

group_and_batch_mm_items(items, *, device=None, pin_memory=False)

Group consecutive items (possibly from different requests) into batches.

Items must be split across groups if any of the following occurs, as the batch would otherwise be invalid: - They have different fields (e.g. mixed image and embedding inputs). - They have different values in MultiModalSharedField.

Parameters:

  • items

    (Sequence[MultiModalKwargsItem]) –

    List of MultiModalKwargsItem.

  • device

    (Device, default: None ) –

    The device to place the grouped tensors on.

  • pin_memory

    (bool, default: False ) –

    Whether to pin memory for faster host-to-device transfer.

Yields:

group_and_batch_mm_kwargs(mm_kwargs, *, device=None, pin_memory=False)

Group consecutive items (possibly from different requests) into batches.

Items must be split across groups if any of the following occurs, as the batch would otherwise be invalid: - They have different fields (e.g. mixed image and embedding inputs). - They have different values in MultiModalSharedField.

To simplify the implementation of embed_multimodal, we add another restriction that the items in a batch must belong to the same modality.

Parameters:

  • mm_kwargs

    (list[tuple[str, MultiModalKwargsItem]]) –

    List of (modality, item).

  • device

    (Device, default: None ) –

    The device to place the grouped tensors on.

  • pin_memory

    (bool, default: False ) –

    Whether to pin memory for faster host-to-device transfer.

Yields:

  • str –

    A tuple (modality, num_items, grouped_kwargs), where:

  • int –
    • modality is the modality of the batch;
  • BatchedTensorInputs –
    • kwargs is a dictionary of keyword arguments to pass to the model;
  • tuple[str, int, BatchedTensorInputs] –
    • num_items is the corresponding number of items.

set_mm_embedding_modality(embed, modality, placement=None)

Attach modality metadata to a gathered multimodal embedding tensor.

Used by interleaved Omni merge paths that need to group embeddings by modality without threading a parallel modalities list through embed_input_ids.

placement is (start, end, is_embed): the embedding fills the batch positions [start, end) selected by the CPU mask is_embed (all of them if None). It lets models build per-modality position masks without reading input_ids on the host.

strip_covered_mm_data(mm_features, num_computed_tokens, uses_mrope=False)

Drop the tensor data of mm items whose placeholder span is fully inside a prefix-cache-covered region: no encoder run can be scheduled for them, so the workers never consume the payload fields. M-RoPE models keep CPU-side metadata fields used to compute positions. SHM address items are also kept so workers can balance the sender's reference count. The scheduler-side Request keeps the full features.