nemo_automodel.shared.import_utils
nemo_automodel.shared.import_utils
Module Contents
Classes
Functions
Data
API
Bases: Exception
Error thrown if a symbol is unavailable due to an issue importing it.
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.
Get the appropriate check_model_inputs decorator based on transformers version.
In transformers >= 4.57.3, check_model_inputs became a function that returns a decorator.
In older versions, it was directly a decorator.
In transformers >= 5.2.0, check_model_inputs was removed and split into
merge_with_config_defaults and capture_outputs.
Returns:
Decorator function to validate model inputs.
Get TE version from version.
Return pytorch version with fallback if unavailable.
Returns:
Pytorch’s version
Get transformers version from version.
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:
The name of the module to import.
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
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:
The name of the module to import.
The name of the symbol to import.
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
Check if minimum version of transformer-engine is installed.
Check if minimum version of torch is installed.
Check if minimum version of transformers is installed.
Helper to check if given symbol is actually a placeholder.
No-op decorator.
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:
The name of the module to import.
An optional error message to be displayed if this module is used after a failed import.
An optional module to be used in place of the given module if it fails to import
Returns: (bool, object)
The imported module, the given alternate, or a class derived from
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:
The name of the module in which the symbol is defined.
The name of the symbol to import.
An optional error message to be displayed if this symbol is used after a failed import.
An optional object to be used in place of the given symbol if it fails to import
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: (object, bool)
The imported symbol, the given alternate, or a class derived from
Safely import Transformer Engine (TE). Returns (True, module) only if the package and its PyTorch extension load successfully. Handles known failure modes so callers get a consistent (False, placeholder) instead of crashes.
Known failure modes (handled by returning False and a placeholder):
-
ImportError when importing transformer_engine (torch is pulled in by TE’s pytorch/init.py), e.g. CUDA/runtime mismatch: ImportError: …/libc10_cuda.so: undefined symbol: cudaGetDriverEntryPointByVersion, version libcudart.so.12
-
FileNotFoundError when TE loads its framework extension (e.g. in transformer_engine.common.load_framework_extension(“torch”)): FileNotFoundError: Could not find shared object file for Transformer Engine torch lib.
-
OSError when TE’s core .so is dlopen’d against an incompatible CUDA lib, e.g. a prebuilt wheel needing a newer cuBLAS than is installed: OSError: …/libtransformer_engine.so: undefined symbol: cublasLtGroupedMatrixLayoutInit_internal, version libcublasLt.so.13
Returns:
Tuple[bool, Union[module, UnavailableMeta]]: (True, te_module) if import