diff --git a/src/dual.jl b/src/dual.jl index 64b43a62..a4b1d922 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -873,6 +873,31 @@ function _eigen_hermitian(A::DualMatrixRealComplex{T,<:Real,N}) where {T,N} return Eigen(_to_duals(Val(T), λ, λ_partials), _to_duals(Val(T), Q, Q_partials)) end +# Matrix products with BLAS # +#---------------------------# + +# Viewed as numbers, an `m×k` matrix of `Dual{T,V,N}` is an `(N+1)m×k` matrix of `V`, so its +# product with a matrix of `V` is a product of matrices of `V`, computed with BLAS. LinearAlgebra +# calls this method after unwrapping the factors (`tA`/`tB` record how), and its fallback for +# matrices of `V` is generic, so that product goes through the 5-argument `mul!`. +@static if VERSION >= v"1.13" + Base.@constprop :aggressive function LinearAlgebra.mul!(C::StridedMatrix{Dual{T,V,N}}, tA, tB, A::StridedMatrix{Dual{T,V,N}}, B::StridedMatrix{V}, α::Number, β::Number) where {T,V<:LinearAlgebra.BlasReal,N} + m, (k, n) = size(A, 1), tB == 'N' ? size(B) : reverse(size(B)) + use_blas = tA == 'N' && tB in ('N', 'T', 'C') && size(A, 2) == k && size(C) == (m, n) && + stride(A, 1) == stride(C, 1) == 1 && !(α isa Dual || β isa Dual) && + # as for matrices of `V`, small square products are faster without BLAS + !(m == k == n <= 3) && + # NaN-safe mode zeroes the product of a zero partial and a number that is not finite, + # which BLAS does not + (!NANSAFE_MODE_ENABLED || (isfinite(α) && isfinite(β) && all(isfinite, B))) + if use_blas + mul!(reinterpret(V, C), reinterpret(V, A), tB == 'N' ? B : transpose(B), α, β) + return C + end + return invoke(mul!, Tuple{AbstractVecOrMat,Any,Any,AbstractVecOrMat,AbstractVecOrMat,Number,Number}, C, tA, tB, A, B, α, β) + end +end + # Functions in SpecialFunctions which return tuples # # Their derivatives are not defined in DiffRules # #---------------------------------------------------# diff --git a/test/MiscTest.jl b/test/MiscTest.jl index e8068e95..79ca872a 100644 --- a/test/MiscTest.jl +++ b/test/MiscTest.jl @@ -219,4 +219,57 @@ end @test B_mmatrix == B end +@testset "matrix products with BLAS" begin + D(N, dims...) = ForwardDiff.Dual{TestTag,Float64,N}[ForwardDiff.Dual{TestTag}(rand(), ntuple(_ -> rand(), N)) for _ in CartesianIndices(dims)] + vals(X) = ForwardDiff.value.(X) + parts(X, i) = ForwardDiff.partials.(X, i) + + function test_mul(C, A, B, α = true, β = false) + C0 = copy(C) + # `C` may be NaN if `β` is zero + scaled(X, Y) = iszero(β) ? α * X : α * X + β * Y + @test mul!(C, A, B, α, β) === C + @test vals(C) ≈ scaled(vals(A) * B, vals(C0)) + for i in 1:ForwardDiff.npartials(eltype(C)) + @test parts(C, i) ≈ scaled(parts(A, i) * B, parts(C0, i)) + end + end + + for N in (0, 2), (m, k, n) in ((2, 2, 2), (5, 3, 4)), β in (0, 0.5) + C() = iszero(β) ? fill(ForwardDiff.Dual{TestTag}(NaN, ntuple(_ -> NaN, N)), m, n) : D(N, m, n) + test_mul(C(), D(N, m, k), rand(k, n), 2, β) + test_mul(C(), D(N, m, k), transpose(rand(n, k)), 2, β) + test_mul(C(), D(N, m, k), rand(n, k)', 2, β) + test_mul(C(), transpose(D(N, k, m)), rand(k, n), 2, β) + end + test_mul(view(D(2, 5, 3), 2:4, 1:2), view(D(2, 4, 6), 1:3, 2:5), view(rand(5, 3), 1:4, 1:2)) + test_mul(view(D(2, 6, 2), 1:2:6, :), view(D(2, 3, 8), :, 1:2:8), rand(4, 2)) + test_mul(D(2, 3, 2), D(2, 3, 0), rand(0, 2), π, 0.5) + test_mul(D(2, 3, 3), D(2, 3, 3), Symmetric(rand(3, 3))) + A32 = ForwardDiff.Dual{TestTag}.(rand(Float32, 3, 4), rand(Float32, 3, 4)) + test_mul(similar(A32, 3, 2), A32, rand(Float32, 4, 2), 1//2) + + A, B, α = D(2, 3, 4), rand(4, 2), ForwardDiff.Dual{TestTag}(2.0, (1.0, 0.0)) + C = mul!(D(2, 3, 2), A, B, α, false) + @test vals(C) ≈ 2 * vals(A) * B + @test parts(C, 1) ≈ (vals(A) + 2 * parts(A, 1)) * B + @test parts(C, 2) ≈ 2 * parts(A, 2) * B + + An = ForwardDiff.Dual{TestTag}.(ForwardDiff.Dual.(rand(3, 4), rand(3, 4)), ForwardDiff.Dual.(rand(3, 4), rand(3, 4))) + Cn = [sum(An[i, p] * B[p, j] for p in 1:4) for i in 1:3, j in 1:2] + @test reinterpret(Float64, An * B) ≈ reinterpret(Float64, Cn) + + # the sizes are those of the `Dual` matrices + @test_throws r"DimensionMismatch.*\(3, ?4\)" mul!(D(2, 3, 2), D(2, 3, 4), rand(5, 2)) + @test_throws r"DimensionMismatch.*\(3, 3\)" mul!(D(2, 3, 3), D(2, 3, 4), rand(4, 2)) + + if ForwardDiff.NANSAFE_MODE_ENABLED + # zero partials times numbers that are not finite stay zero + A, C = ForwardDiff.Dual{TestTag}.(rand(2, 3), 0.0), ForwardDiff.Dual{TestTag}.(rand(2, 2), 0.0) + @test iszero(parts(A * [Inf 1.0; 1.0 1.0; 1.0 1.0], 1)) + @test iszero(parts(mul!(similar(C), A, ones(3, 2), Inf, false), 1)) + @test iszero(parts(mul!(C, A, ones(3, 2), true, Inf), 1)) + end +end + end # module