kaira.models.ModelRegistry

Inheritance diagram of ModelRegistry

Inheritance diagram for ModelRegistry

class kaira.models.ModelRegistry[source]

Bases: object

A registry for models in Kaira.

This class provides a centralized registry system that maintains a mapping between model names and their corresponding classes. It supports: - Registration of new model classes (both directly and via decorator) - Creation of model instances by name - Lookup of registered model classes - Listing of available models

The registry pattern enables dynamic discovery and instantiation of models, which is particularly useful for configurable pipelines and experiments where model architectures need to be selected at runtime.

Example

>>> @ModelRegistry.register_model()
>>> class MyCustomModel(BaseModel):
...     def __init__(self, hidden_size=64):
...         super().__init__()
...         self.hidden_size = hidden_size
...
>>> # Create instance from registry
>>> model = ModelRegistry.create("mycustommodel", hidden_size=128)

Methods

__init__

create

Create a model instance by name.

get

Get a model class by name.

get_model_cls

Get a model class by name.

get_model_info

Get information about a registered model.

list_models

List all available models in the registry.

register

Register a new model class in the registry.

register_model

Decorator to register a model class in the registry.

classmethod register(name: str, model_class: Type[BaseModel]) None[source]

Register a new model class in the registry.

Parameters:
  • name (str) – The name under which to register the model class. This name will be used to look up and create instances of the model.

  • model_class (Type[BaseModel]) – The model class to register. Must be a subclass of BaseModel.

Raises:
  • TypeError – If model_class is not a subclass of BaseModel

  • ValueError – If a model with the given name is already registered

classmethod register_model(name: str | None = None) Callable[source]

Decorator to register a model class in the registry.

This decorator provides a convenient way to register model classes at definition time. If no name is provided, the lowercase version of the class name is used as the registration key.

Parameters:

name (Optional[str]) – Optional custom name for the model. If not provided, the lowercase class name will be used as the registration key.

Returns:

A decorator function that registers the model class

Return type:

Callable

Example

>>> @ModelRegistry.register_model()  # Uses class name as key
>>> class MyModel(BaseModel):
...     # implementation
...
>>> @ModelRegistry.register_model("better_name")  # Uses custom name
>>> class GenericNameThatNeedsBetterRegistryKey(BaseModel):
...     # implementation
classmethod get(name: str) Type[BaseModel][source]

Get a model class by name.

Parameters:

name (str) – The registered name of the model class to retrieve

Returns:

The requested model class

Return type:

Type[BaseModel]

Raises:

KeyError – If no model is registered under the given name

classmethod get_model_cls(name: str) Type[BaseModel][source]

Get a model class by name.

This is an alias for the get() method for backwards compatibility.

Parameters:

name (str) – The registered name of the model class to retrieve

Returns:

The requested model class

Return type:

Type[BaseModel]

Raises:

KeyError – If no model is registered under the given name

classmethod get_model_info(name: str) Dict[source]

Get information about a registered model.

This method provides detailed information about a model registered in the registry, including its name, class name, and constructor signature.

Parameters:

name (str) – The registered name of the model to get information about

Returns:

A dictionary containing information about the model:
  • name: The registered name of the model

  • class: The class name of the model

  • signature: The constructor signature of the model

Return type:

Dict

Raises:

ValueError – If no model is registered under the given name

classmethod create(name: str, **kwargs) BaseModel[source]

Create a model instance by name.

This method provides a convenient way to instantiate models from their registered names, passing any necessary configuration parameters.

Parameters:
  • name (str) – The registered name of the model to create

  • **kwargs – Configuration parameters to pass to the model constructor

Returns:

An instantiated model object

Return type:

BaseModel

Raises:
  • KeyError – If no model is registered under the given name

  • TypeError – If the model constructor receives invalid arguments

classmethod list_models() list[source]

List all available models in the registry.

Returns:

A list of registered model names that can be used with create()

Return type:

list

__init__()