-
-
Notifications
You must be signed in to change notification settings - Fork 68
Add literal_getproperty
disptach for VectorOfArray
#478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
For a reproducible stack trace: using ModelingToolkit
function reactionsystem()
t = ModelingToolkit.t_nounits
D = ModelingToolkit.D_nounits
sts = @variables s1(t)=2.0 s1s2(t)=2.0 s2(t)=2.0
ps = @parameters k1=1.0 c1=2.0 [bounds = (0, 2), tunable = true]
eqs = [D(s1) ~ -0.25 * c1 * k1 * s1 * s2
D(s1s2) ~ 0.25 * c1 * k1 * s1 * s2
D(s2) ~ -0.25 * c1 * k1 * s1 * s2]
return mtkcompile(System(eqs, t; name = :reactionsystem))
end
using SciMLSensitivity
using Zygote
using OrdinaryDiffEqTsit5
using SymbolicIndexingInterface
using DifferentiationInterface
using RecursiveArrayTools
sys = reactionsystem()
prob = ODEProblem(sys, [], (0, 1))
ts = range(0, 1, length = 10)
sol = solve(prob, Tsit5(), saveat = ts)
data = Matrix(sol)
get_vars = getu(sys, [sys.s1, sys.s1s2, sys.s2])
set_x = setsym_oop(sys, [sys.c1, sys.k1])
function squaredl2loss(sol::AbstractVectorOfArray, data)
T = eltype(data)
𝟘 = zero(promote_type(eltype(sol), T))
err = 𝟘
@assert size(sol, 1) == size(data, 1)
@inbounds for (s, d) in zip(sol.u, eachcol(data))
for i in eachindex(s, d)
if !ismissing(d[i])
err += (s[i] - d[i])^2
else
err += 𝟘
end
end
end
return err
end
function loss(x, (prob, get_vars, data, ts, set_x))
new_u0, new_p = set_x(prob, x)
new_prob = remake(prob, p = new_p, u0 = new_u0)
new_sol = solve(new_prob, Tsit5(), saveat = ts)
if SciMLBase.successful_retcode(new_sol)
u = VectorOfArray(get_vars(new_sol))
squaredl2loss(u, data)
else
Inf
end
end
ps = (prob, get_vars, data, ts, set_x);
DifferentiationInterface.gradient(x -> loss(x, ps), AutoZygote(), [1.5, 2.0]) gives
|
DhairyaLGandhi
commented
Aug 13, 2025
I don't think there's an issue with narrowing the type for the backwards pass, but it would be good to define a part of the interface that is |
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.
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
Seen in some places surfacing as
cc @SebastianM-C
I am not a fan of adding a
literal_getproperty
dispatch but this can be a stopgap till we remove all similar ones at once.Add any other context about the problem here.