Repository navigation
Run the LibTorch bridge on the host when the SDK is CPU-only (#36, reimplemented) - #43
Open
NicolasRouquette wants to merge 5 commits into
Open
NicolasRouquette wants to merge 5 commits into
NicolasRouquette wants to merge 5 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
marked this pull request as ready for review
November 23, 2026 04:36
NicolasRouquette
added a commit
to NicolasRouquette/TorchLean
that referenced
this pull request
Nov 23, 2026
…libtorch-host-device-v2, lean-dojo#43)
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.
What
This reimplements #36 on the current bridge. #36 was closed because
mainreplaced the APIs ittargeted; this is new code against the Stable ABI bridge, based on
f9dff853. It keeps thetwo points settled in #36's review:
TORCHLEAN_REQUIRE_CUDA=1requires a CUDA device (Buffer.requireCudaDevice), andTORCHLEAN_REQUIRE_NATIVE=1requires the bridge on whichever device it selected. It also keeps the integration requirement for NVRTC work: CUDA-only code compiles conditionally, and on the host it fails with an error naming the feature.The bridge runs on a CUDA device or on the host, through ATen's CPU kernels, chosen once:
TORCHLEAN_LIBTORCH_DEVICE=cpuselects the host and=cudaa CUDA device.When it is unset, a visible CUDA device is selected, and the host only when the SDK is CPU-only.
A CUDA-enabled SDK with no visible device stays
RuntimeStatus.nativeUnavailable, as today, with the same message.Any other value, or
=cudawith a CPU-only SDK, isnativeUnavailabletoo, and the error names the reason..nativeAvailablemeans "a device is selected", sorequireNativeRuntimeand existing matches are unchanged.LibTorch.DeviceKind/deviceKindreport which device, andBuffer.cudaAvailableis the test for paths that launch generated CUDA.What is new compared with #36
On
main, generated kernels back core operations, not only custom ones, which changes the design:axpyand the ordered segment sums (Adam/AdamW, column sums, scan gradients, row scatters) are reached from pure entry points, where an error would end the process. They get host loops that call the same correctly roundedfmaand fold in the kernels' order.TorchLeanTest/CUDA/Numerics.lean's single-rounding and in-order fixtures pass on the host.Hylo.runandUnfold.runalready fall back to their host references (results_eq), so they now take that path unless a CUDA device is selected.setDevice,setMemoryFractioncallrequire_cuda("<feature>")first and fail throughIOon the host. The encoded upload refuses because the puretorchlean_kernel_run_encodedwould otherwise be reached.aoti_torch_is_contiguous) and CUDA Unstable Remainder (the caching allocator).libtorch_abi.pyexpects them exactly when the library linkslibtorch_cuda.so. With--torch, it also checks that the SDK agrees.Build: an SDK without
torch_cudacompiles withTORCHLEAN_LIBTORCH_CUDA=0, withoutUSE_CUDA, the CUDA toolkit, NVRTC or the driver.On the host, allocator statistics and driver memory read zero, and
synchronizeandemptyCachereturn at once.One ATen CPU difference this needed
ATen's CPU
log1pof torch 2.13 returns0at the least subnormal,2^-149in binary32 and2^-1074in binary64, where the correctly rounded result is the argument itself (checked with pip torch 2.13.0+cpu;1e-42and1e-310come back unchanged). That loses the combined tail of the two-level cross-entropy normalizer, andcheckCrossEntropyUnderflowfailed on the host. The bridge'slog1pnow returnsxon the host wherever|x| < 2^-p, wherexis the correctly rounded value, since the correction-x²/2is below half an ulp. CUDA'slog1pis unchanged.Tests on the host
The CUDA suite runs on the host with these differences: the generated-kernel and configured-binary checks assert that the host refuses them (
KernelGradient,Elementwise); the native memory probes are replaced by a check that the allocator statistics read zero (Stress); the graph checks of configured words and custom operations run on CUDA only (Graph); andtree_testsskips its device checks. Everything else runs on both devices.Verification
RTX A4500 (driver 580), Lean v4.36.0, on
d1f44f24. SDKs: pip torch 2.13.0+cu130 with a CUDA 13.0 toolkit, and pip torch 2.13.0+cpu with no toolkit.cudaTORCHLEAN_REQUIRE_CUDA=1tree_testspassedhostTORCHLEAN_LIBTORCH_DEVICE=cpu TORCHLEAN_REQUIRE_NATIVE=1tree_testspassed (device checks skipped)hostTORCHLEAN_LIBTORCH_DEVICE=cpu TORCHLEAN_REQUIRE_CUDA=1a CUDA device was required, but the LibTorch bridge runs on the host: the SDK is CPU-only, or TORCHLEAN_LIBTORCH_DEVICE=cpu selected the hostTORCHLEAN_LIBTORCH_DEVICE=gpu TORCHLEAN_REQUIRE_NATIVE=1the LibTorch bridge selected no device: TORCHLEAN_LIBTORCH_DEVICE="gpu" is neither "cuda" nor "cpu"ldd:libtorch_cpu,libc10, no CUDA library)hostTORCHLEAN_REQUIRE_NATIVE=1tree_testspassed (device checks skipped)hostTORCHLEAN_REQUIRE_CUDA=1TORCHLEAN_LIBTORCH_DEVICE=cuda TORCHLEAN_REQUIRE_NATIVE=1the LibTorch bridge selected no device: TORCHLEAN_LIBTORCH_DEVICE=cuda, but the SDK is CPU-only-Kcuda=false)TORCHLEAN_REQUIRE_NATIVE=1CUDA was requested, but this executable was built without LibTorch; rebuild and run with `-K cuda=true`libtorch_abi.py --torch --library: OK for both libraries ("a CUDA-enabled build", "a CPU-only build"), and the CPU-only library checked against the cu130 SDK is rejected.repo_lint.py: OK.Not run: the elementwise C++ harness (its CMake only gains
TORCHLEAN_LIBTORCH_CUDA=1), the Compute Sanitizer tools, and the blueprint build.