Repository navigation
Cached treetwister for twist!
#599
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
93baf98
866ee15
cef98d8
378900a
7584f98
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 |
|---|---|---|
|
|
@@ -186,6 +186,144 @@ function EnzymeRules.forward( | |
| end | ||
| end | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TK.twist!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| inds::Const, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| ba::Const...; | ||
| inv::Bool = false | ||
| ) where {RT} | ||
| C_cache = !isa(β, Const) ? copy(C.val) : nothing | ||
| A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing | ||
| # if we need to compute Δa, it is faster to allocate an intermediate twisted A | ||
| # and store that instead of repeating the twist in the pullback each time. | ||
| # effectively, we replace `twist!` by `add ∘ twist`. | ||
| Ap = if !isa(α, Const) | ||
| Ap = twist(A.val, inds.val; inv, copy = true) | ||
| add!(C.val, Ap, α.val, β.val) | ||
| Ap | ||
| else | ||
| bavs = map(a -> a.val, ba) | ||
| TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv) | ||
| nothing | ||
| end | ||
| cache = (C_cache, A_cache, Ap) | ||
| primal = EnzymeRules.needs_primal(config) ? C.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
| function EnzymeRules.reverse( | ||
|
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. Was Enzyme not able to make its own rules for these? Kind of sad if so...
Member
Author
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 changes make |
||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TK.twist!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| inds::Const, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| ba::Const...; | ||
| inv::Bool = false | ||
| ) where {RT} | ||
| C_cache, A_cache, Ap = cache | ||
| Cval = something(C_cache, C.val) | ||
| Aval = something(A_cache, A.val) | ||
| bavs = map(a -> a.val, ba) | ||
| # ΔA | ||
| if !isa(A, Const) && !isa(C, Const) | ||
| TK.twist_pb(A.dval, Aval, C.dval, C.val, inds.val, α.val, bavs...; inv) | ||
| end | ||
| Δαr = pullback_dα(α, C, Ap) | ||
| Δβr = pullback_dβ(β, C, Cval) | ||
| !isa(C, Const) && pullback_dC!(C.dval, β.val) | ||
| return nothing, nothing, nothing, Δαr, Δβr, map(Returns(nothing), ba)... | ||
| end | ||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof(TK.twist!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:AbstractTensorMap}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| inds::Annotation, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number}, | ||
| ba::Const...; | ||
| inv::Bool = false | ||
| ) where {RT} | ||
| bavs = map(a -> a.val, ba) | ||
| if !isa(C, Const) | ||
| if isa(β, Const) | ||
| scale!(C.dval, β.val) | ||
| else | ||
| add!(C.dval, C.val, β.dval, β.val) | ||
| end | ||
| !isa(α, Const) && TK.twist!(C.dval, A.val, inds.val, α.dval, One(), bavs...; inv) | ||
| !isa(A, Const) && TK.twist!(C.dval, A.dval, inds.val, α.val, One(), bavs...; inv) | ||
| end | ||
| TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv) | ||
| 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 | ||
|
|
||
| # in-place twist: the source and destination are the same tensor | ||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TK.twist!)}, | ||
| ::Type{RT}, | ||
| t::Annotation{<:AbstractTensorMap}, | ||
| inds::Const; | ||
| inv::Bool = false | ||
| ) where {RT} | ||
| TK.twist!(t.val, inds.val; inv) | ||
| primal = EnzymeRules.needs_primal(config) ? t.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? t.dval : nothing | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, nothing) | ||
| end | ||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TK.twist!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| t::Annotation{<:AbstractTensorMap}, | ||
| inds::Const; | ||
| inv::Bool = false | ||
| ) where {RT} | ||
| !isa(t, Const) && TK.twist!(t.dval, inds.val; inv = !inv) | ||
| return (nothing, nothing) | ||
| end | ||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof(TK.twist!)}, | ||
| ::Type{RT}, | ||
| t::Annotation{<:AbstractTensorMap}, | ||
| inds::Annotation; | ||
| inv::Bool = false | ||
| ) where {RT} | ||
| TK.twist!(t.val, inds.val; inv) | ||
| !isa(t, Const) && TK.twist!(t.dval, inds.val; inv) | ||
| if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) | ||
| return t | ||
| elseif EnzymeRules.needs_primal(config) | ||
| return t.val | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| return t.dval | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(flip)}, | ||
|
|
||
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.
similar to permute and transform, is there any chance this could secretly be aliasing
A.val?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.
That's what the
copy=trueis for right?