diff --git a/NEWS.md b/NEWS.md index a3e0d0c7..ec7e623e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,19 @@ newest first, aimed at the authors of back-end packages. For the complete list o pull requests, see the [GitHub releases](https://github.com/JuliaGPU/GPUArrays.jl/releases). +## v12.4 + +`dot` works for every pair of `AnyGPUArray`s, i.e. GPU arrays and their wrappers (triangular, +`Symmetric`, `Hermitian`, `Diagonal`, adjoints, views, reshapes, ...), including mixed pairs, +instead of falling back to LinearAlgebra's element-by-element loop. Back-ends can delete their +own `dot` methods for wrapped arrays, such as those for `Symmetric` and `Hermitian` matrices. +Their methods for strided arrays still take precedence in some cases and should become +`GPUArrays.blas_dot` methods. + +`GPUArrays.blas_dot` and `blas_dotu` are also called with operands that alias, e.g. for +`dot(x, x)`. `A' + B'` and `A' - B'` use `GPUArrays.blas_geam!` too, and `axpy!` and `axpby!` +accept operands of the same length but different shapes. + ## v12.3 GPUArrays now implements the LinearAlgebra methods that back-ends used to define in front of diff --git a/Project.toml b/Project.toml index d59dc3e7..3201d90f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "GPUArrays" uuid = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7" -version = "12.3.1" +version = "12.4.0" [workspace] projects = ["lib/GPUArraysCore", "lib/JLArrays", "test", "docs"] diff --git a/src/host/blas.jl b/src/host/blas.jl index a6a89d90..5e03e1d6 100644 --- a/src/host/blas.jl +++ b/src/host/blas.jl @@ -17,9 +17,11 @@ blas_matrix_layout(A::AbstractVecOrMat) = stride(A, 1) == 1 && stride(A, 2) >= s blas_vector_layout(x::AbstractVector) = stride(x, 1) > 0 blas_vector_layout(x::AbstractArray) = strides(x) == Base.size_to_strides(1, size(x)...) blas_vector_operand(x) = x isa AnyStridedGPUArray && !isempty(x) && blas_vector_layout(x) -blas_vector_operands(x, y) = +blas_vector_inputs(x, y) = eltype(x) == eltype(y) && size(x) == size(y) && blas_vector_operand(x) && - blas_vector_operand(y) && !Base.mightalias(x, y) + blas_vector_operand(y) +# for routines that write to one of the operands +blas_vector_operands(x, y) = blas_vector_inputs(x, y) && !Base.mightalias(x, y) # like LinearAlgebra, promote scalars to the output eltype, unless they are of a wider type blas_scalar(::Type, x::Bool) = x @@ -217,7 +219,8 @@ blas_scal!(α, x) = generic_rmul!(x, α) GPUArrays.blas_dotu(x, y) The dot product of `x` and `y`, conjugating `x` (`dot`, which vendor libraries call `dotc` for -complex numbers) or not (`dotu`). +complex numbers) or not (`dotu`). Unlike the operands of the other BLAS 1 hooks, `x` and `y` +can alias, e.g. for `dot(x, x)`. """ blas_dot(x, y) = generic_dot(x, y) diff --git a/src/host/linalg.jl b/src/host/linalg.jl index 324a1962..2c6f076d 100644 --- a/src/host/linalg.jl +++ b/src/host/linalg.jl @@ -612,11 +612,11 @@ function gpu_rdiv!(B, A, D::Diagonal) n = size(A, 2) (k = length(D.diag)) == n || throw(DimensionMismatch(lazy"left hand side has $n columns but D is $k by $k")) any(iszero, D.diag) && throw(SingularException(findfirst(iszero, D.diag))) - B .= A ./ transpose(D.diag) + B .= A ./ (A isa AbstractVector ? D.diag : transpose(D.diag)) end # less specific than the back-ends' methods for their arrays and any Diagonal -LinearAlgebra.rdiv!(A::AnyStridedGPUMatrix, D::Diagonal) = gpu_rdiv!(A, A, D) -Base.:(/)(A::Union{AnyStridedGPUMatrix,AdjOrTrans{<:Any,<:AnyStridedGPUMatrix}}, D::Diagonal) = +LinearAlgebra.rdiv!(A::AnyStridedGPUVecOrMat, D::Diagonal) = gpu_rdiv!(A, A, D) +Base.:(/)(A::Union{AnyStridedGPUVecOrMat,AdjOrTrans{<:Any,<:AnyStridedGPUMatrix}}, D::Diagonal) = gpu_rdiv!(similar(A, typeof(oneunit(eltype(A)) / oneunit(eltype(D)))), A, D) Base.Array(D::Diagonal{<:Any,<:AbstractGPUVector}) = Matrix(Diagonal(Array(D.diag))) @@ -1422,7 +1422,7 @@ function gpu_dot(x, y) length(x) == length(y) || throw(DimensionMismatch(lazy"dot product arguments have lengths $(length(x)) and $(length(y))")) isempty(x) && return dot(zero(eltype(x)), zero(eltype(y))) - blas_vector_operands(x, y) ? blas_dot(x, y) : generic_dot(x, y) + blas_vector_inputs(x, y) ? blas_dot(x, y) : generic_dot(x, y) end generic_dot(x, y) = mapreduce(dot, +, x, y) @@ -1430,15 +1430,52 @@ function gpu_dotu(x, y) length(x) == length(y) || throw(DimensionMismatch(lazy"dot product arguments have lengths $(length(x)) and $(length(y))")) isempty(x) && return zero(eltype(x)) * zero(eltype(y)) - blas_vector_operands(x, y) ? blas_dotu(x, y) : generic_dotu(x, y) + blas_vector_inputs(x, y) ? blas_dotu(x, y) : generic_dotu(x, y) end generic_dotu(x, y) = mapreduce(*, +, x, y) +# LinearAlgebra's fallback, which most wrappers of GPU arrays end up in, indexes them element +# by element +LinearAlgebra.dot(x::AnyGPUArray, y::AnyGPUArray) = wrapped_dot(x, y) +wrapped_dot(x, y) = gpu_dot(x, y) +# keep LinearAlgebra's algorithms for structured matrices +wrapped_dot(x::Transpose, y::Transpose) = dot(parent(x), parent(y)) +wrapped_dot(x::Diagonal, y::Diagonal) = dot(x.diag, y.diag) +function wrapped_dot(x::Diagonal, y::AbstractMatrix) + size(x) == size(y) || + throw(DimensionMismatch(lazy"Diagonal has size $(size(x)) but matrix has size $(size(y))")) + isempty(x) && return dot(zero(eltype(x)), zero(eltype(y))) + mapreduce(identity, +, Broadcast.broadcasted((d, i, y) -> dot(d, y[i, i]), x.diag, axes(y, 1), Ref(y))) +end +wrapped_dot(x::AbstractMatrix, y::Diagonal) = conj(wrapped_dot(y, x)) + +# these take precedence over released back-ends' own methods for some strided arrays, which +# don't handle all of them (e.g. AMDGPU 2.13 and two-dimensional views) LinearAlgebra.dot(x::AbstractGPUArray, y::AbstractGPUArray) = gpu_dot(x, y) LinearAlgebra.dot(x::SubArray{<:Any,<:Any,<:AbstractGPUArray,<:Any,false}, y::SubArray{<:Any,<:Any,<:AbstractGPUArray,<:Any,false}) = gpu_dot(x, y) LinearAlgebra.dot(x::PermutedDimsArray{<:Any,<:Any,<:Any,<:Any,<:AbstractGPUArray}, y::PermutedDimsArray{<:Any,<:Any,<:Any,<:Any,<:AbstractGPUArray}) = gpu_dot(x, y) + +# LinearAlgebra methods that are ambiguous with the `AnyGPUArray` one: for strided arrays of +# BLAS eltypes, which it would pass to the host BLAS library, ... +for T in (Float32, Float64, ComplexF32, ComplexF64) + S = typeintersect(LinearAlgebra.StridedVecLike{T}, AnyGPUArray{T}) + @eval LinearAlgebra.dot(x::$S, y::$S) = gpu_dot(x, y) +end +# ... for adjoints, ... +LinearAlgebra.dot(x::Adjoint{<:Union{Real,Complex},<:AnyGPUArray}, y::Adjoint{<:Union{Real,Complex},<:AnyGPUArray}) = + conj(dot(parent(x), parent(y))) +# ... and for Hermitian matrices, whose dot product is real (and, with Julia 1.11 and later, +# of a real type) +function LinearAlgebra.dot(x::Hermitian{<:Union{Real,Complex},<:AnyGPUArray}, y::Hermitian{<:Union{Real,Complex},<:AnyGPUArray}) + size(x) == size(y) || + throw(DimensionMismatch(lazy"A has dimensions $(size(x)) but B has dimensions $(size(y))")) + T = typeof(dot(zero(eltype(x)), zero(eltype(y)))) + r = isempty(x) ? zero(real(T)) : mapreduce((a, b) -> real(dot(a, b)), +, x, y) + @static VERSION >= v"1.11" ? r : (T <: Complex ? T(r) : r) +end + # LinearAlgebra sends `dot` of other strided arrays with BLAS eltypes, e.g. views, to its BLAS # wrappers, which would take host pointers. Back-ends' own `dot` methods still take precedence. for (T, f, g) in ((:Float32, :dot, :gpu_dot), (:Float64, :dot, :gpu_dot), @@ -1465,7 +1502,8 @@ function LinearAlgebra.axpby!(α::Number, x::AnyStridedGPUArray, β::Number, y:: if α === false β === false ? fill!(y, zero(eltype(y))) : β === true || gpu_scal!(β, y) elseif β === false - y .= x .* α + x′, y′ = blas1_pair(x, y) + y′ .= x′ .* α elseif β === true gpu_axpy!(α, x, y) else @@ -1479,7 +1517,13 @@ function LinearAlgebra.axpby!(α::Number, x::AnyStridedGPUArray, β::Number, y:: end return y end -generic_axpby!(α, x, β, y) = y .= x .* α .+ y .* β +function generic_axpby!(α, x, β, y) + x′, y′ = blas1_pair(x, y) + y′ .= x′ .* α .+ y′ .* β + return y +end +# like BLAS, pair the elements of operands that only have the same length +blas1_pair(x, y) = axes(x) == axes(y) ? (x, y) : (vec(x), vec(y)) function LinearAlgebra.axpy!(α::Number, x::AnyStridedGPUArray, y::AnyStridedGPUArray) length(x) == length(y) || @@ -1491,7 +1535,11 @@ function gpu_axpy!(α, x, y) α′ = blas1_scalar(eltype(y), α) α′ !== nothing && blas_vector_operands(x, y) ? blas_axpy!(α′, x, y) : generic_axpy!(α, x, y) end -generic_axpy!(α, x, y) = y .+= x .* α +function generic_axpy!(α, x, y) + x′, y′ = blas1_pair(x, y) + y′ .+= x′ .* α + return y +end ## matrix addition @@ -1514,6 +1562,7 @@ for op in (:+, :-) @eval begin Base.$op(A::AnyStridedGPUMatrix, B::AdjOrTrans{<:Any,<:AnyStridedGPUMatrix}) = gpu_geam($op, A, B) Base.$op(A::AdjOrTrans{<:Any,<:AnyStridedGPUMatrix}, B::AnyStridedGPUMatrix) = gpu_geam($op, A, B) + Base.$op(A::AdjOrTrans{<:Any,<:AnyStridedGPUMatrix}, B::AdjOrTrans{<:Any,<:AnyStridedGPUMatrix}) = gpu_geam($op, A, B) end end diff --git a/test/runtests.jl b/test/runtests.jl index b43820e8..89911d45 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -64,6 +64,39 @@ if isdefined(Test, :detect_closure_boxes) end end +# BLAS 1 calls that released back-ends' own methods take and get wrong; these move to the test +# suite once back-ends rely on GPUArrays' methods +testsuite["JLArray/blas1"] = quote + using LinearAlgebra + import ..TestSuite: compare + @testset "dot, $T" for T in (Float32, ComplexF32, Float16, Int64) + @test_throws DimensionMismatch dot(Diagonal(JLArray(rand(T, 4))), JLArray(rand(T, 16, 1))) + A, B = rand(T, 4, 6), rand(T, 4, 6) + @test compare((A, B) -> dot(A', B'), JLArray, A, B) + x, y = rand(T, 12), rand(T, 12) + @test compare((x, y) -> dot(view(x, 1:2:12), view(y, 1:2:12)), JLArray, x, y) + @test compare((A, x) -> dot(A, x[1:12]), JLArray, A[:, 1:3], x) + @test compare(x -> (r = Base.ReshapedArray(x, (3, 4), ()); dot(r, r)), JLArray, x) + + S, R = rand(T, 4, 4), rand(T, 4, 4) + @test compare((S, R) -> dot(Hermitian(S), Hermitian(R, :L)), JLArray, S, R) + # the dot product of Hermitian matrices is real, also in type with Julia 1.11 and later + H(S) = Hermitian(JLArray(S)) + @test typeof(dot(H(S), H(R))) == typeof(dot(Hermitian(S), Hermitian(R))) + @test dot(H(S[1:0, 1:0]), H(R[1:0, 1:0])) === zero(typeof(dot(Hermitian(S), Hermitian(R)))) + end + @test compare(B -> dot(Hermitian(B), Hermitian(B)), JLArray, rand(Bool, 4, 4)) + @test compare((x, d) -> rdiv!(x, Diagonal(d)), JLArray, rand(Float32, 3), rand(Float32, 1) .+ 1) + @test compare((x, d) -> x / Diagonal(d), JLArray, rand(Float32, 3), rand(Float32, 1) .+ 1) + @testset "operands of different shapes, $T" for T in (Float32, ComplexF32) + A, x = rand(T, 4, 3), rand(T, 6) + @test compare((A, x) -> axpy!(T(2), view(A, 1:2, :), x), JLArray, A, x) + @test compare((x, A) -> axpy!(T(2), x, view(A, 1:2, :)), JLArray, x, A) + @test compare((A, x) -> axpby!(T(2), view(A, 1:2, :), T(3), x), JLArray, A, x) + @test compare((A, x) -> axpby!(T(2), view(A, 1:2, :), false, x), JLArray, A, x) + end +end + # a type without a COO counterpart must hit a MethodError, not recurse (#780) testsuite["sparse/coo_type"] = quote import GPUArrays diff --git a/test/testsuite/linalg.jl b/test/testsuite/linalg.jl index d0cc97a4..89a47f52 100644 --- a/test/testsuite/linalg.jl +++ b/test/testsuite/linalg.jl @@ -1241,12 +1241,36 @@ end @test compare((A, B) -> dot(v(A), v(B)), AT, A, B) end - @testset "dot of wrapped arrays" begin - T = Float32 - T in eltypes || return + @testset "dot of wrapped arrays, $T" for T in (Float32, ComplexF32, Int64) + T in eltypes || continue A, B = rand(T, 4, 6), rand(T, 4, 6) @test compare((A, B) -> dot(PermutedDimsArray(A, (2, 1)), PermutedDimsArray(B, (2, 1))), AT, A, B) + @test compare((A, B) -> dot(PermutedDimsArray(A, (2, 1)), copy(B')), AT, A, B) @test compare((A, B) -> dot(view(A, 1:2:3, :), view(B, 1:2:3, :)), AT, A, B) + @test compare((A, B) -> dot(transpose(A), transpose(B)), AT, A, B) + @test compare((A, B) -> dot(A', copy(B')), AT, A, B) + + S, R = rand(T, 4, 4), rand(T, 4, 4) + @test compare((S, R) -> dot(UpperTriangular(S), UpperTriangular(R)), AT, S, R) + @test compare((S, R) -> dot(UnitLowerTriangular(S), R), AT, S, R) + @test compare((S, R) -> dot(S, LowerTriangular(R)), AT, S, R) + @test compare((S, R) -> dot(UpperTriangular(S), LowerTriangular(R)), AT, S, R) + @test compare((S, R) -> dot(Symmetric(S), Symmetric(R, :L)), AT, S, R) + @test compare((S, R) -> dot(Symmetric(S), R), AT, S, R) + + d, e = rand(T, 4), rand(T, 4) + @test compare((d, e) -> dot(Diagonal(d), Diagonal(e)), AT, d, e) + @test compare((d, S) -> dot(Diagonal(d), S), AT, d, S) + @test compare((d, S) -> dot(S, Diagonal(d)), AT, d, S) + @test compare((d, S) -> dot(Diagonal(d), UpperTriangular(S)), AT, d, S) + @test compare((d, S) -> dot(Symmetric(S), Diagonal(d)), AT, d, S) + + # stepped views of element types LinearAlgebra doesn't pass to BLAS + if !(T <: LinearAlgebra.BlasFloat) + x, y = rand(T, 12), rand(T, 12) + @test compare((x, y) -> dot(view(x, 1:2:12), view(y, 1:2:12)), AT, x, y) + @test compare((x, y) -> dot(view(x, 12:-2:1), y[1:6]), AT, x, y) + end end @testset "Givens rotations" begin @@ -1270,6 +1294,8 @@ end @test compare((S, R) -> S + R', AT, S, R) @test compare((S, R) -> S - transpose(R), AT, S, R) @test compare((S, R) -> R' - S, AT, S, R) + @test compare((S, R) -> S' + R', AT, S, R) + @test compare((S, R) -> transpose(S) - R', AT, S, R) @test compare(A -> transpose!(similar(A, 3, 5), A), AT, A) @test compare(A -> adjoint!(similar(A, 3, 5), A), AT, A) @test compare((S, R) -> S + R', AT, rand(T, 4, 0), rand(T, 0, 4))