bridge.utils.import_utils
#
Module Contents#
Classes#
A metaclass for generating placeholder objects for unavailable symbols |
|
A placeholder class for unavailable context managers |
Functions#
null_decorator |
|
Helper to check if given symbol is actually a placeholder |
|
A function used to import modules that may not be available. |
|
A function used to import symbols from modules that may not be available. |
|
A function used to import modules required only in GPU installs. |
|
A function used to import symbols required only in GPU installs. |
|
Returns the installed PyTorch version as a packaging.version.Version object. |
|
Check if minimum version of |
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
.’
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
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.
Helper to check if given symbol is actually a placeholder
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
- bridge.utils.import_utils.safe_import(
- module,
- *,
- msg=None,
- alt=None,
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,
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 thefallback_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,
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.