Skip to content

Commit 3ca2954

Browse files
lkdvosclaude
andcommitted
Fix spacecheck_transform ambiguity and Enzyme crash of in-place twist! on Julia 1.10
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3001d19 commit 3ca2954

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎ext/TensorKitEnzymeExt/indexmanipulations.jl‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ 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)
281282
function EnzymeRules.augmented_primal(
282283
config::EnzymeRules.RevConfigWidth{1},
283284
func::Const{typeof(TK.twist!)},
@@ -286,7 +287,7 @@ function EnzymeRules.augmented_primal(
286287
inds::Const;
287288
inv::Bool = false
288289
) where {RT}
289-
TK.twist!(t.val, inds.val; inv)
290+
TK.twist!(t.val, t.val, inds.val; inv)
290291
primal = EnzymeRules.needs_primal(config) ? t.val : nothing
291292
shadow = EnzymeRules.needs_shadow(config) ? t.dval : nothing
292293
return EnzymeRules.AugmentedReturn(primal, shadow, nothing)
@@ -300,7 +301,7 @@ function EnzymeRules.reverse(
300301
inds::Const;
301302
inv::Bool = false
302303
) where {RT}
303-
!isa(t, Const) && TK.twist!(t.dval, inds.val; inv = !inv)
304+
!isa(t, Const) && TK.twist!(t.dval, t.dval, inds.val; inv = !inv)
304305
return (nothing, nothing)
305306
end
306307
function EnzymeRules.forward(
@@ -311,8 +312,8 @@ function EnzymeRules.forward(
311312
inds::Annotation;
312313
inv::Bool = false
313314
) where {RT}
314-
TK.twist!(t.val, inds.val; inv)
315-
!isa(t, Const) && TK.twist!(t.dval, inds.val; inv)
315+
TK.twist!(t.val, t.val, inds.val; inv)
316+
!isa(t, Const) && TK.twist!(t.dval, t.dval, inds.val; inv)
316317
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
317318
return t
318319
elseif EnzymeRules.needs_primal(config)

‎src/tensors/indexmanipulations.jl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ end
559559
)
560560
return nothing
561561
end
562+
# disambiguation with the generic method
563+
spacecheck_transform(f::typeof(twist), Vdst::TensorMapSpace, Vsrc::TensorMapSpace, inds::Index2Tuple) =
564+
invoke(spacecheck_transform, Tuple{typeof(twist), TensorMapSpace, TensorMapSpace, Any}, f, Vdst, Vsrc, inds)
562565
@noinline function spacecheck_transform(::typeof(braid), Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple, levels::IndexTuple)
563566
check_spacetype(Vdst, Vsrc)
564567
braid(Vsrc, p, levels) == Vdst ||

0 commit comments

Comments
 (0)