-
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 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 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 use_overlayed_version(args) | ||
| dargs = map(Enzyme.make_zero, args) | ||
| duplicated = map(Duplicated, args, dargs) | ||
avik-pal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Reactant.overload_autodiff(Reverse, Const(f), Active, duplicated...) | ||
| return dargs | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 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] | ||
| 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.
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]
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.
done
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'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]
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.
That is what I was testing first, but we need to fix our broadcasting quirks before we can work through ChainRules rrules.
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.
yeah....we should definitely fix that
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.
This will error inside Zygote but we have an option