@@ -116,6 +116,57 @@ function EnzymeRules.reverse(
116116 return nothing , nothing , nothing , nothing , Δαr, Δβr, map (Returns (nothing ), ba)...
117117end
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+
119170function EnzymeRules. augmented_primal (
120171 config:: EnzymeRules.RevConfigWidth{1} ,
121172 func:: Const{typeof(flip)} ,
0 commit comments