Skip to content

Flux.Conv type instability #1178

@ltjkoomen

Description

@ltjkoomen

Hi all,

As I was playing around with Flux I noticed that the convolutional layers I was using (Flux.Conv) have some un-inferable types, at least that is my understanding.

The following is a minimal example:

using Flux
imgbatch = rand(Float32, 50, 50, 3, 64)
convlayer = Flux.Conv((3,3), 3=>32)
@code_warntype convlayer(imgbatch)

On my laptop, with Julia version 1.4.1 and Flux version 0.10.4 this results in:


Variables
  c::Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}}
  x::Array{Float32,4}
  #105::Flux.var"#105#106"
  σ::typeof(identity)
  b::Array{Float32,4}
  cdims::DenseConvDims{2,_A,_B,_C,_D,_E,_F,_G} where _G where _F where _E where _D where _C where _B where _A

Body::Any
1 ─ %1  = Base.getproperty(c, :σ)::Core.Compiler.Const(identity, false)
│   %2  = Base.getproperty(c, :bias)::Array{Float32,1}
│   %3  = Core.tuple(%2)::Tuple{Array{Float32,1}}
│         (#105 = %new(Flux.:(var"#105#106")))
│   %5  = #105::Core.Compiler.Const(Flux.var"#105#106"(), false)
│   %6  = Base.getproperty(c, :stride)::Tuple{Int64,Int64}
│   %7  = Flux.map(%5, %6)::Core.Compiler.Const((1, 1), false)
│   %8  = Core.tuple(Flux.:(:), 1)::Core.Compiler.Const((Colon(), 1), false)
│   %9  = Core._apply_iterate(Base.iterate, Flux.reshape, %3, %7, %8)::Array{Float32,4}
│         (σ = %1)
│         (b = %9)
│   %12 = (:stride, :padding, :dilation)::Core.Compiler.Const((:stride, :padding, :dilation), false)
│   %13 = Core.apply_type(Core.NamedTuple, %12)::Core.Compiler.Const(NamedTuple{(:stride, :padding, :dilation),T} where T<:Tuple, false)
│   %14 = Base.getproperty(c, :stride)::Tuple{Int64,Int64}
│   %15 = Base.getproperty(c, :pad)::NTuple{4,Int64}
│   %16 = Base.getproperty(c, :dilation)::Tuple{Int64,Int64}
│   %17 = Core.tuple(%14, %15, %16)::Tuple{Tuple{Int64,Int64},NTuple{4,Int64},Tuple{Int64,Int64}}
│   %18 = (%13)(%17)::NamedTuple{(:stride, :padding, :dilation),Tuple{Tuple{Int64,Int64},NTuple{4,Int64},Tuple{Int64,Int64}}}
│   %19 = Core.kwfunc(Flux.DenseConvDims)::Core.Compiler.Const(Core.var"#Type##kw"(), false)
│   %20 = Base.getproperty(c, :weight)::Array{Float32,4}
│         (cdims = (%19)(%18, Flux.DenseConvDims, x, %20))
│   %22 = σ::Core.Compiler.Const(identity, false)
│   %23 = Base.getproperty(c, :weight)::Array{Float32,4}
│   %24 = Flux.conv(x, %23, cdims)::AbstractArray{yT,4} where yT
│   %25 = Base.broadcasted(Flux.:+, %24, b)::Any
│   %26 = Base.broadcasted(%22, %25)::Any
│   %27 = Base.materialize(%26)::Any
└──       return %27

It seems to be caused by the un-inferable type of cdims, causing the output type of Flux.conv to be inferred as AbstractArray{yT,4} where yT.

While looking at the type definition and constructors of DenseConvDims I noticed that its parametric type is dependent on the contents of, for instance, the variable corresponding to the pad keyword of the Flux.Conv constructor. It might be a stupid question, but why are all the parameters of the type DenseConvDims stored in its type directly instead of in its fields? Doesn't that make the type uncertain by default since the compiler has no way of knowing the contents of variables beforehand? I have only recently started programming in Julia, so please correct me if I am wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions