Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cuda_core/cuda/core/_linker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,10 @@ def _decide_nvjitlink_or_driver() -> bool:
" For best results, consider upgrading to a recent version of"
)

nvjitlink_module = _optional_cuda_import(
"cuda.bindings.nvjitlink",
probe_function=lambda module: module.version(), # probe triggers nvJitLink runtime load
)
# Do not probe via module.version(): nvJitLink 12.0-12.2 lacks the unversioned
# nvJitLinkVersion symbol, so calling version() raises FunctionNotFoundError.
# Use _nvjitlink_has_version_symbol() below instead (symbol pointer inspection).
nvjitlink_module = _optional_cuda_import("cuda.bindings.nvjitlink")
if nvjitlink_module is None:
warn_txt = f"cuda.bindings.nvjitlink is not available, therefore {warn_txt_common} cuda-bindings."
else:
Expand Down
9 changes: 9 additions & 0 deletions cuda_core/docs/source/release/1.2.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ Fixes and enhancements
(`#2357 <https://github.com/NVIDIA/cuda-python/pull/2357>`__,
`#2371 <https://github.com/NVIDIA/cuda-python/pull/2371>`__)

- :meth:`Linker.which_backend` and constructing a :class:`Linker` no longer
raise ``FunctionNotFoundError`` when an nvJitLink older than 12.3
(12.0–12.2) is installed. These versions do not export the unversioned
``nvJitLinkVersion`` symbol, so probing the version crashed instead of
falling back. ``cuda.core`` now warns and falls back to the driver
(``cuLink``) backend, restoring the pre-0.7.0 behavior.
(`#2409 <https://github.com/NVIDIA/cuda-python/pull/2409>`__,
closes `#2408 <https://github.com/NVIDIA/cuda-python/issues/2408>`__)

Deprecation Notices
-------------------

Expand Down
18 changes: 18 additions & 0 deletions cuda_core/tests/test_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,24 @@ def fake_decide():
assert result == "nvJitLink"
assert called, "_decide_nvjitlink_or_driver was not called"

def test_which_backend_falls_back_when_nvjitlink_too_old(self, monkeypatch):
"""Regression test for #2408: old nvJitLink must not crash which_backend()."""
monkeypatch.setattr(_linker, "_use_nvjitlink_backend", None)
monkeypatch.setattr(_linker, "_driver", None)

def fake__optional_cuda_import(modname, probe_function=None):
assert modname == "cuda.bindings.nvjitlink"
assert probe_function is None
return object()

monkeypatch.setattr(_linker, "_optional_cuda_import", fake__optional_cuda_import)
monkeypatch.setattr(_linker, "_nvjitlink_has_version_symbol", lambda _nvjitlink: False)

with pytest.warns(RuntimeWarning, match="too old \\(<12.3\\)"):
assert Linker.which_backend() == "driver"

assert _linker._use_nvjitlink_backend is False

def test_which_backend_is_classmethod(self):
attr = inspect.getattr_static(Linker, "which_backend")
assert isinstance(attr, classmethod)
Expand Down
35 changes: 33 additions & 2 deletions cuda_core/tests/test_optional_dependency_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def fake__optional_cuda_import(modname, probe_function=None):
def test_decide_nvjitlink_or_driver_reraises_nested_module_not_found(monkeypatch):
def fake__optional_cuda_import(modname, probe_function=None):
assert modname == "cuda.bindings.nvjitlink"
assert probe_function is not None
assert probe_function is None
err = ModuleNotFoundError("No module named 'not_a_real_dependency'")
err.name = "not_a_real_dependency"
raise err
Expand All @@ -93,7 +93,7 @@ def fake__optional_cuda_import(modname, probe_function=None):
def test_decide_nvjitlink_or_driver_falls_back_when_module_missing(monkeypatch):
def fake__optional_cuda_import(modname, probe_function=None):
assert modname == "cuda.bindings.nvjitlink"
assert probe_function is not None
assert probe_function is None
return None

monkeypatch.setattr(_linker, "_optional_cuda_import", fake__optional_cuda_import)
Expand All @@ -103,3 +103,34 @@ def fake__optional_cuda_import(modname, probe_function=None):

assert use_driver_backend is True
assert _linker._use_nvjitlink_backend is False


def test_decide_nvjitlink_or_driver_falls_back_when_nvjitlink_too_old(monkeypatch):
def fake__optional_cuda_import(modname, probe_function=None):
assert modname == "cuda.bindings.nvjitlink"
assert probe_function is None
return object()

monkeypatch.setattr(_linker, "_optional_cuda_import", fake__optional_cuda_import)
monkeypatch.setattr(_linker, "_nvjitlink_has_version_symbol", lambda _nvjitlink: False)

with pytest.warns(RuntimeWarning, match="too old \\(<12.3\\)"):
use_driver_backend = _linker._decide_nvjitlink_or_driver()

assert use_driver_backend is True
assert _linker._use_nvjitlink_backend is False


def test_decide_nvjitlink_or_driver_selects_nvjitlink_when_version_symbol_present(monkeypatch):
def fake__optional_cuda_import(modname, probe_function=None):
assert modname == "cuda.bindings.nvjitlink"
assert probe_function is None
return object()

monkeypatch.setattr(_linker, "_optional_cuda_import", fake__optional_cuda_import)
monkeypatch.setattr(_linker, "_nvjitlink_has_version_symbol", lambda _nvjitlink: True)

use_driver_backend = _linker._decide_nvjitlink_or_driver()

assert use_driver_backend is False
assert _linker._use_nvjitlink_backend is True
Loading