Skip to content

Why is seeding ydual necessary? #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gdalle opened this issue Apr 7, 2025 · 5 comments · May be fixed by #743
Closed

Why is seeding ydual necessary? #740

gdalle opened this issue Apr 7, 2025 · 5 comments · May be fixed by #743

Comments

@gdalle
Copy link
Member

gdalle commented Apr 7, 2025

There are some lines in the code that I don't understand, essentially all the ones that show up here:
https://github.com/search?q=repo%3AJuliaDiff%2FForwardDiff.jl%20seed!(y&type=code

Why would we need to set the dual numbers inside ydual to a specific value, before overwriting them with f!(ydual, xdual)? Can we just get rid of these steps?

This is causing issues when y has non-bitstype elements, because they may not be initialized the first time that jacobian is called.

Related:

@gdalle
Copy link
Member Author

gdalle commented Apr 7, 2025

MWE of the error:

julia> using ForwardDiff

julia> x = BigFloat[1, 2]
2-element Vector{BigFloat}:
 1.0
 2.0

julia> y = similar(x)
2-element Vector{BigFloat}:
 #undef
 #undef

julia> ForwardDiff.jacobian(copyto!, y, x)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
  [1] getindex
    @ ./essentials.jl:917 [inlined]
  [2] _broadcast_getindex
    @ ./broadcast.jl:644 [inlined]
  [3] _getindex
    @ ./broadcast.jl:674 [inlined]
  [4] _broadcast_getindex
    @ ./broadcast.jl:650 [inlined]
  [5] getindex
    @ ./broadcast.jl:610 [inlined]
  [6] macro expansion
    @ ./broadcast.jl:973 [inlined]
  [7] macro expansion
    @ ./simdloop.jl:77 [inlined]
  [8] copyto!
    @ ./broadcast.jl:972 [inlined]
  [9] copyto!
    @ ./broadcast.jl:925 [inlined]
 [10] materialize!
    @ ./broadcast.jl:883 [inlined]
 [11] materialize!
    @ ./broadcast.jl:880 [inlined]
 [12] seed!(duals::Vector{ForwardDiff.Dual{…}}, x::Vector{BigFloat}, seed::ForwardDiff.Partials{2, BigFloat})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/apiutils.jl:45
 [13] seed!
    @ ~/.julia/packages/ForwardDiff/UBbGT/src/apiutils.jl:45 [inlined]
 [14] vector_mode_dual_eval!
    @ ~/.julia/packages/ForwardDiff/UBbGT/src/apiutils.jl:30 [inlined]
 [15] vector_mode_jacobian(f!::typeof(copyto!), y::Vector{…}, x::Vector{…}, cfg::ForwardDiff.JacobianConfig{…})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/jacobian.jl:139
 [16] jacobian
    @ ~/.julia/packages/ForwardDiff/UBbGT/src/jacobian.jl:40 [inlined]
 [17] jacobian(f!::typeof(copyto!), y::Vector{…}, x::Vector{…}, cfg::ForwardDiff.JacobianConfig{…})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/jacobian.jl:37
 [18] jacobian(f!::typeof(copyto!), y::Vector{BigFloat}, x::Vector{BigFloat})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/jacobian.jl:37
 [19] top-level scope
    @ REPL[5]:1
Some type information was truncated. Use `show(err)` to see complete types.

@devmotion
Copy link
Member

Maybe to zero out partials from possible previous invocations (yduals is taken from the gradient config)?

@gdalle gdalle changed the title Why is seeding y necessary? Why is seeding ydual necessary? Apr 7, 2025
@gdalle
Copy link
Member Author

gdalle commented Apr 7, 2025

Oh right, because if f! doesn't touch some parts of y we want the partials to be zero, and the primals to match whatever was given.

Any idea how to stop it from erroring in the MWE above?

@devmotion
Copy link
Member

Closing as a duplicate of #436.

@devmotion
Copy link
Member

#743 seems to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants