Skip to content

Handle Base.tail & friends - #567

Merged
mcabbott merged 3 commits into
JuliaDiff:mainfrom
mcabbott:tail_etc
Jul 19, 2022
Merged

mcabbott merged 3 commits into
JuliaDiff:mainfrom
mcabbott:tail_etc

Conversation

@mcabbott

@mcabbott mcabbott commented Jul 19, 2022 •

Copy link
Copy Markdown
Member

It would be nice if Base.tail(::Tangent) worked. Converting to a Tuple isn't safe... I think for example this frule:

https://github.com/JuliaDiff/ChainRules.jl/blob/main/src/rulesets/Base/indexing.jl#L102-L105

goes wrong if ẋ isa NoTangent, which might be causing this failure:

https://github.com/JuliaDiff/Diffractor.jl/runs/7351650418?check_suite_focus=true#step:6:252

@codecov-commenter

codecov-commenter commented Jul 19, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #567 (3db6428) into main (f2e3ac5) will increase coverage by 0.01%.
The diff coverage is 93.75%.

@@            Coverage Diff             @@
##             main     #567      +/-   ##
==========================================
+ Coverage   93.14%   93.15%   +0.01%     
==========================================
  Files          15       15              
  Lines         875      891      +16     
==========================================
+ Hits          815      830      +15     
- Misses         60       61       +1     
Impacted Files Coverage Δ
src/tangent_types/tangent.jl 86.27% <90.00%> (+0.26%) ⬆️
src/tangent_types/abstract_zero.jl 96.00% <100.00%> (+0.54%) ⬆️
src/tangent_types/thunks.jl 95.79% <100.00%> (+0.10%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f2e3ac5...3db6428. Read the comment docs.

@mzgubic mzgubic left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks sensible, just a few questions really

Comment thread src/tangent_types/tangent.jl Resolved
@test getproperty(Tangent{Tuple{Float64}}(2.0), 1) == 2.0
@test getproperty(Tangent{Tuple{Float64}}(@thunk 2.0^2), 1) == 4.0

tang3 = Tangent{Tuple{Float64, String, Vector{Float64}}}(1.0, NoTangent(), @thunk [3.0] .+ 4)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we test this behaviour explicitly?

julia> tang1 = Tangent{Tuple{Float64}}(1.0)
Tangent{Tuple{Float64}}(1.0,)

julia> Base.tail(tang1)
Tangent{Tuple{}}()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe this should just return NoTangent()?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe, would be consistent with

julia> rand_tangent(())
NoTangent()

Comment thread src/tangent_types/tangent.jl Outdated
Base.first(tangent::Tangent{P,T}) where {P,T<:Union{Tuple,NamedTuple}} = first(backing(canonicalize(tangent)))
Base.last(tangent::Tangent{P,T}) where {P,T<:Union{Tuple,NamedTuple}} = last(backing(canonicalize(tangent)))

Base.tail(tangent::Tangent{P}) where {P<:Tuple} = Tangent{_tailtype(P)}(Base.tail(backing(tangent))...)

@mzgubic mzgubic Jul 19, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this limited to Tuples only, does NamedTuple not work in some way?

It's also not clear to me why tail is not defined on any primal P (as for first and last). I thought it is because the order is chosen when defining the Tangent, i.e.

julia> Tangent{Foo}(;a=1.0, b=2.0, c=3.0)
Tangent{Foo}(a = 1.0, b = 2.0, c = 3.0)

julia> Tangent{Foo}(;a=1.0,c=3.0, b=2.0)
Tangent{Foo}(a = 1.0, c = 3.0, b = 2.0)

which was handled in last by adding canonicalize

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't know what this should do for Foo.

Tangent{Foo}(;a=1.0, b=2.0,) is very different to what this does. And very different to indexing; dx[1] does not produce Tangent{Foo}(;a=1.0). Tangents with wrong-order or wrong-length backing are an aberration, and nothing should deliberately produce them.

I didn't add NamedTuples just because they would need a more elaborate _tailtype.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Now NamedTuples are added.

@mzgubic

mzgubic commented Jul 19, 2022

Copy link
Copy Markdown
Member

Thanks! Formatting and good to go :)

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.

3 participants