Skip to content

Method ambiguities with SparseArrays.jl #278

@penelopeysm

Description

@penelopeysm

MWE:

using ReverseDiff
function f2(x)
    xv = @view x[1:end-1]
    xend = x[end]
    x = [xv; xend]
    return sum(x)
end
ReverseDiff.gradient(f2, rand(10)) # works

using SparseArrays
ReverseDiff.gradient(f2, rand(10)) # fails
#=
ERROR: MethodError: vcat(::SubArray{…}, ::ReverseDiff.TrackedReal{…}) is ambiguous.

Candidates:
  vcat(var"389"::AbstractVector, x::ReverseDiff.TrackedReal)
    @ ReverseDiff ~/.julia/packages/ReverseDiff/rKZaG/src/derivatives/arrays.jl:41
  vcat(X1::Union{Number, AbstractVecOrMat{<:Number}}, X::Union{Number, AbstractVecOrMat{<:Number}}...)
    @ SparseArrays ~/.julia/juliaup/julia-1.12.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.12/SparseArrays/src/sparsevector.jl:1275

Possible fix, define
  vcat(::AbstractVector{<:Number}, ::ReverseDiff.TrackedReal)
=#

The issues caused by the SparseArrays.jl definitions of hcat/vcat are I think well known (JuliaSparse/SparseArrays.jl#431) but what I'm seeing from is that the solution for now is to define the necessary methods in ReverseDiff (?).

I think just adding :(AbstractVector{<:Number}) (and :(AbstractMatrix{<:Number})) to this line should fix it?

for i = 0:2, c = combinations([:AbstractVector, :Vector, :TrackedVector, :AbstractMatrix, :TrackedMatrix, :Number, :TrackedReal], i)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions