Skip to content
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

Rosenbrock example: Type instability #1021

Closed
renatobellotti opened this issue Nov 17, 2022 · 5 comments · Fixed by #1026
Closed

Rosenbrock example: Type instability #1021

renatobellotti opened this issue Nov 17, 2022 · 5 comments · Fixed by #1026

Comments

@renatobellotti
Copy link

Hi,

I'm profiling one of my optimisation codes and noticed that a lot of time is spent in typeinf(), so I wondered whether there might be some type instability. Running the Rosenbrock example displays indeed some warnings:

using Optim

rosenbrock(x) =  (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2

@code_warntype optimize(rosenbrock, zeros(2), BFGS())
MethodInstance for Optim.optimize(::typeof(rosenbrock), ::Vector{Float64}, ::BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat})
  from optimize(f, initial_x::AbstractArray, method::Optim.AbstractOptimizer) in Optim at /data/user/bellotti_r/julia_packages/packages/Optim/Zq1jM/src/multivariate/optimize/interface.jl:138
Arguments
  #self#::Core.Const(Optim.optimize)
  f::Core.Const(rosenbrock)
  initial_x::Vector{Float64}
  method::BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}
Locals
  @_5::Optim.Options
Body::Optim.MultivariateOptimizationResults{BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}, Vector{Float64}, Float64, Float64, Vector{OptimizationState{Float64, BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}}}, Bool}
1 ─ %1  = Base.NamedTuple()::Core.Const(NamedTuple())
│   %2  = Optim.default_options(method)::Dict{Symbol, Any}
│   %3  = Base.merge(%1, %2)::NamedTuple
│   %4  = Base.isempty(%3)::Bool
└──       goto #3 if not %4
2 ─       (@_5 = Optim.Options())
└──       goto #4
3 ─ %8  = Core.kwfunc(Optim.Options)::Core.Const(Core.var"#Type##kw"())
└──       (@_5 = (%8)(%3, Optim.Options))
4 ┄ %10 = @_5::Optim.Options
│   %11 = (#self#)(f, initial_x, method, %10)::Optim.MultivariateOptimizationResults{BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}, Vector{Float64}, Float64, Float64, Vector{OptimizationState{Float64, BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}}}, Bool}
└──       return %11

Is this relevant for performance?

@pkofod
Copy link
Member

pkofod commented Nov 23, 2022

Seems to go away if you manually input the options
Uploading image.png…

@renatobellotti
Copy link
Author

I think something went wrong with the image upload...

@pkofod
Copy link
Member

pkofod commented Jan 23, 2023

I think something went wrong with the image upload...

Lol, I see. It was something like optimize(f, x, method, Optim.Options()) so it has to do with the promotion of the tolerances or something like that I'd guess.

@pkofod
Copy link
Member

pkofod commented Jan 23, 2023

image

@pkofod
Copy link
Member

pkofod commented Jan 23, 2023

That wasn't it. Seems to be a splatting of a dict with Any typed keys..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants