-
Notifications
You must be signed in to change notification settings - Fork 80
Add new test mode rewind #2645
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
Open
kshyatt
wants to merge
1
commit into
EnzymeAD:main
Choose a base branch
from
kshyatt:ksh/rewind
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
Add new test mode rewind #2645
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 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 |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """ | ||
| test_rewind(f, Activity, args...; kwargs...) | ||
|
|
||
| Test `Enzyme.autodiff` of `f` in `Forward`-mode by backtracking using `Reverse`-mode, | ||
| which itself is checked against finite differences. This mode can be useful when computing | ||
| derivatives on functions such as matrix factorizations, where a particular choice of | ||
| gauge is important and the finite-differences approach generates tangents in an arbitrary | ||
| gauge. In effect, this plays the derivatives _forward_, then in _reverse_, "rewinding" the | ||
| tape. | ||
|
|
||
| `f` has all constraints of the same argument passed to `Enzyme.autodiff`, with additional | ||
| constraints: | ||
| - If it mutates one of its arguments, it _must_ return that argument. | ||
|
|
||
| To use this test mode, `f` _must_ have both forward and reverse rules defined. | ||
|
|
||
| # Arguments | ||
|
|
||
| - `Activity`: the activity of the return value of `f` | ||
| - `args`: Each entry is either an argument to `f`, an activity type accepted by `autodiff`, | ||
| or a tuple of the form `(arg, Activity)`, where `Activity` is the activity type of | ||
| `arg`. If the activity type specified requires a tangent, a random tangent will be | ||
| automatically generated. | ||
|
|
||
| # Keywords | ||
|
|
||
| - `rng::AbstractRNG`: The random number generator to use for generating random tangents. | ||
| - `fdm=FiniteDifferences.central_fdm(5, 1)`: The finite differences method to use. | ||
| - `fkwargs`: Keyword arguments to pass to `f`. | ||
| - `rtol`: Relative tolerance for `isapprox`. | ||
| - `atol`: Absolute tolerance for `isapprox`. | ||
| - `testset_name`: Name to use for a testset in which all tests are evaluated. | ||
| - `output_tangent`: Optional final tangent to provide at the beginning of the reverse-mode differentiation | ||
|
|
||
| # Examples | ||
|
|
||
| Here we test a rule for a function of scalars. Because we don't provide an activity | ||
| annotation for `y`, it is assumed to be `Const`. | ||
|
|
||
| ```julia | ||
| using Enzyme, EnzymeTestUtils | ||
|
|
||
| x, y = randn(2) | ||
| for Tret in (Const, Duplicated, DuplicatedNoNeed), Tx in (Const, Duplicated) | ||
| test_forward(*, Tret, (x, Tx), y) | ||
| end | ||
| ``` | ||
|
|
||
| Here we test a rule for a function of an array in batch forward-mode: | ||
|
|
||
| ```julia | ||
| x = randn(3) | ||
| y = randn() | ||
| for Tret in (Const, BatchDuplicated, BatchDuplicatedNoNeed), | ||
| Tx in (Const, BatchDuplicated), | ||
| Ty in (Const, BatchDuplicated) | ||
|
|
||
| test_forward(*, Tret, (x, Tx), (y, Ty)) | ||
| end | ||
| ``` | ||
| """ | ||
|
|
||
| function test_rewind( | ||
| f, | ||
| fwd_ret_activity, | ||
| rvs_ret_activity, | ||
| args...; | ||
| rng::Random.AbstractRNG=Random.default_rng(), | ||
| fdm=FiniteDifferences.central_fdm(5, 1), | ||
| fkwargs::NamedTuple=NamedTuple(), | ||
| rtol::Real=1e-9, | ||
| atol::Real=1e-9, | ||
| testset_name=nothing, | ||
| runtime_activity::Bool=false, | ||
| output_tangent=nothing, | ||
| ) | ||
| # first, test reverse as normal with finite differences | ||
| test_reverse(f, rvs_ret_activity, args...; rng=rng, fdm=fdm, fkwargs=fkwargs, rtol=rtol, atol=atol, testset_name=testset_name, runtime_activity=runtime_activity, output_tangent=output_tangent) | ||
| # now, use the reverse rule to compare with the forward result | ||
| if testset_name === nothing | ||
| testset_name = "test_rewind: $f with return activity $fwd_ret_activity on $(_string_activity(args))" | ||
| end | ||
| @testset "$testset_name" begin | ||
| # test reverse rule to make sure it works with FD | ||
| # run fwd mode first | ||
|
|
||
| # format arguments for autodiff and FiniteDifferences | ||
| activities = map(Base.Fix1(auto_activity, rng), (f, args...)) | ||
| primals = map(x -> x.val, activities) | ||
| # call primal, avoid mutating original arguments | ||
| fcopy = deepcopy(first(primals)) | ||
| args_copy = deepcopy(Base.tail(primals)) | ||
| y = fcopy(args_copy...; deepcopy(fkwargs)...) | ||
| mode = if fwd_ret_activity <: Union{DuplicatedNoNeed, BatchDuplicatedNoNeed, Const} | ||
| Forward | ||
| else | ||
| ForwardWithPrimal | ||
| end | ||
| mode = set_runtime_activity(mode, runtime_activity) | ||
| ret_activity2 = if fwd_ret_activity <: DuplicatedNoNeed | ||
| Duplicated | ||
| elseif fwd_ret_activity <: BatchDuplicatedNoNeed | ||
| BatchDuplicated | ||
| else | ||
| fwd_ret_activity | ||
| end | ||
| call_with_kwargs(f, xs...) = f(xs...; fkwargs...) | ||
| y_and_dy_ad = autodiff(mode, call_with_kwargs, ret_activity2, activities...) | ||
| dy_ad = y_and_dy_ad[1] | ||
| # now run this back through reverse mode, using dy_ad from forward mode | ||
| # as the output tangent | ||
| test_reverse(f, rvs_ret_activity, args...; rng=rng, fdm=fdm, fkwargs=fkwargs, rtol=rtol, atol=atol, testset_name=testset_name, runtime_activity=runtime_activity, output_tangent=dy_ad) | ||
| end | ||
| end | ||
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 must admit this is the first time I encounter the term
gauge.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.
Ah sorry it's physicist brain
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.
https://en.wikipedia.org/wiki/Gauge_fixing if you're interested. I'll try to rephrase