-
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.
- Loading branch information
Showing
8 changed files
with
122 additions
and
27 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
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,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(ε)}(ε) | ||
end | ||
|
||
ADTypes.mode(::AutoSimpleFiniteDiff) = ForwardMode() | ||
check_available(::AutoSimpleFiniteDiff) = true | ||
inplace_support(::AutoSimpleFiniteDiff) = InPlaceSupported() | ||
|
||
function BatchSizeSettings(::AutoSimpleFiniteDiff{nothing}, N::Integer) | ||
B = min(12, N) | ||
singlebatch = B == N | ||
aligned = N % B == 0 | ||
return BatchSizeSettings{B,singlebatch,aligned}(N) | ||
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) | ||
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 | ||
yε = f(x + ε * dx, map(unwrap, contexts)...) | ||
y0 = f(x, map(unwrap, contexts)...) | ||
(yε - y0) / ε | ||
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)...) | ||
yε = copy(y) | ||
f!(y, x, map(unwrap, contexts)...) | ||
y0 = copy(y) | ||
(yε - y0) / ε | ||
end | ||
f!(y, x, map(unwrap, contexts)...) | ||
return y, ty | ||
end |
29 changes: 11 additions & 18 deletions
29
...Interface/test/Misc/FromPrimitive/test.jl → ...erface/test/Misc/SimpleFiniteDiff/test.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