-
Notifications
You must be signed in to change notification settings - Fork 95
Add basic Flux integration tests using Enzyme #2968
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| using Enzyme | ||
| using Flux | ||
| using Zygote | ||
| using Test | ||
| using NNlib | ||
| using StableRNGs | ||
| using Random | ||
|
|
||
| # generic loss function for any Flux model | ||
| generic_loss_function(model, x, ps, st) = sum(abs2, first(model(x, ps, st))) | ||
|
|
||
| # compute gradients using Enzyme | ||
| function compute_enzyme_gradient(model, x, ps, st) | ||
| return Enzyme.gradient( | ||
| Enzyme.set_runtime_activity(Reverse), | ||
| generic_loss_function, | ||
| Const(model), | ||
| x, | ||
| ps, | ||
| Const(st), | ||
| )[2:3] | ||
| end | ||
|
|
||
| # compute gradients using Zygote | ||
| function compute_zygote_gradient(model, x, ps, st) | ||
| _, dx, dps, _ = Zygote.gradient(generic_loss_function, model, x, ps, st) | ||
| return dx, dps | ||
| end | ||
|
|
||
| # compare Enzyme gradients with Zygote gradients | ||
| function test_enzyme_gradients(model, x, ps, st) | ||
| dx, dps = compute_enzyme_gradient(model, x, ps, st) | ||
| dx_zygote, dps_zygote = compute_zygote_gradient(model, x, ps, st) | ||
|
|
||
| @test check_approx(dx, dx_zygote; atol = 1.0f-3, rtol = 1.0f-3) | ||
| @test check_approx(dps, dps_zygote; atol = 1.0f-3, rtol = 1.0f-3) | ||
| end | ||
|
|
||
| # small list of models to test | ||
| const MODELS_LIST = [ | ||
| # simple Dense layer | ||
| (Dense(2, 3), randn(Float32, 2, 4)), | ||
|
|
||
| # small Chain | ||
| (Chain(Dense(2, 4, relu), Dense(4, 2)), randn(Float32, 2, 3)), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @CarloLucibello were there more models you wanted to test here, I know the flux ones have a bigger list iirc |
||
|
|
||
| # simple Conv layer | ||
| (Conv((3, 3), 2 => 2), randn(Float32, 5, 5, 2, 1)), | ||
| ] | ||
|
|
||
|
|
||
| @testset "Enzyme Flux Integration" begin | ||
| for (i, (model, x)) in enumerate(MODELS_LIST) | ||
| @testset "[$i] $(nameof(typeof(model)))" begin | ||
| # set up parameters and state | ||
| ps = Flux.trainable(model) | ||
| st = nothing | ||
|
|
||
| # run the gradient test | ||
| test_enzyme_gradients(model, x, ps, st) | ||
|
Comment on lines
+56
to
+60
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This approach won't work. |
||
| end | ||
| end | ||
| end | ||
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.
@gamila-wisam with zygote broken on 1.12, can you have this test against something other than zygote [otherwise we can't compare on 1.12+]
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.
Sure, what about finite-differences gradients?
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.
sure [as long as the models aren't so large that the time would be reasonable]
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.
Yes I've considered that, I will try to ensure that runtime stays reasonable