Skip to content

vllm.utils.registry

ExtensionManager

A registry for managing pluggable extension classes.

This class provides a simple mechanism to register and instantiate extension classes by name. It is commonly used to implement plugin systems where different implementations can be swapped at runtime.

Examples:

Basic usage with a registry instance:

>>> FOO_REGISTRY = ExtensionManager()
>>> @FOO_REGISTRY.register("my_foo_impl")
... class MyFooImpl(Foo):
...     def __init__(self, value):
...         self.value = value
>>> foo_impl = FOO_REGISTRY.load("my_foo_impl", value=123)
Source code in vllm/utils/registry.py
class ExtensionManager:
    """
    A registry for managing pluggable extension classes.

    This class provides a simple mechanism to register and instantiate
    extension classes by name. It is commonly used to implement plugin
    systems where different implementations can be swapped at runtime.

    Examples:
        Basic usage with a registry instance:

        >>> FOO_REGISTRY = ExtensionManager()
        >>> @FOO_REGISTRY.register("my_foo_impl")
        ... class MyFooImpl(Foo):
        ...     def __init__(self, value):
        ...         self.value = value
        >>> foo_impl = FOO_REGISTRY.load("my_foo_impl", value=123)

    """

    def __init__(self) -> None:
        """
        Initialize an empty extension registry.
        """
        self.name2class: dict[str, type] = {}

    def register(self, name: str):
        """
        Decorator to register a class with the given name.
        """

        def wrap(cls_to_register):
            self.name2class[name] = cls_to_register
            return cls_to_register

        return wrap

    def load(self, cls_name: str, *args, **kwargs) -> Any:
        """
        Instantiate and return a registered extension class by name.
        """
        cls = self.name2class.get(cls_name)
        assert cls is not None, f"Extension class {cls_name} not found"
        return cls(*args, **kwargs)

name2class instance-attribute

name2class: dict[str, type] = {}

__init__

__init__() -> None

Initialize an empty extension registry.

Source code in vllm/utils/registry.py
def __init__(self) -> None:
    """
    Initialize an empty extension registry.
    """
    self.name2class: dict[str, type] = {}

load

load(cls_name: str, *args, **kwargs) -> Any

Instantiate and return a registered extension class by name.

Source code in vllm/utils/registry.py
def load(self, cls_name: str, *args, **kwargs) -> Any:
    """
    Instantiate and return a registered extension class by name.
    """
    cls = self.name2class.get(cls_name)
    assert cls is not None, f"Extension class {cls_name} not found"
    return cls(*args, **kwargs)

register

register(name: str)

Decorator to register a class with the given name.

Source code in vllm/utils/registry.py
def register(self, name: str):
    """
    Decorator to register a class with the given name.
    """

    def wrap(cls_to_register):
        self.name2class[name] = cls_to_register
        return cls_to_register

    return wrap