Skip to content

fix(exla): solve f16 triangular systems in f32 on CUDA - #1890

Merged
polvalente merged 1 commit into
elixir-nx:mainfrom
heydtn:exla-f16-triangular-solve-cuda
Oct 20, 2026
Merged

polvalente merged 1 commit into
elixir-nx:mainfrom
heydtn:exla-f16-triangular-solve-cuda

Conversation

@heydtn

@heydtn heydtn commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

On CUDA, Nx.LinAlg.triangular_solve/3 raises for f16 inputs:

Nx.default_backend({EXLA.Backend, client: :cuda})

a = Nx.tensor([[2, 0], [1, 4]], type: :f16)
Nx.LinAlg.triangular_solve(a, Nx.tensor([2, 9], type: :f16))
** (RuntimeError) Invalid type for triangular solve 10

XLA on CUDA rewrites triangular_solve into a cuBLAS trsm custom call, and that call only supports f32, f64, c64 and c128. For bf16, XLA converts the operands to f32 around the call and converts the result back, but it passes f16 through unchanged, so the call fails at runtime. On the CPU, XLA expands the solve into regular ops, so f16 works there.

This PR does for f16 on CUDA what XLA already does for bf16: it solves the system in f32 and converts the result to f16. Other types and platforms are unchanged.

The new test in nx_linalg_doctest_test.exs covers every left_side/lower/transform_a combination on a batch of f16 matrices and compares against the f32 result. It fails on CUDA without this change and passes on both CUDA and host with it.

Tested on an RTX 5070 Ti (CUDA 12.9, cuDNN 9.10, XLA_TARGET=cuda12)

XLA on CUDA lowers triangular_solve to cuBLAS trsm, which has no f16
routine, so f16 triangular_solve raised "Invalid type for triangular
solve". On CUDA, f16 systems are now solved in f32 and the result is
converted back to f16, which is what XLA already does for bf16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApZrZkd5sFVCfLWjPxS8o1
@heydtn
heydtn marked this pull request as ready for review October 20, 2026 22:02
@polvalente
polvalente merged commit 8f216db into elixir-nx:main Oct 20, 2026
9 checks passed
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.

2 participants