Skip to content

feat: Cache context support for ReverseDiff.jl - #1093

Draft
franckgaga wants to merge 8 commits into
JuliaDiff:mainfrom
franckgaga:main
Draft

franckgaga wants to merge 8 commits into
JuliaDiff:mainfrom
franckgaga:main

Conversation

@franckgaga

@franckgaga franckgaga commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

It seems to work on my side. Full Disclaimer: vibe-coded with my VS Code agents (Copilot Fusion).

Here's a MWE:

using DifferentiationInterface, ReverseDiff

f(x, cache) = sum(abs2, copyto!(cache, x))

x = [1.0, 2.0]
context = Cache(zeros(2))
backend = AutoReverseDiff()

prep = prepare_gradient(f, backend, x, context)
gradient(f, prep, backend, x, context) # [2.0, 4.0]

Closes #1092

franckgaga and others added 4 commits March 31, 2026 10:45
- Optimize onearg.jl and twoarg.jl for better efficiency
- Enhance utility functions in utils.jl
- Add new tests in test.jl to ensure functionality
@franckgaga
franckgaga requested a review from gdalle as a code owner December 4, 2026 04:47
@franckgaga

Copy link
Copy Markdown
Contributor Author

I'm not a vibe-coder at all normally. I will try to understand the main idea tomorrow and do some cleaning +114 lines is way too much for me, I'm sure it can be more concise.

@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.20779% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.87%. Comparing base (0eee446) to head (c4ee8c2).

Files with missing lines Patch % Lines
...xt/DifferentiationInterfaceReverseDiffExt/utils.jl 77.77% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1093      +/-   ##
==========================================
- Coverage   98.18%   93.87%   -4.32%     
==========================================
  Files         144      141       -3     
  Lines        8265     8258       -7     
==========================================
- Hits         8115     7752     -363     
- Misses        150      506     +356     
Flag Coverage Δ
DI 94.56% <92.20%> (-3.52%) ⬇️
DIT 91.94% <ø> (-6.56%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@gdalle gdalle 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.

I'm not really worried about the length of the PR, it's more about understanding the mechanisms behind it :) thanks for giving this a shot!

_sig = DI.signature(f, backend, x, contexts...; strict)
config = JacobianConfig(x)
return ReverseDiffOneArgJacobianPrep(_sig, config, nothing)
prep_contexts = translate_toprep(eltype(config.input), contexts)

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.

If I understand correctly, the trick is to create an array of the appropriate TrackedReal element type? Shouldn't we rather have an TrackedArray? I'm not sure how these ReverseDiff internals work since they're not documented

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right, it's currently based on Array{TrackedReal}, not a ReverseDiff.TrackedArray. I will modify this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay so it seems that using ReverseDiff.TrackedArray for the cache is "impossible" with the current design in ReverseDiff.jl. In short, the TrackedArray wrapper is immutable and doesn’t support element assignment, see here: https://github.com/JuliaDiff/ReverseDiff.jl/blob/2584eaa222f616b64ebbcb815b7ceae0622da281/src/tracked.jl#L430. Since the whole point of the Cache is to mutate its content, I don't see a workaround. The Copilot neither.

FYI, PreallocationTools.jl also use a Array{TrackedReal} for the cache.

@franckgaga

franckgaga commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

I'm not really worried about the length of the PR, it's more about understanding the mechanisms behind it :) thanks for giving this a shot!

Fully agree ! I'm working on this today. Not easy for me since I'm unfamiliar with 3 layers: ReverseDiff internals, DifferentiationInterface internals and backpropagation.

@gdalle

gdalle commented Dec 4, 2026

Copy link
Copy Markdown
Member

No stress! If you struggle too much, let me know and I'll take over. But I appreciate efforts to develop human expertise 💪

@gdalle
gdalle marked this pull request as draft December 4, 2026 15:46
@franckgaga

Copy link
Copy Markdown
Contributor Author

@gdalle Should I include the include_constantorcachified test suite also ?

This branch has not been deployed

No deployments
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.

Add support for Cache context in AutoReverseDiff ?

2 participants