-
Notifications
You must be signed in to change notification settings - Fork 61
Forward and reverse Enzyme rules and tests for TensorOperations #451
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
2
commits into
main
Choose a base branch
from
ksh/enz_to
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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("tensoroperations.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,274 @@ | ||
| # tensorcontract! | ||
| # --------------- | ||
| # TODO: it might be beneficial to compare here if it would make sense to simply compute the | ||
| # rrule of permute-permute-gemm-permute, rather than using the contractions directly. | ||
| # This could possibly out save some permutations being carried out twice, at the cost of having | ||
| # to store some more intermediate objects. | ||
| # For example, the combination `ΔC, pΔC, false` appears in the pullback for ΔA and ΔB, so effectively | ||
| # this permutation is done multiple times. | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TensorKit.blas_contract!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| pA::Const{<:Index2Tuple}, | ||
| B::Annotation{<:AbstractTensorMap}, | ||
| pB::Const{<:Index2Tuple}, | ||
| pAB::Const{<:Index2Tuple}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| backend::Const, | ||
| allocator::Const | ||
| ) where {RT} | ||
| Ccache = isa(β, Const) ? nothing : copy(C.val) | ||
| A_needs_cache = EnzymeRules.overwritten(config)[3] && !(typeof(B) <: Const) && !(typeof(C) <: Const) | ||
| Acache = A_needs_cache ? copy(A.val) : nothing | ||
| B_needs_cache = EnzymeRules.overwritten(config)[5] && !(typeof(A) <: Const) && !(typeof(C) <: Const) | ||
| Bcache = B_needs_cache ? copy(B.val) : nothing | ||
| AB = if !isa(α, Const) | ||
| AB = TO.tensorcontract(A.val, pA.val, false, B.val, pB.val, false, pAB.val, One(), backend.val, allocator.val) | ||
| add!(C.val, AB, α.val, β.val) | ||
| AB | ||
| else | ||
| TensorKit.blas_contract!(C.val, A.val, pA.val, B.val, pB.val, pAB.val, α.val, β.val, backend.val, allocator.val) | ||
| nothing | ||
| end | ||
| primal = EnzymeRules.needs_primal(config) ? C.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing | ||
| cache = (Ccache, Acache, Bcache, AB) | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TensorKit.blas_contract!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| pA::Const{<:Index2Tuple}, | ||
| B::Annotation{<:AbstractTensorMap}, | ||
| pB::Const{<:Index2Tuple}, | ||
| pAB::Const{<:Index2Tuple}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| backend::Const, | ||
| allocator::Const | ||
| ) where {RT} | ||
| cacheC, cacheA, cacheB, AB = cache | ||
| Cval = cacheC | ||
| Aval = something(cacheA, A.val) | ||
| Bval = something(cacheB, B.val) | ||
|
|
||
| Δα = pullback_dα(α, C, AB) | ||
| Δβ = pullback_dβ(β, C, Cval) | ||
|
|
||
| if !isa(A, Const) | ||
| blas_contract_pullback_ΔA!( | ||
| A.dval, C.dval, Aval, pA.val, Bval, pB.val, pAB.val, α.val, backend.val, allocator.val | ||
| ) # this typically returns nothing | ||
| end | ||
| if !isa(B, Const) | ||
| blas_contract_pullback_ΔB!( | ||
| B.dval, C.dval, Aval, pA.val, Bval, pB.val, pAB.val, α.val, backend.val, allocator.val | ||
| ) # this typically returns nothing | ||
| end | ||
| !isa(C, Const) && pullback_dC!(C.dval, β.val) # this typically returns nothing | ||
| return nothing, nothing, nothing, nothing, nothing, nothing, Δα, Δβ, nothing, nothing | ||
| end | ||
|
|
||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof(TensorKit.blas_contract!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| pA::Annotation{<:Index2Tuple}, | ||
| B::Annotation{<:AbstractTensorMap}, | ||
| pB::Annotation{<:Index2Tuple}, | ||
| pAB::Annotation{<:Index2Tuple}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| backend::Const, | ||
| allocator::Const | ||
| ) where {RT} | ||
| # ΔC′ = ΔC*β + C*Δβ + A*B*Δα + ΔA*B*α + A*ΔB*α | ||
| if !isa(C, Const) | ||
| if isa(β, Const) | ||
| scale!(C.dval, β.val) | ||
| else | ||
| add!(C.dval, C.val, β.dval, β.val) | ||
| end | ||
| !isa(α, Const) && TensorKit.blas_contract!(C.dval, A.val, pA.val, B.val, pB.val, pAB.val, α.dval, One(), backend.val, allocator.val) | ||
| !isa(A, Const) && TensorKit.blas_contract!(C.dval, A.dval, pA.val, B.val, pB.val, pAB.val, α.val, One(), backend.val, allocator.val) | ||
| !isa(B, Const) && TensorKit.blas_contract!(C.dval, A.val, pA.val, B.dval, pB.val, pAB.val, α.val, One(), backend.val, allocator.val) | ||
| end | ||
| TensorKit.blas_contract!(C.val, A.val, pA.val, B.val, pB.val, pAB.val, α.val, β.val, backend.val, allocator.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 blas_contract_pullback_ΔA!( | ||
| ΔA, ΔC, A, pA, B, pB, pAB, α, backend, allocator | ||
| ) | ||
| ipAB = invperm(linearize(pAB)) | ||
| pΔC = _repartition(ipAB, TO.numout(pA)) | ||
| ipA = _repartition(invperm(linearize(pA)), A) | ||
|
|
||
| tB = twist( | ||
| B, | ||
| TupleTools.vcat( | ||
| filter(x -> !isdual(space(B, x)), pB[1]), | ||
| filter(x -> isdual(space(B, x)), pB[2]) | ||
| ); copy = false | ||
| ) | ||
|
|
||
| project_contract!( | ||
| ΔA, | ||
| ΔC, pΔC, false, | ||
| tB, reverse(pB), true, | ||
| ipA, conj(α), backend, allocator | ||
| ) | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
| function blas_contract_pullback_ΔB!( | ||
| ΔB, ΔC, A, pA, B, pB, pAB, α, backend, allocator | ||
| ) | ||
| ipAB = invperm(linearize(pAB)) | ||
| pΔC = _repartition(ipAB, TO.numout(pA)) | ||
| ipB = _repartition(invperm(linearize(pB)), B) | ||
|
|
||
| tA = twist( | ||
| A, | ||
| TupleTools.vcat( | ||
| filter(x -> isdual(space(A, x)), pA[1]), | ||
| filter(x -> !isdual(space(A, x)), pA[2]) | ||
| ); copy = false | ||
| ) | ||
|
|
||
| project_contract!( | ||
| ΔB, | ||
| tA, reverse(pA), true, | ||
| ΔC, pΔC, false, | ||
| ipB, conj(α), backend, allocator | ||
| ) | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
|
|
||
| # tensortrace! | ||
| # ------------ | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TensorKit.trace_permute!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| p::Const{<:Index2Tuple}, | ||
| q::Const{<:Index2Tuple}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| backend::Const, | ||
| ) where {RT} | ||
| C_cache = !isa(β, Const) ? copy(C.val) : nothing | ||
| A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing | ||
| At = if !isa(α, Const) | ||
| At = TO.tensortrace(A.val, p.val, q.val, false, One(), backend.val) | ||
| add!(C.val, At, α.val, β.val) | ||
| At | ||
| else | ||
| TensorKit.trace_permute!(C.val, A.val, p.val, q.val, α.val, β.val, backend.val) | ||
| nothing | ||
| end | ||
| primal = EnzymeRules.needs_primal(config) ? C.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing | ||
| cache = (C_cache, A_cache, At) | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
|
|
||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TensorKit.trace_permute!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| p::Const{<:Index2Tuple}, | ||
| q::Const{<:Index2Tuple}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| backend::Const, | ||
| ) where {RT} | ||
| C_cache, A_cache, At = cache | ||
| Aval = something(A_cache, A.val) | ||
| Cval = something(C_cache, C.val) | ||
| !isa(A, Const) && !isa(C, Const) && trace_permute_pullback_ΔA!(A.dval, C.dval, Aval, p.val, q.val, α.val, backend.val) | ||
| Δαr = pullback_dα(α, C, At) | ||
| Δβr = pullback_dβ(β, C, Cval) | ||
| !isa(C, Const) && pullback_dC!(C.dval, β.val) | ||
| return nothing, nothing, nothing, nothing, Δαr, Δβr, nothing | ||
| end | ||
|
|
||
| function trace_permute_pullback_ΔA!( | ||
| ΔA, ΔC, A, p, q, α, backend | ||
| ) | ||
| ip = invperm((linearize(p)..., q[1]..., q[2]...)) | ||
| pdA = _repartition(ip, A) | ||
| E = one!(TO.tensoralloc_add(scalartype(A), A, q, false)) | ||
| twist!(E, filter(x -> !isdual(space(E, x)), codomainind(E))) | ||
| pE = ((), trivtuple(TO.numind(q))) | ||
| pΔC = (trivtuple(TO.numind(p)), ()) | ||
| TO.tensorproduct!( | ||
| ΔA, ΔC, pΔC, false, E, pE, false, pdA, conj(α), One(), backend | ||
| ) | ||
| return nothing | ||
| end | ||
|
|
||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof(TensorKit.trace_permute!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| p::Annotation{<:Index2Tuple}, | ||
| q::Annotation{<:Index2Tuple}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| backend::Const, | ||
| ) where {RT} | ||
| # dD = dα * tr(A) + α * tr(dA) + dβ * C + β * dC | ||
| # dC1 = dβ * C + β * dC | ||
| if !isa(C, Const) | ||
| if isa(β, Const) | ||
| scale!(C.dval, β.val) | ||
| else | ||
| add!(C.dval, C.val, β.dval, β.val) | ||
| end | ||
| !isa(α, Const) && TensorKit.trace_permute!(C.dval, A.val, p.val, q.val, α.dval, One(), backend.val) | ||
| !isa(A, Const) && TensorKit.trace_permute!(C.dval, A.dval, p.val, q.val, α.val, One(), backend.val) | ||
| end | ||
| TensorKit.trace_permute!(C.val, A.val, p.val, q.val, α.val, β.val, backend.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 | ||
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.
These generic rules should probably appear somewhere more centrally, in the main TensorKit module? Also, same question as always: do we really need to redefine all these rules? Can't AD do this for us, using only the rules for the lower level primitives?