Skip to content

Why is seeding ydual necessary? #740

Closed

Description

@gdalle

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:

Activity

  1. gdalle commented on Apr 7, 2025

    @gdalle
    MemberAuthor

    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.
  2. devmotion commented on Apr 7, 2025

    @devmotion
    Member

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

  3. changed the title [-]Why is seeding `y` necessary?[/-] [+]Why is seeding `ydual` necessary?[/+] on Apr 7, 2025
  4. gdalle commented on Apr 7, 2025

    @gdalle
    MemberAuthor

    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?

  5. devmotion commented on Apr 7, 2025

    @devmotion
    Member

    Closing as a duplicate of #436.

  6. devmotion commented on Apr 7, 2025

    @devmotion
    Member

    #743 seems to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions