Skip to content

Simplify frule for Base.tail #650

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

Merged
merged 2 commits into from
Jul 19, 2022
Merged

Simplify frule for Base.tail #650

merged 2 commits into from
Jul 19, 2022

Conversation

mcabbott
Copy link
Member

Correction to #643 to avoid this:

julia> frule((NoTangent(), NoTangent()), Base.tail, (3,4))
ERROR: MethodError: no method matching length(::NoTangent)

Closest candidates are:
  length(::Union{Base.KeySet, Base.ValueIterator})
   @ Base abstractdict.jl:58
  length(::Union{NNlib.BatchedAdjoint{T, S}, NNlib.BatchedTranspose{T, S}} where {T, S})
   @ NNlib ~/.julia/packages/NNlib/uDZz6/src/batched/batchedadjtrans.jl:58
  length(::Union{DataStructures.OrderedRobinDict, DataStructures.RobinDict})
   @ DataStructures ~/.julia/packages/DataStructures/59MD0/src/ordered_robin_dict.jl:86
  ...

Stacktrace:
 [1] _similar_shape(itr::NoTangent, #unused#::Base.HasLength)
   @ Base ./array.jl:660
 [2] _collect(cont::UnitRange{Int64}, itr::NoTangent, #unused#::Base.HasEltype, isz::Base.HasLength)
   @ Base ./array.jl:715
 [3] collect(itr::NoTangent)
   @ Base ./array.jl:709
 [4] _totuple(::Type{Tuple}, ::NoTangent)
   @ Base ./tuple.jl:401
 [5] Tuple(itr::NoTangent)
   @ Base ./tuple.jl:369
 [6] frule(::Tuple{NoTangent, NoTangent}, #unused#::typeof(Base.tail), x::Tuple{Int64, Int64})
   @ ChainRules ~/.julia/packages/ChainRules/BbzFc/src/rulesets/Base/indexing.jl:104

which shows up in Diffractor's tests, e.g. here:
https://github.com/JuliaDiff/Diffractor.jl/runs/7414875951?check_suite_focus=true#step:6:259

Relies on JuliaDiff/ChainRulesCore.jl#567

@mcabbott
Copy link
Member Author

BTW, why doesn't frule(::Tuple{NoTangent, NoTangent}, f, x) just return f(x), NoTangent() always? E.g. by adding dispatch at https://github.com/JuliaDiff/ChainRulesCore.jl/blob/main/src/rules.jl#L64

Copy link
Member

@mzgubic mzgubic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, why doesn't frule(::Tuple{NoTangent, NoTangent}, f, x) just return f(x), NoTangent() always? E.g. by adding dispatch at https://github.com/JuliaDiff/ChainRulesCore.jl/blob/main/src/rules.jl#L64

Didn't think about this very deeply but it does seem fine. In fairness frules have not been used too much in the wild I think, so may just be a few low hanging fruit lying around. Could it work for Tuple{Vararg{NoTangent}} as well?

@mcabbott mcabbott merged commit 9adf759 into main Jul 19, 2022
@mcabbott mcabbott deleted the mcabbott-patch-2 branch July 19, 2022 19:22
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

Successfully merging this pull request may close these issues.

2 participants