Skip to content

Commit aced3e8

Browse files
fix: add rrule for getproperty(::NonlinearProblem to avoid mutable accumulation
1 parent 39d3c8a commit aced3e8

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

ext/SciMLBaseChainRulesCoreExt.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,17 @@ function ChainRulesCore.rrule(
137137
SciMLBase.IntervalNonlinearProblem(args...; kwargs...), IntervalNonlinearProblemAdjoint
138138
end
139139

140+
function ChainRulesCore.rrule(::ChainRulesCore.RuleConfig{>:ChainRulesCore.HasReverseMode}, ::typeof(Base.getproperty), x::NonlinearProblem, f::Symbol)
141+
val = getfield(x, f)
142+
function back(der)
143+
dx = if der === nothing
144+
ChainRulesCore.zero_tangent(x)
145+
else
146+
NamedTuple{(f,)}((der,))
147+
end
148+
return (ChainRulesCore.NoTangent(), ChainRulesCore.ProjectTo(x)(dx), ChainRulesCore.NoTangent())
149+
end
150+
return val, back
151+
end
152+
140153
end

0 commit comments

Comments
 (0)