Skip to content

CUDA Helpers

These helpers use NVIDIA's NVML bindings to decide whether a GPU-specific session should run. They are intentionally conservative: if the driver cannot be queried, they report that CUDA is unavailable.

liblaf.nox_recipes.cuda_driver_version

cuda_driver_version() -> int | None

Return the CUDA driver version reported by NVIDIA's NVML library.

Returns:

  • int | None

    The integer version returned by

  • int | None

    pynvml.nvmlSystemGetCudaDriverVersion_v2(), or None when NVML

  • int | None

    cannot be initialized or no supported NVIDIA driver is available.

Source code in src/liblaf/nox_recipes/_cuda.py
def cuda_driver_version() -> int | None:
    """Return the CUDA driver version reported by NVIDIA's NVML library.

    Returns:
        The integer version returned by
        `pynvml.nvmlSystemGetCudaDriverVersion_v2()`, or `None` when NVML
        cannot be initialized or no supported NVIDIA driver is available.
    """
    initialized = False
    try:
        pynvml.nvmlInit()
        initialized = True
        version: int = pynvml.nvmlSystemGetCudaDriverVersion_v2()
    except pynvml.NVMLError:
        return None
    finally:
        if initialized:
            pynvml.nvmlShutdown()
    return version

liblaf.nox_recipes.supports_cuda

supports_cuda(version: int | None = None) -> bool

Return whether the current machine satisfies a CUDA requirement.

Parameters:

  • version

    (int | None, default: None ) –

    Optional minimum CUDA driver version. When omitted, the function only checks whether any CUDA driver is available.

Returns:

  • bool

    True if a CUDA driver is present and meets the requested minimum

  • bool

    version, otherwise False.

Source code in src/liblaf/nox_recipes/_cuda.py
def supports_cuda(version: int | None = None) -> bool:
    """Return whether the current machine satisfies a CUDA requirement.

    Args:
        version:
            Optional minimum CUDA driver version. When omitted, the function
            only checks whether any CUDA driver is available.

    Returns:
        `True` if a CUDA driver is present and meets the requested minimum
        version, otherwise `False`.
    """
    cuda_version: int | None = cuda_driver_version()
    if cuda_version is None:
        return False
    if version is None:
        return True
    return cuda_version >= version