# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import importlib import random import subprocess import sys from pathlib import Path import pytest from cuda.bindings._internal.utils import get_c_compiler from cuda.bindings._v2 import driver, runtime from cuda.bindings.utils import ( check_nvvm_compiler_options, get_cuda_native_handle, get_minimal_required_cuda_ver_from_ptx_ver, get_ptx_ver, ) from cuda.bindings.utils._ptx_utils import _ptx_to_cuda have_cufile = importlib.util.find_spec("cuda.bindings.cufile") is not None have_cudla = importlib.util.find_spec("cuda.bindings.cudla") is not None def _is_libnvvm_available() -> bool: from cuda.bindings._internal.nvvm import _inspect_function_pointer from cuda.pathfinder import DynamicLibNotFoundError try: return _inspect_function_pointer("__nvvmCreateProgram") != 0 except DynamicLibNotFoundError: return False _libnvvm_available = _is_libnvvm_available() _skip_no_libnvvm = pytest.mark.skipif(not _libnvvm_available, reason="libNVVM not available") ptx_88_kernel = r""" .version 8.8 .target sm_75 .address_size 64 // .globl empty_kernel .visible .entry empty_kernel() { ret; } """ ptx_72_kernel = r""" .version 7.2 .target sm_75 .address_size 64 // .globl empty_kernel .visible .entry empty_kernel() { ret; } """ @pytest.mark.parametrize( "kernel,actual_ptx_ver,min_cuda_ver", ((ptx_88_kernel, "8.8", 12090), (ptx_72_kernel, "7.2", 11020)) ) def test_ptx_utils(kernel, actual_ptx_ver, min_cuda_ver): ptx_ver = get_ptx_ver(kernel) assert ptx_ver == actual_ptx_ver cuda_ver = get_minimal_required_cuda_ver_from_ptx_ver(ptx_ver) assert cuda_ver == min_cuda_ver @pytest.mark.agent_authored(model="gpt-6") def test_ptx_mapping_covers_bindings_cuda_version(): """Keep the PTX table in sync with the CUDA release of the generated bindings.""" cuda_version = (driver.CUDA_VERSION // 1000, (driver.CUDA_VERSION % 1000) // 10) assert cuda_version in _ptx_to_cuda.values(), ( f"No PTX ISA mapping for CUDA {cuda_version[0]}.{cuda_version[1]}. " "Check the PTX ISA release history and update cuda_bindings_13/cuda/bindings/utils/_ptx_utils.py." ) @pytest.mark.agent_authored(model="claude-sonnet-5-5") def test_get_handle_v2_int_passthrough(): # The _v2 bindings return handles as plain ints, which already are native handles. ptr = random.randint(1, 1024) assert get_cuda_native_handle(ptr) == ptr @pytest.mark.agent_authored(model="claude-sonnet-5-5") @pytest.mark.parametrize("obj", (True, 1.5, "1", b"1")) def test_get_handle_non_int_scalars_are_rejected(obj): with pytest.raises(TypeError): get_cuda_native_handle(obj) @pytest.mark.agent_authored(model="claude-sonnet-5-5") def test_get_handle_v2_real_handles(device): stream = driver.stream_create(0) event = runtime.event_create() graph = runtime.graph_create(0) try: for handle in (stream, event, graph): assert isinstance(handle, int) assert get_cuda_native_handle(handle) == handle finally: runtime.graph_destroy(graph) runtime.event_destroy(event) driver.stream_destroy_v2(stream) @pytest.mark.parametrize( "target", ( (1, 2, 3, 4), [5, 6], {}, None, ), ) def test_get_handle_error(target): with pytest.raises(TypeError) as e: handle = get_cuda_native_handle(target) @pytest.mark.agent_authored(model="claude-opus-5") def test_get_handle_does_not_report_a_registered_type_as_unknown(monkeypatch): """A KeyError from inside a handle getter is a bug in that getter. Reporting it as "Unknown type" is wrong twice over: the type *is* registered, and `from None` hides the traceback that would say otherwise. """ from cuda.bindings.utils import _handle_getters class Registered: pass def getter(_obj): raise KeyError("lookup inside the getter failed") monkeypatch.setitem(_handle_getters, Registered, getter) with pytest.raises(KeyError, match="lookup inside the getter failed"): get_cuda_native_handle(Registered()) @pytest.mark.parametrize( "module", # Top-level modules for external Python use [ "driver", "runtime", "nvfatbin", "nvjitlink", "nvml", "nvrtc", "nvvm", "utils", "_v2.driver", "_v2.nvrtc", "_v2.runtime", *(["cudla"] if have_cudla else []), *(["cufile"] if have_cufile else []), ], ) def test_cyclical_imports(module): subprocess.check_call( # noqa: S603 [sys.executable, Path(__file__).parent / "utils" / "check_cyclical_import.py", f"cuda.bindings.{module}"], ) def test_get_c_compiler(): c_compiler = get_c_compiler() prefix = ("GCC", "Clang", "MSVC", "Unknown") assert sum(c_compiler.startswith(p) for p in prefix) == 1 @_skip_no_libnvvm def test_check_nvvm_compiler_options_valid(): assert check_nvvm_compiler_options(["-arch=compute_90"]) is True @_skip_no_libnvvm def test_check_nvvm_compiler_options_invalid(): assert check_nvvm_compiler_options(["--this-is-not-a-valid-option"]) is False @_skip_no_libnvvm def test_check_nvvm_compiler_options_empty(): assert check_nvvm_compiler_options([]) is True @_skip_no_libnvvm def test_check_nvvm_compiler_options_multiple_valid(): assert check_nvvm_compiler_options(["-arch=compute_90", "-opt=3", "-g"]) is True @_skip_no_libnvvm def test_check_nvvm_compiler_options_arch_detection(): assert check_nvvm_compiler_options(["-arch=compute_90"]) is True assert check_nvvm_compiler_options(["-arch=compute_99999"]) is False # This test is not currently run in any CI job def test_check_nvvm_compiler_options_no_libnvvm(): if _libnvvm_available: pytest.skip("libNVVM is available; this test targets the fallback path") assert check_nvvm_compiler_options(["-arch=compute_90"]) is False