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

Add output lengths for layers #491

Merged
merged 4 commits into from
Feb 23, 2024
Merged
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
Expand Up @@ -66,7 +66,7 @@ LinearAlgebra = "1.9"
Logging = "1.9"
LuxAMDGPU = "0.2.2"
LuxCUDA = "0.3.2"
LuxCore = "0.1.7"
LuxCore = "0.1.8"
LuxDeviceUtils = "0.1.14"
LuxLib = "0.3.10"
LuxTestUtils = "0.1.15"
Expand Down
3 changes: 2 additions & 1 deletion src/Lux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ PrecompileTools.@recompile_invalidations begin

import LuxCore: AbstractExplicitLayer, AbstractExplicitContainerLayer,
initialparameters, initialstates, parameterlength, statelength,
update_state, trainmode, testmode, setup, apply, display_name
inputsize, outputsize, update_state, trainmode, testmode, setup, apply,
display_name
import LuxDeviceUtils: AbstractLuxDevice, AbstractLuxGPUDevice, AbstractLuxDeviceAdaptor
end

Expand Down
10 changes: 10 additions & 0 deletions src/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
dims::NTuple{N, Int}
end

outputsize(r::ReshapeLayer) = r.dims

Check warning on line 23 in src/layers/basic.jl

View check run for this annotation

Codecov / codecov/patch

src/layers/basic.jl#L23

Added line #L23 was not covered by tests

@inline function (r::ReshapeLayer)(x::AbstractArray, ps, st::NamedTuple)
return reshape(x, r.dims..., size(x, ndims(x))), st
end
Expand Down Expand Up @@ -197,6 +199,8 @@
end
statelength(d::Dense) = 0

outputsize(d::Dense) = (d.out_dims,)

Check warning on line 202 in src/layers/basic.jl

View check run for this annotation

Codecov / codecov/patch

src/layers/basic.jl#L202

Added line #L202 was not covered by tests

@inline function (d::Dense{false})(x::AbstractVecOrMat, ps, st::NamedTuple)
return __apply_activation(d.activation, ps.weight * x), st
end
Expand Down Expand Up @@ -299,6 +303,8 @@
parameterlength(d::Scale{use_bias}) where {use_bias} = (1 + use_bias) * prod(d.dims)
statelength(d::Scale) = 0

outputsize(d::Scale) = d.dims

Check warning on line 306 in src/layers/basic.jl

View check run for this annotation

Codecov / codecov/patch

src/layers/basic.jl#L306

Added line #L306 was not covered by tests

function (d::Scale{true})(x::AbstractArray, ps, st::NamedTuple)
return __apply_activation(d.activation, ps.weight .* x .+ ps.bias), st
end
Expand Down Expand Up @@ -400,6 +406,8 @@
end
statelength(b::Bilinear) = 0

outputsize(b::Bilinear) = (b.out_dims,)

Check warning on line 409 in src/layers/basic.jl

View check run for this annotation

Codecov / codecov/patch

src/layers/basic.jl#L409

Added line #L409 was not covered by tests

function (b::Bilinear{use_bias})((x, y)::Tuple{<:AbstractVecOrMat, <:AbstractVecOrMat},
ps, st::NamedTuple) where {use_bias}
d_z, d_x, d_y = size(ps.weight)
Expand Down Expand Up @@ -500,3 +508,5 @@
function Base.show(io::IO, e::Embedding)
return print(io, "Embedding(", e.in_dims, " => ", e.out_dims, ")")
end

outputsize(e::Embedding) = (e.out_dims,)

Check warning on line 512 in src/layers/basic.jl

View check run for this annotation

Codecov / codecov/patch

src/layers/basic.jl#L512

Added line #L512 was not covered by tests
9 changes: 9 additions & 0 deletions test/layers/basic_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
x = randn(rng, 6, 3) |> aType

@test size(layer(x, ps, st)[1]) == (2, 3, 3)
@test Lux.outputsize(layer) == (2, 3)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not entirely sure if the definition of outputsize makes sense for ReshapeLayer.

Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a bit confused about the fact that the size of the output in the test above (and also in general) has a different size compared to outputsize because the input also matters and we don't know it from the layer definition.


@jet layer(x, ps, st)
__f = x -> sum(first(layer(x, ps, st)))
Expand Down Expand Up @@ -103,6 +104,8 @@ end

@test size(first(Lux.apply(layer, randn(10), ps, st))) == (5,)
@test size(first(Lux.apply(layer, randn(10, 2), ps, st))) == (5, 2)

@test LuxCore.outputsize(layer) == (5,)
end

@testset "zeros" begin
Expand Down Expand Up @@ -178,6 +181,8 @@ end
@test size(first(Lux.apply(layer, randn(10) |> aType, ps, st))) == (10, 5)
@test size(first(Lux.apply(layer, randn(10, 5, 2) |> aType, ps, st))) ==
(10, 5, 2)

@test LuxCore.outputsize(layer) == (10, 5)
end

@testset "zeros" begin
Expand Down Expand Up @@ -274,6 +279,8 @@ end
@test size(layer((x, y), ps, st)[1]) == (3, 1)
@test sum(abs2, layer((x, y), ps, st)[1]) == 0.0f0

@test LuxCore.outputsize(layer) == (3,)

@jet layer((x, y), ps, st)
__f = (x, y, ps) -> sum(first(layer((x, y), ps, st)))
@eval @test_gradients $__f $x $y $ps atol=1.0f-3 rtol=1.0f-3 gpu_testing=$ongpu
Expand Down Expand Up @@ -316,6 +323,8 @@ end

@test size(ps.weight) == (embed_size, vocab_size)

@test LuxCore.outputsize(layer) == (4,)

x = rand(1:vocab_size, 1)[1]
y, st_ = layer(x, ps, st)
@test size(layer(x, ps, st)[1]) == (embed_size,)
Expand Down
Loading