Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mapreduce on AdjOrTrans #46605

Merged
merged 8 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
restrict to commutative ops, extend tests
  • Loading branch information
dkarrasch committed Sep 7, 2022
commit 858e4a9b51474b2d2969e2aca3ace235991d9066
38 changes: 21 additions & 17 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,31 @@ Broadcast.broadcast_preserving_zero_d(f, tvs::Union{Number,TransposeAbsVec}...)


### reductions
# faster to sum the Array than to work through the wrapper
Base._mapreduce_dim(f, op, init::Base._InitialValue, A::Transpose, dims::Colon) =
# faster to sum the Array than to work through the wrapper (but only in commutative reduction ops)
const CommutativeOps = Union{typeof(+),typeof(Base.add_sum),typeof(-),typeof(min),typeof(max),typeof(|),typeof(&)}
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved
Base._mapreduce_dim(f, op::CommutativeOps, init::Base._InitialValue, A::Transpose, dims::Colon) =
transpose(Base._mapreduce_dim(_sandwich(transpose, f), _sandwich(transpose, op), init, parent(A), dims))
Base._mapreduce_dim(f, op, init::Base._InitialValue, A::Adjoint, dims::Colon) =
Base._mapreduce_dim(f, op::CommutativeOps, init::Base._InitialValue, A::Adjoint, dims::Colon) =
adjoint(Base._mapreduce_dim(_sandwich(adjoint, f), _sandwich(adjoint, op), init, parent(A), dims))
N5N3 marked this conversation as resolved.
Show resolved Hide resolved
Base._mapreduce_dim(f, op::Union{typeof(*),typeof(Base.mul_prod)}, init::Base._InitialValue, A::Transpose{<:Union{Real,Complex}}, dims::Colon) =
transpose(Base._mapreduce_dim(_sandwich(transpose, f), _sandwich(transpose, op), init, parent(A), dims))
Base._mapreduce_dim(f, op::Union{typeof(*),typeof(Base.mul_prod)}, init::Base._InitialValue, A::Adjoint{<:Union{Real,Complex}}, dims::Colon) =
adjoint(Base._mapreduce_dim(_sandwich(adjoint, f), _sandwich(adjoint, op), init, parent(A), dims))
# count allows for optimization only if the parent array has Bool eltype
Base._count(::typeof(identity), A::Transpose{Bool}, ::Colon, init) = Base._count(identity, parent(A), :, init)
Base._count(::typeof(identity), A::Adjoint{Bool}, ::Colon, init) = Base._count(identity, parent(A), :, init)
Base._any(f, A::Transpose, ::Colon) = Base._any(f∘transpose, parent(A), :)
Base._any(f, A::Adjoint, ::Colon) = Base._any(f∘adjoint, parent(A), :)
Base._all(f, A::Transpose, ::Colon) = Base._all(f∘transpose, parent(A), :)
Base._all(f, A::Adjoint, ::Colon) = Base._all(f∘adjoint, parent(A), :)
# sum(A'; dims)
Base.mapreducedim!(f, op, B::AbstractArray, A::TransposeAbsMat) =
Base.mapreducedim!(f, op::CommutativeOps, B::AbstractArray, A::TransposeAbsMat) =
transpose(Base.mapreducedim!(_sandwich(transpose, f), _sandwich(transpose, op), transpose(B), parent(A)))
Base.mapreducedim!(f, op::CommutativeOps, B::AbstractArray, A::AdjointAbsMat) =
adjoint(Base.mapreducedim!(_sandwich(adjoint, f), _sandwich(adjoint, op), adjoint(B), parent(A)))
Base.mapreducedim!(f, op::Union{typeof(*),typeof(Base.mul_prod)}, B::AbstractArray, A::TransposeAbsMat{<:Union{Real,Complex}}) =
transpose(Base.mapreducedim!(_sandwich(transpose, f), _sandwich(transpose, op), transpose(B), parent(A)))
Base.mapreducedim!(f, op, B::AbstractArray, A::AdjointAbsMat) =
Base.mapreducedim!(f, op::Union{typeof(*),typeof(Base.mul_prod)}, B::AbstractArray, A::AdjointAbsMat{<:Union{Real,Complex}}) =
adjoint(Base.mapreducedim!(_sandwich(adjoint, f), _sandwich(adjoint, op), adjoint(B), parent(A)))

_sandwich(adj::Function, fun) = (xs...,) -> adj(fun(map(adj, xs)...))
Expand Down Expand Up @@ -452,15 +468,3 @@ pinv(v::TransposeAbsVec, tol::Real = 0) = pinv(conj(v.parent)).parent
## complex conjugate
conj(A::Transpose) = adjoint(A.parent)
conj(A::Adjoint) = transpose(A.parent)

## reductions
for (ttype, transform) in ((:Adjoint, adjoint), (:Transpose, transpose))
@eval _mapreduce(f, op, ::IndexStyle, A::$ttype) =
_mapreduce(f∘$transform, op, IndexStyle(parent(A)), parent(A))
@eval _mapreduce_dim(f, op, ::Base._InitialValue, A::$ttype, ::Colon) =
_mapreduce(f∘$transform, op, IndexStyle(parent(A)), parent(A))
@eval Base._count(f, A::$ttype, ::Colon, init) =
Base._count(f∘$transform, parent(A), :, init)
@eval Base._any(f, A::$ttype, ::Colon) = Base._any(f∘$transform, parent(A), :)
@eval Base._all(f, A::$ttype, ::Colon) = Base._all(f∘$transform, parent(A), :)
end
58 changes: 41 additions & 17 deletions stdlib/LinearAlgebra/test/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -589,23 +589,47 @@ end
end

@testset "reductions: $adjtrans" for adjtrans in [transpose, adjoint]
mat = rand(ComplexF64, 3,5)
@test sum(adjtrans(mat)) ≈ sum(collect(adjtrans(mat)))
@test sum(adjtrans(mat), dims=1) ≈ sum(collect(adjtrans(mat)), dims=1)
@test sum(adjtrans(mat), dims=(1,2)) ≈ sum(collect(adjtrans(mat)), dims=(1,2))

@test sum(imag, adjtrans(mat)) ≈ sum(imag, collect(adjtrans(mat)))
@test sum(imag, adjtrans(mat), dims=1) ≈ sum(imag, collect(adjtrans(mat)), dims=1)

mat = [rand(ComplexF64,2,2) for _ in 1:3, _ in 1:5]
@test sum(adjtrans(mat)) ≈ sum(collect(adjtrans(mat)))
@test sum(adjtrans(mat), dims=1) ≈ sum(collect(adjtrans(mat)), dims=1)
@test sum(adjtrans(mat), dims=(1,2)) ≈ sum(collect(adjtrans(mat)), dims=(1,2))

@test sum(imag, adjtrans(mat)) ≈ sum(imag, collect(adjtrans(mat)))
@test sum(x -> x[1,2], adjtrans(mat)) ≈ sum(x -> x[1,2], collect(adjtrans(mat)))
@test sum(imag, adjtrans(mat), dims=1) ≈ sum(imag, collect(adjtrans(mat)), dims=1)
@test sum(x -> x[1,2], adjtrans(mat), dims=1) ≈ sum(x -> x[1,2], collect(adjtrans(mat)), dims=1)
for (reduction, reduction!, op) in ((sum, sum!, +), (prod, prod!, *), (minimum, minimum!, min), (maximum, maximum!, max))
T = op in (max, min) ? Float64 : ComplexF64
mat = rand(T, 3,5)
rd1 = zeros(T, 1, 3)
rd2 = zeros(T, 5, 1)
rd3 = zeros(T, 1, 1)
@test reduction(adjtrans(mat)) ≈ reduction(copy(adjtrans(mat)))
@test reduction(adjtrans(mat), dims=1) ≈ reduction(copy(adjtrans(mat)), dims=1)
@test reduction(adjtrans(mat), dims=2) ≈ reduction(copy(adjtrans(mat)), dims=2)
@test reduction(adjtrans(mat), dims=(1,2)) ≈ reduction(copy(adjtrans(mat)), dims=(1,2))

@test reduction!(rd1, adjtrans(mat)) ≈ reduction!(rd1, copy(adjtrans(mat)))
@test reduction!(rd2, adjtrans(mat)) ≈ reduction!(rd2, copy(adjtrans(mat)))
@test reduction!(rd3, adjtrans(mat)) ≈ reduction!(rd3, copy(adjtrans(mat)))

@test reduction(imag, adjtrans(mat)) ≈ reduction(imag, copy(adjtrans(mat)))
@test reduction(imag, adjtrans(mat), dims=1) ≈ reduction(imag, copy(adjtrans(mat)), dims=1)
@test reduction(imag, adjtrans(mat), dims=2) ≈ reduction(imag, copy(adjtrans(mat)), dims=2)
@test reduction(imag, adjtrans(mat), dims=(1,2)) ≈ reduction(imag, copy(adjtrans(mat)), dims=(1,2))

@test Base.mapreducedim!(imag, op, rd1, adjtrans(mat)) ≈ Base.mapreducedim!(imag, op, rd1, copy(adjtrans(mat)))
@test Base.mapreducedim!(imag, op, rd2, adjtrans(mat)) ≈ Base.mapreducedim!(imag, op, rd2, copy(adjtrans(mat)))
@test Base.mapreducedim!(imag, op, rd3, adjtrans(mat)) ≈ Base.mapreducedim!(imag, op, rd3, copy(adjtrans(mat)))

op in (max, min) && continue
mat = [rand(T,2,2) for _ in 1:3, _ in 1:5]
rd1 = fill(zeros(T, 2, 2), 1, 3)
rd2 = fill(zeros(T, 2, 2), 5, 1)
rd3 = fill(zeros(T, 2, 2), 1, 1)
@test reduction(adjtrans(mat)) ≈ reduction(copy(adjtrans(mat)))
@test reduction(adjtrans(mat), dims=1) ≈ reduction(copy(adjtrans(mat)), dims=1)
@test reduction(adjtrans(mat), dims=2) ≈ reduction(copy(adjtrans(mat)), dims=2)
@test reduction(adjtrans(mat), dims=(1,2)) ≈ reduction(copy(adjtrans(mat)), dims=(1,2))

@test reduction(imag, adjtrans(mat)) ≈ reduction(imag, copy(adjtrans(mat)))
@test reduction(x -> x[1,2], adjtrans(mat)) ≈ reduction(x -> x[1,2], copy(adjtrans(mat)))
@test reduction(imag, adjtrans(mat), dims=1) ≈ reduction(imag, copy(adjtrans(mat)), dims=1)
@test reduction(x -> x[1,2], adjtrans(mat), dims=1) ≈ reduction(x -> x[1,2], copy(adjtrans(mat)), dims=1)
end
Ac = [1 2; 3 4]'
@test mapreduce(identity, (x, y) -> 10x+y, copy(Ac)) == mapreduce(identity, (x, y) -> 10x+y, Ac) == 1234
end

end # module TestAdjointTranspose