Skip to content

Commit d0393fc

Browse files
committed
test: add proper empty tests
1 parent 8f93335 commit d0393fc

9 files changed

Lines changed: 47 additions & 16 deletions

File tree

DifferentiationInterface/src/utils/batchsize.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Configuration for the batch size deduced from a backend and a sample array of le
66
# Type parameters
77
88
- `B::Int`: batch size
9-
- `singlebatch::Bool`: whether `B == N` (`B > N` is not allowed)
9+
- `singlebatch::Bool`: whether `B == N` (`B > N` is only allowed when `N == 0`)
1010
- `aligned::Bool`: whether `N % B == 0`
1111
1212
# Fields
@@ -127,7 +127,9 @@ Reproduces the heuristic from ForwardDiff to minimize
127127
Source: https://github.com/JuliaDiff/ForwardDiff.jl/blob/ec74fbc32b10bbf60b3c527d8961666310733728/src/prelude.jl#L19-L29
128128
"""
129129
function reasonable_batchsize(N::Integer, Bmax::Integer)
130-
if N <= Bmax
130+
if N == 0
131+
return 1
132+
elseif N <= Bmax
131133
return N
132134
else
133135
A = div(N, Bmax, RoundUp)

DifferentiationInterface/test/Back/Enzyme/test.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,11 @@ end
201201
@test occursin("DifferentiationInterface", msg)
202202
end
203203
end
204+
205+
@testset "Empty arrays" begin
206+
test_differentiation(
207+
[AutoEnzyme(; mode=Enzyme.Forward), AutoEnzyme(; mode=Enzyme.Reverse)],
208+
empty_scenarios();
209+
excluded=[:jacobian],
210+
)
211+
end;

DifferentiationInterface/test/Core/Internals/batchsize.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using Test
1313
BSS = BatchSizeSettings
1414

1515
@testset "Default" begin
16+
@test (@inferred pick_batchsize(AutoZygote(), zeros(0))) isa BSS{1,false,true}
1617
@test (@inferred pick_batchsize(AutoZygote(), zeros(2))) isa BSS{1,false,true}
1718
@test (@inferred pick_batchsize(AutoZygote(), zeros(100))) isa BSS{1,false,true}
1819
@test_throws ArgumentError pick_batchsize(AutoSparse(AutoZygote()), zeros(2))
@@ -25,7 +26,7 @@ BSS = BatchSizeSettings
2526
end
2627

2728
@testset "SimpleFiniteDiff (adaptive)" begin
28-
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(0))) isa BSS{0,true,true}
29+
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(0))) isa BSS{1,false,true}
2930
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(2))) isa BSS{2,true,true}
3031
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(6))) isa BSS{6,true,true}
3132
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(12))) isa BSS{12,true,true}

DifferentiationInterface/test/Core/ZeroBackends/test.jl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,7 @@ end
5353
end
5454

5555
@testset "Empty arrays" begin
56-
make_empty(t) = typeof(t)[]
57-
make_empty!(y, t) = nothing
58-
@test gradient(sum, AutoZeroForward(), Float64[]) == Float64[]
59-
@test derivative(make_empty, AutoZeroReverse(), 1.0) == Float64[]
60-
@test derivative(make_empty!, Float64[], AutoZeroReverse(), 1.0) == Float64[]
61-
@test_broken jacobian(copy, AutoZeroForward(), Float64[]) == I(0)
62-
@test_broken jacobian(copy, AutoZeroReverse(), Float64[]) == I(0)
63-
@test_broken jacobian(copyto!, Float64[], AutoZeroForward(), Float64[]) == I(0)
64-
@test_broken jacobian(copyto!, Float64[], AutoZeroReverse(), Float64[]) == I(0)
65-
end
56+
test_differentiation(
57+
[AutoZeroForward(), AutoZeroReverse()], empty_scenarios(); excluded=[:jacobian]
58+
)
59+
end;

DifferentiationInterface/test/testutils.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ using DifferentiationInterfaceTest:
1010
complex_sparse_scenarios,
1111
static_scenarios,
1212
component_scenarios,
13-
gpu_scenarios
13+
gpu_scenarios,
14+
empty_scenarios
1415

1516
function MyAutoSparse(backend::AbstractADType)
1617
return AutoSparse(

DifferentiationInterfaceTest/src/DifferentiationInterfaceTest.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ using DifferentiationInterface: Rewrap, Context, Constant, Cache, ConstantOrCach
9393
using DifferentiationInterface: PreparationMismatchError
9494
using DocStringExtensions: TYPEDFIELDS, TYPEDSIGNATURES
9595
using JET: @test_opt
96-
using LinearAlgebra: Adjoint, Diagonal, Transpose, dot, parent
96+
using LinearAlgebra: Adjoint, Diagonal, Transpose, I, dot, parent
9797
using ProgressMeter: ProgressUnknown, next!
9898
using Random: AbstractRNG, default_rng, rand!
9999
using SparseArrays:
@@ -124,6 +124,7 @@ include("scenarios/default.jl")
124124
include("scenarios/sparse.jl")
125125
include("scenarios/complex.jl")
126126
include("scenarios/allocfree.jl")
127+
include("scenarios/empty.jl")
127128
include("scenarios/extensions.jl")
128129

129130
include("tests/correctness_eval.jl")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
make_empty(t::Number) = typeof(t)[]
2+
function make_empty!(y::AbstractArray, t::Number)
3+
@assert isempty(y)
4+
return nothing
5+
end
6+
7+
function empty_scenarios()
8+
scens = Scenario[
9+
Scenario{:derivative,:out}(make_empty, 1.0; res1=Float64[]),
10+
Scenario{:derivative,:out}(make_empty!, Float64[], 1.0; res1=Float64[]),
11+
Scenario{:gradient,:out}(sum, Float64[]; res1=Float64[]),
12+
Scenario{:jacobian,:out}(copy, Float64[]; res1=float.(I(0))),
13+
Scenario{:jacobian,:out}(copyto!, Float64[], Float64[]; res1=float.(I(0))),
14+
]
15+
return scens
16+
end

DifferentiationInterfaceTest/test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ using DifferentiationInterfaceTest:
1212
complex_sparse_scenarios,
1313
static_scenarios,
1414
component_scenarios,
15-
gpu_scenarios
15+
gpu_scenarios,
16+
empty_scenarios
1617

1718
GROUP = get(ENV, "JULIA_DIT_TEST_GROUP", "All")
1819

DifferentiationInterfaceTest/test/standard.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ test_differentiation(
2828
logging=LOGGING,
2929
)
3030

31+
test_differentiation(
32+
[AutoForwardDiff()], empty_scenarios(); excluded=[:gradient], logging=LOGGING
33+
)
34+
test_differentiation(
35+
[AutoFiniteDiff()], empty_scenarios(); excluded=[:jacobian], logging=LOGGING
36+
)
37+
3138
## Sparse
3239

3340
sparse_backend = AutoSparse(

0 commit comments

Comments
 (0)