Skip to content
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0fd9643
fix: restore Enzyme gradients through kernel ensembles
ChrisRackauckas Sep 8, 2026
48b3007
docs: clarify Enzyme feature coverage and event-time limitation
ChrisRackauckas Sep 8, 2026
b21b4ce
fix: preserve Enzyme gradients for multi-component kernel problems
ChrisRackauckas Sep 8, 2026
871f3a1
Isolate Enzyme tests from OpenCL compiler dependencies
ChrisRackauckas Sep 8, 2026
6bc89ef
Validate CUDA gradients with the structured allocation dependency fix
ChrisRackauckas Sep 8, 2026
2ee6ae0
Restrict Enzyme tests to CUDA and Const-mark scalar kernel args
ChrisRackauckas Sep 8, 2026
9107d75
Pass AbstractFloat kernel scalars as length-1 device arrays
ChrisRackauckas Sep 9, 2026
002a75c
Use 0-d device arrays for Enzyme-safe scalar kernel args
ChrisRackauckas Sep 9, 2026
4a6fdff
Limit KernelODEProblem layout to CUDA backends
ChrisRackauckas Sep 9, 2026
17fa462
Type-stabilize CUDA KernelODEProblem selection for Enzyme
ChrisRackauckas Sep 9, 2026
112d23e
Generate concrete KernelODEProblem adaptation per backend
ChrisRackauckas Sep 9, 2026
487b3cd
Specialize scalar packing helpers to avoid Enzyme Unions
ChrisRackauckas Sep 9, 2026
5545d00
Fix OpenCL/Metal regressions and Enzyme SVector copy reverse
ChrisRackauckas Sep 9, 2026
12d6c69
Isolate Enzyme prep to CUDA; restore master OpenCL path
ChrisRackauckas Sep 9, 2026
b434345
Fix Enzyme host shadows and restore master OpenCL kernel ABI
ChrisRackauckas Sep 9, 2026
9f84a7f
Isolate CUDA AD kernels and fix KernelODEProblem adapt
ChrisRackauckas Sep 9, 2026
a9beb91
Avoid Enzyme StepRangeLen and Val-dispatch ODE kernel launches
ChrisRackauckas Sep 9, 2026
79b6f89
Load AD kernels only via CUDAExt; inactive time-matrix fill
ChrisRackauckas Sep 9, 2026
811a011
Define Enzyme AD kernels in CUDAExt without mutating DiffEqGPU
ChrisRackauckas Sep 9, 2026
4f20097
Bisect OpenCL: restore master lowerlevel and CUDAExt
ChrisRackauckas Sep 9, 2026
0df4408
Remove broken DiffEqGPU._init_time_matrix! Enzyme inactive rule
ChrisRackauckas Sep 9, 2026
844e7bd
Isolate KernelODEProblem to CUDAExt for OpenCL Dual SPIR-V
ChrisRackauckas Sep 9, 2026
973203d
Publicize GPU callback types used by CUDAExt
ChrisRackauckas Sep 9, 2026
2f282d2
Keep Enzyme AD kernels and KernelODEProblem out of OpenCL image
ChrisRackauckas Sep 9, 2026
b64abf2
Use one EnsembleGPUKernel path for all backends
ChrisRackauckas Sep 10, 2026
befaf13
test: skip in-kernel init regression on strict-codegen backends
ChrisRackauckas Sep 11, 2026
c7d48ca
qa: allow JET 0.11-0.12 and fix newly flagged definite-assignment issues
ChrisRackauckas Sep 12, 2026
3a8d2a4
test: register array-level Enzyme copy rules for aggregate records
ChrisRackauckas Sep 12, 2026
92e1fda
test: end continuous-callback bounce problem past the final event
ChrisRackauckas Sep 12, 2026
c866830
fix: load Enzyme transfer rules and preserve host solver behavior
ChrisRackauckas Sep 12, 2026
299b5f8
Merge upstream master into Enzyme gradient branch
ChrisRackauckas Sep 13, 2026
91b0e60
test: specify tighter tolerances for adaptive callback comparisons
ChrisRackauckas Sep 13, 2026
7997a91
test: resolve released dependencies for Enzyme coverage
ChrisRackauckas Sep 13, 2026
2fb459e
test: run CUDA solver checks before the isolated Enzyme suite
ChrisRackauckas Sep 13, 2026
924157a
build: require SimpleNonlinearSolve 2.14.5 for the Float32 trust-regi…
ChrisRackauckas Sep 13, 2026
886c9f9
test: move the isolated Enzyme environment to test/enzyme/Project.toml
ChrisRackauckas Sep 13, 2026
967ed7c
test: move all Enzyme tests into test/enzyme/ in SciMLTesting form
ChrisRackauckas Sep 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Adaptive controller conventions

Preserve the existing `GPUTsit5` PI-controller behavior. Alternative controllers must be explicit opt-in variants, with documentation explaining their step-size stability and accuracy tradeoffs. Do not change the default based on equal-tolerance timing alone: use work–precision comparisons at matched achieved error, include rejected steps, and test beyond simple nonstiff problems such as Lorenz.

# Differentiation validation

Gradient tests must use the package and its loaded extensions without adding differentiation rules in test code. Keep CPU Enzyme coverage alongside CUDA coverage. Exercise all active problem fields when validating transfer rules, including times and captured function values; parameter-only losses cannot detect discarded sensitivities. Keep initialization regressions enabled across backends and fix their owning dependencies rather than gating the tests.
29 changes: 28 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Chris Rackauckas", "SciML"]
version = "3.21.1"

[deps]
EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869"
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
Expand Down Expand Up @@ -32,6 +33,7 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[weakdeps]
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
Expand All @@ -41,6 +43,7 @@ OpenCL = "08131aa3-fb12-5dee-8b74-c09406e224a2"
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"

[extensions]
EnzymeExt = "Enzyme"
AMDGPUExt = ["AMDGPU"]
CUDAExt = ["CUDA"]
JLArraysExt = ["JLArrays"]
Expand All @@ -53,12 +56,16 @@ oneAPIExt = ["oneAPI"]
AMDGPU = "2"
Adapt = "4"
CUDA = "6"
CUDACore = "6.2"
ChainRulesCore = "1"
CommonSolve = "0.2.6"
DifferentiationInterface = "0.7.18"
DiffEqBase = "7.19"
Distributed = "1"
DocStringExtensions = "0.9"
Enzyme = "0.13.201"
EnzymeCore = "0.8.21"
ExplicitImports = "1.15"
ForwardDiff = "1.3.3"
GPUArraysCore = "0.2"
JLArrays = "0.3"
Expand All @@ -77,7 +84,7 @@ SciMLBase = "3.49"
SciMLPublic = "1"
Setfield = "1"
SimpleDiffEq = "1.11"
SimpleNonlinearSolve = "2.14"
SimpleNonlinearSolve = "2.14.5"
StaticArrays = "1.9.14"
StaticArraysCore = "1.4.3"
TOML = "1"
Expand All @@ -90,6 +97,9 @@ oneAPI = "2"
pocl_jll = "7"

[extras]
ExplicitImports = "7d51a73a-1435-4ff3-83d9-f097790105c7"
CUDACore = "bd0ed864-bdfe-4181-a5ed-ce625a5fdea2"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand All @@ -101,3 +111,20 @@ OpenCL = "08131aa3-fb12-5dee-8b74-c09406e224a2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"
pocl_jll = "627d6b7a-bbe6-5189-83e7-98cc0a5aeadd"

[targets]
test = [
"SciMLTesting",
"AMDGPU",
"CUDA",
"DifferentiationInterface",
"GPUArraysCore",
"JLArrays",
"Metal",
"OpenCL",
"Test",
"oneAPI",
"pocl_jll",
]
enzyme_cuda = ["CUDA", "CUDACore", "Enzyme", "ExplicitImports", "KernelAbstractions", "SciMLBase", "StaticArrays", "Test"]
enzyme = ["Enzyme", "ExplicitImports", "JLArrays", "KernelAbstractions", "SciMLBase", "StaticArrays", "Test"]
4 changes: 4 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
DiffEqGPU = "071ae1c0-96b5-11e9-1965-c90190d839ea"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Expand All @@ -30,7 +32,9 @@ CUDA = "5, 6"
DiffEqBase = "7"
DiffEqGPU = "3"
Documenter = "1"
Enzyme = "0.13.201"
ForwardDiff = "1"
KernelAbstractions = "0.9.38"
Lux = "1"
ModelingToolkit = "11"
Optimisers = "0.4"
Expand Down
51 changes: 51 additions & 0 deletions docs/src/examples/ad.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,54 @@ monteprob = EnsembleProblem(prob; prob_func)
saveat = 1.0f0
)
```

## Enzyme reverse mode with `EnsembleGPUKernel`

For `GPUTsit5`, use Enzyme to differentiate a scalar loss of the ensemble's final
states. Set `cpu_offload` to zero and `save_everystep = false`. Keep the parameters
on the host when constructing the problems; the ensemble solver transfers the
compatible problems to the selected backend.

```@example enzyme_kernel
using DiffEqGPU, Enzyme, KernelAbstractions, SciMLBase, StaticArrays

function ensemble_loss(p, backend)
rhs(u, p, t) = p[1] * u
prob = ODEProblem{false}(rhs, SVector(1.0), (0.0, 1.0), SVector(p[1]))
prob_func = (prob, ctx) -> remake(prob; p = SVector(p[ctx.sim_id]))
ensemble = EnsembleProblem(prob; prob_func, safetycopy = false)
sol = solve(
ensemble, GPUTsit5(), EnsembleGPUKernel(backend, 0.0);
trajectories = length(p), adaptive = false, dt = 0.05,
save_everystep = false
)
return sum(s -> sum(s.u[end]), sol.u)
end

p = [0.2, -0.3]
backend = CPU()
dp = zero(p)
Enzyme.autodiff(Reverse, ensemble_loss, Active, Duplicated(p, dp), Const(backend))
@assert dp ≈ exp.(p)
dp
```

For NVIDIA GPUs, load `CUDA` and pass `CUDA.CUDABackend()` instead of `CPU()`.
CUDA gradients with released dependencies remain unverified. The
[upstream GPUCompiler compatibility work](https://github.com/EnzymeAD/Enzyme.jl/pull/3512)
and compatible CUDA allocation rules are required for that configuration.
Loading Enzyme activates DiffEqGPU's transfer rules automatically. Reset the shadow
buffer `dp` to zero before each independent reverse-mode call: Enzyme accumulates
into it. Fixed-step gradients differentiate the numerical steps; adaptive gradients
differentiate the executed solver path and are not a record-and-replay adjoint
with a frozen mesh.

Callbacks and DAE initialization are solver features; their absence from this
example does not imply that they are unsupported. Representative Float64 CPU
checks also validate Enzyme gradients through a fixed-time discrete callback,
nonlinear initialization, the final integration time, and requested `saveat`
times. These checks do not establish differentiation support for every callback
or a complete singular-mass DAE solve. In particular, a continuous callback with
a parameter-dependent event time can produce
[incorrect gradients](https://github.com/SciML/DiffEqGPU.jl/issues/533); event-time
sensitivities need separate validation.
36 changes: 36 additions & 0 deletions ext/EnzymeExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module EnzymeExt

using DiffEqGPU: DiffEqGPU
using Enzyme: Enzyme, EnzymeRules, Const, Duplicated, Reverse

function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfig, func::Const{typeof(DiffEqGPU._kernel_transfer)},
::Type{RT}, backend::Const, x::Duplicated
) where {RT}
y = func.val(backend.val, x.val)
# Zero the elements on the host; device-array wrapper metadata can be inactive.
dy = func.val(backend.val, Enzyme.make_zero(Array(x.val)))
return EnzymeRules.AugmentedReturn(
EnzymeRules.needs_primal(config) ? y : nothing,
EnzymeRules.needs_shadow(config) ? dy : nothing, dy
)
end

_copy_payload!(dest, src) = (copyto!(dest, src); nothing)

function EnzymeRules.reverse(
config::EnzymeRules.RevConfig, ::Const{typeof(DiffEqGPU._kernel_transfer)},
::Type{RT}, dy, backend::Const, x::Duplicated
) where {RT}
# Let Enzyme accumulate every active field, including captured functions and times.
primal = Array(x.val)
dx = Array(x.dval)
Enzyme.autodiff(
Reverse, _copy_payload!, Const,
Duplicated(copy(primal), Array(dy)), Duplicated(primal, dx)
)
copyto!(x.dval, dx)
return (nothing, nothing)
end

end
1 change: 1 addition & 0 deletions src/DiffEqGPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module DiffEqGPU

using DocStringExtensions: DocStringExtensions
using SciMLPublic: @public
using EnzymeCore: within_autodiff
using KernelAbstractions: KernelAbstractions, @Const, @index, @kernel, CPU
import KernelAbstractions: get_backend, allocate
using SciMLBase: SciMLBase, CallbackSet, CheckInit, ContinuousCallback,
Expand Down
43 changes: 16 additions & 27 deletions src/ensemblegpukernel/kernels.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# Shared EnsembleGPUKernel ODE launchers. Non-@Const probs + scalar unpacking so
# Enzyme reverse works; Duals / plain floats both go through _load_kernel_arg.

@inline _load_kernel_arg(x::Number) = x
@inline _load_kernel_arg(x::AbstractArray{<:Any, 0}) = @inbounds x[]
@inline _load_kernel_arg(x::AbstractArray) = x

@kernel function ode_solve_kernel(
@Const(probs), alg, _us, _ts, dt, callback,
tstops, nsteps,
probs, alg, _us, _ts, _dt, callback,
tstops,
saveat, ::Val{save_everystep}
) where {save_everystep}
i = @index(Global, Linear)
dt = _load_kernel_arg(_dt)

# get the actual problem for this thread
prob = @inbounds probs[i]

# get the input/output arrays for this thread
ts = @inbounds view(_ts, :, i)
us = @inbounds view(_us, :, i)

_saveat = get(prob.kwargs, :saveat, nothing)

saveat = _saveat === nothing ? saveat : _saveat

# Check if initialization is needed for DAEs
u0, p_init,
init_success = if SciMLBase.has_initialization_data(prob.f)
gpu_initialization_solve(prob, SimpleTrustRegion(), 1.0e-6, 1.0e-6)
Expand All @@ -25,7 +28,6 @@
end

if init_success
# Use initialized values
integ = init(
alg, prob.f, false, u0, prob.tspan[1], dt, p_init, tstops,
callback, save_everystep, saveat
Expand All @@ -45,7 +47,6 @@
end

integ.step_idx += 1
# FSAL
while integ.t < tspan[2] && integ.retcode != DiffEqBase.ReturnCode.Terminated
saved_in_cb = step!(integ, ts, us)
!saved_in_cb && savevalues!(integ, ts, us)
Expand All @@ -55,12 +56,10 @@
@inbounds ts[2] = integ.t
end
if integ.t > tspan[2] && saveat === nothing
## Interpolate to tf
@inbounds us[end] = integ(tspan[2])
@inbounds ts[end] = tspan[2]
end
else
# Initialization failed — store initial values and bail out
@inbounds us[1] = prob.u0
@inbounds ts[1] = prob.tspan[1]
if saveat === nothing && !save_everystep
Expand All @@ -71,25 +70,23 @@
end

@kernel function ode_asolve_kernel(
@Const(probs), alg, _us, _ts, dt, callback, tstops,
abstol, reltol,
probs, alg, _us, _ts, _dt, callback, tstops,
_abstol, _reltol,
saveat,
::Val{save_everystep}
) where {save_everystep}
i = @index(Global, Linear)
dt = _load_kernel_arg(_dt)
abstol = _load_kernel_arg(_abstol)
reltol = _load_kernel_arg(_reltol)

# get the actual problem for this thread
prob = @inbounds probs[i]
# get the input/output arrays for this thread
ts = @inbounds view(_ts, :, i)
us = @inbounds view(_us, :, i)
# TODO: optimize contiguous view to return a CuDeviceArray

_saveat = get(prob.kwargs, :saveat, nothing)

saveat = _saveat === nothing ? saveat : _saveat

# Check if initialization is needed for DAEs
u0, p_init,
init_success = if SciMLBase.has_initialization_data(prob.f)
gpu_initialization_solve(prob, SimpleTrustRegion(), abstol, reltol)
Expand All @@ -99,15 +96,9 @@ end

if init_success
tspan = prob.tspan
f = prob.f
p = p_init

t = tspan[1]
tf = prob.tspan[2]

integ = init(
alg, prob.f, false, u0, prob.tspan[1], prob.tspan[2], dt,
p,
p_init,
abstol, reltol, DiffEqBase.ODE_DEFAULT_NORM, tstops, callback,
saveat
)
Expand All @@ -130,7 +121,6 @@ end
end

if integ.t > tspan[2] && saveat === nothing
## Interpolate to tf
@inbounds us[end] = integ(tspan[2])
@inbounds ts[end] = tspan[2]
end
Expand All @@ -140,7 +130,6 @@ end
@inbounds ts[2] = integ.t
end
else
# Initialization failed — store initial values and bail out
@inbounds us[1] = prob.u0
@inbounds ts[1] = prob.tspan[1]
if saveat === nothing && !save_everystep
Expand Down
Loading
Loading