Skip to content

Commit 29f5a17

Browse files
lkdvosclaude
andcommitted
Support twisting DiagonalTensorMaps on GPU and test diagonal twists
GPU diagonal sources go through a cached `diagonal_treetwister`, analogous to `_braid!` and `_transpose!` for diagonal tensors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 45f0ebd commit 29f5a17

5 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
3838

3939
### Performance
4040

41-
- `twist!` and `twist` on a `TensorMap` or its adjoint use a cached tree transformer and the index manipulation kernel instead of a loop over all fusion trees, which removes the per-subblock bookkeeping and turns a twist of a GPU tensor into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
41+
- `twist!` and `twist` on a `TensorMap`, a `DiagonalTensorMap` or their adjoints use a cached tree transformer and the index manipulation kernel instead of a loop over all fusion trees, which removes the per-subblock bookkeeping and turns a twist of a GPU tensor, diagonal or not, into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
4242

4343
## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30
4444

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ function __init__()
412412
Symbol("TensorKitGPUArraysExt.GLOBAL_TREETRANSPOSER_CACHE") => GLOBAL_TREETRANSPOSER_CACHE,
413413
Symbol("TensorKitGPUArraysExt.GLOBAL_DIAGONAL_TREEBRAIDER_CACHE") => GLOBAL_DIAGONAL_TREEBRAIDER_CACHE,
414414
Symbol("TensorKitGPUArraysExt.GLOBAL_DIAGONAL_TREETRANSPOSER_CACHE") => GLOBAL_DIAGONAL_TREETRANSPOSER_CACHE,
415+
Symbol("TensorKitGPUArraysExt.GLOBAL_DIAGONAL_TREETWISTER_CACHE") => GLOBAL_DIAGONAL_TREETWISTER_CACHE,
415416
Symbol("TensorKitGPUArraysExt.GLOBAL_TREETRACER_CACHE") => GLOBAL_TREETRACER_CACHE,
416417
Symbol("TensorKitGPUArraysExt.GLOBAL_TREETWISTER_CACHE") => GLOBAL_TREETWISTER_CACHE,
417418
)
@@ -422,6 +423,7 @@ function __init__()
422423
resize!(GLOBAL_TREETRANSPOSER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
423424
resize!(GLOBAL_DIAGONAL_TREEBRAIDER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
424425
resize!(GLOBAL_DIAGONAL_TREETRANSPOSER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
426+
resize!(GLOBAL_DIAGONAL_TREETWISTER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
425427
resize!(GLOBAL_TREETRACER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
426428
resize!(GLOBAL_TREETWISTER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
427429

@@ -704,6 +706,14 @@ end
704706
return diagonal_device_transformer(A, host, dstdiagonal)
705707
end
706708

709+
@cached function diagonal_treetwister(
710+
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
711+
conjsrc::Bool, twistmask::NTuple{N, Bool}, inv::Bool, dstdiagonal::Bool
712+
) where {TA <: AnyGPUVector, N}
713+
host = TensorKit.treetwister(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, twistmask, inv)
714+
return diagonal_device_transformer(A, host, dstdiagonal)
715+
end
716+
707717
function _add_diagonal!(tdst::GPUDiagonalTensorMap, tsrc, conjsrc, transformer, α, β)
708718
_launch_unique!(tdst.data, tsrc.data, conjsrc ? conj : identity, transformer, α, β, Val(1))
709719
return tdst
@@ -739,6 +749,18 @@ Base.@propagate_inbounds function TensorKit._transpose!(
739749
return _add_diagonal!(tdst, tsrc, conjsrc, transformer, α, β)
740750
end
741751

752+
Base.@propagate_inbounds function TensorKit._twist!(
753+
tdst::Union{GPUTensorMap, GPUDiagonalTensorMap}, tsrc::GPUDiagonalTensorMap,
754+
p::Index2Tuple, conjsrc::Bool, twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator
755+
)
756+
Vdst, Vsrc = space(tdst), space(tsrc)
757+
@boundscheck TensorKit.spacecheck_transform(permute, Vdst, Vsrc, p, conjsrc)
758+
transformer = diagonal_treetwister(
759+
storagetype(tdst), Vdst, Vsrc, p, conjsrc, twistmask, inv, tdst isa DiagonalTensorMap
760+
)
761+
return _add_diagonal!(tdst, tsrc, conjsrc, transformer, α, β)
762+
end
763+
742764
function TensorKit.add_transform_kernel!(
743765
dst::AnyGPUVector, src::AnyGPUVector, p, conjsrc::Bool,
744766
device::DeviceUniqueTreeTransformer, α, β, backend, allocator, ntasks::Int

‎test/amd/tensors.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,17 @@ end
573573
for levels in ((1, 2), (2, 1))
574574
@test adapt(Array, braid(d, ((1, 2), ()), levels)) ≈ braid(dcpu, ((1, 2), ()), levels)
575575
end
576+
for inds in ((1,), (2,), (1, 2)), inv in (false, true)
577+
ref = twist(dcpu, inds; inv)
578+
@test twist(d, inds; inv) isa typeof(d)
579+
@test adapt(Array, twist(d, inds; inv)) ≈ ref
580+
@test adapt(Array, twist(d', inds; inv)) ≈ twist(dcpu', inds; inv)
581+
@test adapt(Array, twist!(copy(d), inds; inv)) ≈ ref
582+
dst = adapt(ROCVector{T}, randn(T, space(dcpu)))
583+
@test adapt(Array, twist!(copy(dst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, dst) + 2 * ref
584+
ddst = diagonal(T, V)
585+
@test adapt(Array, twist!(copy(ddst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, ddst) + 2 * ref
586+
end
576587
end
577588

578589
@testset "real source, complex destination" begin

‎test/cuda/tensors.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,17 @@ end
646646
for levels in ((1, 2), (2, 1))
647647
@test adapt(Array, braid(d, ((1, 2), ()), levels)) ≈ braid(dcpu, ((1, 2), ()), levels)
648648
end
649+
for inds in ((1,), (2,), (1, 2)), inv in (false, true)
650+
ref = twist(dcpu, inds; inv)
651+
@test twist(d, inds; inv) isa typeof(d)
652+
@test adapt(Array, twist(d, inds; inv)) ≈ ref
653+
@test adapt(Array, twist(d', inds; inv)) ≈ twist(dcpu', inds; inv)
654+
@test adapt(Array, twist!(copy(d), inds; inv)) ≈ ref
655+
dst = adapt(CuVector{T}, randn(T, space(dcpu)))
656+
@test adapt(Array, twist!(copy(dst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, dst) + 2 * ref
657+
ddst = diagonal(T, V)
658+
@test adapt(Array, twist!(copy(ddst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, ddst) + 2 * ref
659+
end
649660
end
650661

651662
@testset "real source, complex destination" begin

‎test/tensors/diagonal.jl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ diagspacelist = (
148148
@test convert(TensorMap, t5) == permute(t_tm, (((), (2, 1))))
149149
end
150150
end
151+
if BraidingStyle(I) != NoBraiding()
152+
@timedtestset "Twists" begin
153+
t = DiagonalTensorMap(randn(ComplexF64, reduceddim(V)), V)
154+
t_tm = convert(TensorMap, t)
155+
for inds in ((1,), (2,), (1, 2)), inv in (false, true)
156+
t′ = twist(t, inds; inv)
157+
@test t′ isa DiagonalTensorMap
158+
@test convert(TensorMap, t′) ≈ twist(t_tm, inds; inv)
159+
@test convert(TensorMap, twist(t', inds; inv)) ≈ twist(t_tm', inds; inv)
160+
@test twist!(copy(t), inds; inv) ≈ t′
161+
162+
# diagonal source or destination with dense counterpart
163+
tdst = randn(ComplexF64, space(t))
164+
@test twist!(copy(tdst), t, inds, 2, 3; inv) ≈ 3 * tdst + 2 * t′
165+
ddst = DiagonalTensorMap(randn(ComplexF64, reduceddim(V)), V)
166+
@test twist!(copy(ddst), t, inds, 2, 3; inv) ≈ 3 * ddst + 2 * t′
167+
@test twist!(copy(ddst), t_tm, inds, 2, 3; inv) ≈ 3 * ddst + 2 * t′
168+
end
169+
end
170+
end
151171
@timedtestset "Trace, Multiplication and inverse" begin
152172
t1 = DiagonalTensorMap(rand(Float64, reduceddim(V)), V)
153173
t2 = DiagonalTensorMap(rand(ComplexF64, reduceddim(V)), V)

0 commit comments

Comments
 (0)