From 1ba0a7674b10e4cb81801cd7e3d57c70b52a277b Mon Sep 17 00:00:00 2001 From: ChrisRackauckas Date: Sun, 1 Nov 2026 10:30:27 -0100 Subject: [PATCH 1/2] Specialize kernel preparation on RNG callback types Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.164.0 Agent-Model: gpt-6-astra Agent-Session: local job diffeqgpu-benchgap (no session URL) --- src/solve.jl | 4 ++-- test/gpu_kernel_de/host_fastpath.jl | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/solve.jl b/src/solve.jl index 43651591..632fa9f7 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -213,7 +213,7 @@ _shallow_reconstruct_problem(prob) = deepcopy(prob) deepcopy(prob) end -@noinline function _make_kernel_problem(ensembleprob, i, sim_seeds, rng_func, master_rng) +@noinline function _make_kernel_problem(ensembleprob, i, sim_seeds, rng_func::F, master_rng) where {F} ctx = _make_ensemble_context(i, sim_seeds, rng_func, master_rng) prob = ensembleprob.safetycopy ? _safety_copy_ensemble_prob(ensembleprob.prob) : ensembleprob.prob @@ -235,7 +235,7 @@ end end @inline _adapt_kernel_problem(backend, prob) = _kernel_record(adapt(backend, prob)) -function _prepare_kernel_problems(ensembleprob, backend, I, sim_seeds, rng_func, master_rng) +function _prepare_kernel_problems(ensembleprob, backend, I, sim_seeds, rng_func::F, master_rng) where {F} first_prob = _make_kernel_problem(ensembleprob, first(I), sim_seeds, rng_func, master_rng) first_adapted = _adapt_kernel_problem(backend, first_prob) first_ref = _wrap_kernel_host(first_prob) diff --git a/test/gpu_kernel_de/host_fastpath.jl b/test/gpu_kernel_de/host_fastpath.jl index 8fe6b511..10ba558b 100644 --- a/test/gpu_kernel_de/host_fastpath.jl +++ b/test/gpu_kernel_de/host_fastpath.jl @@ -155,3 +155,26 @@ end @test endpoints ≈ exp.([0.1, 0.2, 0.3]) rtol = 1.0e-10 end end + +function kernel_preparation_allocations(::Type{T}, n) where {T} + f(u, p, t) = p[1] * u + prob = ODEProblem{false}(f, SVector{3, T}(1, 0, 0), (zero(T), one(T)), SVector(T(0.2))) + ensemble = EnsembleProblem( + prob; safetycopy = false, + prob_func = (prob, ctx) -> remake(prob; p = SVector(T(ctx.sim_id) / n)) + ) + backend = KernelAbstractions.CPU() + prepare() = DiffEqGPU._prepare_kernel_problems( + ensemble, backend, 1:n, nothing, SciMLBase.default_rng_func, nothing + ) + probs = prepare() + bytes = @allocated prepare() + return bytes, Base.summarysize(probs) +end + +@testset "Kernel preparation temporary allocations" for T in (Float32, Float64) + kernel_preparation_allocations(T, 4096) + bytes, storage = kernel_preparation_allocations(T, 4096) + # The storage multiple permits allocator overhead while bounding temporary allocations. + @test bytes < 4storage +end From 692fe2898d1d56098d303541f868f200a9f411b8 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas Date: Wed, 4 Nov 2026 06:05:40 -0100 Subject: [PATCH 2/2] Preserve keyword sensitivities in private problem records Normalize private record keywords to a NamedTuple while preserving the noinline constructor and numeric-field order. Cover every active problem field and accumulated gradients on CPU, JLArrays and CUDA. Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.164.0 Agent-Model: gpt-6-astra Agent-Session: local job diffeqgpu-benchgap (no session URL) --- src/solve.jl | 3 ++- test/enzyme/problem_adaptation.jl | 37 +++++++++++++++++++++++++++++++ test/enzyme_environment.jl | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/enzyme/problem_adaptation.jl diff --git a/src/solve.jl b/src/solve.jl index 632fa9f7..179cad6c 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -190,7 +190,8 @@ struct KernelODEProblem{U, T, IIP, P, F, K, PT} <: SciMLBase.AbstractODEProblem{ end _kernel_record(prob) = prob @noinline function _kernel_record(prob::SciMLBase.ImmutableODEProblem{U, T, IIP, P, F, K, PT}) where {U, T, IIP, P, F, K, PT} - return KernelODEProblem{U, T, IIP, P, F, K, PT}(prob.p, prob.u0, prob.tspan, prob.f, prob.kwargs, prob.problem_type) + kwargs = NamedTuple(prob.kwargs) + return KernelODEProblem{U, T, IIP, P, F, typeof(kwargs), PT}(prob.p, prob.u0, prob.tspan, prob.f, kwargs, prob.problem_type) end @inline function _ensemble_problem_fields_isbits(prob) diff --git a/test/enzyme/problem_adaptation.jl b/test/enzyme/problem_adaptation.jl new file mode 100644 index 00000000..6644668c --- /dev/null +++ b/test/enzyme/problem_adaptation.jl @@ -0,0 +1,37 @@ +using DiffEqGPU, Enzyme, KernelAbstractions, SciMLBase, StaticArrays, Test + +const adaptation_backends = if get(ENV, "GROUP", "Enzyme") == "CUDA" + using CUDA + (CUDA.CUDABackend(),) +else + using JLArrays + (CPU(), JLArrays.JLBackend()) +end + +function adaptation_field_loss(x::Vector{T}, backend) where {T} + f = let rate = x[5] + (u, p, t) -> rate * u + end + prob = SciMLBase.ImmutableODEProblem{false}( + f, SVector(x[1]), (x[2], x[3]), SVector(x[4]); weight = x[6] + ) + adapted = DiffEqGPU._adapt_kernel_problem(backend, prob) + if Enzyme.within_autodiff() + @assert adapted isa DiffEqGPU.KernelODEProblem + end + return adapted.u0[1]^2 + adapted.tspan[1] * adapted.tspan[2] + + adapted.p[1]^2 + only(adapted.f(SVector(T(3)), adapted.p, zero(T))) + + adapted.kwargs[:weight]^2 +end + +@testset "Problem adaptation preserves active fields ($backend)" for backend in adaptation_backends + for T in (Float32, Float64) + x = T[2, 0.25, 1.25, 0.5, 0.75, 7] + @test adaptation_field_loss(x, backend) == T(55.8125) + dx = ones(T, 6) + for repetitions in 1:2 + Enzyme.autodiff(Reverse, adaptation_field_loss, Active, Duplicated(x, dx), Const(backend)) + @test dx == ones(T, 6) + repetitions * T[4, 1.25, 0.25, 1, 3, 14] + end + end +end diff --git a/test/enzyme_environment.jl b/test/enzyme_environment.jl index 90eb6140..c07bfa66 100644 --- a/test/enzyme_environment.jl +++ b/test/enzyme_environment.jl @@ -7,6 +7,7 @@ let using SciMLTesting SciMLTesting.activate_group_env($(repr(envdir))) include($(repr(joinpath(envdir, "imports.jl")))) + include($(repr(joinpath(envdir, "problem_adaptation.jl")))) if $cuda include($(repr(joinpath(envdir, "cuda_records.jl")))) end