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

Float32 Testing #414

Merged
merged 29 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c2f0ddf
Extend testing utils for f32
willtebbutt Dec 10, 2024
8ce0fb4
Test basic maths at f32
willtebbutt Dec 10, 2024
69ee861
Test BLAS at f32
willtebbutt Dec 10, 2024
cdcf983
Bump patch
willtebbutt Dec 10, 2024
e52859b
Builtins
willtebbutt Dec 10, 2024
49f2157
fastmath
willtebbutt Dec 10, 2024
a33a34c
Simplify BLAS testing
willtebbutt Dec 10, 2024
5960523
flat_product
willtebbutt Dec 10, 2024
0cc086a
lapack testing
willtebbutt Dec 10, 2024
c661c7d
Tidy up blas testing
willtebbutt Dec 10, 2024
5b52498
Helper functions to simplify testing
willtebbutt Dec 10, 2024
b9c17c1
Tidy up blas and lapack rules
willtebbutt Dec 10, 2024
b55f014
matrix exponential on f32
willtebbutt Dec 10, 2024
a26786b
Fix numerical issue
willtebbutt Dec 10, 2024
b9f3af0
Formatting
willtebbutt Dec 10, 2024
aa0ab4c
Fix up MatrixBeta test
willtebbutt Dec 10, 2024
7920fb7
SpecialFunctions on F32
willtebbutt Dec 10, 2024
eacf751
Fix array integration testing
willtebbutt Dec 10, 2024
9e3ec58
Test LogExpFunctions on F32
willtebbutt Dec 10, 2024
a4a6f65
Enable Lux correctness testing
willtebbutt Dec 10, 2024
971bb9a
Fix LAPACK rules
willtebbutt Dec 10, 2024
04d8c31
Try a different method
willtebbutt Dec 10, 2024
217880d
Formatting
willtebbutt Dec 10, 2024
7aa53b9
Tailor perf for 1.10
willtebbutt Dec 10, 2024
dd087fb
Improve trsm stability
willtebbutt Dec 11, 2024
e22f061
Simplify BLAS and LAPACK testing
willtebbutt Dec 11, 2024
cd1aea1
Use StableRNG everywhere
willtebbutt Dec 11, 2024
94d0631
Formatting
willtebbutt Dec 11, 2024
f6714a5
Style + formatting
willtebbutt Dec 11, 2024
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
@@ -1,7 +1,7 @@
name = "Mooncake"
uuid = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.4.59"
version = "0.4.60"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
218 changes: 95 additions & 123 deletions src/rrules/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,6 @@ function blas_vectors(rng::AbstractRNG, P::Type{<:BlasRealFloat}, p::Int)
xs = Any[
randn(rng, P, p),
view(randn(rng, P, p + 5), 3:(p + 2)),
view(randn(rng, P, 3p), 1:2:(2p)),
view(randn(rng, P, 3p, 3), 1:2:(2p), 2),
]
@assert all(x -> length(x) == p, xs)
Expand All @@ -732,72 +731,52 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:blas})
t_flags = ['N', 'T', 'C']
alphas = [1.0, -0.25]
betas = [0.0, 0.33]
Ps = [Float64, Float32]
rng = rng_ctor(123456)

test_cases = vcat(

# gemv!
vec(
reduce(
vcat,
map(product(t_flags, [1, 3], [1, 2])) do (tA, M, N)
t = tA == 'N'
As = blas_matrices(rng, Float64, t ? M : N, t ? N : M)
xs = blas_vectors(rng, Float64, N)
ys = blas_vectors(rng, Float64, M)
flags = (false, :stability, (lb=1e-3, ub=10.0))
return map(product(As, xs, ys)) do (A, x, y)
return (flags..., BLAS.gemv!, tA, randn(), A, x, randn(), y)
end
end,
),
),
map_prod(t_flags, [1, 3], [1, 2], Ps) do (tA, M, N, P)
As = blas_matrices(rng, P, tA == 'N' ? M : N, tA == 'N' ? N : M)
xs = blas_vectors(rng, P, N)
ys = blas_vectors(rng, P, M)
flags = (false, :stability, (lb=1e-3, ub=10.0))
return map(As, xs, ys) do A, x, y
return (flags..., BLAS.gemv!, tA, randn(P), A, x, randn(P), y)
end
end...,

# symv!
vec(
reduce(
vcat,
map(product(['L', 'U'], alphas, betas)) do (uplo, α, β)
As = blas_matrices(rng, Float64, 5, 5)
ys = blas_vectors(rng, Float64, 5)
xs = blas_vectors(rng, Float64, 5)
return map(product(As, xs, ys)) do (A, x, y)
(false, :stability, nothing, BLAS.symv!, uplo, α, A, x, β, y)
end
end,
),
),
map_prod(['L', 'U'], alphas, betas, Ps) do (uplo, α, β, P)
As = blas_matrices(rng, P, 5, 5)
ys = blas_vectors(rng, P, 5)
xs = blas_vectors(rng, P, 5)
return map(As, xs, ys) do A, x, y
(false, :stability, nothing, BLAS.symv!, uplo, P(α), A, x, P(β), y)
end
end...,

# gemm!
vec(
reduce(
vcat,
map(product(t_flags, t_flags, alphas, betas)) do (tA, tB, a, b)
As = blas_matrices(rng, Float64, tA == 'N' ? 3 : 4, tA == 'N' ? 4 : 3)
Bs = blas_matrices(rng, Float64, tB == 'N' ? 4 : 5, tB == 'N' ? 5 : 4)
Cs = blas_matrices(rng, Float64, 3, 5)
return map(product(As, Bs, Cs)) do (A, B, C)
(false, :none, nothing, BLAS.gemm!, tA, tB, a, A, B, b, C)
end
end,
),
),
map_prod(t_flags, t_flags, alphas, betas, Ps) do (tA, tB, a, b, P)
As = blas_matrices(rng, P, tA == 'N' ? 3 : 4, tA == 'N' ? 4 : 3)
Bs = blas_matrices(rng, P, tB == 'N' ? 4 : 5, tB == 'N' ? 5 : 4)
Cs = blas_matrices(rng, P, 3, 5)
return map(As, Bs, Cs) do A, B, C
(false, :none, nothing, BLAS.gemm!, tA, tB, P(a), A, B, P(b), C)
end
end...,

# symm!
vec(
reduce(
vcat,
map(product(['L', 'R'], ['L', 'U'], alphas, betas)) do (side, uplo, α, β)
nA = side == 'L' ? 5 : 7
As = blas_matrices(rng, Float64, nA, nA)
Bs = blas_matrices(rng, Float64, 5, 7)
Cs = blas_matrices(rng, Float64, 5, 7)
return map(product(As, Bs, Cs)) do (A, B, C)
(false, :stability, nothing, BLAS.symm!, side, uplo, α, A, B, β, C)
end
end,
),
),
map_prod(['L', 'R'], ['L', 'U'], alphas, betas, Ps) do (side, ul, α, β, P)
nA = side == 'L' ? 5 : 7
As = blas_matrices(rng, P, nA, nA)
Bs = blas_matrices(rng, P, 5, 7)
Cs = blas_matrices(rng, P, 5, 7)
return map(As, Bs, Cs) do A, B, C
(false, :stability, nothing, BLAS.symm!, side, ul, P(α), A, B, P(β), C)
end
end...,
)

memory = Any[]
Expand All @@ -807,6 +786,10 @@ end
function generate_derived_rrule!!_test_cases(rng_ctor, ::Val{:blas})
t_flags = ['N', 'T', 'C']
aliased_gemm! = (tA, tB, a, b, A, C) -> BLAS.gemm!(tA, tB, a, A, A, b, C)
Ps = [Float32, Float64]
uplos = ['L', 'U']
dAs = ['N', 'U']
rng = rng_ctor(123)

test_cases = vcat(

Expand All @@ -820,91 +803,80 @@ function generate_derived_rrule!!_test_cases(rng_ctor, ::Val{:blas})
# BLAS LEVEL 1
#

Any[
(false, :none, nothing, BLAS.dot, 3, randn(5), 1, randn(4), 1),
(false, :none, nothing, BLAS.dot, 3, randn(6), 2, randn(4), 1),
(false, :none, nothing, BLAS.dot, 3, randn(6), 1, randn(9), 3),
(false, :none, nothing, BLAS.dot, 3, randn(12), 3, randn(9), 2),
(false, :none, nothing, BLAS.scal!, 10, 2.4, randn(30), 2),
],
map(Ps) do P
Any[
(false, :none, nothing, BLAS.dot, 3, randn(P, 5), 1, randn(P, 4), 1),
(false, :none, nothing, BLAS.dot, 3, randn(P, 6), 2, randn(P, 4), 1),
(false, :none, nothing, BLAS.dot, 3, randn(P, 6), 1, randn(P, 9), 3),
(false, :none, nothing, BLAS.dot, 3, randn(P, 12), 3, randn(P, 9), 2),
(false, :none, nothing, BLAS.scal!, 10, P(2.4), randn(P, 30), 2),
]
end...,

#
# BLAS LEVEL 2
#

# trmv!
vec(
reduce(
vcat,
map(product(['L', 'U'], t_flags, ['N', 'U'], [1, 3])) do (ul, tA, dA, N)
As = [randn(N, N), view(randn(15, 15), 3:(N + 2), 4:(N + 3))]
bs = [randn(N), view(randn(14), 4:(N + 3))]
return map(product(As, bs)) do (A, b)
(false, :none, nothing, BLAS.trmv!, ul, tA, dA, A, b)
end
end,
),
),
map_prod(uplos, t_flags, dAs, [1, 3], Ps) do (ul, tA, dA, N, P)
As = blas_matrices(rng, P, N, N)
bs = blas_vectors(rng, P, N)
return map(As, bs) do A, b
(false, :none, nothing, BLAS.trmv!, ul, tA, dA, A, b)
end
end...,

#
# BLAS LEVEL 3
#

# aliased gemm!
vec(
map(product(t_flags, t_flags)) do (tA, tB)
A = randn(5, 5)
B = randn(5, 5)
(false, :none, nothing, aliased_gemm!, tA, tB, randn(), randn(), A, B)
end,
),
map_prod(t_flags, t_flags, Ps) do (tA, tB, P)
As = blas_matrices(rng, P, 5, 5)
Bs = blas_matrices(rng, P, 5, 5)
return map_prod(As, Bs) do (A, B)
(false, :none, nothing, aliased_gemm!, tA, tB, randn(P), randn(P), A, B)
end
end...,

# syrk!
vec(
map(product(['U', 'L'], t_flags)) do (uplo, t)
A = t == 'N' ? randn(3, 4) : randn(4, 3)
C = randn(3, 3)
return (false, :none, nothing, BLAS.syrk!, uplo, t, randn(), A, randn(), C)
end,
),
map_prod(uplos, t_flags, Ps) do (uplo, t, P)
As = blas_matrices(rng, P, t == 'N' ? 3 : 4, t == 'N' ? 4 : 3)
C = randn(P, 3, 3)
flags = (false, :none, nothing)
return map(As) do A
return (flags..., BLAS.syrk!, uplo, t, randn(P), A, randn(P), C)
end
end...,

# trmm!
vec(
reduce(
vcat,
map(
product(['L', 'R'], ['U', 'L'], t_flags, ['N', 'U'], [1, 3], [1, 2])
) do (side, ul, tA, dA, M, N)
t = tA == 'N'
R = side == 'L' ? M : N
As = [randn(R, R), view(randn(15, 15), 3:(R + 2), 4:(R + 3))]
Bs = [randn(M, N), view(randn(15, 15), 2:(M + 1), 5:(N + 4))]
flags = (false, :none, nothing)
return map(product(As, Bs)) do (A, B)
(flags..., BLAS.trmm!, side, ul, tA, dA, randn(), A, B)
end
end,
),
),
map_prod(
['L', 'R'], uplos, t_flags, dAs, [1, 3], [1, 2], Ps
) do (side, ul, tA, dA, M, N, P)
t = tA == 'N'
R = side == 'L' ? M : N
As = blas_matrices(rng, P, R, R)
Bs = blas_matrices(rng, P, M, N)
return map(As, Bs) do A, B
(false, :none, nothing, BLAS.trmm!, side, ul, tA, dA, randn(P), A, B)
end
end...,

# trsm!
vec(
reduce(
vcat,
map(
product(['L', 'R'], ['U', 'L'], t_flags, ['N', 'U'], [1, 3], [1, 2])
) do (side, ul, tA, dA, M, N)
t = tA == 'N'
R = side == 'L' ? M : N
As = [randn(R, R) + 5I, view(randn(15, 15), 3:(R + 2), 4:(R + 3)) + 5I]
Bs = [randn(M, N), view(randn(15, 15), 2:(M + 1), 5:(N + 4))]
flags = (false, :none, nothing)
return map(product(As, Bs)) do (A, B)
(flags..., BLAS.trsm!, side, ul, tA, dA, randn(), A, B)
end
end,
),
),
map_prod(
['L', 'R'], uplos, t_flags, dAs, [1, 3], [1, 2], Ps
) do (side, ul, tA, dA, M, N, P)
t = tA == 'N'
R = side == 'L' ? M : N
As = map(blas_matrices(rng, P, R, R)) do A
A[diagind(A)] .+= 1
return A
end
Bs = blas_matrices(rng, P, M, N)
return map(As, Bs) do A, B
(false, :none, nothing, BLAS.trsm!, side, ul, tA, dA, randn(P), A, B)
end
end...,
)
memory = Any[]
return test_cases, memory
Expand Down
28 changes: 28 additions & 0 deletions src/rrules/builtins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,11 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:builtins})

# Core.Intrinsics:
(false, :stability, nothing, IntrinsicsWrappers.abs_float, 5.0),
(false, :stability, nothing, IntrinsicsWrappers.abs_float, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.add_float, 4.0, 5.0),
(false, :stability, nothing, IntrinsicsWrappers.add_float, 4.0f0, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.add_float_fast, 4.0, 5.0),
(false, :stability, nothing, IntrinsicsWrappers.add_float_fast, 4.0f0, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.add_int, 1, 2),
(false, :stability, nothing, IntrinsicsWrappers.and_int, 2, 3),
(
Expand Down Expand Up @@ -793,28 +796,40 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:builtins})
(false, :stability, nothing, IntrinsicsWrappers.checked_usub_int, 5, 4),
(false, :stability, nothing, IntrinsicsWrappers.copysign_float, 5.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.copysign_float, 5.0, -3.0),
(false, :stability, nothing, IntrinsicsWrappers.copysign_float, 5.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.copysign_float, 5.0f0, -3.0f0),
(false, :stability, nothing, IntrinsicsWrappers.ctlz_int, 5),
(false, :stability, nothing, IntrinsicsWrappers.ctpop_int, 5),
(false, :stability, nothing, IntrinsicsWrappers.cttz_int, 5),
(false, :stability, nothing, IntrinsicsWrappers.div_float, 5.0, 3.0),
(false, :stability, nothing, IntrinsicsWrappers.div_float_fast, 5.0, 3.0),
(false, :stability, nothing, IntrinsicsWrappers.div_float, 5.0f0, 3.0f0),
(false, :stability, nothing, IntrinsicsWrappers.div_float_fast, 5.0f0, 3.0f0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float, 5.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float, 4.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float, 5.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float, 4.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float_fast, 5.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float_fast, 4.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float_fast, 5.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.eq_float_fast, 4.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.eq_int, 5, 4),
(false, :stability, nothing, IntrinsicsWrappers.eq_int, 4, 4),
(false, :stability, nothing, IntrinsicsWrappers.flipsign_int, 4, -3),
(false, :stability, nothing, IntrinsicsWrappers.floor_llvm, 4.1),
(false, :stability, nothing, IntrinsicsWrappers.fma_float, 5.0, 4.0, 3.0),
(false, :stability, nothing, IntrinsicsWrappers.fma_float, 5.0f0, 4.0f0, 3.0f0),
(true, :stability_and_allocs, nothing, IntrinsicsWrappers.fpext, Float64, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.fpiseq, 4.1, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.fpiseq, 4.0f1, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.fptosi, UInt32, 4.1),
(false, :stability, nothing, IntrinsicsWrappers.fptoui, Int32, 4.1),
(true, :stability, nothing, IntrinsicsWrappers.fptrunc, Float32, 5.0),
(true, :stability, nothing, IntrinsicsWrappers.have_fma, Float64),
(false, :stability, nothing, IntrinsicsWrappers.le_float, 4.1, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.le_float, 4.0f1, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.le_float_fast, 4.1, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.le_float_fast, 4.0f1, 4.0f0),
# llvm_call -- NEEDS IMPLEMENTING AND TESTING
(
false,
Expand All @@ -825,17 +840,26 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:builtins})
0x0000000000000018,
),
(false, :stability, nothing, IntrinsicsWrappers.lt_float, 4.1, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.lt_float, 4.0f1, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.lt_float_fast, 4.1, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.lt_float_fast, 4.0f1, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.mul_float, 5.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.mul_float, 5.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.mul_float_fast, 5.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.mul_float_fast, 5.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.mul_int, 5, 4),
(false, :stability, nothing, IntrinsicsWrappers.muladd_float, 5.0, 4.0, 3.0),
(false, :stability, nothing, IntrinsicsWrappers.muladd_float, 5.0f0, 4.0f0, 3.0f0),
(false, :stability, nothing, IntrinsicsWrappers.ne_float, 5.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.ne_float, 5.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.ne_float_fast, 5.0, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.ne_float_fast, 5.0f0, 4.0f0),
(false, :stability, nothing, IntrinsicsWrappers.ne_int, 5, 4),
(false, :stability, nothing, IntrinsicsWrappers.ne_int, 5, 5),
(false, :stability, nothing, IntrinsicsWrappers.neg_float, 5.0),
(false, :stability, nothing, IntrinsicsWrappers.neg_float, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.neg_float_fast, 5.0),
(false, :stability, nothing, IntrinsicsWrappers.neg_float_fast, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.neg_int, 5),
(false, :stability, nothing, IntrinsicsWrappers.not_int, 5),
(false, :stability, nothing, IntrinsicsWrappers.or_int, 5, 5),
Expand Down Expand Up @@ -869,10 +893,14 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:builtins})
(false, :stability, nothing, IntrinsicsWrappers.sle_int, 5, 4),
(false, :stability, nothing, IntrinsicsWrappers.slt_int, 4, 5),
(false, :stability, nothing, IntrinsicsWrappers.sqrt_llvm, 5.0),
(false, :stability, nothing, IntrinsicsWrappers.sqrt_llvm, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.sqrt_llvm_fast, 5.0),
(false, :stability, nothing, IntrinsicsWrappers.sqrt_llvm_fast, 5.0f0),
(false, :stability, nothing, IntrinsicsWrappers.srem_int, 4, 1),
(false, :stability, nothing, IntrinsicsWrappers.sub_float, 4.0, 1.0),
(false, :stability, nothing, IntrinsicsWrappers.sub_float, 4.0f0, 1.0f0),
(false, :stability, nothing, IntrinsicsWrappers.sub_float_fast, 4.0, 1.0),
(false, :stability, nothing, IntrinsicsWrappers.sub_float_fast, 4.0f0, 1.0f0),
(false, :stability, nothing, IntrinsicsWrappers.sub_int, 4, 1),
(false, :stability, nothing, IntrinsicsWrappers.trunc_int, UInt8, 78),
(false, :stability, nothing, IntrinsicsWrappers.trunc_llvm, 5.1),
Expand Down
Loading
Loading