kaira.models.ModelRegistry

Inheritance diagram for ModelRegistry
- class kaira.models.ModelRegistry[source]
Bases:
objectA 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
Create a model instance by name.
Get a model class by name.
Get a model class by name.
Get information about a registered model.
List all available models in the registry.
Register a new model class in the registry.
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:
- 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_model_cls(name: str) Type[BaseModel][source]
Get a model class by name.
This is an alias for the get() method for backwards compatibility.
- 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:
- Raises:
- 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:
- __init__()