Skip to content

Commit 881fc3f

Browse files
authored
test: make CI faster (#655)
* Make CI faster * Smaller epsilon * Use central finite diff * Import * Fix batch size * Fix batch size
1 parent 031ea47 commit 881fc3f

26 files changed

Lines changed: 272 additions & 133 deletions

File tree

.github/workflows/Test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ jobs:
3131
- "1.10"
3232
- "1"
3333
group:
34-
- Misc/Internals
35-
- Misc/DifferentiateWith
36-
- Misc/FromPrimitive
37-
- Misc/SparsityDetector
38-
- Misc/ZeroBackends
34+
- Core/Internals
35+
- Back/DifferentiateWith
36+
- Core/SimpleFiniteDiff
37+
- Back/SparsityDetector
38+
- Core/ZeroBackends
3939
- Back/ChainRules
4040
# - Back/Diffractor
4141
- Back/Enzyme

DifferentiationInterface/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DifferentiationInterface"
22
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
33
authors = ["Guillaume Dalle", "Adrian Hill"]
4-
version = "0.6.24"
4+
version = "0.6.25"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# until https://github.com/EnzymeAD/Enzyme.jl/pull/1545 is merged
22
function DI.BatchSizeSettings(::AutoEnzyme, N::Integer)
33
B = DI.reasonable_batchsize(N, 16)
4-
singlebatch = B == N
5-
aligned = N % B == 0
6-
return DI.BatchSizeSettings{B,singlebatch,aligned}(N)
4+
return DI.BatchSizeSettings{B}(N)
75
end
86

97
to_val(::DI.BatchSizeSettings{B}) where {B} = Val(B)

DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/utils.jl

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
function DI.BatchSizeSettings(::AutoForwardDiff{nothing}, N::Integer)
2-
B = ForwardDiff.pickchunksize(N)
3-
singlebatch = B == N
4-
aligned = N % B == 0
5-
return DI.BatchSizeSettings{B,singlebatch,aligned}(N)
2+
chunksize = ForwardDiff.pickchunksize(N)
3+
return DI.BatchSizeSettings{chunksize}(N)
64
end
75

86
function DI.BatchSizeSettings(::AutoForwardDiff{chunksize}, N::Integer) where {chunksize}
9-
if chunksize > N
10-
throw(ArgumentError("Fixed chunksize $chunksize larger than input size $N"))
11-
end
12-
B = chunksize
13-
singlebatch = B == N
14-
aligned = N % B == 0
15-
return DI.BatchSizeSettings{B,singlebatch,aligned}(N)
7+
return DI.BatchSizeSettings{chunksize}(N)
168
end
179

1810
function DI.threshold_batchsize(

DifferentiationInterface/ext/DifferentiationInterfaceStaticArraysExt/DifferentiationInterfaceStaticArraysExt.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ end
1414

1515
DI.ismutable_array(::Type{<:SArray}) = false
1616

17+
function DI.BatchSizeSettings(::DI.AutoSimpleFiniteDiff{nothing}, x::StaticArray)
18+
return DI.BatchSizeSettings{length(x),true,true}(length(x))
19+
end
20+
1721
function DI.BatchSizeSettings(::AutoForwardDiff{nothing}, x::StaticArray)
1822
return DI.BatchSizeSettings{length(x),true,true}(length(x))
1923
end
@@ -22,4 +26,16 @@ function DI.BatchSizeSettings(::AutoEnzyme, x::StaticArray)
2226
return DI.BatchSizeSettings{length(x),true,true}(length(x))
2327
end
2428

29+
function DI.BatchSizeSettings(
30+
::DI.AutoSimpleFiniteDiff{chunksize}, x::StaticArray
31+
) where {chunksize}
32+
return DI.BatchSizeSettings{chunksize}(Val(length(x)))
33+
end
34+
35+
function DI.BatchSizeSettings(
36+
::AutoForwardDiff{chunksize}, x::StaticArray
37+
) where {chunksize}
38+
return DI.BatchSizeSettings{chunksize}(Val(length(x)))
39+
end
40+
2541
end

DifferentiationInterface/src/DifferentiationInterface.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ include("fallbacks/change_prep.jl")
6262
include("misc/differentiate_with.jl")
6363
include("misc/from_primitive.jl")
6464
include("misc/sparsity_detector.jl")
65+
include("misc/simple_finite_diff.jl")
6566
include("misc/zero_backends.jl")
6667

6768
## Exported

DifferentiationInterface/src/misc/from_primitive.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ function BatchSizeSettings(fromprim::FromPrimitive, N::Integer)
1919
return BatchSizeSettings(fromprim.backend, N)
2020
end
2121

22-
## Forward
22+
## Forward (no longer used)
2323

24+
#=
2425
struct AutoForwardFromPrimitive{B} <: FromPrimitive
2526
backend::B
2627
end
@@ -104,6 +105,7 @@ function value_and_pushforward!(
104105
f!, y, ty, prep.pushforward_prep, fromprim.backend, x, tx, contexts...
105106
)
106107
end
108+
=#
107109

108110
## Reverse
109111

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
AutoSimpleFiniteDiff <: ADTypes.AbstractADType
3+
4+
Forward mode backend based on the finite difference `(f(x + ε) - f(x)) / ε`, with artificial chunk size to mimick ForwardDiff.
5+
6+
# Constructor
7+
8+
AutoSimpleFiniteDiff(ε=1e-5; chunksize=nothing)
9+
"""
10+
struct AutoSimpleFiniteDiff{chunksize,T<:Real} <: AbstractADType
11+
ε::T
12+
end
13+
14+
function AutoSimpleFiniteDiff=1e-5; chunksize=nothing)
15+
return AutoSimpleFiniteDiff{chunksize,typeof(ε)}(ε)
16+
end
17+
18+
ADTypes.mode(::AutoSimpleFiniteDiff) = ForwardMode()
19+
check_available(::AutoSimpleFiniteDiff) = true
20+
inplace_support(::AutoSimpleFiniteDiff) = InPlaceSupported()
21+
22+
function BatchSizeSettings(::AutoSimpleFiniteDiff{nothing}, N::Integer)
23+
B = reasonable_batchsize(N, 12)
24+
return BatchSizeSettings{B}(N)
25+
end
26+
27+
function BatchSizeSettings(::AutoSimpleFiniteDiff{chunksize}, N::Integer) where {chunksize}
28+
return BatchSizeSettings{chunksize}(N)
29+
end
30+
31+
function threshold_batchsize(
32+
backend::AutoSimpleFiniteDiff{chunksize1}, chunksize2::Integer
33+
) where {chunksize1}
34+
chunksize = isnothing(chunksize1) ? nothing : min(chunksize1, chunksize2)
35+
return AutoSimpleFiniteDiff(backend.ε; chunksize)
36+
end
37+
38+
function prepare_pushforward(
39+
f::F, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C}
40+
) where {F,C}
41+
return NoPushforwardPrep()
42+
end
43+
44+
function prepare_pushforward(
45+
f!::F, y, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C}
46+
) where {F,C}
47+
return NoPushforwardPrep()
48+
end
49+
50+
function value_and_pushforward(
51+
f::F,
52+
::NoPushforwardPrep,
53+
backend::AutoSimpleFiniteDiff,
54+
x,
55+
tx::NTuple{B},
56+
contexts::Vararg{Context,C},
57+
) where {F,B,C}
58+
ε = eltype(x)(backend.ε)
59+
y = f(x, map(unwrap, contexts)...)
60+
ty = map(tx) do dx
61+
y1 = f(x + ε * dx, map(unwrap, contexts)...)
62+
y0 = f(x - ε * dx, map(unwrap, contexts)...)
63+
(y1 - y0) / 2ε
64+
end
65+
return y, ty
66+
end
67+
68+
function value_and_pushforward(
69+
f!::F,
70+
y,
71+
::NoPushforwardPrep,
72+
backend::AutoSimpleFiniteDiff,
73+
x,
74+
tx::NTuple{B},
75+
contexts::Vararg{Context,C},
76+
) where {F,B,C}
77+
ε = eltype(x)(backend.ε)
78+
ty = map(tx) do dx
79+
f!(y, x + ε * dx, map(unwrap, contexts)...)
80+
y1 = copy(y)
81+
f!(y, x - ε * dx, map(unwrap, contexts)...)
82+
y0 = copy(y)
83+
(y1 - y0) / 2ε
84+
end
85+
f!(y, x, map(unwrap, contexts)...)
86+
return y, ty
87+
end

DifferentiationInterface/src/utils/batchsize.jl

Lines changed: 15 additions & 1 deletion
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`
9+
- `singlebatch::Bool`: whether `B == N` (`B > N` is not allowed)
1010
- `aligned::Bool`: whether `N % B == 0`
1111
1212
# Fields
@@ -22,11 +22,25 @@ struct BatchSizeSettings{B,singlebatch,aligned}
2222
end
2323

2424
function BatchSizeSettings{B,singlebatch,aligned}(N::Integer) where {B,singlebatch,aligned}
25+
B > N && throw(ArgumentError("Batch size $B larger than input size $N"))
2526
A = div(N, B, RoundUp)
2627
B_last = N % B
2728
return BatchSizeSettings{B,singlebatch,aligned}(N, A, B_last)
2829
end
2930

31+
function BatchSizeSettings{B}(::Val{N}) where {B,N}
32+
singlebatch = B == N
33+
aligned = N % B == 0
34+
return BatchSizeSettings{B,singlebatch,aligned}(N)
35+
end
36+
37+
function BatchSizeSettings{B}(N::Integer) where {B}
38+
# type-unstable
39+
singlebatch = B == N
40+
aligned = N % B == 0
41+
return BatchSizeSettings{B,singlebatch,aligned}(N)
42+
end
43+
3044
function BatchSizeSettings(::AbstractADType, N::Integer)
3145
B = 1
3246
singlebatch = false

DifferentiationInterface/test/Misc/DifferentiateWith/test.jl renamed to DifferentiationInterface/test/Back/DifferentiateWith/test.jl

File renamed without changes.

0 commit comments

Comments
 (0)