Skip to content

vllm.utils.collection_utils

Contains helpers that are applied to collections.

This is similar in concept to the collections module.

T module-attribute

T = TypeVar('T')

U module-attribute

U = TypeVar('U')

_K module-attribute

_K = TypeVar('_K', bound=Hashable)

_V module-attribute

_V = TypeVar('_V')

ClassRegistry

Bases: UserDict[type[T], _V]

A registry that acts like a dictionary but searches for other classes in the MRO if the original class is not found.

Source code in vllm/utils/collection_utils.py
class ClassRegistry(UserDict[type[T], _V]):
    """
    A registry that acts like a dictionary but searches for other classes
    in the MRO if the original class is not found.
    """

    def __getitem__(self, key: type[T]) -> _V:
        for cls in key.mro():
            if cls in self.data:
                return self.data[cls]

        raise KeyError(key)

    def __contains__(self, key: object) -> bool:
        return self.contains(key)

    def contains(self, key: object, *, strict: bool = False) -> bool:
        if not isinstance(key, type):
            return False

        if strict:
            return key in self.data

        return any(cls in self.data for cls in key.mro())

__contains__

__contains__(key: object) -> bool
Source code in vllm/utils/collection_utils.py
def __contains__(self, key: object) -> bool:
    return self.contains(key)

__getitem__

__getitem__(key: type[T]) -> _V
Source code in vllm/utils/collection_utils.py
def __getitem__(self, key: type[T]) -> _V:
    for cls in key.mro():
        if cls in self.data:
            return self.data[cls]

    raise KeyError(key)

contains

contains(key: object, *, strict: bool = False) -> bool
Source code in vllm/utils/collection_utils.py
def contains(self, key: object, *, strict: bool = False) -> bool:
    if not isinstance(key, type):
        return False

    if strict:
        return key in self.data

    return any(cls in self.data for cls in key.mro())

LazyDict

Bases: Mapping[str, T], Generic[T]

Evaluates dictionary items only when they are accessed.

Adapted from: https://stackoverflow.com/a/47212782/5082708

Source code in vllm/utils/collection_utils.py
class LazyDict(Mapping[str, T], Generic[T]):
    """
    Evaluates dictionary items only when they are accessed.

    Adapted from: https://stackoverflow.com/a/47212782/5082708
    """

    def __init__(self, factory: dict[str, Callable[[], T]]):
        self._factory = factory
        self._dict: dict[str, T] = {}

    def __getitem__(self, key: str) -> T:
        if key not in self._dict:
            if key not in self._factory:
                raise KeyError(key)
            self._dict[key] = self._factory[key]()
        return self._dict[key]

    def __setitem__(self, key: str, value: Callable[[], T]):
        self._factory[key] = value

    def __iter__(self):
        return iter(self._factory)

    def __len__(self):
        return len(self._factory)

_dict instance-attribute

_dict: dict[str, T] = {}

_factory instance-attribute

_factory = factory

__getitem__

__getitem__(key: str) -> T
Source code in vllm/utils/collection_utils.py
def __getitem__(self, key: str) -> T:
    if key not in self._dict:
        if key not in self._factory:
            raise KeyError(key)
        self._dict[key] = self._factory[key]()
    return self._dict[key]

__init__

__init__(factory: dict[str, Callable[[], T]])
Source code in vllm/utils/collection_utils.py
def __init__(self, factory: dict[str, Callable[[], T]]):
    self._factory = factory
    self._dict: dict[str, T] = {}

__iter__

__iter__()
Source code in vllm/utils/collection_utils.py
def __iter__(self):
    return iter(self._factory)

__len__

__len__()
Source code in vllm/utils/collection_utils.py
def __len__(self):
    return len(self._factory)

__setitem__

__setitem__(key: str, value: Callable[[], T])
Source code in vllm/utils/collection_utils.py
def __setitem__(self, key: str, value: Callable[[], T]):
    self._factory[key] = value

as_iter

as_iter(obj: T | Iterable[T]) -> Iterable[T]
Source code in vllm/utils/collection_utils.py
def as_iter(obj: T | Iterable[T]) -> Iterable[T]:
    if isinstance(obj, str) or not isinstance(obj, Iterable):
        return [obj]  # type: ignore[list-item]
    return obj

as_list

as_list(maybe_list: Iterable[T]) -> list[T]

Convert iterable to list, unless it's already a list.

Source code in vllm/utils/collection_utils.py
def as_list(maybe_list: Iterable[T]) -> list[T]:
    """Convert iterable to list, unless it's already a list."""
    return maybe_list if isinstance(maybe_list, list) else list(maybe_list)

chunk_list

chunk_list(
    lst: list[T], chunk_size: int
) -> Generator[list[T]]

Yield successive chunk_size chunks from lst.

Source code in vllm/utils/collection_utils.py
def chunk_list(lst: list[T], chunk_size: int) -> Generator[list[T]]:
    """Yield successive chunk_size chunks from lst."""
    for i in range(0, len(lst), chunk_size):
        yield lst[i : i + chunk_size]

flatten_2d_lists

flatten_2d_lists(lists: Iterable[Iterable[T]]) -> list[T]

Flatten a list of lists to a single list.

Source code in vllm/utils/collection_utils.py
def flatten_2d_lists(lists: Iterable[Iterable[T]]) -> list[T]:
    """Flatten a list of lists to a single list."""
    return [item for sublist in lists for item in sublist]

full_groupby

full_groupby(
    values: Iterable[_V], *, key: Callable[[_V], _K]
)

Unlike itertools.groupby, groups are not broken by non-contiguous data.

Source code in vllm/utils/collection_utils.py
def full_groupby(values: Iterable[_V], *, key: Callable[[_V], _K]):
    """
    Unlike [`itertools.groupby`][], groups are not broken by
    non-contiguous data.
    """
    groups = defaultdict[_K, list[_V]](list)

    for value in values:
        groups[key(value)].append(value)

    return groups.items()

is_list_of

is_list_of(
    value: object,
    typ: type[T] | tuple[type[T], ...],
    *,
    check: Literal["first", "all"] = "first",
) -> TypeIs[list[T]]
Source code in vllm/utils/collection_utils.py
def is_list_of(
    value: object,
    typ: type[T] | tuple[type[T], ...],
    *,
    check: Literal["first", "all"] = "first",
) -> TypeIs[list[T]]:
    if not isinstance(value, list):
        return False

    if check == "first":
        return len(value) == 0 or isinstance(value[0], typ)
    elif check == "all":
        return all(isinstance(v, typ) for v in value)

    assert_never(check)

swap_dict_values

swap_dict_values(
    obj: dict[_K, _K" backlink-type="used-by" backlink-anchor="vllm.utils.collection_utils.swap_dict_values" optional hover>_K], key1: _K, key2: _K
) -> None

Swap values between two keys.

Source code in vllm/utils/collection_utils.py
def swap_dict_values(obj: dict[_K, _V], key1: _K, key2: _K) -> None:
    """Swap values between two keys."""
    v1 = obj.get(key1)
    v2 = obj.get(key2)
    if v1 is not None:
        obj[key2] = v1
    else:
        obj.pop(key2, None)
    if v2 is not None:
        obj[key1] = v2
    else:
        obj.pop(key1, None)