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
33 changes: 30 additions & 3 deletions cuda_bindings/cuda/bindings/utils/_version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@
_major_version_compatibility_checked = False
_lock = threading.Lock()

_DISABLE_WARNING_ENV_VAR = "CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING"


def _warning_disabled() -> bool:
"""Whether the user asked to suppress the major-version warning.

``=0`` means "do not suppress". A bare truthiness test on the raw string
made ``CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING=0`` suppress the warning
-- the exact opposite of what the warning itself tells the user to type,
and the opposite of the other boolean knobs in this repository
(``CUDA_PYTHON_CUDA_PER_THREAD_DEFAULT_STREAM`` and
``CUDA_CORE_DONT_FIX_TAB_COMPLETION``), which both parse their value with
``int()``.

Unset and empty still mean "not disabled". A value that is not an integer
keeps the old set-means-disabled behaviour, so anyone currently relying on
a spelling like ``=true`` does not silently start seeing the warning again.
"""
raw = os.environ.get(_DISABLE_WARNING_ENV_VAR, "").strip()
if not raw:
return False
try:
return int(raw) != 0
except ValueError:
return True


def warn_if_cuda_major_version_mismatch():
"""Warn if the CUDA driver major version is older than cuda-bindings compile-time version.
Expand All @@ -21,7 +47,8 @@ def warn_if_cuda_major_version_mismatch():
The check runs only once per process. Subsequent calls are no-ops.

The warning can be suppressed by setting the environment variable
``CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING=1``.
``CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING=1``. Setting it to ``0`` (or
leaving it unset or empty) keeps the warning enabled.
"""
global _major_version_compatibility_checked
if _major_version_compatibility_checked:
Expand All @@ -32,7 +59,7 @@ def warn_if_cuda_major_version_mismatch():
_major_version_compatibility_checked = True

# Allow users to suppress the warning
if os.environ.get("CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING"):
if _warning_disabled():
return

# Import here to avoid circular imports and allow lazy loading
Expand All @@ -55,7 +82,7 @@ def warn_if_cuda_major_version_mismatch():
f"NVIDIA driver only supports up to CUDA {runtime_major}. Some cuda-bindings "
f"features may not work correctly. Consider updating your NVIDIA driver, "
f"or using a cuda-bindings version built for CUDA {runtime_major}. "
f"(Set CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING=1 to suppress this warning.)",
f"(Set {_DISABLE_WARNING_ENV_VAR}=1 to suppress this warning.)",
UserWarning,
stacklevel=3,
)
47 changes: 47 additions & 0 deletions cuda_bindings/tests/test_version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,53 @@ def test_warning_suppressed_by_env_var(self):
warn_if_cuda_major_version_mismatch()
assert len(w) == 0

@pytest.mark.agent_authored(model="claude-opus-5")
@pytest.mark.parametrize(
("raw", "expected"),
[
pytest.param("0", False, id="zero"),
pytest.param(" 0 ", False, id="zero-padded"),
pytest.param("", False, id="empty"),
pytest.param(" ", False, id="blank"),
pytest.param("1", True, id="one"),
pytest.param("2", True, id="two"),
# Not an integer: keep the old set-means-disabled behaviour so no
# one relying on a spelling like `=true` starts seeing the warning
# again.
pytest.param("true", True, id="true"),
pytest.param("yes", True, id="yes"),
],
)
def test_disable_flag_parsing(self, monkeypatch, raw, expected):
monkeypatch.setenv("CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING", raw)
assert _version_check._warning_disabled() is expected

@pytest.mark.agent_authored(model="claude-opus-5")
def test_disable_flag_unset(self, monkeypatch):
monkeypatch.delenv("CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING", raising=False)
assert _version_check._warning_disabled() is False

@pytest.mark.agent_authored(model="claude-opus-5")
def test_warning_not_suppressed_when_env_var_is_zero(self):
"""``=0`` is how a user says "no, keep warning me".

A bare truthiness test on the raw string made ``=0`` suppress the
warning -- the opposite of what the warning itself tells the user to
type, and the opposite of the other boolean knobs in this repository
(``CUDA_PYTHON_CUDA_PER_THREAD_DEFAULT_STREAM``,
``CUDA_CORE_DONT_FIX_TAB_COMPLETION``), which both parse with ``int()``.
"""
with (
mock.patch.object(driver, "CUDA_VERSION", 13000),
mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)),
mock.patch.dict(os.environ, {"CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING": "0"}),
warnings.catch_warnings(record=True) as w,
):
warnings.simplefilter("always")
warn_if_cuda_major_version_mismatch()
assert len(w) == 1
assert issubclass(w[0].category, UserWarning)

def test_error_when_driver_version_fails(self):
"""Should raise RuntimeError if cuDriverGetVersion fails."""
with (
Expand Down
Loading