Skip to content

feat(DI): add value_gradient_and_hvp and deprecate gradient_and_hvp - #1086

Merged
gdalle merged 5 commits into
JuliaDiff:mainfrom
gdalle-bot:dit-01-value-gradient-and-hvp
Nov 6, 2026
Merged

gdalle merged 5 commits into
JuliaDiff:mainfrom
gdalle-bot:dit-01-value-gradient-and-hvp

Conversation

@gdalle-bot

Copy link
Copy Markdown
Contributor

Important

This PR was generated by a coding agent, please disregard it until @gdalle has reviewed it.

Summary

  • New operators value_gradient_and_hvp(f, [prep,] backend, x, tx, [contexts...]) -> (y, grad, tg) and value_gradient_and_hvp!, so that every second-order operator with a value also returns the primal value (like value_gradient_and_hessian and value_derivative_and_second_derivative).
  • gradient_and_hvp(!) are deprecated and forward to the new operators, with a Base.depwarn.
  • Backends which get the value for free return it: reverse-over-forward (inner value_and_gradient), Mooncake (value_and_hvp!!), Symbolics and FastDifferentiation (gradient preparation), GTPSA (value_gradient_and_hessian!), Zygote (forwards to ForwardDiff-over-Zygote), HyperHessians (hvp_gradient_value). Forward-over-anything and reverse-over-reverse use one extra primal call.
  • DIT now tests value_gradient_and_hvp(!) instead of the deprecated functions, including the primal value. This keeps DIT's JET tests clear of the deprecation warning, and tests the new operator on every backend.
  • DI version bumped to v0.7.22, DIT compat on DI raised accordingly, and CHANGELOG entries added.

This is also a prerequisite for an upcoming revamp of DIT scenarios, where every second-order operator with a value then returns the same orders (y, grad, second-order).

Tests

  • New DifferentiationInterface/test/Core/Internals/deprecations.jl: the deprecated functions return the same results as the new ones and are flagged by @test_deprecated.
  • DIT Standard group passed locally (Julia 1.13, -O1): 57266 tests.

🤖 Generated with Claude Code

Model used: Claude Opus 5.5

gdalle-bot and others added 2 commits November 6, 2026 11:28
Add `value_gradient_and_hvp(!)`, which returns `(y, grad, tg)` so that
every second-order operator with a value returns the primal value.
The deprecated `gradient_and_hvp(!)` now forward to the new operators.

Backends that compute the value for free return it (reverse-over-forward,
Mooncake, Symbolics, FastDifferentiation, GTPSA, Zygote, HyperHessians);
forward-over-anything and reverse-over-reverse use one extra primal call.

The legacy DIT engine now tests `value_gradient_and_hvp(!)`, including
the primal value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gdalle
gdalle marked this pull request as ready for review November 6, 2026 13:59
@gdalle
gdalle merged commit 7f042d1 into JuliaDiff:main Nov 6, 2026
53 of 54 checks passed
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.18%. Comparing base (1ae85dd) to head (6945b9a).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ifferentiationInterfaceMooncakeExt/second_order.jl 0.00% 17 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1086   +/-   ##
=======================================
  Coverage   98.18%   98.18%           
=======================================
  Files         143      144    +1     
  Lines        8246     8265   +19     
=======================================
+ Hits         8096     8115   +19     
  Misses        150      150           
Flag Coverage Δ
DI 98.07% <77.92%> (+<0.01%) ⬆️
DIT 98.49% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants