JuliaDiff / JuliaDiff/ReverseDiff.jl
`MethodError: *(::Diagonal, ::ReverseDiff.TrackedArray)` is ambiguous.
Open
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 393
- Forks
- 60
- Avg merge
- 18h 24m
- Merged PRs (30d)
- 8
Description
I got the following error when using ReverseDiff
using LinearAlgebra, ReverseDiff
n = 30
D = Diagonal([0.1 + 0.9 * (i - 1) / (n - 1) for i = 1:n])
F = x -> sqrt.(D) * (x .- 1)
x = ones(n)
ReverseDiff.gradient(x -> dot(F(x), x), x)
and got the following error
ERROR: MethodError: *(::Diagonal{Float64, Vector{Float64}}, ::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}) is ambiguous. Candidates:
*(x::AbstractMatrix, y::ReverseDiff.TrackedArray{V, D, 1}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
*(x::AbstractArray, y::ReverseDiff.TrackedArray{V, D, 1}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
*(x::AbstractMatrix, y::ReverseDiff.TrackedArray{V, D}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
*(x::AbstractArray, y::ReverseDiff.TrackedArray{V, D}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
*(D::Diagonal, V::AbstractVector) in LinearAlgebra at julia-1.8.0\share\julia\stdlib\v1.8\LinearAlgebra\src\diagonal.jl:242
Possible fix, define
*(::Diagonal, ::ReverseDiff.TrackedArray{V, D, 1}) where {V, D}
Stacktrace:
[1] (::var"#3#4")(x::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}})
@ Main .\REPL[6]:1
[2] (::var"#7#8")(x::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}})
@ Main .\REPL[9]:1
[3] ReverseDiff.GradientTape(f::var"#7#8", input::Vector{Float64}, cfg::ReverseDiff.GradientConfig{ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}})
@ ReverseDiff .julia\packages\ReverseDiff\YkVxM\src\api\tape.jl:199
[4] gradient(f::Function, input::Vector{Float64}, cfg::ReverseDiff.GradientConfig{ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}}) (repeats 2 times)
@ ReverseDiff .julia\packages\ReverseDiff\YkVxM\src\api\gradients.jl:22
[5] top-level scope
@ REPL[9]:1
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
Reproduce the example with LinearAlgebra and ReverseDiff, then inspect src/derivatives/linalg/arithmetic.jl around the reported line 214 methods. Check the dispatch interaction between Diagonal and TrackedArray, and run the relevant linalg tests if available. Done means the example computes a gradient without an ambiguous-method error and existing behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100