Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat: default call for wrapper layers
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Aug 29, 2024
1 parent 8d96de5 commit 2d4d5ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/LuxCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ layer to be wrapped in a container.
Additionally, on calling [`initialparameters`](@ref) and [`initialstates`](@ref), the
parameters and states are **not** wrapped in a `NamedTuple` with the same name as the
field.
As a convenience, we define the fallback call `(::AbstractLuxWrapperLayer)(x, ps, st)`,
which calls `getfield(x, layer)(x, ps, st)`.
"""
abstract type AbstractLuxWrapperLayer{layer} <: AbstractLuxLayer end

Expand All @@ -259,6 +262,10 @@ function statelength(l::AbstractLuxWrapperLayer{layer}) where {layer}
return statelength(getfield(l, layer))
end

function (l::AbstractLuxWrapperLayer{layer})(x, ps, st) where {layer}
return apply(getfield(l, layer), x, ps, st)
end

# Test Mode
"""
testmode(st::NamedTuple)
Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ end

(::Dense)(x, ps, st) = x, st # Dummy Forward Pass

struct DenseWrapper{L} <: AbstractLuxWrapperLayer{:layer}
layer::L
end

# For checking ambiguities in the dispatch
struct DenseWrapper2{L} <: AbstractLuxWrapperLayer{:layer}
layer::L
end

(d::DenseWrapper2)(x::AbstractArray, ps, st) = d.layer(x, ps, st)

struct Chain{L} <: AbstractLuxContainerLayer{(:layers,)}
layers::L
end
Expand Down Expand Up @@ -78,6 +89,18 @@ end
first(LuxCore.apply(model, x, ps, NamedTuple()))

@test_nowarn println(model)

@testset for wrapper in (DenseWrapper, DenseWrapper2)
model2 = DenseWrapper(model)
ps, st = LuxCore.setup(rng, model2)

@test LuxCore.parameterlength(ps) == LuxCore.parameterlength(model2)
@test LuxCore.statelength(st) == LuxCore.statelength(model2)

@test model2(x, ps, st)[1] == model(x, ps, st)[1]

@test_nowarn println(model2)
end
end

@testset "Default Fallbacks" begin
Expand Down

2 comments on commit 2d4d5ed

@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/114072

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.0.0 -m "<description of version>" 2d4d5ed5dc557bda5c0476329abf008f7621b09f
git push origin v1.0.0

Please sign in to comment.