-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
25 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
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,18 @@ | ||
module LuxReactantExt | ||
|
||
using Adapt: adapt | ||
using ArgCheck: @argcheck | ||
using ConcreteStructs: @concrete | ||
using Enzyme: Enzyme, Active, Const, Duplicated | ||
using Functors: fmapstructure, fmap | ||
using Random: AbstractRNG, Xoshiro | ||
using Reactant: Reactant | ||
using Lux: Lux, LuxEltypeAdaptor, AutoReactant | ||
using LuxCore: LuxCore, AbstractExplicitLayer | ||
|
||
include("utils.jl") | ||
|
||
# compile the entire training loop | ||
include("train.jl") | ||
|
||
end |
Empty file.
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,20 @@ | ||
@inline Lux.__make_reactant_array(x::Reactant.RArray) = x | ||
@inline function Lux.__make_reactant_array(x::AbstractArray) | ||
hasmethod(Reactant.ArrayToConcrete, Tuple{typeof(x)}) && | ||
return Reactant.ConcreteRArray(x) | ||
return __make_tracer(x) | ||
end | ||
@inline Lux.__make_reactant_array(x) = __make_tracer(x) | ||
|
||
@inline function __make_tracer(x) | ||
return Reactant.make_tracer(IdDict(), x, (), Reactant.ArrayToConcrete, nothing) | ||
end | ||
|
||
@inline function __try_similar_structure(x::AbstractArray, y::NamedTuple{()}) | ||
length(x) == 0 && return y | ||
throw(DimensionMismatch(lazy"Expected empty array, got $(size(x)).")) | ||
end | ||
@inline function __try_similar_structure(x::AbstractArray, y::AbstractArray) | ||
return parent(x) !== x ? copy(x) : x # unview arrays and such | ||
end | ||
@inline __try_similar_structure(x, y) = fmap(__try_similar_structure, x, y) |
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
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,23 @@ | ||
""" | ||
AutoReactant() | ||
Compile the training loop to MLIR/XLA via `Reactant.jl`. | ||
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. | ||
""" | ||
struct AutoReactant end | ||
|
||
""" | ||
__make_reactant_array(x) | ||
Converts `x` to a `Reactant.ConcreteRArray` if it is not already one. | ||
""" | ||
function __make_reactant_array end | ||
|
||
@inline function __make_reactant_array(nt::NamedTuple{names}) where {names} | ||
return NamedTuple{names}(map(__make_reactant_array, values(nt))) | ||
end | ||
@inline __make_reactant_array(t::Tuple) = map(__make_reactant_array, t) | ||
@inline __make_reactant_array(x::AbstractExplicitLayer) = x |