Skip to content

Commit

Permalink
feat: tracing stateful layers (#1257)
Browse files Browse the repository at this point in the history
* feat: tracing stateful layers

* test: StatefulLuxLayer tracing
  • Loading branch information
avik-pal authored Mar 7, 2025
1 parent 3c93c68 commit 16031b6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ext/LuxReactantExt/LuxReactantExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Reactant: Reactant, @compile, @code_hlo, @trace, AnyTracedRArray, TracedRA
using Setfield: @set!
using Static: True, False

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

Expand All @@ -29,5 +29,6 @@ end
include("patches.jl")
include("training.jl")
include("layers.jl")
include("tracing.jl")

end
48 changes: 48 additions & 0 deletions ext/LuxReactantExt/tracing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@static if isdefined(Reactant, :ConcreteIFRTArray)
function Reactant.traced_type_inner(
@nospecialize(T::Type{StatefulLuxLayer{ST, M, psT, stT}}),
seen,
@nospecialize(mode::Reactant.TraceMode),
@nospecialize(track_numbers::Type),
@nospecialize(sharding),
@nospecialize(runtime)
) where {ST, M, psT, stT}
return StatefulLuxLayer{
ST,
M,
Reactant.traced_type(psT. seen, mode, track_numbers, sharding, runtime),
Reactant.traced_type(stT, seen, mode, track_numbers, sharding, runtime),
}
end
else
function Reactant.traced_type_inner(
@nospecialize(T::Type{StatefulLuxLayer{ST, M, psT, stT}}),
seen,
@nospecialize(mode::Reactant.TraceMode),
@nospecialize(track_numbers::Type),
@nospecialize(sharding)
) where {ST, M, psT, stT}
return StatefulLuxLayer{
ST,
M,
Reactant.traced_type(psT, seen, mode, track_numbers, sharding),
Reactant.traced_type(stT, seen, mode, track_numbers, sharding),
}
end
end

function Reactant.make_tracer(
seen,
@nospecialize(model::StatefulLuxLayer),
@nospecialize(path),
mode;
kwargs...
)
return StatefulLuxLayer(
model.model,
Reactant.make_tracer(seen, model.ps, (path..., :ps), mode; kwargs...),
Reactant.make_tracer(seen, model.st, (path..., :st), mode; kwargs...),
Reactant.make_tracer(seen, model.st_any, (path..., :st_any), mode; kwargs...),
model.fixed_state_type
)
end
2 changes: 1 addition & 1 deletion lib/LuxCore/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LuxCore"
uuid = "bb33d45b-7691-41d6-9220-0943567d0623"
authors = ["Avik Pal <[email protected]> and contributors"]
version = "1.2.2"
version = "1.2.3"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
19 changes: 19 additions & 0 deletions lib/LuxCore/ext/LuxCoreReactantExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ function Reactant.make_tracer(
return model
end

@static if isdefined(Reactant, :ConcreteIFRTArray)
function Reactant.traced_type_inner(
@nospecialize(T::Type{<:AbstractLuxLayer}), seen,
@nospecialize(mode::Reactant.TraceMode),
@nospecialize(track_numbers::Type), @nospecialize(sharding),
@nospecialize(runtime)
)
return T
end
else
function Reactant.traced_type_inner(
@nospecialize(T::Type{<:AbstractLuxLayer}), seen,
@nospecialize(mode::Reactant.TraceMode),
@nospecialize(track_numbers::Type), @nospecialize(sharding)
)
return T
end
end

LuxCore.replicate(rng::Reactant.TracedRNG) = copy(rng)
LuxCore.replicate(rng::Reactant.ConcreteRNG) = copy(rng)

Expand Down
22 changes: 22 additions & 0 deletions test/reactant/tracing_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@testitem "Tracing" tags = [:reactant] skip = :(Sys.iswindows()) begin
using Reactant, Lux, Random

model = Chain(Dense(2 => 3, relu), BatchNorm(3), Dense(3 => 2))
ps, st = Lux.setup(Random.default_rng(), model)

smodel = StatefulLuxLayer{true}(model, ps, st)
smodel_ra = Reactant.to_rarray(smodel)

@test get_device_type(smodel_ra.ps) <: ReactantDevice
@test get_device_type(smodel_ra.st) <: ReactantDevice
@test smodel_ra.st_any === nothing
@test smodel_ra.fixed_state_type == smodel.fixed_state_type

smodel = StatefulLuxLayer{false}(model, ps, st)
smodel_ra = Reactant.to_rarray(smodel)

@test get_device_type(smodel_ra.ps) <: ReactantDevice
@test get_device_type(smodel_ra.st_any) <: ReactantDevice
@test smodel_ra.st === nothing
@test smodel_ra.fixed_state_type == smodel.fixed_state_type
end

6 comments on commit 16031b6

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register subdir=lib/LuxCore

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register subdir=lib/MLDataDevices

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/126519

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a LuxCore-v1.2.3 -m "<description of version>" 16031b68cc1f24069a6c703d7007befbe079eb83
git push origin LuxCore-v1.2.3

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/126520

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a MLDataDevices-v1.9.1 -m "<description of version>" 16031b68cc1f24069a6c703d7007befbe079eb83
git push origin MLDataDevices-v1.9.1

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/126521

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.0 -m "<description of version>" 16031b68cc1f24069a6c703d7007befbe079eb83
git push origin v1.9.0

Please sign in to comment.