Skip to content

Enzyme gradients through parameter-dependent continuous event times are incorrect #533

Open

Description

@ChrisRackauckas-Claude

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-6

Run 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

  1. ChrisRackauckas-Claude commented on Sep 8, 2026

    @ChrisRackauckas-Claude
    MemberAuthor

    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:

    using DiffEqGPU, Enzyme, SciMLBase, Test
    f(p) = DiffEqGPU.gpu_find_root(x -> x - p, (0.7, 0.75), SciMLBase.LeftRootFind)
    p = 0.71
    @test f(p) ≈ p
    observed = only(only(Enzyme.autodiff(Reverse, f, Active, Active(p))))
    @show observed
    @test observed ≈ 1.0

    Command: julia +1.12 --project=<environment developed against clean base> root_gradient_base.jl.

    observed = 0.5
    Test Failed:
      Expression: observed ≈ 1.0
       Evaluated: 0.5 ≈ 1.0
    

    The forward assertion passes. Copying the current gpu_find_root implementation into an environment importing only Enzyme and Test also 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_root implementation 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 is 0.71, but its derivative is 0.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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions