Skip to content

Implement dot for wrapped GPU arrays, and fix other gaps the BLAS hooks left - #888

Merged
maleadt merged 6 commits into
mainfrom
tb/dot-wrappers
Dec 3, 2026
Merged

maleadt merged 6 commits into
mainfrom
tb/dot-wrappers

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

GPUArrays 12.3 implements dot for plain GPU arrays, views that aren't fast-linear, and PermutedDimsArrays. Other wrappers end up in LinearAlgebra's fallback, which indexes element by element: dot(UpperTriangular(A), B), dot(Symmetric(A), B), dot(A, B'), dot(view(x, 1:2:n), view(y, 1:2:n)) for an Int vector, and so on. CUDA.jl had a catch-all dot(::AnyCuArray, ::AnyCuArray) for this, so adopting the BLAS hooks (CUDA.jl#3626) regressed these calls, and AMDGPU.jl#1229 and oneAPI.jl#779 add methods for Symmetric and Hermitian matrices to cover part of it.

This PR adds dot(::AnyGPUArray, ::AnyGPUArray), plus methods for the LinearAlgebra methods it would be ambiguous with:

  • the host-BLAS method for strided arrays of BLAS element types, whose signature (LinearAlgebra.StridedVecLike) is intersected with AnyGPUArray; these keep reaching the blas_dot hook;
  • pairs of adjoints, which keep LinearAlgebra's conj(dot(parent(x), parent(y)));
  • pairs of Hermitian matrices, whose dot product is real, also in type on Julia 1.11 and later, as with LinearAlgebra.

Pairs of transposes and products with a Diagonal keep LinearAlgebra's algorithms, which work on the parents or only on the diagonal. An internal function selects those, rather than more public methods that would need their own tie-breakers. The existing methods stay, as they take precedence over released back-ends' own methods for strided arrays (AMDGPU 2.13's can't handle two-dimensional views).

Other fixes, which back-ends adopting the hooks ran into:

  • dot and transpose(x) * y no longer skip the hooks for operands that alias, which only matters for routines that write. With CUDA.jl 6.5, dot(x, x) used cuBLAS, which accumulates Float16 products in single precision.
  • A' + B' and A' - B' use blas_geam!, like the sums with a single transposed operand. On CUDA.jl with #3626, that takes 295 µs instead of 646 µs for 4096×4096 Float32 matrices on an RTX 5080.
  • axpy! and axpby! pair the elements of operands of the same length but different shapes, as LinearAlgebra does, instead of broadcasting them (DimensionMismatch).
  • rdiv!(x, D) and x / D accept vectors, which CUDA.jl 6.5 did.

I checked dot of every pair of 23 kinds of operands (plain arrays, views, reshapes, reinterpretations, adjoints, transposes, triangular, symmetric, Hermitian, diagonal and permuted matrices) for Float32, ComplexF32, Float16, Int64 and Bool against the CPU result: no failures and no ambiguities with JLArrays on Julia 1.10 and 1.13. With the released versions of CUDA.jl, AMDGPU.jl, oneAPI.jl, Metal.jl and OpenCL.jl, no pair that works with 12.3 fails with this PR, and most of those that failed (from a dozen to about 300 per element type, depending on the back-end) now work; the rest fail in back-ends' own methods, which their hook PRs delete. The test suite only gains cases that released back-ends pass; the others run on JLArrays until back-ends rely on these methods.

Comment thread test/testsuite/linalg.jl
@testset "dot of wrapped arrays" begin
T = Float32
T in eltypes || return
@testset "dot of wrapped arrays, $T" for T in (Float32, ComplexF32, Int64)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test Float16 too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Float16 is tested, but only in the JLArray-only block in test/runtests.jl. I kept it out of the shared suite because AMDGPU 2.13's own dot method takes Float16 strided views and fails, and the shared suite runs in every back-end's CI.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks! <3

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into the shared suite in #889, which stays a draft until AMDGPU.jl#1229 (and the CUDA.jl and oneAPI.jl hook PRs) are merged.

GPUArrays only defined dot for plain GPU arrays, views that aren't
fast-linear, and PermutedDimsArrays. Other wrappers, e.g. triangular
matrices, fast-linear views of element types without BLAS support, or
mixed pairs such as `dot(Symmetric(A), B)`, ended up in LinearAlgebra's
fallback, which indexes element by element. Back-ends worked around
that with their own catch-all or per-wrapper methods.

Add a method for any two `AnyGPUArray`s, and methods for the
LinearAlgebra methods it would be ambiguous with: the one for strided
arrays of BLAS element types, which keeps reaching the `blas_dot` hook,
and those for adjoints and Hermitian matrices. Products of transposes
and with Diagonal matrices keep LinearAlgebra's algorithms, which work
on the parent arrays or only on the diagonal.
`A' + B'` and `A' - B'` were broadcast over both transposes, while the
sums with one transposed operand already went to `blas_geam!`.
Like BLAS and LinearAlgebra, `axpy!` and `axpby!` accept operands of
the same length but different shapes, e.g. a matrix view and a vector.
The generic fallbacks broadcast them, which throws a DimensionMismatch
or broadcasts along the wrong dimensions.
`dot` and `transpose(x) * y` only read their operands, so they don't
need the aliasing check of the BLAS 1 routines that write to one of
them. With it, `dot(x, x)` used the generic kernel, and so computed a
half-precision self-product in half precision, while vendor libraries
accumulate it in single precision.
`rdiv!(x, D)` and `x / D` with a vector `x` (and a 1×1 Diagonal) used
LinearAlgebra's element-by-element loop.
@maleadt
maleadt merged commit 06b45ae into main Dec 3, 2026
2 of 3 checks passed
@maleadt
maleadt deleted the tb/dot-wrappers branch December 3, 2026 05:48
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