Skip to content

Dispatch evaluate_f/evaluate_f!! on iip instead of branching at runtime - #1133

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:evaluate-f-oop-dispatch
Jul 31, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:evaluate-f-oop-dispatch

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

Problem

NonlinearSolveBase.Utils.evaluate_f and evaluate_f!! picked the in-place vs out-of-place path with a runtime branch:

function evaluate_f(prob::AbstractNonlinearProblem, u)
    if SciMLBase.isinplace(prob)
        fu = prob.f.resid_prototype === nothing ? similar(u) : similar(prob.f.resid_prototype)
        prob.f(fu, u, prob.p)
        return fu
    else
        return prob.f(u, prob.p)
    end
end

isinplace is a type parameter so this folds to a constant, but the similar(u) allocation is still part of the one method body that gets compiled. For a GPU kernel solver that is not good enough: an out-of-place solve running per-thread needs a method body with no allocation in it, not one where the allocating branch is merely unreachable, and it needs to stay inlineable so an isbits problem can be solved inside the kernel.

The visible consequence is downstream piracy. ParallelParticleSwarms.jl carries this to make SimpleNonlinearSolve usable inside simplebfgs_run!:

@inline NLBUtils.evaluate_f(prob::ImmutableNonlinearProblem, u) = prob.f.f(u, prob.p)
@inline NLBUtils.evaluate_f!!(prob::ImmutableNonlinearProblem, fu, u) = prob.f.f(u, prob.p)

That is method piracy on an internal helper of another package — fragile for them, and a constraint on this package's internals that nobody agreed to.

Change

Split each function into {<:Any, false} / {<:Any, true} methods and mark the out-of-place ones @inline. No behavior change; the out-of-place path just no longer emits similar at all, so the piracy stops being necessary.

Note this needs no new handling for ImmutableNonlinearProblem specifically — it is already an AbstractNonlinearProblem, and (f::NonlinearFunction)(args...) already exists in SciMLBase. The gap was only ever the shape of the branch.

Verification

All five call paths return identical results to the previous implementation:

oop  NonlinearProblem          evaluate_f   = [-3.0, -5.0]
oop  ImmutableNonlinearProblem evaluate_f   = [-3.0, -5.0]
oop  ImmutableNonlinearProblem evaluate_f!! = [-3.0, -5.0]
iip  NonlinearProblem          evaluate_f   = [-3.0, -5.0]
iip  NonlinearProblem          evaluate_f!! = [-3.0, -5.0]

Dispatch selects the intended methods, and the allocating call is gone from the out-of-place body:

oop dispatches to: evaluate_f(prob::AbstractNonlinearProblem{<:Any, false}, u) @ utils.jl:191
iip dispatches to: evaluate_f(prob::AbstractNonlinearProblem{<:Any, true}, u)  @ utils.jl:193

`similar` present in out-of-place method body: false

Not verified here: the GPU-kernel benefit itself. There is no GPU on the machine I ran this on, so the claim that this removes the need for the downstream overloads is reasoned from the emitted code, not measured on device. Worth confirming against ParallelParticleSwarms' CUDA tests before merge.

🤖 Generated with Claude Code

https://claude.ai/code/session_01R1VkEmNsLeQTkYmc9pvEL3

`evaluate_f` and `evaluate_f!!` selected the in-place or out-of-place path with a
runtime `if SciMLBase.isinplace(...)`. `isinplace` is a type parameter, so this
always folded to a constant, but the in-place branch — including its `similar(u)`
allocation — stayed part of the single method body that gets compiled.

That is a problem for GPU kernel solvers. An out-of-place solve running inside a
kernel must compile to a method body with no allocation in it at all, not one
where the allocating branch happens to be unreachable, and it must stay
inlineable so an isbits problem can be solved per-thread. Downstream packages
have been working around this by adding their own `Utils.evaluate_f` methods for
`ImmutableNonlinearProblem` — method piracy on an internal helper, which is a bad
place for anyone to be.

Split each function into `{<:Any, false}` and `{<:Any, true}` methods and mark the
out-of-place ones `@inline`. Behavior is unchanged; the out-of-place path simply
no longer has `similar` anywhere in the code it emits, so no piracy is needed to
make it kernel-safe.

Verified locally that all five call paths return identical results to the previous
implementation (out-of-place `NonlinearProblem`/`ImmutableNonlinearProblem` for
both functions, plus the in-place pair), that dispatch selects the intended
method, and that `similar` no longer appears in the out-of-place `code_typed`.
The GPU-kernel benefit itself is not verified here: no GPU on this machine.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 31, 2026 13:08
@ChrisRackauckas
ChrisRackauckas merged commit cde684b into SciML:master Jul 31, 2026
85 of 95 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants