From adb914e39e1847b98f534dfbca4aec3c352f69a2 Mon Sep 17 00:00:00 2001 From: Troels Nielsen Date: Wed, 25 Aug 2021 10:05:13 +0200 Subject: [PATCH] Extend the type coverage, and give more control to back-ends. (#373) --- test/testsuite.jl | 21 +++++-- test/testsuite/base.jl | 52 ++++++++--------- test/testsuite/broadcasting.jl | 67 +++++++++++----------- test/testsuite/construction.jl | 28 ++++++---- test/testsuite/gpuinterface.jl | 2 +- test/testsuite/indexing.jl | 20 +++---- test/testsuite/io.jl | 2 +- test/testsuite/linalg.jl | 34 +++++++----- test/testsuite/math.jl | 11 ++-- test/testsuite/random.jl | 9 +-- test/testsuite/reductions.jl | 63 ++++++++++----------- test/testsuite/statistics.jl | 95 +++++++++++++++++--------------- test/testsuite/uniformscaling.jl | 2 +- test/testsuite/vector.jl | 2 +- 14 files changed, 219 insertions(+), 189 deletions(-) diff --git a/test/testsuite.jl b/test/testsuite.jl index af461161..9c285af8 100644 --- a/test/testsuite.jl +++ b/test/testsuite.jl @@ -43,17 +43,28 @@ function compare(f, AT::Type{<:Array}, xs...; kwargs...) return true end -function supported_eltypes() - (Float32, Float64, Int32, Int64, ComplexF32, ComplexF64) -end +# element types that are supported by the array type +supported_eltypes(AT, test) = supported_eltypes(AT) +supported_eltypes(AT) = supported_eltypes() +supported_eltypes() = (Int16, Int32, Int64, + Float16, Float32, Float64, + ComplexF16, ComplexF32, ComplexF64, + Complex{Int16}, Complex{Int32}, Complex{Int64}) + +# some convenience predicates for filtering test eltypes +isrealtype(T) = T <: Real +iscomplextype(T) = T <: Complex +isfloattype(T) = T <: AbstractFloat || T <: Complex{<:AbstractFloat} # list of tests const tests = Dict() macro testsuite(name, ex) - safe_name = lowercase(replace(name, " "=>"_")) + safe_name = lowercase(replace(replace(name, " "=>"_"), "/"=>"_")) fn = Symbol("test_$(safe_name)") quote - $(esc(fn))(AT) = $(esc(ex))(AT) + # the supported element types can be overrided by passing in a different set, + # or by specializing the `supported_eltypes` function on the array type and test. + $(esc(fn))(AT; eltypes=supported_eltypes(AT, $(esc(fn)))) = $(esc(ex))(AT, eltypes) @assert !haskey(tests, $name) tests[$name] = $fn diff --git a/test/testsuite/base.jl b/test/testsuite/base.jl index db647109..b7ab3214 100644 --- a/test/testsuite/base.jl +++ b/test/testsuite/base.jl @@ -25,7 +25,7 @@ function ntuple_closure(ctx, result, ::Val{N}, testval) where N return end -@testsuite "base" AT->begin +@testsuite "base" (AT, eltypes)->begin @testset "copyto!" begin x = fill(0f0, (10, 10)) y = rand(Float32, (20, 10)) @@ -70,15 +70,15 @@ end copyto!(a, r1, b, r2) @test x == Array(a) - x = fill(0., (10,)) - y = fill(1, (10,)) + x = fill(0f0, (10,)) + y = fill(1f0, (10,)) a = AT(x) b = AT(y) copyto!(a, b) - @test Float64.(y) == Array(a) + @test Float32.(y) == Array(a) # wrapped gpu array to wrapped gpu array - x = rand(4, 4) + x = rand(Float32, 4, 4) a = AT(x) b = view(a, 2:3, 2:3) c = AT{eltype(b)}(undef, size(b)) @@ -95,21 +95,23 @@ end # bug in copyto! ## needless N type parameter - @test compare((x,y)->copyto!(y, selectdim(x, 2, 1)), AT, ones(2,2,2), zeros(2,2)) + @test compare((x,y)->copyto!(y, selectdim(x, 2, 1)), AT, ones(Float32, 2, 2, 2), zeros(Float32, 2, 2)) ## inability to copyto! smaller destination ## (this was broken on Julia <1.5) - @test compare((x,y)->copyto!(y, selectdim(x, 2, 1)), AT, ones(2,2,2), zeros(3,3)) - - # mismatched types - let src = rand(Float32, 4) - dst = AT{Float64}(undef, size(src)) - copyto!(dst, src) - @test Array(dst) == src - end - let dst = Array{Float64}(undef, 4) - src = AT(rand(Float32, size(dst))) - copyto!(dst, src) - @test Array(src) == dst + @test compare((x,y)->copyto!(y, selectdim(x, 2, 1)), AT, ones(Float32, 2, 2, 2), zeros(Float32, 3, 3)) + + if (Float32 in eltypes && Float64 in eltypes) + # mismatched types + let src = rand(Float32, 4) + dst = AT{Float64}(undef, size(src)) + copyto!(dst, src) + @test Array(dst) == src + end + let dst = Array{Float64}(undef, 4) + src = AT(rand(Float32, size(dst))) + copyto!(dst, src) + @test Array(src) == dst + end end end @@ -123,11 +125,11 @@ end end @testset "reshape" begin - @test compare(reshape, AT, rand(10), Ref((10,))) - @test compare(reshape, AT, rand(10), Ref((10,1))) - @test compare(reshape, AT, rand(10), Ref((1,10))) + @test compare(reshape, AT, rand(Float32, 10), Ref((10,))) + @test compare(reshape, AT, rand(Float32, 10), Ref((10,1))) + @test compare(reshape, AT, rand(Float32, 10), Ref((1,10))) - @test_throws Exception reshape(AT(rand(10)), (10,2)) + @test_throws Exception reshape(AT(rand(Float32, 10)), (10,2)) end @testset "reinterpret" begin @@ -158,7 +160,7 @@ end AT <: AbstractGPUArray && @testset "cartesian iteration" begin Ac = rand(Float32, 32, 32) A = AT(Ac) - result = fill!(copy(A), 0.0) + result = fill!(copy(A), 0.0f0) gpu_call(cartesian_iter, result, A, size(A)) Array(result) == Ac end @@ -188,10 +190,10 @@ end end @testset "permutedims" begin - @test compare(x->permutedims(x, [1, 2]), AT, rand(4, 4)) + @test compare(x->permutedims(x, [1, 2]), AT, rand(Float32, 4, 4)) inds = rand(1:100, 150, 150) - @test compare(x->permutedims(view(x, inds, :), (3, 2, 1)), AT, rand(100, 100)) + @test compare(x->permutedims(view(x, inds, :), (3, 2, 1)), AT, rand(Float32, 100, 100)) end @testset "circshift" begin diff --git a/test/testsuite/broadcasting.jl b/test/testsuite/broadcasting.jl index e6eeefe4..939af21b 100644 --- a/test/testsuite/broadcasting.jl +++ b/test/testsuite/broadcasting.jl @@ -1,6 +1,6 @@ -@testsuite "broadcasting" AT->begin - broadcasting(AT) - vec3(AT) +@testsuite "broadcasting" (AT, eltypes)->begin + broadcasting(AT, eltypes) + vec3(AT, eltypes) @testset "type instabilities" begin f(x) = x ? 1.0 : 0 @@ -37,8 +37,8 @@ function test_kernel(a::T, b) where T return c end -function broadcasting(AT) - for ET in supported_eltypes() +function broadcasting(AT, eltypes) + for ET in eltypes N = 10 @testset "broadcast $ET" begin @testset "RefValue" begin @@ -91,7 +91,8 @@ function broadcasting(AT) # since GPUArrays adds some arguments to the function, it becomes longer longer, hitting the 12 # so this wont fix for now @test compare(AT, rand(ET, dim), rand(ET, dim), rand(ET, dim), rand(ET, dim), rand(ET, dim), rand(ET, dim)) do a1, a2, a3, a4, a5, a6 - @. a1 = a2 + (1.2) *((1.3)*a3 + (1.4)*a4 + (1.5)*a5 + (1.6)*a6) + c1, c2, c3, c4, c5 = ET(1.2), ET(1.3), ET(1.4), ET(1.5), ET(1.6) + @. a1 = a2 + c1 * (c2 * a3 + c3 * a4 + c4 * a5 + c5 * a6) end @test compare(AT, rand(ET, dim), rand(ET, dim), rand(ET, dim), rand(ET, dim)) do u, uprev, duprev, ku @@ -110,6 +111,14 @@ function broadcasting(AT) dt = ET(1) @. utilde = dt*(btilde1*k1 + btilde2*k2 + btilde3*k3 + btilde4*k4) end + + @testset "0D" begin + x = AT{ET}(undef) + x .= ET(1) + @test collect(x)[] == ET(1) + x /= ET(2) + @test collect(x)[] == ET(0.5) + end end @test compare((x) -> fill!(x, 1), AT, rand(ET, 3,3)) @@ -127,51 +136,43 @@ function broadcasting(AT) end @testset "map! $ET" begin - @test compare(AT, rand(2,2), rand(2,2)) do x,y + @test compare(AT, rand(ET, 2,2), rand(ET, 2,2)) do x,y map!(+, x, y) end - @test compare(AT, rand(2), rand(2,2)) do x,y + @test compare(AT, rand(ET, 2), rand(ET, 2,2)) do x,y map!(+, x, y) end - @test compare(AT, rand(2,2), rand(2)) do x,y + @test compare(AT, rand(ET, 2,2), rand(ET, 2)) do x,y map!(+, x, y) end end @testset "map $ET" begin - @test compare(AT, rand(2,2), rand(2,2)) do x,y + @test compare(AT, rand(ET, 2,2), rand(ET, 2,2)) do x,y map(+, x, y) end - @test compare(AT, rand(2), rand(2,2)) do x,y + @test compare(AT, rand(ET, 2), rand(ET, 2,2)) do x,y map(+, x, y) end - @test compare(AT, rand(2,2), rand(2)) do x,y + @test compare(AT, rand(ET, 2,2), rand(ET, 2)) do x,y map(+, x, y) end end - end - @testset "0D" begin - x = AT{Float64}(undef) - x .= 1 - @test collect(x)[] == 1 - x /= 2 - @test collect(x)[] == 0.5 - end - - @testset "Ref" begin - # as first arg, 0d broadcast - @test compare(x->getindex.(Ref(x),1), AT, [0]) + @testset "Ref" begin + # as first arg, 0d broadcast + @test compare(x->getindex.(Ref(x), 1), AT, ET[0]) - void_setindex!(args...) = (setindex!(args...); return) - @test compare(x->(void_setindex!.(Ref(x),1); x), AT, [0]) + void_setindex!(args...) = (setindex!(args...); return) + @test compare(x->(void_setindex!.(Ref(x), ET(1)); x), AT, ET[0]) - # regular broadcast - a = AT(rand(10)) - b = AT(rand(10)) - cpy(i,a,b) = (a[i] = b[i]; return) - cpy.(1:10, Ref(a), Ref(b)) - @test Array(a) == Array(b) + # regular broadcast + a = AT(rand(ET, 10)) + b = AT(rand(ET, 10)) + cpy(i,a,b) = (a[i] = b[i]; return) + cpy.(1:10, Ref(a), Ref(b)) + @test Array(a) == Array(b) + end end @testset "stackoverflow in copy(::Broadcast)" begin @@ -179,7 +180,7 @@ function broadcasting(AT) end end -function vec3(AT) +function vec3(AT, eltypes) @testset "vec 3" begin N = 20 diff --git a/test/testsuite/construction.jl b/test/testsuite/construction.jl index 90dc7c18..676995c7 100644 --- a/test/testsuite/construction.jl +++ b/test/testsuite/construction.jl @@ -1,5 +1,5 @@ -@testsuite "construct/direct" AT->begin - for T in supported_eltypes() +@testsuite "construct/direct" (AT, eltypes)->begin + for T in eltypes B = AT{T}(undef, 10) @test B isa AT{T,1} @test size(B) == (10,) @@ -45,8 +45,8 @@ end end -@testsuite "construct/similar" AT->begin - for T in supported_eltypes() +@testsuite "construct/similar" (AT, eltypes)->begin + for T in eltypes B = AT{T}(undef, 10) B = similar(B, Int32, 11, 15) @@ -96,8 +96,8 @@ end end end -@testsuite "construct/convenience" AT->begin - for T in supported_eltypes() +@testsuite "construct/convenience" (AT, eltypes)->begin + for T in eltypes A = AT(rand(T, 3)) b = rand(T) fill!(A, b) @@ -126,8 +126,8 @@ end end end -@testsuite "construct/conversions" AT->begin - for T in supported_eltypes() +@testsuite "construct/conversions" (AT, eltypes)->begin + for T in eltypes Bc = round.(rand(10, 10) .* 10.0) B = AT{T}(Bc) @test size(B) == (10, 10) @@ -146,7 +146,13 @@ end @test eltype(B) == T @test Array(B) ≈ Bc - Bc = rand(Int32, 3, 3, 3) + intervals = Dict( + Float16 => -2^11:2^11, + Float32 => -2^24:2^24, + Float64 => -2^53:2^53, + ) + + Bc = rand(Int8, 3, 3, 3) B = convert(AT{T, 3}, Bc) @test size(B) == (3, 3, 3) @test eltype(B) == T @@ -154,8 +160,8 @@ end end end -@testsuite "construct/uniformscaling" AT->begin - for T in supported_eltypes() +@testsuite "construct/uniformscaling" (AT, eltypes)->begin + for T in eltypes x = Matrix{T}(I, 4, 2) x1 = AT{T, 2}(I, 4, 2) diff --git a/test/testsuite/gpuinterface.jl b/test/testsuite/gpuinterface.jl index ec3d1cdc..1455c732 100644 --- a/test/testsuite/gpuinterface.jl +++ b/test/testsuite/gpuinterface.jl @@ -1,4 +1,4 @@ -@testsuite "interface" AT->begin +@testsuite "interface" (AT, eltypes)->begin AT <: AbstractGPUArray || return N = 10 diff --git a/test/testsuite/indexing.jl b/test/testsuite/indexing.jl index b886e131..7f144554 100644 --- a/test/testsuite/indexing.jl +++ b/test/testsuite/indexing.jl @@ -1,4 +1,4 @@ -@testsuite "indexing scalar" AT->begin +@testsuite "indexing scalar" (AT, eltypes)->begin AT <: AbstractGPUArray && @testset "errors and warnings" begin x = AT([0]) @@ -19,7 +19,7 @@ @test_throws ErrorException x[] end - @allowscalar @testset "getindex with $T" for T in supported_eltypes() + @allowscalar @testset "getindex with $T" for T in eltypes x = rand(T, 32) src = AT(x) for (i, xi) in enumerate(x) @@ -29,7 +29,7 @@ @test Array(src[3:end]) == x[3:end] end - @allowscalar @testset "setindex! with $T" for T in supported_eltypes() + @allowscalar @testset "setindex! with $T" for T in eltypes x = fill(zero(T), 7) src = AT(x) for i = 1:7 @@ -41,7 +41,7 @@ src[1] = T(0) end - @allowscalar @testset "issue #42 with $T" for T in supported_eltypes() + @allowscalar @testset "issue #42 with $T" for T in eltypes Ac = rand(Float32, 2, 2) A = AT(Ac) @test A[1] == Ac[1] @@ -59,15 +59,15 @@ end end -@testsuite "indexing multidimensional" AT->begin - @testset "sliced setindex" for T in supported_eltypes() +@testsuite "indexing multidimensional" (AT, eltypes)->begin + @testset "sliced setindex" for T in eltypes x = AT(zeros(T, (10, 10, 10, 10))) y = AT(rand(T, (5, 5, 10, 10))) x[2:6, 2:6, :, :] = y @test Array(x[2:6, 2:6, :, :]) == Array(y) end - @testset "sliced setindex, CPU source" for T in supported_eltypes() + @testset "sliced setindex, CPU source" for T in eltypes x = AT(zeros(T, (2,3,4))) y = AT(rand(T, (2,3))) x[:, :, 2] = y @@ -99,7 +99,7 @@ end end @testset "GPU source" begin - a = rand(3) + a = rand(Float32, 3) i = rand(1:3, 2) @test compare(getindex, AT, a, i) @test compare(getindex, AT, a, i') @@ -108,7 +108,7 @@ end @testset "CPU source" begin # JuliaGPU/CUDA.jl#345 - a = rand(3,4) + a = rand(Float32, 3, 4) i = rand(1:3,2,2) @test compare(a->a[i,:], AT, a) @test compare(a->a[i',:], AT, a) @@ -116,6 +116,6 @@ end end @testset "JuliaGPU/CUDA.jl#461: sliced setindex" begin - @test compare((X,Y)->(X[1,:] = Y), AT, zeros(2,2), ones(2)) + @test compare((X,Y)->(X[1,:] = Y), AT, zeros(Float32, 2,2), ones(Float32, 2)) end end diff --git a/test/testsuite/io.jl b/test/testsuite/io.jl index b4a1ee43..fc871154 100644 --- a/test/testsuite/io.jl +++ b/test/testsuite/io.jl @@ -1,4 +1,4 @@ -@testsuite "input output" AT->begin +@testsuite "input output" (AT, eltypes)->begin # compact=false to avoid type aliases replstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :compact => false, :limit => true, :displaysize => (24, 80), kv...), MIME("text/plain"), x), x) diff --git a/test/testsuite/linalg.jl b/test/testsuite/linalg.jl index 430593a3..3f2ec371 100644 --- a/test/testsuite/linalg.jl +++ b/test/testsuite/linalg.jl @@ -1,5 +1,5 @@ -@testsuite "linalg" AT->begin - @testset "adjoint and trspose" begin +@testsuite "linalg" (AT, eltypes)->begin + @testset "adjoint and transpose" begin @test compare(adjoint, AT, rand(Float32, 32, 32)) @test compare(adjoint!, AT, rand(Float32, 32, 32), rand(Float32, 32, 32)) @test compare(transpose, AT, rand(Float32, 32, 32)) @@ -14,7 +14,7 @@ @test compare(x -> permutedims(x, (2, 1)), AT, rand(Float32, 2, 3)) @test compare(x -> permutedims(x, (2, 1, 3)), AT, rand(Float32, 4, 5, 6)) @test compare(x -> permutedims(x, (3, 1, 2)), AT, rand(Float32, 4, 5, 6)) - @test compare(x -> permutedims(x, [2,1,4,3]), AT, randn(ComplexF64,3,4,5,1)) + @test compare(x -> permutedims(x, [2,1,4,3]), AT, randn(ComplexF32,3,4,5,1)) end @testset "issymmetric/ishermitian" begin @@ -23,6 +23,9 @@ aimg = randn(n,n)/2 @testset for eltya in (Float32, Float64, ComplexF32, ComplexF64) + if !(eltya in eltypes) + continue + end a = convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal) asym = transpose(a) + a # symmetric indefinite aherm = a' + a # Hermitian indefinite @@ -32,7 +35,7 @@ end end -@testsuite "linalg/triangular" AT->begin +@testsuite "linalg/triangular" (AT, eltypes)->begin @testset "copytri!" begin @testset for uplo in ('U', 'L') @test compare(x -> LinearAlgebra.copytri!(x, uplo), AT, rand(Float32, 128, 128)) @@ -69,7 +72,7 @@ end end end -@testsuite "linalg/diagonal" AT->begin +@testsuite "linalg/diagonal" (AT, eltypes)->begin @testset "Array + Diagonal" begin n = 128 A = AT(rand(Float32, (n,n))) @@ -90,13 +93,13 @@ end @testset "$f! with diagonal $d" for (f, f!) in ((triu, triu!), (tril, tril!)), d in -2:2 - A = randn(10, 10) + A = randn(Float32, 10, 10) @test f(A, d) == Array(f!(AT(A), d)) end end -@testsuite "linalg/mul!" AT->begin - @testset "$T gemv y := $f(A) * x * a + y * b" for f in (identity, transpose, adjoint), T in supported_eltypes() +@testsuite "linalg/mul!" (AT, eltypes)->begin + @testset "$T gemv y := $f(A) * x * a + y * b" for f in (identity, transpose, adjoint), T in eltypes y, A, x = rand(T, 4), rand(T, 4, 4), rand(T, 4) # workaround for https://github.com/JuliaLang/julia/issues/35163#issue-584248084 @@ -105,14 +108,14 @@ end @test compare(*, AT, f(A), x) @test compare(mul!, AT, y, f(A), x) @test compare(mul!, AT, y, f(A), x, Ref(T(4)), Ref(T(5))) - @test typeof(AT(rand(3, 3)) * AT(rand(3))) <: AbstractVector + @test typeof(AT(rand(T, 3, 3)) * AT(rand(T, 3))) <: AbstractVector if f !== identity @test compare(mul!, AT, rand(T, 2,2), rand(T, 2,1), f(rand(T, 2))) end end - @testset "$T gemm C := $f(A) * $g(B) * a + C * b" for f in (identity, transpose, adjoint), g in (identity, transpose, adjoint), T in supported_eltypes() + @testset "$T gemm C := $f(A) * $g(B) * a + C * b" for f in (identity, transpose, adjoint), g in (identity, transpose, adjoint), T in eltypes A, B, C = rand(T, 4, 4), rand(T, 4, 4), rand(T, 4, 4) # workaround for https://github.com/JuliaLang/julia/issues/35163#issue-584248084 @@ -121,23 +124,26 @@ end @test compare(*, AT, f(A), g(B)) @test compare(mul!, AT, C, f(A), g(B)) @test compare(mul!, AT, C, f(A), g(B), Ref(T(4)), Ref(T(5))) - @test typeof(AT(rand(3, 3)) * AT(rand(3, 3))) <: AbstractMatrix + @test typeof(AT(rand(T, 3, 3)) * AT(rand(T, 3, 3))) <: AbstractMatrix end - @testset "lmul! and rmul!" for (a,b) in [((3,4),(4,3)), ((3,), (1,3)), ((1,3), (3))], T in supported_eltypes() + @testset "lmul! and rmul!" for (a,b) in [((3,4),(4,3)), ((3,), (1,3)), ((1,3), (3))], T in eltypes @test compare(rmul!, AT, rand(T, a), Ref(rand(T))) @test compare(lmul!, AT, Ref(rand(T)), rand(T, b)) end @testset "$p-norm($sz x $T)" for sz in [(2,), (2,2), (2,2,2)], p in Any[1, 2, 3, Inf, -Inf], - T in supported_eltypes() + T in eltypes + if T <: Complex || T == Int8 + continue + end range = T <: Integer ? (T(1):T(10)) : T # prevent integer overflow @test compare(norm, AT, rand(range, sz), Ref(p)) end end -@testsuite "linalg/symmetrix" AT->begin +@testsuite "linalg/symmetrix" (AT, eltypes)->begin @testset "Hermitian" begin A = rand(Float32,2,2) A = A*A'+I #posdef diff --git a/test/testsuite/math.jl b/test/testsuite/math.jl index 0cc7d006..81bd7b1f 100644 --- a/test/testsuite/math.jl +++ b/test/testsuite/math.jl @@ -1,8 +1,5 @@ -@testsuite "math/intrinsics" AT->begin - for ET in supported_eltypes() - # Skip complex numbers - ET in (Complex, ComplexF32, ComplexF64) && continue - +@testsuite "math/intrinsics" (AT, eltypes)->begin + for ET in filter(!iscomplextype, eltypes) T = AT{ET} @testset "$ET" begin range = ET <: Integer ? (ET(-2):ET(2)) : ET @@ -17,8 +14,8 @@ end end -@testsuite "math/power" AT->begin - for ET in supported_eltypes() +@testsuite "math/power" (AT, eltypes)->begin + for ET in eltypes for p in 0:5 compare(x->x^p, AT, rand(ET, 2,2)) end diff --git a/test/testsuite/random.jl b/test/testsuite/random.jl index 3d86a174..7f6182fd 100644 --- a/test/testsuite/random.jl +++ b/test/testsuite/random.jl @@ -1,4 +1,4 @@ -@testsuite "random" AT->begin +@testsuite "random" (AT, eltypes)->begin rng = if AT <: AbstractGPUArray GPUArrays.default_rng(AT) else @@ -6,10 +6,7 @@ end @testset "rand" begin # uniform - for T in (Int16, Int32, Int64, - Float16, Float32, Float64, - Complex{Float16}, Complex{Float32}, Complex{Float64}, - Complex{Int32}, Complex{Int64}), d in (10, (10,10)) + for T in eltypes, d in (10, (10,10)) A = AT{T}(undef, d) B = copy(A) rand!(rng, A) @@ -34,7 +31,7 @@ end @testset "randn" begin # normally-distributed - for T in (Float16, Float32, Float64), d in (2, (2,2)) + for T in filter(isfloattype, eltypes), d in (2, (2,2)) A = AT{T}(undef, d) B = copy(A) randn!(rng, A) diff --git a/test/testsuite/reductions.jl b/test/testsuite/reductions.jl index a07ce04e..546d8a7c 100644 --- a/test/testsuite/reductions.jl +++ b/test/testsuite/reductions.jl @@ -1,5 +1,5 @@ -@testsuite "reductions/mapreducedim!" AT->begin - @testset "$ET" for ET in supported_eltypes() +@testsuite "reductions/mapreducedim!" (AT, eltypes)->begin + @testset "$ET" for ET in eltypes range = ET <: Real ? (ET(1):ET(10)) : ET for (sz,red) in [(10,)=>(1,), (10,10)=>(1,1), (10,10,10)=>(1,1,1), (10,10,10)=>(10,10,10), (10,10,10)=>(1,10,10), (10,10,10)=>(10,1,10), (10,10,10)=>(10,10,1)] @@ -14,8 +14,8 @@ end end -@testsuite "reductions/reducedim!" AT->begin - @testset "$ET" for ET in supported_eltypes() +@testsuite "reductions/reducedim!" (AT, eltypes)->begin + @testset "$ET" for ET in eltypes range = ET <: Real ? (ET(1):ET(10)) : ET for (sz,red) in [(10,)=>(1,), (10,10)=>(1,1), (10,10,10)=>(1,1,1), (10,10,10)=>(10,10,10), (10,10,10)=>(1,10,10), (10,10,10)=>(10,1,10), (10,10,10)=>(10,10,1)] @@ -25,8 +25,8 @@ end end end -@testsuite "reductions/mapreduce" AT->begin - @testset "$ET" for ET in supported_eltypes() +@testsuite "reductions/mapreduce" (AT, eltypes)->begin + @testset "$ET" for ET in eltypes range = ET <: Real ? (ET(1):ET(10)) : ET for (sz,dims) in [(10,)=>[1], (10,10)=>[1,2], (10,10,10)=>[1,2,3], (10,10,10)=>[], (10,)=>:, (10,10)=>:, (10,10,10)=>:, @@ -38,8 +38,8 @@ end end end -@testsuite "reductions/reduce" AT->begin - @testset "$ET" for ET in supported_eltypes() +@testsuite "reductions/reduce" (AT, eltypes)->begin + @testset "$ET" for ET in eltypes range = ET <: Real ? (ET(1):ET(10)) : ET for (sz,dims) in [(10,)=>[1], (10,10)=>[1,2], (10,10,10)=>[1,2,3], (10,10,10)=>[], (10,)=>:, (10,10)=>:, (10,10,10)=>:, @@ -50,34 +50,35 @@ end end end -@testsuite "reductions/sum prod" AT->begin - @testset "$ET" for ET in supported_eltypes() - range = ET <: Real ? (ET(1):ET(10)) : ET - for (sz,dims) in [(10,)=>[1], (10,10)=>[1,2], (10,10,10)=>[1,2,3], (10,10,10)=>[], - (10,)=>:, (10,10)=>:, (10,10,10)=>:, - (10,10,10)=>[1], (10,10,10)=>[2], (10,10,10)=>[3]] - @test compare(A->sum(A), AT, rand(range, sz)) - @test compare(A->sum(abs, A), AT, rand(range, sz)) - @test compare(A->sum(A; dims=dims), AT, rand(range, sz)) - @test compare(A->prod(A), AT, rand(range, sz)) - @test compare(A->prod(abs, A), AT, rand(range, sz)) - @test compare(A->prod(A; dims=dims), AT, rand(range, sz)) - end +@testsuite "reductions/sum prod" (AT, eltypes)->begin + for ET in eltypes + @testset "$ET" begin + range = ET <: Real ? (ET(1):ET(10)) : ET + for (sz,dims) in [(10,)=>[1], (10,10)=>[1,2], (10,10,10)=>[1,2,3], (10,10,10)=>[], + (10,)=>:, (10,10)=>:, (10,10,10)=>:, + (10,10,10)=>[1], (10,10,10)=>[2], (10,10,10)=>[3]] + @test compare(A->sum(A), AT, rand(range, sz)) + @test compare(A->sum(abs, A), AT, rand(range, sz)) + @test compare(A->sum(A; dims=dims), AT, rand(range, sz)) + @test compare(A->prod(A), AT, rand(range, sz)) + @test compare(A->prod(abs, A), AT, rand(range, sz)) + @test compare(A->prod(A; dims=dims), AT, rand(range, sz)) + end - OT = isbitstype(widen(ET)) ? widen(ET) : ET - if OT in supported_eltypes() - # smaller-scale test to avoid very large values and roundoff issues - for (sz,red) in [(2,)=>(1,), (2,2)=>(1,1), (2,2,2)=>(1,1,1), (2,2,2)=>(2,2,2), - (2,2,2)=>(1,2,2), (2,2,2)=>(2,1,2), (2,2,2)=>(2,2,1)] - @test compare((A,R)->sum!(R, A), AT, rand(range, sz), rand(OT, red)) - @test compare((A,R)->prod!(R, A), AT, rand(range, sz), rand(OT, red)) + if ET in (Float32, Float64, Int64, ComplexF32, ComplexF64) + # smaller-scale test to avoid very large values and roundoff issues + for (sz,red) in [(2,)=>(1,), (2,2)=>(1,1), (2,2,2)=>(1,1,1), (2,2,2)=>(2,2,2), + (2,2,2)=>(1,2,2), (2,2,2)=>(2,1,2), (2,2,2)=>(2,2,1)] + @test compare((A,R)->sum!(R, A), AT, rand(range, sz), rand(ET, red)) + @test compare((A,R)->prod!(R, A), AT, rand(range, sz), rand(ET, red)) + end end end end end -@testsuite "reductions/minimum maximum" AT->begin - @testset "$ET" for ET in supported_eltypes() +@testsuite "reductions/minimum maximum" (AT, eltypes)->begin + @testset "$ET" for ET in eltypes range = ET <: Real ? (ET(1):ET(10)) : ET for (sz,dims) in [(10,)=>[1], (10,10)=>[1,2], (10,10,10)=>[1,2,3], (10,10,10)=>[], (10,)=>:, (10,10)=>:, (10,10,10)=>:, @@ -102,7 +103,7 @@ end end end -@testsuite "reductions/any all count" AT->begin +@testsuite "reductions/any all count" (AT, eltypes)->begin for Ac in ([false, false], [false, true], [true, true], [false false; false false], [false true; false false], [true true; false false], [true true; true true]) diff --git a/test/testsuite/statistics.jl b/test/testsuite/statistics.jl index 2c873d1d..44c5f4af 100644 --- a/test/testsuite/statistics.jl +++ b/test/testsuite/statistics.jl @@ -1,52 +1,61 @@ using Statistics -@testsuite "statistics" AT->begin - @testset "std" begin - @test compare(std, AT, rand(10)) - @test compare(std, AT, rand(10,1,2)) - @test compare(std, AT, rand(10,1,2); corrected=true) - @test compare(std, AT, rand(10,1,2); dims=1) - end +@testsuite "statistics" (AT, eltypes)->begin + for ET in eltypes + if !(ET in [Float16, Float32, Float64]) + continue + end + @testset "std" begin + @test compare(std, AT, rand(ET, 10)) + @test compare(std, AT, rand(ET, 10,1,2)) + @test compare(std, AT, rand(ET, 10,1,2); corrected=true) + @test compare(std, AT, rand(ET, 10,1,2); dims=1) + end - @testset "var" begin - @test compare(var, AT, rand(10)) - @test compare(var, AT, rand(10,1,2)) - @test compare(var, AT, rand(10,1,2); corrected=true) - @test compare(var, AT, rand(10,1,2); dims=1) - @test compare(var, AT, rand(10,1,2); dims=[1]) - @test compare(var, AT, rand(10,1,2); dims=(1,)) - @test compare(var, AT, rand(10,1,2); dims=[2,3]) - @test compare(var, AT, rand(10,1,2); dims=(2,3)) - end + @testset "var" begin + @test compare(var, AT, rand(ET, 10)) + @test compare(var, AT, rand(ET, 10,1,2)) + @test compare(var, AT, rand(ET, 10,1,2); corrected=true) + @test compare(var, AT, rand(ET, 10,1,2); dims=1) + @test compare(var, AT, rand(ET, 10,1,2); dims=[1]) + @test compare(var, AT, rand(ET, 10,1,2); dims=(1,)) + @test compare(var, AT, rand(ET, 10,1,2); dims=[2,3]) + @test compare(var, AT, rand(ET, 10,1,2); dims=(2,3)) + end - @testset "mean" begin - @test compare(mean, AT, rand(2,2)) - @test compare(mean, AT, rand(2,2); dims=2) - @test compare(mean, AT, rand(2,2,2); dims=[1,3]) - @test compare(x->mean(sin, x), AT, rand(2,2)) - @test compare(x->mean(sin, x; dims=2), AT, rand(2,2)) - @test compare(x->mean(sin, x; dims=[1,3]), AT, rand(2,2,2)) + @testset "mean" begin + @test compare(mean, AT, rand(ET, 2, 2)) + @test compare(mean, AT, rand(ET, 2, 2); dims=2) + @test compare(mean, AT, rand(ET, 2, 2, 2); dims=[1,3]) + @test compare(x->mean(sin, x), AT, rand(ET, 2,2)) + @test compare(x->mean(sin, x; dims=2), AT, rand(ET, 2,2)) + @test compare(x->mean(sin, x; dims=[1,3]), AT, rand(ET, 2,2,2)) + end end - @testset "cov" begin - s = 100 - @test compare(cov, AT, rand(s)) - @test compare(cov, AT, rand(Complex{Float64}, s)) - @test compare(cov, AT, rand(s, 2)) - @test compare(cov, AT, rand(Complex{Float64}, s, 2)) - @test compare(cov, AT, rand(s, 2); dims=2) - @test compare(cov, AT, rand(Complex{Float64}, s, 2); dims=2) - @test compare(cov, AT, rand(1:100, s)) - end + for ET in eltypes + # Doesn't work with ComplexF32 in oneAPI for some reason. + if !(ET in [Float32, Float64, Float16, ComplexF64]) + continue + end + @testset "cov" begin + s = 100 + @test compare(cov, AT, rand(ET, s)) + @test compare(cov, AT, rand(ET, s, 2)) + @test compare(cov, AT, rand(ET, s, 2); dims=2) + if ET <: Real + @test compare(cov, AT, rand(ET(1):ET(100), s)) + end + end - @testset "cor" begin - s = 100 - @test compare(cor, AT, rand(s)) - @test compare(cor, AT, rand(Complex{Float64}, s)) - @test compare(cor, AT, rand(s, 2)) - @test compare(cor, AT, rand(Complex{Float64}, s, 2)) - @test compare(cor, AT, rand(s, 2); dims=2) - @test compare(cor, AT, rand(Complex{Float64}, s, 2); dims=2) - @test compare(cor, AT, rand(1:100, s)) + @testset "cor" begin + s = 100 + @test compare(cor, AT, rand(ET, s)) + @test compare(cor, AT, rand(ET, s, 2)) + @test compare(cor, AT, rand(ET, s, 2); dims=2) + if ET <: Real + @test compare(cor, AT, rand(ET(1):ET(100), s)) + end + end end end diff --git a/test/testsuite/uniformscaling.jl b/test/testsuite/uniformscaling.jl index 61793fbc..f331bdca 100644 --- a/test/testsuite/uniformscaling.jl +++ b/test/testsuite/uniformscaling.jl @@ -1,4 +1,4 @@ -@testsuite "uniformscaling" AT->begin +@testsuite "uniformscaling" (AT, eltypes)->begin eltypes = (ComplexF32, Float32) wrappers = (identity, UnitLowerTriangular, UnitUpperTriangular, LowerTriangular, UpperTriangular, Hermitian, Symmetric) diff --git a/test/testsuite/vector.jl b/test/testsuite/vector.jl index c5f8bf40..adbe2c7a 100644 --- a/test/testsuite/vector.jl +++ b/test/testsuite/vector.jl @@ -1,4 +1,4 @@ -@testsuite "vectors" AT->begin +@testsuite "vectors" (AT, eltypes)->begin a = Float32[] x = AT(a) @test length(x) == 0