Julia: type stability using the base.ntuples function

223 views Asked by At

I recently started using Julia, and am at the moment struggling with some aspects of typing. I am trying to define a type TensorTrain (https://www.researchgate.net/profile/Ivan_Oseledets2/publication/220412263_Tensor-Train_Decomposition/links/5bbfb5c5299bf1004c5a56e3/Tensor-Train-Decomposition.pdf), but I cannot get one of the methods I define to be stable:

abstract type AbstractTensorTrain <: Any end

struct TensorTrain{T<:AbstractFloat} <: AbstractTensorTrain
    cores::Vector{Array{T,3}}
end

Base.size(t::TensorTrain, d::Int) = size(t.cores[d],2)
Base.size(t::TensorTrain) = ntuple(d->size(t,d),length(t.cores))

However if I run:

@code_warntype TensorTrain([rand(2,3,4)]);

I get:

Variables
  #self#::Core.Compiler.Const(size, false)
  t::TensorTrain{Float64}
  #19::var"#19#20"{TensorTrain{Float64}}
  d::Int64

Body::Tuple{Vararg{Int64,N} where N}
1 ─ %1 = Base.getproperty(t, :cores)::Array{Array{Float64,3},1}
│        (d = Main.length(%1))
│   %3 = Main.:(var"#19#20")::Core.Compiler.Const(var"#19#20", false)
│   %4 = Core.typeof(t)::Core.Compiler.Const(TensorTrain{Float64}, false)
│   %5 = Core.apply_type(%3, %4)::Core.Compiler.Const(var"#19#20"{TensorTrain{Float64}}, false)
│        (#19 = %new(%5, t))
│   %7 = #19::var"#19#20"{TensorTrain{Float64}}
│   %8 = Main.ntuple(%7, d)::Tuple{Vararg{Int64,N} where N}
└──      return %8

Why is this?

1

There are 1 answers

0
phipsgabler On BEST ANSWER

Because d = length(t.cores) is only known at runtime, so there's no way ntuple could statically infer the N parameter. There is a method that takes a Val, which is useful for type stability in other cases, but it does not change anything here -- the d is still dynamic.

You could make cores an NTuple or StaticVector, though (which are really the same things), then this will work. But do check whether it really makes a difference, before delving into type-level programming.