Repository navigation
Description
A continuous callback locates the event correctly in the forward solve, but Enzyme reverse mode returns incorrect event-time sensitivities. For u′=1, u(0)=0, and a reset to zero when u=p, with 0.5<p<1, exactly one event occurs before t=1. Thus u(1)=1-p and every parameter derivative must be -1.
Reproduced on Julia 1.12.7, Enzyme 0.13.201, KernelAbstractions 0.9.42, and SciMLBase 3.53.1, using the CPU backend and the Enzyme restoration branch from #532. This is a remaining limitation of that restoration, not a claim that its changes introduced the event-time problem. The unmodified base cannot execute the ensemble-gradient reproducer because it fails earlier in Enzyme compilation.
using DiffEqGPU, Enzyme, KernelAbstractions, SciMLBase, StaticArrays, Test
function continuous_loss(p)
prob = ODEProblem{false}((u, p, t) -> SVector(1.0), SVector(0.0), (0.0, 1.0), SVector(p[1]))
pf = (prob, ctx) -> remake(prob; p = SVector(p[ctx.sim_id]))
condition(u, t, integrator) = u[1] - integrator.p[1]
affect!(integrator) = (integrator.u = zero(integrator.u))
cb = ContinuousCallback(condition, affect!; save_positions = (false, false))
sol = solve(EnsembleProblem(prob; prob_func = pf, safetycopy = false), GPUTsit5(), EnsembleGPUKernel(CPU(), 0.0); trajectories = length(p), adaptive = false, dt = 0.05, callback = cb, merge_callbacks = true, save_everystep = false)
return sum(s -> sum(s.u[end]), sol.u)
end
p = [0.63, 0.71, 0.84]
@test continuous_loss(p) ≈ 3 - sum(p)
dp = zero(p)
Enzyme.autodiff(Reverse, continuous_loss, Active, Duplicated(p, dp))
@show dp
@test dp ≈ -ones(3) rtol = 1.0e-6Run with julia +1.12 --project=<environment developed against the Enzyme branch> continuous_event_reproducer.jl.
The forward-value assertion passes. The derivative assertion fails:
dp = [-1.0000000000000027, -0.4999999999999896, -0.7599918704695224]
Test Failed:
Expression: dp ≈ -ones(3) rtol = 1e-6
Evaluated: [-1.0000000000000027, -0.4999999999999896, -0.7599918704695224] ≈ [-1.0, -1.0, -1.0] (rtol=1.0e-6)
The same probe with a fixed-time discrete callback (doubling the exponential solution at t=0.5, using tstops=[0.5] and merge_callbacks=true) passes its analytic gradient check. This distinction should be reflected in documentation: callback execution is supported, but that does not establish correct sensitivities through parameter-dependent event times. Root localization and the differentiated event update need investigation; no cause has been bisected, and GPU behavior has not been verified.
🤖 Investigated with Codex CLI 0.153.4 (model: gpt-6-astra; local session ID: 01a07fcc-1c4f-7ee3-9a1e-51eaab7df293).
Activity
ChrisRackauckas-Claude commentedon Sep 8, 2026
The root-localization failure also reproduces on the clean, unmodified base f1d1f81, independently of the ensemble-gradient compilation changes. I ran this with Julia 1.12.7 and Enzyme 0.13.201:
Command:
julia +1.12 --project=<environment developed against clean base> root_gradient_base.jl.The forward assertion passes. Copying the current
gpu_find_rootimplementation into an environment importing onlyEnzymeandTestalso reproduces incorrect derivatives: parameters(0.63, 0.71, 0.84)give(1.0, 0.5, 0.75)although each forward root equals its parameter. This reduces the issue to differentiating the root-finding iterations, without an ODE solver or SciML runtime dependency.Historical check: ec72622 introduced the local
gpu_find_rootimplementation using bisection. I extracted that exact function into the same Enzyme-only environment and reran the fixed-bracket scalar probe above (replacing the enum comparison with:left). Its forward result is0.71, but its derivative is0.0. Thus the wrong derivative is not newly introduced by the handwritten ITP replacement in 89d3404. There is no verified passing historical baseline, so this is not an introducing-commit bisect result.Differentiating bisection/ITP execution follows the selected floating-point branches and does not necessarily recover the derivative of the mathematical root. A sound fix should investigate implicit differentiation of root localization, including captured callback parameters and state, while preserving the forward root-side semantics. Changing the forward controller or loosening assertions would not address this. No production fix is included in this investigation; full callback sensitivities would still require end-to-end verification after a root rule is implemented.
🤖 Investigated with Codex CLI 0.153.4 (model: gpt-6-astra; local session ID: 01a07fcc-1c4f-7ee3-9a1e-51eaab7df293).