Skip to content

Conversation

@jumerckx
Copy link
Collaborator

@jumerckx jumerckx commented Jan 7, 2025

fixes #442
needs Enzyme.jl: EnzymeAD/Enzyme.jl#2254

I had to introduce a new function call_with_reactant_within_autodiff to smuggle the within_autodiff in the call_with_reactant_generator through the self argument.
I also tried doing things through set_reactant_abi but that didn't seem to suffice (first commit).
Perhaps the extra code in set_reactant_abi isn't strictly necessary now so I can try removing it again if wanted.

@avik-pal avik-pal force-pushed the jm/deferred_within_autodiff branch 2 times, most recently from dd14b85 to 8731c7f Compare September 1, 2025 23:13
@avik-pal avik-pal requested a review from wsmoses September 1, 2025 23:41

if isempty(kwargs)
Reactant.call_with_reactant(f, traced_args...)
if within_autodiff
Copy link
Member

Choose a reason for hiding this comment

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

I feel like a cleaner way to do this, is not to have a second interpreter. But instead we can create a new global ref set to false, and overlay within_autodiff to lookup that var, and during autodiff set that to true

Copy link
Member

Choose a reason for hiding this comment

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

I'm okay with this though, but if we were to do it in this form, I would probably change call_with_reactant to take a config type var, which stores the current state of whether in autodiff or not (and also we can extend to other things down the line as well)

jumerckx and others added 5 commits September 1, 2025 20:18
… === overload_autodiff`.

This doesn't work for some reason, the function within overload autodiff uses the original interpreter (?)
…mlir_fn. In order to pass this information from make_mlir_fn to call_with_reactant_generator, I introduced a new function `call_with_reactant_within_autodiff` which allows detection by looking at `self`.
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@avik-pal avik-pal force-pushed the jm/deferred_within_autodiff branch from a3115b5 to 2a4d0f2 Compare September 2, 2025 00:18
@codecov
Copy link

codecov bot commented Sep 2, 2025

Codecov Report

❌ Patch coverage is 78.57143% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.56%. Comparing base (71b744e) to head (2a4d0f2).

Files with missing lines Patch % Lines
src/utils.jl 50.00% 2 Missing ⚠️
src/TracedUtils.jl 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #490      +/-   ##
==========================================
+ Coverage   42.55%   42.56%   +0.01%     
==========================================
  Files         123      123              
  Lines       21816    21826      +10     
==========================================
+ Hits         9283     9290       +7     
- Misses      12533    12536       +3     

☔ View full report in Codecov by Sentry.
📢 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.

@avik-pal
Copy link
Collaborator

julia> using Enzyme

julia> function error_not_within_autodiff()
           !Enzyme.within_autodiff() && error("Not within autodiff")
           return nothing
       end
error_not_within_autodiff (generic function with 1 method)

julia> fwd_within_autodiff(Mode, RT) = Enzyme.autodiff(Mode, error_not_within_autodiff, RT)
fwd_within_autodiff (generic function with 1 method)

julia> error_not_within_autodiff()
ERROR: Not within autodiff
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] error_not_within_autodiff()
   @ Main ./REPL[5]:2
 [3] top-level scope
   @ REPL[7]:1
 [4] top-level scope
   @ none:1

julia> fwd_within_autodiff(Forward, Const)
()

julia> error_not_within_autodiff()

julia> Enzyme.within_autodiff()
false

I am extremely confused why is the 2nd call not throw an error here. Only happens if I call fwd_within_autodiff in between. cc @wsmoses this is in isolation from Reactant

@wsmoses
Copy link
Member

wsmoses commented Sep 13, 2025

@vchuravy er wat

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.

Enzyme.within_autodiff returns true inside compile

4 participants