Repository navigation
Implement dot for wrapped GPU arrays, and fix other gaps the BLAS hooks left - #888
Merged
Merged
Conversation
This was referenced Dec 2, 2026
kshyatt
reviewed
Dec 3, 2026
| @testset "dot of wrapped arrays" begin | ||
| T = Float32 | ||
| T in eltypes || return | ||
| @testset "dot of wrapped arrays, $T" for T in (Float32, ComplexF32, Int64) |
Member
Author
There was a problem hiding this comment.
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.
Member
Author
There was a problem hiding this comment.
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.
kshyatt
approved these changes
Dec 3, 2026
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
force-pushed
the
tb/dot-wrappers
branch
from
December 3, 2026 05:47
80eb005 to
31f928b
Compare
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.
GPUArrays 12.3 implements
dotfor plain GPU arrays, views that aren't fast-linear, andPermutedDimsArrays. 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 anIntvector, and so on. CUDA.jl had a catch-alldot(::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 forSymmetricandHermitianmatrices to cover part of it.This PR adds
dot(::AnyGPUArray, ::AnyGPUArray), plus methods for the LinearAlgebra methods it would be ambiguous with:LinearAlgebra.StridedVecLike) is intersected withAnyGPUArray; these keep reaching theblas_dothook;conj(dot(parent(x), parent(y)));Hermitianmatrices, whose dot product is real, also in type on Julia 1.11 and later, as with LinearAlgebra.Pairs of transposes and products with a
Diagonalkeep 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:
dotandtranspose(x) * yno 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 accumulatesFloat16products in single precision.A' + B'andA' - B'useblas_geam!, like the sums with a single transposed operand. On CUDA.jl with #3626, that takes 295 µs instead of 646 µs for 4096×4096Float32matrices on an RTX 5080.axpy!andaxpby!pair the elements of operands of the same length but different shapes, as LinearAlgebra does, instead of broadcasting them (DimensionMismatch).rdiv!(x, D)andx / Daccept vectors, which CUDA.jl 6.5 did.I checked
dotof every pair of 23 kinds of operands (plain arrays, views, reshapes, reinterpretations, adjoints, transposes, triangular, symmetric, Hermitian, diagonal and permuted matrices) forFloat32,ComplexF32,Float16,Int64andBoolagainst 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.