Skip to content

Commit 7584f98

Browse files
lkdvosclaude
andcommitted
Return t from the in-place twist! wrapper to avoid an Enzyme crash on Julia 1.10
Enzyme aborts in `fixup_callconv!` when compiling the wrapper while it returned the result of the three-argument `twist!`, which also broke the fermionic contraction rules. The in-place Enzyme rules call the wrapper again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 378900a commit 7584f98

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎ext/TensorKitEnzymeExt/indexmanipulations.jl‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ function EnzymeRules.forward(
278278
end
279279

280280
# in-place twist: the source and destination are the same tensor
281-
# (calling the wrapper `twist!(t, inds)` here crashes Enzyme on Julia 1.10)
282281
function EnzymeRules.augmented_primal(
283282
config::EnzymeRules.RevConfigWidth{1},
284283
func::Const{typeof(TK.twist!)},
@@ -287,7 +286,7 @@ function EnzymeRules.augmented_primal(
287286
inds::Const;
288287
inv::Bool = false
289288
) where {RT}
290-
TK.twist!(t.val, t.val, inds.val; inv)
289+
TK.twist!(t.val, inds.val; inv)
291290
primal = EnzymeRules.needs_primal(config) ? t.val : nothing
292291
shadow = EnzymeRules.needs_shadow(config) ? t.dval : nothing
293292
return EnzymeRules.AugmentedReturn(primal, shadow, nothing)
@@ -301,7 +300,7 @@ function EnzymeRules.reverse(
301300
inds::Const;
302301
inv::Bool = false
303302
) where {RT}
304-
!isa(t, Const) && TK.twist!(t.dval, t.dval, inds.val; inv = !inv)
303+
!isa(t, Const) && TK.twist!(t.dval, inds.val; inv = !inv)
305304
return (nothing, nothing)
306305
end
307306
function EnzymeRules.forward(
@@ -312,8 +311,8 @@ function EnzymeRules.forward(
312311
inds::Annotation;
313312
inv::Bool = false
314313
) where {RT}
315-
TK.twist!(t.val, t.val, inds.val; inv)
316-
!isa(t, Const) && TK.twist!(t.dval, t.dval, inds.val; inv)
314+
TK.twist!(t.val, inds.val; inv)
315+
!isa(t, Const) && TK.twist!(t.dval, inds.val; inv)
317316
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
318317
return t
319318
elseif EnzymeRules.needs_primal(config)

‎src/tensors/indexmanipulations.jl‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ function twist!(t::AbstractTensorMap, inds; inv::Bool = false)
6969
(scalartype(t) <: Real && !(sectorscalartype(sectortype(t)) <: Real)) &&
7070
throw(ArgumentError("Can't in-place twist a real tensor with complex sector type"))
7171
has_shared_twist(t, inds) && return t
72-
return @inbounds twist!(t, t, inds, One(), Zero(); inv)
72+
# returning `t` rather than the result of `twist!` avoids an Enzyme crash on Julia 1.10
73+
@inbounds twist!(t, t, inds, One(), Zero(); inv)
74+
return t
7375
end
7476
@propagate_inbounds function twist!(
7577
tdst::AbstractTensorMap, tsrc::AbstractTensorMap, inds,

0 commit comments

Comments
 (0)