Skip to content

Commit 472c027

Browse files
authored
Rename extras to prep (#491)
* Rename extras to prep * Typos * Remove map fix
1 parent f518a6b commit 472c027

64 files changed

Lines changed: 1665 additions & 1863 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DifferentiationInterface/docs/src/dev_guide.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ Most operators have 4 variants, which look like this in the first order: `operat
2323
To implement a new operator for an existing backend, you need to write 5 methods: 1 for [preparation](@ref Preparation) and 4 corresponding to the variants of the operator (see above).
2424
For first-order operators, you may also want to support [in-place functions](@ref "Mutation and signatures"), which requires another 5 methods (defined on `f!` instead of `f`).
2525

26-
The method `prepare_operator` must output an `extras` object of the correct type.
27-
For instance, `prepare_gradient(f, backend, x)` must return a [`DifferentiationInterface.GradientExtras`](@ref).
28-
Assuming you don't need any preparation for said operator, you can use the trivial extras that are already defined, like `DifferentiationInterface.NoGradientExtras`.
29-
Otherwise, define a custom struct like `MyGradientExtras <: DifferentiationInterface.GradientExtras` and put the necessary storage in there.
26+
The method `prepare_operator` must output a `prep` object of the correct type.
27+
For instance, `prepare_gradient(f, backend, x)` must return a [`DifferentiationInterface.GradientPrep`](@ref).
28+
Assuming you don't need any preparation for said operator, you can use the trivial prep that are already defined, like `DifferentiationInterface.NoGradientPrep`.
29+
Otherwise, define a custom struct like `MyGradientPrep <: DifferentiationInterface.GradientPrep` and put the necessary storage in there.
3030

3131
## New backend
3232

DifferentiationInterface/docs/src/explanation/operators.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,28 @@ In addition, the preparation syntax depends on the number of arguments accepted
107107
| out-of-place function | `prepare_op(f, backend, x, [t])` |
108108
| in-place function | `prepare_op(f!, y, backend, x, [t])` |
109109

110-
Preparation creates an object called `extras` which contains the the necessary information to speed up an operator and its variants.
111-
The idea is that you prepare only once, which can be costly, but then call the operator several times while reusing the same `extras`.
110+
Preparation creates an object called `prep` which contains the the necessary information to speed up an operator and its variants.
111+
The idea is that you prepare only once, which can be costly, but then call the operator several times while reusing the same `prep`.
112112

113113
```julia
114114
op(f, backend, x, [t]) # slow because it includes preparation
115-
op(f, extras, backend, x, [t]) # fast because it skips preparation
115+
op(f, prep, backend, x, [t]) # fast because it skips preparation
116116
```
117117

118118
!!! warning
119-
The `extras` object is the last argument before `backend` and it is always mutated, regardless of the bang `!` in the operator name.
119+
The `prep` object is the last argument before `backend` and it is always mutated, regardless of the bang `!` in the operator name.
120120

121121
### Reusing preparation
122122

123123
Deciding whether it is safe to reuse the results of preparation is not easy.
124124
Here are the general rules that we strive to implement:
125125

126-
For different-point preparation, the output `extras` of `prepare_op(f, b, x, [t])` can be reused in `op(f, extras, b, other_x, [other_t])`, provided that:
126+
For different-point preparation, the output `prep` of `prepare_op(f, b, x, [t])` can be reused in `op(f, prep, b, other_x, [other_t])`, provided that:
127127

128128
- the inputs `x` and `other_x` have similar types and equal shapes
129129
- the tangents in `t` and `other_t` have similar types and equal shapes
130130

131-
For same-point preparation, the output `extras` of `prepare_op_same_point(f, b, x, [t])` can be reused in `op(f, extras, b, x, other_t)`, provided that:
131+
For same-point preparation, the output `prep` of `prepare_op_same_point(f, b, x, [t])` can be reused in `op(f, prep, b, x, other_t)`, provided that:
132132

133133
- the input `x` remains the same
134134
- the tangents in `t` and `other_t` have similar types and equal shapes

DifferentiationInterface/docs/src/tutorials/advanced.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ gradient(f_multiarg, backend, x, Constant(10))
4545
Preparation also works in this case, even if the constant changes before execution:
4646

4747
```@example tuto_advanced
48-
extras_other_constant = prepare_gradient(f_multiarg, backend, x, Constant(-1))
49-
gradient(f_multiarg, extras_other_constant, backend, x, Constant(10))
48+
prep_other_constant = prepare_gradient(f_multiarg, backend, x, Constant(-1))
49+
gradient(f_multiarg, prep_other_constant, backend, x, Constant(10))
5050
```
5151

5252
## Sparsity
@@ -120,15 +120,15 @@ The speedup becomes very visible in large dimensions.
120120

121121
```@example tuto_advanced
122122
n = 1000
123-
jac_extras_dense = prepare_jacobian(f_sparse_vector, dense_first_order_backend, zeros(n))
124-
jac_extras_sparse = prepare_jacobian(f_sparse_vector, sparse_first_order_backend, zeros(n))
123+
jac_prep_dense = prepare_jacobian(f_sparse_vector, dense_first_order_backend, zeros(n))
124+
jac_prep_sparse = prepare_jacobian(f_sparse_vector, sparse_first_order_backend, zeros(n))
125125
nothing # hide
126126
```
127127

128128
```@example tuto_advanced
129-
@benchmark jacobian($f_sparse_vector, $jac_extras_dense, $dense_first_order_backend, $(randn(n)))
129+
@benchmark jacobian($f_sparse_vector, $jac_prep_dense, $dense_first_order_backend, $(randn(n)))
130130
```
131131

132132
```@example tuto_advanced
133-
@benchmark jacobian($f_sparse_vector, $jac_extras_sparse, $sparse_first_order_backend, $(randn(n)))
133+
@benchmark jacobian($f_sparse_vector, $jac_prep_sparse, $sparse_first_order_backend, $(randn(n)))
134134
```

DifferentiationInterface/docs/src/tutorials/basic.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,26 @@ These objects can be reused between gradient computations, even on different inp
8080
We abstract away the preparation step behind a backend-agnostic syntax:
8181

8282
```@example tuto_basic
83-
extras = prepare_gradient(f, backend, zero(x))
83+
prep = prepare_gradient(f, backend, zero(x))
8484
```
8585

8686
You don't need to know what this object is, you just need to pass it to the gradient operator.
8787
Note that preparation does not depend on the actual components of the vector `x`, just on its type and size.
88-
You can thus reuse the `extras` for different values of the input.
88+
You can thus reuse the `prep` for different values of the input.
8989

9090
```@example tuto_basic
9191
grad = similar(x)
92-
gradient!(f, grad, extras, backend, x)
92+
gradient!(f, grad, prep, backend, x)
9393
grad # has been mutated
9494
```
9595

9696
Preparation makes the gradient computation much faster, and (in this case) allocation-free.
9797

9898
```@example tuto_basic
99-
@benchmark gradient!($f, $grad, $extras, $backend, $x)
99+
@benchmark gradient!($f, $grad, $prep, $backend, $x)
100100
```
101101

102-
Beware that the `extras` object is nearly always mutated by differentiation operators, even though it is given as the last positional argument.
102+
Beware that the `prep` object is nearly always mutated by differentiation operators, even though it is given as the last positional argument.
103103

104104
## Switching backends
105105

@@ -121,9 +121,9 @@ gradient(f, backend2, x)
121121
And you can run the same benchmarks to see what you gained (although such a small input may not be realistic):
122122

123123
```@example tuto_basic
124-
extras2 = prepare_gradient(f, backend2, zero(x))
124+
prep2 = prepare_gradient(f, backend2, zero(x))
125125
126-
@benchmark gradient!($f, $grad, $extras2, $backend2, $x)
126+
@benchmark gradient!($f, $grad, $prep2, $backend2, $x)
127127
```
128128

129129
In short, DifferentiationInterface.jl allows for easy testing and comparison of AD backends.

DifferentiationInterface/ext/DifferentiationInterfaceChainRulesCoreExt/DifferentiationInterfaceChainRulesCoreExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using ChainRulesCore:
1212
using Compat
1313
import DifferentiationInterface as DI
1414
using DifferentiationInterface:
15-
DifferentiateWith, NoPullbackExtras, NoPushforwardExtras, PullbackExtras, Tangents
15+
DifferentiateWith, NoPullbackPrep, NoPushforwardPrep, PullbackPrep, Tangents
1616

1717
ruleconfig(backend::AutoChainRules) = backend.ruleconfig
1818

DifferentiationInterface/ext/DifferentiationInterfaceChainRulesCoreExt/differentiate_with.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function ChainRulesCore.rrule(dw::DifferentiateWith, x)
22
@compat (; f, backend) = dw
33
y = f(x)
4-
extras_same = DI.prepare_pullback_same_point(f, backend, x, DI.Tangents(y))
4+
prep_same = DI.prepare_pullback_same_point(f, backend, x, DI.Tangents(y))
55
function pullbackfunc(dy)
6-
tx = DI.pullback(f, extras_same, backend, x, DI.Tangents(dy))
6+
tx = DI.pullback(f, prep_same, backend, x, DI.Tangents(dy))
77
return (NoTangent(), only(tx))
88
end
99
return y, pullbackfunc

DifferentiationInterface/ext/DifferentiationInterfaceChainRulesCoreExt/reverse_onearg.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
## Pullback
22

3-
struct ChainRulesPullbackExtrasSamePoint{Y,PB} <: PullbackExtras
3+
struct ChainRulesPullbackPrepSamePoint{Y,PB} <: PullbackPrep
44
y::Y
55
pb::PB
66
end
77

8-
DI.prepare_pullback(f, ::AutoReverseChainRules, x, ty::Tangents) = NoPullbackExtras()
8+
DI.prepare_pullback(f, ::AutoReverseChainRules, x, ty::Tangents) = NoPullbackPrep()
99

1010
function DI.prepare_pullback_same_point(
11-
f, ::NoPullbackExtras, backend::AutoReverseChainRules, x, ty::Tangents
11+
f, ::NoPullbackPrep, backend::AutoReverseChainRules, x, ty::Tangents
1212
)
1313
rc = ruleconfig(backend)
1414
y, pb = rrule_via_ad(rc, f, x)
15-
return ChainRulesPullbackExtrasSamePoint(y, pb)
15+
return ChainRulesPullbackPrepSamePoint(y, pb)
1616
end
1717

1818
function DI.value_and_pullback(
19-
f, ::NoPullbackExtras, backend::AutoReverseChainRules, x, ty::Tangents
19+
f, ::NoPullbackPrep, backend::AutoReverseChainRules, x, ty::Tangents
2020
)
2121
rc = ruleconfig(backend)
2222
y, pb = rrule_via_ad(rc, f, x)
@@ -27,19 +27,19 @@ function DI.value_and_pullback(
2727
end
2828

2929
function DI.value_and_pullback(
30-
f, extras::ChainRulesPullbackExtrasSamePoint, ::AutoReverseChainRules, x, ty::Tangents
30+
f, prep::ChainRulesPullbackPrepSamePoint, ::AutoReverseChainRules, x, ty::Tangents
3131
)
32-
@compat (; y, pb) = extras
32+
@compat (; y, pb) = prep
3333
tx = map(ty) do dy
3434
last(pb(dy))
3535
end
3636
return copy(y), tx
3737
end
3838

3939
function DI.pullback(
40-
f, extras::ChainRulesPullbackExtrasSamePoint, ::AutoReverseChainRules, x, ty::Tangents
40+
f, prep::ChainRulesPullbackPrepSamePoint, ::AutoReverseChainRules, x, ty::Tangents
4141
)
42-
@compat (; pb) = extras
42+
@compat (; pb) = prep
4343
tx = map(ty) do dy
4444
last(pb(dy))
4545
end

DifferentiationInterface/ext/DifferentiationInterfaceDiffractorExt/DifferentiationInterfaceDiffractorExt.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module DifferentiationInterfaceDiffractorExt
22

33
using ADTypes: ADTypes, AutoDiffractor
44
import DifferentiationInterface as DI
5-
using DifferentiationInterface: NoPushforwardExtras, Tangents
5+
using DifferentiationInterface: NoPushforwardPrep, Tangents
66
using Diffractor: DiffractorRuleConfig, TaylorTangentIndex, ZeroBundle, bundle, ∂☆
77

88
DI.check_available(::AutoDiffractor) = true
@@ -11,9 +11,9 @@ DI.pullback_performance(::AutoDiffractor) = DI.PullbackSlow()
1111

1212
## Pushforward
1313

14-
DI.prepare_pushforward(f, ::AutoDiffractor, x, tx::Tangents) = NoPushforwardExtras()
14+
DI.prepare_pushforward(f, ::AutoDiffractor, x, tx::Tangents) = NoPushforwardPrep()
1515

16-
function DI.pushforward(f, ::NoPushforwardExtras, ::AutoDiffractor, x, tx::Tangents)
16+
function DI.pushforward(f, ::NoPushforwardPrep, ::AutoDiffractor, x, tx::Tangents)
1717
ty = map(tx) do dx
1818
# code copied from Diffractor.jl
1919
z = ∂☆{1}()(ZeroBundle{1}(f), bundle(x, dx))
@@ -23,9 +23,9 @@ function DI.pushforward(f, ::NoPushforwardExtras, ::AutoDiffractor, x, tx::Tange
2323
end
2424

2525
function DI.value_and_pushforward(
26-
f, extras::NoPushforwardExtras, backend::AutoDiffractor, x, tx::Tangents
26+
f, prep::NoPushforwardPrep, backend::AutoDiffractor, x, tx::Tangents
2727
)
28-
return f(x), DI.pushforward(f, extras, backend, x, tx)
28+
return f(x), DI.pushforward(f, prep, backend, x, tx)
2929
end
3030

3131
end

DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/DifferentiationInterfaceEnzymeExt.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ using Base: Fix1
55
import DifferentiationInterface as DI
66
using DifferentiationInterface:
77
Context,
8-
DerivativeExtras,
9-
GradientExtras,
10-
JacobianExtras,
11-
HVPExtras,
12-
PullbackExtras,
13-
PushforwardExtras,
14-
NoDerivativeExtras,
15-
NoGradientExtras,
16-
NoHVPExtras,
17-
NoJacobianExtras,
18-
NoPullbackExtras,
19-
NoPushforwardExtras,
8+
DerivativePrep,
9+
GradientPrep,
10+
JacobianPrep,
11+
HVPPrep,
12+
PullbackPrep,
13+
PushforwardPrep,
14+
NoDerivativePrep,
15+
NoGradientPrep,
16+
NoHVPPrep,
17+
NoJacobianPrep,
18+
NoPullbackPrep,
19+
NoPushforwardPrep,
2020
Tangents,
2121
pick_batchsize
2222
using Enzyme:

0 commit comments

Comments
 (0)