JuliaDiff / JuliaDiff/ReverseDiff.jl

Method ambiguities with SparseArrays.jl

Open
#278 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
393
Forks
60
Avg merge
18h 24m
Merged PRs (30d)
8

Description

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 (https://github.com/JuliaSparse/SparseArrays.jl/issues/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?

https://github.com/JuliaDiff/ReverseDiff.jl/blob/7cc65886962c82973e5d3877d8d05c3722063dd9/src/derivatives/arrays.jl#L36

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the ambiguity with the MWE in the issue, then inspect the hcat/vcat derivative definitions around src/derivatives/arrays.jl:36. Confirm the relevant method signatures and check that the gradient call completes without a MethodError; the issue is done when this SparseArrays combination dispatches unambiguously.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.