From 3378ffd8a412159a5693462b5c8d4a57ee485f10 Mon Sep 17 00:00:00 2001 From: aris <116081909+arismavridis@users.noreply.github.com> Date: Sat, 24 Aug 2024 14:25:48 +0100 Subject: [PATCH 1/3] Added precompilation for nonlinear least squares Please refer to discourse post: https://discourse.julialang.org/t/excessively-long-ttfx-with-optimization-and-optimizationoptimjl/118457/7 --- .../src/OptimizationOptimJL.jl | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl b/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl index c1727929e..23f1278df 100644 --- a/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl +++ b/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl @@ -453,4 +453,34 @@ function SciMLBase.__solve(cache::OptimizationCache{ stats = stats) end + +PrecompileTools.@compile_workload begin + + function obj_f(x, p) + A = p[1] + b = p[2] + return sum((A * x - b) .^ 2) + end + + function solve_nonnegative_least_squares(A, b, solver) + + optf = Optimization.OptimizationFunction(obj_f, Optimization.AutoForwardDiff()) + prob = Optimization.OptimizationProblem(optf, ones(size(A, 2)), (A, b), lb=zeros(size(A, 2)), ub=Inf * ones(size(A, 2))) + x = OptimizationOptimJL.solve(prob, solver, maxiters=5000, maxtime=100) + + return x + end + + solver_list = [OptimizationOptimJL.LBFGS(), + OptimizationOptimJL.ConjugateGradient(), + OptimizationOptimJL.GradientDescent(), + OptimizationOptimJL.BFGS()] + + for solver in solver_list + x = solve_nonnegative_least_squares(rand(4, 4), rand(4), solver) + x = solve_nonnegative_least_squares(rand(35, 35), rand(35), solver) + x = solve_nonnegative_least_squares(rand(35, 10), rand(35), solver) + end +end + end From 899daa6931ea1c90bee6bc3d922c8859c4a2883c Mon Sep 17 00:00:00 2001 From: aris <116081909+arismavridis@users.noreply.github.com> Date: Sat, 24 Aug 2024 22:10:43 +0100 Subject: [PATCH 2/3] Update Project.toml --- lib/OptimizationOptimJL/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/OptimizationOptimJL/Project.toml b/lib/OptimizationOptimJL/Project.toml index db72cb860..4a53c0d47 100644 --- a/lib/OptimizationOptimJL/Project.toml +++ b/lib/OptimizationOptimJL/Project.toml @@ -8,6 +8,7 @@ Optim = "429524aa-4258-5aef-a3af-852621145aeb" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" [compat] Optim = "1" From 38471df8d89d10dd7744a3d36431a10ec5fccc21 Mon Sep 17 00:00:00 2001 From: aris <116081909+arismavridis@users.noreply.github.com> Date: Sat, 24 Aug 2024 22:12:19 +0100 Subject: [PATCH 3/3] added using PrecompileTools --- lib/OptimizationOptimJL/src/OptimizationOptimJL.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl b/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl index 23f1278df..4a6a6d24e 100644 --- a/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl +++ b/lib/OptimizationOptimJL/src/OptimizationOptimJL.jl @@ -453,7 +453,7 @@ function SciMLBase.__solve(cache::OptimizationCache{ stats = stats) end - +using PrecompileTools PrecompileTools.@compile_workload begin function obj_f(x, p)