Skip to content

Commit e79bc20

Browse files
authored
Update operator doc strings (#347)
* Document alternative pushforward and pb names * Document operator preparation * Add `document_preparation` helper * Document preparation everywhere
1 parent 60cdb04 commit e79bc20

10 files changed

Lines changed: 102 additions & 3 deletions

File tree

DifferentiationInterface/src/DifferentiationInterface.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ include("utils/batch.jl")
5454
include("utils/check.jl")
5555
include("utils/exceptions.jl")
5656
include("utils/maybe.jl")
57+
include("utils/printing.jl")
5758

5859
include("first_order/pushforward.jl")
5960
include("first_order/pushforward_batched.jl")
@@ -77,8 +78,6 @@ include("misc/differentiate_with.jl")
7778
include("misc/sparsity_detector.jl")
7879
include("misc/from_primitive.jl")
7980

80-
include("utils/printing.jl")
81-
8281
function __init__()
8382
@require_extensions
8483
end

DifferentiationInterface/src/first_order/derivative.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function prepare_derivative end
1717
value_and_derivative(f!, y, backend, x, [extras]) -> (y, der)
1818
1919
Compute the value and the derivative of the function `f` at point `x`.
20+
21+
$(document_preparation("derivative"))
2022
"""
2123
function value_and_derivative end
2224

@@ -25,14 +27,18 @@ function value_and_derivative end
2527
value_and_derivative!(f!, y, der, backend, x, [extras]) -> (y, der)
2628
2729
Compute the value and the derivative of the function `f` at point `x`, overwriting `der`.
28-
"""
30+
31+
$(document_preparation("derivative"))
32+
"""
2933
function value_and_derivative! end
3034

3135
"""
3236
derivative(f, backend, x, [extras]) -> der
3337
derivative(f!, y, backend, x, [extras]) -> der
3438
3539
Compute the derivative of the function `f` at point `x`.
40+
41+
$(document_preparation("derivative"))
3642
"""
3743
function derivative end
3844

@@ -41,6 +47,8 @@ function derivative end
4147
derivative!(f!, y, der, backend, x, [extras]) -> der
4248
4349
Compute the derivative of the function `f` at point `x`, overwriting `der`.
50+
51+
$(document_preparation("derivative"))
4452
"""
4553
function derivative! end
4654

DifferentiationInterface/src/first_order/gradient.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,35 @@ function prepare_gradient end
1414
value_and_gradient(f, backend, x, [extras]) -> (y, grad)
1515
1616
Compute the value and the gradient of the function `f` at point `x`.
17+
18+
$(document_preparation("gradient"))
1719
"""
1820
function value_and_gradient end
1921

2022
"""
2123
value_and_gradient!(f, grad, backend, x, [extras]) -> (y, grad)
2224
2325
Compute the value and the gradient of the function `f` at point `x`, overwriting `grad`.
26+
27+
$(document_preparation("gradient"))
2428
"""
2529
function value_and_gradient! end
2630

2731
"""
2832
gradient(f, backend, x, [extras]) -> grad
2933
3034
Compute the gradient of the function `f` at point `x`.
35+
36+
$(document_preparation("gradient"))
3137
"""
3238
function gradient end
3339

3440
"""
3541
gradient!(f, grad, backend, x, [extras]) -> grad
3642
3743
Compute the gradient of the function `f` at point `x`, overwriting `grad`.
44+
45+
$(document_preparation("gradient"))
3846
"""
3947
function gradient! end
4048

DifferentiationInterface/src/first_order/jacobian.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function prepare_jacobian end
1717
value_and_jacobian(f!, y, backend, x, [extras]) -> (y, jac)
1818
1919
Compute the value and the Jacobian matrix of the function `f` at point `x`.
20+
21+
$(document_preparation("jacobian"))
2022
"""
2123
function value_and_jacobian end
2224

@@ -25,6 +27,8 @@ function value_and_jacobian end
2527
value_and_jacobian!(f!, y, jac, backend, x, [extras]) -> (y, jac)
2628
2729
Compute the value and the Jacobian matrix of the function `f` at point `x`, overwriting `jac`.
30+
31+
$(document_preparation("jacobian"))
2832
"""
2933
function value_and_jacobian! end
3034

@@ -33,6 +37,8 @@ function value_and_jacobian! end
3337
jacobian(f!, y, backend, x, [extras]) -> jac
3438
3539
Compute the Jacobian matrix of the function `f` at point `x`.
40+
41+
$(document_preparation("jacobian"))
3642
"""
3743
function jacobian end
3844

@@ -41,6 +47,8 @@ function jacobian end
4147
jacobian!(f!, y, jac, backend, x, [extras]) -> jac
4248
4349
Compute the Jacobian matrix of the function `f` at point `x`, overwriting `jac`.
50+
51+
$(document_preparation("jacobian"))
4452
"""
4553
function jacobian! end
4654

DifferentiationInterface/src/first_order/pullback.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ function prepare_pullback_same_point end
3030
3131
Compute the value and the pullback of the function `f` at point `x` with seed `dy`.
3232
33+
$(document_preparation("pullback"; same_point=true))
34+
35+
!!! tip
36+
Pullbacks are also commonly called vector-Jacobian products or VJPs.
37+
This function could have been named `value_and_vjp`.
38+
3339
!!! info
3440
Required primitive for reverse mode backends.
3541
"""
@@ -40,6 +46,12 @@ function value_and_pullback end
4046
value_and_pullback!(f!, y, dx, backend, x, dy, [extras]) -> (y, dx)
4147
4248
Compute the value and the pullback of the function `f` at point `x` with seed `dy`, overwriting `dx`.
49+
50+
$(document_preparation("pullback"; same_point=true))
51+
52+
!!! tip
53+
Pullbacks are also commonly called vector-Jacobian products or VJPs.
54+
This function could have been named `value_and_vjp!`.
4355
"""
4456
function value_and_pullback! end
4557

@@ -48,6 +60,12 @@ function value_and_pullback! end
4860
pullback(f!, y, backend, x, dy, [extras]) -> dx
4961
5062
Compute the pullback of the function `f` at point `x` with seed `dy`.
63+
64+
$(document_preparation("pullback"; same_point=true))
65+
66+
!!! tip
67+
Pullbacks are also commonly called vector-Jacobian products or VJPs.
68+
This function could have been named `vjp`.
5169
"""
5270
function pullback end
5371

@@ -56,6 +74,12 @@ function pullback end
5674
pullback!(f!, y, dx, backend, x, dy, [extras]) -> dx
5775
5876
Compute the pullback of the function `f` at point `x` with seed `dy`, overwriting `dx`.
77+
78+
$(document_preparation("pullback"; same_point=true))
79+
80+
!!! tip
81+
Pullbacks are also commonly called vector-Jacobian products or VJPs.
82+
This function could have been named `vjp!`.
5983
"""
6084
function pullback! end
6185

DifferentiationInterface/src/first_order/pushforward.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ function prepare_pushforward_same_point end
3030
3131
Compute the value and the pushforward of the function `f` at point `x` with seed `dx`.
3232
33+
$(document_preparation("pushforward"; same_point=true))
34+
35+
!!! tip
36+
Pushforwards are also commonly called Jacobian-vector products or JVPs.
37+
This function could have been named `value_and_jvp`.
38+
3339
!!! info
3440
Required primitive for forward mode backends.
3541
"""
@@ -40,6 +46,12 @@ function value_and_pushforward end
4046
value_and_pushforward!(f!, y, dy, backend, x, dx, [extras]) -> (y, dy)
4147
4248
Compute the value and the pushforward of the function `f` at point `x` with seed `dx`, overwriting `dy`.
49+
50+
$(document_preparation("pushforward"; same_point=true))
51+
52+
!!! tip
53+
Pushforwards are also commonly called Jacobian-vector products or JVPs.
54+
This function could have been named `value_and_jvp!`.
4355
"""
4456
function value_and_pushforward! end
4557

@@ -48,6 +60,12 @@ function value_and_pushforward! end
4860
pushforward(f!, y, backend, x, dx, [extras]) -> dy
4961
5062
Compute the pushforward of the function `f` at point `x` with seed `dx`.
63+
64+
$(document_preparation("pushforward"; same_point=true))
65+
66+
!!! tip
67+
Pushforwards are also commonly called Jacobian-vector products or JVPs.
68+
This function could have been named `jvp`.
5169
"""
5270
function pushforward end
5371

@@ -56,6 +74,12 @@ function pushforward end
5674
pushforward!(f!, y, dy, backend, x, dx, [extras]) -> dy
5775
5876
Compute the pushforward of the function `f` at point `x` with seed `dx`, overwriting `dy`.
77+
78+
$(document_preparation("pushforward"; same_point=true))
79+
80+
!!! tip
81+
Pushforwards are also commonly called Jacobian-vector products or JVPs.
82+
This function could have been named `jvp!`.
5983
"""
6084
function pushforward! end
6185

DifferentiationInterface/src/second_order/hessian.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,35 @@ function prepare_hessian end
1414
hessian(f, backend, x, [extras]) -> hess
1515
1616
Compute the Hessian matrix of the function `f` at point `x`.
17+
18+
$(document_preparation("hessian"))
1719
"""
1820
function hessian end
1921

2022
"""
2123
hessian!(f, hess, backend, x, [extras]) -> hess
2224
2325
Compute the Hessian matrix of the function `f` at point `x`, overwriting `hess`.
26+
27+
$(document_preparation("hessian"))
2428
"""
2529
function hessian! end
2630

2731
"""
2832
value_gradient_and_hessian(f, backend, x, [extras]) -> (y, grad, hess)
2933
3034
Compute the value, gradient vector and Hessian matrix of the function `f` at point `x`.
35+
36+
$(document_preparation("hessian"))
3137
"""
3238
function value_gradient_and_hessian end
3339

3440
"""
3541
value_gradient_and_hessian!(f, grad, hess, backend, x, [extras]) -> (y, grad, hess)
3642
3743
Compute the value, gradient vector and Hessian matrix of the function `f` at point `x`, overwriting `grad` and `hess`.
44+
45+
$(document_preparation("hessian"))
3846
"""
3947
function value_gradient_and_hessian! end
4048

DifferentiationInterface/src/second_order/hvp.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ function prepare_hvp_same_point end
2424
hvp(f, backend, x, dx, [extras]) -> dg
2525
2626
Compute the Hessian-vector product of `f` at point `x` with seed `dx`.
27+
28+
$(document_preparation("hvp"; same_point=true))
2729
"""
2830
function hvp end
2931

3032
"""
3133
hvp!(f, dg, backend, x, dx, [extras]) -> dg
3234
3335
Compute the Hessian-vector product of `f` at point `x` with seed `dx`, overwriting `dg`.
36+
37+
$(document_preparation("hvp"; same_point=true))
3438
"""
3539
function hvp! end
3640

DifferentiationInterface/src/second_order/second_derivative.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,35 @@ function prepare_second_derivative end
1414
second_derivative(f, backend, x, [extras]) -> der2
1515
1616
Compute the second derivative of the function `f` at point `x`.
17+
18+
$(document_preparation("second_derivative"))
1719
"""
1820
function second_derivative end
1921

2022
"""
2123
second_derivative!(f, der2, backend, x, [extras]) -> der2
2224
2325
Compute the second derivative of the function `f` at point `x`, overwriting `der2`.
26+
27+
$(document_preparation("second_derivative"))
2428
"""
2529
function second_derivative! end
2630

2731
"""
2832
value_derivative_and_second_derivative(f, backend, x, [extras]) -> (y, der, der2)
2933
3034
Compute the value, first derivative and second derivative of the function `f` at point `x`.
35+
36+
$(document_preparation("second_derivative"))
3137
"""
3238
function value_derivative_and_second_derivative end
3339

3440
"""
3541
value_derivative_and_second_derivative!(f, der, der2, backend, x, [extras]) -> (y, der, der2)
3642
3743
Compute the value, first derivative and second derivative of the function `f` at point `x`, overwriting `der` and `der2`.
44+
45+
$(document_preparation("second_derivative"))
3846
"""
3947
function value_derivative_and_second_derivative! end
4048

DifferentiationInterface/src/utils/printing.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ function package_name(b::AbstractADType)
99
end
1010

1111
package_name(b::AutoSparse) = package_name(dense_ad(b))
12+
13+
function document_preparation(operator_name::AbstractString; same_point=false)
14+
if same_point
15+
return "To improve performance via operator preparation, refer to [`prepare_$(operator_name)`](@ref) and [`prepare_$(operator_name)_same_point`](@ref)."
16+
else
17+
return "To improve performance via operator preparation, refer to [`prepare_$(operator_name)`](@ref)."
18+
end
19+
end

0 commit comments

Comments
 (0)