Skip to content

Fix Zygote gradients for mutable surrogates - #632

Draft
ChrisRackauckas-Claude wants to merge 1 commit into
masterfrom
agent/628-surr628
Draft

ChrisRackauckas-Claude wants to merge 1 commit into
masterfrom
agent/628-surr628

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

🚧 UNREVIEWED — awaiting review by @ChrisRackauckas. Filed by an AI agent running as @ChrisRackauckas; Chris has not seen this.
Harness: Codex CLI 0.171.0 · Model: gpt-6.2-sol
Conversation: local session 01a21836-a14f-7210-a6ca-1e0f89d2ea7f (no session URL)

Inverse-distance, Lobachevsky, Kriging, and GEK prediction closures now capture their fitted fields directly. This keeps a mutable surrogate out of the differentiated reductions and comprehensions where Zygote 0.7.20 tried to add a ChainRules MutableTangent to Zygote’s named-tuple tangent.

Verification

Julia 1.13.4 with ChainRulesCore 1.26.8. Each command uses an isolated project selected by AD_ENV, with Surrogates developed at either the base revision or the PR revision.

Before the patch, Zygote 0.7.20 (exit 1). The direct include stopped at the failing Zygote testset.

julia --startup-file=no --project="$AD_ENV" -e 'using Surrogates, Zygote, ChainRulesCore; @show pkgversion(Zygote) pkgversion(ChainRulesCore); @assert occursin("baseline", pathof(Surrogates)); include("test/AD_compatibility.jl")'

Output excerpt from the failing test summary.

ERROR: LoadError: Some tests did not pass: 118 passed, 0 failed, 6 errored, 0 broken.
Test Summary:                      | Pass  Error  Total     Time
Zygote                             |  118      6    124  1m40.0s

After the patch, the same test file ran with Zygote 0.7.20 and 0.7.21.

julia --startup-file=no --project="$AD_ENV" -e 'using Zygote, ChainRulesCore; @show pkgversion(Zygote) pkgversion(ChainRulesCore); include("test/AD_compatibility.jl")'

Zygote 0.7.21 output tail.

pkgversion(Zygote) = v"0.7.21"
pkgversion(ChainRulesCore) = v"1.26.8"
Test Summary: | Pass  Total     Time
ForwardDiff   |   90     90  3m25.4s
Test Summary: | Pass  Total     Time
Zygote        |  143    143  1m26.5s
Test Summary:               | Pass  Total   Time
AD for extension surrogates |   16     16  23.8s
Test Summary:   | Pass  Total  Time
multi-output AD |   11     11  9.4s
Test Summary:                             | Pass  Total     Time
AD for the remaining extension surrogates |   29     29  1m00.0s

Zygote 0.7.20 output tail.

pkgversion(Zygote) = v"0.7.20"
pkgversion(ChainRulesCore) = v"1.26.8"
Test Summary: | Pass  Total     Time
ForwardDiff   |   90     90  4m08.4s
Test Summary: | Pass  Total     Time
Zygote        |  143    143  1m25.1s
Test Summary:               | Pass  Total   Time
AD for extension surrogates |   16     16  24.5s
Test Summary:   | Pass  Total  Time
multi-output AD |   11     11  9.1s
Test Summary:                             | Pass  Total   Time
AD for the remaining extension surrogates |   29     29  54.4s

Review

Please check whether moving the fitted field reads outside the closures is enough for downstream callers that differentiate through model parameters as well as through query points. Local verification covered the complete AD compatibility file on the two versions above. The rest of the repository test suite, other Julia versions, and gradients with respect to fitted model parameters were outside that verification.

Closes #628


🤖 Posted by an AI agent — harness: Codex CLI 0.171.0 · model: gpt-6.2-sol
Conversation: local session 01a21836-a14f-7210-a6ca-1e0f89d2ea7f (no session URL)

Read fitted fields before the reductions and comprehensions so Zygote
does not accumulate a MutableTangent from ChainRules against its own
named-tuple tangent for the same surrogate.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Codex <noreply@openai.com>
Agent-Harness: Codex CLI 0.171.0
Agent-Model: gpt-6.2-sol
Agent-Job: 628-codex
Agent-Session: local session 01a21836-a14f-7210-a6ca-1e0f89d2ea7f (no session URL)
@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #632

Reviewed head: d2e7806
Raw SHA-256: 0e5ec0ca3b0d33bef379e4621d89c739760e3ebb491006150e12c7a08fbad4bf
Source SHA-256: 68f53f0fa036e31d96c17dd098297577c72cd873cf8a4873e8b07773ee23cc77
Source: operator-attested-codex-v1 (runner attested by pankgeorg)
Reviewer session: 01a223e0-4a6e-77b0-be01-2f81e1a9bfe7
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes
Redaction: ran (substitutions=0)

Risk assessment

  • Risk: medium-low (ML11)
  • Verdict: MERGE
  • Reviewed head: d2e7806

Review report

VERDICT: MERGE
PR: #632
Head: d2e7806
Base: 7e54927
Reviewer: codex-cli 0.172.0 / gpt-6.2-sol
Session: 01a223e0-4a6e-77b0-be01-2f81e1a9bfe7

Risk

medium-low (ML11) — Before/after benchmarks show lower Zygote allocation on common prediction paths, with matching prediction and query-gradient results and passing downstream tests.

Surrogates.jl users calling Kriging, GEK, InverseDistanceSurrogate, or LobachevskySurrogate are affected, including callers of Zygote.gradient, Kriging and GEK’s std_error_at_point, and optimizers that consume these predictions. The patch changes closure captures during evaluation. Public signatures, fitting defaults, solver selection, solve/init, caches, and modeling compilation are outside the diff. An explicit inverse-distance response-gradient call also has a verified wrong-to-right fix.

Blocking findings

None.

Nonblocking findings

  • test/AD_compatibility.jl:531, src/InverseDistanceSurrogate.jl:76 — The inverse-distance tests differentiate the query, leaving the tied-sample response pullback unprotected. Add a regression for InverseDistanceSurrogate([0., 0., 2.], y, 0., 2.; p=2.)(0.) that asserts the Zygote gradient with respect to y = [1., 7., 5.] is [0.5, 0.5, 0.0]. I reproduced [1.5, 1.0, 0.0] before the patch and the analytical result at head.
  • test/GEK.jl:14, test/AD_compatibility.jl:594, test/AD_compatibility.jl:857 — reference_gek_std_error takes its cross-covariance from the changed _gek_r, so a sign or ordering error can enter both sides of the comparison. The existing Zygote accuracy assertions are commented out in both dimensions. Build the reference vector independently from Gaussian kernel derivatives and add a numerical assertion to the Zygote test. For a single sample at zero with value 3, slope 2, and theta=0.75, the query gradient at 0.4 should be approximately 1.3481190638100793.

What I ran

  • diff -qr before head and git apply --check against before/ and in reverse against head/. The patch matched both snapshots and touched four source files. The test files were identical.

  • G1 and G2. On ARM64 with Julia 1.13.4 and Zygote 0.7.21, ran julia --startup-file=no -O1 --project=.review/probe-<snapshot> .review/check.jl on both snapshots. Both passed 32/32 assertions. The references were closed-form ordinary Kriging, Gaussian Hermite interpolation for GEK, Shepard weights, and the centered cubic B-spline. Across the printed 1D and 2D predictions and Zygote query gradients, the largest absolute error was 1.11e-15. Kriging and GEK standard errors matched closed forms. Separate edge.jl and response.jl runs reproduced the tied-response fix, verified off-sample response gradients, and checked both uncertainty gradients against centered finite differences. My initial harness had errors in the multidimensional reference calculations. Both snapshots passed after I corrected those references.

  • G3. The captures are local to each call. The inverse-distance probe verified that a prediction after update! sees the newly added sample. The four model-specific test files and VariableFidelity.jl passed 913 assertions on head. julia --startup-file=no -O1 --project=.review/head .review/downstream.jl passed 12/12 assertions covering EI with Kriging and GEK and SRBF with inverse distance and Lobachevsky.

  • Ran julia --startup-file=no --project=.review/<snapshot> .review/bench.jl for both snapshots, then compared the results with python3 .review/compare_bench.py. Predictions and query gradients matched on both grids. Direct prediction allocations were identical. Repeated same-process inverse-distance timings showed no consistent slowdown. These are warmed medians on ARM64 for Zygote query gradients at 100 training points.

    Model Time before → head (ms) Allocated before → head (MB)
    Kriging 90.99 → 7.59 16.47 → 3.21
    GEK 40.22 → 11.05 17.81 → 5.02
    Inverse distance 6.56 → 1.34 2.20 → 1.24
    Lobachevsky 35.06 → 8.97 12.39 → 6.00

What I did not verify

  • I stopped the full AD_compatibility.jl and optimization.jl runs before they produced a summary. The targeted probes above exercised the changed CPU AD paths and downstream optimizer calls. The diff contains no GPU kernel, Reactant, or MPI changes, and I ran no accelerator tests.
  • I did not reproduce the 32-bit x86 job. ci.json records failures at both head and target, but supplies no failure logs to establish whether the causes match. Its successful benchmark jobs supply conclusions only. The measurements above are local. Head x86 job · Target x86 job

Head link: d2e7806


🤖 Posted by an AI agent — harness: codex-cli 0.172.0 · model: gpt-6.2-sol
Conversation: local session 01a223e0-4a6e-77b0-be01-2f81e1a9bfe7 (no session URL)

This branch has not been deployed

No deployments
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.

[JOSS review] Zygote.gradient fails on several surrogates since Zygote 0.7.16

4 participants