JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
892 stars 145 forks source link

DiffResults objects are not re-aliased properly #696

Open gdalle opened 8 months ago

gdalle commented 8 months ago

The DiffResults documentation clearly states that whenever a DiffResult object is used, it must be realiased at the end of the function call:

result = do_stuff!(result, args...)

See for instance the docstrings in https://juliadiff.org/DiffResults.jl/stable/#Mutating-a-DiffResult, or the issue DiffResults#17

This is not done by ForwardDiff, for instance here:

https://github.com/JuliaDiff/ForwardDiff.jl/blob/42b1554493f04a7e3d2606f328e2a0e365bf8f69/src/gradient.jl#L35-L44

Am I right in deducing that it can lead to incorrectness?

gdalle commented 8 months ago

See also https://github.com/JuliaDiff/ReverseDiff.jl/issues/251

gdalle commented 8 months ago

I can't manage to get an actual bug out of this, presumably because the StaticArrays extension of ForwardDiff overloads the vulnerable parts I've tried. But for ReverseDiff this does lead to wrong behavior