Skip to content
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

feat: don't unroll Recurrence #1209

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Lux"
uuid = "b2108857-7c20-44ae-9111-449ecde12c47"
authors = ["Avik Pal <[email protected]> and contributors"]
version = "1.5.1"
version = "1.6.0"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
5 changes: 4 additions & 1 deletion ext/LuxReactantExt/LuxReactantExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module LuxReactantExt

using Enzyme: Enzyme, Const, Duplicated, Active
using Optimisers: Optimisers
using Reactant: Reactant, @compile, @code_hlo, AnyTracedRArray, TracedRArray, TracedRNumber
using Reactant: Reactant, @compile, @code_hlo, @trace, AnyTracedRArray, TracedRArray,
TracedRNumber
using Setfield: @set!
using Static: True, False

using Lux: Lux, LuxOps, Training, Utils
using Lux.Training: TrainingBackendCache, ReactantBackend
using LuxCore: LuxCore

Lux.is_extension_loaded(::Val{:Reactant}) = true

Expand All @@ -26,5 +28,6 @@ end

include("patches.jl")
include("training.jl")
include("layers.jl")

end
52 changes: 52 additions & 0 deletions ext/LuxReactantExt/layers.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Embedding
function (e::Lux.Embedding)(x::TracedRNumber{<:Reactant.ReactantInt}, ps, st::NamedTuple)
return ps.weight[:, x], st
end

# Recurrent Layers
function (r::Lux.Recurrence{False})(x::AnyTracedRArray, ps, st::NamedTuple)
if r.ordering isa Lux.TimeLastIndex ||
(r.ordering isa Lux.BatchLastIndex && ndims(x) == 2)
idxs = ntuple(Returns(Colon()), ndims(x) - 1)
(out, carry), st = r.cell(x[idxs..., 1], ps, st)
@trace for i in 2:size(x, ndims(x))
(out, carry), st = r.cell((x[idxs..., i], carry), ps, st)
end
return out, st
elseif r.ordering isa Lux.BatchLastIndex
idxs = ntuple(Returns(Colon()), ndims(x) - 2)
(out, carry), st = r.cell(x[idxs..., 1, :], ps, st)
@trace for i in 2:size(x, ndims(x) - 1)
(out, carry), st = r.cell((x[idxs..., i, :], carry), ps, st)
end
return out, st
else
error("Unknown ordering: $(r.ordering)")
end
end

function (r::Lux.Recurrence{True})(x::AnyTracedRArray, ps, st::NamedTuple)
if r.ordering isa Lux.TimeLastIndex ||
(r.ordering isa Lux.BatchLastIndex && ndims(x) == 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
(r.ordering isa Lux.BatchLastIndex && ndims(x) == 2)
(r.ordering isa Lux.BatchLastIndex && ndims(x) == 2)

idxs = ntuple(Returns(Colon()), ndims(x) - 1)
(out, carry), st = r.cell(x[idxs..., 1], ps, st)
sequence = similar(out, size(out)..., size(x, ndims(x)))
sequence[idxs..., 1] .= out
@trace for i in 2:size(x, ndims(x))
(out, carry), st = r.cell((x[idxs..., i], carry), ps, st)
sequence[idxs..., i] = out
end
elseif r.ordering isa Lux.BatchLastIndex
idxs = ntuple(Returns(Colon()), ndims(x) - 2)
(out, carry), st = r.cell(x[idxs..., 1, :], ps, st)
sequence = similar(out, size(out)..., size(x, ndims(x) - 1))
sequence[idxs..., :, 1] .= out
@trace for i in 2:size(x, ndims(x) - 1)
(out, carry), st = r.cell((x[idxs..., i, :], carry), ps, st)
sequence[idxs..., :, i] = out
end
else
error("Unknown ordering: $(r.ordering)")
end
return (out, eachslice(sequence; dims=ndims(sequence))), st
end
5 changes: 0 additions & 5 deletions ext/LuxReactantExt/patches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ Utils.vec(x::AnyTracedRArray) = Reactant.TracedUtils.materialize_traced_array(ve

# XXX: Use PoolDims once EnzymeJAX supports stablehlo.reduce_window adjoint
Lux.calculate_pool_dims(g::Lux.GlobalPoolMode, ::TracedRArray) = g

# Embedding
function (e::Lux.Embedding)(x::TracedRNumber{<:Reactant.ReactantInt}, ps, st::NamedTuple)
return ps.weight[:, x], st
end
Loading