Skip to content

Commit 1ebab2c

Browse files
Fix OptimizationFunction{iip} to respect explicit iip parameter
Remove isinplace call from OptimizationFunction{iip} constructor when iip is explicitly provided. This matches the behavior of ODEFunction and fixes issue #1103 where functions that fail isinplace checks (like compiled Reactant functions) cannot be used even when the user explicitly specifies the iip parameter. The isinplace check is kept for the non-parameterized OptimizationFunction constructor where it's needed to infer the iip value. Fixes #1103 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1eb35ad commit 1ebab2c

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/scimlfunctions.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4279,7 +4279,6 @@ function OptimizationFunction{iip}(f, adtype::AbstractADType = NoAD();
42794279
lag_hess_colorvec = nothing,
42804280
initialization_data = __has_initialization_data(f) ? f.initialization_data :
42814281
nothing) where {iip}
4282-
isinplace(f, 2; has_two_dispatches = false, isoptimization = true)
42834282
sys = sys_or_symbolcache(sys, syms, paramsyms)
42844283
OptimizationFunction{
42854284
iip, typeof(adtype), typeof(f), typeof(grad), typeof(fg), typeof(hess),

test/function_building_error_messages.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,14 @@ optf(u, p) = 1.0
507507
OptimizationFunction(optf)
508508
OptimizationProblem(optf, 1.0)
509509

510+
# Test OptimizationFunction{iip} respects explicit iip parameter
511+
# This function would fail isinplace check due to method ambiguity
512+
struct ProblematicOptFunction end
513+
(::ProblematicOptFunction)(x, p) = sum(x.^2)
514+
# But when iip is explicitly provided, it should work without calling isinplace
515+
@test_nowarn OptimizationFunction{false}(ProblematicOptFunction())
516+
@test_nowarn OptimizationFunction{true}(ProblematicOptFunction())
517+
510518
# BVPFunction
511519

512520
bfoop(u, p, t) = u

0 commit comments

Comments
 (0)