Skip to content

Commit

Permalink
Rerun tests with chance of false negative once.
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd committed Dec 20, 2024
1 parent ea1d6ad commit 004bb7e
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ const INPLACE_TUPLES = [[(rand!, T) for T in RAND_TYPES];
const OOPLACE_TUPLES = [[(Metal.rand, rand, T) for T in RAND_TYPES];
[(Metal.randn, rand, T) for T in RANDN_TYPES]]

# `f` must return a bool
function retry_on_failure(f::Function, arr::AbstractArray{T}; n_tries=2) where T
i = n_tries
pass = false
while !pass && i > 0
i != n_tries && @info "Retrying test with array size $(size(arr)) and type $T"
i -= 1
pass = f(arr)
end
return pass
end

@testset "random" begin
# in-place
@testset "in-place" begin
Expand All @@ -16,17 +28,22 @@ const OOPLACE_TUPLES = [[(Metal.rand, rand, T) for T in RAND_TYPES];
A = MtlArray{T}(undef, d)

# default_rng
fill!(A, T(0))
f(A)
@test !iszero(collect(A))
@test retry_on_failure(A) do arr
fill!(arr, T(0))
f(arr)
!iszero(collect(arr))
end

# specified MPS rng
if T != Float16
fill!(A, T(0))
if Metal.can_use_mpsrandom(A)
f(rng, A)
@test !iszero(collect(A))
@test retry_on_failure(A) do arr
fill!(arr, T(0))
f(rng, arr)
!iszero(collect(arr))
end
else
fill!(A, T(0))
@test_throws "Destination buffer" f(rng, A)
end
end
Expand Down Expand Up @@ -225,11 +242,14 @@ const OOPLACE_TUPLES = [[(Metal.rand, rand, T) for T in RAND_TYPES];
rng = Metal.MPS.RNG()
@testset "$f with $T" for (f, T) in mps_tuples
@testset "$d" for d in (1, 3, (3, 3), (3, 3, 3), 16, (16, 16), (16, 16, 16), (1000,), (1000,1000))
A = zeros(T, d)
if (prod(d) * sizeof(T)) % 4 == 0
f(rng, A)
@test !iszero(collect(A))
A = zeros(T, d)
@test retry_on_failure(A) do arr
f(rng, arr)
!iszero(collect(arr))
end
else
A = zeros(T, d)
@test_throws "Destination buffer" f(rng, A)
end
end
Expand Down

0 comments on commit 004bb7e

Please sign in to comment.