4.1.8 pde.backends.registry module
Defines the registry for managing backends.
Class handling all backends and their configurations. |
|
Load a default configuration from a module. |
|
Return backend specified by string of instance. |
|
Returns all registered backends. |
|
Returns all backends that can be used in the current environment. |
- class BackendRegistry[source]
Bases:
objectClass handling all backends and their configurations.
Backends can exist in three different states in registry: * Registered meta-information on how to load a backend package * Loaded backend module, so the class is available * Fully instantiated
BackendBaseclasses- get_backend(name: str, *, config: ConfigLike | None = None, **kwargs) pde.backends.base.BackendBase[source]
Return backend object, potentially loading the respective package.
The returned backend is cached if config is not specified. Consequently, the same object will be returned for repeated calls to get_backend, which allows sharing configuration parameters. Moreover, if name only specifies a backend, i.e., does not contain a colon :, the configuration of this backend is linked with the global configuration
pde.config, such that changes to the config are reflected globally.- Parameters:
- Returns:
An instance of the backend with the particular configuration
- Return type:
- is_available(name: str) bool[source]
Check whether a backend can be used in the current environment.
The check is based on the python modules that were given as requires when the backend was registered, so no backend is imported by this method.
- Parameters:
name (str) – Name of the backend. Additional information given after a colon, e.g., in
torch:cuda, is ignored, so this method only checks whether the backend itself could be loaded.- Returns:
Whether the backend can be used. Backends that are not registered at all are reported as being unavailable.
- Return type:
- register_backend(backend: BackendBase) None[source]
Register a loaded backend object.
- Parameters:
backend (
BackendBase) – Implementation of the backend
- register_class(name: str, cls: type[BackendBase])[source]
Register a backend class.
- Parameters:
name (str) – Name of the backend
cls (subclass of
BackendBase) – The class for creating a backend
- register_package(name: str, package_path: str, *, config: ConfigLike | Sequence[Parameter] | None = None, requires: Sequence[str] = ()) None[source]
Register a backend python package (without loading it yet)
- Parameters:
name (str) – Name of the backend
package_path (str) – Import path for the package
config (list) – Configuration options for the package
requires (sequence of str) – Names of the python modules that need to be installed for this backend to work. They are used by
BackendRegistry.is_available()to check whether the backend can be used without importing it.
- values() Iterator[pde.backends.base.BackendBase][source]
Iterate over all backends that can be imported.
- available_backends() list[str][source]
Returns all backends that can be used in the current environment.
In contrast to
registered_backends(), this omits backends whose python packages are not installed, e.g., the torch backend iftorchis missing.
- get_backend(backend: str | pde.backends.base.BackendBase, config: ConfigLike | None = None) pde.backends.base.BackendBase[source]
Return backend specified by string of instance.
The returned backend is cached if config is not specified. Consequently, the same object will be returned for repeated calls to get_backend, which allows sharing configuration parameters. Moreover, if name only specifies a backend, i.e., does not contain a colon :, the configuration of this backend is linked with the global configuration
pde.config, such that changes to the config are reflected globally.- Parameters:
backend (str or
BackendBase) – Backend specified by name given as a string. If the string contains a colon, the first part determines the backend, whereas the second part can be used to convey additional information. For example,torch:cudamay load a torch backend and use a cuda device. As a special case, we also allow full backend objects, which are simply returned. This is a simple way to allow providing full backend objects in places where we otherwise would expect a backend name.config (dict) – Additional configuration options for this specific backend. The full configuration will be taken from the global configuration and merged with the given options here. This option is only permitted if backend is a string since there otherwise might be unintended side effects of modifying an existing backend.
- Returns:
An initialized backend
- Return type: