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

Commit

Permalink
test: more detailed layernorm testing
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Aug 29, 2024
1 parent 4853042 commit e490c1e
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions test/normalization/layernorm_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,48 @@ function setup_layernorm(gen_f, aType, T, x_size, affine_shape)
end

function run_layernorm_testing(gen_f, aType, T, x_size, affine_shape, act, ongpu, mode)
dims = Colon()
epsilon = LuxLib.Utils.default_epsilon(T)
_f = (args...) -> layernorm(args..., act, dims, epsilon)
@testset for dims in (Colon(), nothing)
if dims === nothing
affine_shape === nothing && continue
length(x_size) length(affine_shape) && continue
end

x, scale, bias = setup_layernorm(gen_f, aType, T, x_size, affine_shape)
epsilon = LuxLib.Utils.default_epsilon(T)
_f = (args...) -> layernorm(args..., act, dims, epsilon)

@test @inferred(layernorm(x, scale, bias, act, dims, epsilon)) isa Any
@jet layernorm(x, scale, bias, act, dims, epsilon)
x, scale, bias = setup_layernorm(gen_f, aType, T, x_size, affine_shape)

y = _f(x, scale, bias)
@test @inferred(layernorm(x, scale, bias, act, dims, epsilon)) isa Any
@jet layernorm(x, scale, bias, act, dims, epsilon)

@test y isa aType{T, length(x_size)}
@test size(y) == x_size
y = _f(x, scale, bias)

if affine_shape === nothing && act === identity
@test check_approx(mean(y; dims), 0; atol=1e-3, rtol=1e-3)
@test check_approx(std(y; dims), 1; atol=1e-1, rtol=1e-1)
end
@test y isa aType{T, length(x_size)}
@test size(y) == x_size

fp16 = T == Float16
atol = fp16 ? 1.0f-2 : 1.0f-3
rtol = fp16 ? 1.0f-2 : 1.0f-3
if affine_shape === nothing && act === identity
@test check_approx(mean(y; dims), 0; atol=1e-3, rtol=1e-3)
@test check_approx(std(y; dims), 1; atol=1e-1, rtol=1e-1)
end

soft_fail = fp16 ? fp16 : [AutoFiniteDiff()]
if affine_shape !== nothing
__f = (args...) -> sum(_f(args...))
test_gradients(__f, x, scale, bias; atol, rtol, soft_fail)
else
__f = x -> sum(_f(x, scale, bias))
test_gradients(__f, x; atol, rtol, soft_fail)
end
fp16 = T == Float16
atol = fp16 ? 1.0f-2 : 1.0f-3
rtol = fp16 ? 1.0f-2 : 1.0f-3

soft_fail = fp16 ? fp16 : [AutoFiniteDiff()]
if affine_shape !== nothing
__f = (args...) -> sum(_f(args...))
test_gradients(__f, x, scale, bias; atol, rtol, soft_fail)
else
__f = x -> sum(_f(x, scale, bias))
test_gradients(__f, x; atol, rtol, soft_fail)
end

if anonact !== act
lfn = (x, sc, b, act, dim, ϵ) -> sum(layernorm(x, sc, b, act, dim, ϵ))
@test @inferred(Zygote.gradient(lfn, x, scale, bias, act, dims, epsilon)) isa Any
if anonact !== act
lfn = (x, sc, b, act, dim, ϵ) -> sum(layernorm(x, sc, b, act, dim, ϵ))
@test @inferred(Zygote.gradient(lfn, x, scale, bias, act, dims, epsilon)) isa
Any
end
end
end

Expand Down Expand Up @@ -115,3 +122,16 @@ end
end
end
end

@testitem "Layer Norm: Error Checks" tags=[:layer_norm] setup=[SharedTestSetup] begin
@testset "$mode" for (mode, aType, ongpu) in MODES
x = rand(2, 3) |> aType

@test_throws ArgumentError layernorm(x, nothing, nothing, identity, nothing, 1e-5)

sc = rand(2, 1) |> aType
b = rand(2, 1) |> aType

@test_throws AssertionError layernorm(x, sc, b, identity, nothing, 1e-5)
end
end

0 comments on commit e490c1e

Please sign in to comment.