-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NDTensorsCUDAExt] Fix bug in slicing Adjoint CuMatrix (#1236)
- Loading branch information
Showing
18 changed files
with
166 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using Test | ||
using NDTensors.Unwrap | ||
using NDTensors | ||
using LinearAlgebra | ||
|
||
include("../../../test/device_list.jl") | ||
@testset "Testing Unwrap" for dev in devices_list(ARGS) | ||
v = dev(Vector{Float64}(undef, 10)) | ||
vt = transpose(v) | ||
va = v' | ||
|
||
E = expose(v) | ||
Et = expose(vt) | ||
Ea = expose(va) | ||
v_type = typeof(v) | ||
e_type = eltype(v) | ||
@test typeof(E) == Exposed{v_type,v_type} | ||
@test typeof(Et) == Exposed{v_type,LinearAlgebra.Transpose{e_type,v_type}} | ||
@test typeof(Ea) == Exposed{v_type,LinearAlgebra.Adjoint{e_type,v_type}} | ||
|
||
@test parent(E) == v | ||
@test parent(Et) == v | ||
@test parent(Ea) == v | ||
@test transpose(E) == vt | ||
@test cpu(E) == v | ||
@test cpu(Et) == vt | ||
|
||
m = reshape(v, (5, 2)) | ||
mt = transpose(m) | ||
ma = m' | ||
E = expose(m) | ||
Et = expose(mt) | ||
Ea = expose(ma) | ||
|
||
m_type = typeof(m) | ||
@test typeof(E) == Exposed{m_type,m_type} | ||
@test typeof(Et) == Exposed{m_type,LinearAlgebra.Transpose{e_type,m_type}} | ||
@test typeof(Ea) == Exposed{m_type,LinearAlgebra.Adjoint{e_type,m_type}} | ||
|
||
o = dev(Vector{Float32})(undef, 1) | ||
expose(o)[] = 2 | ||
@test expose(o)[] == 2 | ||
|
||
fill!(m, 0) | ||
@test any(!Base.isinf, expose(m)) | ||
|
||
mp = copy(Ea) | ||
@test mp == ma | ||
fill!(ma, 2.0) | ||
copyto!(expose(mp), expose(ma)) | ||
@test mp == ma | ||
|
||
q, r = qr(expose(mp)) | ||
@test q * r ≈ mp | ||
|
||
q, r = Unwrap.qr_positive(expose(mp)) | ||
@test q * r ≈ mp | ||
|
||
square = dev(rand(Float64, (10, 10))) | ||
square = (square + transpose(square)) ./ 2.0 | ||
## CUDA only supports Hermitian or Symmetric eigen decompositions | ||
## So I symmetrize square and call symetric here | ||
l, U = eigen(expose(Symmetric(square))) | ||
@test square * U ≈ U * Diagonal(l) | ||
|
||
U, S, V, = svd(expose(mp)) | ||
@test U * Diagonal(S) * V' ≈ mp | ||
|
||
cm = dev(fill!(Matrix{Float64}(undef, (2, 2)), 0.0)) | ||
mul!(expose(cm), expose(mp), expose(mp'), 1.0, 0.0) | ||
@test cm ≈ mp * mp' | ||
|
||
@test permutedims(expose(mp), (2, 1)) == transpose(mp) | ||
fill!(mt, 3.0) | ||
permutedims!(expose(m), expose(mt), (2, 1)) | ||
@test norm(m) == sqrt(3^2 * 10) | ||
@test size(m) == (5, 2) | ||
permutedims!(expose(m), expose(mt), (2, 1), +) | ||
@test size(m) == (5, 2) | ||
@test norm(m) == sqrt(6^2 * 10) | ||
|
||
m = reshape(m, (5, 2, 1)) | ||
mt = fill!(similar(m), 3.0) | ||
m = permutedims(expose(m), (2, 1, 3)) | ||
@test size(m) == (2, 5, 1) | ||
permutedims!(expose(m), expose(mt), (2, 1, 3)) | ||
@test norm(m) == sqrt(3^2 * 10) | ||
permutedims!(expose(m), expose(mt), (2, 1, 3), -) | ||
@test norm(m) == 0 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
NDTensors/test/ITensors/TestITensorDMRG/TestITensorDMRG.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## This is getting closer still working on it. No need to review | ||
## Failing for CUDA mostly with eigen (I believe there is some noise in | ||
## eigen decomp with CUBLAS to give slightly different answer than BLAS) | ||
module TestITensorDMRG | ||
using Test | ||
using ITensors | ||
using NDTensors | ||
using Random | ||
|
||
reference_energies = Dict([ | ||
(4, -1.6160254037844384), (8, -3.374932598687889), (10, -4.258035207282885) | ||
]) | ||
|
||
default_rtol(elt::Type) = 10^(0.75 * log10(eps(real(elt)))) | ||
include("dmrg.jl") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function test_dmrg(elt, N::Integer, dev::Function) | ||
sites = siteinds("S=1/2", N) | ||
|
||
os = OpSum() | ||
for j in 1:(N - 1) | ||
os += "Sz", j, "Sz", j + 1 | ||
os += 0.5, "S+", j, "S-", j + 1 | ||
os += 0.5, "S-", j, "S+", j + 1 | ||
end | ||
|
||
Random.seed!(1234) | ||
psi0 = dev(randomMPS(Float64, sites; linkdims=4)) | ||
H = dev(MPO(elt, os, sites)) | ||
|
||
nsweeps = 3 | ||
cutoff = [1e-3, 1e-13] | ||
noise = [1e-12, 0] | ||
## running these with nsweeps = 100 and no maxdim | ||
## all problems do not have a maxlinkdim > 32 | ||
maxdim = 32 | ||
|
||
energy, psi = dmrg(H, psi0; nsweeps, cutoff, maxdim, noise, outputlevel=0) | ||
@test energy ≈ reference_energies[N] rtol = default_rtol(elt) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Test | ||
using NDTensors | ||
include("TestITensorDMRG.jl") | ||
|
||
include("../../device_list.jl") | ||
|
||
@testset "Testing DMRG different backends" begin | ||
for dev in devices_list(ARGS), | ||
N in [4, 10], | ||
elt in (Float32, ComplexF32, Float64, ComplexF64) | ||
|
||
TestITensorDMRG.test_dmrg(elt, N, dev) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include("TestITensorDMRG/runtests.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
using Test | ||
using NDTensors | ||
|
||
include(joinpath(pkgdir(NDTensors), "src", "Unwrap", "test", "runtests.jl")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters