Skip to content

Commit 828a6cd

Browse files
Merge pull request #1000 from ChrisRackauckas-Claude/fix-sophia-shadow-generation
Fix Sophia optimizer shadow generation for ComponentArrays
2 parents e9dca08 + bbd74a3 commit 828a6cd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/sophia.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ function SciMLBase.__solve(cache::OptimizationCache{
151151

152152
if i % cache.opt.k == 1
153153
hₜ₋₁ = copy(hₜ)
154-
u = randn(uType, length(θ))
154+
u = similar(θ)
155+
randn!(u)
155156
f.hv(hₜ, θ, u, d)
156157
hₜ = βs[2] .* hₜ₋₁ + (1 - βs[2]) .* (u .* hₜ)
157158
end

test/native.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,14 @@ optf1 = OptimizationFunction(loss, AutoSparseForwardDiff())
6161
prob1 = OptimizationProblem(optf1, rand(5), data)
6262
sol1 = solve(prob1, OptimizationOptimisers.Adam(), maxiters = 1000, callback = callback)
6363
@test sol1.objective < l0
64+
65+
# Test Sophia with ComponentArrays + Enzyme (shadow generation fix)
66+
using ComponentArrays
67+
x0_comp = ComponentVector(a = 0.0, b = 0.0)
68+
rosenbrock_comp(x, p = nothing) = (1 - x.a)^2 + 100 * (x.b - x.a^2)^2
69+
70+
optf_sophia = OptimizationFunction(rosenbrock_comp, AutoEnzyme())
71+
prob_sophia = OptimizationProblem(optf_sophia, x0_comp)
72+
res_sophia = solve(prob_sophia, Optimization.Sophia=0.01, k=5), maxiters = 50)
73+
@test res_sophia.objective < rosenbrock_comp(x0_comp) # Test optimization progress
74+
@test res_sophia.retcode == Optimization.SciMLBase.ReturnCode.Success

0 commit comments

Comments
 (0)