|
| 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 |
0 commit comments