Skip to content

Commit 49049d9

Browse files
authored
Add an adapt method for SectorVector (#610)
1 parent aad694b commit 49049d9

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎src/tensors/sectorvector.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Base.similar(v::SectorVector, ::Type{T}, V::ElementarySpace) where {T} = SectorV
4141

4242
Base.copy(v::SectorVector) = SectorVector(copy(v.data), v.structure)
4343

44+
Adapt.adapt_structure(to, v::SectorVector) = SectorVector(Adapt.adapt(to, parent(v)), v.structure)
45+
4446
# AbstractDict interface
4547
# ----------------------
4648
Base.keytype(v::SectorVector) = keytype(typeof(v))

‎test/amd/tensors.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ for V in spacelist
9797
@test domain(t2) == one(W)
9898
end
9999
end
100+
@timedtestset "Adapt SectorVector" begin
101+
for T in (Float32, ComplexF64)
102+
v = TensorKit.SectorVector{T}(undef, V1)
103+
rand!(parent(v))
104+
v_gpu = @constinferred adapt(ROCArray, v)
105+
@test v_gpu isa TensorKit.SectorVector{T, I, <:ROCArray{T}}
106+
@test v_gpu.structure == v.structure
107+
v_cpu = @constinferred adapt(Array, v_gpu)
108+
@test v_cpu isa TensorKit.SectorVector{T, I, Vector{T}}
109+
@test parent(v_cpu) == parent(v)
110+
t = AMDGPU.randn(T, V1 ⊗ V2 ← V3)
111+
s = svd_vals(t)
112+
s_cpu = adapt(Array, s)
113+
@test parent(s_cpu) == collect(parent(s))
114+
@test all(c -> block(s_cpu, c) == collect(block(s, c)), blocksectors(s))
115+
end
116+
end
100117
@timedtestset "Tensor Dict conversion" begin
101118
W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)'
102119
for T in (Int, Float32, ComplexF64)

‎test/cuda/tensors.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ for V in spacelist
115115
@test storagetype(t_cpu) <: Array{T}
116116
end
117117
end
118+
@timedtestset "Adapt SectorVector" begin
119+
for T in (Float32, ComplexF64)
120+
v = TensorKit.SectorVector{T}(undef, V1)
121+
rand!(parent(v))
122+
v_gpu = @constinferred adapt(CuArray, v)
123+
@test v_gpu isa TensorKit.SectorVector{T, I, <:CuArray{T}}
124+
@test v_gpu.structure == v.structure
125+
v_cpu = @constinferred adapt(Array, v_gpu)
126+
@test v_cpu isa TensorKit.SectorVector{T, I, Vector{T}}
127+
@test parent(v_cpu) == parent(v)
128+
t = CUDA.randn(T, V1 ⊗ V2 ← V3)
129+
s = svd_vals(t)
130+
s_cpu = adapt(Array, s)
131+
@test parent(s_cpu) == collect(parent(s))
132+
@test all(c -> block(s_cpu, c) == collect(block(s, c)), blocksectors(s))
133+
end
134+
end
118135
@timedtestset "Tensor Dict conversion" begin
119136
W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)'
120137
for T in (Int, Float32, ComplexF64)

‎test/tensors/sectorvector.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Test, TestExtras
22
using TensorKit
33
using TensorKit: SectorVector, diagonalblockstructure
4+
using Adapt: adapt
45

56
spacelist = (
67
(ℂ^4)',
@@ -29,6 +30,19 @@ spacelist = (
2930
@test length(v3) == reduceddim(V)
3031
end
3132

33+
@timedtestset "Adapt ($T)" begin
34+
v = SectorVector{T}(undef, V)
35+
rand!(parent(v))
36+
w = @testinferred adapt(Array, v)
37+
@test w isa SectorVector{T, I, Vector{T}}
38+
@test parent(w) == parent(v)
39+
@test w.structure == v.structure
40+
T′ = T <: Complex ? ComplexF64 : Float64
41+
w′ = @testinferred adapt(Array{T′}, v)
42+
@test w′ isa SectorVector{T′, I, Vector{T′}}
43+
@test parent(w′) ≈ parent(v)
44+
end
45+
3246
@timedtestset "AbstractVector interface ($T)" begin
3347
v = SectorVector{T}(undef, V)
3448
rand!(parent(v))

0 commit comments

Comments
 (0)