JuliaDiff / ReverseDiff.jl

Reverse Mode Automatic Differentiation for Julia
Other
348 stars 57 forks source link

Addition between TrackedReal is ambiguous #179

Closed MrVPlusOne closed 3 years ago

MrVPlusOne commented 3 years ago

Hi, when I was using the Hamiltonian Monte Carlo implementation in Gen.jl, I encountered this MethodError:

ERROR: LoadError: MethodError: (::ReverseDiff.ForwardOptimize{typeof(+)})(::ReverseDiff.TrackedReal{Float64, Float64, Nothing}, ::ReverseDiff.TrackedReal{ReverseDiff.TrackedReal{Float64, Float64, Nothing}, ReverseDiff.TrackedReal{Float64, Float64, Nothing}, Nothing}) is ambiguous. Candidates:
  (self::ReverseDiff.ForwardOptimize{F})(t::ReverseDiff.TrackedReal{V, D, O} where O, x::Real) where {F, V, D} in ReverseDiff at /Users/jiayiwei/.julia/packages/ReverseDiff/60noS/src/macros.jl:121
  (self::ReverseDiff.ForwardOptimize{F})(x::Real, t::ReverseDiff.TrackedReal{V, D, O} where O) where {F, V, D} in ReverseDiff at /Users/jiayiwei/.julia/packages/ReverseDiff/60noS/src/macros.jl:109
Possible fix, define
  (::ReverseDiff.ForwardOptimize{F})(::ReverseDiff.TrackedReal{V, D, O} where O, ::ReverseDiff.TrackedReal{V, D, O} where O) where {F, V, D, V, D}
Stacktrace:
  [1] +(a::ReverseDiff.TrackedReal{Float64, Float64, Nothing}, b::ReverseDiff.TrackedReal{ReverseDiff.TrackedReal{Float64, Float64, Nothing}, ReverseDiff.TrackedReal{Float64, Float64, Nothing}, Nothing})
    @ ReverseDiff ~/.julia/packages/ReverseDiff/60noS/src/derivatives/scalars.jl:9

It looks like it caused by applying ForwardOptimize{typeof(+)} to a TrackedReal of Float64 and a TrackedReal of TrackedReal. I'm not familiar with how TrackedReal works. Does this type of addition make sense? If so, is there a quick way to fix this? Many thanks!

MrVPlusOne commented 3 years ago

Actually, this seems to be caused by using custom Gen probability distributions. Maybe I implemented the custom gradient function improperly, so I will close this issue for now.