You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DiffResults objects are not re-aliased properly #251
Fixed by #295, which realiases the whole chain — gradients.jl, jacobians.jl, hessians.jl, tape.jl and utils.jl.
The rebind has to be unbroken. The leaf extract_result!/extract_result_value! methods in utils.jl already realiased correctly, but every caller discarded the result, so patching any single layer leaves the bug in place.
Tuple results needed a slightly different fix: those methods looped over result[i], which is not assignable, so the returned struct had nowhere to go. They now map over the tuple instead, which also removes the pattern that allowed the bug — map has no place to drop a return value. The result and input tuples are constrained to a common NTuple{N,Any} so a mismatched arity is a dispatch error rather than a silent truncation. Allocations and inference are unchanged.
f(x, y) =sum(abs2, x) +sum(abs2, y)
x, y =MVector{2}(3.0, 5.0), MVector{2}(2.0, 4.0)
res = (DiffResults.GradientResult(MVector(x)), DiffResults.GradientResult(MVector(y)))
res = ReverseDiff.gradient!(res, f, (x, y))
map(DiffResults.value, res) # was (3.0, 2.0), now (54.0, 54.0)map(DiffResults.gradient, res) # ([6.0, 10.0], [4.0, 8.0])
Activity
gdalle commentedon Mar 15, 2024
See also JuliaDiff/ForwardDiff.jl#696
gdalle commentedon Mar 15, 2024
Here's an MWE demonstrating the bug:
Realias `result` on the Jacobian, Hessian and tuple paths (fixes #251)
devmotion commentedon Sep 16, 2026
Fixed by #295, which realiases the whole chain —
gradients.jl,jacobians.jl,hessians.jl,tape.jlandutils.jl.The rebind has to be unbroken. The leaf
extract_result!/extract_result_value!methods inutils.jlalready realiased correctly, but every caller discarded the result, so patching any single layer leaves the bug in place.Tuple results needed a slightly different fix: those methods looped over
result[i], which is not assignable, so the returned struct had nowhere to go. They nowmapover the tuple instead, which also removes the pattern that allowed the bug —maphas no place to drop a return value. The result and input tuples are constrained to a commonNTuple{N,Any}so a mismatched arity is a dispatch error rather than a silent truncation. Allocations and inference are unchanged.Realias `result` on the gradient path (fixes #269)
Realias `result` on the Jacobian, Hessian and tuple paths (fixes #251)