Skip to content

[ad-17] add finite-difference residual HVP fallback#51

Open
krystophny wants to merge 25 commits into
developfrom
ad-17-fd-residual-hvp
Open

[ad-17] add finite-difference residual HVP fallback#51
krystophny wants to merge 25 commits into
developfrom
ad-17-fd-residual-hvp

Conversation

@krystophny

Copy link
Copy Markdown
Member

Closes #18.

Add a finite-difference Hessian-vector product fallback over the public raw residual API:

  • State.hessian_vector_product(vector, eps=1e-6, method="fd") returns the centered residual difference at the current state.
  • The method validates vector length before perturbing the state.
  • Unknown methods raise UnsupportedFeatureError, so the finite-difference path remains explicit and does not pretend to be the future exact Enzyme HVP.
  • Tests cover the centered formula, zero vector, state/input preservation, wrong length, and unknown method behavior.

Verification

Test fails on main

On the parent of this PR branch:

$ uv run pytest test-CI/unit-pygvec/test_ad_hvp.py -q --require-pygvec
FFFFF                                                                    [100%]
AttributeError: 'State' object has no attribute 'hessian_vector_product'
5 failed in 0.91s

Test passes after fix

$ uv run pytest test-CI/unit-pygvec/test_ad_hvp.py -q --require-pygvec
.....                                                                    [100%]
5 passed in 9.78s

$ uv run pytest test-CI/unit-pygvec/test_ad_layout.py test-CI/unit-pygvec/test_ad_energy.py test-CI/unit-pygvec/test_ad_raw_force.py test-CI/unit-pygvec/test_ad_context.py test-CI/unit-pygvec/test_ad_public_api.py test-CI/unit-pygvec/test_ad_preconditioner.py test-CI/unit-pygvec/test_ad_hvp.py -q --require-pygvec
...............................sss..s...                                 [100%]
36 passed, 4 skipped in 10.29s

$ git diff --check
# no output

$ fo
fo: no Fortran project detected

@krystophny

Copy link
Copy Markdown
Member Author

Fixed AD-17 by adding State.hessian_vector_product(..., method="fd") over the public raw residual API. The fallback is explicit, validates vector length before perturbation, preserves state and input vector, returns zero for the zero direction, and rejects non-fd methods so exact Enzyme HVP remains a separate future path.

@krystophny krystophny force-pushed the ad-17-fd-residual-hvp branch from 8c2776b to ea4be1a Compare July 9, 2026 20:01
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.

1 participant