-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathlayernorm_tests.jl
160 lines (132 loc) · 5.67 KB
/
layernorm_tests.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
@testsetup module LayerNormSetup
using LuxLib, LuxTestUtils, Random, Test, Zygote, NNlib, Statistics
using LuxTestUtils: check_approx
function setup_layernorm(gen_f, aType, T, x_size, affine_shape, expand_dims::Bool=true)
x = gen_f(T, x_size) |> aType
if affine_shape !== nothing
if expand_dims
scale = gen_f(T, (affine_shape..., 1)) |> aType
bias = gen_f(T, (affine_shape..., 1)) |> aType
else
scale = gen_f(T, affine_shape) |> aType
bias = gen_f(T, affine_shape) |> aType
end
return x, scale, bias
else
return x, nothing, nothing
end
end
function run_layernorm_testing(gen_f, aType, T, x_size, affine_shape, act, ongpu, mode)
@testset for dims in (Colon(), nothing)
if dims === nothing
affine_shape === nothing && continue
length(x_size) ≤ length(affine_shape) && continue
x, scale, bias = setup_layernorm(gen_f, aType, T, x_size, affine_shape, false)
else
x, scale, bias = setup_layernorm(gen_f, aType, T, x_size, affine_shape)
end
run_layernorm_testing_core(
aType, T, x_size, affine_shape, act, dims, x, scale, bias)
end
end
function run_layernorm_testing_core(
aType, T, x_size, affine_shape, act, dims, x, scale, bias)
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)
y = _f(x, scale, bias)
@test y isa aType{T, length(x_size)}
@test size(y) == x_size
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
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
end
end
anonact = x -> x^3
const ALL_TEST_CONFIGS = Any[]
for T in (Float16, Float32, Float64),
x_shape in ((3, 3, 2, 1), (2, 2, 2, 1), (2, 3, 2, 2)),
affine_shape in (nothing, x_shape[1:3], (1, 1, 1), (1, 1, x_shape[3])),
act in (identity, relu, tanh_fast, sigmoid_fast, anonact)
push!(ALL_TEST_CONFIGS, (T, x_shape, affine_shape, act))
end
const TEST_BLOCKS = collect(Iterators.partition(
ALL_TEST_CONFIGS, ceil(Int, length(ALL_TEST_CONFIGS) / 5)))
export ALL_TEST_CONFIGS, TEST_BLOCKS, run_layernorm_testing
end
@testitem "Layer Norm: Group 1" tags=[:normalization] setup=[
SharedTestSetup, LayerNormSetup] begin
@testset "$mode" for (mode, aType, ongpu, fp64) in MODES
@testset "eltype $T, size $x_shape, $act" for (T, x_shape, affine_shape, act) in TEST_BLOCKS[1]
!fp64 && T == Float64 && continue
run_layernorm_testing(
generate_fixed_array, aType, T, x_shape, affine_shape, act, ongpu, mode)
end
end
end
@testitem "Layer Norm: Group 2" tags=[:normalization] setup=[
SharedTestSetup, LayerNormSetup] begin
@testset "$mode" for (mode, aType, ongpu, fp64) in MODES
@testset "eltype $T, size $x_shape, $act" for (T, x_shape, affine_shape, act) in TEST_BLOCKS[2]
!fp64 && T == Float64 && continue
run_layernorm_testing(
generate_fixed_array, aType, T, x_shape, affine_shape, act, ongpu, mode)
end
end
end
@testitem "Layer Norm: Group 3" tags=[:normalization] setup=[
SharedTestSetup, LayerNormSetup] begin
@testset "$mode" for (mode, aType, ongpu, fp64) in MODES
@testset "eltype $T, size $x_shape, $act" for (T, x_shape, affine_shape, act) in TEST_BLOCKS[3]
!fp64 && T == Float64 && continue
run_layernorm_testing(
generate_fixed_array, aType, T, x_shape, affine_shape, act, ongpu, mode)
end
end
end
@testitem "Layer Norm: Group 4" tags=[:normalization] setup=[
SharedTestSetup, LayerNormSetup] begin
@testset "$mode" for (mode, aType, ongpu, fp64) in MODES
@testset "eltype $T, size $x_shape, $act" for (T, x_shape, affine_shape, act) in TEST_BLOCKS[4]
!fp64 && T == Float64 && continue
run_layernorm_testing(
generate_fixed_array, aType, T, x_shape, affine_shape, act, ongpu, mode)
end
end
end
@testitem "Layer Norm: Group 5" tags=[:normalization] setup=[
SharedTestSetup, LayerNormSetup] begin
@testset "$mode" for (mode, aType, ongpu, fp64) in MODES
@testset "eltype $T, size $x_shape, $act" for (T, x_shape, affine_shape, act) in TEST_BLOCKS[5]
!fp64 && T == Float64 && continue
run_layernorm_testing(
generate_fixed_array, aType, T, x_shape, affine_shape, act, ongpu, mode)
end
end
end
@testitem "Layer Norm: Error Checks" tags=[:normalization] setup=[SharedTestSetup] begin
@testset "$mode" for (mode, aType, ongpu, fp64) in MODES
!fp64 && continue
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