-
Notifications
You must be signed in to change notification settings - Fork 61
A couple missing zero-derivs and an inplace rrule test for svd_trunc #448
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
Merged
+45
−74
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06c74f9
A couple missing zero-derivs and an inplace rrule for svd_trunc
kshyatt 9c21640
Fixes and add test
kshyatt 01338d8
Mark init output as zero derivative
kshyatt ab4bf41
rrule liposuction
kshyatt b2f068d
Enable svd_full and svd_compact tests too
kshyatt 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,2 @@ | ||
| for f in (:svd_compact, :svd_full) | ||
| f_pullback = Symbol(f, :_pullback) | ||
| @eval begin | ||
| @is_primitive DefaultCtx ReverseMode Tuple{typeof($f), AbstractTensorMap, MatrixAlgebraKit.AbstractAlgorithm} | ||
| function Mooncake.rrule!!(::CoDual{typeof($f)}, A_dA::CoDual{<:AbstractTensorMap}, alg_dalg::CoDual) | ||
| A, dA = arrayify(A_dA) | ||
| alg = primal(alg_dalg) | ||
|
|
||
| USVᴴ = $f(A, primal(alg_dalg)) | ||
| USVᴴ_dUSVᴴ = Mooncake.zero_fcodual(USVᴴ) | ||
| dUSVᴴ = last.(arrayify.(USVᴴ, tangent(USVᴴ_dUSVᴴ))) | ||
|
|
||
| function $f_pullback(::NoRData) | ||
| MatrixAlgebraKit.svd_pullback!(dA, A, USVᴴ, dUSVᴴ) | ||
| MatrixAlgebraKit.zero!.(dUSVᴴ) | ||
| return ntuple(Returns(NoRData()), 3) | ||
| end | ||
|
|
||
| return USVᴴ_dUSVᴴ, $f_pullback | ||
| end | ||
| end | ||
|
|
||
| # mutating version is not guaranteed to actually mutate | ||
| # so we can simply use the non-mutating version instead and avoid having to worry about | ||
| # storing copies and restoring state | ||
| f! = Symbol(f, :!) | ||
| f!_pullback = Symbol(f!, :_pullback) | ||
| @eval begin | ||
| @is_primitive DefaultCtx ReverseMode Tuple{typeof($f!), AbstractTensorMap, Any, MatrixAlgebraKit.AbstractAlgorithm} | ||
| Mooncake.rrule!!(::CoDual{typeof($f!)}, A_dA::CoDual{<:AbstractTensorMap}, USVᴴ_dUSVᴴ::CoDual, alg_dalg::CoDual) = | ||
| Mooncake.rrule!!(Mooncake.zero_fcodual($f), A_dA, alg_dalg) | ||
| end | ||
| end | ||
|
|
||
| @is_primitive DefaultCtx ReverseMode Tuple{typeof(svd_trunc), AbstractTensorMap, MatrixAlgebraKit.AbstractAlgorithm} | ||
| function Mooncake.rrule!!( | ||
| ::CoDual{typeof(svd_trunc)}, | ||
| A_dA::CoDual{<:AbstractTensorMap}, | ||
| alg_dalg::CoDual{<:MatrixAlgebraKit.TruncatedAlgorithm} | ||
| ) | ||
| A, dA = arrayify(A_dA) | ||
| alg = primal(alg_dalg) | ||
|
|
||
| USVᴴ = svd_compact(A, alg.alg) | ||
| USVᴴtrunc, ind = MatrixAlgebraKit.truncate(svd_trunc!, USVᴴ, alg.trunc) | ||
| ϵ = MatrixAlgebraKit.truncation_error(diagview(USVᴴ[2]), ind) | ||
|
|
||
| USVᴴtrunc_dUSVᴴtrunc = Mooncake.zero_fcodual((USVᴴtrunc..., ϵ)) | ||
| dUSVᴴtrunc = last.(arrayify.(USVᴴtrunc, Base.front(tangent(USVᴴtrunc_dUSVᴴtrunc)))) | ||
|
|
||
| function svd_trunc_pullback((_, _, _, dϵ)::Tuple{NoRData, NoRData, NoRData, Real}) | ||
| abs(dϵ) ≤ MatrixAlgebraKit.defaulttol(dϵ) || | ||
| @warn "Gradient for `svd_trunc` ignores non-zero tangents for truncation error" | ||
| MatrixAlgebraKit.svd_pullback!(dA, A, USVᴴ, dUSVᴴtrunc, ind) | ||
| return ntuple(Returns(NoRData()), 3) | ||
| end | ||
|
|
||
| return USVᴴtrunc_dUSVᴴtrunc, svd_trunc_pullback | ||
| end | ||
|
|
||
| @is_primitive DefaultCtx ReverseMode Tuple{typeof(svd_trunc!), AbstractTensorMap, Any, MatrixAlgebraKit.AbstractAlgorithm} | ||
| Mooncake.rrule!!(::CoDual{typeof(svd_trunc!)}, A_dA::CoDual{<:AbstractTensorMap}, USVᴴ_dUSVᴴ::CoDual, alg_dalg::CoDual) = | ||
| Mooncake.rrule!!(Mooncake.zero_fcodual(svd_trunc), A_dA, alg_dalg) | ||
| # needed for the ising bimodule case | ||
| @zero_derivative DefaultCtx Tuple{typeof(MatrixAlgebraKit.initialize_output), Any, AbstractTensorMap, MatrixAlgebraKit.AbstractAlgorithm} |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.