Skip to content

Add a scratch argument to prepare for storage the problem writes into - #189

Merged
shravanngoswamii merged 21 commits into
mainfrom
sg/prepare-cache
Oct 2, 2026
Merged

shravanngoswamii merged 21 commits into
mainfrom
sg/prepare-cache

Conversation

@shravanngoswamii

@shravanngoswamii shravanngoswamii commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

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.

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ 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.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #189      +/-   ##
==========================================
+ Coverage   92.21%   92.32%   +0.10%     
==========================================
  Files          17       17              
  Lines        2106     2162      +56     
==========================================
+ Hits         1942     1996      +54     
- Misses        164      166       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

AbstractPPL.jl documentation for PR #189 is available at:
https://TuringLang.github.io/AbstractPPL.jl/previews/PR189/

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The DifferentiationInterface compatibility range includes releases that do not provide ConstantOrCache.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Adds writable evaluator caches so AD can track values stored and later read by probabilistic programs.

Changes:

  • Threads cache through vector evaluators and AD backends.
  • Supports Mooncake and Enzyme; rejects ForwardDiff caches.
  • Adds documentation and conformance tests.
File Description
src/​evaluators/​Evaluators.jl Defines the cache API and evaluator plumbing.
src/​AbstractPPL.jl Documents the cache test group.
ext/​AbstractPPLDifferentiationInterfaceExt.jl Passes caches as ConstantOrCache.
ext/​AbstractPPLMooncakeExt.jl Integrates caches with Mooncake targets.
ext/​AbstractPPLForwardDiffExt.jl Rejects unsupported caches.
ext/​AbstractPPLTestExt.jl Adds cache conformance cases.
test/​evaluators/​Evaluators.jl Tests core plumbing and compatibility.
test/​ext/​differentiationinterface/​main.jl Tests Enzyme cache gradients.
test/​ext/​differentiationinterface/​Project.toml Adds Enzyme test dependencies.
test/​ext/​mooncake/​main.jl Tests both Mooncake modes.
test/​ext/​forwarddiff/​main.jl Tests cache rejection.
docs/​src/​evaluators.md Documents writable storage usage.
HISTORY.md Records the new API.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

ForwardDiff validates only the supplied keyword and can mishandle a cache introduced by structural preparation.

Review effort: Balanced
Findings: None

Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity 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).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Compiled ReverseDiff accepts caches but bypasses the required ConstantOrCache argument path.

Review effort: Balanced
Findings: 1 High severity

Open (1)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation consistently preserves structural evaluator fields, enforces backend limitations, and covers cache reuse and context overrides.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@sunxd3 sunxd3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks right to me

@yebai yebai left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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 _ in 1:3
    w[1] = 0.0
    @show DI.gradient(f, prep, backend, [1.0], DI.ConstantOrCache(w))
end
# [2.0], [3.0], [3.0]

Comment thread HISTORY.md Resolved
Comment thread docs/src/evaluators.md Resolved
Comment thread src/evaluators/Evaluators.jl Resolved
Comment thread ext/AbstractPPLMooncakeExt.jl Resolved
Comment thread ext/AbstractPPLMooncakeExt.jl Resolved
Comment thread ext/AbstractPPLMooncakeExt.jl Resolved
Comment thread test/ext/mooncake/main.jl Resolved
Comment thread ext/AbstractPPLForwardDiffExt.jl Resolved
Comment thread ext/AbstractPPLMooncakeExt.jl Resolved
Comment thread ext/AbstractPPLTestExt.jl Resolved
Comment thread src/evaluators/Evaluators.jl Resolved
@yebai

yebai commented Oct 1, 2026

Copy link
Copy Markdown
Member

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 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
@shravanngoswamii

Copy link
Copy Markdown
Member Author

Renamed to scratch, thank you @yebai. Let me know if you have any other suggestions.

@shravanngoswamii
shravanngoswamii merged commit d6d8257 into main Oct 2, 2026
15 checks passed
@shravanngoswamii
shravanngoswamii deleted the sg/prepare-cache branch October 2, 2026 06:56
shravanngoswamii added a commit to TuringLang/JuliaBUGS.jl that referenced this pull request Oct 3, 2026
…d mode (#555)

Fixes #549.

This uses the `scratch` argument to `prepare` from
TuringLang/AbstractPPL.jl#189, released in AbstractPPL 0.15.6.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants