JuliaDiff / ReverseDiff.jl

Reverse Mode Automatic Differentiation for Julia
Other
348 stars 56 forks source link

Ambiguity issue with transposed of sparse matrices from SparseArrays.jl #131

Open torfjelde opened 4 years ago

torfjelde commented 4 years ago

Ran into the following issue:

julia> using SparseArrays, ReverseDiff

julia> A
3×1 SparseArrays.SparseMatrixCSC{Bool,Int64} with 1 stored entry:
  [1, 1]  =  1

julia> ReverseDiff.jacobian(x -> A' * x, ones(1))
ERROR: MethodError: *(::LinearAlgebra.Adjoint{Bool,SparseArrays.SparseMatrixCSC{Bool,Int64}}, ::ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}) is ambiguous. Candidates:
  *(adjA::LinearAlgebra.Adjoint{#s662,#s661} where #s661<:AbstractArray{T,2} where #s662, x::AbstractArray{S,1}) where {T, S} in LinearAlgebra at /opt/julia-1.4.0/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:111
  *(x::LinearAlgebra.Adjoint{#s54,#s53} where #s53<:(AbstractArray{T,2} where T) where #s54, y::ReverseDiff.TrackedArray{V,D,1,VA,DA} where DA where VA) where {V, D} in ReverseDiff at /home/tor/.julia/packages/ReverseDiff/uy0uk/src/derivatives/linalg/arithmetic.jl:222
Possible fix, define
  *(::LinearAlgebra.Adjoint{#s54,#s53} where #s53<:AbstractArray{T,2} where #s54, ::ReverseDiff.TrackedArray{V,D,1,VA,DA}) where {V, D, T, VA, DA}
Stacktrace:
 [1] (::var"#15#16")(::ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}) at ./REPL[33]:1
 [2] ReverseDiff.JacobianTape(::var"#15#16", ::Array{Float64,1}, ::ReverseDiff.JacobianConfig{ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}},Nothing}) at /home/tor/.julia/packages/ReverseDiff/uy0uk/src/api/tape.jl:229
 [3] jacobian(::Function, ::Array{Float64,1}, ::ReverseDiff.JacobianConfig{ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}},Nothing}) at /home/tor/.julia/packages/ReverseDiff/uy0uk/src/api/jacobians.jl:23 (repeats 2 times)
 [4] top-level scope at REPL[33]:1

Ran into similar issues with Tracker.jl a good while back and this PR fixed those issues: https://github.com/FluxML/Tracker.jl/pull/37. Related?

mohamed82008 commented 4 years ago

Try adding more methods here https://github.com/JuliaDiff/ReverseDiff.jl/blob/c6ae6ddacbc82ff6a2cbe5bba3ebb0fdba414cf6/src/derivatives/linalg/arithmetic.jl#L193.

mohamed82008 commented 4 years ago

But the problem is ReverseDiff uses similar to allocate for the gradient wrt to the input and similar(sprand(10, 10, 1.0)') gives an empty sparse array. This is probably a bug in SparseArrays.jl.

mohamed82008 commented 4 years ago

similar(sprand(10, 10, 1.0)) gives the expected output.