Hi. I am stumped as to why the forward pass is type unstable. Itβs effectively a varying length chain similar to Flux.Chain on an alternating sequence of dense and dropout layers:
struct MLP
dense::Vector{Flux.Dense}
drop::Vector{Flux.Dropout}
end
function MLP(layer_dims::Vector{Int}, dropout::Bool=true, activation=tanh)
dense = Flux.Dense[]
drop = Flux.Dropout[]
for i=1:length(layer_dims)-1
if dropout && i > 1
push!(drop, Flux.Dropout(0.5))
end
if i < length(layer_dims)
push!(dense, Flux.Dense(layer_dims[i]=>layer_dims[i+1], activation))
else
push!(dense, Flux.Dense(layer[i] => layer[i+1]))
end
end
end
function (mlp::MLP)(x::Matrix{Float32}) # Forward pass
temp = x
for i=1:length(mlp.drop)
temp = mlp.dense[i](temp)
temp = mlp.drop[i](temp)
end
mlp.dense[i](temp)
mlp.dense[end](temp)
temp
end
mlp = SR.MLP([2,2,])
x_fake = rand(Float32, 2, 100)
@code_warntype mlp(x_fake)
The lowered rep shows that temp
is unstable:
MethodInstance for ()
Arguments
mlp::MLP
x::Matrix{Float32}
Locals
@_3::Union{Nothing, Tuple{Int64, Int64}}
temp::Any
i::Int64
Body::Any
1 β (temp = x)
β %2 = Base.getproperty(mlp, :drop)::Vector{Flux.Dropout}
β %3 = length(%2)::Int64
β %4 = (1:%3)::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])
β (@_3 = Base.iterate(%4))
β %6 = (@_3 === nothing)::Bool
β %7 = Base.not_int(%6)::Bool
βββ goto #4 if not %7
2 β %9 = @_3::Tuple{Int64, Int64}
β (i = Core.getfield(%9, 1))
β %11 = Core.getfield(%9, 2)::Int64
β %12 = Base.getproperty(mlp, :dense)::Vector{Flux.Dense}
β %13 = Base.getindex(%12, i)::Flux.Dense
β (temp = (%13)(temp))
β %15 = Base.getproperty(mlp, :drop)::Vector{Flux.Dropout}
β %16 = Base.getindex(%15, i)::Flux.Dropout
β (temp = (%16)(temp))
β (@_3 = Base.iterate(%4, %11))
β %19 = (@_3 === nothing)::Bool
β %20 = Base.not_int(%19)::Bool
βββ goto #4 if not %20
3 β goto #2
4 β %23 = Base.getproperty(mlp, :dense)::Vector{Flux.Dense}
β %24 = Base.getindex(%23, i)::Any
β (%24)(temp)
β %26 = Base.getproperty(mlp, :dense)::Vector{Flux.Dense}
β %27 = Base.lastindex(%26)::Int64
β %28 = Base.getindex(%26, %27)::Flux.Dense
β (%28)(temp)
βββ return temp
I even tried evaluating with one Dense layer and the output it still Any. Is there anything I can do about this or is it not a problem?