Skip to content

Conversation

@avik-pal
Copy link
Collaborator

@avik-pal avik-pal commented Sep 11, 2025

I know @ChrisRackauckas will like this 😅

@avik-pal avik-pal requested a review from wsmoses September 11, 2025 19:29

@reactant_overlay function Zygote.gradient(f::F, args...) where {F}
# TODO: check `f` as well once #1642 is merged
if use_overlayed_version(args)
Copy link
Member

Choose a reason for hiding this comment

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

If we do this, we should quite aggressively yell that we're gonig to do this -- I would even be okay saying to do this for each call [not even each callsite]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

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 also kind of debating if we want to have this behind a feature flag as well [as perhaps it is useful to compare the performance of zygote as a frontend vs us inside the compiler]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is what I was testing first, but we need to fix our broadcasting quirks before we can work through ChainRules rrules.

Copy link
Member

Choose a reason for hiding this comment

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

yeah....we should definitely fix that

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will error inside Zygote but we have an option

Reactant.with_config(; overlay_zygote_calls=false) do
    @jit Zygote.gradient(sumabs2, x)
end

@codecov
Copy link

codecov bot commented Sep 11, 2025

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 68.55%. Comparing base (20c6a4c) to head (f8785aa).
⚠️ Report is 28 commits behind head on main.

Files with missing lines Patch % Lines
ext/ReactantZygoteExt.jl 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1658      +/-   ##
==========================================
- Coverage   68.76%   68.55%   -0.21%     
==========================================
  Files         103      104       +1     
  Lines       11380    11567     +187     
==========================================
+ Hits         7825     7930     +105     
- Misses       3555     3637      +82     

☔ 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 avik-pal requested a review from wsmoses September 11, 2025 21:01
end

# Overlay Zygote.jl
const OVERLAY_ZYGOTE_CALLS = ScopedValue(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 feel like it might be better to default to false, at least to start? could be convinced either way

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't like the idea of a default that errors out almost always. Rn we should always work with the switching. If anyone really disagrees with switching they can easy opt-out in which case their code will just crash

Copy link
Member

Choose a reason for hiding this comment

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

I suppose that's fair, and I'm okay with this. I guess the specific caveat being that I think we should reserve the right to swap the default (and do so once either more downstream things are set to use enzyme properly and/or we fix broadcasting or other limitations)

Copy link
Member

Choose a reason for hiding this comment

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

and yeah I do agree it's a lot better to get an early error message with a backtrace where it's at least possible to see where to do the switch

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated the text a bit more.

Copy link
Member

@wsmoses wsmoses left a comment

Choose a reason for hiding this comment

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

looks good to me either way, but I think I explicitly want

@vchuravy and @giordano to review/give thoughts

Copy link
Member

@giordano giordano left a comment

Choose a reason for hiding this comment

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

At a high level, I'm not a big fan of magically replacing one package with another one. Since you're overlaying Zygote, one option would be to throw an informative error rather than doing the magic substitution? Or the idea is to let people keep their existing code? At least I appreciate this is documented (and kinda like the idea of showing a warning when this magic replacement happens, although warnings are easy to miss in automated runs, or could clutter log files)

Edit: ok, I see this was discussed at #1658 (comment)

@avik-pal
Copy link
Collaborator Author

Or the idea is to let people keep their existing code?

The idea is to make people progressively switch to Enzyme inside Reactant. Earlier today one of our students had switched the models and parameters to use reactant (xref SciML/NeuralPDE.jl#967) but did not switch the AD she was using.

Instead of crashing we now do the switching with a loud enough warning that users know that they need to eventually fix this behavior.

@avik-pal
Copy link
Collaborator Author

marking as draft to avoid accidental merge before others have a chance to comment

@avik-pal avik-pal marked this pull request as draft September 11, 2025 21:25
@ChrisRackauckas
Copy link

Best way to fix people's code and performance 10/10

@avik-pal
Copy link
Collaborator Author

@giordano @vchuravy any final thoughts on this?

@giordano
Copy link
Member

I'm mildly in favour of this.

@avik-pal avik-pal marked this pull request as ready for review September 21, 2025 16:08
@avik-pal avik-pal merged commit fd603c2 into main Sep 21, 2025
71 of 74 checks passed
@avik-pal avik-pal deleted the ap/zygote_support branch September 21, 2025 16:08
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