-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add a training loop version for Enzyme
Showing
4 changed files
with
35 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,26 @@ | ||
module LuxEnzymeExt | ||
|
||
using ADTypes: AutoEnzyme | ||
using Enzyme: Enzyme | ||
using Lux: Lux | ||
using Setfield: @set! | ||
|
||
function Lux.Experimental.compute_gradients(::AutoEnzyme, objective_function::F, data, | ||
ts::Lux.Experimental.TrainState) where {F} | ||
dps = Enzyme.make_zero(ts.parameters) | ||
fwd, rev = Enzyme.autodiff_thunk( | ||
Enzyme.ReverseSplitWithPrimal, Enzyme.Const{typeof(objective_function)}, | ||
Enzyme.Active, Enzyme.Const{typeof(ts.model)}, | ||
Enzyme.Duplicated{typeof(ts.parameters)}, | ||
Enzyme.Const{typeof(ts.states)}, Enzyme.Const{typeof(data)}) | ||
tape, (loss, st_new, stats), shadow_result = fwd( | ||
Enzyme.Const(objective_function), Enzyme.Const(ts.model), | ||
Enzyme.Duplicated(ts.parameters, dps), Enzyme.Const(ts.states), Enzyme.Const(data)) | ||
rev(Enzyme.Const(objective_function), Enzyme.Const(ts.model), | ||
Enzyme.Duplicated(ts.parameters, dps), Enzyme.Const(ts.states), Enzyme.Const(data), | ||
(one(loss), Enzyme.make_zero(st_new), Enzyme.make_zero(stats)), tape) | ||
@set! ts.states = st_new | ||
return dps, loss, stats, ts | ||
end | ||
|
||
end |
This file contains 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 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