Module utilities.backend_management

Utility functionality for managing backends

To check a standard backend, use backend_available(). To check a non-standard backend specification, use check_backend().

To build a shim class that raises an error on instantiation, for when a required backend is not available, use missing_backend_shim().

Functions overview

backend_available(*backend_names)

Report if a backend is available for use

check_backend(backend_name[, ...])

Check if a backend is available, and register it in a list of available backends

check_samna_available()

check_samna_available controls if samna package is "installed" and "usable" The default backend_available() operation cannot correctly identifies the samna availability.

check_torch_cuda_available()

list_backends()

Print a list of computational backends available in this session

missing_backend_error(class_name, backend_name)

Raise a ModuleNotFoundError exception, with information about a missing backend

missing_backend_shim(class_name, backend_name)

Make a class constructor that raises an error about a missing backend

torch_version_satisfied([req_major, ...])

Check if the installed version of torch satisfies a minimum version requirement

Functions

utilities.backend_management.backend_available(*backend_names) bool[source]

Report if a backend is available for use

This function returns immediately if the named backend has already been checked previously. Otherwise, if the backend is either a defined standard backend, or is a simple importable python module, then it will be checked for availability. If the backend is non-standard, it cannot be checked automatically. In that case you must use check_backend() directly.

Parameters:
  • backend_name0 (str) – A backend to check

  • backend_name1 (str) – A backend to check

  • ... (str) – A backend to check

Returns:

True iff the backend is available for use

Return type:

bool

utilities.backend_management.check_backend(backend_name: str, required_modules: Tuple[str] | List[str] | None = None, check_flag: bool = True) bool[source]

Check if a backend is available, and register it in a list of available backends

Parameters:
  • backend_name (str) – The name of this backend to check for and register

  • required_modules (Optional[List[str]]) – A list of required modules to search for. If None (default), check the backend name

  • check_flag (bool) – A manual check that can be performed externally, to see if the backend is available

Returns:

The backend is available

Return type:

bool

utilities.backend_management.check_samna_available() bool[source]

check_samna_available controls if samna package is “installed” and “usable” The default backend_available() operation cannot correctly identifies the samna availability. In the case that one installed samna and then uninstalled via pip * pip install samna then pip uninstall samna samna leaves a trace and import samna does not raise an error even though the package is not usable.

Returns:

true if samna package available

Return type:

bool

utilities.backend_management.check_torch_cuda_available() bool[source]
utilities.backend_management.list_backends()[source]

Print a list of computational backends available in this session

utilities.backend_management.missing_backend_error(class_name: str, backend_name: str)[source]

Raise a ModuleNotFoundError exception, with information about a missing backend

Parameters:
  • class_name (str) – Name of a class which is unavailable

  • backend_name (str) – “User-facing” of the backend which is unavailable

Raises:

ModuleNotFoundError – Describe the missing backend

utilities.backend_management.missing_backend_shim(class_name: str, backend_name: str)[source]

Make a class constructor that raises an error about a missing backend

Examples

Generate a LIFTorch class shim, that will raise an error on instantiation.

>>> LIFTorch = missing_backend_shim('LIFTorch', 'torch')
>>> LIFTorch((3,), tau_syn = 10e-3)
ModuleNotFoundError: Missing the `torch` backend. `LIFTorch` objects, and others relying on `torch` are not available.
Parameters:
  • class_name (str) – The intended class name

  • backend_name (str) – The required backend that is missing

Returns:

A class that raises an error on construction

Return type:

Class

utilities.backend_management.torch_version_satisfied(req_major: int = 0, req_minor: int = 0, req_patch: int = 0) bool[source]

Check if the installed version of torch satisfies a minimum version requirement

i.e.

torch 2.0.0 >= 1.12.0 : True torch 1.12.0 >= 1.12.0 : True torch 1.11.0 >= 1.12.0 : False

Parameters:
  • req_major (int) – The minimum major version required

  • req_minor (int) – The minimum minor version required

  • req_patch (int) – The minimum patch version required

Returns:

The installed version of torch satisfies the minimum version requirement

Return type:

bool