-
Notifications
You must be signed in to change notification settings - Fork 31
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
Support frules with keyword arguments #266
Open
oxinabox
wants to merge
1
commit into
main
Choose a base branch
from
ox/fw_kwargs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,11 +117,33 @@ function (::∂☆internal{1})(args::AbstractTangentBundle{1}...) | |
end | ||
end | ||
|
||
_frule(partials, primals...) = frule(DiffractorRuleConfig(), partials, primals...) | ||
function _frule(::NTuple{<:Any, AbstractZero}, f, primal_args...) | ||
# frules with keywords support: | ||
function (::∂☆internal{1})(kwc::ATB{1, typeof(Core.kwcall)}, kw::ATB{1}, f::ATB{1}, args::ATB{1}...) | ||
args_primals = (primal(f), map(primal, args)...) | ||
args_partials = (first_partial(f), map(first_partial, args)...) | ||
# First check if directly overloading frule for kwcall | ||
r = _frule( | ||
(first_partial(kwc), first_partial(kw), args_partials...), | ||
primal(kwc), primal(kw), args_primals... | ||
) | ||
if r===nothing | ||
# then check if the frule for f accepts keywords | ||
# This silently discards tangents of the kw-args | ||
# TODO: should we error if they nonzero? | ||
r = _frule(args_partials, args_primals...; primal(kw)...) | ||
end | ||
if r === nothing | ||
return ∂☆recurse{1}()(kwc, kw, f, args...) | ||
else | ||
return shuffle_base(r) | ||
end | ||
end | ||
|
||
_frule(partials, primals...; kwargs...) = frule(DiffractorRuleConfig(), partials, primals...; kwargs...) | ||
function _frule(::NTuple{<:Any, AbstractZero}, f, primal_args...; kwargs...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect for performance, you probably want to define the non-kwargs version separately, so that higher-order AD doesn't have to AD through all the kwargs dispatch logic, like we do in ChainRules. |
||
# frules are linear in partials, so zero maps to zero, no need to evaluate the frule | ||
# If all partials are immutable AbstractZero subtyoes we know we don't have to worry about a mutating frule either | ||
r = f(primal_args...) | ||
r = f(primal_args...; kwargs...) | ||
return r, zero_tangent(r) | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like this fallback. The problem is that if we support it at first order, we need to have the same logic at higher orders and it just becomes complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't have this, then we do not need this PR at all, since writing the rule for
kwcall
currently does work.This particular logic is the code of this PR.
It's what makes it possible to write frules that have keywords args that match to functions with keyword args.
I don't understand what the problem with this at higher order is?