# Type stability in Optimization.jl

**URL:** https://discourse.julialang.org/t/type-stability-in-optimization-jl/124522
**Category:** Optimization (Mathematical)
**Created:** [January 8, 2025, 2:41am UTC](https://discourse.julialang.org/t/type-stability-in-optimization-jl/124522 "2025-01-08T02:41:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Addison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/addison/32/214577_2.png) [@Addison](https://discourse.julialang.org/u/Addison)
#### Post date: [January 8, 2025, 2:41am UTC](https://discourse.julialang.org/t/type-stability-in-optimization-jl/124522/1 "2025-01-08T02:41:05Z")

</div>

I’ve got some code that solves several optimization problems using Optimization.jl. When checking for type stability with JET’s `report_opt` function, it outputs all sorts of errors around the call to OptimizationFunction. This can be duplicated with the rosenbrock problem from the tutorial; report\_opt finds 10 errors with this simple test:

```julia
using Optimization, JET

rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2

@report_opt OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())

```

I’ve only been using Julia for a couple of weeks so I’m hoping there’s a simple explanation. Thanks in advance!

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [January 8, 2025, 3:41am UTC](https://discourse.julialang.org/t/type-stability-in-optimization-jl/124522/2 "2025-01-08T03:41:45Z")

</div>

Instantiating the `OptimizationFunction` is a setup step before launching the optimization. Some type instability during setup is usually not a concern as long as the optimization loop itself runs without type instabilities, so the developers of Optimization.jl apparently haven’t prioritized the effort to make this call type stable (assuming it’s even possible).

Keep in mind that what JET calls “errors” here are really just cases where the types of some variables and the corresponding methods had to be looked up at runtime instead of compile time. There’s nothing wrong/incorrect with the code.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 8, 2025, 7:16am UTC](https://discourse.julialang.org/t/type-stability-in-optimization-jl/124522/3 "2025-01-08T07:16:03Z")

</div>

> [@danielwe](#):
>
> so the developers of [Optimization.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/Optimization) apparently haven’t prioritized the effort to make this call type stable (assuming it’s even possible).

You cannot make it type stable with ForwardDiff, since it needs to do the chunk size choice.

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [January 8, 2025, 9:34am UTC](https://discourse.julialang.org/t/type-stability-in-optimization-jl/124522/4 "2025-01-08T09:34:00Z")

</div>

I think it’s a better workflow to run the optimization with e.g. `@btime` from `BenchmarkTools` to see if there is a problem with excessive allocations (which is what type instability produces). If such problems appear, it’s time to investigate with `@code_warntype` or `JET` or similar things.

---

<div class="post-metadata">

### Author: ![Addison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/addison/32/214577_2.png) [@Addison](https://discourse.julialang.org/u/Addison)
#### Post date: [January 8, 2025, 5:32pm UTC](https://discourse.julialang.org/t/type-stability-in-optimization-jl/124522/5 "2025-01-08T17:32:00Z")

</div>

There’s the simple explanation I was hoping for. Thanks!
