Skip to content

Fix implementation of fused_map_reduce - #171

Merged
odow merged 4 commits into
masterfrom
od/fix-fused-map-reduce
Nov 15, 2022
Merged

odow merged 4 commits into
masterfrom
od/fix-fused-map-reduce

Conversation

@odow

@odow odow commented Nov 7, 2022

Copy link
Copy Markdown
Member

Closes #92

This results in a few more allocations, but it fixes incorrect behavior.

In particular, master currently produces:

julia> x = [5.0, 6.0]
2-element Vector{Float64}:
 5.0
 6.0

julia> y = Vector{Union{Float64,String}}(x)
2-element Vector{Union{Float64, String}}:
 5.0
 6.0

julia> MA.operate(*, x', y)
ERROR: MethodError: no method matching zero(::Type{Union{Float64, String}})
Closest candidates are:
  zero(::Union{Type{P}, P}) where P<:Dates.Period at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/Dates/src/periods.jl:53
  zero(::JuMP.GenericQuadExpr) at /Users/oscar/.julia/packages/JuMP/Z1pVn/src/quad_expr.jl:109
  zero(::JuMP.GenericAffExpr) at /Users/oscar/.julia/packages/JuMP/Z1pVn/src/aff_expr.jl:192
  ...
Stacktrace:
  [1] _instantiate_zero(#unused#::Type{Union{Float64, String}})
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:28
  [2] promote_operation_fallback(op::typeof(*), #unused#::Type{Float64}, #unused#::Type{Union{Float64, String}})
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:51
  [3] promote_operation(::typeof(*), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:113
  [4] promote_operation_fallback(#unused#::typeof(LinearAlgebra.dot), #unused#::Type{Float64}, #unused#::Type{Union{Float64, String}})
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:666
  [5] promote_operation(::typeof(LinearAlgebra.dot), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:113
  [6] promote_map_reduce(::Function, ::Type, ::Type)
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/reduce.jl:32
  [7] fused_map_reduce(::typeof(MutableArithmetics.add_dot), ::Vector{Float64}, ::Vector{Union{Float64, String}})
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/reduce.jl:41
  [8] operate(#unused#::typeof(LinearAlgebra.dot), x::Vector{Float64}, y::Vector{Union{Float64, String}})
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/implementations/LinearAlgebra.jl:486
  [9] operate(#unused#::typeof(*), x::LinearAlgebra.Adjoint{Float64, Vector{Float64}}, y::Vector{Union{Float64, String}})
    @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/implementations/LinearAlgebra.jl:459
 [10] top-level scope
    @ REPL[141]:1

julia> z = [x, x]
2-element Vector{Vector{Float64}}:
 [5.0, 6.0]
 [5.0, 6.0]

julia> MA.operate(LinearAlgebra.dot, z, z)
ERROR: MethodError: promote_operation_fallback(::typeof(LinearAlgebra.dot), ::Type{Vector{Float64}}, ::Type{Vector{Float64}}) is ambiguous. Candidates:
  promote_operation_fallback(op::Function, x::Type{var"#s1"} where var"#s1"<:AbstractArray, y::Type{var"#s2"} where var"#s2"<:AbstractArray) in MutableArithmetics at /Users/oscar/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:15
  promote_operation_fallback(::typeof(LinearAlgebra.dot), ::Type{A}, ::Type{B}) where {A, B} in MutableArithmetics at /Users/oscar/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:661
Possible fix, define
  promote_operation_fallback(::typeof(LinearAlgebra.dot), ::Type{A}, ::Type{B}) where {A<:AbstractArray, B<:AbstractArray}
Stacktrace:
 [1] promote_operation(::typeof(LinearAlgebra.dot), ::Type, ::Type)
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/interface.jl:113
 [2] promote_map_reduce(::Function, ::Type, ::Type)
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/reduce.jl:32
 [3] fused_map_reduce(::typeof(MutableArithmetics.add_dot), ::Vector{Vector{Float64}}, ::Vector{Vector{Float64}})
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/reduce.jl:41
 [4] operate(#unused#::typeof(LinearAlgebra.dot), x::Vector{Vector{Float64}}, y::Vector{Vector{Float64}})
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/maUDe/src/implementations/LinearAlgebra.jl:486
 [5] top-level scope
   @ REPL[143]:1

@odow
odow requested a review from blegat November 7, 2022 21:41
@odow
odow force-pushed the od/fix-fused-map-reduce branch from a9f125e to 07cd38e Compare November 7, 2022 21:56
@odow
odow force-pushed the od/fix-fused-map-reduce branch from 07cd38e to 22ddc6e Compare November 7, 2022 22:00
@codecov

codecov Bot commented Nov 7, 2022 •

Copy link
Copy Markdown

Codecov Report

Base: 83.63% // Head: 83.66% // Increases project coverage by +0.03% 🎉

Coverage data is based on head (c2bde72) compared to base (4dc6932).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #171      +/-   ##
==========================================
+ Coverage   83.63%   83.66%   +0.03%     
==========================================
  Files          20       20              
  Lines        1894     1898       +4     
==========================================
+ Hits         1584     1588       +4     
  Misses        310      310              
Impacted Files Coverage Δ
src/interface.jl 91.72% <100.00%> (+0.19%) ⬆️
src/reduce.jl 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment thread src/reduce.jl Resolved
@blegat

blegat commented Nov 10, 2022

Copy link
Copy Markdown
Member

Can't we just implement promote_operation(::typeof(*), ::Type{Array...}, ::Type{Array...}) ? _accumulator is another solution but it's quite orthogonal to the way the rest of MA works.

@odow

odow commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

Promote doesn't handle the case of Vector{Union{String,Float64}} containing only Float64. LinearAlgebra.dot and Base.:* both work fine, because they deal with the element data at runtime, rather than looking at the container type.

For better or worse, Vector{Union{String,Float64}} shows up in the JuMP documentation, so not handling this case properly is a breaking change.

The current MA works around it by first calling MA.operate!!(MA.add_mul, MA.Zero(), arg) on every argument in *(args...). That causes additional copies, and I think was just added to work-around this particular case.

Co-authored-by: Benoît Legat <[email protected]>
Comment thread src/reduce.jl Resolved
@odow
odow merged commit 201aafb into master Nov 15, 2022
@odow
odow deleted the od/fix-fused-map-reduce branch November 15, 2022 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Fix dot for Array of Arrays

2 participants