From 730b6a0f4d5318b7bc55e16f6720ca24c5a9aee8 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sat, 10 May 2025 16:00:29 +0200 Subject: [PATCH 1/2] refactor: remove automatic code generation in DI for better coverage stats --- .../src/DifferentiationInterface.jl | 3 - .../src/fallbacks/change_prep.jl | 147 ------------ .../src/fallbacks/no_prep.jl | 218 ------------------ .../src/first_order/derivative.jl | 76 +++++- .../src/first_order/gradient.jl | 33 ++- .../src/first_order/jacobian.jl | 76 +++++- .../src/first_order/pullback.jl | 134 ++++++++++- .../src/first_order/pushforward.jl | 136 ++++++++++- .../src/second_order/hessian.jl | 33 ++- .../src/second_order/hvp.jl | 54 ++++- .../src/second_order/second_derivative.jl | 43 +++- .../test/Core/Internals/_formalities.jl | 5 +- .../test/formalities.jl | 5 +- 13 files changed, 544 insertions(+), 419 deletions(-) delete mode 100644 DifferentiationInterface/src/fallbacks/change_prep.jl delete mode 100644 DifferentiationInterface/src/fallbacks/no_prep.jl diff --git a/DifferentiationInterface/src/DifferentiationInterface.jl b/DifferentiationInterface/src/DifferentiationInterface.jl index ab7ff9aea..32e699572 100644 --- a/DifferentiationInterface/src/DifferentiationInterface.jl +++ b/DifferentiationInterface/src/DifferentiationInterface.jl @@ -61,9 +61,6 @@ include("second_order/second_derivative.jl") include("second_order/hvp.jl") include("second_order/hessian.jl") -include("fallbacks/no_prep.jl") -include("fallbacks/change_prep.jl") - include("misc/differentiate_with.jl") include("misc/from_primitive.jl") include("misc/sparsity_detector.jl") diff --git a/DifferentiationInterface/src/fallbacks/change_prep.jl b/DifferentiationInterface/src/fallbacks/change_prep.jl deleted file mode 100644 index 504d156c5..000000000 --- a/DifferentiationInterface/src/fallbacks/change_prep.jl +++ /dev/null @@ -1,147 +0,0 @@ -for op in [ - :derivative, - :gradient, - :jacobian, - :second_derivative, - :hessian, - :pushforward, - :pullback, - :hvp, -] - op! = Symbol(op, "!") - val_and_op = if op == :second_derivative - :value_derivative_and_second_derivative - elseif op == :hessian - :value_gradient_and_hessian - elseif op == :hvp - nothing - else - Symbol("value_and_", op) - end - val_and_op! = Symbol(val_and_op, "!") - prep_op = Symbol("prepare_", op) - prep_op_nokwarg = Symbol("prepare_", op, "_nokwarg") - prep_op! = Symbol("prepare!_", op) - prep_op_same_point = Symbol("prepare_", op, "_same_point") - prep_op_same_point_nokwarg = Symbol("prepare_", op, "_same_point_nokwarg") - P = if op == :derivative - DerivativePrep - elseif op == :gradient - GradientPrep - elseif op == :jacobian - JacobianPrep - elseif op == :second_derivative - SecondDerivativePrep - elseif op == :hessian - HessianPrep - elseif op == :pushforward - PushforwardPrep - elseif op == :pullback - PullbackPrep - elseif op == :hvp - HVPPrep - end - - if op in (:derivative, :gradient, :jacobian) - # 1-arg - @eval function $prep_op!( - f::F, old_prep::$P, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - check_prep(f, old_prep, backend, x, contexts...) - return $prep_op_nokwarg(is_strict(old_prep), f, backend, x, contexts...) - end - op == :gradient && continue - # 2-arg - @eval function $prep_op!( - f!::F, y, old_prep::$P, backend::AbstractADType, x, contexts::Vararg{Context,C}; - ) where {F,C} - check_prep(f!, y, old_prep, backend, x, contexts...) - return $prep_op_nokwarg(is_strict(old_prep), f!, y, backend, x, contexts...) - end - - elseif op in (:second_derivative, :hessian) - # 1-arg - @eval function $prep_op!( - f::F, old_prep::$P, backend::AbstractADType, x, contexts::Vararg{Context,C}; - ) where {F,C} - check_prep(f, old_prep, backend, x, contexts...) - return $prep_op_nokwarg(is_strict(old_prep), f, backend, x, contexts...) - end - - elseif op in (:pushforward, :pullback, :hvp) - # 1-arg - @eval function $prep_op!( - f::F, - old_prep::$P, - backend::AbstractADType, - x, - seed::NTuple, - contexts::Vararg{Context,C}; - ) where {F,C} - check_prep(f, old_prep, backend, x, seed, contexts...) - return $prep_op_nokwarg(is_strict(old_prep), f, backend, x, seed, contexts...) - end - @eval function $prep_op_same_point( - f::F, - prep::$P, - backend::AbstractADType, - x, - seed::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - check_prep(f, prep, backend, x, seed, contexts...) - return prep - end - @eval function $prep_op_same_point_nokwarg( - strict::Val, - f::F, - backend::AbstractADType, - x, - seed::NTuple, - contexts::Vararg{Context,C}; - ) where {F,C} - prep = $prep_op_nokwarg(strict, f, backend, x, seed, contexts...) - return $prep_op_same_point(f, prep, backend, x, seed, contexts...) - end - op == :hvp && continue - # 2-arg - @eval function $prep_op!( - f!::F, - y, - old_prep::$P, - backend::AbstractADType, - x, - seed::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - check_prep(f!, y, old_prep, backend, x, seed, contexts...) - return $prep_op_nokwarg( - is_strict(old_prep), f!, y, backend, x, seed, contexts... - ) - end - @eval function $prep_op_same_point( - f!::F, - y, - prep::$P, - backend::AbstractADType, - x, - seed::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - check_prep(f!, y, prep, backend, x, seed, contexts...) - return prep - end - @eval function $prep_op_same_point_nokwarg( - strict::Val, - f!::F, - y, - backend::AbstractADType, - x, - seed::NTuple, - contexts::Vararg{Context,C}; - ) where {F,C} - prep = $prep_op_nokwarg(strict, f!, y, backend, x, seed, contexts...) - return $prep_op_same_point(f!, y, prep, backend, x, seed, contexts...) - end - end -end diff --git a/DifferentiationInterface/src/fallbacks/no_prep.jl b/DifferentiationInterface/src/fallbacks/no_prep.jl deleted file mode 100644 index f7dc633d6..000000000 --- a/DifferentiationInterface/src/fallbacks/no_prep.jl +++ /dev/null @@ -1,218 +0,0 @@ -for op in [ - :derivative, - :gradient, - :jacobian, - :second_derivative, - :hessian, - :pushforward, - :pullback, - :hvp, -] - op! = Symbol(op, "!") - val_and_op = if op == :second_derivative - :value_derivative_and_second_derivative - elseif op == :hessian - :value_gradient_and_hessian - elseif op == :hvp - :gradient_and_hvp - else - Symbol("value_and_", op) - end - val_and_op! = Symbol(val_and_op, "!") - prep_op = Symbol("prepare_", op) - prep_op_nokwarg = Symbol("prepare_", op, "_nokwarg") - prep_op! = Symbol("prepare!_", op) - prep_op_same_point = Symbol("prepare_", op, "_same_point") - prep_op_same_point_nokwarg = Symbol("prepare_", op, "_same_point_nokwarg") - P = if op == :derivative - DerivativePrep - elseif op == :gradient - GradientPrep - elseif op == :jacobian - JacobianPrep - elseif op == :second_derivative - SecondDerivativePrep - elseif op == :hessian - HessianPrep - elseif op == :pushforward - PushforwardPrep - elseif op == :pullback - PullbackPrep - elseif op == :hvp - HVPPrep - end - - if op in (:derivative, :jacobian, :gradient) - # 1-arg - @eval function $op( - f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $op(f, prep, backend, x, contexts...) - end - @eval function $op!( - f::F, result, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $op!(f, result, prep, backend, x, contexts...) - end - @eval function $val_and_op( - f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $val_and_op(f, prep, backend, x, contexts...) - end - @eval function $val_and_op!( - f::F, result, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $val_and_op!(f, result, prep, backend, x, contexts...) - end - op == :gradient && continue - # 2-arg - @eval function $op( - f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, contexts...) - return $op(f!, y, prep, backend, x, contexts...) - end - @eval function $op!( - f!::F, y, result, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, contexts...) - return $op!(f!, y, result, prep, backend, x, contexts...) - end - @eval function $val_and_op( - f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, contexts...) - return $val_and_op(f!, y, prep, backend, x, contexts...) - end - @eval function $val_and_op!( - f!::F, y, result, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, contexts...) - return $val_and_op!(f!, y, result, prep, backend, x, contexts...) - end - - elseif op in (:second_derivative, :hessian) - # 1-arg - @eval function $op( - f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $op(f, prep, backend, x, contexts...) - end - @eval function $op!( - f::F, result2, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $op!(f, result2, prep, backend, x, contexts...) - end - @eval function $val_and_op( - f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $val_and_op(f, prep, backend, x, contexts...) - end - @eval function $val_and_op!( - f::F, result1, result2, backend::AbstractADType, x, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, contexts...) - return $val_and_op!(f, result1, result2, prep, backend, x, contexts...) - end - - elseif op in (:pushforward, :pullback, :hvp) - @eval function $op( - f::F, backend::AbstractADType, x, tang::NTuple, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, tang, contexts...) - return $op(f, prep, backend, x, tang, contexts...) - end - @eval function $op!( - f::F, - result::NTuple, - backend::AbstractADType, - x, - tang::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, tang, contexts...) - return $op!(f, result, prep, backend, x, tang, contexts...) - end - @eval function $val_and_op( - f::F, backend::AbstractADType, x, tang::NTuple, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, tang, contexts...) - return $val_and_op(f, prep, backend, x, tang, contexts...) - end - - if op in (:pushforward, :pullback) - @eval function $val_and_op!( - f::F, - result::NTuple, - backend::AbstractADType, - x, - tang::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, tang, contexts...) - return $val_and_op!(f, result, prep, backend, x, tang, contexts...) - end - elseif op == :hvp - @eval function $val_and_op!( - f::F, - result1, - result2::NTuple, - backend::AbstractADType, - x, - tang::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f, backend, x, tang, contexts...) - return $val_and_op!( - f, result1, result2, prep, backend, x, tang, contexts... - ) - end - end - - op == :hvp && continue - - @eval function $op( - f!::F, y, backend::AbstractADType, x, tang::NTuple, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, tang, contexts...) - return $op(f!, y, prep, backend, x, tang, contexts...) - end - @eval function $op!( - f!::F, - y, - result::NTuple, - backend::AbstractADType, - x, - tang::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, tang, contexts...) - return $op!(f!, y, result, prep, backend, x, tang, contexts...) - end - @eval function $val_and_op( - f!::F, y, backend::AbstractADType, x, tang::NTuple, contexts::Vararg{Context,C} - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, tang, contexts...) - return $val_and_op(f!, y, prep, backend, x, tang, contexts...) - end - @eval function $val_and_op!( - f!::F, - y, - result::NTuple, - backend::AbstractADType, - x, - tang::NTuple, - contexts::Vararg{Context,C}, - ) where {F,C} - prep = $prep_op_nokwarg(Val(true), f!, y, backend, x, tang, contexts...) - return $val_and_op!(f!, y, result, prep, backend, x, tang, contexts...) - end - end -end diff --git a/DifferentiationInterface/src/first_order/derivative.jl b/DifferentiationInterface/src/first_order/derivative.jl index 69075fc3a..1784247d8 100644 --- a/DifferentiationInterface/src/first_order/derivative.jl +++ b/DifferentiationInterface/src/first_order/derivative.jl @@ -29,7 +29,25 @@ end $(docstring_prepare!("derivative")) """ -function prepare!_derivative end +function prepare!_derivative( + f::F, old_prep::DerivativePrep, backend::AbstractADType, x, contexts::Vararg{Context,C}; +) where {F,C} + check_prep(f, old_prep, backend, x, contexts...) + return prepare_derivative_nokwarg(is_strict(old_prep), f, backend, x, contexts...) +end + +function prepare!_derivative( + f!::F, + y, + old_prep::DerivativePrep, + backend::AbstractADType, + x, + contexts::Vararg{Context,C}; + strict::Val=Val(false), +) where {F,C} + check_prep(f!, y, old_prep, backend, x, contexts...) + return prepare_derivative_nokwarg(is_strict(old_prep), f!, y, backend, x, contexts...) +end """ value_and_derivative(f, [prep,] backend, x, [contexts...]) -> (y, der) @@ -39,7 +57,19 @@ Compute the value and the derivative of the function `f` at point `x`. $(docstring_preparation_hint("derivative")) """ -function value_and_derivative end +function value_and_derivative( + f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return value_and_derivative(f, prep, backend, x, contexts...) +end + +function value_and_derivative( + f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f!, y, backend, x, contexts...) + return value_and_derivative(f!, y, prep, backend, x, contexts...) +end """ value_and_derivative!(f, der, [prep,] backend, x, [contexts...]) -> (y, der) @@ -49,7 +79,19 @@ Compute the value and the derivative of the function `f` at point `x`, overwriti $(docstring_preparation_hint("derivative")) """ -function value_and_derivative! end +function value_and_derivative!( + f::F, der, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return value_and_derivative!(f, der, prep, backend, x, contexts...) +end + +function value_and_derivative!( + f!::F, y, der, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f!, y, backend, x, contexts...) + return value_and_derivative!(f!, y, der, prep, backend, x, contexts...) +end """ derivative(f, [prep,] backend, x, [contexts...]) -> der @@ -59,7 +101,19 @@ Compute the derivative of the function `f` at point `x`. $(docstring_preparation_hint("derivative")) """ -function derivative end +function derivative( + f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return derivative(f, prep, backend, x, contexts...) +end + +function derivative( + f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f!, y, backend, x, contexts...) + return derivative(f!, y, prep, backend, x, contexts...) +end """ derivative!(f, der, [prep,] backend, x, [contexts...]) -> der @@ -69,7 +123,19 @@ Compute the derivative of the function `f` at point `x`, overwriting `der`. $(docstring_preparation_hint("derivative")) """ -function derivative! end +function derivative!( + f::F, der, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return derivative!(f, der, prep, backend, x, contexts...) +end + +function derivative!( + f!::F, y, der, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_derivative_nokwarg(Val(true), f!, y, backend, x, contexts...) + return derivative!(f!, y, der, prep, backend, x, contexts...) +end ## Preparation diff --git a/DifferentiationInterface/src/first_order/gradient.jl b/DifferentiationInterface/src/first_order/gradient.jl index d15344b0d..426fb205b 100644 --- a/DifferentiationInterface/src/first_order/gradient.jl +++ b/DifferentiationInterface/src/first_order/gradient.jl @@ -16,7 +16,12 @@ end $(docstring_prepare!("gradient")) """ -function prepare!_gradient end +function prepare!_gradient( + f::F, old_prep::GradientPrep, backend::AbstractADType, x, contexts::Vararg{Context,C}; +) where {F,C} + check_prep(f, old_prep, backend, x, contexts...) + return prepare_gradient_nokwarg(is_strict(old_prep), f, backend, x, contexts...) +end """ value_and_gradient(f, [prep,] backend, x, [contexts...]) -> (y, grad) @@ -25,7 +30,12 @@ Compute the value and the gradient of the function `f` at point `x`. $(docstring_preparation_hint("gradient")) """ -function value_and_gradient end +function value_and_gradient( + f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_gradient_nokwarg(Val(true), f, backend, x, contexts...) + return value_and_gradient(f, prep, backend, x, contexts...) +end """ value_and_gradient!(f, grad, [prep,] backend, x, [contexts...]) -> (y, grad) @@ -34,7 +44,12 @@ Compute the value and the gradient of the function `f` at point `x`, overwriting $(docstring_preparation_hint("gradient")) """ -function value_and_gradient! end +function value_and_gradient!( + f::F, grad, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_gradient_nokwarg(Val(true), f, backend, x, contexts...) + return value_and_gradient!(f, grad, prep, backend, x, contexts...) +end """ gradient(f, [prep,] backend, x, [contexts...]) -> grad @@ -43,7 +58,10 @@ Compute the gradient of the function `f` at point `x`. $(docstring_preparation_hint("gradient")) """ -function gradient end +function gradient(f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}) where {F,C} + prep = prepare_gradient_nokwarg(Val(true), f, backend, x, contexts...) + return gradient(f, prep, backend, x, contexts...) +end """ gradient!(f, grad, [prep,] backend, x, [contexts...]) -> grad @@ -52,7 +70,12 @@ Compute the gradient of the function `f` at point `x`, overwriting `grad`. $(docstring_preparation_hint("gradient")) """ -function gradient! end +function gradient!( + f::F, grad, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_gradient_nokwarg(Val(true), f, backend, x, contexts...) + return gradient!(f, grad, prep, backend, x, contexts...) +end ## Preparation diff --git a/DifferentiationInterface/src/first_order/jacobian.jl b/DifferentiationInterface/src/first_order/jacobian.jl index 24932b289..997a4dc75 100644 --- a/DifferentiationInterface/src/first_order/jacobian.jl +++ b/DifferentiationInterface/src/first_order/jacobian.jl @@ -29,7 +29,25 @@ end $(docstring_prepare!("jacobian")) """ -function prepare!_jacobian end +function prepare!_jacobian( + f::F, old_prep::JacobianPrep, backend::AbstractADType, x, contexts::Vararg{Context,C}; +) where {F,C} + check_prep(f, old_prep, backend, x, contexts...) + return prepare_jacobian_nokwarg(is_strict(old_prep), f, backend, x, contexts...) +end + +function prepare!_jacobian( + f!::F, + y, + old_prep::JacobianPrep, + backend::AbstractADType, + x, + contexts::Vararg{Context,C}; + strict::Val=Val(false), +) where {F,C} + check_prep(f!, y, old_prep, backend, x, contexts...) + return prepare_jacobian_nokwarg(is_strict(old_prep), f!, y, backend, x, contexts...) +end """ value_and_jacobian(f, [prep,] backend, x, [contexts...]) -> (y, jac) @@ -39,17 +57,41 @@ Compute the value and the Jacobian matrix of the function `f` at point `x`. $(docstring_preparation_hint("jacobian")) """ -function value_and_jacobian end +function value_and_jacobian( + f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f, backend, x, contexts...) + return value_and_jacobian(f, prep, backend, x, contexts...) +end + +function value_and_jacobian( + f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f!, y, backend, x, contexts...) + return value_and_jacobian(f!, y, prep, backend, x, contexts...) +end """ value_and_jacobian!(f, jac, [prep,] backend, x, [contexts...]) -> (y, jac) value_and_jacobian!(f!, y, jac, [prep,] backend, x, [contexts...]) -> (y, jac) Compute the value and the Jacobian matrix of the function `f` at point `x`, overwriting `jac`. - + $(docstring_preparation_hint("jacobian")) """ -function value_and_jacobian! end +function value_and_jacobian!( + f::F, jac, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f, backend, x, contexts...) + return value_and_jacobian!(f, jac, prep, backend, x, contexts...) +end + +function value_and_jacobian!( + f!::F, y, jac, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f!, y, backend, x, contexts...) + return value_and_jacobian!(f!, y, jac, prep, backend, x, contexts...) +end """ jacobian(f, [prep,] backend, x, [contexts...]) -> jac @@ -59,7 +101,17 @@ Compute the Jacobian matrix of the function `f` at point `x`. $(docstring_preparation_hint("jacobian")) """ -function jacobian end +function jacobian(f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f, backend, x, contexts...) + return jacobian(f, prep, backend, x, contexts...) +end + +function jacobian( + f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f!, y, backend, x, contexts...) + return jacobian(f!, y, prep, backend, x, contexts...) +end """ jacobian!(f, jac, [prep,] backend, x, [contexts...]) -> jac @@ -69,7 +121,19 @@ Compute the Jacobian matrix of the function `f` at point `x`, overwriting `jac`. $(docstring_preparation_hint("jacobian")) """ -function jacobian! end +function jacobian!( + f::F, jac, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f, backend, x, contexts...) + return jacobian!(f, jac, prep, backend, x, contexts...) +end + +function jacobian!( + f!::F, y, jac, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_jacobian_nokwarg(Val(true), f!, y, backend, x, contexts...) + return jacobian!(f!, y, jac, prep, backend, x, contexts...) +end ## Preparation diff --git a/DifferentiationInterface/src/first_order/pullback.jl b/DifferentiationInterface/src/first_order/pullback.jl index a22277a6f..2ac1265c0 100644 --- a/DifferentiationInterface/src/first_order/pullback.jl +++ b/DifferentiationInterface/src/first_order/pullback.jl @@ -35,7 +35,30 @@ end $(docstring_prepare!("pullback")) """ -function prepare!_pullback end +function prepare!_pullback( + f::F, + old_prep::PullbackPrep, + backend::AbstractADType, + x, + ty::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f, old_prep, backend, x, ty, contexts...) + return prepare_pullback_nokwarg(is_strict(old_prep), f, backend, x, ty, contexts...) +end + +function prepare!_pullback( + f!::F, + y, + old_prep::PullbackPrep, + backend::AbstractADType, + x, + ty::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f!, y, old_prep, backend, x, ty, contexts...) + return prepare_pullback_nokwarg(is_strict(old_prep), f!, y, backend, x, ty, contexts...) +end """ prepare_pullback_same_point(f, backend, x, ty, [contexts...]; strict=Val(false)) -> prep_same @@ -66,6 +89,51 @@ function prepare_pullback_same_point( return prepare_pullback_same_point_nokwarg(strict, f!, y, backend, x, ty, contexts...) end +function prepare_pullback_same_point_nokwarg( + strict::Val, f::F, backend::AbstractADType, x, ty::NTuple, contexts::Vararg{Context,C}; +) where {F,C} + prep = prepare_pullback_nokwarg(strict, f, backend, x, ty, contexts...) + return prepare_pullback_same_point(f, prep, backend, x, ty, contexts...) +end + +function prepare_pullback_same_point_nokwarg( + strict::Val, + f!::F, + y, + backend::AbstractADType, + x, + ty::NTuple, + contexts::Vararg{Context,C}; +) where {F,C} + prep = prepare_pullback_nokwarg(strict, f!, y, backend, x, ty, contexts...) + return prepare_pullback_same_point(f!, y, prep, backend, x, ty, contexts...) +end + +function prepare_pullback_same_point( + f::F, + prep::PullbackPrep, + backend::AbstractADType, + x, + ty::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f, prep, backend, x, ty, contexts...) + return prep +end + +function prepare_pullback_same_point( + f!::F, + y, + prep::PullbackPrep, + backend::AbstractADType, + x, + ty::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f!, y, prep, backend, x, ty, contexts...) + return prep +end + """ value_and_pullback(f, [prep,] backend, x, ty, [contexts...]) -> (y, tx) value_and_pullback(f!, y, [prep,] backend, x, ty, [contexts...]) -> (y, tx) @@ -74,14 +142,26 @@ Compute the value and the pullback of the function `f` at point `x` with a tuple $(docstring_preparation_hint("pullback"; same_point=true)) -!!! tip +!!! tip Pullbacks are also commonly called vector-Jacobian products or VJPs. This function could have been named `value_and_vjp`. !!! info Required primitive for reverse mode backends. """ -function value_and_pullback end +function value_and_pullback( + f::F, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f, backend, x, ty, contexts...) + return value_and_pullback(f, prep, backend, x, ty, contexts...) +end + +function value_and_pullback( + f!::F, y, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f!, y, backend, x, ty, contexts...) + return value_and_pullback(f!, y, prep, backend, x, ty, contexts...) +end """ value_and_pullback!(f, dx, [prep,] backend, x, ty, [contexts...]) -> (y, tx) @@ -91,11 +171,23 @@ Compute the value and the pullback of the function `f` at point `x` with a tuple $(docstring_preparation_hint("pullback"; same_point=true)) -!!! tip +!!! tip Pullbacks are also commonly called vector-Jacobian products or VJPs. This function could have been named `value_and_vjp!`. """ -function value_and_pullback! end +function value_and_pullback!( + f::F, tx, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f, backend, x, ty, contexts...) + return value_and_pullback!(f, tx, prep, backend, x, ty, contexts...) +end + +function value_and_pullback!( + f!::F, y, tx, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f!, y, backend, x, ty, contexts...) + return value_and_pullback!(f!, y, tx, prep, backend, x, ty, contexts...) +end """ pullback(f, [prep,] backend, x, ty, [contexts...]) -> tx @@ -105,11 +197,23 @@ Compute the pullback of the function `f` at point `x` with a tuple of tangents ` $(docstring_preparation_hint("pullback"; same_point=true)) -!!! tip +!!! tip Pullbacks are also commonly called vector-Jacobian products or VJPs. This function could have been named `vjp`. """ -function pullback end +function pullback( + f::F, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f, backend, x, ty, contexts...) + return pullback(f, prep, backend, x, ty, contexts...) +end + +function pullback( + f!::F, y, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f!, y, backend, x, ty, contexts...) + return pullback(f!, y, prep, backend, x, ty, contexts...) +end """ pullback!(f, dx, [prep,] backend, x, ty, [contexts...]) -> tx @@ -119,11 +223,23 @@ Compute the pullback of the function `f` at point `x` with a tuple of tangents ` $(docstring_preparation_hint("pullback"; same_point=true)) -!!! tip +!!! tip Pullbacks are also commonly called vector-Jacobian products or VJPs. This function could have been named `vjp!`. """ -function pullback! end +function pullback!( + f::F, tx, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f, backend, x, ty, contexts...) + return pullback!(f, tx, prep, backend, x, ty, contexts...) +end + +function pullback!( + f!::F, y, tx, backend::AbstractADType, x, ty, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pullback_nokwarg(Val(true), f!, y, backend, x, ty, contexts...) + return pullback!(f!, y, tx, prep, backend, x, ty, contexts...) +end ## Preparation diff --git a/DifferentiationInterface/src/first_order/pushforward.jl b/DifferentiationInterface/src/first_order/pushforward.jl index 601461d6a..edba42655 100644 --- a/DifferentiationInterface/src/first_order/pushforward.jl +++ b/DifferentiationInterface/src/first_order/pushforward.jl @@ -35,7 +35,32 @@ end $(docstring_prepare!("pushforward")) """ -function prepare!_pushforward end +function prepare!_pushforward( + f::F, + old_prep::PushforwardPrep, + backend::AbstractADType, + x, + tx::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f, old_prep, backend, x, tx, contexts...) + return prepare_pushforward_nokwarg(is_strict(old_prep), f, backend, x, tx, contexts...) +end + +function prepare!_pushforward( + f!::F, + y, + old_prep::PushforwardPrep, + backend::AbstractADType, + x, + tx::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f!, y, old_prep, backend, x, tx, contexts...) + return prepare_pushforward_nokwarg( + is_strict(old_prep), f!, y, backend, x, tx, contexts... + ) +end """ prepare_pushforward_same_point(f, backend, x, tx, [contexts...]; strict=Val(false)) -> prep_same @@ -68,6 +93,51 @@ function prepare_pushforward_same_point( ) end +function prepare_pushforward_same_point_nokwarg( + strict::Val, f::F, backend::AbstractADType, x, tx::NTuple, contexts::Vararg{Context,C}; +) where {F,C} + prep = prepare_pushforward_nokwarg(strict, f, backend, x, tx, contexts...) + return prepare_pushforward_same_point(f, prep, backend, x, tx, contexts...) +end + +function prepare_pushforward_same_point_nokwarg( + strict::Val, + f!::F, + y, + backend::AbstractADType, + x, + tx::NTuple, + contexts::Vararg{Context,C}; +) where {F,C} + prep = prepare_pushforward_nokwarg(strict, f!, y, backend, x, tx, contexts...) + return prepare_pushforward_same_point(f!, y, prep, backend, x, tx, contexts...) +end + +function prepare_pushforward_same_point( + f::F, + prep::PushforwardPrep, + backend::AbstractADType, + x, + tx::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f, prep, backend, x, tx, contexts...) + return prep +end + +function prepare_pushforward_same_point( + f!::F, + y, + prep::PushforwardPrep, + backend::AbstractADType, + x, + tx::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f!, y, prep, backend, x, tx, contexts...) + return prep +end + """ value_and_pushforward(f, [prep,] backend, x, tx, [contexts...]) -> (y, ty) value_and_pushforward(f!, y, [prep,] backend, x, tx, [contexts...]) -> (y, ty) @@ -76,14 +146,26 @@ Compute the value and the pushforward of the function `f` at point `x` with a tu $(docstring_preparation_hint("pushforward"; same_point=true)) -!!! tip +!!! tip Pushforwards are also commonly called Jacobian-vector products or JVPs. This function could have been named `value_and_jvp`. !!! info Required primitive for forward mode backends. """ -function value_and_pushforward end +function value_and_pushforward( + f::F, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f, backend, x, tx, contexts...) + return value_and_pushforward(f, prep, backend, x, tx, contexts...) +end + +function value_and_pushforward( + f!::F, y, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f!, y, backend, x, tx, contexts...) + return value_and_pushforward(f!, y, prep, backend, x, tx, contexts...) +end """ value_and_pushforward!(f, dy, [prep,] backend, x, tx, [contexts...]) -> (y, ty) @@ -93,11 +175,23 @@ Compute the value and the pushforward of the function `f` at point `x` with a tu $(docstring_preparation_hint("pushforward"; same_point=true)) -!!! tip +!!! tip Pushforwards are also commonly called Jacobian-vector products or JVPs. This function could have been named `value_and_jvp!`. """ -function value_and_pushforward! end +function value_and_pushforward!( + f::F, ty, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f, backend, x, tx, contexts...) + return value_and_pushforward!(f, ty, prep, backend, x, tx, contexts...) +end + +function value_and_pushforward!( + f!::F, y, ty, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f!, y, backend, x, tx, contexts...) + return value_and_pushforward!(f!, y, ty, prep, backend, x, tx, contexts...) +end """ pushforward(f, [prep,] backend, x, tx, [contexts...]) -> ty @@ -107,11 +201,23 @@ Compute the pushforward of the function `f` at point `x` with a tuple of tangent $(docstring_preparation_hint("pushforward"; same_point=true)) -!!! tip +!!! tip Pushforwards are also commonly called Jacobian-vector products or JVPs. This function could have been named `jvp`. """ -function pushforward end +function pushforward( + f::F, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f, backend, x, tx, contexts...) + return pushforward(f, prep, backend, x, tx, contexts...) +end + +function pushforward( + f!::F, y, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f!, y, backend, x, tx, contexts...) + return pushforward(f!, y, prep, backend, x, tx, contexts...) +end """ pushforward!(f, dy, [prep,] backend, x, tx, [contexts...]) -> ty @@ -121,11 +227,23 @@ Compute the pushforward of the function `f` at point `x` with a tuple of tangent $(docstring_preparation_hint("pushforward"; same_point=true)) -!!! tip +!!! tip Pushforwards are also commonly called Jacobian-vector products or JVPs. This function could have been named `jvp!`. """ -function pushforward! end +function pushforward!( + f::F, ty, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f, backend, x, tx, contexts...) + return pushforward!(f, ty, prep, backend, x, tx, contexts...) +end + +function pushforward!( + f!::F, y, ty, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_pushforward_nokwarg(Val(true), f!, y, backend, x, tx, contexts...) + return pushforward!(f!, y, ty, prep, backend, x, tx, contexts...) +end ## Preparation diff --git a/DifferentiationInterface/src/second_order/hessian.jl b/DifferentiationInterface/src/second_order/hessian.jl index dbdd16da6..8e17cff99 100644 --- a/DifferentiationInterface/src/second_order/hessian.jl +++ b/DifferentiationInterface/src/second_order/hessian.jl @@ -16,7 +16,12 @@ end $(docstring_prepare!("hessian")) """ -function prepare!_hessian end +function prepare!_hessian( + f::F, old_prep::HessianPrep, backend::AbstractADType, x, contexts::Vararg{Context,C}; +) where {F,C} + check_prep(f, old_prep, backend, x, contexts...) + return prepare_hessian_nokwarg(is_strict(old_prep), f, backend, x, contexts...) +end """ hessian(f, [prep,] backend, x, [contexts...]) -> hess @@ -25,7 +30,10 @@ Compute the Hessian matrix of the function `f` at point `x`. $(docstring_preparation_hint("hessian")) """ -function hessian end +function hessian(f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}) where {F,C} + prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...) + return hessian(f, prep, backend, x, contexts...) +end """ hessian!(f, hess, [prep,] backend, x, [contexts...]) -> hess @@ -34,7 +42,12 @@ Compute the Hessian matrix of the function `f` at point `x`, overwriting `hess`. $(docstring_preparation_hint("hessian")) """ -function hessian! end +function hessian!( + f::F, hess, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...) + return hessian!(f, hess, prep, backend, x, contexts...) +end """ value_gradient_and_hessian(f, [prep,] backend, x, [contexts...]) -> (y, grad, hess) @@ -43,7 +56,12 @@ Compute the value, gradient vector and Hessian matrix of the function `f` at poi $(docstring_preparation_hint("hessian")) """ -function value_gradient_and_hessian end +function value_gradient_and_hessian( + f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...) + return value_gradient_and_hessian(f, prep, backend, x, contexts...) +end """ value_gradient_and_hessian!(f, grad, hess, [prep,] backend, x, [contexts...]) -> (y, grad, hess) @@ -52,7 +70,12 @@ Compute the value, gradient vector and Hessian matrix of the function `f` at poi $(docstring_preparation_hint("hessian")) """ -function value_gradient_and_hessian! end +function value_gradient_and_hessian!( + f::F, grad, hess, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...) + return value_gradient_and_hessian!(f, grad, hess, prep, backend, x, contexts...) +end ## Preparation diff --git a/DifferentiationInterface/src/second_order/hvp.jl b/DifferentiationInterface/src/second_order/hvp.jl index a8d31f8dd..1c4f3292a 100644 --- a/DifferentiationInterface/src/second_order/hvp.jl +++ b/DifferentiationInterface/src/second_order/hvp.jl @@ -21,7 +21,17 @@ end $(docstring_prepare("hvp")) """ -function prepare!_hvp end +function prepare!_hvp( + f::F, + old_prep::HVPPrep, + backend::AbstractADType, + x, + tx::NTuple, + contexts::Vararg{Context,C}, +) where {F,C} + check_prep(f, old_prep, backend, x, tx, contexts...) + return prepare_hvp_nokwarg(is_strict(old_prep), f, backend, x, tx, contexts...) +end """ prepare_hvp_same_point(f, backend, x, tx, [contexts...]; strict=Val(false)) -> prep_same @@ -39,6 +49,20 @@ function prepare_hvp_same_point( return prepare_hvp_same_point_nokwarg(strict, f, backend, x, tx, contexts...) end +function prepare_hvp_same_point_nokwarg( + strict::Val, f::F, backend::AbstractADType, x, tx::NTuple, contexts::Vararg{Context,C}; +) where {F,C} + prep = prepare_hvp_nokwarg(strict, f, backend, x, tx, contexts...) + return prepare_hvp_same_point(f, prep, backend, x, tx, contexts...) +end + +function prepare_hvp_same_point( + f::F, prep::HVPPrep, backend::AbstractADType, x, tx::NTuple, contexts::Vararg{Context,C} +) where {F,C} + check_prep(f, prep, backend, x, tx, contexts...) + return prep +end + """ hvp(f, [prep,] backend, x, tx, [contexts...]) -> tg @@ -46,7 +70,10 @@ Compute the Hessian-vector product of `f` at point `x` with a tuple of tangents $(docstring_preparation_hint("hvp"; same_point=true)) """ -function hvp end +function hvp(f::F, backend::AbstractADType, x, tx, contexts::Vararg{Context,C}) where {F,C} + prep = prepare_hvp_nokwarg(Val(true), f, backend, x, tx, contexts...) + return hvp(f, prep, backend, x, tx, contexts...) +end """ hvp!(f, tg, [prep,] backend, x, tx, [contexts...]) -> tg @@ -55,7 +82,12 @@ Compute the Hessian-vector product of `f` at point `x` with a tuple of tangents $(docstring_preparation_hint("hvp"; same_point=true)) """ -function hvp! end +function hvp!( + f::F, tg, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_hvp_nokwarg(Val(true), f, backend, x, tx, contexts...) + return hvp!(f, tg, prep, backend, x, tx, contexts...) +end """ gradient_and_hvp(f, [prep,] backend, x, tx, [contexts...]) -> (grad, tg) @@ -64,7 +96,12 @@ Compute the gradient and the Hessian-vector product of `f` at point `x` with a t $(docstring_preparation_hint("hvp"; same_point=true)) """ -function gradient_and_hvp end +function gradient_and_hvp( + f::F, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_hvp_nokwarg(Val(true), f, backend, x, tx, contexts...) + return gradient_and_hvp(f, prep, backend, x, tx, contexts...) +end """ gradient_and_hvp!(f, grad, tg, [prep,] backend, x, tx, [contexts...]) -> (grad, tg) @@ -73,7 +110,14 @@ Compute the gradient and the Hessian-vector product of `f` at point `x` with a t $(docstring_preparation_hint("hvp"; same_point=true)) """ -function gradient_and_hvp! end +function gradient_and_hvp!( + f::F, grad, tg, backend::AbstractADType, x, tx, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_hvp_nokwarg(Val(true), f, backend, x, tx, contexts...) + return gradient_and_hvp!(f, grad, tg, prep, backend, x, tx, contexts...) +end + +## Preparation function prepare_hvp_nokwarg( strict::Val, f::F, backend::AbstractADType, x, tx::NTuple, contexts::Vararg{Context,C}; diff --git a/DifferentiationInterface/src/second_order/second_derivative.jl b/DifferentiationInterface/src/second_order/second_derivative.jl index 6a9814b40..5c0903153 100644 --- a/DifferentiationInterface/src/second_order/second_derivative.jl +++ b/DifferentiationInterface/src/second_order/second_derivative.jl @@ -16,7 +16,18 @@ end $(docstring_prepare!("second_derivative")) """ -function prepare!_second_derivative end +function prepare!_second_derivative( + f::F, + old_prep::SecondDerivativePrep, + backend::AbstractADType, + x, + contexts::Vararg{Context,C}; +) where {F,C} + check_prep(f, old_prep, backend, x, contexts...) + return prepare_second_derivative_nokwarg( + is_strict(old_prep), f, backend, x, contexts... + ) +end """ second_derivative(f, [prep,] backend, x, [contexts...]) -> der2 @@ -25,7 +36,12 @@ Compute the second derivative of the function `f` at point `x`. $(docstring_preparation_hint("second_derivative")) """ -function second_derivative end +function second_derivative( + f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_second_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return second_derivative(f, prep, backend, x, contexts...) +end """ second_derivative!(f, der2, [prep,] backend, x, [contexts...]) -> der2 @@ -34,7 +50,12 @@ Compute the second derivative of the function `f` at point `x`, overwriting `der $(docstring_preparation_hint("second_derivative")) """ -function second_derivative! end +function second_derivative!( + f::F, der2, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_second_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return second_derivative!(f, der2, prep, backend, x, contexts...) +end """ value_derivative_and_second_derivative(f, [prep,] backend, x, [contexts...]) -> (y, der, der2) @@ -43,7 +64,12 @@ Compute the value, first derivative and second derivative of the function `f` at $(docstring_preparation_hint("second_derivative")) """ -function value_derivative_and_second_derivative end +function value_derivative_and_second_derivative( + f::F, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_second_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return value_derivative_and_second_derivative(f, prep, backend, x, contexts...) +end """ value_derivative_and_second_derivative!(f, der, der2, [prep,] backend, x, [contexts...]) -> (y, der, der2) @@ -52,7 +78,14 @@ Compute the value, first derivative and second derivative of the function `f` at $(docstring_preparation_hint("second_derivative")) """ -function value_derivative_and_second_derivative! end +function value_derivative_and_second_derivative!( + f::F, der, der2, backend::AbstractADType, x, contexts::Vararg{Context,C} +) where {F,C} + prep = prepare_second_derivative_nokwarg(Val(true), f, backend, x, contexts...) + return value_derivative_and_second_derivative!( + f, der, der2, prep, backend, x, contexts... + ) +end ## Preparation diff --git a/DifferentiationInterface/test/Core/Internals/_formalities.jl b/DifferentiationInterface/test/Core/Internals/_formalities.jl index 02f567aac..3e0cd4b81 100644 --- a/DifferentiationInterface/test/Core/Internals/_formalities.jl +++ b/DifferentiationInterface/test/Core/Internals/_formalities.jl @@ -11,7 +11,10 @@ using SparseArrays @testset "Aqua" begin Aqua.test_all( - DifferentiationInterface; ambiguities=false, deps_compat=(; check_extras=false) + DifferentiationInterface; + ambiguities=false, + deps_compat=(; check_extras=false), + undocumented_names=true, ) end diff --git a/DifferentiationInterfaceTest/test/formalities.jl b/DifferentiationInterfaceTest/test/formalities.jl index 4bfec0101..7b93958c4 100644 --- a/DifferentiationInterfaceTest/test/formalities.jl +++ b/DifferentiationInterfaceTest/test/formalities.jl @@ -9,7 +9,10 @@ using Test @testset "Aqua" begin Aqua.test_all( - DifferentiationInterfaceTest; ambiguities=false, deps_compat=(; check_extras=false) + DifferentiationInterfaceTest; + ambiguities=false, + deps_compat=(; check_extras=false), + undocumented_names=true, ) end @testset "JuliaFormatter" begin From 9a66695db4050b7e77843ee22052dcd6f095ca48 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sat, 10 May 2025 17:02:25 +0200 Subject: [PATCH 2/2] Remove Aqua check --- DifferentiationInterface/test/Core/Internals/_formalities.jl | 5 +---- DifferentiationInterfaceTest/test/formalities.jl | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/DifferentiationInterface/test/Core/Internals/_formalities.jl b/DifferentiationInterface/test/Core/Internals/_formalities.jl index 3e0cd4b81..02f567aac 100644 --- a/DifferentiationInterface/test/Core/Internals/_formalities.jl +++ b/DifferentiationInterface/test/Core/Internals/_formalities.jl @@ -11,10 +11,7 @@ using SparseArrays @testset "Aqua" begin Aqua.test_all( - DifferentiationInterface; - ambiguities=false, - deps_compat=(; check_extras=false), - undocumented_names=true, + DifferentiationInterface; ambiguities=false, deps_compat=(; check_extras=false) ) end diff --git a/DifferentiationInterfaceTest/test/formalities.jl b/DifferentiationInterfaceTest/test/formalities.jl index 7b93958c4..4bfec0101 100644 --- a/DifferentiationInterfaceTest/test/formalities.jl +++ b/DifferentiationInterfaceTest/test/formalities.jl @@ -9,10 +9,7 @@ using Test @testset "Aqua" begin Aqua.test_all( - DifferentiationInterfaceTest; - ambiguities=false, - deps_compat=(; check_extras=false), - undocumented_names=true, + DifferentiationInterfaceTest; ambiguities=false, deps_compat=(; check_extras=false) ) end @testset "JuliaFormatter" begin