Repository navigation
Dispatch evaluate_f/evaluate_f!! on iip instead of branching at runtime - #1133
Merged
ChrisRackauckas merged 1 commit intoJul 31, 2026
Merged
Conversation
`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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
NonlinearSolveBase.Utils.evaluate_fandevaluate_f!!picked the in-place vs out-of-place path with a runtime branch:isinplaceis a type parameter so this folds to a constant, but thesimilar(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
SimpleNonlinearSolveusable insidesimplebfgs_run!: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 emitssimilarat all, so the piracy stops being necessary.Note this needs no new handling for
ImmutableNonlinearProblemspecifically — it is already anAbstractNonlinearProblem, 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:
Dispatch selects the intended methods, and the allocating call is gone from the out-of-place body:
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