|
| 1 | +## Pushforward |
| 2 | + |
| 3 | +struct MooncakeTwoArgPushforwardPrep{SIG,Tcache,DX,DY} <: DI.PushforwardPrep{SIG} |
| 4 | + _sig::Val{SIG} |
| 5 | + cache::Tcache |
| 6 | + dx_righttype::DX |
| 7 | + dy_righttype::DY |
| 8 | +end |
| 9 | + |
| 10 | +function DI.prepare_pushforward_nokwarg( |
| 11 | + strict::Val, |
| 12 | + f!::F, |
| 13 | + y, |
| 14 | + backend::AutoMooncakeForward, |
| 15 | + x, |
| 16 | + tx::NTuple, |
| 17 | + contexts::Vararg{DI.Context,C}; |
| 18 | +) where {F,C} |
| 19 | + _sig = DI.signature(f!, y, backend, x, tx, contexts...; strict) |
| 20 | + config = get_config(backend) |
| 21 | + cache = prepare_derivative_cache( |
| 22 | + f!, |
| 23 | + y, |
| 24 | + x, |
| 25 | + map(DI.unwrap, contexts)...; |
| 26 | + config.debug_mode, |
| 27 | + config.silence_debug_messages, |
| 28 | + ) |
| 29 | + dx_righttype = zero_tangent(x) |
| 30 | + dy_righttype = zero_tangent(y) |
| 31 | + prep = MooncakeTwoArgPushforwardPrep(_sig, cache, dx_righttype, dy_righttype) |
| 32 | + return prep |
| 33 | +end |
| 34 | + |
| 35 | +function DI.value_and_pushforward( |
| 36 | + f!::F, |
| 37 | + y, |
| 38 | + prep::MooncakeTwoArgPushforwardPrep, |
| 39 | + backend::AutoMooncakeForward, |
| 40 | + x::X, |
| 41 | + tx::NTuple, |
| 42 | + contexts::Vararg{DI.Context,C}; |
| 43 | +) where {F,C,X} |
| 44 | + DI.check_prep(f!, y, prep, backend, x, tx, contexts...) |
| 45 | + ty = map(tx) do dx |
| 46 | + dx_righttype = |
| 47 | + dx isa tangent_type(X) ? dx : _copy_to_output!!(prep.dx_righttype, dx) |
| 48 | + y_dual = zero_dual(y) |
| 49 | + value_and_derivative!!( |
| 50 | + prep.cache, |
| 51 | + zero_dual(f!), |
| 52 | + y_dual, |
| 53 | + Dual(x, dx_righttype), |
| 54 | + map(zero_dual ∘ DI.unwrap, contexts)..., |
| 55 | + ) |
| 56 | + dy = _copy_output(tangent(y_dual)) |
| 57 | + return dy |
| 58 | + end |
| 59 | + return y, ty |
| 60 | +end |
| 61 | + |
| 62 | +function DI.pushforward( |
| 63 | + f!::F, |
| 64 | + y, |
| 65 | + prep::MooncakeTwoArgPushforwardPrep, |
| 66 | + backend::AutoMooncakeForward, |
| 67 | + x, |
| 68 | + tx::NTuple, |
| 69 | + contexts::Vararg{DI.Context,C}; |
| 70 | +) where {F,C} |
| 71 | + DI.check_prep(f!, y, prep, backend, x, tx, contexts...) |
| 72 | + return DI.value_and_pushforward(f!, y, prep, backend, x, tx, contexts...)[2] |
| 73 | +end |
| 74 | + |
| 75 | +function DI.value_and_pushforward!( |
| 76 | + f!::F, |
| 77 | + y::Y, |
| 78 | + ty::NTuple, |
| 79 | + prep::MooncakeTwoArgPushforwardPrep, |
| 80 | + backend::AutoMooncakeForward, |
| 81 | + x::X, |
| 82 | + tx::NTuple, |
| 83 | + contexts::Vararg{DI.Context,C}; |
| 84 | +) where {F,C,X,Y} |
| 85 | + DI.check_prep(f!, y, prep, backend, x, tx, contexts...) |
| 86 | + foreach(tx, ty) do dx, dy |
| 87 | + dx_righttype = |
| 88 | + dx isa tangent_type(X) ? dx : _copy_to_output!!(prep.dx_righttype, dx) |
| 89 | + dy_righttype = |
| 90 | + dy isa tangent_type(Y) ? dy : _copy_to_output!!(prep.dy_righttype, dy) |
| 91 | + value_and_derivative!!( |
| 92 | + prep.cache, |
| 93 | + zero_dual(f!), |
| 94 | + Dual(y, dy_righttype), |
| 95 | + Dual(x, dx_righttype), |
| 96 | + map(zero_dual ∘ DI.unwrap, contexts)..., |
| 97 | + ) |
| 98 | + dy === dy_righttype || copyto!(dy, dy_righttype) |
| 99 | + end |
| 100 | + return y, ty |
| 101 | +end |
| 102 | + |
| 103 | +function DI.pushforward!( |
| 104 | + f!::F, |
| 105 | + y, |
| 106 | + ty::NTuple, |
| 107 | + prep::MooncakeTwoArgPushforwardPrep, |
| 108 | + backend::AutoMooncakeForward, |
| 109 | + x, |
| 110 | + tx::NTuple, |
| 111 | + contexts::Vararg{DI.Context,C}; |
| 112 | +) where {F,C} |
| 113 | + DI.check_prep(f!, y, prep, backend, x, tx, contexts...) |
| 114 | + DI.value_and_pushforward!(f!, y, ty, prep, backend, x, tx, contexts...) |
| 115 | + return ty |
| 116 | +end |
0 commit comments