Skip to content

Commit e288a38

Browse files
kshyattKatharine Hyatt
andauthored
Restore custom rule for twist to calm 1.10 (#559)
* Restore custom rule for twist to calm 1.10 * Loosen annotation type for twist fwd rule * Bump Enzyme compat --------- Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com>
1 parent ddeb327 commit e288a38

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Adapt = "4"
5151
CUDA = "6"
5252
ChainRulesCore = "1"
5353
Dictionaries = "0.4"
54-
Enzyme = "0.13.195"
54+
Enzyme = "0.13.207"
5555
EnzymeTestUtils = "0.2.8"
5656
FiniteDifferences = "0.12"
5757
GPUArrays = "11.4.1"

‎ext/TensorKitEnzymeExt/indexmanipulations.jl‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,57 @@ function EnzymeRules.reverse(
116116
return nothing, nothing, nothing, nothing, Δαr, Δβr, map(Returns(nothing), ba)...
117117
end
118118

119+
# Differentiating through the fusion tree loop corrupts the
120+
# heap on Julia 1.10 and causes segfaults in the GC. Remove this
121+
# custom rule when we drop support for 1.10
122+
function EnzymeRules.augmented_primal(
123+
config::EnzymeRules.RevConfigWidth{1},
124+
func::Const{typeof(twist!)},
125+
::Type{RT},
126+
t::Annotation{<:AbstractTensorMap},
127+
inds::Const;
128+
inv::Bool = false
129+
) where {RT}
130+
twist!(t.val, inds.val; inv)
131+
primal = EnzymeRules.needs_primal(config) ? t.val : nothing
132+
shadow = EnzymeRules.needs_shadow(config) ? t.dval : nothing
133+
return EnzymeRules.AugmentedReturn(primal, shadow, nothing)
134+
end
135+
136+
function EnzymeRules.reverse(
137+
config::EnzymeRules.RevConfigWidth{1},
138+
func::Const{typeof(twist!)},
139+
::Type{RT},
140+
cache,
141+
t::Annotation{<:AbstractTensorMap},
142+
inds::Const;
143+
inv::Bool = false
144+
) where {RT}
145+
!isa(t, Const) && twist!(t.dval, inds.val; inv = !inv)
146+
return (nothing, nothing)
147+
end
148+
149+
function EnzymeRules.forward(
150+
config::EnzymeRules.FwdConfigWidth{1},
151+
func::Const{typeof(twist!)},
152+
::Type{RT},
153+
t::Annotation{<:AbstractTensorMap},
154+
inds::Annotation;
155+
inv::Bool = false
156+
) where {RT}
157+
twist!(t.val, inds.val; inv)
158+
!isa(t, Const) && twist!(t.dval, inds.val; inv)
159+
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
160+
return Duplicated(t.val, t.dval)
161+
elseif EnzymeRules.needs_primal(config)
162+
return t.val
163+
elseif EnzymeRules.needs_shadow(config)
164+
return t.dval
165+
else
166+
return nothing
167+
end
168+
end
169+
119170
function EnzymeRules.augmented_primal(
120171
config::EnzymeRules.RevConfigWidth{1},
121172
func::Const{typeof(flip)},

0 commit comments

Comments
 (0)