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

Handle Base.tail & friends #567

Merged
merged 3 commits into from
Jul 19, 2022
Merged

Handle Base.tail & friends #567

merged 3 commits into from
Jul 19, 2022

Conversation

mcabbott
Copy link
Member

@mcabbott mcabbott commented Jul 19, 2022

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
Copy link

codecov-commenter commented Jul 19, 2022

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.

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.

Looks sensible, just a few questions really

@@ -78,6 +78,13 @@ end
@test getindex(Tangent{Tuple{Float64}}(@thunk 2.0^2), 1) == 4.0
@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
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
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
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()

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))...)
Copy link
Member

@mzgubic mzgubic Jul 19, 2022

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
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
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
Copy link
Member

mzgubic commented Jul 19, 2022

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