[AI] OptimizationBase: fix Enzyme out-of-place grad/fg wiring#1284
Draft
ChrisRackauckas-Claude wants to merge 1 commit into
Draft
[AI] OptimizationBase: fix Enzyme out-of-place grad/fg wiring#1284ChrisRackauckas-Claude wants to merge 1 commit into
ChrisRackauckas-Claude wants to merge 1 commit into
Conversation
7 tasks
Two pre-existing bugs in the `OptimizationFunction{false}` method of
`OptimizationEnzymeExt.instantiate_function`:
- The fallback that forwards a user-supplied gradient was gated on `fg == true`
rather than `g == true`, so `g = true, fg = false` with a supplied `f.grad`
produced `grad === nothing`. Every other extension, and the in-place Enzyme
method, gate on `g`.
- The generated `fg!` differentiated into `res_fg` but returned `res`, the
buffer belonging to the `grad` branch. With `g = false` that name is never
assigned and `fg!` throws `UndefVarError: res not defined`; with `g = true`
it silently returns the untouched gradient buffer — `[0.0, 0.0]` on a
first call.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014Uj4Pu1LKHYqLSTLfPYDPo
ChrisRackauckas-Claude
force-pushed
the
enzyme-oop-fixes
branch
from
July 26, 2026 21:10
b83f751 to
a471019
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rebased onto
masternow that #1283 has merged (as e254f94). This PR is now standalone: one commit, 2 source lines plus a testset.Two pre-existing bugs in the
OptimizationFunction{false}method ofOptimizationEnzymeExt.instantiate_function, noticed while working on #1282. Neither is related to that regression.1. Gradient fallback gated on the wrong flag
g = true, fg = falsewith a suppliedf.gradfalls through tograd = nothing. Every other extension — and the in-place Enzyme method 400 lines up — gates ong.2.
fg!returns the wrong bufferresbelongs to thegradbranch. Withg = falseit is never assigned andfg!throws; withg = trueit silently returns the untouched gradient buffer.Observed, before vs after
Driver:
OptimizationFunction{false}(rosen, AutoEnzyme(); grad = rosen_grad), evaluated at[0.5, 0.7]where the true gradient is[-91.0, 90.0].g=true, fg=false, user gradoptf.grad === nothingoptf.grad(z) ≈ [-91.0, 90.0]g=false, fg=trueUndefVarError: res not definedy ≈ 20.5,G ≈ [-91.0, 90.0]g=true, fg=trueG = [0.0, 0.0](silently wrong)G ≈ [-91.0, 90.0]The third row is the dangerous one — a zero gradient with no error.
Note these paths need both
ChainRulesCoreandEnzymeloaded to be reached, since the extension trigger isOptimizationEnzymeExt = ["ChainRulesCore", "Enzyme"]; with onlyEnzymeloaded,AutoEnzymefalls through to the generic DI extension, which is presumably why this went unnoticed.OptimizationBase5.2.3 → 5.2.4.Test plan
New
"Enzyme out-of-place grad/fg wiring"testset inlib/OptimizationBase/test/AD/adtests.jlcovering all three cases above. All three fail on the parent branch.Run locally on Julia 1.11:
OPTIMIZATION_TEST_GROUP=AD Pkg.test()for OptimizationBase — 781 pass, 0 fail (re-run after the rebase, against the newmasterbase)🤖 Generated with Claude Code
https://claude.ai/code/session_014Uj4Pu1LKHYqLSTLfPYDPo