Skip to content

Run the LibTorch bridge on the host when the SDK is CPU-only (#36, reimplemented) - #43

Open
NicolasRouquette wants to merge 5 commits into
lean-dojo:mainfrom
NicolasRouquette:libtorch-host-device-v2
Open

NicolasRouquette wants to merge 5 commits into
lean-dojo:mainfrom
NicolasRouquette:libtorch-host-device-v2

Conversation

@NicolasRouquette

@NicolasRouquette NicolasRouquette commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

This reimplements #36 on the current bridge. #36 was closed because main replaced the APIs it
targeted; this is new code against the Stable ABI bridge, based on f9dff853. It keeps the
two points settled in #36's review:

  • TORCHLEAN_REQUIRE_CUDA=1 requires a CUDA device (Buffer.requireCudaDevice), and
  • TORCHLEAN_REQUIRE_NATIVE=1 requires 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=cpu selects the host and =cuda a 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 =cuda with a CPU-only SDK, is nativeUnavailable too, and the error names the reason.
  • .nativeAvailable means "a device is selected", so requireNativeRuntime and existing matches are unchanged.
    LibTorch.DeviceKind/deviceKind report which device, and Buffer.cudaAvailable is 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:

  • axpy and 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 rounded fma and fold in the kernels' order. TorchLeanTest/CUDA/Numerics.lean's single-rounding and in-order fixtures pass on the host.
  • Tree folds and unfolds launch generated row kernels. Hylo.run and Unfold.run already fall back to their host references (results_eq), so they now take that path unless a CUDA device is selected.
  • Custom operations, encoded uploads, setDevice, setMemoryFraction call require_cuda("<feature>") first and fail through IO on the host. The encoded upload refuses because the pure torchlean_kernel_run_encoded would otherwise be reached.
  • The ABI check. A CPU-only library imports fewer symbols, so TRUST gains CUDA C Shims (the CUDA guard, current stream and aoti_torch_is_contiguous) and CUDA Unstable Remainder (the caching allocator). libtorch_abi.py expects them exactly when the library links libtorch_cuda.so. With --torch, it also checks that the SDK agrees.

Build: an SDK without torch_cuda compiles with TORCHLEAN_LIBTORCH_CUDA=0, without USE_CUDA, the CUDA toolkit, NVRTC or the driver.

On the host, allocator statistics and driver memory read zero, and synchronize and emptyCache return at once.

One ATen CPU difference this needed

ATen's CPU log1p of torch 2.13 returns 0 at the least subnormal, 2^-149 in binary32 and 2^-1074 in binary64, where the correctly rounded result is the argument itself (checked with pip torch 2.13.0+cpu; 1e-42 and 1e-310 come back unchanged). That loses the combined tail of the two-level cross-entropy normalizer, and checkCrossEntropyUnderflow failed on the host. The bridge's log1p now returns x on the host wherever |x| < 2^-p, where x is the correctly rounded value, since the correction -x²/2 is below half an ulp. CUDA's log1p is 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); and tree_tests skips 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.

build device env result
cu130 SDK cuda TORCHLEAN_REQUIRE_CUDA=1 all curated tests passed; tree_tests passed
same binary host TORCHLEAN_LIBTORCH_DEVICE=cpu TORCHLEAN_REQUIRE_NATIVE=1 all curated tests passed; tree_tests passed (device checks skipped)
same binary, strict host TORCHLEAN_LIBTORCH_DEVICE=cpu TORCHLEAN_REQUIRE_CUDA=1 fails: a CUDA device was required, but the LibTorch bridge runs on the host: the SDK is CPU-only, or TORCHLEAN_LIBTORCH_DEVICE=cpu selected the host
same binary — TORCHLEAN_LIBTORCH_DEVICE=gpu TORCHLEAN_REQUIRE_NATIVE=1 fails: the LibTorch bridge selected no device: TORCHLEAN_LIBTORCH_DEVICE="gpu" is neither "cuda" nor "cpu"
CPU-only SDK (ldd: libtorch_cpu, libc10, no CUDA library) host TORCHLEAN_REQUIRE_NATIVE=1 all curated tests passed; tree_tests passed (device checks skipped)
same binary, strict host TORCHLEAN_REQUIRE_CUDA=1 fails, same message as above
same binary — TORCHLEAN_LIBTORCH_DEVICE=cuda TORCHLEAN_REQUIRE_NATIVE=1 fails: the LibTorch bridge selected no device: TORCHLEAN_LIBTORCH_DEVICE=cuda, but the SDK is CPU-only
default build (-Kcuda=false) — — all curated tests passed
default build — TORCHLEAN_REQUIRE_NATIVE=1 fails: CUDA 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.

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.

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