Skip to content

Commit

Permalink
Merge branch 'main' into kmp5/feature/JLArrays_extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kmp5VT authored Sep 4, 2024
2 parents 094c3e3 + 988e216 commit b751d64
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 82 deletions.
4 changes: 2 additions & 2 deletions NDTensors/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NDTensors"
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
authors = ["Matthew Fishman <[email protected]>"]
version = "0.3.39"
version = "0.3.42"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down Expand Up @@ -56,7 +56,7 @@ NDTensorsTBLISExt = "TBLIS"
NDTensorscuTENSORExt = "cuTENSOR"

[compat]
AMDGPU = "0.9"
AMDGPU = "0.9, 1"
Accessors = "0.1.33"
Adapt = "3.7, 4"
ArrayLayouts = "1.4"
Expand Down
3 changes: 2 additions & 1 deletion NDTensors/src/diag/diagtensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ setdiag(T::UniformDiagTensor, val) = tensor(Diag(val), inds(T))
end
end
@propagate_inbounds getindex(T::DiagTensor{<:Number,1}, ind::Int) = storage(T)[ind]
@propagate_inbounds getindex(T::DiagTensor{<:Number,0}) = storage(T)[1]
using NDTensors.Expose: expose
@propagate_inbounds getindex(T::DiagTensor{<:Number,0}) = getindex(expose(storage(T)))

# Set diagonal elements
# Throw error for off-diagonal
Expand Down
141 changes: 68 additions & 73 deletions NDTensors/src/lib/TensorAlgebra/test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,83 +122,78 @@ end
@test eltype(a_split) === elt
@test a_split reshape(a, (2, 3, 20))
end
## Right now TensorOperations version is downgraded when using cuTENSOR to `v0.7` we
## are waiting for TensorOperations to support the breaking changes in cuTENSOR 2.x
if !("cutensor" ARGS)
using TensorOperations: TensorOperations
@testset "contract (eltype1=$elt1, eltype2=$elt2)" for elt1 in elts, elt2 in elts
dims = (2, 3, 4, 5, 6, 7, 8, 9, 10)
labels = (:a, :b, :c, :d, :e, :f, :g, :h, :i)
for (d1s, d2s, d_dests) in (
((1, 2), (1, 2), ()),
((1, 2), (2, 1), ()),
((1, 2), (2, 1, 3), (3,)),
((1, 2, 3), (2, 1), (3,)),
((1, 2), (2, 3), (1, 3)),
((1, 2), (2, 3), (3, 1)),
((2, 1), (2, 3), (3, 1)),
((1, 2, 3), (2, 3, 4), (1, 4)),
((1, 2, 3), (2, 3, 4), (4, 1)),
((3, 2, 1), (4, 2, 3), (4, 1)),
((1, 2, 3), (3, 4), (1, 2, 4)),
((1, 2, 3), (3, 4), (4, 1, 2)),
((1, 2, 3), (3, 4), (2, 4, 1)),
((3, 1, 2), (3, 4), (2, 4, 1)),
((3, 2, 1), (4, 3), (2, 4, 1)),
((1, 2, 3, 4, 5, 6), (4, 5, 6, 7, 8, 9), (1, 2, 3, 7, 8, 9)),
((2, 4, 5, 1, 6, 3), (6, 4, 9, 8, 5, 7), (1, 7, 2, 8, 3, 9)),
)
a1 = randn(elt1, map(i -> dims[i], d1s))
labels1 = map(i -> labels[i], d1s)
a2 = randn(elt2, map(i -> dims[i], d2s))
labels2 = map(i -> labels[i], d2s)
labels_dest = map(i -> labels[i], d_dests)
using TensorOperations: TensorOperations
@testset "contract (eltype1=$elt1, eltype2=$elt2)" for elt1 in elts, elt2 in elts
dims = (2, 3, 4, 5, 6, 7, 8, 9, 10)
labels = (:a, :b, :c, :d, :e, :f, :g, :h, :i)
for (d1s, d2s, d_dests) in (
((1, 2), (1, 2), ()),
((1, 2), (2, 1), ()),
((1, 2), (2, 1, 3), (3,)),
((1, 2, 3), (2, 1), (3,)),
((1, 2), (2, 3), (1, 3)),
((1, 2), (2, 3), (3, 1)),
((2, 1), (2, 3), (3, 1)),
((1, 2, 3), (2, 3, 4), (1, 4)),
((1, 2, 3), (2, 3, 4), (4, 1)),
((3, 2, 1), (4, 2, 3), (4, 1)),
((1, 2, 3), (3, 4), (1, 2, 4)),
((1, 2, 3), (3, 4), (4, 1, 2)),
((1, 2, 3), (3, 4), (2, 4, 1)),
((3, 1, 2), (3, 4), (2, 4, 1)),
((3, 2, 1), (4, 3), (2, 4, 1)),
((1, 2, 3, 4, 5, 6), (4, 5, 6, 7, 8, 9), (1, 2, 3, 7, 8, 9)),
((2, 4, 5, 1, 6, 3), (6, 4, 9, 8, 5, 7), (1, 7, 2, 8, 3, 9)),
)
a1 = randn(elt1, map(i -> dims[i], d1s))
labels1 = map(i -> labels[i], d1s)
a2 = randn(elt2, map(i -> dims[i], d2s))
labels2 = map(i -> labels[i], d2s)
labels_dest = map(i -> labels[i], d_dests)

# Don't specify destination labels
a_dest, labels_dest′ = TensorAlgebra.contract(a1, labels1, a2, labels2)
a_dest_tensoroperations = TensorOperations.tensorcontract(
labels_dest′, a1, labels1, a2, labels2
)
@test a_dest a_dest_tensoroperations
# Don't specify destination labels
a_dest, labels_dest′ = TensorAlgebra.contract(a1, labels1, a2, labels2)
a_dest_tensoroperations = TensorOperations.tensorcontract(
labels_dest′, a1, labels1, a2, labels2
)
@test a_dest a_dest_tensoroperations

# Specify destination labels
a_dest = TensorAlgebra.contract(labels_dest, a1, labels1, a2, labels2)
a_dest_tensoroperations = TensorOperations.tensorcontract(
labels_dest, a1, labels1, a2, labels2
)
@test a_dest a_dest_tensoroperations
# Specify destination labels
a_dest = TensorAlgebra.contract(labels_dest, a1, labels1, a2, labels2)
a_dest_tensoroperations = TensorOperations.tensorcontract(
labels_dest, a1, labels1, a2, labels2
)
@test a_dest a_dest_tensoroperations

# Specify α and β
elt_dest = promote_type(elt1, elt2)
# TODO: Using random `α`, `β` causing
# random test failures, investigate why.
α = elt_dest(1.2) # randn(elt_dest)
β = elt_dest(2.4) # randn(elt_dest)
a_dest_init = randn(elt_dest, map(i -> dims[i], d_dests))
a_dest = copy(a_dest_init)
TensorAlgebra.contract!(a_dest, labels_dest, a1, labels1, a2, labels2, α, β)
a_dest_tensoroperations = TensorOperations.tensorcontract(
labels_dest, a1, labels1, a2, labels2
)
## Here we loosened the tolerance because of some floating point roundoff issue.
## with Float32 numbers
@test a_dest α * a_dest_tensoroperations + β * a_dest_init rtol =
50 * default_rtol(elt_dest)
end
# Specify α and β
elt_dest = promote_type(elt1, elt2)
# TODO: Using random `α`, `β` causing
# random test failures, investigate why.
α = elt_dest(1.2) # randn(elt_dest)
β = elt_dest(2.4) # randn(elt_dest)
a_dest_init = randn(elt_dest, map(i -> dims[i], d_dests))
a_dest = copy(a_dest_init)
TensorAlgebra.contract!(a_dest, labels_dest, a1, labels1, a2, labels2, α, β)
a_dest_tensoroperations = TensorOperations.tensorcontract(
labels_dest, a1, labels1, a2, labels2
)
## Here we loosened the tolerance because of some floating point roundoff issue.
## with Float32 numbers
@test a_dest α * a_dest_tensoroperations + β * a_dest_init rtol =
50 * default_rtol(elt_dest)
end
end
@testset "qr (eltype=$elt)" for elt in elts
a = randn(elt, 5, 4, 3, 2)
labels_a = (:a, :b, :c, :d)
labels_q = (:b, :a)
labels_r = (:d, :c)
q, r = qr(a, labels_a, labels_q, labels_r)
label_qr = :qr
a′ = TensorAlgebra.contract(
labels_a, q, (labels_q..., label_qr), r, (label_qr, labels_r...)
)
@test a a′
end
end

@testset "qr (eltype=$elt)" for elt in elts
a = randn(elt, 5, 4, 3, 2)
labels_a = (:a, :b, :c, :d)
labels_q = (:b, :a)
labels_r = (:d, :c)
q, r = qr(a, labels_a, labels_q, labels_r)
label_qr = :qr
a′ = TensorAlgebra.contract(
labels_a, q, (labels_q..., label_qr), r, (label_qr, labels_r...)
)
@test a a′
end
end
3 changes: 0 additions & 3 deletions NDTensors/test/NDTensorsTestUtils/device_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ if "metal" in ARGS || "all" in ARGS
using Metal
end
if "cutensor" in ARGS || "all" in ARGS
if in("TensorOperations", map(v -> v.name, values(Pkg.dependencies())))
Pkg.rm("TensorOperations")
end
Pkg.add("cuTENSOR")
Pkg.add("CUDA")
using CUDA, cuTENSOR
Expand Down
2 changes: 1 addition & 1 deletion NDTensors/test/test_diag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ using .NDTensorsTestUtils: devices_list, is_supported_eltype
@test x == dev(diagm(0 => vr))
@test x == D
end

@test sqrt(contract(D, (-1, -2), conj(D), (-1, -2))[]) norm(D)
# This if statement corresponds to the reported bug:
# https://github.com/JuliaGPU/Metal.jl/issues/364
if !(dev == NDTensors.mtl && elt === ComplexF32)
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensors"
uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5"
authors = ["Matthew Fishman <[email protected]>", "Miles Stoudenmire <[email protected]>"]
version = "0.6.16"
version = "0.6.17"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ITensorMPS/src/mpo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ end
"""
error_contract(y::MPS, A::MPO, x::MPS;
make_inds_match::Bool = true)
error_contract(y::MPS, x::MPS, x::MPO;
error_contract(y::MPS, x::MPS, A::MPO;
make_inds_match::Bool = true)
Compute the distance between A|x> and an approximation MPS y:
Expand Down

0 comments on commit b751d64

Please sign in to comment.