-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make CI faster * Smaller epsilon * Use central finite diff * Import * Fix batch size * Fix batch size
- Loading branch information
Showing
26 changed files
with
272 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 3 additions & 11 deletions
14
DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/utils.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
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(ε)}(ε) | ||
end | ||
|
||
ADTypes.mode(::AutoSimpleFiniteDiff) = ForwardMode() | ||
check_available(::AutoSimpleFiniteDiff) = true | ||
inplace_support(::AutoSimpleFiniteDiff) = InPlaceSupported() | ||
|
||
function BatchSizeSettings(::AutoSimpleFiniteDiff{nothing}, N::Integer) | ||
B = reasonable_batchsize(N, 12) | ||
return BatchSizeSettings{B}(N) | ||
end | ||
|
||
function BatchSizeSettings(::AutoSimpleFiniteDiff{chunksize}, N::Integer) where {chunksize} | ||
return BatchSizeSettings{chunksize}(N) | ||
end | ||
|
||
function threshold_batchsize( | ||
backend::AutoSimpleFiniteDiff{chunksize1}, chunksize2::Integer | ||
) where {chunksize1} | ||
chunksize = isnothing(chunksize1) ? nothing : min(chunksize1, chunksize2) | ||
return AutoSimpleFiniteDiff(backend.ε; chunksize) | ||
end | ||
|
||
function prepare_pushforward( | ||
f::F, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C} | ||
) where {F,C} | ||
return NoPushforwardPrep() | ||
end | ||
|
||
function prepare_pushforward( | ||
f!::F, y, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C} | ||
) where {F,C} | ||
return NoPushforwardPrep() | ||
end | ||
|
||
function value_and_pushforward( | ||
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 | ||
y1 = f(x + ε * dx, map(unwrap, contexts)...) | ||
y0 = f(x - ε * dx, map(unwrap, contexts)...) | ||
(y1 - y0) / 2ε | ||
end | ||
return y, ty | ||
end | ||
|
||
function value_and_pushforward( | ||
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)...) | ||
y1 = copy(y) | ||
f!(y, x - ε * dx, map(unwrap, contexts)...) | ||
y0 = copy(y) | ||
(y1 - y0) / 2ε | ||
end | ||
f!(y, x, map(unwrap, contexts)...) | ||
return y, ty | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
101 changes: 101 additions & 0 deletions
101
DifferentiationInterface/test/Core/Internals/batchsize.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using ADTypes | ||
using DifferentiationInterface | ||
using DifferentiationInterface: | ||
AutoSimpleFiniteDiff, | ||
BatchSizeSettings, | ||
pick_batchsize, | ||
reasonable_batchsize, | ||
threshold_batchsize | ||
import DifferentiationInterface as DI | ||
using StaticArrays | ||
using Test | ||
|
||
BSS = BatchSizeSettings | ||
|
||
@testset "Default" begin | ||
@test (@inferred pick_batchsize(AutoZygote(), zeros(2))) isa BSS{1,false,true} | ||
@test (@inferred pick_batchsize(AutoZygote(), zeros(100))) isa BSS{1,false,true} | ||
@test_throws ArgumentError pick_batchsize(AutoSparse(AutoZygote()), zeros(2)) | ||
@test_throws ArgumentError pick_batchsize( | ||
SecondOrder(AutoZygote(), AutoZygote()), zeros(2) | ||
) | ||
@test_throws ArgumentError pick_batchsize( | ||
MixedMode(AutoSimpleFiniteDiff(), AutoZygote()), zeros(2) | ||
) | ||
end | ||
|
||
@testset "SimpleFiniteDiff (adaptive)" begin | ||
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(2))) isa BSS{2,true,true} | ||
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(6))) isa BSS{6,true,true} | ||
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(12))) isa BSS{12,true,true} | ||
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(24))) isa BSS{12,false,true} | ||
@test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(100))) isa BSS{12,false,false} | ||
@test (@inferred pick_batchsize(AutoSimpleFiniteDiff(), @SVector(zeros(2)))) isa | ||
BSS{2,true,true} | ||
@test (@inferred pick_batchsize(AutoSimpleFiniteDiff(), @SVector(zeros(6)))) isa | ||
BSS{6,true,true} | ||
@test (@inferred pick_batchsize(AutoSimpleFiniteDiff(), @SVector(zeros(100)))) isa | ||
BSS{100,true,true} | ||
end | ||
|
||
@testset "SimpleFiniteDiff (fixed)" begin | ||
@test_throws ArgumentError pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(2)) | ||
@test_throws ArgumentError pick_batchsize( | ||
AutoSimpleFiniteDiff(; chunksize=4), @SVector(zeros(2)) | ||
) | ||
@test pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(6)) isa BSS{4} | ||
@test pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(100)) isa BSS{4} | ||
BSS{4,true,true} | ||
@test pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(99)) isa BSS{4} | ||
BSS{4,true,false} | ||
@test (@inferred pick_batchsize( | ||
AutoSimpleFiniteDiff(; chunksize=4), @SVector(zeros(6)) | ||
)) isa BSS{4,false,false} | ||
@test (@inferred pick_batchsize( | ||
AutoSimpleFiniteDiff(; chunksize=4), @SVector(zeros(100)) | ||
)) isa BSS{4,false,true} | ||
end | ||
|
||
@testset "Thresholding" begin | ||
@test threshold_batchsize(AutoSimpleFiniteDiff(), 2) isa AutoSimpleFiniteDiff{nothing} | ||
@test threshold_batchsize(AutoSimpleFiniteDiff(; chunksize=4), 2) isa | ||
AutoSimpleFiniteDiff{2} | ||
@test threshold_batchsize(AutoSimpleFiniteDiff(; chunksize=4), 6) isa | ||
AutoSimpleFiniteDiff{4} | ||
@test threshold_batchsize(AutoSparse(AutoSimpleFiniteDiff(; chunksize=4)), 2) isa | ||
AutoSparse{<:AutoSimpleFiniteDiff{2}} | ||
@test threshold_batchsize( | ||
SecondOrder( | ||
AutoSimpleFiniteDiff(; chunksize=4), AutoSimpleFiniteDiff(; chunksize=3) | ||
), | ||
6, | ||
) isa SecondOrder{<:AutoSimpleFiniteDiff{4},<:AutoSimpleFiniteDiff{3}} | ||
@test threshold_batchsize( | ||
SecondOrder( | ||
AutoSimpleFiniteDiff(; chunksize=4), AutoSimpleFiniteDiff(; chunksize=3) | ||
), | ||
2, | ||
) isa SecondOrder{<:AutoSimpleFiniteDiff{2},<:AutoSimpleFiniteDiff{2}} | ||
@test threshold_batchsize( | ||
SecondOrder( | ||
AutoSimpleFiniteDiff(; chunksize=1), AutoSimpleFiniteDiff(; chunksize=3) | ||
), | ||
2, | ||
) isa SecondOrder{<:AutoSimpleFiniteDiff{1},<:AutoSimpleFiniteDiff{2}} | ||
@test threshold_batchsize( | ||
SecondOrder( | ||
AutoSimpleFiniteDiff(; chunksize=4), AutoSimpleFiniteDiff(; chunksize=1) | ||
), | ||
2, | ||
) isa SecondOrder{<:AutoSimpleFiniteDiff{2},<:AutoSimpleFiniteDiff{1}} | ||
end | ||
|
||
@testset "Reasonable" begin | ||
for Bmax in 1:5 | ||
@test all(<=(Bmax), reasonable_batchsize.(1:10, Bmax)) | ||
@test issorted(div.(1:10, reasonable_batchsize.(1:10, Bmax), RoundUp)) | ||
if Bmax > 2 | ||
@test reasonable_batchsize(Bmax + 1, Bmax) < Bmax | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
881fc3f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register subdir=DifferentiationInterface
881fc3f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/120674
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: