diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 192b4d5..5c16152 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.10", "3.11", "3.12", "3.13"] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] steps: - uses: actions/checkout@v4 diff --git a/CHANGELOG.md b/CHANGELOG.md index 428c44b..3ae90ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,18 @@ All notable changes to pytest-gpu-proof are documented here. The format is based on [Keep a Changelog](https://keepachangelog.com/); versions follow [SemVer](https://semver.org/) (pre-1.0: minor bumps may break). +## [Unreleased] + +### Added + +- Python 3.9 support (the oldest Python on a JetPack 5 Jetson), with a CI test + lane. The code already avoided 3.10-only syntax; only the floor moved. +- `environment.gpu_info` and the `gpu_required` probe fall back to the CUDA + driver library (`libcuda` through `ctypes`) when neither `nvidia-smi` nor + PyTorch is available, as on Jetson boards before JetPack 7. The record names + its source (`nvidia-smi`, `torch`, or `libcuda`) and stays self-reported + metadata, not evidence of execution. + ## [0.5.0] — 2026-10-28 ### Added diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6146764..7e69386 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -19,7 +19,7 @@ python -m build python -m twine check --strict dist/* ``` -CI runs the test suite on Python 3.10, 3.11, 3.12, and 3.13, independently enforces +CI runs the test suite on Python 3.9, 3.10, 3.11, 3.12, and 3.13, independently enforces 100% line and branch coverage, builds docs strictly, and smoke-tests the wheel. ## Change expectations diff --git a/README.md b/README.md index 3e71426..35d31f0 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ a map that shard bin-packers can calibrate from. |---|---| | `@pytest.mark.gpu_proof` | Include the test in the receipt | | `@pytest.mark.gpu_equivalence` | Alias for `gpu_proof` | -| `@pytest.mark.gpu_required` | Skip when neither `nvidia-smi` nor PyTorch reports a GPU | +| `@pytest.mark.gpu_required` | Skip when none of `nvidia-smi`, PyTorch, or the CUDA driver library (`libcuda` through `ctypes`) reports a GPU | Skipped tests are recorded. Verification rejects them by default. Prefer an exact `--expected-skips` baseline over the broad `--allow-skipped` escape diff --git a/docs/architecture.md b/docs/architecture.md index 7cc4b62..baa2057 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -52,7 +52,9 @@ The top-level blocks are: - `fingerprint`: manifest algorithm, tracked paths, extra paths, count, digest; - `session`: start/end, pass/fail, exact node IDs, pytest arguments; - `tests`: terminal outcome, phase, duration, and comparison checks; -- `environment`: Python, platform, pytest, plugin, and self-reported GPU data; +- `environment`: Python, platform, pytest, plugin, and self-reported GPU data + (from `nvidia-smi`, else PyTorch, else the CUDA driver library through `ctypes`; + `gpu_info.source` names which); - `shards`: optional narrow manifests and carry metadata; - `signer`: signed username, algorithm, backend, and key fingerprint; - `signature`: base64 signature value only. diff --git a/pyproject.toml b/pyproject.toml index 7625922..dd913f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,10 +4,10 @@ build-backend = "hatchling.build" [project] name = "pytest-gpu-proof" -version = "0.5.0" +version = "0.5.1.dev0" description = "pytest plugin for GPU equivalence testing with signed receipts verified via GitHub SSH keys" readme = "README.md" -requires-python = ">=3.10" +requires-python = ">=3.9" license = { text = "MIT" } keywords = ["pytest", "gpu", "cuda", "testing", "signing", "attestation"] classifiers = [ @@ -17,6 +17,7 @@ classifiers = [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", diff --git a/src/pytest_gpu_proof/__init__.py b/src/pytest_gpu_proof/__init__.py index 3d18726..0759765 100644 --- a/src/pytest_gpu_proof/__init__.py +++ b/src/pytest_gpu_proof/__init__.py @@ -1 +1 @@ -__version__ = "0.5.0" +__version__ = "0.5.1.dev0" diff --git a/src/pytest_gpu_proof/plugin.py b/src/pytest_gpu_proof/plugin.py index 6352632..d6cc85e 100644 --- a/src/pytest_gpu_proof/plugin.py +++ b/src/pytest_gpu_proof/plugin.py @@ -30,10 +30,12 @@ def _has_gpu() -> bool: pass try: import torch - return torch.cuda.is_available() + if torch.cuda.is_available(): + return True except ImportError: pass - return False + from .receipt import _gpu_info_from_libcuda + return _gpu_info_from_libcuda(include_uuid=False) is not None class GpuProofPlugin: diff --git a/src/pytest_gpu_proof/receipt.py b/src/pytest_gpu_proof/receipt.py index b01af4c..81375be 100644 --- a/src/pytest_gpu_proof/receipt.py +++ b/src/pytest_gpu_proof/receipt.py @@ -3,6 +3,7 @@ from __future__ import annotations import base64 +import ctypes import datetime import json import os @@ -10,6 +11,7 @@ import subprocess import sys import tempfile +import uuid from pathlib import Path from typing import Any, Dict, List, Optional @@ -56,6 +58,67 @@ def _gpu_info_from_torch() -> Optional[Dict[str, Any]]: "source": "torch"} +_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75 +_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR = 76 + + +def _gpu_info_from_libcuda(include_uuid: bool = True) -> Optional[Dict[str, Any]]: + """nvidia-smi-free, torch-free fallback through the CUDA driver API: ctypes + on libcuda. Jetson boards before JetPack 7 ship no nvidia-smi, and a plain + pytest venv there has no torch, so without this a Jetson receipt records + `gpu_info: null` and fails `require_gpu` for the wrong reason. Still + self-reported metadata, like the other two sources.""" + try: + cuda = ctypes.CDLL("libcuda.so.1") + except OSError: + return None + + def call(name: str, *args: Any) -> None: + rc = getattr(cuda, name)(*args) + if rc != 0: + raise OSError(f"{name} returned CUresult {rc}") + + try: + call("cuInit", 0) + count = ctypes.c_int() + call("cuDeviceGetCount", ctypes.byref(count)) + driver = ctypes.c_int() + call("cuDriverGetVersion", ctypes.byref(driver)) + driver_version = f"{driver.value // 1000}.{(driver.value % 1000) // 10}" + devices = [] + for index in range(count.value): + device = ctypes.c_int() + call("cuDeviceGet", ctypes.byref(device), index) + name = ctypes.create_string_buffer(256) + call("cuDeviceGetName", name, 256, device) + major, minor = ctypes.c_int(), ctypes.c_int() + call("cuDeviceGetAttribute", ctypes.byref(major), + _CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device) + call("cuDeviceGetAttribute", ctypes.byref(minor), + _CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device) + total = ctypes.c_size_t() + call("cuDeviceTotalMem_v2", ctypes.byref(total), device) + device_uuid = None + if include_uuid: + raw = ctypes.create_string_buffer(16) + call("cuDeviceGetUuid", raw, device) + device_uuid = f"GPU-{uuid.UUID(bytes=raw.raw)}" + devices.append({ + "index": str(index), + "uuid": device_uuid, + "name": name.value.decode("utf-8", "replace"), + "driver_version": driver_version, + "memory": f"{total.value // (1024 * 1024)} MiB", + "compute_capability": f"{major.value}.{minor.value}", + }) + except (AttributeError, OSError): # missing symbol, failed driver call + return None + if not devices: + return None + return {"devices": devices, "cuda_visible_devices": os.environ.get("CUDA_VISIBLE_DEVICES"), + "source": "libcuda"} + + def _gpu_info(include_uuid: bool = True) -> Optional[Dict[str, Any]]: try: result = subprocess.run( @@ -69,9 +132,9 @@ def _gpu_info(include_uuid: bool = True) -> Optional[Dict[str, Any]]: timeout=10, ) except (FileNotFoundError, subprocess.TimeoutExpired): - return _gpu_info_from_torch() + return _gpu_info_from_torch() or _gpu_info_from_libcuda(include_uuid) if result.returncode != 0 or not result.stdout.strip(): - return _gpu_info_from_torch() + return _gpu_info_from_torch() or _gpu_info_from_libcuda(include_uuid) devices = [] for line in result.stdout.splitlines(): parts = [part.strip() for part in line.split(",")] diff --git a/tests/test_plugin_capture.py b/tests/test_plugin_capture.py index 6dc9f07..63841b4 100644 --- a/tests/test_plugin_capture.py +++ b/tests/test_plugin_capture.py @@ -455,6 +455,31 @@ def no_torch(name, *args, **kwargs): raise ImportError return real_import(name, *args, **kwargs) monkeypatch.setattr(builtins, "__import__", no_torch) + monkeypatch.setattr("pytest_gpu_proof.receipt._gpu_info_from_libcuda", + lambda include_uuid=True: None) + assert plugin_module._has_gpu() is False + + +def test_has_gpu_falls_back_to_libcuda(monkeypatch): + """2026-11-02: the marker probe and the receipt share the libcuda fallback, + so a Jetson without nvidia-smi or torch neither skips gpu_required tests + nor records gpu_info: null.""" + import subprocess + from types import SimpleNamespace + + monkeypatch.setattr(subprocess, "run", lambda *a, **k: subprocess.CompletedProcess([], 1)) + monkeypatch.setitem(__import__("sys").modules, "torch", + SimpleNamespace(cuda=SimpleNamespace(is_available=lambda: False))) + seen = [] + + def probe(include_uuid=True): + seen.append(include_uuid) + return {"devices": [{"index": "0"}], "cuda_visible_devices": None, "source": "libcuda"} + monkeypatch.setattr("pytest_gpu_proof.receipt._gpu_info_from_libcuda", probe) + assert plugin_module._has_gpu() is True + assert seen == [False] + monkeypatch.setattr("pytest_gpu_proof.receipt._gpu_info_from_libcuda", + lambda include_uuid=True: None) assert plugin_module._has_gpu() is False @@ -517,6 +542,8 @@ def no_torch(name, *args, **kwargs): raise ImportError return real_import(name, *args, **kwargs) monkeypatch.setattr(builtins, "__import__", no_torch) + monkeypatch.setattr("pytest_gpu_proof.receipt._gpu_info_from_libcuda", + lambda include_uuid=True: None) assert plugin_module._has_gpu() is False diff --git a/tests/test_receipt.py b/tests/test_receipt.py index f64bba1..7401195 100644 --- a/tests/test_receipt.py +++ b/tests/test_receipt.py @@ -110,6 +110,8 @@ def test_gpu_capture_unavailable(monkeypatch, behavior): monkeypatch.setattr( "pytest_gpu_proof.receipt.subprocess.run", lambda *a, **k: behavior() ) + monkeypatch.setattr("pytest_gpu_proof.receipt._gpu_info_from_libcuda", + lambda include_uuid=True: None) assert _gpu_info() is None @@ -313,6 +315,7 @@ def test_gpu_info_falls_back_to_torch_when_nvidia_smi_is_absent(monkeypatch): def no_smi(*args, **kwargs): raise FileNotFoundError("nvidia-smi") monkeypatch.setattr(receipt_mod.subprocess, "run", no_smi) + monkeypatch.setattr(receipt_mod, "_gpu_info_from_libcuda", lambda include_uuid=True: None) props = types.SimpleNamespace(name="Fake GPU", total_memory=8 * 1024 ** 3, major=12, minor=0) fake_cuda = types.SimpleNamespace(is_available=lambda: True, device_count=lambda: 1, get_device_properties=lambda i: props) @@ -338,6 +341,87 @@ def test_gpu_info_falls_back_to_torch_on_nvidia_smi_failure(monkeypatch): monkeypatch.setattr(receipt_mod.subprocess, "run", lambda *a, **k: types.SimpleNamespace(returncode=1, stdout="")) monkeypatch.setitem(sys.modules, "torch", None) + monkeypatch.setattr(receipt_mod, "_gpu_info_from_libcuda", lambda include_uuid=True: None) + assert receipt_mod._gpu_info() is None + + +class _FakeLibcuda: + """Stand-in for ctypes.CDLL('libcuda.so.1'): every entry point writes the + canned answer into its ctypes out-argument and returns CUDA_SUCCESS, except + `fail`, which returns a nonzero CUresult, and `missing`, which is absent.""" + + def __init__(self, count=1, fail=None, missing=None): + self.count, self.fail, self.missing = count, fail, missing + + def __getattr__(self, name): + if name == self.missing: + raise AttributeError(name) + + def entry(*args): + if name == self.fail: + return 999 + if name == "cuDeviceGetCount": + args[0]._obj.value = self.count + elif name == "cuDriverGetVersion": + args[0]._obj.value = 13020 + elif name == "cuDeviceGet": + args[0]._obj.value = args[1] + elif name == "cuDeviceGetName": + args[0].value = b"Fake Orin" + elif name == "cuDeviceGetAttribute": + args[0]._obj.value = {75: 8, 76: 7}[args[1]] + elif name == "cuDeviceTotalMem_v2": + args[0]._obj.value = 62878 * 1024 * 1024 + elif name == "cuDeviceGetUuid": + args[0].raw = bytes(range(16)) + return 0 + return entry + + +def _no_smi_no_torch(monkeypatch, receipt_mod): + import sys + + def no_smi(*args, **kwargs): + raise FileNotFoundError("nvidia-smi") + monkeypatch.setattr(receipt_mod.subprocess, "run", no_smi) + monkeypatch.setitem(sys.modules, "torch", None) + + +def test_gpu_info_falls_back_to_libcuda(monkeypatch): + """2026-11-02: a Jetson before JetPack 7 has no nvidia-smi and a plain venv + has no torch; the CUDA driver library still identifies the device.""" + from pytest_gpu_proof import receipt as receipt_mod + _no_smi_no_torch(monkeypatch, receipt_mod) + monkeypatch.setattr(receipt_mod.ctypes, "CDLL", lambda name: _FakeLibcuda(count=2)) + monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0") + info = receipt_mod._gpu_info() + assert info["source"] == "libcuda" + assert info["cuda_visible_devices"] == "0" + assert info["devices"][0] == { + "index": "0", "uuid": "GPU-00010203-0405-0607-0809-0a0b0c0d0e0f", "name": "Fake Orin", + "driver_version": "13.2", "memory": "62878 MiB", "compute_capability": "8.7"} + assert info["devices"][1]["index"] == "1" + anonymous = receipt_mod._gpu_info(include_uuid=False) + assert [d["uuid"] for d in anonymous["devices"]] == [None, None] + assert anonymous["devices"][0]["name"] == "Fake Orin" + + +@pytest.mark.parametrize("fake", [ + None, # libcuda.so.1 not loadable + _FakeLibcuda(fail="cuInit"), # a driver call fails + _FakeLibcuda(fail="cuDeviceGetUuid"), # a per-device call fails + _FakeLibcuda(missing="cuDeviceGetName"), # symbol absent from the library + _FakeLibcuda(count=0), # driver loads, no device +]) +def test_gpu_info_libcuda_fallback_fails_closed(monkeypatch, fake): + from pytest_gpu_proof import receipt as receipt_mod + _no_smi_no_torch(monkeypatch, receipt_mod) + + def load(name): + if fake is None: + raise OSError(f"{name}: cannot open shared object file") + return fake + monkeypatch.setattr(receipt_mod.ctypes, "CDLL", load) assert receipt_mod._gpu_info() is None