Skip to content

feat: Add reverse/forward counterparts#1036

Merged
gdalle merged 4 commits into
JuliaDiff:mainfrom
rsenne:forward-reverse-helpers
Jul 7, 2026
Merged

feat: Add reverse/forward counterparts#1036
gdalle merged 4 commits into
JuliaDiff:mainfrom
rsenne:forward-reverse-helpers

Conversation

@rsenne

@rsenne rsenne commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

PR to add the forward/backward counterparts discussed. I wasn't sure if for a generic backend if there was no reasonable counterpart to return e.g., if one did reverse_counterpart(AutoForwardDiff) if the code should throw an ArgumentError or if it should just return the backend. I opted for the former

Resolves #1025.

@rsenne rsenne requested a review from gdalle as a code owner June 30, 2026 02:01
@rsenne rsenne changed the title Add reverse/forward counterparts feat: Add reverse/forward counterparts Jun 30, 2026
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.94%. Comparing base (5572e56) to head (1802709).

Files with missing lines Patch % Lines
DifferentiationInterface/src/misc/zero_backends.jl 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1036      +/-   ##
==========================================
+ Coverage   98.21%   98.94%   +0.72%     
==========================================
  Files         138      117      -21     
  Lines        8131     5955    -2176     
==========================================
- Hits         7986     5892    -2094     
+ Misses        145       63      -82     
Flag Coverage Δ
DI 98.94% <96.15%> (-0.03%) ⬇️
DIT ?

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.

Thank you for getting this started!
Most test failures are a red herring (test CI runs by forcing the highest possible version of each package, which here leads to incompatibilities between the recent OrderedCollections v2.0 and the not-yet-updated DataFrames)

Comment thread DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/counterparts.jl Outdated
Comment thread DifferentiationInterface/src/utils/counterparts.jl Outdated
Comment thread DifferentiationInterface/src/utils/counterparts.jl Outdated
Comment thread DifferentiationInterface/test/Core/Internals/backends.jl
Comment thread DifferentiationInterface/test/Back/Mooncake/test.jl
Comment thread DifferentiationInterface/test/Back/Enzyme/test.jl
@rsenne

rsenne commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

I think I addressed everything:

  • We now emit a warning (and return the backend unchanged) instead of throwing an error
  • Added a testing recipe (test_counterparts) in DIT that I now call in each backend's tests
  • Moved the Mooncake counterparts into the extension
  • Tested that the Mooncake config is carried over to the counterpart
  • Tested that the Enzyme mode attributes are preserved across the flip, plus the <:ReverseMode signature so extra type params won't break DI
  • Generic fallbacks now also cover ForwardOrReverseMode and SymbolicMode

Let me know if anything needs further adjustment!

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

Almost there!

Comment thread DifferentiationInterface/src/utils/counterparts.jl Outdated
Comment thread DifferentiationInterface/src/utils/counterparts.jl Outdated
Comment thread DifferentiationInterface/src/utils/counterparts.jl Outdated
Comment thread DifferentiationInterface/test/Core/Internals/backends.jl Outdated
Comment thread DifferentiationInterfaceTest/src/tests/counterparts.jl
Comment thread DifferentiationInterfaceTest/src/DifferentiationInterfaceTest.jl
@rsenne rsenne requested a review from gdalle July 3, 2026 00:19
@rsenne

rsenne commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

okay think i got everything again

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

LGTM except

  • the warnings to remove
  • the test failures for Zygote and ReverseDiff
  • the missing coverage

Comment thread DifferentiationInterface/src/utils/counterparts.jl Outdated
Comment thread DifferentiationInterface/src/utils/counterparts.jl Outdated
Comment thread DifferentiationInterfaceTest/test/zero_backends.jl

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

Thanks, sorry for the long-winded review!

@gdalle gdalle merged commit 233ba49 into JuliaDiff:main Jul 7, 2026
62 of 77 checks passed
function DI.forward_counterpart(
::AutoEnzyme{
<:ReverseMode{
ReturnPrimal, RuntimeActivity, StrongZero, ABI, Holomorphic, ErrIfFuncWritten,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this unquestionably uses internal apis and is subject to breakage.

@gdalle / @rsenne instead can I recommend you add a version of this in enzymecore [and then call that version here]. That way it won't get broken if/when we add/change type parameters to the mode

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.

I can author an upstream PR -- that work for you @gdalle?

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.

Yeah I have already mentioned it above in the PR, but DI uses these internals at other points cause it definitely needs them. We'll upstream all we can to EnzymeCore, feel free to get the ball rolling @rsenne and I'll follow up

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.

Backend counterparts

3 participants