Repository navigation
Keep tracked values on the tape in deepcopy - #329
Merged
Merged
Conversation
`deepcopy` recurses with `deepcopy_internal`, so the `Base.deepcopy` method for `TrackedReal` was skipped inside containers and for `TrackedArray`s, whose copies then recorded onto an orphan tape and got zero gradients. Specialise `deepcopy_internal` instead: while its tape is recording, a tracked value is returned as is; afterwards it is copied together with its tape, so copies of tapes stay independent. Copies keep `NULL_TAPE`, and copies of a `CompiledTape` are recompiled, since copied `FunctionWrapper`s still point to the original instructions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #329 +/- ##
==========================================
+ Coverage 94.29% 94.32% +0.03%
==========================================
Files 19 19
Lines 1699 1710 +11
==========================================
+ Hits 1602 1613 +11
Misses 97 97 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
commented
Nov 6, 2026
devmotion
commented
Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #327.
deepcopycallsdeepcopy_internalwhen it recurses, so the existingBase.deepcopy(::TrackedReal)method only applied at the top level. Tracked numbers inside aVector,TupleorNamedTuple, and everyTrackedArray, were copied together with theirInstructionTape. Later operations recorded onto that orphan tape, and the gradients silently came back as zero.Following the
deepcopydocstring, this PR specialisesdeepcopy_internalinstead ofdeepcopy:copy, ForwardDiff (whoseDuals are isbits) and Zygote (where the derivative ofdeepcopyis the identity).deepcopy(tape)stays independent. Returning the value unchanged in all cases would break this, as noted in the issue.NULL_TAPEitself, since ReverseDiff recognises it by identity.CompiledTapeare recompiled. CopiedFunctionWrappers still point to the original tape's instructions, so before this PRdeepcopy(compile(tape))returned zero gradients as well. This affects e.g. AbstractMCMC'sMCMCThreads, which deepcopies models, together with LogDensityProblemsAD's compiled ReverseDiff tapes.deepcopy_internalhas no docstring and is notpublic, but thedeepcopydocstring names it as the method to specialise, so it is added to the ExplicitImports ignore list.The new tests cover the cases from the issue with
gradientand with replays of plain and compiled tapes, plus independence of copied tapes. Without the fix, 17 of the 24 new tests fail.🤖 Generated with Claude Code