-
Notifications
You must be signed in to change notification settings - Fork 84
Compile training loop with Reactant #673
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39b1401
feat(reactant): compile `single_train_step!` using `Reactant`
avik-pal 196045f
refactor(reactant): avoid potential name conflict with ADTypes
avik-pal b7740f3
refactor(reactant): uniform naming across extensions
avik-pal d151da2
feat(training): add inference mode for `TrainState`
avik-pal 9741de1
feat(reactant): auto compile inference mode if possible
avik-pal 0ef2b6a
refactor(reactant): move optimisers into main pkg
avik-pal 2f7aee5
fix: tstate inference should return the state
avik-pal 70b7a86
chore: format suggestion
avik-pal c47a9d0
fix: update to new API
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,14 @@ | ||
| module LuxReactantExt | ||
|
|
||
| using Enzyme: Enzyme, Active, Const, Duplicated | ||
| using Reactant: Reactant | ||
| using Static: Static, False | ||
| using Setfield: @set! | ||
|
|
||
| using Lux: Lux, ReactantBackend | ||
| using Lux.Training: TrainingBackendCache, TrainState | ||
| using LuxCore: LuxCore | ||
|
|
||
| include("training.jl") | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| function Lux.Training.single_train_step!( | ||
| backend::ReactantBackend, obj_fn::F, data, ts::TrainState) where {F} | ||
| data = Reactant.to_rarray(data) | ||
| ps = Reactant.to_rarray(ts.parameters) | ||
| st = Reactant.to_rarray(ts.states) | ||
| st_opt = Reactant.to_rarray(ts.optimizer_state) | ||
|
|
||
| compiled_inference = if backend.input_prototype !== nothing | ||
| Reactant.compile(LuxCore.apply, | ||
| (ts.model, Reactant.to_rarray(backend.input_prototype), | ||
| ps, LuxCore.testmode(st))) | ||
| else | ||
| nothing | ||
| end | ||
|
|
||
| compiled_grad_and_step! = Reactant.compile( | ||
| internal_grad_and_step!, (obj_fn, ts.model, ps, st, st_opt, data, ts.optimizer)) | ||
|
|
||
| loss, st_updated, stats = compiled_grad_and_step!( | ||
| obj_fn, ts.model, ps, st, st_opt, data, ts.optimizer) | ||
|
|
||
| cache = TrainingBackendCache(backend, False(), nothing, (; compiled_grad_and_step!, | ||
| compiled_inference)) | ||
| @set! ts.cache = cache | ||
| @set! ts.objective_function = obj_fn | ||
| @set! ts.parameters = ps | ||
| @set! ts.states = st_updated | ||
| @set! ts.optimizer_state = st_opt | ||
| @set! ts.step = ts.step + 1 | ||
|
|
||
| return nothing, loss, stats, ts # TODO: Return the gradients | ||
| end | ||
|
|
||
| function Lux.Training.single_train_step!(::ReactantBackend, obj_fn::F, data, | ||
| ts::TrainState{<:TrainingBackendCache{<:ReactantBackend}, F}) where {F} | ||
| data = Reactant.to_rarray(data) | ||
|
|
||
| loss, st_updated, stats = ts.cache.extras.compiled_grad_and_step!( | ||
| obj_fn, ts.model, ts.parameters, ts.states, ts.optimizer_state, data, ts.optimizer) | ||
|
|
||
| @set! ts.objective_function = obj_fn | ||
| @set! ts.states = st_updated | ||
| @set! ts.step = ts.step + 1 | ||
|
|
||
| return nothing, loss, stats, ts # TODO: Return the gradients | ||
| end | ||
|
|
||
| function internal_grad_and_step!( | ||
| obj_fn::F, model, ps, st, st_opt, data, optimizer) where {F} | ||
| dps = Lux.recursive_make_zero(ps) | ||
|
|
||
| _, (loss, st_updated, stats) = Enzyme.autodiff( | ||
| Enzyme.ReverseWithPrimal, obj_fn, Active, Const(model), | ||
| Duplicated(ps, dps), Const(st), Const(data)) | ||
|
|
||
| Lux.simple_optimizers_apply!(optimizer, st_opt, ps, dps) # ps & st_opt are updated in-place | ||
|
|
||
| return loss, st_updated, stats | ||
| end | ||
|
|
||
| function (tstate::TrainState{<:TrainingBackendCache{<:ReactantBackend}})(data) | ||
| data_reactant = Reactant.to_rarray(data) | ||
| compiled_inference = if tstate.cache.extras.compiled_inference !== nothing | ||
| tstate.cache.extras.compiled_inference | ||
| else | ||
| @warn "Inference function not compiled before. This will trigger compilation on \ | ||
| every inference call to `(::TrainState)(data)`. Please use \ | ||
| `ReactantBackend(; input_prototype = data)` to compile the inference \ | ||
| function on the first call to `single_train_step!` or \ | ||
| `single_train_step`." maxlog=1 | ||
| Reactant.compile(LuxCore.apply, | ||
| (tstate.model, data_reactant, tstate.parameters, | ||
| LuxCore.testmode(tstate.states))) | ||
| end | ||
| return compiled_inference( | ||
| tstate.model, data_reactant, tstate.parameters, LuxCore.testmode(tstate.states)) | ||
| 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,29 @@ | ||
| abstract type AbstractCompilerBackend end | ||
|
|
||
| """ | ||
| ReactantBackend(; input_prototype = nothing) | ||
|
|
||
| Compile Lux model and gradient computation to MLIR/XLA via `Reactant.jl`. | ||
|
|
||
| !!! tip "Newly Added Feature!" | ||
|
|
||
| This has been added to Lux very recently and is under-going rapid development. | ||
| Currently, only a limited subset of Lux models can be compiled via `Reactant.jl`. If you | ||
| encounter any issues, please report them on the `Lux.jl` or `Reactant.jl` GitHub | ||
| repository. | ||
|
|
||
| ## Keyword Arguments | ||
|
|
||
| - `input_prototype`: Input data representative of the data that will be used for | ||
| inference. If this is provided, we will compile the inference function with | ||
| `Reactant.jl` on the first call to [`Lux.Experimental.single_train_step!`](@ref) or | ||
| [`Lux.Experimental.single_train_step`](@ref). If this is not provided, we will have to | ||
| recompile the inference function on every call to `(::TrainState)(data)` and this will | ||
| be prohibitively expensive. | ||
|
|
||
| See [`Lux.Experimental.single_train_step!`](@ref) or | ||
| [`Lux.Experimental.single_train_step`](@ref) for information on how to use this backend. | ||
| """ | ||
| @kwdef @concrete struct ReactantBackend <: AbstractCompilerBackend | ||
| input_prototype = nothing | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # These are meant to be used internally for compiling certain lux optiomization | ||
| function simple_optimizers_apply!(ps, gs, leaf::Leaf{<:Descent}) | ||
| @. ps -= leaf.rule.eta * gs | ||
| end | ||
|
|
||
| for opt in (Descent,) | ||
| @eval function simple_optimizers_apply!(::$(opt), st_opt, ps, gs) | ||
| recursive_map(simple_optimizers_apply!, ps, gs, st_opt) | ||
| end | ||
| end | ||
|
|
||
| function simple_optimizers_apply!(opt, st_opt, ps, gs) | ||
| throw(ArgumentError("Optimizer $(typeof(opt)) not yet supported.")) | ||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.