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

Construct natural tangents for Number and AbstractArray{<:Number} in forward mode #276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

oxinabox
Copy link
Member

@oxinabox oxinabox commented Mar 5, 2024

Ok.
Basically a lot of the code in ChainRules assumes that if your type is a Number then its tangent is a number of that same type.
A slightly smaller, but still non-zero portion of code assumed the same about

This is an alternative to
#272 and JuliaDiff/ChainRules.jl#787
Since if you always work with natural tangent types for Numbers you don't run into problems like in ability to add them, since that same addition occurs in the primal problem.
And that feels better, mostly.

I think it will also obsolete a lot of hackier things like our special handling for StaticArrays
e.g. what had to be changed in #275
we shouldn't need that rule at all. And probably some of the other rules in extra_rules.jl can go away.

To support Diffractor over ForwardDiff we need both handling of natural tangents for AbstractArray{<Number} (for ForwardDiff.Partials) and handling of natural tangents for Number (for ForwardDiff.Dual, which uses ForwarDiff.Partials)

@oxinabox
Copy link
Member Author

oxinabox commented Mar 7, 2024

This should also be called in
zero_bundle since zero_tangent doesn't do it for AbstractArrays only Arrays

Comment on lines +61 to +66
# TODO: should this use `_construct` ?
# TODO: is this right?
unwrap_tup(x::Tangent{<:Tuple}) = ChainRulesCore.backing(x)
unwrap_tup(x) = x
field_tangents = map(unwrap_tup, structural_tangent)
B(field_tangents...)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Keno can you advise here?

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