Skip to content

feat(libtorch): report the identity of a CUDA device - #45

Open
NicolasRouquette wants to merge 8 commits into
lean-dojo:mainfrom
NicolasRouquette:libtorch-device-identity-v2
Open

NicolasRouquette wants to merge 8 commits into
lean-dojo:mainfrom
NicolasRouquette:libtorch-device-identity-v2

Conversation

@NicolasRouquette

@NicolasRouquette NicolasRouquette commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #43

This branch is based on #43 (the bridge on the host), so until #43 merges the diff here also shows

#43's five commits. This PR's own commits are the last three:

  • 38925c2d the change described above, with the two comments both PRs reword merged;
  • a1d36a02 gives identify_device a fallback in the CPU-only branch of kernels.cpp. Without it
    a CPU-only SDK does not link (undefined reference to torchlean::identify_device(long)'`). No
    device is visible there, so it fails with the error a CUDA-enabled SDK gives for an index outside
    the count (the Lean side rejects the index before the native call in any case);
  • 33195192 makes currentDeviceInfo fail when the bridge runs on the host. A visible GPU still
    reports its identity by index there, but it is not the device the work runs on, so a benchmark
    header would have named a GPU for host results. The test checks that rejection instead of
    comparing with Buffer.Memory.read, which reads 0 on the host; before, it failed with
    device identity: 20990394368 total bytes, but Buffer.Memory.read reports 0.

What

TorchLean.LibTorch.deviceInfo (index : UInt32) : IO DeviceInfo reads a visible CUDA device's name, compute capability (10 * major + minor, so 86 reads as sm_86), multiprocessor count, core and memory clocks, memory bus width, total memory and the driver's CUDA version.

currentDeviceInfo is deviceInfo (← getDevice), and DeviceInfo.format gives one line for a benchmark header:

NVIDIA RTX A4500 (device 0, sm_86, 56 SMs, 19.5 GiB, 640 GB/s peak, driver 13.0)

docs/TRUST_BOUNDARIES.md asks that runtime numerical evidence identify the device it was taken on; Buffer.Memory.read says how much memory the selected device has, and this says which device it is.

This reimplements #35 (itself the LibTorch follow-up to #30), which was closed when main replaced the APIs it targeted. It is new code against the Stable ABI bridge.

How

  • From the driver, in kernels.cpp. feat(libtorch): report device identity per index #35 read at::cuda::getDeviceProperties, which is ATen's C++ API. Here the record comes from cuDeviceGetName, cuDeviceGetAttribute, cuDeviceTotalMem and cuDriverGetVersion. These sit beside the device count and memory the bridge already takes from the driver, so unstable.cpp and the TRUST lists are unchanged, and the bridge still calls no CUDA runtime function. libtorch_abi.py --torch --library passes. The library gains three driver imports and no new NEEDED entry.
  • By index, in one call. One native call reads every field of one device, so the answer does not depend on the selected device, and a record never mixes fields of two devices (the review finding on feat(cuda): report device identity and compiled architecture #30). The read creates no context.
  • Failures. As setDevice does, deviceInfo checks the index against deviceCount before the native call, so an index outside the visible count, and every query without LibTorch (count 0), fails through IO with LibTorch: CUDA device i is outside the visible count n.
  • peakBandwidthBytesPerSecond is exact (Nat, double data rate), and format rounds with integer arithmetic.

Not carried over

The runtime version (#35's cudaRuntimeGetVersion) needs the CUDA runtime, which the bridge does not call. LibTorch.version already names the torch runtime.
#30's compiled-architecture warning has no counterpart either.
NVRTC compiles TorchLean's generated kernels for the device's own architecture, and the Stable ABI does not expose which architectures torch's own kernels were built for. The module docstring keeps those two questions apart.

Tests

TorchLeanTest/CUDA/DeviceInfo.lean, called from TorchLeanTest/Suite.lean under every runtime status: the formatting of a fixed record. With a device, every visible index reports itself with plausible nonzero fields, two reads agree, currentDeviceInfo reads the selected device, totalBytes agrees with Buffer.Memory.read's cuMemGetInfo total, and the index at the count is rejected.

When the bridge runs on the host, the visible devices still report themselves by index and currentDeviceInfo is rejected. Without a device, and without LibTorch, queries are rejected.

Checks run

RTX A4500 (sm_86, driver 580.126.18), torch 2.13.0+cu130 with a CUDA 13.0 toolkit, and torch
2.13.0 CPU-only, Lean v4.36.0, on a tree identical to 33195192's.

build / run result
-Kcuda=true build nn_tests_suite (cu130) Build completed successfully (17634 jobs).
same, CPU-only SDK Build completed successfully (17634 jobs).
TORCHLEAN_REQUIRE_CUDA=1 nn_tests_suite == TorchLean: all curated tests passed ==
TORCHLEAN_LIBTORCH_DEVICE=cpu TORCHLEAN_REQUIRE_NATIVE=1 nn_tests_suite the A4500 by index, the current device is rejected on the host, == TorchLean: all curated tests passed ==
CUDA_VISIBLE_DEVICES= nn_tests_suite (CUDA build) queries are rejected with no visible device, then the suite's own TorchLean was built with LibTorch, but no usable device is selected
TORCHLEAN_REQUIRE_NATIVE=1 nn_tests_suite (CPU-only SDK) the current device is rejected on the host, == TorchLean: all curated tests passed ==
scripts/lake.sh lint No findings.; the 29264 theorems of 1020 modules depend on at most propext, Classical.choice and Quot.sound

Not run: the blueprint build (the GPU chapter gains one paragraph, plus one clause in 33195192),
the sanitizer and elementwise C++ harnesses, and more than one device, since this host has one
GPU. The multi-device case rests on the per-index reads, not on an observation.

The LibTorch bridge runs on a CUDA device or on the host, through ATen's
CPU kernels. The device is selected once: TORCHLEAN_LIBTORCH_DEVICE=cpu
selects the host and =cuda a CUDA device; unset, a visible CUDA device,
and the host only with a CPU-only SDK. A CUDA-enabled SDK without a
visible device stays nativeUnavailable.

- CMake: an SDK without torch_cuda builds with TORCHLEAN_LIBTORCH_CUDA=0,
  without USE_CUDA, the CUDA toolkit, NVRTC or the driver.
- kernels.cpp and the caching-allocator calls compile for a CUDA SDK
  only; generated kernels call require_cuda and fail through IO on the
  host, and so does the encoded upload of configured binary arithmetic.
- axpy and the ordered segment sums, reached from pure entry points,
  have host loops with the kernels' fma and summation order.
- Tree folds and unfolds take their host references unless a CUDA
  device is selected (Buffer.cudaAvailable).
- Lean: LibTorch.DeviceKind, deviceKind, Buffer.requireCudaDevice. The
  suite's TORCHLEAN_REQUIRE_CUDA=1 requires a CUDA device, and
  TORCHLEAN_REQUIRE_NATIVE=1 the bridge on any device.
- TRUST lists CUDA C Shims and CUDA Unstable Remainder; libtorch_abi.py
  expects them exactly when the library links libtorch_cuda.so.
ATen's CPU log1p of torch 2.13 returns zero at the least subnormal of
binary32 and binary64, which lost the combined tail of the two-level
cross-entropy normalizer on the host. The bridge's log1p returns x on
the host wherever |x| < 2^-p, where x is the correctly rounded value;
CUDA's log1p is unchanged.

requireNativeRuntime reports why the bridge selected no device (an
unknown TORCHLEAN_LIBTORCH_DEVICE, or =cuda with a CPU-only SDK)
through torchlean_libtorch_unavailable_reason, and keeps its message
when no CUDA device is visible.
On the host, generated kernels and configured-binary recordings are
checked to be refused through IO (KernelGradient, Elementwise), the
native memory probes are replaced by a check that the allocator
statistics read zero (Stress), and the graph checks of configured words
and custom operations run on CUDA only (Graph). Every other check runs
on both devices.
The backend README gains a Host device section: the selection table,
what runs, what is refused and the log1p repair. scripts/README.md,
TorchLeanTest/README.md, the tree README and the CUDA page describe
TORCHLEAN_REQUIRE_NATIVE=1 and the CPU-only SDK build.
@NicolasRouquette
NicolasRouquette force-pushed the libtorch-device-identity-v2 branch from 090dcc6 to 139c962 Compare November 23, 2026 16:46
`TorchLean.LibTorch.deviceInfo index` reads a visible CUDA device's name,
compute capability, multiprocessor count, core and memory clocks, memory
bus width, total memory and the driver's CUDA version, and
`currentDeviceInfo` reads the selected device. `DeviceInfo.format` gives
one line for a benchmark header, and `peakBandwidthBytesPerSecond` the
theoretical peak a measured bandwidth can be compared with.

The record comes from the CUDA driver in `kernels.cpp`, like the device
count and memory the bridge already reports, so the bridge still calls no
CUDA runtime function. One native call reads every field of one device by
index, creates no context, and does not depend on the selected device.
An index outside the visible count fails through `IO`, as `setDevice`
does, and so does every query without LibTorch.
A CPU-only SDK compiles none of the driver code in kernels.cpp, so its
fallback branch needs identify_device too, or the library does not link.
No device is visible there, so every index is outside the count, with the
error a CUDA-enabled SDK gives.
On the host bridge the visible devices still report their identity by
index, but none of them is the device the work runs on, so
currentDeviceInfo would name a GPU for host evidence. It now fails there.
The test checks that rejection instead of comparing the current device's
memory with Buffer.Memory.read, which reads zero on the host.
@NicolasRouquette
NicolasRouquette force-pushed the libtorch-device-identity-v2 branch from 139c962 to 3319519 Compare November 23, 2026 16:49
NicolasRouquette added a commit to NicolasRouquette/TorchLean that referenced this pull request Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant