Convert formatting to use ruff instead of yapf + isort (#26247)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor
2025-10-05 07:06:22 -07:00
committed by GitHub
parent 17edd8a807
commit d6953beb91
1508 changed files with 115205 additions and 94107 deletions
+106 -78
View File
@@ -7,8 +7,7 @@ from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from functools import partial
from itertools import accumulate
from typing import (TYPE_CHECKING, Any, Literal, Optional, TypedDict, Union,
cast, final)
from typing import TYPE_CHECKING, Any, Literal, Optional, TypedDict, Union, cast, final
import numpy as np
from typing_extensions import NotRequired, TypeAlias, TypeVar, deprecated
@@ -35,8 +34,9 @@ A `transformers.image_utils.ImageInput` representing a single image
item, which can be passed to a HuggingFace `ImageProcessor`.
"""
HfVideoItem: TypeAlias = Union[list["Image"], np.ndarray, "torch.Tensor",
list[np.ndarray], list["torch.Tensor"]]
HfVideoItem: TypeAlias = Union[
list["Image"], np.ndarray, "torch.Tensor", list[np.ndarray], list["torch.Tensor"]
]
"""
A `transformers.image_utils.VideoInput` representing a single video
item, which can be passed to a HuggingFace `VideoProcessor`.
@@ -58,8 +58,9 @@ which are treated as image embeddings;
these are directly passed to the model without HF processing.
"""
VideoItem: TypeAlias = Union[HfVideoItem, "torch.Tensor",
tuple[HfVideoItem, dict[str, Any]]]
VideoItem: TypeAlias = Union[
HfVideoItem, "torch.Tensor", tuple[HfVideoItem, dict[str, Any]]
]
"""
A `transformers.video_utils.VideoInput` representing a single video item.
This can be passed to a HuggingFace `VideoProcessor`
@@ -70,8 +71,7 @@ which are treated as video embeddings;
these are directly passed to the model without HF processing.
"""
AudioItem: TypeAlias = Union[HfAudioItem, tuple[np.ndarray, float],
"torch.Tensor"]
AudioItem: TypeAlias = Union[HfAudioItem, tuple[np.ndarray, float], "torch.Tensor"]
"""
Represents a single audio
item, which can be passed to a HuggingFace `AudioProcessor`.
@@ -177,8 +177,12 @@ class PlaceholderRange:
return nested_tensors_equal(self.is_embed, other.is_embed)
NestedTensors: TypeAlias = Union[list["NestedTensors"], list["torch.Tensor"],
"torch.Tensor", tuple["torch.Tensor", ...]]
NestedTensors: TypeAlias = Union[
list["NestedTensors"],
list["torch.Tensor"],
"torch.Tensor",
tuple["torch.Tensor", ...],
]
"""
Uses a list instead of a tensor if the dimensions of each element do not match.
"""
@@ -193,11 +197,13 @@ def nested_tensors_equal(a: NestedTensors, b: NestedTensors) -> bool:
return isinstance(a, torch.Tensor) and torch.equal(b, a)
if isinstance(a, list):
return (isinstance(b, list)
and all(nested_tensors_equal(a_, b_) for a_, b_ in zip(a, b)))
return isinstance(b, list) and all(
nested_tensors_equal(a_, b_) for a_, b_ in zip(a, b)
)
if isinstance(b, list):
return (isinstance(a, list)
and all(nested_tensors_equal(b_, a_) for b_, a_ in zip(b, a)))
return isinstance(a, list) and all(
nested_tensors_equal(b_, a_) for b_, a_ in zip(b, a)
)
# Both a and b are scalars
return a == b
@@ -214,7 +220,7 @@ A dictionary containing nested tensors which have been batched via
class MultiModalFeatureSpec:
"""
Represents a single multimodal input with its processed data and metadata.
Used by the V1 engine to track multimodal data through processing and
caching. A request containing multiple multimodal items will have one
MultiModalFeatureSpec per item.
@@ -280,9 +286,11 @@ class MultiModalFieldElem:
else:
data_equal = nested_tensors_equal(self.data, other.data)
return ((self.modality, self.key) == (other.modality, other.key)
and data_equal
and type(self.field) == type(other.field)) # noqa: E721
return (
(self.modality, self.key) == (other.modality, other.key)
and data_equal
and type(self.field) == type(other.field)
) # noqa: E721
@dataclass(frozen=True)
@@ -385,10 +393,12 @@ class MultiModalBatchedField(BaseMultiModalField):
return batch[0].unsqueeze(0).contiguous()
first_shape = batch[0].shape
if all(elem.shape == first_shape for elem in batch):
out = torch.empty((len(batch), *batch[0].shape),
dtype=batch[0].dtype,
device=batch[0].device,
pin_memory=pin_memory)
out = torch.empty(
(len(batch), *batch[0].shape),
dtype=batch[0].dtype,
device=batch[0].device,
pin_memory=pin_memory,
)
return torch.stack(batch, out=out)
return batch
@@ -401,6 +411,7 @@ class MultiModalFlatField(BaseMultiModalField):
[`MultiModalFieldConfig.flat`][vllm.multimodal.inputs.MultiModalFieldConfig.flat]
[`MultiModalFieldConfig.flat_from_sizes`][vllm.multimodal.inputs.MultiModalFieldConfig.flat_from_sizes]
"""
slices: Union[Sequence[slice], Sequence[Sequence[slice]]]
dim: int = 0
@@ -412,8 +423,9 @@ class MultiModalFlatField(BaseMultiModalField):
) -> Sequence[MultiModalFieldElem]:
field_factory = self._field_factory(modality=modality, key=key)
if not is_list_of(self.slices, slice, check="all"):
assert isinstance(data, torch.Tensor), \
assert isinstance(data, torch.Tensor), (
"torch.Tensor is required for multiple slices"
)
return [field_factory(data[cast(slice, s)]) for s in self.slices]
def _reduce_data(
@@ -433,17 +445,19 @@ class MultiModalFlatField(BaseMultiModalField):
dim = self.dim + (self.dim < 0) * len(batch[0].shape)
def _shape_before_after(tensor: torch.Tensor):
return tensor.shape[:dim], tensor.shape[dim + 1:]
return tensor.shape[:dim], tensor.shape[dim + 1 :]
first_shape = _shape_before_after(batch[0])
if all(_shape_before_after(elem) == first_shape for elem in batch):
shape_before, shape_after = first_shape
shape_concat = sum(item.shape[dim] for item in batch)
out = torch.empty((*shape_before, shape_concat, *shape_after),
dtype=batch[0].dtype,
device=batch[0].device,
pin_memory=pin_memory)
out = torch.empty(
(*shape_before, shape_concat, *shape_after),
dtype=batch[0].dtype,
device=batch[0].device,
pin_memory=pin_memory,
)
return torch.concat(batch, dim=self.dim, out=out)
assert self.dim == 0, "dim == 0 is required for nested list"
@@ -456,6 +470,7 @@ class MultiModalSharedField(BaseMultiModalField):
Info:
[`MultiModalFieldConfig.shared`][vllm.multimodal.inputs.MultiModalFieldConfig.shared]
"""
batch_size: int
def build_elems(
@@ -477,7 +492,6 @@ class MultiModalSharedField(BaseMultiModalField):
class MultiModalFieldConfig:
@staticmethod
def batched(modality: str):
"""
@@ -508,9 +522,11 @@ class MultiModalFieldConfig:
)
@staticmethod
def flat(modality: str,
slices: Union[Sequence[slice], Sequence[Sequence[slice]]],
dim: int = 0):
def flat(
modality: str,
slices: Union[Sequence[slice], Sequence[Sequence[slice]]],
dim: int = 0,
):
"""
Defines a field where an element in the batch is obtained by
slicing along the first dimension of the underlying data.
@@ -561,9 +577,7 @@ class MultiModalFieldConfig:
)
@staticmethod
def flat_from_sizes(modality: str,
size_per_item: "torch.Tensor",
dim: int = 0):
def flat_from_sizes(modality: str, size_per_item: "torch.Tensor", dim: int = 0):
"""
Defines a field where an element in the batch is obtained by
slicing along the first dimension of the underlying data.
@@ -609,13 +623,17 @@ class MultiModalFieldConfig:
"""
if size_per_item.ndim != 1:
raise ValueError("size_per_item should be a 1-D tensor, "
f"but found shape: {size_per_item.shape}")
raise ValueError(
"size_per_item should be a 1-D tensor, "
f"but found shape: {size_per_item.shape}"
)
slice_idxs = [0, *accumulate(size_per_item)]
slices = [(slice(None, None, None), ) * dim +
(slice(slice_idxs[i], slice_idxs[i + 1]), )
for i in range(len(size_per_item))]
slices = [
(slice(None, None, None),) * dim
+ (slice(slice_idxs[i], slice_idxs[i + 1]),)
for i in range(len(size_per_item))
]
return MultiModalFieldConfig.flat(modality, slices, dim=dim)
@@ -745,7 +763,8 @@ class MultiModalKwargsItems(UserDict[str, Sequence[_I]]):
if len(set(batch_sizes.values())) > 1:
raise ValueError(
f"Cannot merge different batch sizes for {modality=}! "
f"Found: {batch_sizes=}")
f"Found: {batch_sizes=}"
)
batch_size = next(iter(batch_sizes.values()))
for item_idx in range(batch_size):
@@ -761,8 +780,10 @@ class MultiModalKwargsItems(UserDict[str, Sequence[_I]]):
def __getitem__(self, modality: str) -> Sequence[_I]:
if modality not in self:
raise KeyError(f"Modality {modality!r} not found. "
f"Available modalities: {set(self.keys())}")
raise KeyError(
f"Modality {modality!r} not found. "
f"Available modalities: {set(self.keys())}"
)
return super().__getitem__(modality) # type: ignore[return-value]
@@ -770,8 +791,7 @@ class MultiModalKwargsItems(UserDict[str, Sequence[_I]]):
for modality, items in self.items():
for i, item in enumerate(items):
if item is None:
raise RuntimeError(
f"Found empty mm_items[{modality}][{i}]")
raise RuntimeError(f"Found empty mm_items[{modality}][{i}]")
return self # type: ignore[return-value]
@@ -780,17 +800,19 @@ class MultiModalKwargsItems(UserDict[str, Sequence[_I]]):
for modality, items in self.items():
for i, item in enumerate(items):
if item is None:
raise RuntimeError("Cannot build data from empty "
f"mm_items[{modality}][{i}]")
raise RuntimeError(
f"Cannot build data from empty mm_items[{modality}][{i}]"
)
for key, elem in item.items():
elems_by_key[key].append(elem)
return MultiModalKwargs({
key:
elems[0].field.reduce_data(elems, pin_memory=pin_memory)
for key, elems in elems_by_key.items()
})
return MultiModalKwargs(
{
key: elems[0].field.reduce_data(elems, pin_memory=pin_memory)
for key, elems in elems_by_key.items()
}
)
MultiModalKwargsOptionalItems: TypeAlias = Union[
@@ -806,33 +828,36 @@ class MultiModalKwargs(UserDict[str, NestedTensors]):
"""
@staticmethod
@deprecated("`MultiModalKwargs.from_hf_inputs` is deprecated and "
"will be removed in v0.13. "
"Please use `MultiModalKwargsItems.from_hf_inputs` and "
"access the tensor data using `.get_data()`.")
@deprecated(
"`MultiModalKwargs.from_hf_inputs` is deprecated and "
"will be removed in v0.13. "
"Please use `MultiModalKwargsItems.from_hf_inputs` and "
"access the tensor data using `.get_data()`."
)
def from_hf_inputs(
hf_inputs: "BatchFeature",
config_by_key: Mapping[str, MultiModalFieldConfig],
):
return MultiModalKwargsItems.from_hf_inputs(hf_inputs, config_by_key) \
.get_data()
return MultiModalKwargsItems.from_hf_inputs(hf_inputs, config_by_key).get_data()
@staticmethod
@deprecated("`MultiModalKwargs.from_items` is deprecated and "
"will be removed in v0.13. "
"Please use `MultiModalKwargsItems.from_seq` and "
"access the tensor data using `.get_data()`.")
@deprecated(
"`MultiModalKwargs.from_items` is deprecated and "
"will be removed in v0.13. "
"Please use `MultiModalKwargsItems.from_seq` and "
"access the tensor data using `.get_data()`."
)
def from_items(
items: Sequence[MultiModalKwargsItem],
*,
pin_memory: bool = False,
):
return MultiModalKwargsItems.from_seq(items) \
.get_data(pin_memory=pin_memory)
return MultiModalKwargsItems.from_seq(items).get_data(pin_memory=pin_memory)
@staticmethod
def _try_stack(nested_tensors: NestedTensors,
pin_memory: bool = False) -> NestedTensors:
def _try_stack(
nested_tensors: NestedTensors, pin_memory: bool = False
) -> NestedTensors:
"""
Stack the inner dimensions that have the same shape in
a nested list of tensors.
@@ -849,9 +874,7 @@ class MultiModalKwargs(UserDict[str, NestedTensors]):
if isinstance(nested_tensors, (int, float)):
return torch.tensor(nested_tensors)
stacked = [
MultiModalKwargs._try_stack(t, pin_memory) for t in nested_tensors
]
stacked = [MultiModalKwargs._try_stack(t, pin_memory) for t in nested_tensors]
if not is_list_of(stacked, torch.Tensor, check="all"):
# Only tensors (not lists) can be stacked.
return stacked
@@ -867,16 +890,19 @@ class MultiModalKwargs(UserDict[str, NestedTensors]):
# The tensors have incompatible shapes and can't be stacked.
return tensors_
outputs = torch.empty(len(tensors_),
*tensors_[0].shape,
dtype=tensors_[0].dtype,
device=tensors_[0].device,
pin_memory=pin_memory)
outputs = torch.empty(
len(tensors_),
*tensors_[0].shape,
dtype=tensors_[0].dtype,
device=tensors_[0].device,
pin_memory=pin_memory,
)
return torch.stack(tensors_, out=outputs)
@staticmethod
def batch(inputs_list: list["MultiModalKwargs"],
pin_memory: bool = False) -> BatchedTensorInputs:
def batch(
inputs_list: list["MultiModalKwargs"], pin_memory: bool = False
) -> BatchedTensorInputs:
"""
Batch multiple inputs together into a dictionary.
@@ -915,8 +941,10 @@ class MultiModalKwargs(UserDict[str, NestedTensors]):
def __getitem__(self, key: str):
if key not in self:
raise KeyError(f"Keyword argument {key!r} not found. "
f"Available keys: {set(self.keys())}")
raise KeyError(
f"Keyword argument {key!r} not found. "
f"Available keys: {set(self.keys())}"
)
return super().__getitem__(key)