Skip to content

Commit c3b0232

Browse files
authored
Use linear scans instead of hashing for contraction labels (#190)
## Summary Does the dimname/label bookkeeping in the out-of-place contraction path with linear scans instead of the `Set`- and `Dict`-based `Base.setdiff`/`intersect`/`indexin`. For the handful of labels a tensor carries the hashing those build dominates, and on a small contraction it costs more than the matrix multiply itself. `biperms` and `contract_labels` use order-preserving `Vector` comprehensions for the output and contracted labels, and `tuple_indexin` finds label positions with `findfirst` rather than `Base.indexin`. The old check that every label appears exactly twice is replaced by a check that the destination carries exactly the uncontracted labels: the contracted and output groups partition the operands by construction, so that is the only consistency left to verify. These all assume the labels within each group are unique, which holds for a contraction. On a 4x4 contraction sharing one index, `a1 * a2` drops from 4.6 μs / 124 allocations to 1.9 μs / 60.
1 parent 546fa59 commit c3b0232

4 files changed

Lines changed: 55 additions & 16 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "TensorAlgebra"
22
uuid = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
3-
version = "0.13.0"
3+
version = "0.13.1"
44
authors = ["ITensor developers <support@itensor.org> and contributors"]
55

66
[workspace]

src/contract/biperms.jl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
# `Base.indexin` doesn't accept tuples; return the positions of `x` in `y` as a tuple.
2-
function tuple_indexin(x::Tuple, y::AbstractArray)
3-
return Tuple{Vararg{Any, length(x)}}(Base.indexin(x, y))
4-
end
5-
tuple_indexin(x::Tuple, y) = tuple_indexin(x, collect(y))
1+
# `a ∖ b` and `a ∩ b` as a `Vector`, preserving the order of `a`, via a linear
2+
# scan. For the small collections here `Base.setdiff`/`intersect` are slower
3+
# because they build a `Set` and hash. Both assume set-like (unique) inputs.
4+
smallsetdiff(a, b) = [x for x in a if x b]
5+
smallintersect(a, b) = [x for x in a if x b]
6+
7+
# Position of each element of `x` in `y`, as a tuple. Linear scan, no hashing
8+
# (`Base.indexin` builds a `Dict`), for the small collections here.
9+
tuple_indexin(x::Tuple, y) = map(v -> findfirst(==(v), y), x)
610

711
"""
812
biperm(t, t1, t2) -> (p1, p2)
@@ -30,14 +34,16 @@ length_codomain(t) = length(t) - length_domain(t)
3034

3135
# codomain <-- domain
3236
function biperms(::typeof(contract), dimnames_dest, dimnames1, dimnames2)
33-
dimnames = collect(Iterators.flatten((dimnames_dest, dimnames1, dimnames2)))
34-
for i in unique(dimnames)
35-
count(==(i), dimnames) == 2 || throw(ArgumentError("Invalid contraction labels"))
36-
end
37-
38-
codomain = Tuple(setdiff(dimnames1, dimnames2))
39-
contracted = Tuple(intersect(dimnames1, dimnames2))
40-
domain = Tuple(setdiff(dimnames2, dimnames1))
37+
codomain = Tuple(smallsetdiff(dimnames1, dimnames2))
38+
contracted = Tuple(smallintersect(dimnames1, dimnames2))
39+
domain = Tuple(smallsetdiff(dimnames2, dimnames1))
40+
41+
# `codomain`/`contracted` and `contracted`/`domain` partition the operands by
42+
# construction, so the only label consistency left to check is that the
43+
# destination carries exactly the uncontracted labels. `biperm` below then
44+
# checks each group lands in the destination.
45+
length(codomain) + length(domain) == length(dimnames_dest) ||
46+
throw(ArgumentError("Invalid contraction labels"))
4147

4248
perm_codomain_dest, perm_domain_dest = biperm(dimnames_dest, codomain, domain)
4349
invperm_dest = invperm((perm_codomain_dest..., perm_domain_dest...))

src/contract/contract_labels.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function contract_labels(a1::AbstractArray, labels1, a2::AbstractArray, labels2)
22
return contract_labels(labels1, labels2)
33
end
44
function contract_labels(labels1, labels2)
5-
diff1 = setdiff(labels1, labels2)
6-
diff2 = setdiff(labels2, labels1)
5+
diff1 = smallsetdiff(labels1, labels2)
6+
diff2 = smallsetdiff(labels2, labels1)
77
return vcat(diff1, diff2)
88
end

test/test_setoperations.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using TensorAlgebra: biperms, contract, smallintersect, smallsetdiff, tuple_indexin
2+
using Test: @test, @test_throws, @testset
3+
4+
@testset "smallsetdiff/smallintersect" begin
5+
# Order-preserving, returning a `Vector`.
6+
@test smallsetdiff((:i, :j, :k), (:k, :i)) == [:j]
7+
@test smallintersect((:i, :j, :k), (:k, :i)) == [:i, :k]
8+
@test smallsetdiff([:i, :j, :k], [:k, :i]) == [:j]
9+
@test smallintersect([:i, :j, :k], [:k, :i]) == [:i, :k]
10+
# Disjoint and empty cases.
11+
@test smallsetdiff((:i, :j), ()) == [:i, :j]
12+
@test smallintersect((:i, :j), (:k,)) == []
13+
end
14+
15+
@testset "tuple_indexin" begin
16+
# Position of each element of the first tuple in the second collection.
17+
@test tuple_indexin((:c, :b), (:a, :b, :c, :d)) == (3, 2)
18+
@test tuple_indexin((:c, :b), [:a, :b, :c, :d]) == (3, 2)
19+
@test tuple_indexin((), (:a, :b)) == ()
20+
end
21+
22+
@testset "biperms rejects an inconsistent destination" begin
23+
# The destination must carry exactly the uncontracted labels.
24+
@test_throws ArgumentError biperms(contract, (:i, :j, :k), (:i, :j), (:j, :k))
25+
@test_throws ArgumentError biperms(contract, (:i,), (:i, :j), (:j, :k))
26+
end
27+
28+
@testset "biperms with tuple and vector labels agree" begin
29+
# A representative multi-index contraction: C_il = A_ijk B_kjl.
30+
bp = biperms(contract, (:i, :l), (:i, :j, :k), (:k, :j, :l))
31+
@test biperms(contract, [:i, :l], (:i, :j, :k), (:k, :j, :l)) == bp
32+
@test biperms(contract, (:i, :l), [:i, :j, :k], [:k, :j, :l]) == bp
33+
end

0 commit comments

Comments
 (0)