JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
887 stars 141 forks source link

ERROR: MethodError: no method matching Float32(::ForwardDiff.Dual{ForwardDiff.Tag{var"#11#12", Float32}, Float32, 12}) #611

Open lazarusA opened 1 year ago

lazarusA commented 1 year ago

I'm running a set of functions, at the moment I don't have a proper MWE (and not sure how exactly get the same error), but I would like to know if this is something familiar to some of you and how I could start debugging the issue.

This is part of the error trace that I can share at the moment:

ERROR: MethodError: no method matching Float32(::ForwardDiff.Dual{ForwardDiff.Tag{var"#11#12", Float32}, Float32, 12})
Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat at rounding.jl:200
  (::Type{T})(::T) where T<:Number at boot.jl:772
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  ...
Stacktrace:
  [1] convert(#unused#::Type{Float32}, x::ForwardDiff.Dual{ForwardDiff.Tag{var"#11#12", Float32}, Float32, 12})
    @ Base ./number.jl:7
  [2] setindex!(A::Vector{Float32}, x::ForwardDiff.Dual{ForwardDiff.Tag{var"#11#12", Float32}, Float32, 12}, i1::Int64)
    @ Base ./array.jl:966
devmotion commented 1 year ago

That error usually occurs (also in your case as the stack trace suggests) if you try to update an array of non-dual numbers with dual numbers. You should ensure that the array that you write to is an array of dual numbers.

lazarusA commented 1 year ago

Thanks, I see. Is there a way to define rules for this kind of matrices. Or, it needs to be done explicitly for each one of them ?

devmotion commented 1 year ago

I'm not sure what you mean by rules here. As in other non-ForwardDiff Julia code, you just have to ensure that the arrays you create can store the results of your computations. E.g., by computing a result first, using map, or using heuristics based on the involved types and operations.