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

Support frules with keyword arguments #266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/stage1/forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Copy link
Collaborator

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.

Copy link
Member Author

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?

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

Choose a reason for hiding this comment

The 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

Expand Down
38 changes: 38 additions & 0 deletions test/forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,42 @@ end
end
end

@testset "frule with kwarg" begin
mulby_kw(v; n) = n*v
triple(v) = mulby_kw(v; n=3)
frule_hits = 0
function ChainRulesCore.frule((_, dv), ::typeof(mulby_kw), v; n)
y = mulby_kw(v; n)
dy = n*dv
frule_hits +=1
return y, dy
end

let var"'" = Diffractor.PrimeDerivativeFwd
@assert frule_hits == 0
@test triple'(2.0) == 3.0
@test frule_hits == 1
end

mulby_kw2(v; n) = n*v
square(v) = mulby_kw2(v; n=v)
frule_hits = 0
function ChainRulesCore.frule((_, dkw, _, dv), ::typeof(Core.kwcall), kw, ::typeof(mulby_kw2), v)
n = kw.n
dn = dkw.n
y = mulby_kw2(v; n)
dy = n*dv + dn*v
frule_hits +=1
return y, dy
end

let var"'" = Diffractor.PrimeDerivativeFwd
@assert frule_hits == 0
@test square'(3.0) == 6.0
@test frule_hits == 1
end
end

end # module