Repository navigation
Keep the unwrapped adjoint problem out of AutoDespecialize - #1324
ChrisRackauckas merged 2 commits into
Conversation
`_unwrap_despecialized_problem` kept the original specialization, so an `AutoDespecialize` problem stayed `AutoDespecialize` with concrete parameters. When the sensitivity code concretized it again, `_despecialize_parameters` wrapped `p` back into `DespecializedParameters`, and the parameter tangent gained an extra `params` field (Mooncake through SCC initialization fails). This used to be hidden because `@set prob.f.f = ...` in the ChainRules rule reset the specialization to `AutoSpecialize`; SciMLBase#1606 keeps it. Build the unwrapped function as `AutoSpecialize`, which is what this path had before. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP
| # Not `AutoDespecialize`: the unwrapped problem carries the concrete parameters, and | ||
| # `_despecialize_parameters` must not wrap them again when the sensitivity code | ||
| # concretizes it a second time. |
There was a problem hiding this comment.
Is this the right branch?
There was a problem hiding this comment.
so the fix should live in SciMLSensitivity rather than using the specialization as a marker?
This worked before the specialization fix because the @set prob.f.f = get_raw_f(…) at the top of the rrule which resets the specialization level to AutoSpecialize which had the net effect of leaving p alone, but now, after the fix (SciML/SciMLBase.jl#1606) it would get re-wrapped.
| # `_despecialize_parameters` must not wrap them again when the sensitivity code | ||
| # concretizes it a second time. | ||
| f = _map_parameter_callbacks( | ||
| _unwrap_parameter_callback, prob.f, residual, SciMLBase.AutoSpecialize |
There was a problem hiding this comment.
Seems weird to hardcode this?
There was a problem hiding this comment.
that mirrors the old behavior from before the SciMLBase fix, which was leading to AutoSpecialize, should this use FullSpecialize instead?
There was a problem hiding this comment.
Went with FullSpecialize in 5760526. AutoSpecialize kept p concrete, but on re-concretization maybe_wrap_nonlinear_f still wraps the residual in an AutoSpecializeCallable for any in-place array-state problem. The extended test checks that f.f stays raw and fails under AutoSpecialize. The SciMLSensitivity question in the other thread is still open.
posted by Claude claude-opus-5-5[1m] on behalf of Sebastian
Verdict: changes needed before merge (reviewed at The patch builds the internal unwrapped adjoint/Enzyme problem with AutoSpecialize so subsequent concretization preserves concrete parameters and raw parameter callbacks. The regression is confirmed and all failing CI checks match master, but the unchanged draft still has an unresolved discussion with you about the specialization policy and which package should own the fix, so it is not ready for an unconditional merge recommendation. Risk assessment
Findings
Validation detailsCommands run from the scratch directory: julia +1.12 --project=env-base -e 'using NonlinearSolveBase, SciMLBase; include("wt-head/lib/NonlinearSolveBase/test/autodespecialize.jl")'
julia +1.12 --project=env-head -e 'using NonlinearSolveBase, SciMLBase; include("wt-head/lib/NonlinearSolveBase/test/autodespecialize.jl")'
GROUP=Core julia +1.12 --compiled-modules=existing --project=env-head -e 'using Pkg; Pkg.test("NonlinearSolveBase"; julia_args=["--compiled-modules=existing"])'
julia +1.12 --project=@runic -m Runic --check --diff wt-head/lib/NonlinearSolveBase/src/autospecialize.jl wt-head/lib/NonlinearSolveBase/test/autodespecialize.jlThe first Core attempt stopped before tests because Pkg resolved the sibling SciMLJacobianOperators source to a missing scratch path. After supplying a scratch-local symlink to the head's sibling package, the same test command passed. No tracked source files were changed. The author's end-to-end Mooncake/MTK gradient and finite-difference comparison were not independently rerun; local evidence covers the structural regression and the full Core group. The diff adds no skipped/weakened assertions, dependency, new dependency-internal access, or new public name; no SemVer-major or new-public-API documentation requirement was identified.
Head NonlinearSolveBase Core checks, root Adjoint checks, documentation, Runic and Typos are green. Recent master Sublibrary CI runs that only performed change detection were not treated as evidence that these individual checks passed. AI-generated review — harness: Codex CLI 0.156.1 (Mac); model: gpt-6-astra. Conversation: local session ID Push a fix and the PR is reviewed again automatically at the new head. 🤖 Posted by an AI agent — harness: Claude Code · model: claude-opus-5-5[1m] (fleet master); review by Codex CLI (Mac) / gpt-6-astra |
The unwrapped problem carries raw callables and concrete parameters for the
adjoint and Enzyme paths. `AutoSpecialize` kept the parameters concrete when
the sensitivity code concretizes the problem again, but `maybe_wrap_nonlinear_f`
still wrapped the residual in an `AutoSpecializeCallable`. `FullSpecialize`
opts out of both wrappings.
The regression test now also covers `Vector{Float64}` parameters and checks
that the residual stays raw after re-concretization; that check fails with
`AutoSpecialize`.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pick up SciML#1324 AutoDespecialize adjoint unwrap fix so Adjoint CI matches master. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Cursor Agent <[email protected]> Agent-Harness: Cursor Agent CLI 2026.09.26-dd393fe Agent-Model: auto Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/NonlinearSolve.jl/jobs/1197-cursor/log.txt on amdci2.julia.csail.mit.edu
Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
_unwrap_despecialized_problembuilds the problem that the adjoint path (_solve_adjoint) and the Enzyme path (maybe_unwrap_prob_for_enzyme) use: raw residual, raw callbacks, concretep. It kept the original specialization, so anAutoDespecializeproblem stayedAutoDespecialize. When the sensitivity code concretizes that problem again,_despecialize_parameterssees a problem that isAutoDespecializeand not wrapped, and wrapspback intoDespecializedParameters.Why this didn't show before: the ChainRules
solve_uprrule starts with@set prob.f.f = get_raw_f(prob.f.f), and SciMLBase'sconstructorofreset every rebuilt function toAutoSpecialize. The problem reaching_unwrap_despecialized_problemwas therefore neverAutoDespecializeon this path. SciML/SciMLBase.jl#1606 makes@setkeep the specialization, which exposes this.Symptom (with SciML/SciMLBase.jl#1606): SciMLSensitivity Core8,
desauty_dae_mwe.jl:163"Mooncake through init" (use_scc = true). The parameter tangent coming out of the steady-state backpass gains an extraparams::MTKParametersfield next totunable/initials/..., andNonlinearSolveBaseMooncakeExt._accum_cr_tangent!throwsArgumentError: The fdata type Mooncake.FData{...} ... and tangent type @NamedTuple{params::MTKParameters, tunable, ...}.Change:
_unwrap_despecialized_problembuilds the unwrapped function asFullSpecialize._map_parameter_callbackstakes an optional specialization argument for this. When the problem is concretized again,FullSpecializekeeps bothpand the residual as they are:AutoDespecializewould wrappback intoDespecializedParameters, andAutoSpecialize(what this path got through the reset before) would wrap the residual in anAutoSpecializeCallablefor in-place array-state problems.Tests: a new testset in
test/autodespecialize.jlchecks that the unwrapped problem keeps the concretep, the raw residual and raw callbacks afterget_concrete_problem, for a structpand aVector{Float64}p. Thepand Jacobian checks fail on master, the residual check fails withAutoSpecialize, and all pass with this change; the rest of the file passes in all three. NonlinearSolveBase Core passes locally (Julia 1.12.7, 535 assertions).Verification (Julia 1.13.0, local, CI's versions: MTK 11.45.1, Mooncake 0.5.60, SCCNonlinearSolve 1.15.3, SciMLSensitivity 7.119.11):
FullSpecialize(5760526) and released SciMLBase 3.56.1, which includes Keep the specialization when rebuilding a SciML function SciMLBase.jl#1606: SciMLSensitivitytest/Core8/desauty_dae_mwe.jlgives 19 pass, 5 broken, 1 error, and "Mooncake through init" passes. With released NonlinearSolveBase 2.54.0 in the same environment it gives 17 pass, 5 broken, 2 errors: "Mooncake through init" throws theArgumentErrorabove. The remaining error in both is "Enzyme through init" (EnzymeNoTypeError).AutoSpecializeversion):test/Core8/desauty_dae_mwe.jlin full: identical with Keep the specialization when rebuilding a SciML function SciMLBase.jl#1606 + this and with released SciMLBase 3.56.0 + this: 19 pass, 5 broken, 1 error. The error is "Enzyme through init" (:123,EnzymeNoTypeError), which already fails on master. "Mooncake through init" passes in both.No compat bump is needed: the change is correct with both released SciMLBase and SciML/SciMLBase.jl#1606.
Related: SciML/SciMLBase.jl#1606 exposed other places that relied on the reset:
promote_fwas not idempotent for concretizedAutoDespecializefunctions.AutoRespecializecallbacks lost their type erasure.I also checked NonlinearSolveBase's other
@set f.f = get_raw_f(...)sites (Jacobian cache, bounds transform, Enzyme unwrap, autodiff). They are local transforms inside__init; none of them goes throughget_concrete_problemagain, so the kept specialization doesn't change them.Written by Claude Code on Sebastian's behalf.
🤖 Generated with Claude Code
https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP