Concatenation causes multiplication type problems in ReverseDiff

fratrik
Tue, 20 Dec 2022 21:38:19 +0000
General Usage

using ReverseDiff

function f(a)
    b = [1 2]
    ([a; b]*a)*a
    [a*a*a; b*a*a]

    [a*a; b*a]*a # error here
    0
end

ReverseDiff.hessian(f, randn(2, 2))

the code fails on [a*a; b*a]*a with message

ERROR: LoadError: MethodError: *(::ReverseDiff.TrackedArray{ReverseDiff.TrackedReal{Float64, Float64, Nothing}, ReverseDiff.TrackedReal{Float64, Float64, Nothing}, 2, Matrix{ReverseDiff.TrackedReal{Float64, Float64, Nothing}}, Matrix{ReverseDiff.TrackedReal{Float64, Float64, Nothing}}}, ::ReverseDiff.TrackedArray{ReverseDiff.TrackedReal{Float64, Float64, ReverseDiff.TrackedArray{Float64, Float64, 2, Matrix{Float64}, Matrix{Float64}}}, ReverseDiff.TrackedReal{Float64, Float64, ReverseDiff.TrackedArray{Float64, Float64, 2, Matrix{Float64}, Matrix{Float64}}}, 2, Matrix{ReverseDiff.TrackedReal{Float64, Float64, ReverseDiff.TrackedArray{Float64, Float64, 2, Matrix{Float64}, Matrix{Float64}}}}, Matrix{ReverseDiff.TrackedReal{Float64, Float64, ReverseDiff.TrackedArray{Float64, Float64, 2, Matrix{Float64}, Matrix{Float64}}}}}) is ambiguous.

Something seems to be happening with the type of [a*a; b*a]

ReverseDiff.TrackedArray{ReverseDiff.TrackedReal{Float64, Float64, Nothing}, ReverseDiff.TrackedReal{Float64, Float64, Nothing}, 2, Matrix{ReverseDiff.TrackedReal{Float64, Float64, Nothing}}, Matrix{ReverseDiff.TrackedReal{Float64, Float64, Nothing}}}

Then, [a*a; b*a]::TrackedMatrix{X, D} for D=ReverseDiff.TrackedReal{Float64, Float64, Nothing} which doesn’t match up with a::TrackedMatrix{Y, E} for E= ReverseDiff.TrackedArray{Float64, Float64, 2, Matrix{Float64}, Matrix{Float64}}

so the

LinearAlgebra.:*(x::$(S1){X,D}, y::$(S2){Y,D}) where {X,Y,D} = record_mul(x, y, D)

won’t match. Maybe there needs to be a promotion of some sort so that a::TrackedMatrix{Y, E} can be treated as TrackedMatrix{Y, D} from above?

1 post - 1 participant