Skip to content

Use prob.f.adtype for the HybridPSO local-phase gradient - #147

Merged
utkarsh530 merged 2 commits into
SciML:mainfrom
AdityaPandeyCN:hybrid-enzyme-adtype
Oct 1, 2026
Merged

utkarsh530 merged 2 commits into
SciML:mainfrom
AdityaPandeyCN:hybrid-enzyme-adtype

Conversation

@AdityaPandeyCN

@AdityaPandeyCN AdityaPandeyCN commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

The local phase of HybridPSO (both SimpleLBFGS and BFGS) always used ForwardDiff for the gradient. It now uses instantiate_gradient(f, prob.f.adtype), so users can opt in to Enzyme with OptimizationFunction(f, AutoEnzyme()).

Changes:

  • EnzymeGradient gets an SVector method that passes the input as Active through autodiff_deferred, so it can run inside GPU kernels without the MVector shadow the generic method uses.
  • instantiate_gradient(f, _) falls back to ForwardDiffGradient, so problems without an AD type (NoAD(), the default) behave exactly as before.
  • test/lbfgs.jl runs the hybrid Rosenbrock cases (bounded and unbounded) with AutoEnzyme() and NoAD().

The SimpleLBFGS and BFGS local phases in HybridPSO hardcoded ForwardDiff.
They now go through instantiate_gradient, so OptimizationFunction(f, AutoEnzyme())
uses Enzyme for the local phase.

EnzymeGradient gets an SVector method that differentiates with an Active argument
via autodiff_deferred, so it runs inside GPU kernels without the MVector shadow.
Problems without an AD type (NoAD()) fall back to ForwardDiff, keeping the
current default behavior.
@AdityaPandeyCN
AdityaPandeyCN marked this pull request as draft September 30, 2026 20:34
@AdityaPandeyCN

Copy link
Copy Markdown
Contributor Author

@utkarsh530 Please take a look

@AdityaPandeyCN
AdityaPandeyCN marked this pull request as ready for review September 30, 2026 21:39
@utkarsh530
utkarsh530 merged commit f170cc9 into SciML:main Oct 1, 2026
28 checks passed
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