ForwardDiff.jl icon indicating copy to clipboard operation
ForwardDiff.jl copied to clipboard

ForwardDiff causes many ambiguities which may hurt performance

Open ChrisRackauckas opened this issue 3 years ago • 2 comments

According to @vtjnash in https://github.com/SciML/OrdinaryDiffEq.jl/issues/1750, method ambiguities can hurt performance. It seems that a major source is ForwardDiff, and so it would be good for these to get cleaned up. I found this via:

using OrdinaryDiffEq, Aqua
Aqua.test_all(OrdinaryDiffEq)
50 ambiguities found
Ambiguity #1
StatsBase.TestStat(v) in StatsBase at C:\Users\accou\.julia\packages\StatsBase\XgjIN\src\statmodels.jl:80
(::Type{T})(z::Complex) where T<:Real in Base at complex.jl:44

Possible fix, define
  StatsBase.TestStat(::Complex)

Ambiguity #2
ReverseDiff.TrackedReal{V, D, O}(value) where {V, D, O} in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\tracked.jl:56
(::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50

Possible fix, define
  ReverseDiff.TrackedReal{V, D, O}(::AbstractChar) where {V, D, O}

Ambiguity #3
convert(::Type{ForwardDiff.Dual{T, V, N}}, x) where {T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:432
convert(::Type{T}, x::AbstractChar) where T<:Number in Base at char.jl:185

Possible fix, define
  convert(::Type{ForwardDiff.Dual{T, V, N}}, ::AbstractChar) where {T, V, N}

Ambiguity #4
^(x::Real, y::ForwardDiff.Dual{Ty}) where Ty in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:145
^(::Irrational{:ℯ}, x::Number) in Base.MathConstants at mathconstants.jl:119

Possible fix, define
  ^(::Irrational{:ℯ}, ::ForwardDiff.Dual{Ty}) where Ty

Ambiguity #5
reshape(parent::FillArrays.AbstractFill, dims::Union{Colon, Integer}...) in FillArrays at C:\Users\accou\.julia\packages\FillArrays\MQ1cf\src\FillArrays.jl:233
reshape(parent::AbstractVector, ::Colon) in Base at reshapedarray.jl:115

Possible fix, define
  reshape(::FillArrays.AbstractFill{T, 1} where T, ::Colon)

Ambiguity #6
==(x::Real, y::ForwardDiff.Dual{Ty}) where Ty in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:145
==(x::AbstractIrrational, y::Real) in Base at irrationals.jl:88

Possible fix, define
  ==(::AbstractIrrational, ::ForwardDiff.Dual{Ty}) where Ty

Ambiguity #7
ReverseDiff.TrackedReal{V, D, O}(value) where {V, D, O} in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\tracked.jl:56
(::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:266

Possible fix, define
  ReverseDiff.TrackedReal{V, D, O}(::Base.TwicePrecision) where {V, D, O}

Ambiguity #8
promote_rule(::Type{R}, ::Type{ForwardDiff.Dual{T, V, N}}) where {R<:Real, T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:425
promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational, T<:Number} in Base at irrationals.jl:45

Possible fix, define
  promote_rule(::Type{S}, ::Type{ForwardDiff.Dual{T, V, N}}) where {S<:AbstractIrrational, T, V, N}

Ambiguity #9
promote_rule(::Type{R}, ::Type{ReverseDiff.TrackedReal{V, D, O}}) where {R<:Real, V, D, O} in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\tracked.jl:276
promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational, T<:Number} in Base at irrationals.jl:45

Possible fix, define
  promote_rule(::Type{S}, ::Type{ReverseDiff.TrackedReal{V, D, O}}) where {S<:AbstractIrrational, V, D, O}

Ambiguity #10
Base.Broadcast.BroadcastStyle(::ReverseDiff.TrackedStyle, b::Base.Broadcast.BroadcastStyle) in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\derivatives\broadcast.jl:38
Base.Broadcast.BroadcastStyle(::S, ::Base.Broadcast.Unknown) where S<:Base.Broadcast.BroadcastStyle in Base.Broadcast at broadcast.jl:133

Possible fix, define
  Base.Broadcast.BroadcastStyle(::ReverseDiff.TrackedStyle, ::Base.Broadcast.Unknown)

Ambiguity #11
promote_rule(::Type{R}, ::Type{ReverseDiff.TrackedReal{V, D, O}}) where {R<:Real, V, D, O} in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\tracked.jl:276
promote_rule(::Type{<:AbstractIrrational}, ::Type{T}) where T<:Real in Base at irrationals.jl:44

Possible fix, define
  promote_rule(::Type{R}, ::Type{ReverseDiff.TrackedReal{V, D, O}}) where {V, D, O, R<:AbstractIrrational}

Ambiguity #12
convert(::Type{ForwardDiff.Dual{T, V, N}}, x) where {T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:432
convert(::Type{T}, index::CartesianIndex{1}) where T<:Number in Base.IteratorsMD at multidimensional.jl:130

Possible fix, define
  convert(::Type{ForwardDiff.Dual{T, V, N}}, ::CartesianIndex{1}) where {T, V, N}

Ambiguity #13
(ForwardDiff.Dual{T})(value) where T in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:69
(::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:266

Possible fix, define
  (ForwardDiff.Dual{T})(::Base.TwicePrecision) where T

Ambiguity #14
promote_rule(::Type{R}, ::Type{ForwardDiff.Dual{T, V, N}}) where {R<:Real, T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:425
promote_rule(::Type{<:AbstractIrrational}, ::Type{T}) where T<:Real in Base at irrationals.jl:44

Possible fix, define
  promote_rule(::Type{R}, ::Type{ForwardDiff.Dual{T, V, N}}) where {T, V, N, R<:AbstractIrrational}

Ambiguity #15
^(x::Irrational, y::ForwardDiff.Dual{Ty}) where Ty in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:145
^(::Irrational{:ℯ}, x::Number) in Base.MathConstants at mathconstants.jl:119

Possible fix, define
  ^(::Irrational{:ℯ}, ::ForwardDiff.Dual{Ty}) where Ty

Ambiguity #16
reshape(parent::FillArrays.AbstractFill, dims::Tuple{Vararg{Union{Colon, Integer}}}) in FillArrays at C:\Users\accou\.julia\packages\FillArrays\MQ1cf\src\FillArrays.jl:235
reshape(parent::AbstractVector, ::Tuple{Colon}) in Base at reshapedarray.jl:116

Possible fix, define
  reshape(::FillArrays.AbstractFill{T, 1} where T, ::Tuple{Colon})

Ambiguity #17
^(a::Real, b::ReverseDiff.TrackedReal) in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\derivatives\scalars.jl:17
^(::Irrational{:ℯ}, x::Number) in Base.MathConstants at mathconstants.jl:119

Possible fix, define
  ^(::Irrational{:ℯ}, ::ReverseDiff.TrackedReal)

Ambiguity #18
log(a::Real, b::ReverseDiff.TrackedReal) in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\derivatives\scalars.jl:17
log(::Irrational{:ℯ}, x::Number) in Base.MathConstants at mathconstants.jl:124

Possible fix, define
  log(::Irrational{:ℯ}, ::ReverseDiff.TrackedReal)

Ambiguity #19
ReverseDiff.TrackedReal{V, D, O}(value) where {V, D, O} in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\tracked.jl:56
(::Type{T})(z::Complex) where T<:Real in Base at complex.jl:44

Possible fix, define
  ReverseDiff.TrackedReal{V, D, O}(::Complex) where {V, D, O}

Ambiguity #20
_reshape(parent::RecursiveArrayTools.VectorOfArray, dims::Tuple{Vararg{Int64, N}} where N) in RecursiveArrayTools at C:\Users\accou\.julia\packages\RecursiveArrayTools\IgoeN\src\vector_of_array.jl:262
_reshape(v::AbstractVector, dims::Tuple{Int64}) in Base at reshapedarray.jl:168

Possible fix, define
  _reshape(::RecursiveArrayTools.VectorOfArray{T, 1} where T, ::Tuple{Int64})

Ambiguity #21
==(x::Union{StatsBase.PValue, StatsBase.TestStat}, y::Real) in StatsBase at C:\Users\accou\.julia\packages\StatsBase\XgjIN\src\statmodels.jl:90
==(x::Real, y::AbstractIrrational) in Base at irrationals.jl:89

Possible fix, define
  ==(::Union{StatsBase.PValue, StatsBase.TestStat}, ::AbstractIrrational)

Ambiguity #22
ForwardDiff.Dual(args...) in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:73
(::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:266

Possible fix, define
  ForwardDiff.Dual(::Base.TwicePrecision)

Ambiguity #23
ForwardDiff.Dual{T, V, N}(x) where {T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:77 
(::Type{T})(z::Complex) where T<:Real in Base at complex.jl:44

Possible fix, define
  ForwardDiff.Dual{T, V, N}(::Complex) where {T, V, N}

Ambiguity #24
ForwardDiff.Dual(args...) in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:73
(::Type{T})(x::T) where T<:Number in Core at boot.jl:772

Possible fix, define
  ForwardDiff.Dual(::ForwardDiff.Dual)

Ambiguity #25
log(x::Irrational, y::ForwardDiff.Dual{Ty}) where Ty in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:145
log(::Irrational{:ℯ}, x::Number) in Base.MathConstants at mathconstants.jl:124

Possible fix, define
  log(::Irrational{:ℯ}, ::ForwardDiff.Dual{Ty}) where Ty

Ambiguity #26
copyto!(dest::AbstractArray, A::RecursiveArrayTools.ArrayPartition) in RecursiveArrayTools at C:\Users\accou\.julia\packages\RecursiveArrayTools\IgoeN\src\array_partition.jl:167
copyto!(dest::PermutedDimsArray, src::AbstractArray) in Base.PermutedDimsArrays at permuteddimsarray.jl:215

Possible fix, define
  copyto!(::PermutedDimsArray, ::RecursiveArrayTools.ArrayPartition)

Ambiguity #27
(ForwardDiff.Dual{T, V})(x) where {T, V} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:79
(::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50

Possible fix, define
  (ForwardDiff.Dual{T, V})(::AbstractChar) where {T, V}

Ambiguity #28
StatsBase.TestStat(v) in StatsBase at C:\Users\accou\.julia\packages\StatsBase\XgjIN\src\statmodels.jl:80
(::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50

Possible fix, define
  StatsBase.TestStat(::AbstractChar)

Ambiguity #29
==(a::Real, b::ReverseDiff.TrackedReal) in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\derivatives\scalars.jl:41
==(x::AbstractIrrational, y::Real) in Base at irrationals.jl:88

Possible fix, define
  ==(::AbstractIrrational, ::ReverseDiff.TrackedReal)

Ambiguity #30
ForwardDiff.Dual(args...) in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:73
(::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50

Possible fix, define
  ForwardDiff.Dual(::AbstractChar)

Ambiguity #31
(ForwardDiff.Dual{T, V})(x) where {T, V} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:79     
(::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:266

Possible fix, define
  (ForwardDiff.Dual{T, V})(::Base.TwicePrecision) where {T, V}

Ambiguity #32
sum(x::ReverseDiff.TrackedArray{V, D}, dims) where {V, D} in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\derivatives\linalg\reductions.jl:41
sum(f, a::AbstractArray; dims, kw...) in Base at reducedim.jl:995

Possible fix, define
  sum(::ReverseDiff.TrackedArray{V, D}, ::AbstractArray) where {V, D}

Ambiguity #33
log(x::Real, y::ForwardDiff.Dual{Ty}) where Ty in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:145
log(::Irrational{:ℯ}, x::Number) in Base.MathConstants at mathconstants.jl:124

Possible fix, define
  log(::Irrational{:ℯ}, ::ForwardDiff.Dual{Ty}) where Ty

Ambiguity #34
==(x::ForwardDiff.Dual{Tx}, y::Real) where Tx in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:144
==(x::Real, y::AbstractIrrational) in Base at irrationals.jl:89

Possible fix, define
  ==(::ForwardDiff.Dual{Tx}, ::AbstractIrrational) where Tx

Ambiguity #35
(ForwardDiff.Dual{T})(value) where T in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:69
(::Type{T})(z::Complex) where T<:Real in Base at complex.jl:44

Possible fix, define
  (ForwardDiff.Dual{T})(::Complex) where T

Ambiguity #36
reshape(parent::FillArrays.AbstractFill, dims::Union{Colon, Int64}...) in FillArrays at C:\Users\accou\.julia\packages\FillArrays\MQ1cf\src\FillArrays.jl:232
reshape(parent::AbstractVector, ::Colon) in Base at reshapedarray.jl:115

Possible fix, define
  reshape(::FillArrays.AbstractFill{T, 1} where T, ::Colon)

Ambiguity #37
StatsBase.TestStat(v) in StatsBase at C:\Users\accou\.julia\packages\StatsBase\XgjIN\src\statmodels.jl:80
(::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:266

Possible fix, define
  StatsBase.TestStat(::Base.TwicePrecision)

Ambiguity #38
ReverseDiff.TrackedReal{V, D, O}(value) where {V, D, O} in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\tracked.jl:56
(::Type{T})(x::T) where T<:Number in Core at boot.jl:772

Possible fix, define
  ReverseDiff.TrackedReal{V, D, O}(::ReverseDiff.TrackedReal{V, D, O}) where {V, D, O}

Ambiguity #39
copyto!(dest::AbstractArray, A::RecursiveArrayTools.ArrayPartition) in RecursiveArrayTools at C:\Users\accou\.julia\packages\RecursiveArrayTools\IgoeN\src\array_partition.jl:167
copyto!(dest::PermutedDimsArray{T, N}, src::AbstractArray{T, N}) where {T, N} in Base.PermutedDimsArrays at permuteddimsarray.jl:211

Possible fix, define
  copyto!(::PermutedDimsArray{T, 1}, ::RecursiveArrayTools.ArrayPartition{T}) where T

Ambiguity #40
==(y::Real, x::Union{StatsBase.PValue, StatsBase.TestStat}) in StatsBase at C:\Users\accou\.julia\packages\StatsBase\XgjIN\src\statmodels.jl:91
==(x::AbstractIrrational, y::Real) in Base at irrationals.jl:88

Possible fix, define
  ==(::AbstractIrrational, ::Union{StatsBase.PValue, StatsBase.TestStat})

Ambiguity #41
reshape(parent::FillArrays.AbstractFill, dims::Tuple{Vararg{Union{Colon, Int64}}}) in FillArrays at C:\Users\accou\.julia\packages\FillArrays\MQ1cf\src\FillArrays.jl:237
reshape(parent::AbstractVector, ::Tuple{Colon}) in Base at reshapedarray.jl:116

Possible fix, define
  reshape(::FillArrays.AbstractFill{T, 1} where T, ::Tuple{Colon})

Ambiguity #42
ForwardDiff.Dual{T, V, N}(x::Number) where {T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:78
(::Type{T})(z::Complex) where T<:Real in Base at complex.jl:44

Possible fix, define
  ForwardDiff.Dual{T, V, N}(::Complex) where {T, V, N}

Ambiguity #43
(ForwardDiff.Dual{T, V})(x) where {T, V} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:79
(::Type{T})(z::Complex) where T<:Real in Base at complex.jl:44

Possible fix, define
  (ForwardDiff.Dual{T, V})(::Complex) where {T, V}

Ambiguity #44
convert(::Type{ForwardDiff.Dual{T, V, N}}, x) where {T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:432
convert(::Type{T}, x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:273

Possible fix, define
  convert(::Type{ForwardDiff.Dual{T, V, N}}, ::Base.TwicePrecision) where {T, V, N}

Ambiguity #45
(ForwardDiff.Dual{T})(value) where T in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:69
(::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50

Possible fix, define
  (ForwardDiff.Dual{T})(::AbstractChar) where T

Ambiguity #46
(ForwardDiff.Dual{T, V})(x) where {T, V} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:79     
(::Type{T})(x::T) where T<:Number in Core at boot.jl:772

Possible fix, define
  (ForwardDiff.Dual{T, V})(::ForwardDiff.Dual{T, V}) where {T, V}

Ambiguity #47
ForwardDiff.Dual(args...) in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:73
(::Type{T})(z::Complex) where T<:Real in Base at complex.jl:44

Possible fix, define
  ForwardDiff.Dual(::Complex)

Ambiguity #48
ForwardDiff.Dual{T, V, N}(x) where {T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:77
(::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50

Possible fix, define
  ForwardDiff.Dual{T, V, N}(::AbstractChar) where {T, V, N}

Ambiguity #49
ForwardDiff.Dual{T, V, N}(x) where {T, V, N} in ForwardDiff at C:\Users\accou\.julia\packages\ForwardDiff\pDtsf\src\dual.jl:77 
(::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:266

Possible fix, define
  ForwardDiff.Dual{T, V, N}(::Base.TwicePrecision) where {T, V, N}

Ambiguity #50
==(a::ReverseDiff.TrackedReal, b::Real) in ReverseDiff at C:\Users\accou\.julia\packages\ReverseDiff\5MMPp\src\derivatives\scalars.jl:42
==(x::Real, y::AbstractIrrational) in Base at irrationals.jl:89

Possible fix, define
  ==(::ReverseDiff.TrackedReal, ::AbstractIrrational)

Method ambiguity: Test Failed at C:\Users\accou\.julia\packages\Aqua\HWLbM\src\ambiguities.jl:117
  Expression: success(pipeline(cmd; stdout = stdout, stderr = stderr))
Stacktrace:
 [1] macro expansion
   @ C:\Users\accou\.julia\juliaup\julia-1.8.0+0.x64\share\julia\stdlib\v1.8\Test\src\Test.jl:464 [inlined]
 [2] _test_ambiguities(packages::Vector{Base.PkgId}; color::Nothing, exclude::Vector{Any}, detect_ambiguities_options::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Aqua C:\Users\accou\.julia\packages\Aqua\HWLbM\src\ambiguities.jl:117
Test Summary:    | Fail  Total   Time
Method ambiguity |    1      1  59.1s
ERROR: Some tests did not pass: 0 passed, 1 failed, 0 errored, 0 broken.

ChrisRackauckas avatar Sep 07 '22 05:09 ChrisRackauckas

There seem to be also some performance reserves in #516...

j-fu avatar Sep 18 '22 12:09 j-fu

Is there an outline of how this sort of issue is fixed in general? It seems like writing composable code opens the door wide open for ambiguities. I understand how to fix this for a few ambiguities, but the general problem seems a bit like whack a mole. If a package like ForwardDiff were to start off with the test @test Aqua.test_all(ForwardDiff), I imagine that it would be very difficult, or maybe impossible, to write composable, non-ambiguous, and generic code. I guess what I'm asking is, what does the JuliaLang ecosystem look like if all packages pass @test Aqua.test_all(SomePackage)?

charleskawczynski avatar Sep 21 '22 03:09 charleskawczynski