Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_gpu_proof/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.0"
__version__ = "0.5.1.dev0"
6 changes: 4 additions & 2 deletions src/pytest_gpu_proof/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
67 changes: 65 additions & 2 deletions src/pytest_gpu_proof/receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from __future__ import annotations

import base64
import ctypes
import datetime
import json
import os
import platform
import subprocess
import sys
import tempfile
import uuid
from pathlib import Path
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -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(
Expand All @@ -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(",")]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_plugin_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
84 changes: 84 additions & 0 deletions tests/test_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -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


Expand Down
Loading