nemo_automodel.shared.import_utils#

Module Contents#

Classes#

UnavailableMeta

A metaclass for generating placeholder objects for unavailable symbols.

Functions#

null_decorator

No-op 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

Return pytorch version with fallback if unavailable.

is_torch_min_version

Check if minimum version of torch is installed.

Data#

API#

nemo_automodel.shared.import_utils.logger#

‘getLogger(…)’

nemo_automodel.shared.import_utils.GPU_INSTALL_STRING = <Multiline-String>#
nemo_automodel.shared.import_utils.MISSING_TRITON_MSG#

‘triton is not installed. Please install it with pip install triton.’

nemo_automodel.shared.import_utils.MISSING_QWEN_VL_UTILS_MSG#

‘qwen_vl_utils is not installed. Please install it with pip install qwen-vl-utils.’

nemo_automodel.shared.import_utils.MISSING_CUT_CROSS_ENTROPY_MSG#

‘cut_cross_entropy is not installed. Please install it with pip install cut-cross-entropy.’

exception nemo_automodel.shared.import_utils.UnavailableError[source]#

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.

nemo_automodel.shared.import_utils.null_decorator(*args, **kwargs)[source]#

No-op decorator.

class nemo_automodel.shared.import_utils.UnavailableMeta[source]#

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)[source]#
__call__(*args, **kwargs)[source]#
__getattr__(name)[source]#
__eq__(other)[source]#
__lt__(other)[source]#
__gt__(other)[source]#
__le__(other)[source]#
__ge__(other)[source]#
__ne__(other)[source]#
__abs__()[source]#
__add__(other)[source]#
__radd__(other)[source]#
__iadd__(other)[source]#
__floordiv__(other)[source]#
__rfloordiv__(other)[source]#
__ifloordiv__(other)[source]#
__lshift__(other)[source]#
__rlshift__(other)[source]#
__mul__(other)[source]#
__rmul__(other)[source]#
__imul__(other)[source]#
__ilshift__(other)[source]#
__pow__(other)[source]#
__rpow__(other)[source]#
__ipow__(other)[source]#
__rshift__(other)[source]#
__rrshift__(other)[source]#
__irshift__(other)[source]#
__sub__(other)[source]#
__rsub__(other)[source]#
__isub__(other)[source]#
__truediv__(other)[source]#
__rtruediv__(other)[source]#
__itruediv__(other)[source]#
__divmod__(other)[source]#
__rdivmod__(other)[source]#
__neg__()[source]#
__invert__()[source]#
__hash__()[source]#
__index__()[source]#
__iter__()[source]#
__delitem__(name)[source]#
__setitem__(name, value)[source]#
__enter__(*args, **kwargs)[source]#
__get__(*args, **kwargs)[source]#
__delete__(*args, **kwargs)[source]#
__len__()[source]#
nemo_automodel.shared.import_utils.is_unavailable(obj)[source]#

Helper to check if given symbol is actually a placeholder.

nemo_automodel.shared.import_utils.safe_import(module, *, msg=None, alt=None)[source]#

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 or None) – An optional error message to be displayed if this module is used after a failed import.

  • alt (object) – An optional module to be used in place of the given module if it fails to import

Returns:

The imported module, the given alternate, or a class derived from UnavailableMeta, and a boolean indicating whether the intended import was successful.

Return type:

Tuple(bool, object)

nemo_automodel.shared.import_utils.safe_import_from(
module,
symbol,
*,
msg=None,
alt=None,
fallback_module=None,
)[source]#

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 or None) – An optional error message to be displayed if this symbol is used after a failed import.

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

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

Returns:

The imported symbol, the given alternate, or a class derived from UnavailableMeta, and a boolean indicating whether the intended import was successful.

Return type:

Tuple(object, bool)

nemo_automodel.shared.import_utils.gpu_only_import(module, *, alt=None)[source]#

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) – An optional module to be used in place of the given module if it fails to import in a non-GPU-enabled install

Returns:

The imported module, the given alternate, or a class derived from UnavailableMeta.

Return type:

object

nemo_automodel.shared.import_utils.gpu_only_import_from(module, symbol, *, alt=None)[source]#

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) – An optional object to be used in place of the given symbol if it fails to import in a non-GPU-enabled install

Returns:

The imported symbol, the given alternate, or a class derived from UnavailableMeta.

Return type:

object

nemo_automodel.shared.import_utils.get_torch_version()[source]#

Return pytorch version with fallback if unavailable.

Returns:

Pytorch’s version

Return type:

PkgVersion

nemo_automodel.shared.import_utils.is_torch_min_version(version, check_equality=True)[source]#

Check if minimum version of torch is installed.