Skip to content
Merged
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
7 changes: 4 additions & 3 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -213,7 +214,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
Expand All @@ -235,7 +236,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)
Expand Down
37 changes: 37 additions & 0 deletions test/enzyme/problem_adaptation.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/enzyme_environment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/gpu_kernel_de/host_fastpath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading