Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message for missing chain rule #10

Open
baggepinnen opened this issue Feb 8, 2023 · 2 comments
Open

Error message for missing chain rule #10

baggepinnen opened this issue Feb 8, 2023 · 2 comments

Comments

@baggepinnen
Copy link
Contributor

The following snippet appears to fail due to a missing frule for exp(::Matrix) (there's one for exp!(::Matrix) only)

using ForwardDiff
using ForwardDiffChainRules
@ForwardDiff_frule Base.exp(x1::AbstractMatrix{<:ForwardDiff.Dual})
foo(x) = sum(exp(reshape(x, 2, 2)))
v = randn(4)
ForwardDiff.gradient(foo, v)

The error message is rather cryptic and could maybe be improved :)

ERROR: MethodError: no method matching iterate(::Nothing)
Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen}) at range.jl:872
  iterate(::Union{LinRange, StepRangeLen}, ::Integer) at range.jl:872
  iterate(::T) where T<:Union{Base.KeySet{<:Any, <:Dict}, Base.ValueIterator{<:Dict}} at dict.jl:712
  ...
Stacktrace:
 [1] indexed_iterate(I::Nothing, i::Int64)
   @ Base ./tuple.jl:91
 [2] exp(x1::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4}})
   @ Main ~/.julia/packages/ForwardDiffChainRules/s5si2/src/ForwardDiffChainRules.jl:62
 [3] foo(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4}})
   @ Main ~/Desktop/semi_tmp/diffexp.jl:2
 [4] vector_mode_dual_eval!(f::typeof(foo), cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4}}}, x::Vector{Float64})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/QdStj/src/apiutils.jl:37
 [5] vector_mode_gradient(f::typeof(foo), x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4}}})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/QdStj/src/gradient.jl:106
 [6] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4}}}, ::Val{true})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/QdStj/src/gradient.jl:19
 [7] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 4}}}) (repeats 2 times)
   @ ForwardDiff ~/.julia/packages/ForwardDiff/QdStj/src/gradient.jl:17
 [8] top-level scope
   @ ~/Desktop/semi_tmp/diffexp.jl:4

cc: @mohamed82008

@mohamed82008
Copy link
Collaborator

someone can open a PR to check if the output of frule is nothing, before unpacking it

https://github.com/ThummeTo/ForwardDiffChainRules.jl/blob/master/src/ForwardDiffChainRules.jl#L62

@mohamed82008
Copy link
Collaborator

if it is nothing, throw an informative error saying that no frule is defined for f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants