|
1 | | -abstract type FromPrimitive <: AbstractADType end |
| 1 | +abstract type FromPrimitive{inplace} <: AbstractADType end |
2 | 2 |
|
3 | 3 | check_available(fromprim::FromPrimitive) = check_available(fromprim.backend) |
4 | | -inplace_support(fromprim::FromPrimitive) = inplace_support(fromprim.backend) |
| 4 | +inplace_support(::FromPrimitive{true}) = InPlaceSupported() |
| 5 | +inplace_support(::FromPrimitive{false}) = InPlaceNotSupported() |
5 | 6 |
|
6 | 7 | function pick_batchsize(fromprim::FromPrimitive, N::Integer) |
7 | 8 | return pick_batchsize(fromprim.backend, N) |
8 | 9 | end |
9 | 10 |
|
| 11 | +""" |
| 12 | + AutoForwardFromPrimitive |
| 13 | +
|
| 14 | +Wrapper which forces a given backend to act as a reverse-mode backend. |
| 15 | +
|
| 16 | +Used in internal testing. |
| 17 | +""" |
| 18 | +struct AutoForwardFromPrimitive{inplace,B<:AbstractADType} <: FromPrimitive{inplace} |
| 19 | + backend::B |
| 20 | +end |
| 21 | + |
| 22 | +function AutoForwardFromPrimitive(backend::AbstractADType; inplace=true) |
| 23 | + return AutoForwardFromPrimitive{inplace,typeof(backend)}(backend) |
| 24 | +end |
| 25 | + |
| 26 | +ADTypes.mode(::AutoForwardFromPrimitive) = ADTypes.ForwardMode() |
| 27 | + |
| 28 | +function threshold_batchsize( |
| 29 | + fromprim::AutoForwardFromPrimitive{inplace}, dimension::Integer |
| 30 | +) where {inplace} |
| 31 | + return AutoForwardFromPrimitive( |
| 32 | + threshold_batchsize(fromprim.backend, dimension); inplace |
| 33 | + ) |
| 34 | +end |
| 35 | + |
| 36 | +struct FromPrimitivePushforwardPrep{E<:PushforwardPrep} <: PushforwardPrep |
| 37 | + pushforward_prep::E |
| 38 | +end |
| 39 | + |
| 40 | +function prepare_pushforward( |
| 41 | + f::F, fromprim::AutoForwardFromPrimitive, x, tx::NTuple, contexts::Vararg{Context,C} |
| 42 | +) where {F,C} |
| 43 | + primitive_prep = prepare_pushforward(f, fromprim.backend, x, tx, contexts...) |
| 44 | + return FromPrimitivePushforwardPrep(primitive_prep) |
| 45 | +end |
| 46 | + |
| 47 | +function prepare_pushforward( |
| 48 | + f!::F, y, fromprim::AutoForwardFromPrimitive, x, tx::NTuple, contexts::Vararg{Context,C} |
| 49 | +) where {F,C} |
| 50 | + primitive_prep = prepare_pushforward(f!, y, fromprim.backend, x, tx, contexts...) |
| 51 | + return FromPrimitivePushforwardPrep(primitive_prep) |
| 52 | +end |
| 53 | + |
| 54 | +function value_and_pushforward( |
| 55 | + f::F, |
| 56 | + prep::FromPrimitivePushforwardPrep, |
| 57 | + fromprim::AutoForwardFromPrimitive, |
| 58 | + x, |
| 59 | + tx::NTuple, |
| 60 | + contexts::Vararg{Context,C}, |
| 61 | +) where {F,C} |
| 62 | + return value_and_pushforward( |
| 63 | + f, prep.pushforward_prep, fromprim.backend, x, tx, contexts... |
| 64 | + ) |
| 65 | +end |
| 66 | + |
| 67 | +function value_and_pushforward( |
| 68 | + f!::F, |
| 69 | + y, |
| 70 | + prep::FromPrimitivePushforwardPrep, |
| 71 | + fromprim::AutoForwardFromPrimitive, |
| 72 | + x, |
| 73 | + tx::NTuple, |
| 74 | + contexts::Vararg{Context,C}, |
| 75 | +) where {F,C} |
| 76 | + return value_and_pushforward( |
| 77 | + f!, y, prep.pushforward_prep, fromprim.backend, x, tx, contexts... |
| 78 | + ) |
| 79 | +end |
| 80 | + |
| 81 | +function value_and_pushforward!( |
| 82 | + f::F, |
| 83 | + ty::NTuple, |
| 84 | + prep::FromPrimitivePushforwardPrep, |
| 85 | + fromprim::AutoForwardFromPrimitive, |
| 86 | + x, |
| 87 | + tx::NTuple, |
| 88 | + contexts::Vararg{Context,C}, |
| 89 | +) where {F,C} |
| 90 | + return value_and_pushforward!( |
| 91 | + f, ty, prep.pushforward_prep, fromprim.backend, x, tx, contexts... |
| 92 | + ) |
| 93 | +end |
| 94 | + |
| 95 | +function value_and_pushforward!( |
| 96 | + f!::F, |
| 97 | + y, |
| 98 | + ty::NTuple, |
| 99 | + prep::FromPrimitivePushforwardPrep, |
| 100 | + fromprim::AutoForwardFromPrimitive, |
| 101 | + x, |
| 102 | + tx::NTuple, |
| 103 | + contexts::Vararg{Context,C}, |
| 104 | +) where {F,C} |
| 105 | + return value_and_pushforward!( |
| 106 | + f!, y, ty, prep.pushforward_prep, fromprim.backend, x, tx, contexts... |
| 107 | + ) |
| 108 | +end |
| 109 | + |
10 | 110 | """ |
11 | 111 | AutoReverseFromPrimitive |
12 | 112 |
|
13 | 113 | Wrapper which forces a given backend to act as a reverse-mode backend. |
14 | 114 |
|
15 | 115 | Used in internal testing. |
16 | 116 | """ |
17 | | -struct AutoReverseFromPrimitive{inplace,B<:AbstractADType} <: FromPrimitive |
| 117 | +struct AutoReverseFromPrimitive{inplace,B<:AbstractADType} <: FromPrimitive{inplace} |
18 | 118 | backend::B |
19 | 119 | end |
20 | 120 |
|
21 | 121 | function AutoReverseFromPrimitive(backend::AbstractADType; inplace=true) |
22 | 122 | return AutoReverseFromPrimitive{inplace,typeof(backend)}(backend) |
23 | 123 | end |
24 | 124 |
|
25 | | -inplace_support(::AutoReverseFromPrimitive{true}) = InPlaceSupported() |
26 | | -inplace_support(::AutoReverseFromPrimitive{false}) = InPlaceNotSupported() |
27 | 125 | ADTypes.mode(::AutoReverseFromPrimitive) = ADTypes.ReverseMode() |
28 | 126 |
|
29 | | -function threshold_batchsize(fromprim::AutoReverseFromPrimitive, dimension::Integer) |
30 | | - return AutoReverseFromPrimitive(threshold_batchsize(fromprim.backend, dimension)) |
| 127 | +function threshold_batchsize( |
| 128 | + fromprim::AutoReverseFromPrimitive{inplace}, dimension::Integer |
| 129 | +) where {inplace} |
| 130 | + return AutoReverseFromPrimitive( |
| 131 | + threshold_batchsize(fromprim.backend, dimension); inplace |
| 132 | + ) |
31 | 133 | end |
32 | 134 |
|
33 | 135 | struct FromPrimitivePullbackPrep{E<:PullbackPrep} <: PullbackPrep |
|
0 commit comments