Skip to content

Solve each safetycopy branch of batch_func on its own problem - #1634

Merged
ChrisRackauckas merged 3 commits into
SciML:masterfrom
wsmoses:wm/batch-func-no-prob-phi
Nov 3, 2026
Merged

ChrisRackauckas merged 3 commits into
SciML:masterfrom
wsmoses:wm/batch-func-no-prob-phi

Conversation

@wsmoses

@wsmoses wsmoses commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Fixes reverse-mode Enzyme through an EnsembleProblem of SDEProblems (SciML/SciMLSensitivity.jl#1696).

Problem. batch_func joined the three problem choices into one value before solving:

_prob = if prob.safetycopy
    deepcopy(prob.prob)
elseif thread_prob !== nothing
    thread_prob
else
    prob.prob
end
new_prob = prob.prob_func(_prob, ctx)

When the problem is passed by reference, this join is a pointer phi between a freshly allocated GC object (the deepcopy) and the caller's memory (prob.prob). Enzyme cannot keep that phi rooted for the garbage collector, so compilation failed with an EnzymeInternalError (nodecayed_phis!). On the latest Enzyme main this becomes a runtime EnzymeRuntimeExceptionMI instead, but the gradient still fails. The join block is reached on every trajectory, so it fails even with the default safetycopy = false, where the deepcopy branch never runs.

Change. A helper _batch_solve picks the problem and calls _batch_solve_one (prob_func → solve → output_func) inside each branch. Only the results join, never the problems. The rerun loop uses the same helper. Behavior is otherwise unchanged.

Results (Julia 1.12.7, Enzyme 0.13.210):

  • The issue's reproducer differentiates with EnsembleSerial and EnsembleThreads.
  • With fixed per-trajectory noise seeds, the Enzyme gradient matches ForwardDiff to about 1e-15 for serial and threaded ensembles.
  • The Core and Downstream test groups pass locally.
  • New test test/downstream/ensemble_enzyme_sde.jl (DownstreamAD group) compares Enzyme against ForwardDiff for both algorithms. It passes with this change and fails with EnzymeInternalError without it.

The test sets safetycopy = false, because a custom prob_func otherwise turns safetycopy on. On that path Enzyme hits a separate problem differentiating the deepcopy of an SDEProblem itself (MethodError: no method matching accumulate_into(::Base.RefValue{SDEProblem…})). That is an Enzyme issue and is not addressed here.

🤖 Generated with Claude Code

https://claude.ai/code/session_015RAaewsVwF5wyRBfErBEEr

wsmoses and others added 2 commits October 5, 2026 10:27
batch_func joined the deepcopy, the per-task copy and the user's problem
into one problem before solving. When the problem is passed by reference,
that join is a pointer phi of a fresh GC object and the caller's memory,
which Enzyme cannot keep rooted, so reverse-mode Enzyme through an
EnsembleProblem of SDEProblems failed (SciMLSensitivity.jl#1696). Solve
within each branch instead, through a shared helper, so only the results
join. The rerun loop uses the same helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015RAaewsVwF5wyRBfErBEEr
@wsmoses

wsmoses commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@ChrisRackauckas

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

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

Independent review (Codex CLI 0.166.0 / gpt-6.1-sol), session 01a19722-009c-71c0-9045-b568beca2f51, reviewed head 9fe43bf

  • Verdict: CHANGES
  • Risk: medium-high (MH1: the refactor affects initial and repeated trajectory execution through a core solve entry point).
  • Round / slot: 1 / 1
  • Blocking priorities: P1

Review written by the reviewer named above in a session other than the PR author's, and copied here by the posting agent without change to its findings. The posting agent did not re-run the reviewer's commands.

Summary: Splits ensemble problem selection into separate solve branches to avoid an Enzyme pointer-phi failure and adds serial/threaded SDE gradient coverage. Changes are required because the new regression errors in both supported CI configurations and the local head run.

Blast radius: All basic ensemble backends call batch_func; safetycopy, per-task JumpProblem copies, output hooks, and reruns share the extracted helpers.

Evidence: Blind diff assessment was frozen and hashed before author context. CI has 65 successful and seven failed checks; the added serial SDE regression errors on Julia 1 and lts, while the Julia 1 threaded case passes. The local Julia 1.13.2 head run also errors with UndefRefError (exit 1); the same added test fails on merge-base with EnzymeRuntimeException and fails on head with UndefRefError, so no passing-after result exists. An analytic copy/rerun probe passes 39/39 assertions.

Blocking findings

  1. [P1] The added regression does not pass on supported CI versions (test/downstream/ensemble_enzyme_sde.jl:42; test/runtests.jl:202). Confirmed by running: the reviewer's Julia 1.13.2 head run raises UndefRefError and ends with zero passed and one errored test. CI Julia 1 also raises UndefRefError: access to undefined reference in the Enzyme reverse solve rule for EnsembleSerial; lts raises MixedReturnException and later segfaults in the same new test. Expected: both ensemble algorithms finish and their gradients satisfy the ForwardDiff comparison. Resolve the Enzyme/SciMLSensitivity integration problem, with compatible dependency bounds if an upstream repair is required, and obtain passing DownstreamAD jobs on both supported versions without skipping or weakening this regression.

Non-blocking findings

  1. non-blocking: Rewrite the retrospective explanation in test/downstream/ensemble_enzyme_sde.jl:6 ("used to join") as a concise description of the invariant being tested. The new test's comment block is also substantially above the requested approximately 10% comment budget.

What I ran

  • git status --short, git rev-parse HEAD origin/master, git diff --stat origin/master...HEAD, and git diff origin/master...HEAD: exit 0; inspected all three changed files and surrounding ensemble dispatch, RNG, copy, and rerun logic.

  • git fetch origin '+refs/pull/1634/head:refs/remotes/pr/1634' master, git checkout --detach refs/remotes/pr/1634, git fetch origin '+refs/heads/master:refs/remotes/origin/master', and git merge-base origin/master HEAD: exit 0. Reviewed head matches live headRefOid; merge-base is the Base above. Fetched master tip is 030ec28.

  • gh pr view 1634 --repo SciML/SciMLBase.jl --json number,url,state,isDraft,headRefOid,baseRefName,baseRefOid: exit 0; OPEN, non-draft, target head unchanged. After hashing INITIAL.md, inspected the PR body, commit metadata, and gh issue view 1696 --repo SciML/SciMLSensitivity.jl --json title,body,state (exit 0).

  • git log -1 --no-merges --format=%B: exit 0; neither Agent-Harness nor Agent-Model trailer exists.

  • gh pr checks 1634 --repo SciML/SciMLBase.jl: exit 1, seven failures. gh pr checks 1634 --repo SciML/SciMLBase.jl --json name,state,link: exit 0, 65 SUCCESS and seven FAILURE checks.

  • gh run view 37336692017 --repo SciML/SciMLBase.jl --log-failed, gh run view 37336691832 --repo SciML/SciMLBase.jl --log-failed, and gh run view 37336690678 --repo SciML/SciMLBase.jl --log-failed: exit 0 with a writable XDG cache override; the initial attempt without the override failed because the default cache was read-only. Inspected Julia 1 DownstreamAD, lts DownstreamAD, integration failures, and release failure. Julia 1 tail: 1 passed, 0 failed, 1 errored, 0 broken; the pass belongs to EnsembleThreads. Other failures: SCCNonlinearProblem missing u0, Waltman matrix containing Infs/NaNs, Mooncake constructor for Any, and Catalyst extension-project permission denied.

  • gh run list --repo SciML/SciMLBase.jl --branch master --event push --limit 5 --json databaseId,headSha,conclusion,url,name,createdAt: exit 0. gh run view 41250980771 --repo SciML/SciMLBase.jl --log-failed and gh run view 41250980378 --repo SciML/SciMLBase.jl --log-failed: exit 0 with writable cache. Master tests fail with a different indexing BoundsError; this does not excuse the PR's missing-u0 error. Master release test has the same Catalyst extension-project permission failure.

  • Julia run 1: timeout 3600 julia +1.13.2 --startup-file=no run_head.jl, with JULIA_NUM_THREADS=4, OPENBLAS_NUM_THREADS=1, an isolated TMPDIR, the designated writable depot followed by the default depots, automatic Pkg precompilation disabled, and four precompile tasks. The script activates a separate environment, develops the head checkout, instantiates without registry updates, and includes the PR's unchanged test/downstream/ensemble_enzyme_sde.jl. Exit 1; VERSION=1.13.2 THREADS=4; UndefRefError: access to undefined reference; 0 passed, 0 failed, 1 errored, 0 broken; serial testset time 4m52.0s. Versions: Enzyme 0.13.216, SciMLSensitivity 7.119.14, StochasticDiffEq 7.2.0, ForwardDiff 1.4.6.

  • gh run list --repo SciML/SciMLBase.jl --branch master --event push --workflow Downstream.yml --limit 5 --json databaseId,headSha,conclusion,url,name,createdAt and gh run view 41250980630 --repo SciML/SciMLBase.jl --log-failed: exit 0. Master integration run shows different Core2/Core4/Core8 sensitivity errors; no matching Waltman or Mooncake constructor failure was established.

  • Julia run 2: timeout 3600 julia +1.13.2 --startup-file=no run_base.jl, with the same environment variables as run 1. git -C src archive 432e9856bd21cb7d7fe078768de0040da0ee0237 supplies an isolated merge-base tree. The base environment copies the head's resolved Project/Manifest and changes only SciMLBase's source path to that tree; the script activates it and includes the unchanged test file from the PR head. Exit 1; VERSION=1.13.2 THREADS=4; EnzymeRuntimeException: Enzyme execution failed within responsible_map; 0 passed, 0 failed, 1 errored, 0 broken; serial testset time 1m56.0s. The trace points into the merge-base's ensemble solver. Own-run conclusion: fails before, also fails after.

  • Julia run 3: timeout 3600 julia +1.13.2 --startup-file=no run_probe.jl, with the same environment variables as run 1 and the head environment. Exit 0; Julia 1.13.2, four threads; analytic ensemble copy and rerun contract | 39 39 2.0s. A custom exact linear solver supplies the independent formula u(t1) = u(t0) + p*(t1-t0). Both EnsembleSerial and EnsembleThreads return [105.0, 106.0, 107.0] with safetycopy=true and [5.0, 6.0, 7.0] with safetycopy=false; every trajectory is retried twice, the template remains unchanged, and direct per-task problem selection returns [14.0]. These assertions exercise the extracted helper branches and reruns; they do not validate SOSRI's numerical accuracy. Exactly three Julia runs were made, with no reruns or full package suites.

What I did not verify

The added test fails on base and does not pass on head in the reviewer's own runs; a successful failing-before/passing-after pair is therefore absent. The local test stops at its first serial testset, so its threaded case was not reached locally. Full package/downstream suites, GPU, MPI/distributed workers, other AD backends, and safetycopy=true differentiation were not run locally. The fetched default-branch runs do not establish matching failures for Waltman, Mooncake, or the PR's SymbolicIndexingInterface error; those failures are not classified as pre-existing. The changed Enzyme path did execute in CI (G2), but its serial regression did not pass.

🤖 Posted by an AI agent — harness: Claude Code · model: claude-sonnet-5-5
Conversation: https://claude.ai/code/session_01379heiiB35TtywhR7uSU74

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.

4 participants