Skip to content

[ad-18] add production Enzyme energy HVP#52

Open
krystophny wants to merge 27 commits into
developfrom
ad-18-enzyme-ad-fixture-hvp
Open

[ad-18] add production Enzyme energy HVP#52
krystophny wants to merge 27 commits into
developfrom
ad-18-enzyme-ad-fixture-hvp

Conversation

@krystophny

@krystophny krystophny commented Jul 9, 2026

Copy link
Copy Markdown
Member

Summary

  • Generate a production forward-over-reverse energy HVP symbol (gvec_mhd3d_flat_energy_hvp) from the fixed-map kernel with __enzyme_fwddiff over the reverse-mode gradient.
  • Cache a gradient work buffer in MODgvec_py_state_ad and expose eval_energy_hvp_enzyme with size and context status checks.
  • Add State.energy_hessian_vector_product(q, v, method=...) and State.raw_residual_jvp(q, v, method=...) with the sign contract raw_residual_jvp == -energy_hvp; State.hessian_vector_product(v) stays as the current-state residual-JVP compatibility API.
  • Rename the production gradient test executable to gvec_enzyme_production_operators, register it as CTest gvec_enzyme_hvp, and extend it with an HVP check against a centered finite difference of the raw projected force; no duplicate executable run.
  • Enzyme-off builds keep the API and raise a typed UnsupportedFeatureError for exact requests.

Closes #19.

Verification

Toolchain: LLVM/Flang 22.1.6, Enzyme LLVMEnzyme-22.so built from commit c9650834.

Before (b678d3a)

No production HVP symbol and no production HVP test:

$ nm lib/libGVEC.a | grep -E " T gvec_mhd3d_flat_energy"
00000000000020c0 T gvec_mhd3d_flat_energy
0000000000002220 T gvec_mhd3d_flat_energy_gradient
$ ctest -N -R production
  Test #6: gvec_enzyme_production_gradient

After (8825058)

Fresh fixed-RZ and fixed-cyl Flang/Enzyme builds (USE_ENZYME=ON, static lib, no netCDF):

RZ:  100% tests passed, 0 tests failed out of 6  (ctest -R '^gvec_enzyme_')
cyl: 100% tests passed, 0 tests failed out of 6
OpenMP RZ: 100% tests passed, 0 tests failed out of 7
           incl. gvec_enzyme_hvp (OMP_NUM_THREADS=1) and gvec_enzyme_hvp_omp4 (OMP_NUM_THREADS=4)

Symbol audit, both hmaps: exactly one definition each and no unresolved Enzyme markers.

$ nm lib/libGVEC.a | grep -E " T gvec_mhd3d_flat_energy"
1 gvec_mhd3d_flat_energy
1 gvec_mhd3d_flat_energy_gradient
1 gvec_mhd3d_flat_energy_hvp
$ nm lib/libGVEC.a | grep -c " U __enzyme"
0

Isolated Python builds:

Enzyme-off: test_ad_public_api.py + test_ad_hvp.py -> 15 passed
Enzyme-on (fixed RZ, Flang-built f90wrap): 15 passed

Exact HVP against centered FD on ad_elliptok_lowres (n_dof=20, seed 42):

HVP exact vs FD: rel diff 4.760e-09, cosine 1.000000000000
raw_residual_jvp == -energy_hvp: True (bitwise)
energy gradient norm 26.115398458257

fo full pipeline passed.

@krystophny

Copy link
Copy Markdown
Member Author

Implemented the AD-18 validation slice here: the low-resolution AD fixture now computes an Enzyme forward-over-reverse HVP and checks the residual sign contract on interior DOFs against centered raw-force differences. Verification: Enzyme CTest surface is 9/9 passing, including gvec_enzyme_ad_fixture_hvp and _omp4; uv run pytest test-CI/unit-pygvec/test_ad_hvp.py -q --require-pygvec is 5/5 passing; git diff --check is clean; fo reports no Fortran project detected.

@krystophny krystophny force-pushed the ad-18-enzyme-ad-fixture-hvp branch from a561d3e to c7ff715 Compare July 9, 2026 20:01
@krystophny krystophny force-pushed the ad-18-enzyme-ad-fixture-hvp branch from c7ff715 to 8825058 Compare July 9, 2026 22:01
@krystophny krystophny changed the title [ad-18] validate Enzyme residual HVP on AD fixture [ad-18] add production Enzyme energy HVP Jul 9, 2026
@krystophny krystophny force-pushed the ad-18-enzyme-ad-fixture-hvp branch from eb3c73e to 07668ed Compare July 9, 2026 22:02
@krystophny krystophny force-pushed the ad-18-enzyme-ad-fixture-hvp branch from 07668ed to 9ef05f0 Compare July 9, 2026 22:12
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