noether.core.factory

Submodules

Classes

Factory

Base factory. Implements base structures for creating a single object, a list of objects and a dict

DatasetFactory

Specialized factory for datasets. Next to the standard instantiation of datasets, it also supports wrapping of the dataset inside dataset wrappers.

OptimizerFactory

Factory for creating optimizers. Handles wrapping into OptimizerWrapper by creating the corresponding constructor

ScheduleFactory

Factory for creating schedules. Handles wrapping into ScheduleWrapper which handles update/epoch based

Functions

class_constructor_from_class_path(class_path)

Creates a callable that constructs an object from a classpath. This callable is either a type (if no further

Package Contents

class noether.core.factory.Factory(returns_partials=False)

Base factory. Implements base structures for creating a single object, a list of objects and a dict of objects.

For example, creating a list:

class Example:
    def __init__(self, callbacks: list[CallbackConfig] | None = None):
        # automatic none check in create_list (this is how Factory is implemented)
        self.callbacks = Factory().create_list(callbacks)
        # required none check after creating the list (this is how one could implement it without create_list)

Objects can be instantiated from either:

  • BaseModel: In this case, the kind field is used to determine the class path of the object to be instantiated. The full pydantic model is passed to the constructor.

    class ExampleConfig(pydantic.BaseModel):
        kind: str
        param1: int
        param2: str
    
    
    class ExampleObject:
        def __init__(self, config: ExampleConfig):
            self.param1 = config.param1
            self.param2 = config.param2
            # kind is also in the config, but usually not needed in the object itself
    
    
    example_config = ExampleConfig(kind="path.to.ExampleObject", param1=42, param2="hello")
    example_object = Factory().create(example_config)
    

    Using BaseModel is the preferred way of instantiating objects as it provides type safety and validation.

  • Dictionary: In this case, the kind key is used to determine the class path of the object to be instantiated. The full dictionary is passed to the constructor. However, almost all classes in Noether take a config object as input to the constructor. This approach will only work for custom classes that take named arguments in the constructor.

    example_dict = {"kind": "path.to.ExampleObject", "param1": 42, "param2": "hello"}
    
    
    class ExampleObject:
        # constructor takes named arguments directly, and kind popped before passing to constructor
        def __init__(self, param1: int, param2: str):
            self.param1 = param1
            self.param2 = param2
    
    
    example_object = Factory().create(example_dict)
    
Parameters:

returns_partials (bool)

logger
returns_partials = False
create(obj_or_kwargs, **kwargs)

Creates an object if the object is specified as dictionary. If the object was already instantiated, it will simply return the existing object. If obj_or_kwargs is None, None is returned.

Parameters:
  • obj_or_kwargs (Any | dict[str, Any] | pydantic.BaseModel | None) – Either an existing object or a description of how an object should be instantiated (dict or pydantic.BaseModel).

  • kwargs – Further kwargs that are passed when creating the object. These are often dependencies such as UpdateCounter, PathProvider, MetricPropertyProvider, etc.

Returns:

The instantiated object.

Return type:

Any | None

create_list(collection, **kwargs)

Creates a list of objects by calling the create() function for every item in the collection.

If collection is None, an empty list is returned.

Parameters:
Returns:

The instantiated list of objects or an empty list.

Return type:

list[Any]

create_dict(collection, **kwargs)

Creates a dict of objects by calling the create() function for every item in the collection.

If collection is None, an empty dictionary is returned.

Parameters:
  • collection (dict[str, Any] | dict[str, dict[str, Any]] | None) – Either a dict of existing objects or a dict of descriptions how the objects should be instantiated and what their identifier in the dict is.

  • kwargs – Further kwargs that are passed to all object instantiations. These are often dependencies such as UpdateCounter, PathProvider, MetricPropertyProvider, etc.

Returns:

The instantiated dict of objects or an empty dict.

Return type:

dict[str, Any]

instantiate(object_config=None, **kwargs)

Instantiates an object based on its fully specified classpath.

Parameters:

object_config (pydantic.BaseModel | None) –

Configuration containing the fully specified type of the object in the kind field.

For example: "torch.optim.SGD" or "noether.core.callbacks.CheckpointCallback".

kwargs: kwargs passed to the type when instantiating the object.

Returns:

The instantiated object.

Return type:

Any

class noether.core.factory.DatasetFactory(dataset_wrapper_factory=None)

Bases: noether.core.factory.base.Factory

Specialized factory for datasets. Next to the standard instantiation of datasets, it also supports wrapping of the dataset inside dataset wrappers.

Example config:

kind: path.to.custom_dataset.CustomDataset
dataset_param1: value1
dataset_param2: value2
dataset_wrappers:
  - kind: noether.data.base.wrappers.SomeDatasetWrapper
    param1: value1
  - kind: noether.data.base.wrappers.AnotherDatasetWrapper
    param2: value2
Parameters:

dataset_wrapper_factory (noether.core.factory.base.Factory | None)

dataset_wrapper_factory
instantiate(dataset_config, **kwargs)

Instantiates the dataset either based on dataset_config

Parameters:
Returns:

The instantiated dataset, possibly wrapped in dataset wrappers specified in the configuration.

Return type:

Any

class noether.core.factory.OptimizerFactory

Bases: noether.core.factory.base.Factory

Factory for creating optimizers. Handles wrapping into OptimizerWrapper by creating the corresponding constructor for the underlying torch.optim.Optimizer. Objects are returned as partials, as creating the optimizer requires the model parameters from the instantiated model.

instantiate(optimizer_config)

Instantiates the optimizer based on the provided configuration.

Parameters:

optimizer_config (noether.core.schemas.optimizers.OptimizerConfig) – Configuration for the optimizer to create. This config contains both the torch.optim.Optimizer and the OptimizerWrapper configurations. Optimizer and the OptimizerWrapper configurations. See OptimizerConfig for available options.

Returns:

A callable that initializes the OptimizerWrapper.

Return type:

collections.abc.Callable[Ellipsis, noether.core.optimizer.OptimizerWrapper]

class noether.core.factory.ScheduleFactory(returns_partials=False)

Bases: noether.core.factory.base.Factory

Factory for creating schedules. Handles wrapping into ScheduleWrapper which handles update/epoch based scheduling. Additionally, populates the effective_batch_size and updates_per_epoch to avoid specifying it in the config.

Parameters:

returns_partials (bool)

create(schedule_config, **kwargs)

Creates a schedule based on the provided config and wraps it into a ScheduleWrapper.

Parameters:
  • schedule_config (noether.core.schemas.schedules.AnyScheduleConfig) – The schedule config or already instantiated schedule. See AnyScheduleConfig for available options.

  • **kwargs – Additional keyword arguments that are passed to the schedule constructor.

Returns:

The instantiated schedule wrapped in ScheduleWrapper.

Return type:

noether.core.utils.training.ScheduleWrapper | None

noether.core.factory.class_constructor_from_class_path(class_path)

Creates a callable that constructs an object from a classpath. This callable is either a type (if no further kwargs are needed to be passed) or a partial() otherwise. This is equivalent to Hydra instantiation with _target_, which is also based on class paths.

Parameters:

class_path (str) – Fully specified module path of the object. For example: "torch.optim.SGD" or "noether.core.callbacks.checkpoint.checkpoint.CheckpointCallback".

Returns:

A callable that constructs the object.

Return type:

collections.abc.Callable[Ellipsis, Any]