JuliaDiff / JuliaDiff/ForwardDiff.jl

Derivative issue with DiffResults and nested arrays

Open
#725 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

The following code works:

using ForwardDiff

g(x) = [[x^2, sqrt(x)]]
println(ForwardDiff.derivative(g, 2))

But this code:

using DiffResults
using ForwardDiff

g(x) = [[x^2, sqrt(x)]]
result = DiffResults.DiffResult(similar(g(2)), similar(g(2)))
result = ForwardDiff.derivative!(result, g, 2)
println(result)

Leads to a runtime error:

ERROR: LoadError: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1})

Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
   @ Base rounding.jl:207
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  Float64(::IrrationalConstants.Fourπ)
   @ IrrationalConstants C:\Users\bruno\.julia\packages\IrrationalConstants\vp5v4\src\macro.jl:112
  ...

Stacktrace:
  [1] convert(::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1})
    @ Base .\number.jl:7
  [2] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1}, i1::Int64)
    @ Base .\array.jl:1021
  [3] _unsafe_copyto!(dest::Vector{Float64}, doffs::Int64, src::Vector{ForwardDiff.Dual{…}}, soffs::Int64, n::Int64)
    @ Base .\array.jl:299
  [4] unsafe_copyto!
    @ .\array.jl:353 [inlined]
  [5] _copyto_impl!
    @ .\array.jl:376 [inlined]
  [6] copyto!
    @ .\array.jl:363 [inlined]
  [7] copyto!
    @ .\array.jl:385 [inlined]
  [8] copyto_axcheck!
    @ .\abstractarray.jl:1177 [inlined]
  [9] Vector{Float64}(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1}})
    @ Base .\array.jl:673
 [10] convert
    @ .\array.jl:665 [inlined]
 [11] setindex!
    @ .\array.jl:1021 [inlined]
 [12] map!
    @ .\abstractarray.jl:3279 [inlined]
 [13] value!
    @ C:\Users\bruno\.julia\packages\DiffResults\YLo25\src\DiffResults.jl:167 [inlined]
 [14] extract_value!
    @ C:\Users\bruno\.julia\packages\ForwardDiff\UBbGT\src\apiutils.jl:5 [inlined]
 [15] derivative!(result::DiffResults.MutableDiffResult{1, Vector{Vector{…}}, Tuple{Vector{…}}}, f::typeof(g), x::Int64)
    @ ForwardDiff C:\Users\bruno\.julia\packages\ForwardDiff\UBbGT\src\derivative.jl:49

Adding the following line fixes this problem:

ForwardDiff.value(::Type{T}, x::AbstractArray) where T = map(d -> ForwardDiff.value(T, d), x)

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

Reproduce the two examples in the issue and compare the stack-trace entries in DiffResults.jl:167, apiutils.jl:5, and derivative.jl:49. Inspect how nested arrays are handled by value! and extract_value!, then confirm that derivative! completes for g(x) without the reported conversion error.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.