Skip to content

Commit

Permalink
Make CI faster
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Dec 4, 2024
1 parent 031ea47 commit 1adc2ff
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
group:
- Misc/Internals
- Misc/DifferentiateWith
- Misc/FromPrimitive
- Misc/SimpleFiniteDiff
- Misc/SparsityDetector
- Misc/ZeroBackends
- Back/ChainRules
Expand Down
1 change: 1 addition & 0 deletions DifferentiationInterface/src/DifferentiationInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ include("fallbacks/change_prep.jl")
include("misc/differentiate_with.jl")
include("misc/from_primitive.jl")
include("misc/sparsity_detector.jl")
include("misc/simple_finite_diff.jl")
include("misc/zero_backends.jl")

## Exported
Expand Down
4 changes: 3 additions & 1 deletion DifferentiationInterface/src/misc/from_primitive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ function BatchSizeSettings(fromprim::FromPrimitive, N::Integer)
return BatchSizeSettings(fromprim.backend, N)
end

## Forward
## Forward (no longer used)

#=
struct AutoForwardFromPrimitive{B} <: FromPrimitive
backend::B
end
Expand Down Expand Up @@ -104,6 +105,7 @@ function value_and_pushforward!(
f!, y, ty, prep.pushforward_prep, fromprim.backend, x, tx, contexts...
)
end
=#

## Reverse

Expand Down
93 changes: 93 additions & 0 deletions DifferentiationInterface/src/misc/simple_finite_diff.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
AutoSimpleFiniteDiff <: ADTypes.AbstractADType
Forward mode backend based on the finite difference `(f(x + ε) - f(x)) / ε`, with artificial chunk size to mimick ForwardDiff.
# Constructor
AutoSimpleFiniteDiff(ε=1e-5; chunksize=nothing)
"""
struct AutoSimpleFiniteDiff{chunksize,T<:Real} <: AbstractADType
ε::T
end

function AutoSimpleFiniteDiff=1e-5; chunksize=nothing)
return AutoSimpleFiniteDiff{chunksize,typeof(ε)}(ε)

Check warning on line 15 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L14-L15

Added lines #L14 - L15 were not covered by tests
end

ADTypes.mode(::AutoSimpleFiniteDiff) = ForwardMode()
check_available(::AutoSimpleFiniteDiff) = true
inplace_support(::AutoSimpleFiniteDiff) = InPlaceSupported()

Check warning on line 20 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L18-L20

Added lines #L18 - L20 were not covered by tests

function BatchSizeSettings(::AutoSimpleFiniteDiff{nothing}, N::Integer)
B = min(12, N)
singlebatch = B == N
aligned = N % B == 0
return BatchSizeSettings{B,singlebatch,aligned}(N)

Check warning on line 26 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L22-L26

Added lines #L22 - L26 were not covered by tests
end

function BatchSizeSettings(::AutoSimpleFiniteDiff{chunksize}, N::Integer) where {chunksize}
@assert chunksize <= N
B = chunksize
singlebatch = B == N
aligned = N % B == 0
return BatchSizeSettings{B,singlebatch,aligned}(N)

Check warning on line 34 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L29-L34

Added lines #L29 - L34 were not covered by tests
end

function threshold_batchsize(

Check warning on line 37 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L37

Added line #L37 was not covered by tests
backend::AutoSimpleFiniteDiff{chunksize1}, chunksize2::Integer
) where {chunksize1}
chunksize = isnothing(chunksize1) ? nothing : min(chunksize1, chunksize2)
return AutoSimpleFiniteDiff(backend.ε; chunksize)

Check warning on line 41 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L40-L41

Added lines #L40 - L41 were not covered by tests
end

function prepare_pushforward(

Check warning on line 44 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L44

Added line #L44 was not covered by tests
f::F, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C}
) where {F,C}
return NoPushforwardPrep()

Check warning on line 47 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L47

Added line #L47 was not covered by tests
end

function prepare_pushforward(

Check warning on line 50 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L50

Added line #L50 was not covered by tests
f!::F, y, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C}
) where {F,C}
return NoPushforwardPrep()

Check warning on line 53 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L53

Added line #L53 was not covered by tests
end

function value_and_pushforward(

Check warning on line 56 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L56

Added line #L56 was not covered by tests
f::F,
::NoPushforwardPrep,
backend::AutoSimpleFiniteDiff,
x,
tx::NTuple{B},
contexts::Vararg{Context,C},
) where {F,B,C}
ε = eltype(x)(backend.ε)
y = f(x, map(unwrap, contexts)...)
ty = map(tx) do dx
= f(x + ε * dx, map(unwrap, contexts)...)
y0 = f(x, map(unwrap, contexts)...)
(yε - y0) / ε

Check warning on line 69 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L64-L69

Added lines #L64 - L69 were not covered by tests
end
return y, ty

Check warning on line 71 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L71

Added line #L71 was not covered by tests
end

function value_and_pushforward(

Check warning on line 74 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L74

Added line #L74 was not covered by tests
f!::F,
y,
::NoPushforwardPrep,
backend::AutoSimpleFiniteDiff,
x,
tx::NTuple{B},
contexts::Vararg{Context,C},
) where {F,B,C}
ε = eltype(x)(backend.ε)
ty = map(tx) do dx
f!(y, x + ε * dx, map(unwrap, contexts)...)
= copy(y)
f!(y, x, map(unwrap, contexts)...)
y0 = copy(y)
(yε - y0) / ε

Check warning on line 89 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L83-L89

Added lines #L83 - L89 were not covered by tests
end
f!(y, x, map(unwrap, contexts)...)
return y, ty

Check warning on line 92 in DifferentiationInterface/src/misc/simple_finite_diff.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/misc/simple_finite_diff.jl#L91-L92

Added lines #L91 - L92 were not covered by tests
end
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
using DifferentiationInterface, DifferentiationInterfaceTest
using DifferentiationInterface: AutoForwardFromPrimitive, AutoReverseFromPrimitive
using DifferentiationInterface: AutoSimpleFiniteDiff, AutoReverseFromPrimitive
using DifferentiationInterfaceTest
using ForwardDiff: ForwardDiff
using Test

LOGGING = get(ENV, "CI", "false") == "false"

backends = [ #
AutoForwardFromPrimitive(AutoForwardDiff(; chunksize=5)),
AutoReverseFromPrimitive(AutoForwardDiff(; chunksize=4)),
AutoSimpleFiniteDiff(; chunksize=5),
AutoReverseFromPrimitive(AutoSimpleFiniteDiff(; chunksize=4)),
]

second_order_backends = [ #
SecondOrder(
AutoForwardFromPrimitive(AutoForwardDiff(; chunksize=5)),
AutoReverseFromPrimitive(AutoForwardDiff(; chunksize=4)),
AutoSimpleFiniteDiff(; chunksize=5),
AutoReverseFromPrimitive(AutoSimpleFiniteDiff(; chunksize=4)),
),
SecondOrder(
AutoReverseFromPrimitive(AutoForwardDiff(; chunksize=5)),
AutoForwardFromPrimitive(AutoForwardDiff(; chunksize=4)),
AutoReverseFromPrimitive(AutoSimpleFiniteDiff(; chunksize=5)),
AutoSimpleFiniteDiff(; chunksize=4),
),
]

adaptive_backends = [ #
AutoForwardFromPrimitive(AutoForwardDiff()),
AutoReverseFromPrimitive(AutoForwardDiff()),
SecondOrder(
AutoForwardFromPrimitive(AutoForwardDiff()),
AutoReverseFromPrimitive(AutoForwardDiff()),
),
SecondOrder(
AutoReverseFromPrimitive(AutoForwardDiff()),
AutoForwardFromPrimitive(AutoForwardDiff()),
),
AutoSimpleFiniteDiff(),
AutoReverseFromPrimitive(AutoSimpleFiniteDiff()),
SecondOrder(AutoSimpleFiniteDiff(), AutoReverseFromPrimitive(AutoSimpleFiniteDiff())),
SecondOrder(AutoReverseFromPrimitive(AutoSimpleFiniteDiff()), AutoSimpleFiniteDiff()),
]

for backend in vcat(backends, second_order_backends)
Expand Down
4 changes: 4 additions & 0 deletions DifferentiationInterfaceTest/src/scenarios/scenario.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ function adapt_batchsize(backend::AbstractADType, scen::Scenario)
return backend
end
end

function no_matrices(scens::AbstractVector{<:Scenario})
return filter(s -> !isa(s.x, AbstractMatrix) && !isa(s.y, AbstractMatrix), scens)
end
5 changes: 4 additions & 1 deletion DifferentiationInterfaceTest/test/weird.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ static_scenarios(;
## Weird arrays

test_differentiation(
AutoForwardDiff(), static_scenarios(); benchmark=:full, logging=LOGGING
AutoForwardDiff(),
DIT.no_matrices(static_scenarios());
benchmark=:prepared,
logging=LOGGING,
)

test_differentiation(AutoForwardDiff(), component_scenarios(); logging=LOGGING)
Expand Down
11 changes: 5 additions & 6 deletions DifferentiationInterfaceTest/test/zero_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using ADTypes
using DifferentiationInterface
using DifferentiationInterface: AutoZeroForward, AutoZeroReverse
using DifferentiationInterfaceTest
using DifferentiationInterfaceTest: allocfree_scenarios
using DifferentiationInterfaceTest: allocfree_scenarios, no_matrices
using Test

LOGGING = get(ENV, "CI", "false") == "false"
Expand All @@ -29,17 +29,16 @@ test_differentiation(

data0 = benchmark_differentiation(
AutoZeroForward(),
default_scenarios(; include_batchified=false, include_constantified=true);
no_matrices(default_scenarios(; include_batchified=false, include_constantified=true));
logging=LOGGING,
benchmark_seconds=0.1,
);

data1 = benchmark_differentiation(
AutoZeroForward(),
default_scenarios(; include_batchified=false);
no_matrices(default_scenarios(; include_batchified=false));
benchmark=:full,
logging=LOGGING,
benchmark_seconds=0.2,
benchmark_seconds=0.05,
benchmark_aggregation=maximum,
);

Expand All @@ -48,7 +47,7 @@ ADTypes.mode(::FakeBackend) = ADTypes.ForwardMode()

data2 = benchmark_differentiation(
FakeBackend(),
default_scenarios(; include_batchified=false);
no_matrices(default_scenarios(; include_batchified=false));
logging=false,
benchmark_test=false,
);
Expand Down

0 comments on commit 1adc2ff

Please sign in to comment.