-
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
base: main
Are you sure you want to change the base?
Changes from all commits
58023a9
2554d8d
3c2139d
5357ee2
b6b7e4e
14afb58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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) | ||
|
Comment on lines
+75
to
+77
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are probably all regular
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it might be nicer to simply have the default |
||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.