Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/DiffEqGPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module DiffEqGPU

using DocStringExtensions: DocStringExtensions
using SciMLPublic: @public
using EnzymeCore: within_autodiff
using EnzymeCore: ignore_derivatives, within_autodiff
using FastPower: fastpower
using KernelAbstractions: KernelAbstractions, @Const, @index, @kernel, CPU
import KernelAbstractions: get_backend, allocate
Expand Down
58 changes: 52 additions & 6 deletions src/ensemblegpukernel/integrators/integrator_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ end
integrator.step_idx -= Int(round((integrator.t - t) / integrator.dt))
integrator.t = t
#integrator.dt = integrator.t - integrator.tprev
elseif within_autodiff()
# An event exactly at the step end leaves the values unchanged, but its time may
# carry a different sensitivity than the step end, which must reach the state.
# The difference of equal interpolated values is +0.0, so `u` is unchanged.
integrator.u = integrator.u - (integrator(integrator.t) - integrator(t))
integrator.t = t
end
end
@inline function DiffEqBase.change_t_via_interpolation!(
Expand Down Expand Up @@ -410,11 +416,32 @@ end
end
end

if rootfind == SciMLBase.LeftRootFind
return left
else
return right
root = rootfind == SciMLBase.LeftRootFind ? left : right
return within_autodiff() ? _implicit_root(f, root) : root
end

struct EventTimeTag end

@inline _event_time_derivative(y::ForwardDiff.Dual{EventTimeTag}) = ForwardDiff.partials(y, 1)
@inline _event_time_derivative(y) = zero(y)
@inline _event_value(y::ForwardDiff.Dual{EventTimeTag}) = ForwardDiff.value(y)
@inline _event_value(y) = y

# The bracketing iterations carry no derivative information about the root, so attach
# the implicit-function-theorem sensitivity dτ = -∂f/∂θ / ∂f/∂t at the converged root.
# Both partials come from one dual evaluation at the detached root, which interpolates
# the state even at a step endpoint, so ∂f/∂θ holds the evaluation time fixed instead of
# following a moving endpoint. The correction is identically zero in the primal, so the
# root is unchanged, and its zero numerator makes the slope's own sensitivity drop out.
@inline function _implicit_root(f, root)
t = ignore_derivatives(root)
y = f(ForwardDiff.Dual{EventTimeTag}(t, one(t)))
slope = _event_time_derivative(y)
ft = _event_value(y)
if iszero(slope) || !isfinite(slope) || !isfinite(ft)
error("Cannot differentiate a continuous callback event time: the condition's time derivative at the event is zero or non-finite (tangential or singular crossing).")
end
return t - convert(typeof(t), (ft - ignore_derivatives(ft)) / slope)
end

@inline function DiffEqBase.find_callback_time(
Expand Down Expand Up @@ -459,15 +486,18 @@ end
bottom_sign * top_sign <= 0

event_idx = 1
zero_func(abst, p = nothing) = DiffEqBase.get_condition(integrator, callback, abst)

if !event_occurred
callback_t = integrator.t
residual = zero(bottom_condition)
elseif callback.rootfind == SciMLBase.NoRootFind || iszero(top_sign)
elseif callback.rootfind == SciMLBase.NoRootFind
callback_t = top_t
residual = zero(bottom_condition)
elseif iszero(top_sign)
callback_t = within_autodiff() ? _implicit_root(zero_func, top_t) : top_t
residual = zero(bottom_condition)
else
zero_func(abst, p = nothing) = DiffEqBase.get_condition(integrator, callback, abst)
callback_t = gpu_find_root(zero_func, (bottom_t, top_t), callback.rootfind)
residual = zero_func(callback_t)
end
Expand Down Expand Up @@ -511,3 +541,19 @@ end
end
return callback.condition(tmp, abst, integrator)
end

# Event-time derivatives must include the interpolated state's dependence on time, so
# they always interpolate instead of reusing the stored endpoint states.
@inline function DiffEqBase.get_condition(
integrator::SciMLBase.AbstractODEIntegrator{
AlgType,
IIP,
S, T,
},
callback,
abst::ForwardDiff.Dual{EventTimeTag}
) where {
AlgType <: GPUODEAlgorithm, IIP, S, T,
}
return callback.condition(integrator(abst), abst, integrator)
end
18 changes: 17 additions & 1 deletion src/ensemblegpukernel/integrators/nonstiff/interpolants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,30 @@ end
)
end

@inline _tsit5_bθs(rs, θ) = SimpleDiffEq.bθs(rs, θ)

# `SimpleDiffEq.bθs` requires θ to share the coefficients' element type, which excludes
# the dual times used to differentiate event conditions.
@inline function _tsit5_bθs(rs::SVector{22}, θ::ForwardDiff.Dual{EventTimeTag})
r11, r12, r13, r14, r22, r23, r24, r32, r33, r34, r42, r43, r44, r52, r53,
r54, r62, r63, r64, r72, r73, r74 = rs
z = zero(eltype(rs))
return (
@evalpoly(θ, z, r11, r12, r13, r14), @evalpoly(θ, z, z, r22, r23, r24),
@evalpoly(θ, z, z, r32, r33, r34), @evalpoly(θ, z, z, r42, r43, r44),
@evalpoly(θ, z, z, r52, r53, r54), @evalpoly(θ, z, z, r62, r63, r64),
@evalpoly(θ, z, z, r72, r73, r74),
)
end

@inline @muladd function _ode_interpolant(
Θ, dt, y₀,
integ::T
) where {
T <:
Union{GPUT5I, GPUAT5I},
}
b1θ, b2θ, b3θ, b4θ, b5θ, b6θ, b7θ = SimpleDiffEq.bθs(integ.rs, Θ)
b1θ, b2θ, b3θ, b4θ, b5θ, b6θ, b7θ = _tsit5_bθs(integ.rs, Θ)
return y₀ +
dt *
(
Expand Down
6 changes: 6 additions & 0 deletions src/ensemblegpukernel/kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
if integ.t > tspan[2] && saveat === nothing
@inbounds us[end] = integ(tspan[2])
@inbounds ts[end] = tspan[2]
elseif within_autodiff() && integ.t == tspan[2] && saveat === nothing
# A grid ending exactly at tspan[2] can still carry a different time
# sensitivity (e.g. after an event on a step endpoint). The difference of
# equal interpolated values is +0.0, so the saved state is unchanged.
@inbounds us[end] = integ.u - (integ(integ.t) - integ(tspan[2]))
@inbounds ts[end] = tspan[2]
end
else
@inbounds us[1] = prob.u0
Expand Down
160 changes: 160 additions & 0 deletions test/enzyme/gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,163 @@ end
Enzyme.autodiff(Reverse, initial_state_loss, Active, Duplicated(u0, du0), Const(backend))
@test du0 ≈ fill(exp(0.2), length(u0)) rtol = 1.0e-7
end

linear_event(u, t, integrator) = u[1] - integrator.p[1]
function curved_event(u, t, integrator)
d = u[1] - integrator.p[1]
return d + eltype(u)(1.0e4) * d^3
end
reset_half!(integrator) = (integrator.u = SVector(oftype(integrator.u[1], 0.5)))

function event_time_loss(p::Vector{T}, rhs, condition, u0, backend) where {T}
prob = ODEProblem{false}(rhs, SVector(T(u0)), (zero(T), one(T)), SVector(p[1]))
prob_func = (prob, ctx) -> remake(prob; p = SVector(p[ctx.sim_id]))
cb = ContinuousCallback(condition, reset_half!; save_positions = (false, false))
sol = solve(
EnsembleProblem(prob; prob_func, safetycopy = false), GPUTsit5(),
EnsembleGPUKernel(backend, 0.0); trajectories = length(p), adaptive = false,
dt = T(0.05), callback = cb, merge_callbacks = true, save_everystep = false
)
return sum(s -> only(s.u[end]), sol.u)
end

function event_root(p::T, g, bracket, rootfind) where {T}
return DiffEqGPU.gpu_find_root(t -> g(t, p), T.(bracket), rootfind)
end

@testset "Continuous event root sensitivities ($T, p = $p, $rootfind)" for (T, g, p, bracket) in (
(Float32, (t, p) -> (t - p) + 1.0f4 * (t - p)^3, 0.71f0, (0.7, 0.75)),
(Float64, (t, p) -> (t - p) + 1.0e4 * (t - p)^3, 0.71, (0.7, 0.75)),
(Float64, (t, p) -> (t - p) + (t - p)^3, 1.0e9, (1.0e9 - 1, 1.0e9 + 1)),
(Float32, (t, p) -> expm1(25000.0f0 * (t - p)), 0.71f0, (0.7065, 0.7135)),
), rootfind in (SciMLBase.LeftRootFind, SciMLBase.RightRootFind)
# Each condition crosses zero transversally at t = p, so dt/dp = 1 exactly.
dp, root = Enzyme.autodiff(
ReverseWithPrimal, event_root, Active, Active(p), Const(g), Const(bracket),
Const(rootfind)
)
@test root === event_root(p, g, bracket, rootfind)
@test first(dp) ≈ 1 rtol = 10 * eps(T)
end

@testset "Event root sensitivities reject degenerate crossings" begin
# Zero time derivative at the exact root t = 0, a slope that overflows Float32, and
# a condition whose time derivative cancels. The slopes depend on `p` at run time,
# as kernel conditions always do through the integrator state.
for (p, g, bracket) in (
(0.0, (t, p) -> t^3 - p, (-1.0, 1.0)),
(0.71f0, (t, p) -> ((t - p) * p * 1.0f38) * 10.0f0, (0.7, 0.75)),
(0.5, (t, p) -> p * t - p * t + (one(t) - p), (-1.0, 1.0)),
)
@test_throws r"time derivative at the event is zero or non-finite" Enzyme.autodiff(
Reverse, event_root, Active, Active(p), Const(g), Const(bracket),
Const(SciMLBase.LeftRootFind)
)
end
end

@testset "Parameter-dependent continuous event time gradients ($T)" for T in (Float32, Float64)
rtol = T === Float32 ? 2.0e-5 : 1.0e-8
# u′ = 1 from 0 hits u = p > 0.75 once, at t = p, so u(1) = 1.5 - p.
p = T[0.78, 0.84, 0.93]
rhs = (u, p, t) -> SVector(one(eltype(u)))
for condition in (linear_event, curved_event)
@test event_time_loss(p, rhs, condition, 0, backend) ≈ sum(T(1.5) .- p) rtol = rtol
dp = zero(p)
Enzyme.autodiff(
Reverse, event_time_loss, Active, Duplicated(p, dp),
Const(rhs), Const(condition), Const(0), Const(backend)
)
@test dp ≈ -ones(T, 3) rtol = rtol
end
end

@testset "Nonlinear continuous event time gradients" begin
# u′ = u from 1 hits u = p at t = log(p), so u(1) = 0.5e / p.
p = [1.3, 1.7, 2.2]
rhs = (u, p, t) -> u
@test event_time_loss(p, rhs, linear_event, 1, backend) ≈ sum(0.5 * ℯ ./ p) rtol = 1.0e-7
dp = zero(p)
Enzyme.autodiff(
Reverse, event_time_loss, Active, Duplicated(p, dp),
Const(rhs), Const(linear_event), Const(1), Const(backend)
)
@test dp ≈ -0.5 * ℯ ./ p .^ 2 rtol = 1.0e-6
end

time_event(u, t, integrator) = t - integrator.p[1]
reset_zero!(integrator) = (integrator.u = zero(integrator.u))
double_state!(integrator) = (integrator.u = 2 * integrator.u)

function endpoint_event_loss(p, affect!, backend)
prob = ODEProblem{false}(
(u, p, t) -> SVector(1.0), SVector(0.0), (0.0, 1.0), SVector(p[1])
)
cb = ContinuousCallback(time_event, affect!; save_positions = (false, false))
sol = solve(
EnsembleProblem(prob; safetycopy = false), GPUTsit5(),
EnsembleGPUKernel(backend, 0.0); trajectories = 3, adaptive = false,
dt = 0.25, callback = cb, merge_callbacks = true, save_everystep = false
)
return sum(s -> only(s.u[end]), sol.u) / 3
end

@testset "Event time gradients at step endpoints ($(nameof(affect!)))" for (affect!, sign) in (
(reset_zero!, -1), (double_state!, 1),
)
# u′ = 1 from 0 with the event at t = p: resetting gives u(1) = 1 - p and doubling
# gives u(1) = 2p + (1 - p) = 1 + p. p = 0.5 and 0.75 land exactly on step endpoints.
for p in (0.5, 0.74, 0.75, 0.76)
@test endpoint_event_loss([p], affect!, backend) ≈ 1 + sign * p rtol = 1.0e-12
dp = [0.0]
Enzyme.autodiff(
Reverse, endpoint_event_loss, Active, Duplicated([p], dp),
Const(affect!), Const(backend)
)
@test only(dp) ≈ sign rtol = 1.0e-10
end
end

function event_fields_loss(x, condition, adaptive, backend)
a, b, t0, tf, c, r, dt = x
prob = ODEProblem{false}(
(u, p, t) -> SVector(p[1]), SVector(b), (t0, tf), SVector(a, c, r)
)
affect!(integrator) = (integrator.u = SVector(integrator.p[3]))
cb = ContinuousCallback(condition, affect!; save_positions = (false, false))
sol = solve(
EnsembleProblem(prob; safetycopy = false), GPUTsit5(),
EnsembleGPUKernel(backend, 0.0); trajectories = 3, adaptive, dt,
callback = cb, merge_callbacks = true, save_everystep = false
)
return sum(s -> only(s.u[end]), sol.u) / 3
end

mixed_event(u, t, integrator) = u[1] + 10 * t - integrator.p[2]
state_event(u, t, integrator) = u[1] - integrator.p[2]

@testset "Event time gradients for all problem fields (adaptive = $adaptive)" for adaptive in (false, true)
# x = [a, b, t0, tf, c, r, dt] for u′ = a, u(t0) = b, reset to r, dt = 0.25.
# u + 10t = c fires once at τ = (c - b + a t0) / (a + 10), so L = r + a (tf - τ).
# c = 8.25 puts τ = 0.75 exactly on a step endpoint.
for c in (8.14, 8.25, 8.36)
x = [1.0, 0.0, 0.0, 1.0, c, 2.0, 0.25]
dx = zero(x)
@test event_fields_loss(x, mixed_event, adaptive, backend) ≈ 3 - c / 11 atol = 1.0e-12
Enzyme.autodiff(
Reverse, event_fields_loss, Active, Duplicated(x, dx),
Const(mixed_event), Const(adaptive), Const(backend)
)
@test dx ≈ [1 - 10c / 121, 1 / 11, -1 / 11, 1, -1 / 11, 1, 0] atol = 1.0e-10
end
# u = c fires at τ₁ = t0 + (c - b) / a and, after each reset to r = 0, every c / a,
# giving six events per solve, several within one original step: L = a (tf - τ₆).
x = [1.0, 0.0, 0.0, 1.0, 0.15, 0.0, 0.25]
dx = zero(x)
@test event_fields_loss(x, state_event, adaptive, backend) ≈ 0.1 atol = 1.0e-12
Enzyme.autodiff(
Reverse, event_fields_loss, Active, Duplicated(x, dx),
Const(state_event), Const(adaptive), Const(backend)
)
@test dx ≈ [1, 1, -1, 1, -6, 6, 0] atol = 1.0e-10
end
Loading