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
Add a scratch argument to prepare for storage the problem writes into - #189
JuliaBUGS's generated log density writes each parameter into its evaluation environment and reads it back, and Enzyme returns a wrong gradient with no error when that storage reaches it as a constant (TuringLang/JuliaBUGS.jl#549). TuringLang/JuliaBUGS.jl#555 passes the environment as scratch once this is released.
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.32%. Comparing base (3c2b770) to head (d7b628c). ⚠️ Report is 1 commits behind head on main.
Validate returned evaluator cache before ForwardDiff invocation
ext/AbstractPPLForwardDiffExt.jl:69
This checks only the caller's keyword, not the VectorEvaluator returned by structural prepare. A downstream prepare(problem, x; ...) can return an evaluator with its own non-empty cache even when this keyword is empty; the probe then succeeds, but _fd_call later omits that cache and invokes f with the wrong arity. Reject evaluator.cache as well (or consistently thread it through the ForwardDiff target).
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @shravanngoswamii -- this looks good and fixes the JuliaBUGS case. Please find some suggestions below. Two edits sit outside the diff, so they're code blocks in the comments.
One upstream thing is worth knowing. DI's Enzyme extension builds Cache/ConstantOrCache shadows once in prepare_gradient (make_context_shadows), and _translate_prepared! reuses them without zeroing, so derivative state carries across calls:
import DifferentiationInterface as DI
using Enzyme: Enzyme
f(x, w) = (s = w[1]; w[1] = x[1]; s + w[1]^2) # ∂f/∂x = 2x[1]
backend = DI.AutoEnzyme(; mode=Enzyme.Reverse)
w = [0.0]
prep = DI.prepare_gradient(f, backend, [1.0], DI.ConstantOrCache(w))
for _ in1:3
w[1] =0.0@show DI.gradient(f, prep, backend, [1.0], DI.ConstantOrCache(w))
end# [2.0], [3.0], [3.0]
Thanks, Shravan. This looks quite nice. One suggestion: I'd rename cache to scratch before 0.15.6 is tagged.
cache is already taken in this package. Prepared.cache holds the backend's AD preparation, and the test harness has a :cache_reuse tag for exercising it. After this PR, p.cache and p.evaluator.cache would sit side by side and mean quite different things, which is a fine way to confuse the next reader.
The contract also points to a different name. Every written slot must be written before it is read, on every call; Enzyme relies on that. That's scratch storage. A cache is the opposite: something you expect to find still there next time.
shravanngoswamii
changed the title
Add a cache argument to prepare for storage the problem writes into
Add a scratch argument to prepare for storage the problem writes into
Oct 1, 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
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.
JuliaBUGS's generated log density writes each parameter into its evaluation environment and reads it back, and Enzyme returns a wrong gradient with no error when that storage reaches it as a constant (TuringLang/JuliaBUGS.jl#549).
TuringLang/JuliaBUGS.jl#555 passes the environment as
scratchonce this is released.