From a227a67e61876f60a02abc18e3e16f63596d7672 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 10 Nov 2026 13:32:10 +0100 Subject: [PATCH 01/11] Use GPUArrays' BLAS hooks for LinearAlgebra's products and triangular 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. Float16 and BFloat16 products still use gemm_ex!. The triangular-triangular product now zeroes a triangle on a copy of its operand, instead of on the caller's matrix. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- Project.toml | 2 +- src/blas/highlevel.jl | 264 +++++++++--------------------------------- 2 files changed, 58 insertions(+), 208 deletions(-) diff --git a/Project.toml b/Project.toml index bbbef1fc5..be0e00d7c 100644 --- a/Project.toml +++ b/Project.toml @@ -61,7 +61,7 @@ BFloat16s = "0.6.0" CEnum = "0.5, 1" ChainRulesCore = "1" EnzymeCore = "0.8" -GPUArrays = "12.1" +GPUArrays = "12.3" GPUCompiler = "2.23" GPUToolbox = "3.6" KernelAbstractions = "0.10" diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index 87a3a90d6..08530a209 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -1,10 +1,5 @@ rocblas_size(t::Char, M::ROCVecOrMat) = (size(M, t=='N' ? 1 : 2), size(M, t=='N' ? 2 : 1)) -@static if VERSION ≥ v"1.12.0-rc" - # we need to use the generic wrapper to avoid dispatch to the 2x2or3x3 method - using LinearAlgebra: generic_matmatmul_wrapper!, BlasFlag, _symm_hemm_generic! -end - # # BLAS 1 # @@ -109,169 +104,6 @@ LinearAlgebra.istriu(::ROCLowerOrUnitLowerTriangular) = false LinearAlgebra.istril(::ROCLowerOrUnitLowerTriangular) = true end -# multiplication -LinearAlgebra.generic_trimatmul!( - c::StridedROCVector{T}, uploc, isunitc, tfun::Function, - A::StridedROCMatrix{T}, b::StridedROCVector{T}, -) where T <: ROCBLASFloat = trmv!( - uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', - isunitc, A, c === b ? c : copyto!(c, b)) -# division -LinearAlgebra.generic_trimatdiv!( - C::StridedROCVector{T}, uploc, isunitc, tfun::Function, - A::StridedROCMatrix{T}, B::StridedROCVector{T}, -) where T <: ROCBLASFloat = trsv!( - uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', - isunitc, A, C === B ? C : copyto!(C, B)) - -# GEMV -function LinearAlgebra.mul!( - Y::ROCVector, tA::AbstractChar, A::StridedROCMatrix, B::StridedROCVector, - alpha::Number, beta::Number, -) - mA, nA = tA == 'N' ? size(A) : reverse(size(A)) - - nA != length(B) && throw(DimensionMismatch( - "second dimension of A, $nA, does not match length of B, $(length(B))")) - mA != length(Y) && throw(DimensionMismatch( - "first dimension of A, $mA, does not match length of Y, $(length(Y))")) - - mA == 0 && return Y - nA == 0 && return LinearAlgebra._rmul_or_fill!(Y, beta) - - T = eltype(Y) - if alpha isa Union{Bool,T} && beta isa Union{Bool,T} - α, β = T(alpha), T(beta) - if T <: ROCBLASFloat && eltype(A) == eltype(B) == T - if tA in ('N', 'T', 'C') - return gemv!(tA, α, A, B, β, Y) - elseif tA in ('S', 's') - return symv!(tA == 'S' ? 'U' : 'L', α, A, B, β, Y) - elseif tA in ('H', 'h') - return hemv!(tA == 'H' ? 'U' : 'L', α, A, B, β, Y) - end - end - end - LinearAlgebra.mul!(Y, tA, 'N', A, B, alpha, beta) -end - -# -# BLAS 3 -# - -@static if VERSION ≥ v"1.12.0-rc" - function LinearAlgebra.generic_matmatmul_wrapper!( - C::StridedROCMatrix{T}, tA::AbstractChar, tB::AbstractChar, A::StridedROCVecOrMat{T}, B::StridedROCVecOrMat{T}, - alpha::Number, beta::Number, val::LinearAlgebra.BlasFlag.SyrkHerkGemm) where {T<:ROCBLASFloatWithHalf} - LinearAlgebra.mul!(C, tA, tB, A, B, alpha, beta) - end - function LinearAlgebra._symm_hemm_generic!( - C::StridedROCMatrix{T}, tA::AbstractChar, tB::AbstractChar, A::StridedROCMatrix{T}, B::StridedROCMatrix{T}, - alpha, beta, ::Val{BlasFlag.SYMM}) where {T} - lrchar, ulchar = LinearAlgebra._lrchar_ulchar(tA, tB) - if lrchar == 'L' - symm!(lrchar, ulchar, alpha, A, B, beta, C) - else - symm!(lrchar, ulchar, alpha, B, A, beta, C) - end - end - function LinearAlgebra._symm_hemm_generic!( - C::StridedROCMatrix{T}, tA::AbstractChar, tB::AbstractChar, A::StridedROCMatrix{T}, B::StridedROCMatrix{T}, - alpha, beta, ::Val{BlasFlag.HEMM}) where {T} - lrchar, ulchar = LinearAlgebra._lrchar_ulchar(tA, tB) - if lrchar == 'L' - hemm!(lrchar, ulchar, alpha, A, B, beta, C) - else - hemm!(lrchar, ulchar, alpha, B, A, beta, C) - end - end -end - -function LinearAlgebra.mul!( - C::StridedROCVecOrMat, tA, tB, A::StridedROCVecOrMat, - B::StridedROCVecOrMat, alpha::Number, beta::Number, -) - mA, nA = size(A, tA == 'N' ? 1 : 2), size(A, tA == 'N' ? 2 : 1) - mB, nB = size(B, tB == 'N' ? 1 : 2), size(B, tB == 'N' ? 2 : 1) - - nA != mB && throw(DimensionMismatch( - "A has dimensions ($mA,$nA) but B has dimensions ($mB,$nB)")) - (C === A || B === C) && throw(ArgumentError( - "output matrix must not be aliased with input matrix")) - - if mA == 0 || nA == 0 || nB == 0 - size(C) != (mA, nB) && throw(DimensionMismatch( - "C has dimensions $(size(C)), should have ($mA,$nB)")) - return LinearAlgebra._rmul_or_fill!(C, beta) - end - - T = eltype(C) - if T <: ROCBLASFloat && alpha isa Union{Bool, T} && beta isa Union{Bool, T} - α, β = T(alpha), T(beta) - - if ( - all(in(('N', 'T', 'C')), (tA, tB)) && T <: ROCBLASFloat && - A isa StridedROCArray{T} && B isa StridedROCArray{T} - ) - return gemm!(tA, tB, α, A, B, β, C) - elseif (tA == 'S' || tA == 's') && tB == 'N' - return symm!('L', tA == 'S' ? 'U' : 'L', α, A, B, β, C) - elseif (tB == 'S' || tB == 's') && tA == 'N' - return symm!('R', tB == 'S' ? 'U' : 'L', α, B, A, β, C) - elseif (tA == 'H' || tA == 'h') && tB == 'N' - return hemm!('L', tA == 'H' ? 'U' : 'L', α, A, B, β, C) - elseif (tB == 'H' || tB == 'h') && tA == 'N' - return hemm!('R', tB == 'H' ? 'U' : 'L', α, B, A, β, C) - end - elseif ( - T in (Float16, BFloat16) && alpha isa Real && beta isa Real && - all(in(('N', 'T', 'C')), (tA, tB)) && - A isa StridedROCArray{T} && B isa StridedROCArray{T} - ) - # The non gemm_ex version is slower than the gemm_ex for 16 bit floats - return gemm_ex!(tA, tB, alpha, A, B, beta, C) - end - - GPUArrays.generic_matmatmul!(C, wrap(A, tA), wrap(B, tB), alpha, beta) -end - -# Julia < 1.13 dispatches on the non-public `generic_matvecmul!` and `generic_matmatmul!`, -# which JuliaLang/LinearAlgebra.jl#1671 superseded by the `mul!` methods above. Forward from -# the old names, both the alpha/beta variants (1.12) and the ones taking a final MulAddMul -# (1.10 and 1.11). -@static if VERSION < v"1.13.0-rc4" - LinearAlgebra.generic_matvecmul!( - Y::ROCVector, tA::AbstractChar, A::StridedROCMatrix, B::StridedROCVector, - alpha::Number, beta::Number, - ) = LinearAlgebra.mul!(Y, tA, A, B, alpha, beta) - LinearAlgebra.generic_matvecmul!( - Y::ROCVector, tA::AbstractChar, A::StridedROCMatrix, B::StridedROCVector, - _add::MulAddMul, - ) = LinearAlgebra.mul!(Y, tA, A, B, _add.alpha, _add.beta) - LinearAlgebra.generic_matmatmul!( - C::StridedROCVecOrMat, tA, tB, A::StridedROCVecOrMat, B::StridedROCVecOrMat, - alpha::Number, beta::Number, - ) = LinearAlgebra.mul!(C, tA, tB, A, B, alpha, beta) - LinearAlgebra.generic_matmatmul!( - C::StridedROCVecOrMat, tA, tB, A::StridedROCVecOrMat, B::StridedROCVecOrMat, - _add::MulAddMul, - ) = LinearAlgebra.mul!(C, tA, tB, A, B, _add.alpha, _add.beta) -end - -LinearAlgebra.generic_trimatmul!( - C::StridedROCMatrix{T}, uploc, isunitc, tfun::Function, - A::StridedROCMatrix{T}, B::StridedROCMatrix{T}, -) where T <: ROCBLASFloat = trmm!( - 'L', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', - isunitc, one(T), A, B, C) - -LinearAlgebra.generic_mattrimul!( - C::StridedROCMatrix{T}, uploc, isunitc, tfun::Function, - A::StridedROCMatrix{T}, B::StridedROCMatrix{T}, -) where T <: ROCBLASFloat = trmm!( - 'R', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', - isunitc, one(T), B, A, C) - const AdjOrTransOrROCMatrix{T} = Union{ StridedROCMatrix{T}, AdjOrTrans{<: T, <: StridedROCMatrix}} @@ -287,58 +119,76 @@ function LinearAlgebra.generic_trimatmul!( tfunB = LinearAlgebra.wrapperop(B) transa = tfunA === identity ? 'N' : tfunA === transpose ? 'T' : 'C' transb = tfunB === identity ? 'N' : tfunB === transpose ? 'T' : 'C' + # one operand gets its other triangle zeroed so rocBLAS can treat it as a full matrix; + # do so on a copy, as both are the caller's if uplocA == 'L' && tfunA === identity && tfunB === identity && uplocB == 'U' && isunitcB == 'N' # lower * upper - triu!(B) - trmm!('L', uplocA, transa, isunitcA, one(T), A, B, C) + trmm!('L', uplocA, transa, isunitcA, one(T), A, triu!(copy(B)), C) elseif uplocA == 'U' && tfunA === identity && tfunB === identity && uplocB == 'L' && isunitcB == 'N' # upper * lower - tril!(B) - trmm!('L', uplocA, transa, isunitcA, one(T), A, B, C) + trmm!('L', uplocA, transa, isunitcA, one(T), A, tril!(copy(B)), C) elseif uplocA == 'U' && tfunA === identity && tfunB !== identity && uplocB == 'U' && isunitcA == 'N' # operation is reversed to avoid executing the transpose - triu!(A) - trmm!('R', uplocB, transb, isunitcB, one(T), parent(B), A, C) + trmm!('R', uplocB, transb, isunitcB, one(T), parent(B), triu!(copy(A)), C) elseif uplocA == 'L' && tfunA !== identity && tfunB === identity && uplocB == 'L' && isunitcB == 'N' - tril!(B) - trmm!('L', uplocA, transa, isunitcA, one(T), A, B, C) + trmm!('L', uplocA, transa, isunitcA, one(T), A, tril!(copy(B)), C) elseif uplocA == 'U' && tfunA !== identity && tfunB === identity && uplocB == 'U' && isunitcB == 'N' - triu!(B) - trmm!('L', uplocA, transa, isunitcA, one(T), A, B, C) + trmm!('L', uplocA, transa, isunitcA, one(T), A, triu!(copy(B)), C) elseif uplocA == 'L' && tfunA === identity && tfunB !== identity && uplocB == 'L' && isunitcA == 'N' - tril!(A) - trmm!('R', uplocB, transb, isunitcB, one(T), parent(B), A, C) + trmm!('R', uplocB, transb, isunitcB, one(T), parent(B), tril!(copy(A)), C) else throw("mixed triangular-triangular multiplication") # TODO: rethink end return C end -LinearAlgebra.generic_trimatdiv!( - C::StridedROCMatrix{T}, uploc, isunitc, tfun::Function, - A::StridedROCMatrix{T}, B::AdjOrTransOrROCMatrix{T}, -) where T <: ROCBLASFloat = trsm!( - 'L', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', - isunitc, one(T), A, C === B ? C : copyto!(C, B)) - -LinearAlgebra.generic_mattridiv!( - C::StridedROCMatrix{T}, uploc, isunitc, tfun::Function, - A::AdjOrTransOrROCMatrix{T}, B::StridedROCMatrix{T}, -) where T <: ROCBLASFloat = trsm!( - 'R', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', - isunitc, one(T), B, C === A ? C : copyto!(C, A)) - -# Matrix inversion. - -for (t, uploc, isunitc) in ( - (:LowerTriangular, 'U', 'N'), - (:UnitLowerTriangular, 'U', 'U'), - (:UpperTriangular, 'L', 'N'), - (:UnitUpperTriangular, 'L', 'U'), -) - @eval function LinearAlgebra.inv(x::$t{T, <: ROCMatrix{T}}) where T <: ROCBLASFloat - out = ROCArray{T}(I(size(x, 1))) - $t(LinearAlgebra.ldiv!(x, out)) - end -end +# GPUArrays implements LinearAlgebra's matrix products and triangular operations, and +# calls these hooks for the operations that BLAS supports. + +GPUArrays.blas_gemv!( + tA::AbstractChar, α::Union{Bool,T}, A::StridedROCMatrix{T}, x::StridedROCVector{T}, + β::Union{Bool,T}, y::StridedROCVector{T}, +) where T <: ROCBLASFloat = gemv!(tA, T(α), A, x, T(β), y) +GPUArrays.blas_symv!( + uplo::AbstractChar, α::Union{Bool,T}, A::StridedROCMatrix{T}, x::StridedROCVector{T}, + β::Union{Bool,T}, y::StridedROCVector{T}, +) where T <: ROCBLASFloat = symv!(uplo, T(α), A, x, T(β), y) +GPUArrays.blas_hemv!( + uplo::AbstractChar, α::Union{Bool,T}, A::StridedROCMatrix{T}, x::StridedROCVector{T}, + β::Union{Bool,T}, y::StridedROCVector{T}, +) where T <: ROCBLASComplex = hemv!(uplo, T(α), A, x, T(β), y) +GPUArrays.blas_trmv!( + uplo::AbstractChar, trans::AbstractChar, diag::AbstractChar, + A::StridedROCMatrix{T}, x::StridedROCVector{T}, +) where T <: ROCBLASFloat = trmv!(uplo, trans, diag, A, x) +GPUArrays.blas_trsv!( + uplo::AbstractChar, trans::AbstractChar, diag::AbstractChar, + A::StridedROCMatrix{T}, x::StridedROCVector{T}, +) where T <: ROCBLASFloat = trsv!(uplo, trans, diag, A, x) + +GPUArrays.blas_gemm!( + tA::AbstractChar, tB::AbstractChar, α::Union{Bool,T}, A::StridedROCVecOrMat{T}, + B::StridedROCVecOrMat{T}, β::Union{Bool,T}, C::StridedROCVecOrMat{T}, +) where T <: ROCBLASFloat = gemm!(tA, tB, T(α), A, B, T(β), C) +# the non gemm_ex version is slower than the gemm_ex for 16 bit floats +GPUArrays.blas_gemm!( + tA::AbstractChar, tB::AbstractChar, α::Real, A::StridedROCVecOrMat{T}, + B::StridedROCVecOrMat{T}, β::Real, C::StridedROCVecOrMat{T}, +) where T <: Union{Float16, BFloat16} = gemm_ex!(tA, tB, α, A, B, β, C) +GPUArrays.blas_symm!( + side::AbstractChar, uplo::AbstractChar, α::Union{Bool,T}, A::StridedROCMatrix{T}, + B::StridedROCMatrix{T}, β::Union{Bool,T}, C::StridedROCMatrix{T}, +) where T <: ROCBLASFloat = symm!(side, uplo, T(α), A, B, T(β), C) +GPUArrays.blas_hemm!( + side::AbstractChar, uplo::AbstractChar, α::Union{Bool,T}, A::StridedROCMatrix{T}, + B::StridedROCMatrix{T}, β::Union{Bool,T}, C::StridedROCMatrix{T}, +) where T <: ROCBLASComplex = hemm!(side, uplo, T(α), A, B, T(β), C) +GPUArrays.blas_trmm!( + side::AbstractChar, uplo::AbstractChar, trans::AbstractChar, diag::AbstractChar, + α::Union{Bool,T}, A::StridedROCMatrix{T}, B::StridedROCMatrix{T}, C::StridedROCMatrix{T}, +) where T <: ROCBLASFloat = trmm!(side, uplo, trans, diag, T(α), A, B, C) +GPUArrays.blas_trsm!( + side::AbstractChar, uplo::AbstractChar, trans::AbstractChar, diag::AbstractChar, + α::Union{Bool,T}, A::StridedROCMatrix{T}, B::StridedROCMatrix{T}, +) where T <: ROCBLASFloat = trsm!(side, uplo, trans, diag, T(α), A, B) # Diagonal matrix. From 2ead5c6b95d0c32a87b24e9c13badcab3850fd2d Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 10 Nov 2026 15:53:06 +0100 Subject: [PATCH 02/11] Use GPUArrays' BLAS 1, syrk, herk, geam and dgmm hooks GPUArrays now implements the BLAS 1 operations, the Diagonal and triangular-triangular products, and matrix addition with a transposed operand, calling hooks for the BLAS routines. Implement those with rocBLAS, and drop our copies of the rest. rocBLAS' nrm2 doesn't rescale, so norm keeps using GPUArrays' implementation. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- src/blas/highlevel.jl | 224 +++++++----------------------------------- 1 file changed, 35 insertions(+), 189 deletions(-) diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index 08530a209..79255c447 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -1,92 +1,5 @@ rocblas_size(t::Char, M::ROCVecOrMat) = (size(M, t=='N' ? 1 : 2), size(M, t=='N' ? 2 : 1)) -# -# BLAS 1 -# - -LinearAlgebra.rmul!(x::ROCArray{<:ROCBLASFloat}, k::Number) = - scal!(length(x), convert(eltype(x), k), x, 1) - -LinearAlgebra.rmul!(x::ROCArray{<:ROCBLASComplex}, k::Real) = - scal!(length(x), convert(real(eltype(x)), k), x, 1) - -LinearAlgebra.rmul!(x::ROCArray{<:ROCBLASComplex}, k::Number) = - scal!(length(x), convert(eltype(x), k), x, 1) - -# Work around ambiguity with GPUArrays wrapper -LinearAlgebra.rmul!(x::ROCArray{<:ROCBLASFloat}, k::Real) = - invoke(rmul!, Tuple{typeof(x), Number}, x, k) - -function LinearAlgebra.dot( - DX::StridedROCArray{T}, DY::StridedROCArray{T}, -) where T <: Union{Float16, Float32, Float64} - n = length(DX) - n == length(DY) || throw(DimensionMismatch( - "dot product arguments have lengths $(length(DX)) and $(length(DY))")) - dot(n, DX, stride(DX, 1), DY, stride(DY, 1)) -end - -function LinearAlgebra.dot( - DX::StridedROCArray{T}, DY::StridedROCArray{T}, -) where T <: ROCBLASComplex - n = length(DX) - n == length(DY) || throw(DimensionMismatch( - "dot product arguments have lengths $(length(DX)) and $(length(DY))")) - dotc(n, DX, stride(DX, 1), DY, stride(DY, 1)) -end - -function LinearAlgebra.:(*)( - transx::Transpose{<:Any, <:StridedROCVector{T}}, y::StridedROCVector{T}, -) where T <: Union{ComplexF16, ROCBLASComplex} - x = transx.parent - n = length(x) - n == length(y) || throw(DimensionMismatch( - "dot product arguments have lengths $(length(x)) and $(length(y))")) - dotu(n, x, stride(x, 1), y, stride(y, 1)) -end - -LinearAlgebra.norm(x::Diagonal{T, <:StridedROCVector{T}}, p::Real=2) where {T<:Union{Float16, ComplexF16, ROCBLASFloat}} = norm(x.diag, p) -LinearAlgebra.norm(x::ROCArray{T}) where T <: ROCBLASFloat = nrm2(length(x), x, stride(x, 1)) -LinearAlgebra.BLAS.asum(x::ROCArray{T}) where T <: ROCBLASFloat = asum(length(x), x, stride(x, 1)) - -function LinearAlgebra.axpy!( - alpha::Number, x::ROCArray{T}, y::ROCArray{T}, -) where T <: ROCBLASFloatWithHalf - length(x) == length(y) || throw(DimensionMismatch("")) - axpy!(length(x), convert(T,alpha), x, 1, y, 1) -end - -function LinearAlgebra.axpby!( - alpha::Number, x::ROCArray{T}, beta::Number, y::ROCArray{T}, -) where T <: Union{ROCBLASFloat, ROCBLASComplex} - lx, ly = length(x), length(y) - lx == ly || throw(DimensionMismatch("")) - beta = isa(beta, Real) ? convert(real(T), beta) : convert(T, beta) - scal!(ly, beta, y, 1) - axpy!(lx, convert(T,alpha), x, 1, y, 1) -end - -function LinearAlgebra.rotate!( - x::ROCArray{T}, y::ROCArray{T}, c::Number, s::Number, -) where T <: Union{ROCBLASFloat, ROCBLASComplex} - lx, ly = length(x), length(y) - lx == ly || throw(DimensionMismatch( - "rotate arguments have lengths $lx and $ly")) - rot!(lx, x, 1, y, 1, c, s) - x, y -end - -function LinearAlgebra.reflect!( - x::ROCArray{T}, y::ROCArray{T}, c::Number, s::Number, -) where T <: Union{ROCBLASFloat, ROCBLASComplex} - lx, ly = length(x), length(y) - lx == ly || throw(DimensionMismatch( - "reflect arguments have lengths $lx and $ly")) - rot!(lx, x, 1, y, 1, c, s) - scal!(ly, -real(one(T)), y, 1) - x, y -end - # # BLAS 2 # @@ -104,42 +17,6 @@ LinearAlgebra.istriu(::ROCLowerOrUnitLowerTriangular) = false LinearAlgebra.istril(::ROCLowerOrUnitLowerTriangular) = true end -const AdjOrTransOrROCMatrix{T} = Union{ - StridedROCMatrix{T}, - AdjOrTrans{<: T, <: StridedROCMatrix}} - -function LinearAlgebra.generic_trimatmul!( - C::StridedROCMatrix{T}, uplocA, isunitcA, - tfunA::Function, A::StridedROCMatrix{T}, - triB::UpperOrLowerTriangular{T, <: AdjOrTransOrROCMatrix{T}}, -) where T <: ROCBLASFloat - uplocB = LinearAlgebra.uplo_char(triB) - isunitcB = LinearAlgebra.isunit_char(triB) - B = parent(triB) - tfunB = LinearAlgebra.wrapperop(B) - transa = tfunA === identity ? 'N' : tfunA === transpose ? 'T' : 'C' - transb = tfunB === identity ? 'N' : tfunB === transpose ? 'T' : 'C' - # one operand gets its other triangle zeroed so rocBLAS can treat it as a full matrix; - # do so on a copy, as both are the caller's - if uplocA == 'L' && tfunA === identity && tfunB === identity && uplocB == 'U' && isunitcB == 'N' # lower * upper - trmm!('L', uplocA, transa, isunitcA, one(T), A, triu!(copy(B)), C) - elseif uplocA == 'U' && tfunA === identity && tfunB === identity && uplocB == 'L' && isunitcB == 'N' # upper * lower - trmm!('L', uplocA, transa, isunitcA, one(T), A, tril!(copy(B)), C) - elseif uplocA == 'U' && tfunA === identity && tfunB !== identity && uplocB == 'U' && isunitcA == 'N' - # operation is reversed to avoid executing the transpose - trmm!('R', uplocB, transb, isunitcB, one(T), parent(B), triu!(copy(A)), C) - elseif uplocA == 'L' && tfunA !== identity && tfunB === identity && uplocB == 'L' && isunitcB == 'N' - trmm!('L', uplocA, transa, isunitcA, one(T), A, tril!(copy(B)), C) - elseif uplocA == 'U' && tfunA !== identity && tfunB === identity && uplocB == 'U' && isunitcB == 'N' - trmm!('L', uplocA, transa, isunitcA, one(T), A, triu!(copy(B)), C) - elseif uplocA == 'L' && tfunA === identity && tfunB !== identity && uplocB == 'L' && isunitcA == 'N' - trmm!('R', uplocB, transb, isunitcB, one(T), parent(B), tril!(copy(A)), C) - else - throw("mixed triangular-triangular multiplication") # TODO: rethink - end - return C -end - # GPUArrays implements LinearAlgebra's matrix products and triangular operations, and # calls these hooks for the operations that BLAS supports. @@ -189,80 +66,49 @@ GPUArrays.blas_trsm!( side::AbstractChar, uplo::AbstractChar, trans::AbstractChar, diag::AbstractChar, α::Union{Bool,T}, A::StridedROCMatrix{T}, B::StridedROCMatrix{T}, ) where T <: ROCBLASFloat = trsm!(side, uplo, trans, diag, T(α), A, B) +GPUArrays.blas_syrk!( + uplo::AbstractChar, trans::AbstractChar, α::Union{Bool,T}, A::StridedROCMatrix{T}, + β::Union{Bool,T}, C::StridedROCMatrix{T}, +) where T <: ROCBLASFloat = syrk!(uplo, trans, T(α), A, T(β), C) +GPUArrays.blas_herk!( + uplo::AbstractChar, trans::AbstractChar, α::Real, A::ROCMatrix{T}, β::Real, C::ROCMatrix{T}, +) where T <: ROCBLASComplex = herk!(uplo, trans, real(T)(α), A, real(T)(β), C) +GPUArrays.blas_geam!( + tA::AbstractChar, tB::AbstractChar, α::Union{Bool,T}, A::ROCMatrix{T}, + β::Union{Bool,T}, B::ROCMatrix{T}, C::ROCMatrix{T}, +) where T <: ROCBLASFloat = geam!(tA, tB, T(α), A, T(β), B, C) +GPUArrays.blas_dgmm!( + side::AbstractChar, A::StridedROCMatrix{T}, x::StridedROCVector{T}, C::StridedROCMatrix{T}, +) where T <: ROCBLASFloat = dgmm!(side, A, x, C) + +# the level 1 wrappers take dense arrays and an increment +GPUArrays.blas_scal!(α::Number, x::ROCArray{T}) where T <: ROCBLASFloat = + scal!(length(x), convert(T, α), x, 1) +GPUArrays.blas_scal!(α::Real, x::ROCArray{T}) where T <: ROCBLASComplex = + scal!(length(x), convert(real(T), α), x, 1) +GPUArrays.blas_dot(x::ROCArray{T}, y::ROCArray{T}) where T <: Union{Float16, ROCBLASReal} = + dot(length(x), x, 1, y, 1) +GPUArrays.blas_dot(x::ROCArray{T}, y::ROCArray{T}) where T <: ROCBLASComplex = + dotc(length(x), x, 1, y, 1) +GPUArrays.blas_dotu(x::ROCArray{T}, y::ROCArray{T}) where T <: ROCBLASComplex = + dotu(length(x), x, 1, y, 1) +GPUArrays.blas_asum(x::ROCArray{<:ROCBLASFloat}) = asum(length(x), x, 1) +GPUArrays.blas_axpy!(α::Number, x::ROCArray{T}, y::ROCArray{T}) where T <: ROCBLASFloatWithHalf = + axpy!(length(x), convert(T, α), x, 1, y, 1) +function GPUArrays.blas_axpby!(α::Number, x::ROCArray{T}, β::Number, y::ROCArray{T}) where T <: ROCBLASFloat + GPUArrays.blas_scal!(β, y) + axpy!(length(x), convert(T, α), x, 1, y, 1) +end +GPUArrays.blas_rot!(x::ROCArray{T}, y::ROCArray{T}, c::Real, s::Number) where T <: ROCBLASFloat = + rot!(length(x), x, 1, y, 1, convert(real(T), c), s isa Real ? convert(real(T), s) : convert(T, s)) # Diagonal matrix. -Base.Array(D::Diagonal{T, <: ROCArray{T}}) where T = Diagonal(Array(D.diag)) - AMDGPU.ROCArray(D::Diagonal{T, <: Vector{T}}) where T = Diagonal(ROCArray(D.diag)) -function LinearAlgebra.inv(D::Diagonal{T, <: ROCArray{T}}) where T - Di = map(inv, D.diag) - any(isinf, Di) && error("Singular Exception $Di") - Diagonal(Di) -end - -function Base.:/(A::ROCArray, D::Diagonal) - B = similar(A, typeof(oneunit(eltype(A)) / oneunit(eltype(D)))) - _rdiv!(B, A, D) -end - -function LinearAlgebra._rdiv!(B::ROCArray, A::ROCArray, D::Diagonal) - m, n = size(A, 1), size(A, 2) - (k = length(D.diag)) != n && throw(DimensionMismatch( - "left hand side has $n columns but D is $k by $k")) - - B .= A * inv(D) - return B -end - if VERSION ≥ v"1.12-" # Otherwise, dispatches to: # https://github.com/JuliaLang/LinearAlgebra.jl/blob/4e7c3f40316a956119ac419a97c4b8aad7a17e6c/src/generic.jl#L2092 LinearAlgebra.copytrito!(B::Matrix{T}, A::ROCMatrix{T}, uplo::AbstractChar) where {T <: ROCBLASFloat} = invoke(LinearAlgebra.copytrito!, Tuple{AbstractMatrix, AbstractMatrix, AbstractChar}, B, A, uplo) end - -function LinearAlgebra.lmul!(A::Diagonal{T,<:ROCVector{T}}, B::ROCMatrix{T}) where {T<:ROCBLASFloat} - return dgmm!('L', B, A.diag, B) -end - -function LinearAlgebra.rmul!(A::ROCMatrix{T}, B::Diagonal{T,<:ROCVector{T}}) where {T<:ROCBLASFloat} - return dgmm!('R', A, B.diag, A) -end - -function LinearAlgebra.mul!(C::Diagonal{T, <:ROCVector}, A::Union{<:ROCMatrix{T}, Adjoint{T, <:ROCMatrix}, Transpose{T, <:ROCMatrix}}, B::Union{<:ROCMatrix{T}, Adjoint{T, <:ROCMatrix}, Transpose{T, <:ROCMatrix}}) where {T<:ROCBLASFloat} - Cfull = A*B - C.diag .= diag(Cfull) - return C -end - -# eltypes do not match -function LinearAlgebra.lmul!(A::Diagonal{T,<:ROCVector{T}}, B::ROCMatrix) where {T<:ROCBLASFloat} - @. B = A.diag * B - return B -end -function LinearAlgebra.lmul!(A::Diagonal{Td,<:ROCVector{Td}}, B::Transpose{Tt, <:ROCMatrix{Tt}}) where {Td<:ROCBLASFloat, Tt<:ROCBLASFloat} - @. B = A.diag * B - return B -end -function LinearAlgebra.lmul!(A::Diagonal{Td,<:ROCVector{Td}}, B::Adjoint{Tt, <:ROCMatrix{Tt}}) where {Td<:ROCBLASFloat, Tt<:ROCBLASFloat} - @. B = A.diag * B - return B -end -# eltypes do not match -function LinearAlgebra.rmul!(A::ROCMatrix, B::Diagonal{T,<:ROCVector{T}}) where {T<:ROCBLASFloat} - At = transpose(A) - @. At = B.diag * At - return A -end -function LinearAlgebra.rmul!(A::Transpose{Tt, <:ROCMatrix{Tt}}, B::Diagonal{Td,<:ROCVector{Td}}) where {Td<:ROCBLASFloat, Tt<:ROCBLASFloat} - At = parent(A) - @. At = B.diag * At - return transpose(At) -end -function LinearAlgebra.rmul!(A::Adjoint{Tt, <:ROCMatrix{Tt}}, B::Diagonal{Td,<:ROCVector{Td}}) where {Td<:ROCBLASFloat, Tt<:ROCBLASFloat} - At = parent(A) - @. At = adjoint(B.diag) * At - return adjoint(At) -end From 245016ea16381400e39c69e214fd7fa514ca2107 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 10 Nov 2026 18:17:32 +0100 Subject: [PATCH 03/11] Restore dot methods, and test strided views, strong zeros and Diagonal edge cases Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- src/blas/highlevel.jl | 8 ++ test/hip_rocarray/blas_edge_cases.jl | 106 +++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 test/hip_rocarray/blas_edge_cases.jl diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index 79255c447..a1d58f255 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -86,6 +86,14 @@ GPUArrays.blas_scal!(α::Number, x::ROCArray{T}) where T <: ROCBLASFloat = scal!(length(x), convert(T, α), x, 1) GPUArrays.blas_scal!(α::Real, x::ROCArray{T}) where T <: ROCBLASComplex = scal!(length(x), convert(real(T), α), x, 1) +# more specific than LinearAlgebra's methods for host BLAS +LinearAlgebra.dot(x::StridedROCArray{T}, y::StridedROCArray{T}) where T <: Union{Float16, ROCBLASFloat} = + GPUArrays.gpu_dot(x, y) +LinearAlgebra.dot(x::AnyROCArray, y::AnyROCArray) = GPUArrays.gpu_dot(x, y) +LinearAlgebra.dot(x::Adjoint{<:Union{Real,Complex},<:ROCArray}, y::Adjoint{<:Union{Real,Complex},<:ROCArray}) = + GPUArrays.gpu_dot(x, y) +LinearAlgebra.dot(A::Hermitian{<:Union{Real,Complex},<:ROCMatrix}, B::Hermitian{<:Union{Real,Complex},<:ROCMatrix}) = + GPUArrays.gpu_dot(A, B) GPUArrays.blas_dot(x::ROCArray{T}, y::ROCArray{T}) where T <: Union{Float16, ROCBLASReal} = dot(length(x), x, 1, y, 1) GPUArrays.blas_dot(x::ROCArray{T}, y::ROCArray{T}) where T <: ROCBLASComplex = diff --git a/test/hip_rocarray/blas_edge_cases.jl b/test/hip_rocarray/blas_edge_cases.jl new file mode 100644 index 000000000..c710f1ae8 --- /dev/null +++ b/test/hip_rocarray/blas_edge_cases.jl @@ -0,0 +1,106 @@ +using Test +using AMDGPU +using AMDGPU: ROCArray +using LinearAlgebra + +@testset "triangular operations on strided views" begin + n = 4 + # the first n rows can be addressed by BLAS, every other row can't + @testset "$T, rows $rows" for T in (Float32, Float64, ComplexF32), rows in (1:n, 1:2:2n) + v(X) = view(X, rows, 1:n) + + A = rand(T, 2n, 2n) + for i in 1:n + A[rows[i], i] += 4 + end + B = rand(T, 2n, n) + + @test testf((A, B) -> UpperTriangular(v(A)) \ v(B), A, B) + @test testf((A, B) -> LowerTriangular(v(A))' \ v(B), A, B) + @test testf((A, B) -> UnitUpperTriangular(v(A)) \ v(B)', A, B) + @test testf((A, B) -> v(B) / UpperTriangular(v(A)), A, B) + @test testf((A, B) -> v(B)' / transpose(LowerTriangular(v(A))), A, B) + + @test testf((A, B) -> UpperTriangular(v(A)) * v(B), A, B) + @test testf((A, B) -> transpose(LowerTriangular(v(A))) * v(B), A, B) + @test testf((A, B) -> v(B) * UnitUpperTriangular(v(A)), A, B) + @test testf((A, B) -> v(B) * LowerTriangular(v(A))', A, B) + + @test testf((A, B) -> copy(ldiv!(LowerTriangular(v(A)), v(B))), A, B) + @test testf((A, B) -> copy(lmul!(UpperTriangular(v(A)), v(B))), A, B) + @test testf((A, B) -> copy(rdiv!(v(B), UnitLowerTriangular(v(A)))), A, B) + + @test testf(A -> triu(parent(inv(UpperTriangular(v(A))))), A) + @test testf(A -> tril(parent(inv(LowerTriangular(v(A))))), A) + + # LinearAlgebra's BLAS calls only take views with contiguous columns + if step(rows) == 1 + @test testf((A, B) -> Symmetric(v(A)) * v(B), A, B) + @test testf((A, B) -> v(B)' * Hermitian(v(A), :L), A, B) + end + + @testset "vector $vrows" for vrows in (1:n, 1:2:2n, n:-1:1) + w(x) = view(x, vrows) + b = rand(T, 2n) + @test testf((A, b) -> UpperTriangular(v(A)) \ w(b), A, b) + @test testf((A, b) -> LowerTriangular(v(A))' \ w(b), A, b) + @test testf((A, b) -> copy(ldiv!(UpperTriangular(v(A)), w(b))), A, b) + @test testf((A, b) -> v(A) * w(b), A, b) + if step(rows) == 1 + @test testf((A, b) -> UnitLowerTriangular(v(A)) * w(b), A, b) + @test testf((A, b) -> Symmetric(v(A)) * w(b), A, b) + @test testf((A, b) -> Hermitian(v(A))' * w(b), A, b) + end + end + end +end + +@testset "false is a strong zero, $T" for T in (Float32, ComplexF32) + n = 4 + nan(d...) = fill(T(NaN), d...) + A, B, x = rand(T, n, n), rand(T, n, n), rand(T, n) + + @test testf((C, A, B) -> mul!(C, A, B, false, true), rand(T, n, n), nan(n, n), B) + @test testf((C, A, B) -> mul!(C, A, B, true, false), nan(n, n), A, B) + @test testf((y, A, x) -> mul!(y, A, x, false, true), rand(T, n), nan(n, n), x) + @test testf((y, A, x) -> mul!(y, A, x, true, false), nan(n), A, x) + @test testf((C, A, B) -> mul!(C, Symmetric(A), B, true, false), nan(n, n), A, B) + @test testf((y, A, x) -> mul!(y, Symmetric(A), x, true, false), nan(n), A, x) + @test testf((C, A) -> mul!(C, A', A, true, false), nan(n, n), A) + @test testf((C, A) -> transpose!(C, A), nan(n, n), A) + @test testf((C, x, A) -> mul!(C, Diagonal(x), A), nan(n, n), x, A) + @test testf((C, A, B) -> mul!(C, UpperTriangular(A), B), nan(n, n), A, B) + + @testset "$(isnothing(rows) ? "dense" : "rows $rows")" for rows in (nothing, 1:2:12) + v(x) = isnothing(rows) ? x : view(x, rows) + m = isnothing(rows) ? 6 : 12 + x, y = rand(T, m), rand(T, m) + @test testf(x -> rmul!(v(x), false), nan(m)) + @test testf((x, y) -> axpby!(T(2), v(x), false, v(y)), x, nan(m)) + @test testf((x, y) -> axpby!(false, v(x), T(3), v(y)), nan(m), y) + @test testf((x, y) -> copy(first(rotate!(v(x), v(y), false, true))), nan(m), y) + end +end + +@testset "Diagonal" begin + @testset "norm(::Diagonal, $p)" for p in (2, 1, Inf, -Inf, 0, -1) + @test testf(d -> norm(Diagonal(d), p), rand(Float32, 4)) + end + @test_throws SingularException inv(Diagonal(ROCArray(Float32[1, 0, 2]))) +end + +@testset "dot of wrapped arrays, $T" for T in (Float32, ComplexF32) + A, B = rand(T, 4, 6), rand(T, 4, 6) + @test testf((A, B) -> dot(A', B'), A, B) + @test testf((A, B) -> dot(transpose(A), transpose(B)), A, B) + @test testf((A, B) -> dot(Symmetric(A[:, 1:4]), Symmetric(B[:, 1:4])), A, B) + @test testf((A, B) -> dot(Hermitian(A[:, 1:4], :L), Hermitian(B[:, 1:4], :L)), A, B) + @test testf((x, y) -> dot(view(x, 1:4), view(y, 1:4)), vec(A), vec(B)) +end + +@testset "dot of strided views, $T" for T in (Float32, ComplexF32) + x, y = rand(T, 12), rand(T, 12) + @test testf((x, y) -> dot(view(x, 1:2:12), view(y, 1:2:12)), x, y) + @test testf((x, y) -> dot(view(x, 12:-2:1), view(y, 1:2:12)), x, y) + T <: Complex && @test testf((x, y) -> transpose(view(x, 1:2:12)) * view(y, 1:2:12), x, y) +end From 4ef64e2e665ca800783a60877ea1fdbe2c2d7018 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 12 Nov 2026 12:50:53 +0100 Subject: [PATCH 04/11] Test unit triangular products Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- test/hip_rocarray/blas_edge_cases.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/hip_rocarray/blas_edge_cases.jl b/test/hip_rocarray/blas_edge_cases.jl index c710f1ae8..1c256c2e4 100644 --- a/test/hip_rocarray/blas_edge_cases.jl +++ b/test/hip_rocarray/blas_edge_cases.jl @@ -104,3 +104,8 @@ end @test testf((x, y) -> dot(view(x, 12:-2:1), view(y, 1:2:12)), x, y) T <: Complex && @test testf((x, y) -> transpose(view(x, 1:2:12)) * view(y, 1:2:12), x, y) end + +@testset "unit triangular products, $T" for T in (Float32, ComplexF32) + S, R = rand(T, 4, 4), rand(T, 4, 4) + @test testf((S, R) -> UnitLowerTriangular(S) * UnitUpperTriangular(R), S, R) +end From 086c093bfefd833ec4c1c9e91947aafabc736fd5 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Wed, 25 Nov 2026 10:11:39 +0100 Subject: [PATCH 05/11] Only keep dot methods for Symmetric and Hermitian matrices Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- src/blas/highlevel.jl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index a1d58f255..6bd3794a7 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -86,12 +86,8 @@ GPUArrays.blas_scal!(α::Number, x::ROCArray{T}) where T <: ROCBLASFloat = scal!(length(x), convert(T, α), x, 1) GPUArrays.blas_scal!(α::Real, x::ROCArray{T}) where T <: ROCBLASComplex = scal!(length(x), convert(real(T), α), x, 1) -# more specific than LinearAlgebra's methods for host BLAS -LinearAlgebra.dot(x::StridedROCArray{T}, y::StridedROCArray{T}) where T <: Union{Float16, ROCBLASFloat} = - GPUArrays.gpu_dot(x, y) -LinearAlgebra.dot(x::AnyROCArray, y::AnyROCArray) = GPUArrays.gpu_dot(x, y) -LinearAlgebra.dot(x::Adjoint{<:Union{Real,Complex},<:ROCArray}, y::Adjoint{<:Union{Real,Complex},<:ROCArray}) = - GPUArrays.gpu_dot(x, y) +LinearAlgebra.dot(A::Symmetric{<:Union{Real,Complex},<:ROCMatrix}, B::Symmetric{<:Union{Real,Complex},<:ROCMatrix}) = + GPUArrays.gpu_dot(A, B) LinearAlgebra.dot(A::Hermitian{<:Union{Real,Complex},<:ROCMatrix}, B::Hermitian{<:Union{Real,Complex},<:ROCMatrix}) = GPUArrays.gpu_dot(A, B) GPUArrays.blas_dot(x::ROCArray{T}, y::ROCArray{T}) where T <: Union{Float16, ROCBLASReal} = From 5c6e287aa5cb19cf78e635882213a1f3313589ae Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Wed, 2 Dec 2026 13:36:50 +0100 Subject: [PATCH 06/11] Skip the BLAS edge case tests when rocBLAS is unavailable Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index e5ece8af9..46a0000b5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -43,7 +43,7 @@ end for (lib, pred) in [ :MIOpen => name -> startswith(name, "hip_dnn/"), :rocsparse => name -> startswith(name, "hip_rocsparse/"), - :rocblas => name -> name == "hip_rocarray/blas", + :rocblas => name -> startswith(name, "hip_rocarray/blas"), :rocfft => name -> name == "hip_rocarray/fft", :rocrand => name -> name == "hip_rocarray/random", :rocsolver => name -> name == "hip_rocarray/solver", From d9eee5dac0a2c8fa179cc67cc224beb0be3da770 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Wed, 2 Dec 2026 13:50:20 +0100 Subject: [PATCH 07/11] Remove unused rocblas_size Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- src/blas/highlevel.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index 6bd3794a7..abac72ea5 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -1,5 +1,3 @@ -rocblas_size(t::Char, M::ROCVecOrMat) = (size(M, t=='N' ? 1 : 2), size(M, t=='N' ? 2 : 1)) - # # BLAS 2 # From b95305d5472534d1216199178432ceff901d94f7 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Wed, 2 Dec 2026 13:59:04 +0100 Subject: [PATCH 08/11] Use rocBLAS geam for real scalars with complex operands Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013b7DCbTAjtfi3JqXU4xZmW --- src/blas/highlevel.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index abac72ea5..a90c44dd9 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -72,8 +72,8 @@ GPUArrays.blas_herk!( uplo::AbstractChar, trans::AbstractChar, α::Real, A::ROCMatrix{T}, β::Real, C::ROCMatrix{T}, ) where T <: ROCBLASComplex = herk!(uplo, trans, real(T)(α), A, real(T)(β), C) GPUArrays.blas_geam!( - tA::AbstractChar, tB::AbstractChar, α::Union{Bool,T}, A::ROCMatrix{T}, - β::Union{Bool,T}, B::ROCMatrix{T}, C::ROCMatrix{T}, + tA::AbstractChar, tB::AbstractChar, α::Union{Real,T}, A::ROCMatrix{T}, + β::Union{Real,T}, B::ROCMatrix{T}, C::ROCMatrix{T}, ) where T <: ROCBLASFloat = geam!(tA, tB, T(α), A, T(β), B, C) GPUArrays.blas_dgmm!( side::AbstractChar, A::StridedROCMatrix{T}, x::StridedROCVector{T}, C::StridedROCMatrix{T}, From c9920338bedbeb63b5213693654b2d6bfad84f28 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Wed, 2 Dec 2026 16:17:57 +0100 Subject: [PATCH 09/11] Last comments --- src/blas/highlevel.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index a90c44dd9..4442ee939 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -72,14 +72,15 @@ GPUArrays.blas_herk!( uplo::AbstractChar, trans::AbstractChar, α::Real, A::ROCMatrix{T}, β::Real, C::ROCMatrix{T}, ) where T <: ROCBLASComplex = herk!(uplo, trans, real(T)(α), A, real(T)(β), C) GPUArrays.blas_geam!( - tA::AbstractChar, tB::AbstractChar, α::Union{Real,T}, A::ROCMatrix{T}, - β::Union{Real,T}, B::ROCMatrix{T}, C::ROCMatrix{T}, + tA::AbstractChar, tB::AbstractChar, α::Union{Real,T,real(T)}, A::ROCMatrix{T}, + β::Union{Real,T,real(T)}, B::ROCMatrix{T}, C::ROCMatrix{T}, ) where T <: ROCBLASFloat = geam!(tA, tB, T(α), A, T(β), B, C) GPUArrays.blas_dgmm!( side::AbstractChar, A::StridedROCMatrix{T}, x::StridedROCVector{T}, C::StridedROCMatrix{T}, ) where T <: ROCBLASFloat = dgmm!(side, A, x, C) # the level 1 wrappers take dense arrays and an increment +# no blas_nrm2: rocBLAS' nrm2 doesn't rescale (ROCm/rocm-libraries#16703), so norm uses GPUArrays' generic kernel GPUArrays.blas_scal!(α::Number, x::ROCArray{T}) where T <: ROCBLASFloat = scal!(length(x), convert(T, α), x, 1) GPUArrays.blas_scal!(α::Real, x::ROCArray{T}) where T <: ROCBLASComplex = From 403735b87f08764f3c25eb8648ce95da91bcea5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20R=C3=A4ss?= <61313342+luraess@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:22:48 +0100 Subject: [PATCH 10/11] Revert --- src/blas/highlevel.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index 4442ee939..9ccada4d0 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -72,8 +72,8 @@ GPUArrays.blas_herk!( uplo::AbstractChar, trans::AbstractChar, α::Real, A::ROCMatrix{T}, β::Real, C::ROCMatrix{T}, ) where T <: ROCBLASComplex = herk!(uplo, trans, real(T)(α), A, real(T)(β), C) GPUArrays.blas_geam!( - tA::AbstractChar, tB::AbstractChar, α::Union{Real,T,real(T)}, A::ROCMatrix{T}, - β::Union{Real,T,real(T)}, B::ROCMatrix{T}, C::ROCMatrix{T}, + tA::AbstractChar, tB::AbstractChar, α::Union{Real,T}, A::ROCMatrix{T}, + β::Union{Real,T}, B::ROCMatrix{T}, C::ROCMatrix{T}, ) where T <: ROCBLASFloat = geam!(tA, tB, T(α), A, T(β), B, C) GPUArrays.blas_dgmm!( side::AbstractChar, A::StridedROCMatrix{T}, x::StridedROCVector{T}, C::StridedROCMatrix{T}, From b1f31b094e3f39ca15b08ae2116fbe27dc058e73 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 2 Dec 2026 18:00:35 +0100 Subject: [PATCH 11/11] Rely on GPUArrays' dot for Symmetric and Hermitian matrices GPUArrays 12.4 implements dot for all wrapped GPU arrays, and returns a real number for Hermitian matrices, as LinearAlgebra does. --- Project.toml | 2 +- src/blas/highlevel.jl | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 7285a8dc9..2b3b82078 100644 --- a/Project.toml +++ b/Project.toml @@ -61,7 +61,7 @@ BFloat16s = "0.6.0" CEnum = "0.5, 1" ChainRulesCore = "1" EnzymeCore = "0.8" -GPUArrays = "12.3" +GPUArrays = "12.4" GPUCompiler = "2.24" GPUToolbox = "3.7" KernelAbstractions = "0.10" diff --git a/src/blas/highlevel.jl b/src/blas/highlevel.jl index 9ccada4d0..a5a983084 100644 --- a/src/blas/highlevel.jl +++ b/src/blas/highlevel.jl @@ -85,10 +85,6 @@ GPUArrays.blas_scal!(α::Number, x::ROCArray{T}) where T <: ROCBLASFloat = scal!(length(x), convert(T, α), x, 1) GPUArrays.blas_scal!(α::Real, x::ROCArray{T}) where T <: ROCBLASComplex = scal!(length(x), convert(real(T), α), x, 1) -LinearAlgebra.dot(A::Symmetric{<:Union{Real,Complex},<:ROCMatrix}, B::Symmetric{<:Union{Real,Complex},<:ROCMatrix}) = - GPUArrays.gpu_dot(A, B) -LinearAlgebra.dot(A::Hermitian{<:Union{Real,Complex},<:ROCMatrix}, B::Hermitian{<:Union{Real,Complex},<:ROCMatrix}) = - GPUArrays.gpu_dot(A, B) GPUArrays.blas_dot(x::ROCArray{T}, y::ROCArray{T}) where T <: Union{Float16, ROCBLASReal} = dot(length(x), x, 1, y, 1) GPUArrays.blas_dot(x::ROCArray{T}, y::ROCArray{T}) where T <: ROCBLASComplex =