Skip to content

Commit eb05b83

Browse files
committed
Keep normalizations of GPUTensorMaps from synchronizing
1 parent 9458d76 commit eb05b83

5 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎ext/TensorKitCUDAExt/TensorKitCUDAExt.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ using Random
1919

2020
include("cutensormap.jl")
2121

22+
function TensorKit._nrm2!(
23+
out::CuVector{R}, x::cuBLAS.StridedCuVecOrDenseMat{T}
24+
) where {R <: Union{Float32, Float64}, T <: Union{R, Complex{R}}}
25+
cuBLAS.nrm2(x, out)
26+
return out
27+
end
28+
2229
end

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module TensorKitGPUArraysExt
22

33
using GPUArrays
4+
using LinearAlgebra: LinearAlgebra
45
using GPUArrays: @allowscalar
56
using GPUArrays.KernelAbstractions: @kernel, @index, get_backend
67
using Adapt
@@ -653,6 +654,46 @@ TensorKit._transform_subblocks(
653654
const GPUDiagonalTensorMap = DiagonalTensorMap{<:Any, <:Any, <:AnyGPUVector}
654655
const GPUTensorMap = TensorMap{<:Any, <:Any, <:Any, <:Any, <:AnyGPUVector}
655656

657+
# `norm` returns a host scalar by default, which forces the GPU to wait and synchronize.
658+
# Here we add some methods for various `p` that avoid copying back to the host if we
659+
# don't need to, e.g. for normalization. For the 2-norm, the norm is instead kept in a
660+
# 1-element device array and broadcast into the data. This matches the vendor device-side
661+
# nrm2 versions.
662+
function TensorKit._nrm2!(out::AnyGPUVector, x::AnyGPUArray)
663+
out .= sqrt.(vec(sum(abs2, x; dims = ntuple(identity, ndims(x)))))
664+
return out
665+
end
666+
TensorKit._nrm2!(out::AnyGPUVector, x::LinearAlgebra.Diagonal) = TensorKit._nrm2!(out, x.diag)
667+
668+
function _device_norm2(t::Union{GPUTensorMap, GPUDiagonalTensorMap})
669+
InnerProductStyle(t) === EuclideanInnerProduct() || TensorKit.throw_invalid_innerproduct(:norm)
670+
out = similar(t.data, float(real(scalartype(t))), 1)
671+
FusionStyle(sectortype(t)) isa UniqueFusion && return TensorKit._nrm2!(out, t.data)
672+
fill!(out, zero(eltype(out)))
673+
nb = similar(out)
674+
for (c, b) in blocks(t)
675+
out .+= dim(c) .* abs2.(TensorKit._nrm2!(nb, b))
676+
end
677+
return out .= sqrt.(out)
678+
end
679+
680+
function LinearAlgebra.normalize!(t::Union{GPUTensorMap, GPUDiagonalTensorMap}, p::Real = 2)
681+
p == 2 || return scale!(t, inv(norm(t, p)))
682+
t.data ./= _device_norm2(t)
683+
return t
684+
end
685+
function LinearAlgebra.normalize(t::Union{GPUTensorMap, GPUDiagonalTensorMap}, p::Real = 2)
686+
p == 2 || return scale(t, inv(norm(t, p)))
687+
tdst = similar(t, float(scalartype(t)))
688+
tdst.data .= t.data ./ _device_norm2(t)
689+
return tdst
690+
end
691+
function LinearAlgebra.normalize!(t::AdjointTensorMap{<:Any, <:Any, <:Any, <:Any, <:GPUTensorMap}, p::Real = 2)
692+
normalize!(parent(t), p)
693+
return t
694+
end
695+
LinearAlgebra.normalize(t::AdjointTensorMap{<:Any, <:Any, <:Any, <:Any, <:GPUTensorMap}, p::Real = 2) = adjoint(normalize(parent(t), p))
696+
656697
# position of each subblock in the data of a `DiagonalTensorMap` in the same space
657698
_diagonal_offsets(structure) = first(_work_offsets(first(first(s)) for s in structure))
658699

‎src/tensors/linalg.jl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ Base.:\(α::Number, t::AbstractTensorMap) = *(t, one(scalartype(t)) / α)
1818
LinearAlgebra.normalize!(t::AbstractTensorMap, p::Real = 2) = scale!(t, inv(norm(t, p)))
1919
LinearAlgebra.normalize(t::AbstractTensorMap, p::Real = 2) = scale(t, inv(norm(t, p)))
2020

21+
"""
22+
_nrm2!(out, x)
23+
24+
Write the 2-norm of the array `x` into the 1-element array `out`, without copying it to the host.
25+
Used to normalize GPU-backed tensors without synchronizing; the GPU extensions provide methods.
26+
"""
27+
function _nrm2! end
28+
2129
# destination allocation for matrix multiplication
2230
# note that we don't fall back to `tensoralloc_contract` since that needs to account for
2331
# permutations, which might require complex scalartypes even if the inputs are real.

‎test/amd/tensors.jl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ for V in spacelist
153153
p = 3 * rand(Float64)
154154
@test norm(t + t, p) ≈ 2 * norm(t, p)
155155
@test norm(t) ≈ norm(t')
156+
for p in (1, 2, 3 * rand(Float64), Inf)
157+
tn = @constinferred normalize(t, p)
158+
@test storagetype(tn) == storagetype(t)
159+
@test norm(tn, p) ≈ 1
160+
@test tn ≈ t / norm(t, p)
161+
@test normalize!(copy(t), p) ≈ tn
162+
@test normalize(t', p) ≈ tn'
163+
ta = copy(t)'
164+
@test normalize!(ta, p) === ta
165+
@test ta ≈ tn'
166+
end
156167

157168
t2 = @constinferred rand!(similar(t))
158169
β = rand(T)
@@ -566,6 +577,13 @@ end
566577
dcpu = adapt(Array, d)
567578
scalars = ((TensorKit.One(), TensorKit.Zero()), (T(2), T(3)))
568579

580+
for p in (1, 2, 3 * rand(Float64), Inf)
581+
dn = @constinferred normalize(d, p)
582+
@test dn isa typeof(d)
583+
@test adapt(Array, dn) ≈ normalize(dcpu, p)
584+
@test adapt(Array, normalize!(copy(d), p)) ≈ normalize(dcpu, p)
585+
end
586+
569587
for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((), (2, 1)), ((1,), (2,)), ((2,), (1,))), conjd in (false, true)
570588
W = TO.tensoradd_structure(dcpu, p, conjd)
571589
dst = adapt(ROCVector{T}, randn(T, W))

‎test/cuda/tensors.jl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ for V in spacelist
172172
p = 3 * rand(Float64)
173173
@test norm(t + t, p) ≈ 2 * norm(t, p)
174174
@test norm(t) ≈ norm(t')
175+
for p in (1, 2, 3 * rand(Float64), Inf)
176+
tn = @constinferred normalize(t, p)
177+
@test storagetype(tn) == storagetype(t)
178+
@test norm(tn, p) ≈ 1
179+
@test tn ≈ t / norm(t, p)
180+
@test normalize!(copy(t), p) ≈ tn
181+
@test normalize(t', p) ≈ tn'
182+
ta = copy(t)'
183+
@test normalize!(ta, p) === ta
184+
@test ta ≈ tn'
185+
end
175186

176187
t2 = @constinferred rand!(similar(t))
177188
β = rand(T)
@@ -639,6 +650,13 @@ end
639650
dcpu = adapt(Array, d)
640651
scalars = ((TensorKit.One(), TensorKit.Zero()), (T(2), T(3)))
641652

653+
for p in (1, 2, 3 * rand(Float64), Inf)
654+
dn = @constinferred normalize(d, p)
655+
@test dn isa typeof(d)
656+
@test adapt(Array, dn) ≈ normalize(dcpu, p)
657+
@test adapt(Array, normalize!(copy(d), p)) ≈ normalize(dcpu, p)
658+
end
659+
642660
for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((), (2, 1)), ((1,), (2,)), ((2,), (1,))), conjd in (false, true)
643661
W = TO.tensoradd_structure(dcpu, p, conjd)
644662
dst = adapt(CuVector{T}, randn(T, W))

0 commit comments

Comments
 (0)