Skip to content

Adding Mooncake to the AD list. #936

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ LoggingExtras = "0.4, 1"
Lux = "1.12.4"
MLUtils = "0.4"
ModelingToolkit = "9"
Mooncake = "0.4.138"
Optim = ">= 1.4.1"
OptimizationBase = "2"
OptimizationMOI = "0.5"
Expand Down Expand Up @@ -99,9 +100,10 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"

[targets]
test = ["Aqua", "BenchmarkTools", "Boltz", "ComponentArrays", "DiffEqFlux", "Enzyme", "FiniteDiff", "Flux", "ForwardDiff",
"Ipopt", "IterTools", "Lux", "MLUtils", "ModelingToolkit", "Optim", "OptimizationMOI", "OptimizationOptimJL", "OptimizationOptimisers",
"OrdinaryDiffEqTsit5", "Pkg", "Random", "ReverseDiff", "SafeTestsets", "SciMLSensitivity", "SparseArrays", "SparseDiffTools",
"Symbolics", "Test", "Tracker", "Zygote"]
"Symbolics", "Test", "Tracker", "Zygote", "Mooncake"]
2 changes: 2 additions & 0 deletions docs/src/API/ad.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The choices for the auto-AD fill-ins with quick descriptions are:
- `AutoFiniteDiff()`: Finite differencing, not optimal but always applicable
- `AutoModelingToolkit()`: The fastest choice for large scalar optimizations
- `AutoEnzyme()`: Highly performant AD choice for type stable and optimized code
- `AutoMooncake()`: Like Zygote and ReverseDiff, but supports GPU and mutating code

## Automatic Differentiation Choice API

Expand All @@ -22,4 +23,5 @@ OptimizationBase.AutoZygote
OptimizationBase.AutoTracker
OptimizationBase.AutoModelingToolkit
OptimizationBase.AutoEnzyme
OptimizationBase.AutoMooncake
```
19 changes: 11 additions & 8 deletions test/ADtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Optimization, OptimizationOptimJL, OptimizationMOI, Ipopt, Test
using ForwardDiff, Zygote, ReverseDiff, FiniteDiff, Tracker
using ForwardDiff, Zygote, ReverseDiff, FiniteDiff, Tracker, Mooncake
using Enzyme, Random

x0 = zeros(2)
Expand Down Expand Up @@ -35,7 +35,7 @@ end
@testset "No constraint" begin
for adtype in [AutoEnzyme(), AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
AutoFiniteDiff(), AutoModelingToolkit(), AutoSparseForwardDiff(),
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true)]
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true), AutoMooncake()]
optf = OptimizationFunction(rosenbrock, adtype)

prob = OptimizationProblem(optf, x0)
Expand All @@ -46,10 +46,13 @@ end
@test sol.retcode == ReturnCode.Success
end

sol = solve(prob, Optim.Newton())
@test 10 * sol.objective < l1
if adtype != AutoFiniteDiff()
@test sol.retcode == ReturnCode.Success
# `Newton` requires Hession, which Mooncake doesn't support at the moment.
if adtype != AutoMooncake()
sol = solve(prob, Optim.Newton())
@test 10 * sol.objective < l1
if adtype != AutoFiniteDiff()
@test sol.retcode == ReturnCode.Success
end
end

sol = solve(prob, Optim.KrylovTrustRegion())
Expand All @@ -67,7 +70,7 @@ end
@testset "One constraint" begin
for adtype in [AutoEnzyme(), AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
AutoFiniteDiff(), AutoModelingToolkit(), AutoSparseForwardDiff(),
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true)]
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true), AutoMooncake()]
cons = (res, x, p) -> (res[1] = x[1]^2 + x[2]^2 - 1.0; return nothing)
optf = OptimizationFunction(rosenbrock, adtype, cons = cons)

Expand All @@ -85,7 +88,7 @@ end
@testset "Two constraints" begin
for adtype in [AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
AutoFiniteDiff(), AutoModelingToolkit(), AutoSparseForwardDiff(),
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true)]
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true), AutoMooncake()]
function con2_c(res, x, p)
res[1] = x[1]^2 + x[2]^2
res[2] = x[2] * sin(x[1]) - x[1]
Expand Down
Loading