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
deepcopy of a GradientConfig keeps the original's tracked input, so the copy returns zero gradients #337
Both should be [2.0, 4.0]. Copies of a JacobianConfig or a HessianConfig return zeros as well.
It comes from deepcopy_internal, which leaves a tracked value as it is while its tape is recording (tracked.jl#L440-L446).
The tape of a config counts as recording between calls (tape.jl#L41, gradients.jl#L44), so f(cfg2.input) gets recorded onto the original tape while the reverse pass runs over the empty tape of the copy (tape.jl#L213-L218).
Those instructions stay on the original tape until its next call replays them.
We ran into this with AbstractMCMC's MCMCThreads, which samples from deepcopies of the model, and a gradient prepared by DifferentiationInterface for AutoReverseDiff().
With a JuliaBUGS model and two chains, the original tape held 85 million instructions after one sample, and the next deepcopy of the model threw a StackOverflowError.
Activity
devmotion commentedon Nov 16, 2026
As in #327 (comment), a general warning: One should be very very careful with using
deepcopyat all in general JuliaLang/julia#42899 (comment)shravanngoswamii commentedon Nov 16, 2026
Agreed. Here it's
MCMCThreadsthat deepcopies the model (sample.jl#L593).devmotion commentedon Nov 16, 2026
Ideally, that would use
copy(which might require a specialization ofcopyfor models).