vllm.multimodal.utils
¶
Functions:
-
argsort_mm_positions–Given a
MultiModalPlaceholders, output a sequence of keys to -
copy_mm_embedding_modality–Copy
modalityandmm_placementfromsrcontodstif present. -
encode_audio_base64–Encode audio as base64.
-
encode_audio_url–Encode audio as a data URL.
-
encode_image_base64–Encode a pillow image to base64 format.
-
encode_image_url–Encode a pillow image as a data URL.
-
fetch_audio–Args:
-
fetch_image–Args:
-
fetch_video–Args:
-
get_load_audio_from_video–Whether each video's audio track is loaded and fused into the video, or
-
get_mm_embedding_modalities–Collect per-embedding modalities previously set on the tensors.
-
get_mm_embedding_modality_masks–Build a position mask per modality from each embedding's
mm_placement. -
get_mm_features_in_window–Return (lo, hi) indices for features overlapping [start, end).
-
group_and_batch_mm_items–Group consecutive items (possibly from different requests) into batches.
-
group_and_batch_mm_kwargs–Group consecutive items (possibly from different requests) into batches.
-
set_mm_embedding_modality–Attach modality metadata to a gathered multimodal embedding tensor.
-
strip_covered_mm_data–Drop the tensor data of mm items whose placeholder span is fully inside
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:
-
list[tuple[str, int]]–A list of
(modality, idx), which can be used to access an item -
list[tuple[str, int]]–by
mm_positions[modality][idx].
Source code in vllm/multimodal/utils.py
copy_mm_embedding_modality(src, dst)
¶
Copy modality and mm_placement from src onto dst if present.
Source code in vllm/multimodal/utils.py
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.
Source code in vllm/multimodal/utils.py
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.
Source code in vllm/multimodal/utils.py
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.
Source code in vllm/multimodal/utils.py
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!
Source code in vllm/multimodal/utils.py
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!
Source code in vllm/multimodal/utils.py
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!
Source code in vllm/multimodal/utils.py
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.
Source code in vllm/multimodal/utils.py
get_mm_embedding_modalities(multimodal_embeddings)
¶
Collect per-embedding modalities previously set on the tensors.
Source code in vllm/multimodal/utils.py
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.
Source code in vllm/multimodal/utils.py
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.
Source code in vllm/multimodal/utils.py
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:
-
Generator[tuple[int, BatchedTensorInputs]]–A tuple
(num_items, grouped_kwargs), where: -
Generator[tuple[int, BatchedTensorInputs]]–kwargsis a dictionary of keyword arguments to pass to the model;
-
Generator[tuple[int, BatchedTensorInputs]]–num_itemsis the corresponding number of items.
Source code in vllm/multimodal/utils.py
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–modalityis the modality of the batch;
-
BatchedTensorInputs–kwargsis a dictionary of keyword arguments to pass to the model;
-
tuple[str, int, BatchedTensorInputs]–num_itemsis the corresponding number of items.
Source code in vllm/multimodal/utils.py
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.
Source code in vllm/multimodal/utils.py
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.