-
Notifications
You must be signed in to change notification settings - Fork 1
Add rrule_from_frule macro #2
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
Draft
niklasschmitz
wants to merge
8
commits into
JuliaDiff:main
Choose a base branch
from
niklasschmitz:rrule_from_frule
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.
Draft
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b5cc93
add rrule_from_frule v0.1
niklasschmitz 16dc6e2
export rrule_from_frule macro
niklasschmitz 2107a6f
add Zygote test
niklasschmitz 462cd4a
fix escaping bug
niklasschmitz 3ff01c4
mv test imports to runtests.jl
niklasschmitz 825372e
add docstring
niklasschmitz 4518ae9
add ref
niklasschmitz 7ca0048
add support for varargs
niklasschmitz 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| module ChainRulesDeclarationHelpers | ||
|
|
||
| export @rrule_from_frule | ||
| include("rrule_from_frule.jl") | ||
| end # module |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using ChainRulesCore | ||
|
|
||
| """ | ||
| @rrule_from_frule(signature_expression) | ||
|
|
||
| A helper to define an rrule by calling back into AD on an already defined frule. | ||
| """ | ||
| macro rrule_from_frule(signature_expression) | ||
| @assert Meta.isexpr(signature_expression, :call) | ||
| @assert length(signature_expression.args) == 2 "Only single-argument functions are implemented." | ||
| # TODO add support for multiple arguments, varargs, kwargs | ||
|
|
||
| f = signature_expression.args[1] | ||
| arg = signature_expression.args[2] | ||
| @assert Meta.isexpr(arg, :(::), 2) | ||
|
|
||
| return rrule_from_frule_expr(__source__, f, arg) | ||
| end | ||
|
|
||
| function rrule_from_frule_expr(__source__, f, arg) | ||
| f_instance_name = gensym(Symbol(:instance_, Symbol(f))) | ||
| return quote | ||
| function ChainRulesCore.rrule(config::RuleConfig{>:HasReverseMode}, $f_instance_name::Core.Typeof($(esc(f))), $arg) | ||
| $(__source__) | ||
| pushforward(Δfarg...) = frule(Δfarg, $f_instance_name, $arg)[2] | ||
| _, back = rrule_via_ad(config, pushforward, $f_instance_name, $arg) | ||
| y = $f_instance_name($arg) # TODO optimize away redundant primal computation | ||
| f_pullback(Δy) = back(Δy)[2:end] | ||
| return y, f_pullback | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using Test | ||
| using ChainRulesDeclarationHelpers | ||
| using ChainRulesCore | ||
| using ChainRulesTestUtils | ||
| using Zygote | ||
|
|
||
|
|
||
| @testset "rrule_from_frule" begin | ||
| function f(x) | ||
| a = sin.(x) | ||
| b = sum(a) | ||
| c = b * a | ||
| return c | ||
| end | ||
|
|
||
| function ChainRulesCore.frule((Δself, Δx), ::typeof(f), x) | ||
| a, ȧ = sin.(x), cos.(x) .* Δx | ||
| b, ḃ = sum(a), sum(ȧ) | ||
| c, ċ = b * a, ḃ * a + b * ȧ | ||
| return c, ċ | ||
| end | ||
|
|
||
| x = rand(3) | ||
| test_frule(f, x) | ||
|
|
||
| @rrule_from_frule f(x::AbstractArray{<:Real}) | ||
| test_rrule(Zygote.ZygoteRuleConfig(), f, x; check_inferred=false) | ||
| end | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.