Repository navigation
feat(libtorch): report the identity of a CUDA device - #45
Open
NicolasRouquette wants to merge 8 commits into
Open
NicolasRouquette wants to merge 8 commits into
NicolasRouquette wants to merge 8 commits into
Conversation
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
force-pushed
the
libtorch-device-identity-v2
branch
from
November 23, 2026 16:46
090dcc6 to
139c962
Compare
`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
force-pushed
the
libtorch-device-identity-v2
branch
from
November 23, 2026 16:49
139c962 to
3319519
Compare
NicolasRouquette
added a commit
to NicolasRouquette/TorchLean
that referenced
this pull request
Nov 23, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
38925c2dthe change described above, with the two comments both PRs reword merged;a1d36a02givesidentify_devicea fallback in the CPU-only branch ofkernels.cpp. Without ita CPU-only SDK does not link (
undefined reference totorchlean::identify_device(long)'`). Nodevice 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);
33195192makescurrentDeviceInfofail when the bridge runs on the host. A visible GPU stillreports 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 withdevice identity: 20990394368 total bytes, but Buffer.Memory.read reports 0.What
TorchLean.LibTorch.deviceInfo (index : UInt32) : IO DeviceInforeads a visible CUDA device's name, compute capability (10 * major + minor, so86reads assm_86), multiprocessor count, core and memory clocks, memory bus width, total memory and the driver's CUDA version.currentDeviceInfoisdeviceInfo (← getDevice), andDeviceInfo.formatgives one line for a benchmark header:docs/TRUST_BOUNDARIES.mdasks that runtime numerical evidence identify the device it was taken on;Buffer.Memory.readsays 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
mainreplaced the APIs it targeted. It is new code against the Stable ABI bridge.How
kernels.cpp. feat(libtorch): report device identity per index #35 readat::cuda::getDeviceProperties, which is ATen's C++ API. Here the record comes fromcuDeviceGetName,cuDeviceGetAttribute,cuDeviceTotalMemandcuDriverGetVersion. These sit beside the device count and memory the bridge already takes from the driver, sounstable.cppand the TRUST lists are unchanged, and the bridge still calls no CUDA runtime function.libtorch_abi.py --torch --librarypasses. The library gains three driver imports and no newNEEDEDentry.setDevicedoes,deviceInfochecks the index againstdeviceCountbefore the native call, so an index outside the visible count, and every query without LibTorch (count 0), fails throughIOwithLibTorch: CUDA device i is outside the visible count n.peakBandwidthBytesPerSecondis exact (Nat, double data rate), andformatrounds with integer arithmetic.Not carried over
The runtime version (#35's
cudaRuntimeGetVersion) needs the CUDA runtime, which the bridge does not call.LibTorch.versionalready 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 fromTorchLeanTest/Suite.leanunder every runtime status: the formatting of a fixed record. With a device, every visible index reports itself with plausible nonzero fields, two reads agree,currentDeviceInforeads the selected device,totalBytesagrees withBuffer.Memory.read'scuMemGetInfototal, and the index at the count is rejected.When the bridge runs on the host, the visible devices still report themselves by index and
currentDeviceInfois 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.-Kcuda=true build nn_tests_suite(cu130)Build completed successfully (17634 jobs).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_suitethe 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 ownTorchLean was built with LibTorch, but no usable device is selectedTORCHLEAN_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 lintNo findings.;the 29264 theorems of 1020 modules depend on at most propext, Classical.choice and Quot.soundNot 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.