Skip to content

Commit 6ae9532

Browse files
authored
docs: improve and automate docstrings (#742)
1 parent 9d2d9ba commit 6ae9532

21 files changed

+216
-163
lines changed

DifferentiationInterface/src/DifferentiationInterface.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ using ADTypes:
3636
using LinearAlgebra: dot
3737

3838
include("compat.jl")
39+
include("docstrings.jl")
3940

4041
include("first_order/mixed_mode.jl")
4142
include("second_order/second_order.jl")
@@ -45,7 +46,7 @@ include("utils/traits.jl")
4546
include("utils/basis.jl")
4647
include("utils/batchsize.jl")
4748
include("utils/check.jl")
48-
include("utils/printing.jl")
49+
include("utils/errors.jl")
4950
include("utils/context.jl")
5051
include("utils/linalg.jl")
5152
include("utils/sparse.jl")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function docstring_preptype(preptype::AbstractString, operator::AbstractString)
2+
return """
3+
$(preptype)
4+
5+
Abstract type for additional information needed by [`$(operator)`](@ref) and its variants.
6+
"""
7+
end
8+
9+
function samepoint_warning(samepoint::Bool)
10+
if samepoint
11+
", _if they are applied at the same point `x` and with the same `contexts`_"
12+
else
13+
""
14+
end
15+
end
16+
17+
function docstring_prepare(operator; samepoint=false, inplace=false)
18+
return """
19+
Create a `prep` object that can be given to [`$(operator)`](@ref) and its variants to speed them up$(samepoint_warning(samepoint)).
20+
21+
Depending on the backend, this can have several effects (preallocating memory, recording an execution trace) which are transparent to the user.
22+
23+
!!! warning
24+
The preparation result is only reusable as long as the arguments to `$operator` do not change type or size, and the function and backend themselves are not modified.
25+
Otherwise, preparation will be invalidated and you will need to run it again.
26+
$(inplace ? "\nFor in-place functions, `y` is mutated by `f!` during preparation." : "")
27+
"""
28+
end
29+
30+
function docstring_prepare!(operator)
31+
return """
32+
Same behavior as [`prepare_$(operator)`](@ref) but can resize the contents of an existing `prep` object to avoid some allocations.
33+
34+
There is no guarantee that `prep` will be mutated, or that performance will be improved compared to preparation from scratch.
35+
36+
!!! danger
37+
Compared to when `prep` was first created, the only authorized modification is a size change for input `x` or output `y`.
38+
Any other modification (like a change of type for the input) is not supported and will give erroneous results.
39+
40+
!!! danger
41+
For efficiency, this function needs to rely on backend package internals, therefore it not protected by semantic versioning.
42+
"""
43+
end
44+
45+
function docstring_preparation_hint(operator::AbstractString; same_point=false)
46+
if same_point
47+
return "To improve performance via operator preparation, refer to [`prepare_$(operator)`](@ref) and [`prepare_$(operator)_same_point`](@ref)."
48+
else
49+
return "To improve performance via operator preparation, refer to [`prepare_$(operator)`](@ref)."
50+
end
51+
end

DifferentiationInterface/src/first_order/derivative.jl

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@
44
prepare_derivative(f, backend, x, [contexts...]) -> prep
55
prepare_derivative(f!, y, backend, x, [contexts...]) -> prep
66
7-
Create a `prep` object that can be given to [`derivative`](@ref) and its variants.
8-
9-
!!! warning
10-
If the function changes in any way, the result of preparation will be invalidated, and you will need to run it again.
11-
For in-place functions, `y` is mutated by `f!` during preparation.
7+
$(docstring_prepare("derivative"; inplace=true))
128
"""
139
function prepare_derivative end
1410

1511
"""
1612
prepare!_derivative(f, prep, backend, x, [contexts...]) -> new_prep
1713
prepare!_derivative(f!, y, prep, backend, x, [contexts...]) -> new_prep
1814
19-
Same behavior as [`prepare_derivative`](@ref) but can modify an existing `prep` object to avoid some allocations.
20-
21-
There is no guarantee that `prep` will be mutated, or that performance will be improved compared to preparation from scratch.
22-
23-
!!! danger
24-
For efficiency, this function needs to rely on backend package internals, therefore it not protected by semantic versioning.
15+
$(docstring_prepare!("derivative"))
2516
"""
2617
function prepare!_derivative end
2718

@@ -31,7 +22,7 @@ function prepare!_derivative end
3122
3223
Compute the value and the derivative of the function `f` at point `x`.
3324
34-
$(document_preparation("derivative"))
25+
$(docstring_preparation_hint("derivative"))
3526
"""
3627
function value_and_derivative end
3728

@@ -41,7 +32,7 @@ function value_and_derivative end
4132
4233
Compute the value and the derivative of the function `f` at point `x`, overwriting `der`.
4334
44-
$(document_preparation("derivative"))
35+
$(docstring_preparation_hint("derivative"))
4536
"""
4637
function value_and_derivative! end
4738

@@ -51,7 +42,7 @@ function value_and_derivative! end
5142
5243
Compute the derivative of the function `f` at point `x`.
5344
54-
$(document_preparation("derivative"))
45+
$(docstring_preparation_hint("derivative"))
5546
"""
5647
function derivative end
5748

@@ -61,7 +52,7 @@ function derivative end
6152
6253
Compute the derivative of the function `f` at point `x`, overwriting `der`.
6354
64-
$(document_preparation("derivative"))
55+
$(docstring_preparation_hint("derivative"))
6556
"""
6657
function derivative! end
6758

DifferentiationInterface/src/first_order/gradient.jl

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@
33
"""
44
prepare_gradient(f, backend, x, [contexts...]) -> prep
55
6-
Create a `prep` object that can be given to [`gradient`](@ref) and its variants.
7-
8-
!!! warning
9-
If the function changes in any way, the result of preparation will be invalidated, and you will need to run it again.
6+
$(docstring_prepare("gradient"))
107
"""
118
function prepare_gradient end
129

1310
"""
1411
prepare!_gradient(f, prep, backend, x, [contexts...]) -> new_prep
1512
16-
Same behavior as [`prepare_gradient`](@ref) but can modify an existing `prep` object to avoid some allocations.
17-
18-
There is no guarantee that `prep` will be mutated, or that performance will be improved compared to preparation from scratch.
19-
20-
!!! danger
21-
For efficiency, this function needs to rely on backend package internals, therefore it not protected by semantic versioning.
13+
$(docstring_prepare!("gradient"))
2214
"""
2315
function prepare!_gradient end
2416

@@ -27,7 +19,7 @@ function prepare!_gradient end
2719
2820
Compute the value and the gradient of the function `f` at point `x`.
2921
30-
$(document_preparation("gradient"))
22+
$(docstring_preparation_hint("gradient"))
3123
"""
3224
function value_and_gradient end
3325

@@ -36,7 +28,7 @@ function value_and_gradient end
3628
3729
Compute the value and the gradient of the function `f` at point `x`, overwriting `grad`.
3830
39-
$(document_preparation("gradient"))
31+
$(docstring_preparation_hint("gradient"))
4032
"""
4133
function value_and_gradient! end
4234

@@ -45,7 +37,7 @@ function value_and_gradient! end
4537
4638
Compute the gradient of the function `f` at point `x`.
4739
48-
$(document_preparation("gradient"))
40+
$(docstring_preparation_hint("gradient"))
4941
"""
5042
function gradient end
5143

@@ -54,7 +46,7 @@ function gradient end
5446
5547
Compute the gradient of the function `f` at point `x`, overwriting `grad`.
5648
57-
$(document_preparation("gradient"))
49+
$(docstring_preparation_hint("gradient"))
5850
"""
5951
function gradient! end
6052

DifferentiationInterface/src/first_order/jacobian.jl

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@
44
prepare_jacobian(f, backend, x, [contexts...]) -> prep
55
prepare_jacobian(f!, y, backend, x, [contexts...]) -> prep
66
7-
Create a `prep` object that can be given to [`jacobian`](@ref) and its variants.
8-
9-
!!! warning
10-
If the function changes in any way, the result of preparation will be invalidated, and you will need to run it again.
11-
For in-place functions, `y` is mutated by `f!` during preparation.
7+
$(docstring_prepare("jacobian"; inplace=true))
128
"""
139
function prepare_jacobian end
1410

1511
"""
1612
prepare!_jacobian(f, prep, backend, x, [contexts...]) -> new_prep
1713
prepare!_jacobian(f!, y, prep, backend, x, [contexts...]) -> new_prep
1814
19-
Same behavior as [`prepare_jacobian`](@ref) but can modify an existing `prep` object to avoid some allocations.
20-
21-
There is no guarantee that `prep` will be mutated, or that performance will be improved compared to preparation from scratch.
22-
23-
!!! danger
24-
For efficiency, this function needs to rely on backend package internals, therefore it not protected by semantic versioning.
15+
$(docstring_prepare!("jacobian"))
2516
"""
2617
function prepare!_jacobian end
2718

@@ -31,7 +22,7 @@ function prepare!_jacobian end
3122
3223
Compute the value and the Jacobian matrix of the function `f` at point `x`.
3324
34-
$(document_preparation("jacobian"))
25+
$(docstring_preparation_hint("jacobian"))
3526
"""
3627
function value_and_jacobian end
3728

@@ -41,7 +32,7 @@ function value_and_jacobian end
4132
4233
Compute the value and the Jacobian matrix of the function `f` at point `x`, overwriting `jac`.
4334
44-
$(document_preparation("jacobian"))
35+
$(docstring_preparation_hint("jacobian"))
4536
"""
4637
function value_and_jacobian! end
4738

@@ -51,7 +42,7 @@ function value_and_jacobian! end
5142
5243
Compute the Jacobian matrix of the function `f` at point `x`.
5344
54-
$(document_preparation("jacobian"))
45+
$(docstring_preparation_hint("jacobian"))
5546
"""
5647
function jacobian end
5748

@@ -61,7 +52,7 @@ function jacobian end
6152
6253
Compute the Jacobian matrix of the function `f` at point `x`, overwriting `jac`.
6354
64-
$(document_preparation("jacobian"))
55+
$(docstring_preparation_hint("jacobian"))
6556
"""
6657
function jacobian! end
6758

DifferentiationInterface/src/first_order/mixed_mode.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
MixedMode
33
4-
Combination of a forward and a reverse mode backend for mixed-mode Jacobian computation.
4+
Combination of a forward and a reverse mode backend for mixed-mode sparse Jacobian computation.
55
66
!!! danger
7-
`MixedMode` backends only support [`jacobian`](@ref) and its variants.
7+
`MixedMode` backends only support [`jacobian`](@ref) and its variants, and it should be used inside an [`AutoSparse`](@extref ADTypes.AutoSparse) wrapper.
88
99
# Constructor
1010
@@ -20,8 +20,24 @@ struct MixedMode{F<:AbstractADType,R<:AbstractADType} <: AbstractADType
2020
end
2121
end
2222

23+
"""
24+
forward_backend(m::MixedMode)
25+
26+
Return the forward-mode part of a `MixedMode` backend.
27+
"""
2328
forward_backend(m::MixedMode) = m.forward
29+
30+
"""
31+
reverse_backend(m::MixedMode)
32+
33+
Return the reverse-mode part of a `MixedMode` backend.
34+
"""
2435
reverse_backend(m::MixedMode) = m.reverse
2536

37+
"""
38+
ForwardAndReverseMode <: ADTypes.AbstractMode
39+
40+
Appropriate mode type for `MixedMode` backends.
41+
"""
2642
struct ForwardAndReverseMode <: ADTypes.AbstractMode end
2743
ADTypes.mode(::MixedMode) = ForwardAndReverseMode()

DifferentiationInterface/src/first_order/pullback.jl

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,23 @@
44
prepare_pullback(f, backend, x, ty, [contexts...]) -> prep
55
prepare_pullback(f!, y, backend, x, ty, [contexts...]) -> prep
66
7-
Create a `prep` object that can be given to [`pullback`](@ref) and its variants.
8-
9-
!!! warning
10-
If the function changes in any way, the result of preparation will be invalidated, and you will need to run it again.
11-
For in-place functions, `y` is mutated by `f!` during preparation.
7+
$(docstring_prepare("pullback"; inplace=true))
128
"""
139
function prepare_pullback end
1410

1511
"""
1612
prepare!_pullback(f, prep, backend, x, ty, [contexts...]) -> new_prep
1713
prepare!_pullback(f!, y, prep, backend, x, ty, [contexts...]) -> new_prep
1814
19-
Same behavior as [`prepare_pullback`](@ref) but can modify an existing `prep` object to avoid some allocations.
20-
21-
There is no guarantee that `prep` will be mutated, or that performance will be improved compared to preparation from scratch.
22-
23-
!!! danger
24-
For efficiency, this function needs to rely on backend package internals, therefore it not protected by semantic versioning.
15+
$(docstring_prepare!("pullback"))
2516
"""
2617
function prepare!_pullback end
2718

2819
"""
2920
prepare_pullback_same_point(f, backend, x, ty, [contexts...]) -> prep_same
3021
prepare_pullback_same_point(f!, y, backend, x, ty, [contexts...]) -> prep_same
3122
32-
Create an `prep_same` object that can be given to [`pullback`](@ref) and its variants _if they are applied at the same point `x` and with the same `contexts`_.
33-
34-
!!! warning
35-
If the function or the point changes in any way, the result of preparation will be invalidated, and you will need to run it again.
36-
For in-place functions, `y` is mutated by `f!` during preparation.
23+
$(docstring_prepare("pullback"; samepoint=true, inplace=true))
3724
"""
3825
function prepare_pullback_same_point end
3926

@@ -43,7 +30,7 @@ function prepare_pullback_same_point end
4330
4431
Compute the value and the pullback of the function `f` at point `x` with a tuple of tangents `ty`.
4532
46-
$(document_preparation("pullback"; same_point=true))
33+
$(docstring_preparation_hint("pullback"; same_point=true))
4734
4835
!!! tip
4936
Pullbacks are also commonly called vector-Jacobian products or VJPs.
@@ -60,7 +47,7 @@ function value_and_pullback end
6047
6148
Compute the value and the pullback of the function `f` at point `x` with a tuple of tangents `ty`, overwriting `dx`.
6249
63-
$(document_preparation("pullback"; same_point=true))
50+
$(docstring_preparation_hint("pullback"; same_point=true))
6451
6552
!!! tip
6653
Pullbacks are also commonly called vector-Jacobian products or VJPs.
@@ -74,7 +61,7 @@ function value_and_pullback! end
7461
7562
Compute the pullback of the function `f` at point `x` with a tuple of tangents `ty`.
7663
77-
$(document_preparation("pullback"; same_point=true))
64+
$(docstring_preparation_hint("pullback"; same_point=true))
7865
7966
!!! tip
8067
Pullbacks are also commonly called vector-Jacobian products or VJPs.
@@ -88,7 +75,7 @@ function pullback end
8875
8976
Compute the pullback of the function `f` at point `x` with a tuple of tangents `ty`, overwriting `dx`.
9077
91-
$(document_preparation("pullback"; same_point=true))
78+
$(docstring_preparation_hint("pullback"; same_point=true))
9279
9380
!!! tip
9481
Pullbacks are also commonly called vector-Jacobian products or VJPs.

0 commit comments

Comments
 (0)