Skip to content

Use GPUArrays' BLAS hooks for LinearAlgebra operations - #3626

Open
kshyatt wants to merge 2 commits into
mainfrom
ksh/blas-hooks
Open

kshyatt wants to merge 2 commits into
mainfrom
ksh/blas-hooks

Conversation

@kshyatt

@kshyatt kshyatt commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

cuBLAS kept its own copy of the logic that maps LinearAlgebra's products, triangular operations and BLAS 1 functions to BLAS routines: dimension checks, unwrapping adjoints and triangular wrappers into BLAS characters, layout checks for views, and strong zeros. AMDGPU.jl and oneAPI.jl have their own copies, and they had drifted apart, e.g. axpby!(2, x, false, y) and rmul!(view(x, 1:2:n), false) propagated NaNs from y and x here. GPUArrays 12.3 (JuliaGPU/GPUArrays.jl#852) implements that logic once and calls GPUArrays.blas_* hooks for the BLAS routines.

This PR deletes our copy and implements the hooks with cuBLAS: gemm (including the mixed-precision products that gemmEx supports), gemv, symm/symv, hemm/hemv, trmm/trmv, trsm/trsv, syrk, herk, geam, dgmm, and the BLAS 1 routines. Our custom dot kernel for element types cuBLAS doesn't support becomes a method of the blas_dot hook. It requires GPUArrays 12.4 (JuliaGPU/GPUArrays.jl#888), which implements dot for wrapped GPU arrays (dot(UpperTriangular(A), B), dot(Symmetric(A), B), ...), as our catch-all dot(::AnyCuArray, ::AnyCuArray) did.

Behavior changes:

  • false is a strong zero in mul!, rmul!, axpby! and rotate!, and dot(A', B') works (it was a MethodError).
  • A' * A uses syrk or herk.
  • Triangular products and solves with views that cuBLAS can't address (that skip rows, or reversed vectors) use GPUArrays' kernels instead of copying the operands first. That is slower for large matrices.

On an RTX 5080 (minimum of repeated runs):

main this PR
using CUDA 1.82 s 1.55 s
A' * A, 2048² Float32 605 µs 360 µs
UpperTriangular(view(A, 1:2:2n, :)) \ B, n = 2048 1.04 ms 1.66 ms
UpperTriangular(view(A, 1:2:2n, :)) * B, n = 2048 0.68 ms 1.83 ms

gemm, gemv, dot, axpy!, norm, transpose!, A ± B', A' ± B' and Diagonal products are unchanged.

The cuBLAS tests gain cases for triangular operations on views, strong zeros, and dot of strided views; the backend-agnostic dot cases are in GPUArrays' test suite.

@kshyatt kshyatt added arrays The array abstraction: CuArray, broadcasting, mapreduce, indexing, views, generic linear algebra. libraries Vendor library wrappers (cuBLAS, cuSPARSE, cuFFT, cuDNN, ...). labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with 3f940e353, the merge base.
The environment differs from the baseline's: AcceleratedKernels 0.5.14 → 0.5.15, CUDACoreExt 0.5.14 → 0.5.15, GPUArrays 12.3.0 → 12.4.0, GPUCompiler 2.25.0 → 2.26.0, LLVMDowngrader_jll 0.12.2+1 → 0.13.0+0, SPIRVIntrinsics 1.6.1 → 1.6.2, SPIRV_LLVM_Backend_jll 23.1.4+6 → 23.1.4+7, StaticArrays 1.9.25 → 1.9.26, StaticArraysStatisticsExt 1.9.25 → 1.9.26.

Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance +0.8% 0 0 79
Latency −10.0% 0 3 3
Applications +0.1% 0 0 6
Benchmark Baseline This PR Change
Latency
import 3.90 s 3.46 s −11.4% 🟢 faster
ttfp 4.28 s 3.81 s −11.0% 🟢 faster
precompile 6.34 s 5.86 s −7.6% 🟢 faster
All benchmarks
Benchmark Baseline This PR Change
Performance
array/accumulate/Float32/1d 43.7 µs 43.5 µs −0.4%
array/accumulate/Float32/dims=1 30.2 µs 30.3 µs +0.2%
array/accumulate/Float32/dims=1L 96.2 µs 96.7 µs +0.5%
array/accumulate/Float32/dims=2 48.2 µs 48.6 µs +0.7%
array/accumulate/Float32/dims=2L 369 µs 365 µs −1.1%
array/accumulate/Int64/1d 67.2 µs 68.1 µs +1.4%
array/accumulate/Int64/dims=1 40.6 µs 41.2 µs +1.4%
array/accumulate/Int64/dims=1L 140 µs 140 µs +0.1%
array/accumulate/Int64/dims=2 70.6 µs 71.2 µs +0.9%
array/accumulate/Int64/dims=2L 770 µs 762 µs −1.0%
array/broadcast 10.8 µs 10.8 µs −0.1%
array/broadcast launch 5.81 µs 5.81 µs +0.0%
array/construct 1.32 µs 1.37 µs +4.3%
array/copy 17.6 µs 17.4 µs −1.0%
array/copyto!/cpu_to_gpu 206 µs 207 µs +0.4%
array/copyto!/gpu_to_cpu 239 µs 241 µs +0.7%
array/copyto!/gpu_to_gpu 12.0 µs 11.9 µs −1.0%
array/iteration/findall/bool 47.4 µs 48.4 µs +2.2%
array/iteration/findall/int 59.1 µs 56.5 µs −4.4%
array/iteration/findfirst/bool 32.1 µs 31.9 µs −0.8%
array/iteration/findfirst/int 33.2 µs 33.4 µs +0.5%
array/iteration/findmin/1d 63.5 µs 62.2 µs −2.0%
array/iteration/findmin/2d 39.3 µs 40.5 µs +2.9%
array/iteration/logical 47.0 µs 48.1 µs +2.4%
array/iteration/scalar 57.0 µs 57.7 µs +1.3%
array/permutedims/2d 55.0 µs 55.4 µs +0.8%
array/permutedims/3d 51.4 µs 51.4 µs +0.2%
array/permutedims/4d 43.0 µs 43.1 µs +0.2%
array/random/rand!/Float32 9.82 µs 9.85 µs +0.3%
array/random/rand!/Int64 16.2 µs 16.4 µs +1.4%
array/random/rand/Float32 11.7 µs 11.7 µs +0.4%
array/random/rand/Int64 20.8 µs 21.0 µs +1.3%
array/random/randn!/Float32 29.3 µs 29.3 µs +0.1%
array/random/randn/Float32 39.0 µs 39.1 µs +0.5%
array/reductions/mapreduce/Float32/1d 30.1 µs 29.9 µs −0.7%
array/reductions/mapreduce/Float32/dims=1 17.5 µs 17.6 µs +0.7%
array/reductions/mapreduce/Float32/dims=1L 54.0 µs 54.2 µs +0.3%
array/reductions/mapreduce/Float32/dims=2 24.3 µs 24.6 µs +1.3%
array/reductions/mapreduce/Float32/dims=2L 60.8 µs 61.2 µs +0.6%
array/reductions/mapreduce/Int64/1d 31.1 µs 31.0 µs −0.3%
array/reductions/mapreduce/Int64/dims=1 19.7 µs 20.2 µs +2.6%
array/reductions/mapreduce/Int64/dims=1L 92.5 µs 92.7 µs +0.3%
array/reductions/mapreduce/Int64/dims=2 26.9 µs 26.8 µs −0.1%
array/reductions/mapreduce/Int64/dims=2L 96.0 µs 96.8 µs +0.8%
array/reductions/reduce/Float32/1d 29.5 µs 30.3 µs +2.7%
array/reductions/reduce/Float32/dims=1 17.6 µs 17.8 µs +0.7%
array/reductions/reduce/Float32/dims=1L 54.0 µs 54.4 µs +0.8%
array/reductions/reduce/Float32/dims=2 24.3 µs 24.2 µs −0.5%
array/reductions/reduce/Float32/dims=2L 60.9 µs 61.0 µs +0.1%
array/reductions/reduce/Int64/1d 30.6 µs 31.2 µs +2.1%
array/reductions/reduce/Int64/dims=1 19.8 µs 20.0 µs +0.8%
array/reductions/reduce/Int64/dims=1L 92.8 µs 92.7 µs −0.1%
array/reductions/reduce/Int64/dims=2 26.6 µs 27.0 µs +1.4%
array/reductions/reduce/Int64/dims=2L 96.0 µs 95.7 µs −0.3%
array/reverse/1d 19.1 µs 19.4 µs +1.4%
array/reverse/1d_inplace 12.0 µs 12.0 µs +0.3%
array/reverse/1dL 73.0 µs 73.3 µs +0.4%
array/reverse/1dL_inplace 68.7 µs 68.7 µs −0.0%
array/reverse/2d 23.1 µs 23.2 µs +0.5%
array/reverse/2d_inplace 15.1 µs 15.2 µs +0.6%
array/reverse/2dL 92.5 µs 92.6 µs +0.1%
array/reverse/2dL_inplace 69.4 µs 69.5 µs +0.1%
array/sorting/1d 825 µs 825 µs +0.0%
array/sorting/2d 248 µs 247 µs −0.1%
array/sorting/by 966 µs 966 µs −0.1%
cuda/graph/capture 25.0 µs 25.1 µs +0.5%
cuda/graph/eager 24.9 µs 25.3 µs +1.8%
cuda/graph/launch 16.6 µs 16.8 µs +1.6%
cuda/synchronization/context/auto 1.88 µs 1.89 µs +0.5%
cuda/synchronization/context/blocking 661 ns 677 ns +2.5%
cuda/synchronization/context/nonblocking 5.51 µs 6.67 µs +21.1%
cuda/synchronization/stream/auto 602 ns 600 ns −0.3%
cuda/synchronization/stream/blocking 539 ns 548 ns +1.7%
cuda/synchronization/stream/nonblocking 6.46 µs 6.54 µs +1.3%
kernel/indexing 11.7 µs 11.8 µs +0.4%
kernel/indexing_checked 12.3 µs 12.4 µs +1.0%
kernel/launch 1.44 µs 1.47 µs +2.2%
kernel/occupancy 30.0 ns 30.0 ns +0.0%
kernel/rand 16.2 µs 16.3 µs +0.6%
Latency
import 3.90 s 3.46 s −11.4% 🟢 faster
precompile 6.34 s 5.86 s −7.6% 🟢 faster
ttfp 4.28 s 3.81 s −11.0% 🟢 faster
Applications
byval/reference 145 µs 145 µs +0.0%
byval/slices=1 146 µs 146 µs +0.1%
byval/slices=2 286 µs 286 µs +0.1%
byval/slices=3 426 µs 426 µs +0.1%
cudadevrt 104 µs 104 µs +0.0%
volumerhs 10.3 ms 10.3 ms −0.0%

History: https://juliagpu.org/perf/?pkg=cuda

Katharine Hyatt added 2 commits December 2, 2026 17:59
… operations

GPUArrays 12.3 implements the dispatch from LinearAlgebra's storage-level
`mul!` and triangular methods to BLAS routines, and calls hooks that
back-ends implement with their library. Replace our copy of that
dispatch with methods for those hooks. The gemm hook keeps using gemm!
for the mixed-precision products that gemmEx supports.

Triangular products and solves with operands that cuBLAS cannot address
(views that skip rows, reversed vectors) now use GPUArrays' kernels
instead of copying those operands first.
GPUArrays implements the BLAS 1 functions, `A' * A`, matrix additions
with transposed operands, transposition, and products with Diagonal
matrices, and calls hooks for the BLAS routines. Implement those with
cuBLAS and drop our own methods. Our dot kernel for element types cuBLAS
lacks becomes a method of the dot hook.

GPUArrays 12.4 also implements dot for wrapped arrays, which our
catch-all method covered so far.
@maleadt maleadt changed the title [WIP] Offload shared BLAS logic to GPUArrays Use GPUArrays' BLAS hooks for LinearAlgebra operations Dec 2, 2026
@maleadt

maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

I've reworked this so it can go in with GPUArrays 12.4 (JuliaGPU/GPUArrays.jl#888), and force-pushed; the previous head was e7a4f1f.

  • The dot methods for Symmetric, Hermitian and half-precision views are gone. GPUArrays 12.4 implements dot for every wrapped GPU array, which also covers the cases our old catch-all handled and this PR had lost: dot(UpperTriangular(A), B), dot(Symmetric(A), B), stepped views of Int vectors. It returns a real number for Hermitian matrices, as LinearAlgebra does on 1.11 and later.
  • GPUArrays 12.4 also sends A' ± B' to geam again (this PR had made it 2.2x slower), lets dot(x, x) use cuBLAS again, and accepts vectors in rdiv!(x, D). The geam hook here now takes the real coefficient that GPUArrays uses to subtract complex matrices.
  • The blas_dot fallback (our kernel for element types cuBLAS lacks) only accepts what GPUArrays passes it: strided operands of one eltype.
  • The tests from edge_cases.jl are now in linalg.jl; the backend-agnostic dot ones moved to GPUArrays' test suite.
  • The history is squashed into two commits, both still yours.

AMDGPU.jl#1229 and oneAPI.jl#779 need the same dot change. Not addressed here, and predating this PR: the gemm hook converts scalars that don't fit the output eltype (e.g. a Float64 α of 1e40 for a Float32 product), and doesn't check the alignment that integer gemmEx requires.

@maleadt

maleadt commented Dec 3, 2026

Copy link
Copy Markdown
Member

GPUArrays 12.4 is registered, which was the cause of the earlier CI failures. With it, the Julia 1.10 and 1.13 jobs pass; the CUDA version matrix is still running.

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

arrays The array abstraction: CuArray, broadcasting, mapreduce, indexing, views, generic linear algebra. libraries Vendor library wrappers (cuBLAS, cuSPARSE, cuFFT, cuDNN, ...).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants