# Type stable code with autodiff

**URL:** <https://discourse.julialang.org/t/type-stable-code-with-autodiff/102242>\
**Category:** General Usage\
**Created:** [July 29, 2023, 2:28pm UTC](https://discourse.julialang.org/t/type-stable-code-with-autodiff/102242 "2023-07-29T14:28:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mesonepigreco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mesonepigreco/32/46262_2.png) [@mesonepigreco](https://discourse.julialang.org/u/mesonepigreco)\
**Post date:** [July 29, 2023, 2:28pm UTC](https://discourse.julialang.org/t/type-stable-code-with-autodiff/102242/1 "2023-07-29T14:28:30Z")

</div>

Dear Julia users

I am writing a quite important julia project where performances are very important, and, to enforce type stability, almost all my functions look like this:

```julia
function get_kinetic_energy(scha::SCHA{T}, ensemble::Ensemble{T}, nltransf::NLTransform{T}; 
        debug::Bool=false)::T where {T}

```

I thought it was a best practice to tell beforehand to the compiler that all the types of the things inside are the same, so no type conversion is required when there are operations between the variables.

However, now I am experimenting with automatic differentiation, but I realized that the type of variables I want to differentiate is the ForwardDiff.Dual, and this prevents the multiple dispatchers to correctly resolving the types (as some types are T (Float64), others are Dual).  
Indeed, if I remove all the type constraints, the autodiff works perfectly, but I fear it would lose performance by not allowing the compiler to fix potential type instabilities.

What are the best practices for writing code that is both automatically differentiable and type-stable?

---

<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:** [July 29, 2023, 3:09pm UTC](https://discourse.julialang.org/t/type-stable-code-with-autodiff/102242/2 "2023-07-29T15:09:08Z")

</div>

> [@mesonepigreco](#):
>
> Indeed, if I remove all the type constraints, the autodiff works perfectly, but I fear it would lose performance by not allowing the compiler to fix potential type instabilities.

This has nothing to do with type stability. Dispatches auto-specialize on types.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 29, 2023, 5:39pm UTC](https://discourse.julialang.org/t/type-stable-code-with-autodiff/102242/3 "2023-07-29T17:39:31Z")

</div>

Type annotations will not cause code to become type stable, they only restrict what input types are allowed. If your code is type stable _with_ annotations, it will also be type stable _without_ them, for the same inputs, at least.

You can use other tools to check for type stability. Traditionally, people have used `@code_warntype` for this, but now there are some newer ones, like Cthulu.jl, I believe.
