Hi. Upon trying to understand how Flux.Chains evaluates inputs, I saw the two following definitions:
@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
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