-
Notifications
You must be signed in to change notification settings - Fork 61
Forward and reverse Enzyme tests and rules for linalg #449
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
kshyatt
wants to merge
4
commits into
main
Choose a base branch
from
ksh/enz_linalg
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 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,16 @@ | ||
| module TensorKitEnzymeExt | ||
|
|
||
| using Enzyme | ||
| using TensorKit | ||
| import TensorKit as TK | ||
| using VectorInterface | ||
| using TensorOperations: TensorOperations, IndexTuple, Index2Tuple, linearize | ||
| import TensorOperations as TO | ||
| using MatrixAlgebraKit | ||
| using TupleTools | ||
| using Random: AbstractRNG | ||
|
|
||
| include("utility.jl") | ||
| include("linalg.jl") | ||
|
|
||
| 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,262 @@ | ||
| # Shared | ||
| # ------ | ||
| # Can Enzyme do this itself? Apparently not... | ||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(mul!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| B::Annotation{<:AbstractTensorMap}, | ||
| α::Annotation, | ||
| β::Annotation, | ||
| ) where {RT} | ||
| cacheC = !isa(β, Const) && copy(C.val) | ||
| cacheA = !isa(B, Const) && EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing | ||
| cacheB = !isa(A, Const) && EnzymeRules.overwritten(config)[4] ? copy(B.val) : nothing | ||
| AB = if !isa(α, Const) | ||
| AB = A.val * B.val | ||
| add!(C.val, AB, α.val, β.val) | ||
| AB | ||
| else | ||
| mul!(C.val, A.val, B.val, α.val, β.val) | ||
| nothing | ||
| end | ||
| primal = EnzymeRules.needs_primal(config) ? C.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing | ||
| cache = (cacheC, cacheA, cacheB, AB) | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(mul!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| B::Annotation{<:AbstractTensorMap}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| ) where {RT} | ||
| if RT <: Const | ||
| Δα = isa(α, Const) ? nothing : zero(α.val) | ||
| Δβ = isa(β, Const) ? nothing : zero(β.val) | ||
| return (nothing, nothing, nothing, Δα, Δβ) | ||
| end | ||
| cacheC, cacheA, cacheB, AB = cache | ||
| Cval = something(cacheC, C.val) | ||
| Aval = something(cacheA, A.val) | ||
| Bval = something(cacheB, B.val) | ||
|
|
||
| !isa(A, Const) && !isa(C, Const) && project_mul!(A.dval, C.dval, Bval', conj(α.val)) | ||
| !isa(B, Const) && !isa(C, Const) && project_mul!(B.dval, Aval', C.dval, conj(α.val)) | ||
| Δαr = pullback_dα(α, C, AB) | ||
| Δβr = pullback_dβ(β, C, Cval) | ||
| !isa(C, Const) && pullback_dC!(C.dval, β.val) | ||
|
|
||
| return (nothing, nothing, nothing, Δαr, Δβr) | ||
| end | ||
|
|
||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof(mul!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| B::Annotation{<:AbstractTensorMap}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| ) where {RT} | ||
| # ΔC′ = ΔC*β + C*Δβ + A*B*Δα + ΔA*B*α + A*ΔB*α | ||
| if !isa(C, Const) | ||
| scale!(C.dval, β.val) | ||
| !isa(β, Const) && add!(C.dval, C.val, β.dval) | ||
| !isa(α, Const) && project_mul!(C.dval, A.val, B.val, α.dval) | ||
| !isa(A, Const) && project_mul!(C.dval, A.dval, B.val, α.val) | ||
| !isa(B, Const) && project_mul!(C.dval, A.val, B.dval, α.val) | ||
| end | ||
| mul!(C.val, A.val, B.val, α.val, β.val) | ||
| if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) | ||
| return C | ||
| elseif EnzymeRules.needs_primal(config) | ||
| return C.val | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| return C.dval | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(tr)}, | ||
| ::Type{RT}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| ) where {RT} | ||
| ret = func.val(A.val) | ||
| primal = EnzymeRules.needs_primal(config) ? ret : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? zero(ret) : nothing | ||
| cache = EnzymeRules.overwritten(config)[2] ? copy(A.val) : nothing | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(tr)}, | ||
| dret::Active, | ||
| cache, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| ) | ||
| Aval = something(cache, A.val) | ||
| Δtrace = dret.val | ||
| if !isa(A, Const) | ||
| for (_, b) in blocks(A.dval) | ||
| TensorKit.diagview(b) .+= Δtrace | ||
| end | ||
| end | ||
| return (nothing,) | ||
| end | ||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(tr)}, | ||
| ::Type{<:Const}, | ||
| cache, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| ) | ||
| return (nothing,) | ||
| end | ||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| ::Type{RT}, | ||
| func::Const{typeof(tr)}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| ) where {RT} | ||
| y = EnzymeRules.needs_primal(config) ? tr(A.val) : nothing | ||
| Δy = if EnzymeRules.needs_shadow(config) && !isa(A, Const) | ||
| tr(A.dval) | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| zero(eltype(A.dval)) | ||
| else | ||
| nothing | ||
| end | ||
| if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) | ||
| return Duplicated(y, Δy) | ||
| elseif EnzymeRules.needs_primal(config) | ||
| return y | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| return Δy | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(norm)}, | ||
| ::Type{RT}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| p::Const{<:Real}, | ||
| ) where {RT} | ||
| p.val == 2 || error("currently only implemented for p = 2") | ||
| ret = func.val(A.val, p.val) | ||
| primal = EnzymeRules.needs_primal(config) ? ret : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? zero(ret) : nothing | ||
| cacheA = EnzymeRules.overwritten(config)[2] ? copy(A.val) : nothing | ||
| cache = (ret, cacheA) | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(norm)}, | ||
| dret::Active, | ||
| cache, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| p::Const{<:Real}, | ||
| ) | ||
| n, cacheA = cache | ||
| Δn = dret.val | ||
| p.val == 2 || error("currently only implemented for p = 2") | ||
| Aval = something(cacheA, A.val) | ||
| if !isa(A, Const) | ||
| x = (Δn' + Δn) / 2 / hypot(n, eps(one(n))) | ||
| add!(A.dval, A.val, x) | ||
| end | ||
| return (nothing, nothing) | ||
| end | ||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(norm)}, | ||
| ::Type{<:Const}, | ||
| cache, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| p::Const{<:Real}, | ||
| ) | ||
| return (nothing, nothing) | ||
| end | ||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof(norm)}, | ||
| ::Type{RT}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| p::Const{<:Real}, | ||
| ) where {RT} | ||
| y = norm(A.val, p.val) | ||
| Δy = if EnzymeRules.needs_shadow(config) && !isa(A, Const) | ||
| real(dot(A.val, A.dval)) * pinv(y) | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| zero(eltype(A.dval)) | ||
| else | ||
| nothing | ||
| end | ||
| if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) | ||
| return Duplicated(y, Δy) | ||
| elseif EnzymeRules.needs_primal(config) | ||
| return y | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| return Δy | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(inv)}, | ||
| ::Type{RT}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| ) where {RT} | ||
| ret = inv(A.val) | ||
| primal = EnzymeRules.needs_primal(config) ? ret : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? make_zero(ret) : nothing | ||
| cache = (ret, shadow) | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(inv)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| ) where {RT} | ||
| Ainv, ΔAinv = cache | ||
| !isa(A, Const) && mul!(A.dval, Ainv' * ΔAinv, Ainv', -1, One()) | ||
| return (nothing,) | ||
| end | ||
|
|
||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof(inv)}, | ||
| ::Type{RT}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| ) where {RT} | ||
| Ainv = inv(A.val) | ||
| ΔAinv = !isa(A, Const) ? scale!(Ainv * A.dval * Ainv, -1) : make_zero(Ainv) | ||
| if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) | ||
| return Duplicated(Ainv, ΔAinv) | ||
| elseif EnzymeRules.needs_primal(config) | ||
| return Ainv | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| return ΔAinv | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
Oops, something went wrong.
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.
Since my Enzyme knowledge has fallen back to zero, I was comparing this with some
mul!rule in Enzyme.jl ( https://github.com/EnzymeAD/Enzyme.jl/blob/6d9c0cb7fa1ab4a4ce347ba506ea9715761365a8/src/internal_rules/linalg.jl#L304 ), and while there are clearly some similarities, there are also some differences, e.g. in whichoverwritten(config)positions are checked to decide oncacheAandcacheB(they use 5 and 6, compared to 3 and 4 here). Is there a good explanation for that?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.
It's probably that they are correct and I made a mistake. This part is very difficult to test because it only arises in a longer set of operations. I wish they had written their
mul!rule to be a little more genericThere 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://enzymead.github.io/Enzyme.jl/dev/generated/custom_rule/#Defining-a-reverse-mode-rule what's confusing is it doesn't match what is shown here. I will ask the devs.
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://github.com/EnzymeAD/Enzyme.jl/blob/6d9c0cb7fa1ab4a4ce347ba506ea9715761365a8/src/internal_rules/linalg.jl#L35 Seems to follow what I have here.
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 also think your counting is consistent with the doc string of
overwritten.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 posted about it on Slack to hopefully get this figured out but now I'm so confused about who is right 😹