Skip to content

Cyclic imports in cuda-bindings causing import errors #789

@gflegar

Description

@gflegar

Is this a duplicate?

Type of Bug

Compile-time Error

Component

cuda.bindings

Describe the bug

We are using a custom Python environment, where cyclic imports in Python do not work. This is usually not a problem for the vast majority of Python packages, but unfortunately it does cause issues when trying to use cuda-bindings, since there are some cyclic imports between the various cython files.

One example is the cycle between the driver -> _lib.utils -> driver submodules.

Would it be possible to refactor this code to remove the cycle?

How to Reproduce

Run the following code with cuda-bindings installed, which attempts to approximate our import system:

orig_import = __builtins__.__import__

import_stack = []


def import_hook(name, globals=None, locals=None, fromlist=(), *args, **kwargs):
  """Approximate a custom import system that does not allow import cycles."""

  stack_entry = (tuple(fromlist) if fromlist is not None else None, name)
  if stack_entry in import_stack:
    raise ImportError(
        f"Import cycle detected: {stack_entry}, stack: {import_stack}"
    )
  import_stack.append(stack_entry)
  res = orig_import(name, globals, locals, fromlist, *args, **kwargs)
  import_stack.pop()
  return res


__builtins__.__import__ = import_hook


import cuda.bindings.nvvm   # works
import cuda.bindings.nvrtc  # fails due to driver -> _lib.utils -> driver cycle

I get the following error on an Ubuntu system with Python 3.12.9 when running the above code:

Traceback (most recent call last):
  File "import_hook_test.py", line 24, in <module>
    import cuda.bindings.nvrtc  # fails due to driver -> _lib.utils -> driver cycle
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "import_hook_test.py", line 15, in import_hook
    res = orig_import(name, globals, locals, fromlist, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "cuda/bindings/nvrtc.pyx", line 1, in init cuda.bindings.nvrtc
  File "import_hook_test.py", line 15, in import_hook
    res = orig_import(name, globals, locals, fromlist, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "cuda/bindings/driver.pyx", line 1, in init cuda.bindings.driver
  File "import_hook_test.py", line 15, in import_hook
    res = orig_import(name, globals, locals, fromlist, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "cuda/bindings/_lib/utils.pyx", line 1, in init cuda.bindings._lib.utils
  File "import_hook_test.py", line 11, in import_hook
    raise ImportError(
ImportError: Import cycle detected: ((), 'cuda.bindings.driver'), stack: [(None, 'cuda.bindings.nvrtc'), ((), 'cuda.bindings.driver'), ((), 'cuda.bindings._lib.utils')]

Expected behavior

Imports with the custom hook without raising an exception.

Operating System

Custom linux-based system

nvidia-smi output

N/A - fails at import

Metadata

Metadata

Assignees

Labels

P0High priority - Must do!cuda.bindingsEverything related to the cuda.bindings moduleenhancementAny code-related improvements

Type

No type

Projects

Status

Needs Triage

Relationships

None yet

Development

No branches or pull requests

Issue actions