bridge.utils.import_utils#

Module Contents#

Classes#

UnavailableMeta

A metaclass for generating placeholder objects for unavailable symbols

UnavailableNullContext

A placeholder class for unavailable context managers

Functions#

null_decorator

null_decorator

is_unavailable

Helper to check if given symbol is actually a placeholder

safe_import

A function used to import modules that may not be available.

safe_import_from

A function used to import symbols from modules that may not be available.

gpu_only_import

A function used to import modules required only in GPU installs.

gpu_only_import_from

A function used to import symbols required only in GPU installs.

get_torch_version

Returns the installed PyTorch version as a packaging.version.Version object.

is_torch_min_version

Check if minimum version of torch is installed.

Data#

API#

bridge.utils.import_utils.logger#

‘getLogger(…)’

bridge.utils.import_utils.GPU_INSTALL_STRING = <Multiline-String>#
bridge.utils.import_utils.MISSING_NEMO_EXPORT_DEPLOY_MSG#

‘nemo-export-deploy is not available. Please install it with pip install nemo-export-deploy.’

bridge.utils.import_utils.MISSING_NVRX_MSG#

‘nvidia-resiliency-ext is not available. Please install it with pip install nvidia-resiliency-ext.’

bridge.utils.import_utils.MISSING_NEMO_RUN_MSG#

‘nemo-run is not available. Please install it with pip install nemo-run.’

exception bridge.utils.import_utils.UnavailableError#

Bases: Exception

Error thrown if a symbol is unavailable due to an issue importing it

Initialization

Initialize self. See help(type(self)) for accurate signature.

bridge.utils.import_utils.null_decorator(*args, **kwargs)#

null_decorator

class bridge.utils.import_utils.UnavailableMeta#

Bases: type

A metaclass for generating placeholder objects for unavailable symbols

This metaclass allows errors to be deferred from import time to the time that a symbol is actually used in order to streamline the usage of optional dependencies. This is particularly useful for attempted imports of GPU-only modules which will only be invoked if GPU-only functionality is specifically used.

If an attempt to import a symbol fails, this metaclass is used to generate a class which stands in for that symbol. Any attempt to call the symbol (instantiate the class) or access its attributes will throw an UnavailableError exception. Furthermore, this class can be used in e.g. isinstance checks, since it will (correctly) fail to match any instance it is compared against.

In addition to calls and attribute access, a number of dunder methods are implemented so that other common usages of imported symbols (e.g. arithmetic) throw an UnavailableError, but this is not guaranteed for all possible uses. In such cases, other exception types (typically TypeErrors) will be thrown instead.

__new__(name, bases, dct)#
__call__(*args, **kwargs)#
__getattr__(name)#
__eq__(other)#
__lt__(other)#
__gt__(other)#
__le__(other)#
__ge__(other)#
__ne__(other)#
__abs__()#
__add__(other)#
__radd__(other)#
__iadd__(other)#
__floordiv__(other)#
__rfloordiv__(other)#
__ifloordiv__(other)#
__lshift__(other)#
__rlshift__(other)#
__mul__(other)#
__rmul__(other)#
__imul__(other)#
__ilshift__(other)#
__pow__(other)#
__rpow__(other)#
__ipow__(other)#
__rshift__(other)#
__rrshift__(other)#
__irshift__(other)#
__sub__(other)#
__rsub__(other)#
__isub__(other)#
__truediv__(other)#
__rtruediv__(other)#
__itruediv__(other)#
__divmod__(other)#
__rdivmod__(other)#
__neg__()#
__invert__()#
__hash__()#
__index__()#
__iter__()#
__delitem__(name)#
__setitem__(name, value)#
__enter__(*args, **kwargs)#
__get__(*args, **kwargs)#
__delete__(*args, **kwargs)#
__len__()#
bridge.utils.import_utils.is_unavailable(obj)#

Helper to check if given symbol is actually a placeholder

class bridge.utils.import_utils.UnavailableNullContext(*args, **kwargs)#

A placeholder class for unavailable context managers

This context manager will return a value which will throw an UnavailableError if used in any way, but the context manager itself can be safely invoked.

Initialization

__enter__()#
__exit__(*args, **kwargs)#
bridge.utils.import_utils.safe_import(
module,
*,
msg=None,
alt=None,
) Tuple[object, bool]#

A function used to import modules that may not be available.

This function will attempt to import a module with the given name, but it will not throw an ImportError if the module is not found. Instead, it will return a placeholder object which will raise an exception only if used.

Parameters:
  • module (str) – The name of the module to import.

  • msg (str, optional) – An error message to be displayed if this module is used after a failed import. Defaults to None.

  • alt (object, optional) – A module to be used in place of the given module if it fails to import. Defaults to None.

Returns:

A tuple containing two elements. The first element is the imported module, the given alternate, or a class derived from UnavailableMeta. The second element is a boolean indicating whether the intended import was successful.

Return type:

tuple

bridge.utils.import_utils.safe_import_from(
module,
symbol,
*,
msg=None,
alt=None,
fallback_module=None,
) Tuple[object, bool]#

A function used to import symbols from modules that may not be available.

This function will attempt to import a symbol with the given name from the given module, but it will not throw an ImportError if the symbol is not found. Instead, it will return a placeholder object which will raise an exception only if used.

Parameters:
  • module (str) – The name of the module in which the symbol is defined.

  • symbol (str) – The name of the symbol to import.

  • msg (str, optional) – An error message to be displayed if this symbol is used after a failed import. Defaults to None.

  • alt (object, optional) – An object to be used in place of the given symbol if it fails to import. Defaults to None.

  • fallback_module (str, optional) – Alternative name of the model in which the symbol is defined. The function will first try to import using the module value and if that fails will also try the fallback_module. Defaults to None.

Returns:

A tuple containing two elements. The first element is the imported symbol, the given alternate, or a class derived from UnavailableMeta. The second element is a boolean indicating whether the intended import was successful.

Return type:

tuple

bridge.utils.import_utils.gpu_only_import(module, *, alt=None) Tuple[object, bool]#

A function used to import modules required only in GPU installs.

This function will attempt to import a module with the given name. This function will attempt to import a symbol with the given name from the given module, but it will not throw an ImportError if the symbol is not found. Instead, it will return a placeholder object which will raise an exception only if used with instructions on installing a GPU build.

Parameters:
  • module (str) – The name of the module to import.

  • alt (object, optional) – A module to be used in place of the given module if it fails to import in a non-GPU-enabled install. Defaults to None.

Returns:

A tuple containing two elements. The first element is the imported module, the given alternate, or a class derived from UnavailableMeta. The second element is a boolean indicating whether the intended import was successful.

Return type:

tuple

bridge.utils.import_utils.gpu_only_import_from(
module,
symbol,
*,
alt=None,
) Tuple[object, bool]#

A function used to import symbols required only in GPU installs.

This function will attempt to import a module with the given name. This function will attempt to import a symbol with the given name from the given module, but it will not throw an ImportError if the symbol is not found. Instead, it will return a placeholder object which will raise an exception only if used with instructions on installing a GPU build.

Parameters:
  • module (str) – The name of the module to import.

  • symbol (str) – The name of the symbol to import.

  • alt (object, optional) – An object to be used in place of the given symbol if it fails to import in a non-GPU-enabled install. Defaults to None.

Returns:

A tuple containing two elements. The first element is the imported symbol, the given alternate, or a class derived from UnavailableMeta. The second element is a boolean indicating whether the intended import was successful.

Return type:

tuple

bridge.utils.import_utils.get_torch_version()#

Returns the installed PyTorch version as a packaging.version.Version object.

Handles potential exceptions during version parsing, returning a dummy version (“0.0.0”) if parsing fails (e.g., during documentation builds where torch might not be fully imported or available).

Returns:

The parsed PyTorch version, or Version(“0.0.0”) on error.

Return type:

packaging.version.Version

bridge.utils.import_utils.is_torch_min_version(version, check_equality=True)#

Check if minimum version of torch is installed.