JuliaDiff / JuliaDiff/ReverseDiff.jl
Method ambiguities with SparseArrays.jl
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?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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