Hi all-
I am experiencing a type instability problem with my code. I was able to narrow down the problem and find a solution, but it doesn’t make sense to me. It seems that iterating over objects rather than indices is the culprit. Below, I posted simplified examples in which @code_warntype
reveals a problematic Union
. I am wondering why this is happening and if it is an inherent limitation of type inference.
using Combinatorics,StatsFuns
#not type stable
#Union{Nothing, Tuple{Tuple{Int64,Array{Bool,1}},Tuple{Int64,Array{Int64,1}}}}
function myFun1(n1,n2)
M = multiset_permutations([fill(true,n1);fill(false,n2)],n1+n2)
LLs = fill(0.0,length(M))
for (i,m) in enumerate(M)
m
end
return logsumexp(LLs)
end
#not type stable
#Union{Nothing, Tuple{Tuple{Int64,Array{Bool,1}},Tuple{Int64,Array{Int64,1}}}}
function myFun2(n1,n2)
M = multiset_permutations([fill(true,n1);fill(false,n2)],n1+n2)
LLs = fill(0.0,length(M))
for m in M
m
end
return logsumexp(LLs)
end
#type stable
function myFun3(n1,n2)
M = collect(multiset_permutations([fill(true,n1);fill(false,n2)],n1+n2))
LLs = fill(0.0,length(M))
for i in 1:length(M)
M[i]
end
return logsumexp(LLs)
end