-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: init hidden state for reactant (#1026)
* fix: init hidden state for reactant [skip tests] [skip docs] [skip ci] * test: Reactant with recurrent layers * fix: handle cases where similar returns a AoS * chore: bump minimum reactant version * test: use @jit for simplified testing code * test: compile functions in tests correctly * test: update and fix reactant tests
- Loading branch information
Showing
10 changed files
with
126 additions
and
71 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
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,7 @@ | ||
# For some reason xlogx and xlogy with boolean inputs leads to incorrect results sometimes | ||
# XXX: Once https://github.com/EnzymeAD/Reactant.jl/pull/278 is merged and tagged | ||
LuxOps.xlogx(x::TracedRNumber{Bool}) = zero(x) | ||
|
||
function LuxOps.xlogy(x::TracedRNumber, y::TracedRNumber) | ||
return invoke(LuxOps.xlogy, Tuple{Number, Number}, float(x), float(y)) | ||
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
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,65 @@ | ||
@testsetup module SharedReactantLayersTestSetup | ||
|
||
using Lux, Reactant, Enzyme, Zygote | ||
|
||
sumabs2(model, x, ps, st) = sum(abs2, first(model(x, ps, st))) | ||
|
||
function ∇sumabs2_zygote(model, x, ps, st) | ||
return Zygote.gradient((x, ps) -> sumabs2(model, x, ps, st), x, ps) | ||
end | ||
|
||
function ∇sumabs2_enzyme(model, x, ps, st) | ||
dx = Enzyme.make_zero(x) | ||
dps = Enzyme.make_zero(ps) | ||
Enzyme.autodiff( | ||
Enzyme.Reverse, sumabs2, Active, | ||
Const(model), Duplicated(x, dx), | ||
Duplicated(ps, dps), Const(st) | ||
) | ||
return dx, dps | ||
end | ||
|
||
export ∇sumabs2_zygote, ∇sumabs2_enzyme | ||
|
||
end | ||
|
||
@testitem "Recurrent Layers" tags=[:reactant] setup=[ | ||
SharedTestSetup, SharedReactantLayersTestSetup] skip=:(Sys.iswindows()) begin | ||
using Reactant, Lux | ||
using LuxTestUtils: check_approx | ||
|
||
rng = StableRNG(123) | ||
|
||
@testset "$(mode)" for (mode, atype, dev, ongpu) in MODES | ||
if mode == "amdgpu" | ||
@warn "Skipping AMDGPU tests for Reactant" | ||
continue | ||
end | ||
|
||
if ongpu | ||
Reactant.set_default_backend("gpu") | ||
else | ||
Reactant.set_default_backend("cpu") | ||
end | ||
|
||
@testset for cell in (RNNCell, LSTMCell, GRUCell) | ||
model = Recurrence(cell(4 => 4)) | ||
ps, st = Lux.setup(rng, model) | ||
ps_ra, st_ra = (ps, st) |> Reactant.to_rarray | ||
x = rand(Float32, 4, 16, 12) | ||
x_ra = x |> Reactant.to_rarray | ||
|
||
y_ra, _ = @jit model(x_ra, ps_ra, st_ra) | ||
y, _ = model(x, ps, st) | ||
|
||
@test y_ra≈y atol=1e-3 rtol=1e-3 | ||
|
||
@testset "gradient" begin | ||
∂x, ∂ps = ∇sumabs2_zygote(model, x, ps, st) | ||
∂x_ra, ∂ps_ra = @jit ∇sumabs2_enzyme(model, x_ra, ps_ra, st_ra) | ||
@test ∂x_ra≈∂x atol=1e-3 rtol=1e-3 | ||
@test check_approx(∂ps_ra, ∂ps; atol=1e-3, rtol=1e-3) | ||
end | ||
end | ||
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
bbf5033
There was a problem hiding this comment.
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
bbf5033
There was a problem hiding this comment.
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/119457
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.
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:
bbf5033
There was a problem hiding this comment.
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
bbf5033
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register subdir=lib/LuxTestUtils
bbf5033
There was a problem hiding this comment.
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/119458
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.
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:
bbf5033
There was a problem hiding this comment.
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/119459
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.
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:
bbf5033
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register subdir=lib/LuxLib
bbf5033
There was a problem hiding this comment.
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/119461
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.
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:
bbf5033
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
bbf5033
There was a problem hiding this comment.
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/119463
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.
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: