Skip to content

Commit

Permalink
Support kwargs in rrule_via_ad (#1055)
Browse files Browse the repository at this point in the history
* allow kwargs

* add test for kwarg support

* bump patch

* unwrap closure

* first solution

* second solution kwf()

* Revert "second solution kwf()"

This reverts commit b53a381.

* avoid creating closure unnecesasrily

* short function

* first instead of only
  • Loading branch information
mzgubic authored Aug 20, 2021
1 parent 7f2b169 commit 05d0c2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Zygote"
uuid = "e88e6eb3-aa80-5325-afca-941959d7151f"
version = "0.6.19"
version = "0.6.20"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
14 changes: 11 additions & 3 deletions src/compiler/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,18 @@ As per [`chain_rrule`](@ref) but with support for kwargs.
return y, kw_zpullback
end

function ChainRulesCore.rrule_via_ad(config::ZygoteRuleConfig, f_args...; kwargs...)
# create a closure to work around _pullback not accepting kwargs
# but avoid creating a closure unnecessarily (pullbacks of closures do not infer)
y, pb = if !isempty(kwargs)
kwf() = first(f_args)(Base.tail(f_args)...; kwargs...)
_y, _pb = _pullback(config.context, kwf)
_y, Δ -> first(_pb(Δ)).f_args # `first` should be `only`
else
_pullback(config.context, f_args...)
end

function ChainRulesCore.rrule_via_ad(config::ZygoteRuleConfig, f, args...)
y, pb = _pullback(config.context, f, args...)
ad_pullback(Δ) = zygote2differential(pb(wrap_chainrules_output(Δ)), (f, args...))
ad_pullback(Δ) = zygote2differential(pb(wrap_chainrules_output(Δ)), f_args)
return y, ad_pullback
end

Expand Down
7 changes: 7 additions & 0 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ end
test_rrule(ZygoteRuleConfig(), getindex, rand(5), 3; rrule_f=rrule_via_ad)
end

@testset "kwargs" begin
test_rrule(
ZygoteRuleConfig(), sum, [1.0 2; 3 4];
rrule_f=rrule_via_ad, check_inferred=false, fkwargs=(;dims=1)
)
end

@testset "struct" begin
struct Foo
x
Expand Down

2 comments on commit 05d0c2a

@oxinabox
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/43226

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.20 -m "<description of version>" 05d0c2ae04f334a2ec61e42decfe1172d0f2e6e8
git push origin v0.6.20

Please sign in to comment.