# Why does Flux.Chain creates Exprs to get type stability?

**URL:** <https://discourse.julialang.org/t/why-does-flux-chain-creates-exprs-to-get-type-stability/98346>\
**Category:** General Usage\
**Created:** [May 4, 2023, 9:48pm UTC](https://discourse.julialang.org/t/why-does-flux-chain-creates-exprs-to-get-type-stability/98346 "2023-05-04T21:48:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_L](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ian_l/32/49509_2.png) [@Ian\_L](https://discourse.julialang.org/u/Ian_L)\
**Post date:** [May 4, 2023, 9:48pm UTC](https://discourse.julialang.org/t/why-does-flux-chain-creates-exprs-to-get-type-stability/98346/1 "2023-05-04T21:48:38Z")

</div>

Hi. Upon trying to understand how Flux.Chains evaluates inputs, I saw the two following definitions:

```julia
@generated function _applychain(layers::Tuple{Vararg{Any,N}}, x) where {N}
  symbols = vcat(:x, [gensym() for _ in 1:N])
  calls = [:($(symbols[i+1]) = layers[$i]($(symbols[i]))) for i in 1:N]
  Expr(:block, calls...)
end

```

and

```julia
function _applychain(layers::AbstractVector, x) # type-unstable path, helps compile times
  for f in layers
    x = f(x)
  end
  x
end

```

The second one _feels_ type unstable but I don’t know a concrete reason why, but I’m at a loss for the first one. Can anyone give insight as to why the first one is better (unrolling?), and why the second specifically is type unstable.

Thanks.

Can be found here: [https://github.com/FluxML/Flux.jl/blob/ccf87bb13f01ff0c0a1a08d900f0a9d8c9122da3/src/layers/basic.jl#L53](https://github.com/FluxML/Flux.jl/blob/ccf87bb13f01ff0c0a1a08d900f0a9d8c9122da3/src/layers/basic.jl#L53)

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 5, 2023, 1:19am UTC](https://discourse.julialang.org/t/why-does-flux-chain-creates-exprs-to-get-type-stability/98346/2 "2023-05-05T01:19:15Z")

</div>

This was added [in this PR](https://github.com/FluxML/Flux.jl/pull/1809), because it was faster (both on the first run, and once all compiled) than the previous version – [times here](https://github.com/FluxML/Flux.jl/pull/1809#issuecomment-1009516145). I think both were type-stable, but this one more Zygote-friendly.

A `Vector` of different layers has to have abstract eltype, which will certainly make that method unstable. This is normally a bad thing, but sometimes it saves a lot of compile time, for small runtime cost.
