-
Couldn't load subscription status.
- Fork 33
feat: overlay Zygote.gradient and use Enzyme instead #1658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c83518
feat: overlay Zygote.gradient and use Enzyme instead
avik-pal bc56a8a
fix: add a warning message
avik-pal 46e1792
fix: update text
avik-pal 2a27445
fix: remove maxlog
avik-pal 6dca23f
fix: add config flag for zygote
avik-pal be5f2a7
test: check for no overlay
avik-pal 6602657
fix: use Enzyme.gradient directly
avik-pal f8785aa
fix: more strong wording
avik-pal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| module ReactantZygoteExt | ||
|
|
||
| using Reactant: | ||
| Reactant, CallWithReactant, @reactant_overlay, use_overlayed_version, call_with_reactant | ||
| using Zygote: Zygote | ||
| using Enzyme: Enzyme, Reverse, Active, Const, Duplicated | ||
|
|
||
| # TODO: overload the following as well | ||
| # - Zygote.pullback | ||
| # - Zygote.jacobian | ||
| # - Zygote.hessian | ||
|
|
||
| @reactant_overlay function Zygote.gradient(f::F, args...) where {F} | ||
| # TODO: check `f` as well once #1642 is merged | ||
| if Reactant.OVERLAY_ZYGOTE_CALLS[] && use_overlayed_version(args) | ||
| @warn "Reactant doesn't support using Zygote for computing gradients. Replacing \ | ||
| `Zygote.gradient` with `Enzyme.autodiff` call. Please update your code to \ | ||
| not use `Zygote.gradient` and instead use `Enzyme.gradient` inside \ | ||
| `Reactant.@compile`. If this behavior is undesirable, set the \ | ||
| `overlay_zygote_calls` scoped value via `Reactant.with_config` to \ | ||
| `false`.\n\nReactant can remove this switching without any breaking change \ | ||
| and hence reliance on this behavior is strongly discouraged." | ||
| return Enzyme.gradient(Reverse, Const(f), args...) | ||
| else | ||
| return Base.inferencebarrier(Zygote.gradient)(CallWithReactant(f), args...) | ||
| end | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Zygote, Reactant, Enzyme, Test | ||
|
|
||
| sumabs2(x) = sum(abs2, x) | ||
|
|
||
| @testset "Zygote" begin | ||
| @testset "Zygote.gradient" begin | ||
| x = Reactant.to_rarray(rand(Float32, 32, 10)) | ||
|
|
||
| zyg_grad = @jit Zygote.gradient(sumabs2, x) | ||
| enz_grad = @jit Enzyme.gradient(Reverse, Const(sumabs2), x) | ||
| @test zyg_grad[1] isa Reactant.ConcreteRArray | ||
| @test enz_grad[1] ≈ zyg_grad[1] | ||
|
|
||
| @testset "Disable Overlay" begin | ||
| @test_throws Zygote.CompileError Reactant.with_config(; | ||
| overlay_zygote_calls=false | ||
| ) do | ||
| @jit Zygote.gradient(sumabs2, x) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.