Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DifferentiationInterface/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/JuliaDiff/DifferentiationInterface.jl/compare/DifferentiationInterface-v0.7.21...main)

### Added

- New operators `value_gradient_and_hvp` and `value_gradient_and_hvp!`, which also return the primal value ([#1086](https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/1086))

### Deprecated

- `gradient_and_hvp` and `gradient_and_hvp!` are deprecated in favor of `value_gradient_and_hvp` and `value_gradient_and_hvp!` ([#1086](https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/1086))

### Fixed

- Speed up `pick_batchsize` when the batch size is only known at run time ([#1076](https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/1076))
Expand Down
2 changes: 1 addition & 1 deletion DifferentiationInterface/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DifferentiationInterface"
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
version = "0.7.21"
version = "0.7.22"
authors = ["Guillaume Dalle", "Adrian Hill"]

[deps]
Expand Down
2 changes: 2 additions & 0 deletions DifferentiationInterface/docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ prepare_hvp
prepare_hvp_same_point
hvp
hvp!
value_gradient_and_hvp
value_gradient_and_hvp!
gradient_and_hvp
gradient_and_hvp!
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Several variants of each operator are defined:
| [`jacobian`](@ref) | [`jacobian!`](@ref) | [`value_and_jacobian`](@ref) | [`value_and_jacobian!`](@ref) |
| [`pushforward`](@ref) | [`pushforward!`](@ref) | [`value_and_pushforward`](@ref) | [`value_and_pushforward!`](@ref) |
| [`pullback`](@ref) | [`pullback!`](@ref) | [`value_and_pullback`](@ref) | [`value_and_pullback!`](@ref) |
| [`hvp`](@ref) | [`hvp!`](@ref) | [`gradient_and_hvp`](@ref) | [`gradient_and_hvp!`](@ref) |
| [`hvp`](@ref) | [`hvp!`](@ref) | [`value_gradient_and_hvp`](@ref) | [`value_gradient_and_hvp!`](@ref) |

## Mutation and signatures

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ function DI.hvp!(
return tg
end

function DI.gradient_and_hvp(
function DI.value_gradient_and_hvp(
f,
prep::FastDifferentiationHVPPrep,
backend::AutoFastDifferentiation,
Expand All @@ -609,11 +609,11 @@ function DI.gradient_and_hvp(
) where {C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
tg = DI.hvp(f, prep, backend, x, tx, contexts...)
grad = DI.gradient(f, prep.gradient_prep, backend, x, contexts...)
return grad, tg
y, grad = DI.value_and_gradient(f, prep.gradient_prep, backend, x, contexts...)
return y, grad, tg
end

function DI.gradient_and_hvp!(
function DI.value_gradient_and_hvp!(
f,
grad,
tg::NTuple,
Expand All @@ -625,8 +625,8 @@ function DI.gradient_and_hvp!(
) where {C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
DI.hvp!(f, tg, prep, backend, x, tx, contexts...)
DI.gradient!(f, grad, prep.gradient_prep, backend, x, contexts...)
return grad, tg
y, _ = DI.value_and_gradient!(f, grad, prep.gradient_prep, backend, x, contexts...)
return y, grad, tg
end

## Hessian
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ function DI.hvp!(
return tg
end

function DI.gradient_and_hvp(
function DI.value_gradient_and_hvp(
f,
prep::GTPSAOneArgHVPPrep,
backend::AutoGTPSA{D},
Expand All @@ -619,7 +619,7 @@ function DI.gradient_and_hvp(
) where {D, C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
grad = similar(x, eltype(prep.hess))
DI.value_gradient_and_hessian!(
y, _, _ = DI.value_gradient_and_hessian!(
f, grad, prep.hess, prep.hessprep, backend, x, contexts...
)
tg = map(tx) do dx
Expand All @@ -634,10 +634,10 @@ function DI.gradient_and_hvp(
end
return dg
end
return grad, tg
return y, grad, tg
end

function DI.gradient_and_hvp!(
function DI.value_gradient_and_hvp!(
f,
grad,
tg,
Expand All @@ -648,7 +648,7 @@ function DI.gradient_and_hvp!(
contexts::Vararg{DI.Constant, C},
) where {D, C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
DI.value_gradient_and_hessian!(
y, _, _ = DI.value_gradient_and_hessian!(
f, grad, prep.hess, prep.hessprep, backend, x, contexts...
)
for b in eachindex(tg, tx)
Expand All @@ -662,5 +662,5 @@ function DI.gradient_and_hvp!(
j += 1
end
end
return grad, tg
return y, grad, tg
end
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function DI.hvp!(
return hvp!(tg, fc, x, tx, prep.cfg)
end

function DI.gradient_and_hvp(
function DI.value_gradient_and_hvp(
f,
prep::HyperHessiansHVPPrep,
backend::DI.AutoHyperHessians,
Expand All @@ -295,10 +295,10 @@ function DI.gradient_and_hvp(
contexts_prepared = translate_prepared(contexts, prep.contexts_prepared)
fc = DI.fix_tail(f, contexts_prepared...)
res = hvp_gradient_value(fc, x, tx, prep.cfg)
return res.gradient, res.hvp
return res.value, res.gradient, res.hvp
end

function DI.gradient_and_hvp!(
function DI.value_gradient_and_hvp!(
f,
grad,
tg::NTuple,
Expand All @@ -311,8 +311,8 @@ function DI.gradient_and_hvp!(
DI.check_prep(f, prep, backend, x, tx, contexts...)
contexts_prepared = translate_prepared(contexts, prep.contexts_prepared)
fc = DI.fix_tail(f, contexts_prepared...)
hvp_gradient_value!(tg, grad, fc, x, tx, prep.cfg)
return grad, tg
y = hvp_gradient_value!(tg, grad, fc, x, tx, prep.cfg)
return y, grad, tg
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function DI.prepare_hvp_nokwarg(
return prep
end

function DI.gradient_and_hvp(
function DI.value_gradient_and_hvp(
f::F,
prep::MooncakeHVPPrep{Y},
backend::AutoMooncakeForwardOverReverse,
Expand All @@ -24,19 +24,19 @@ function DI.gradient_and_hvp(
DI.check_prep(f, prep, backend, x, tx, contexts...)
dx = only(tx)
if isempty(contexts)
_, new_g, new_dg = value_and_hvp!!(
y, new_g, new_dg = value_and_hvp!!(
prep.cache, f, (dx,), x
)
else
dall = (dx, map(zero_tangent_unwrap, contexts)...)
_, (new_g,), (new_dg,) = value_and_hvp!!(
y, (new_g,), (new_dg,) = value_and_hvp!!(
prep.cache, f, dall, x, map(DI.unwrap, contexts)...
)
end
return _copy_output(new_g), (_copy_output(new_dg),)
return y, _copy_output(new_g), (_copy_output(new_dg),)
end

function DI.gradient_and_hvp(
function DI.value_gradient_and_hvp(
f::F,
prep::MooncakeHVPPrep{Y},
backend::AutoMooncakeForwardOverReverse,
Expand All @@ -45,25 +45,25 @@ function DI.gradient_and_hvp(
contexts::Vararg{DI.Context, C},
) where {F, Y, C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
gs_and_tg = map(tx) do dx
ys_gs_and_tg = map(tx) do dx
if isempty(contexts)
_, new_g, new_dg = value_and_hvp!!(
y, new_g, new_dg = value_and_hvp!!(
prep.cache, f, dx, x
)
else
dall = (dx, map(zero_tangent_unwrap, contexts)...)
_, (new_g,), (new_dg,) = value_and_hvp!!(
y, (new_g,), (new_dg,) = value_and_hvp!!(
prep.cache, f, dall, x, map(DI.unwrap, contexts)...
)
end
_copy_output(new_g), _copy_output(new_dg)
y, _copy_output(new_g), _copy_output(new_dg)
end
g = first(gs_and_tg[1])
tg = map(last, gs_and_tg)
return g, tg
y, g, _ = ys_gs_and_tg[1]
tg = map(last, ys_gs_and_tg)
return y, g, tg
end

function DI.gradient_and_hvp!(
function DI.value_gradient_and_hvp!(
f::F,
g,
tg::NTuple,
Expand All @@ -74,10 +74,10 @@ function DI.gradient_and_hvp!(
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
new_g, new_tg = DI.gradient_and_hvp(f, prep, backend, x, tx, contexts...)
y, new_g, new_tg = DI.value_gradient_and_hvp(f, prep, backend, x, tx, contexts...)
copyto!(g, new_g)
foreach(copyto!, tg, new_tg)
return g, tg
return y, g, tg
end

function DI.hvp(
Expand All @@ -89,7 +89,7 @@ function DI.hvp(
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
return DI.gradient_and_hvp(f, prep, backend, x, tx, contexts...)[2]
return DI.value_gradient_and_hvp(f, prep, backend, x, tx, contexts...)[3]
end

function DI.hvp!(
Expand All @@ -102,7 +102,7 @@ function DI.hvp!(
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
_, new_tg = DI.gradient_and_hvp(f, prep, backend, x, tx, contexts...)
_, _, new_tg = DI.value_gradient_and_hvp(f, prep, backend, x, tx, contexts...)
foreach(copyto!, tg, new_tg)
return tg
end
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ function DI.hvp!(
return tg
end

function DI.gradient_and_hvp(
function DI.value_gradient_and_hvp(
f,
prep::SymbolicsOneArgHVPPrep,
backend::AutoSymbolics,
Expand All @@ -493,11 +493,11 @@ function DI.gradient_and_hvp(
) where {C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
tg = DI.hvp(f, prep, backend, x, tx, contexts...)
grad = DI.gradient(f, prep.gradient_prep, backend, x, contexts...)
return grad, tg
y, grad = DI.value_and_gradient(f, prep.gradient_prep, backend, x, contexts...)
return y, grad, tg
end

function DI.gradient_and_hvp!(
function DI.value_gradient_and_hvp!(
f,
grad,
tg::NTuple,
Expand All @@ -509,8 +509,8 @@ function DI.gradient_and_hvp!(
) where {C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
DI.hvp!(f, tg, prep, backend, x, tx, contexts...)
DI.gradient!(f, grad, prep.gradient_prep, backend, x, contexts...)
return grad, tg
y, _ = DI.value_and_gradient!(f, grad, prep.gradient_prep, backend, x, contexts...)
return y, grad, tg
end

## Second derivative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function DI.hvp!(
)
end

function DI.gradient_and_hvp(
function DI.value_gradient_and_hvp(
f,
prep::ZygoteHVPPrep,
backend::AutoZygote,
Expand All @@ -238,12 +238,12 @@ function DI.gradient_and_hvp(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
return DI.gradient_and_hvp(
return DI.value_gradient_and_hvp(
f, prep.fd_prep, DI.SecondOrder(AutoForwardDiff(), backend), x, tx, contexts...
)
end

function DI.gradient_and_hvp!(
function DI.value_gradient_and_hvp!(
f,
grad,
tg::NTuple,
Expand All @@ -254,7 +254,7 @@ function DI.gradient_and_hvp!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, tx, contexts...)
return DI.gradient_and_hvp!(
return DI.value_gradient_and_hvp!(
f,
grad,
tg,
Expand Down
3 changes: 2 additions & 1 deletion DifferentiationInterface/src/DifferentiationInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export jacobian!, jacobian

export second_derivative!, second_derivative
export value_derivative_and_second_derivative, value_derivative_and_second_derivative!
export hvp!, hvp, gradient_and_hvp, gradient_and_hvp!
export hvp!, hvp, value_gradient_and_hvp, value_gradient_and_hvp!
export gradient_and_hvp, gradient_and_hvp!
export hessian!, hessian
export value_gradient_and_hessian, value_gradient_and_hessian!

Expand Down
Loading
Loading