bitorch_engine.utils.safe_import.import_extension

bitorch_engine.utils.safe_import.import_extension(module_name: str, not_yet_implemented: bool = False) Any[source]

Dynamically imports a Python extension module by name, providing a safe mechanism to handle cases where the module is not yet built or not yet implemented. This function is particularly useful for conditionally importing modules that provide optional functionality or are platform-specific.

If the module is marked as not yet implemented (not_yet_implemented=True), or if the module cannot be found during import, the function returns a placeholder object instead of raising an ImportError. This allows the application to continue running and gracefully handle the absence of the module.

Parameters:
  • module_name (str) – The name of the module to be imported. The actual module name will be prefixed with a predefined prefix defined in EXTENSION_PREFIX to form the full module name.

  • not_yet_implemented (bool, optional) – A flag indicating whether the module is known to be not yet implemented. If True, the function immediately returns a placeholder without attempting to import the module. Defaults to False.

Returns:

An imported module if successful, or an instance of ExtensionModulePlaceholder if the module

is not implemented or cannot be found.

Return type:

Any

Example

binary_linear_cuda = import_extension(“binary_linear_cuda”)

This example attempts to import a module named “binary_linear_cuda” (prefixed appropriately), returning the module if found, or a placeholder if not found or not implemented.

Note

This function prints a warning message to the console if the module cannot be found, informing the user of the issue without interrupting the execution of the program.