Ambiguity error on vcat()

I have this error

ERROR: MethodError: vcat(::Array{Real,2}, ::TrackedArray{…,Array{Float32,2}}) is ambiguous. Candidates:
  vcat(364::AbstractArray, x::Union{TrackedArray, TrackedReal}, xs::Union{Number, AbstractArray}...) in Tracker at C:\Users\Henri\.julia\packages\Tracker\6wcYJ\src\lib\array.jl:167
  vcat(A::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T...) in Base at abstractarray.jl:1296
Possible fix, define
  vcat(::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T, ::Union{TrackedArray{T,1,A} where A<:AbstractArray{T,1} where T, TrackedArray{T,2,A} where A<:AbstractArray{T,2} where T}, ::Vararg{Union{AbstractArray{T,2}, AbstractArray{T,1}} where T,N} where N)

Telling me that two vcat() functions are ambiguous. I want to use the Base.vcat() function but using it explicitly throws the same error. Why is that ? And what is this “possible fix” proposed by the error throw?

Here is a code that reproduces the error:

using Flux

function loss(a, b, net, net2)
    net2(vcat(net(a),a))
    
end

function test()    
    opt = ADAM()
    net = Chain(Dense(3,3))
    net2 = Chain(Dense(6,1))
    L(a, b) = loss(a, b, net, net2)

    data = tuple(rand(3,1), rand(3,1))
    xs = Flux.params(net)
    gs = Tracker.gradient(() -> L(data...), xs)
    Tracker.update!(opt, xs, gs)
end

Moreover, when I call manually each line inside the function in the REPL no error is thrown. I do not understand this behavior. This only happens when vcat() is in a function called inside another function. Like in my example above.

I can replicate this. The fix is suggested in the error message (define a method for the intersection). Check if there is an issue for Flux about this, and if not, open one. Or ideally, you could make a PR fixing this.

Already reported to Flux (by OP?) but seems it was closed: https://github.com/FluxML/Flux.jl/issues/763

Yes, I closed because I thought this was not related to Flux. You think I should re-open ?

Hi, @Tamas_Papp

I’d gladly make a PR to fix this but here are three things:

  1. The error can be corrected by calling net2(vcat(net(a), Float32.(a)))
  2. The error message I show is from my actual project, where instead of an Array{Float64,2} (as in my example) it’s an Array{Real,2}. That, I think would have to be corrected using another new definition.
  3. I don’t really know what to put in the body of the new definition of vcat(). I’m kind of a beginner.

(Still, the fact that the error happens when called inside a function but not when lines are sequentially sent to the REPL is weird and) I’m not even sure that’s specific to TrackedArray, thus not specific to Flux. This may happen with other packages proposing AbstractArrays.

I get the same error when sending the lines in test() to the REPL directly.

@Tamas_Papp Oh… yes. I guess I must have been doing it slightly different in the REPL to not get the error. My bad, not very rigorous of me. Still, the rest of my message stays relevant.

So does mine about opening an issue. You may want to just reopen the one above, edit and remove the red herring about the REPL, and then it can be fixed in Flux.

Okay sure, it’s done. Thank you for your help.