Skip to content

Commit 56c7610

Browse files
lkdvosclaude
andcommitted
Fix thread-parity bug in the benchmark harness and re-measure
The harness ran with a single Julia thread, which left Strided.jl serial while TBLIS used its own 16-thread pool, inflating every TBLIS number. Give Strided the same thread count and refuse to run when Julia was not started with enough threads. With matched threads TBLIS is no longer a throughput win on this machine: 1.19x on a permuted real contraction, 1.35x on a permuted add, and slower than the built-in backends elsewhere -- the earlier 9x add and 23x trace figures were entirely the thread mismatch. What survives is that TBLIS allocates no permuted temporaries where StridedBLAS allocates 76.6 MiB for the same contraction. Docstring and docs adjusted accordingly. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 2b90f7c commit 56c7610

4 files changed

Lines changed: 71 additions & 40 deletions

File tree

‎benchmarks/README.md‎

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,75 @@
33
## `tblis_backend.jl`
44

55
Compares `TensorOperations.TBLISBackend()` against the built-in `StridedBLAS()` and
6-
`StridedNative()` backends. BLAS and TBLIS are handed the same number of threads.
6+
`StridedNative()` backends.
77

88
```console
99
$ julia --project=benchmarks -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate()'
10-
$ julia --project=benchmarks benchmarks/tblis_backend.jl 16
10+
$ julia --project=benchmarks --threads=16 benchmarks/tblis_backend.jl 16
1111
```
1212

13-
The optional argument is the thread count, defaulting to half of `Sys.CPU_THREADS`.
13+
The argument is the thread count handed to BLAS, TBLIS and Strided alike. `Strided.jl`
14+
parallelizes over Julia's own threads, so the process has to be *started* with `--threads`;
15+
the script refuses to run if `Threads.nthreads()` is below the requested count, because
16+
otherwise a threaded TBLIS is timed against a serial Strided and wins by several-fold for
17+
reasons that have nothing to do with the backends.
1418

1519
### Reference numbers
1620

17-
2× Intel Xeon Gold 6244 (16 physical cores total), 16 threads for both OpenBLAS and TBLIS,
18-
Julia 1.12.6, TensorOperations v5.7.0, TBLIS.jl v0.3.0, `tblis_jll` v1.3.0. Best of two runs
19-
on a shared node; the TBLIS timings were stable to a few percent, the BLAS baselines drift by
20-
up to ~20%.
21+
2× Intel Xeon Gold 6244 (16 physical cores total), 16 threads for OpenBLAS, TBLIS and
22+
Strided, Julia 1.12.6, TensorOperations v5.7.0, TBLIS.jl v0.3.0, `tblis_jll` v1.3.0. Best of
23+
three runs per cell on a shared node.
2124

2225
`eltype = Float64`
2326

2427
| case | StridedBLAS | StridedNative | TBLIS | speedup |
2528
| :------------------------------------------ | ----------: | ------------: | -------: | ------: |
26-
| matmul 2000³ | 25.41 ms | 5.52 s | 46.47 ms | 0.55× |
27-
| contract aligned (48,48,1024)×(1024,48,48) | 20.19 ms | 4.25 s | 30.04 ms | 0.67× |
28-
| contract permuted (48,1024,48)×(48,1024,48) | 58.51 ms | 4.13 s | 29.28 ms | 2.00× |
29-
| MPS·MPO·env (χ=256, d=4, D=8) | 22.87 ms | 836.49 ms | 12.38 ms | 1.85× |
30-
| permuted add 200³ | 41.05 ms | 41.30 ms | 4.81 ms | 8.53× |
31-
| partial trace (128,32,128,32) | 288.0 μs | 287.7 μs | 87.0 μs | 3.31× |
32-
| contract small 8³×8³ | 12.6 μs | 38.3 μs | 56.6 μs | 0.22× |
29+
| matmul 2000³ | 29.22 ms | 759.64 ms | 45.85 ms | 0.64× |
30+
| contract aligned (48,48,1024)×(1024,48,48) | 21.90 ms | 373.54 ms | 29.80 ms | 0.73× |
31+
| contract permuted (48,1024,48)×(48,1024,48) | 34.33 ms | 583.81 ms | 28.84 ms | 1.19× |
32+
| MPS·MPO·env (χ=256, d=4, D=8) | 11.95 ms | 419.41 ms | 12.71 ms | 0.94× |
33+
| permuted add 200³ | 5.68 ms | 5.75 ms | 4.22 ms | 1.35× |
34+
| partial trace (128,32,128,32) | 51.1 μs | 49.4 μs | 88.5 μs | 0.56× |
35+
| contract small 8³×8³ | 13.6 μs | 38.3 μs | 57.3 μs | 0.24× |
3336

3437
`eltype = ComplexF64`
3538

3639
| case | StridedBLAS | StridedNative | TBLIS | speedup |
3740
| :-------------------------------------------- | ----------: | ------------: | --------: | ------: |
38-
| matmul 2000³ | 116.22 ms | 16.49 s | 2.39 s | 0.05× |
39-
| contract aligned (48,48,1024)×(1024,48,48) | 80.87 ms | 12.38 s | 1.75 s | 0.05× |
40-
| contract permuted (48,1024,48)×(48,1024,48) | 187.10 ms | 11.73 s | 1.58 s | 0.12× |
41-
| MPS·MPO·env (χ=256, d=4, D=8) | 48.23 ms | 2.43 s | 374.83 ms | 0.13× |
42-
| permuted add 200³ | 53.49 ms | 54.06 ms | 10.25 ms | 5.22× |
43-
| partial trace (128,32,128,32) | 2.46 ms | 2.47 ms | 107.0 μs | 23.0× |
44-
| contract small 8³×8³ | 24.6 μs | 87.0 μs | 65.1 μs | 0.38× |
45-
| contract conj(A) (48,1024,48)×(48,1024,48) | 186.47 ms | 11.75 s | 1.68 s | 0.11× |
46-
| contract conj(A)conj(B) (48,1024,48)×(48,1024,48) | 207.53 ms | 12.39 s | 1.16 s | 0.18× |
41+
| matmul 2000³ | 117.87 ms | 2.23 s | 2.59 s | 0.05× |
42+
| contract aligned (48,48,1024)×(1024,48,48) | 77.46 ms | 1.19 s | 1.81 s | 0.04× |
43+
| contract permuted (48,1024,48)×(48,1024,48) | 114.58 ms | 1.59 s | 1.77 s | 0.06× |
44+
| MPS·MPO·env (χ=256, d=4, D=8) | 40.40 ms | 1.22 s | 379.67 ms | 0.11× |
45+
| permuted add 200³ | 10.48 ms | 10.84 ms | 12.75 ms | 0.82× |
46+
| partial trace (128,32,128,32) | 237.3 μs | 232.1 μs | 112.4 μs | 2.06× |
47+
| contract small 8³×8³ | 25.2 μs | 87.0 μs | 68.0 μs | 0.37× |
48+
| contract conj(A) (48,1024,48)×(48,1024,48) | 110.37 ms | 1.54 s | 1.77 s | 0.06× |
49+
| contract conj(A)conj(B) (48,1024,48)×(48,1024,48) | 124.44 ms | 1.63 s | 1.76 s | 0.07× |
4750

4851
`speedup` is the fastest built-in backend divided by TBLIS, so values above 1 mean TBLIS wins.
4952

5053
### Reading the numbers
5154

52-
* Contractions that need a **permutation** before they can be handed to GEMM — the common
53-
case in tensor-network codes — are about **2× faster** with TBLIS, which contracts the
54-
strided memory directly instead of materializing permuted copies.
55-
* **Additions and traces** are where TBLIS is furthest ahead (3–23×), since `Strided.jl`
56-
loses to a blocked, threaded copy for these memory-bound shapes.
57-
* Plain **GEMM-shaped** contractions stay OpenBLAS territory (TBLIS at 0.5–0.7×), and **small
58-
tensors** are dominated by the per-call overhead of building the TBLIS descriptors.
55+
**On this machine TBLIS is not a throughput win.** Once all three thread pools are matched,
56+
the built-in backends are at least as fast almost everywhere: 0.64× on GEMM, 1.19× on a
57+
permutation-heavy real contraction, 0.94× on a DMRG-style network, 0.56× on a partial trace.
58+
The only clear wins are the real permuted add (1.35×) and the complex partial trace (2.06×).
59+
60+
What TBLIS does buy is **memory**. For the permuted contraction above, `StridedBLAS` allocates
61+
76.6 MiB of permuted temporaries per call (against 18 MiB inputs and a 40.5 MiB output) while
62+
TBLIS allocates nothing at all, because it contracts the strided memory in place. For
63+
workloads that are allocation- or footprint-bound rather than FLOP-bound, that can matter more
64+
than the wall-clock column.
65+
66+
Two further caveats:
67+
5968
* **Complex contractions are 8–20× slower than BLAS.** This is a property of the `tblis`
6069
binaries, not of this extension: a bare `tblis_tensor_mult` on a 1000³ complex matrix
6170
product reaches ~25 GFLOP/s against ~500 GFLOP/s for `mul!`, and `tblis_jll` v1.2.0 behaves
62-
identically, so it is not a regression from the v1.3 bump. Complex `add`/`trace` are
63-
unaffected. Prefer `StridedBLAS` for complex-valued contractions.
71+
identically, so it is not a regression from the v1.3 bump.
72+
* **Small tensors** are dominated by the per-call cost of building the TBLIS descriptors.
73+
74+
Earlier revisions of this file reported speedups of up to 9× for the add and trace cases.
75+
Those were an artifact of running the harness with a single Julia thread, which left
76+
`Strided.jl` serial while TBLIS used all 16 cores; single-threaded, the permuted add takes
77+
46.80 ms with TBLIS against 45.79 ms with Strided.

‎benchmarks/tblis_backend.jl‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# Compare the `TBLISBackend` against the built-in strided backends on a handful of
22
# representative workloads.
33
#
4-
# julia --project=benchmarks benchmarks/tblis_backend.jl [nthreads]
4+
# julia --project=benchmarks --threads=16 benchmarks/tblis_backend.jl 16
55
#
6-
# BLAS and TBLIS are given the same number of threads, so that the comparison is about the
7-
# algorithms rather than about the thread pools.
6+
# BLAS, TBLIS and Strided are all given the same number of threads, so that the comparison is
7+
# about the algorithms rather than about the thread pools. Strided.jl parallelizes over
8+
# Julia's own threads, so the process has to be *started* with `--threads`: running this with
9+
# the default single Julia thread pits a threaded TBLIS against a serial Strided and
10+
# overstates the TBLIS numbers several-fold.
811

912
using TensorOperations
1013
using TensorOperations: TBLISBackend, StridedBLAS, StridedNative
@@ -14,10 +17,21 @@ using LinearAlgebra
1417
using Printf
1518
using Random
1619

20+
const Strided = TensorOperations.Strided
21+
1722
const NTHREADS = length(ARGS) ≥ 1 ? parse(Int, ARGS[1]) : max(1, Sys.CPU_THREADS ÷ 2)
1823

24+
Threads.nthreads() ≥ NTHREADS || error(
25+
"""
26+
Julia was started with $(Threads.nthreads()) thread(s) but the benchmark was asked for
27+
$NTHREADS. Strided.jl can then only use $(Threads.nthreads()) of them, which makes the
28+
comparison against TBLIS meaningless. Restart with `julia --threads=$NTHREADS`.
29+
"""
30+
)
31+
1932
BLAS.set_num_threads(NTHREADS)
2033
TBLIS.set_num_threads(NTHREADS)
34+
Strided.set_num_threads(NTHREADS)
2135

2236
const BACKENDS = (
2337
"StridedBLAS" => StridedBLAS(),
@@ -230,7 +244,7 @@ end
230244
#-------------------------------------------------------------------------------------------
231245
println(
232246
"threads: BLAS = $(BLAS.get_num_threads()), TBLIS = $(TBLIS.get_num_threads()), ",
233-
"julia = $(Threads.nthreads())"
247+
"Strided = $(Strided.get_num_threads()), julia = $(Threads.nthreads())"
234248
)
235249
println("BLAS: ", BLAS.get_config())
236250

‎docs/src/man/backends.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ TBLIS.set_num_threads(8)
8686

8787
Because TBLIS requires all tensors in a single operation to share one element type out of `Float32`, `Float64`, `ComplexF32` and `ComplexF64`, operations that do not satisfy this -- as well as non-strided arrays -- are delegated to the backend that `select_backend` would have chosen, so that the backend can be set once for a whole `@tensor` block.
8888

89+
Note that contracting in place is a trade of throughput for memory rather than a free win: with BLAS, TBLIS and `Strided.jl` all given the same number of threads, this backend is roughly on par with `StridedBLAS` for permuted real contractions and slower for most other shapes, while allocating no permuted temporaries at all. Complex contractions are an order of magnitude slower still, a limitation of the `tblis` binaries themselves. See `benchmarks/README.md` for measurements.
90+
8991
Finally, we also provide a `cuTENSORBackend` for use with the `cuTENSOR.jl` library, which is a NVidia GPU-accelerated tensor contraction library.
9092
This backend is only available through a package extension for `cuTENSOR`.
9193

‎src/backends.jl‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ meet these requirements are silently delegated to the backend that
8888
[`select_backend`](@ref TensorOperations.select_backend) would have picked.
8989
9090
!!! note
91-
The contraction kernels shipped by `tblis_jll` are an order of magnitude slower than BLAS
92-
for complex element types, so this backend pays off mostly for real contractions that
93-
require a permutation, and for additions and traces of any element type. See
94-
`benchmarks/README.md` for measurements.
91+
Contracting in place trades throughput for memory. On a 16-core node this backend is
92+
roughly on par with [`StridedBLAS`](@ref TensorOperations.StridedBLAS) for permuted real
93+
contractions and slower elsewhere, but it allocates no permuted temporaries at all. The
94+
contraction kernels shipped by `tblis_jll` are moreover an order of magnitude slower than
95+
BLAS for complex element types. See `benchmarks/README.md` for measurements.
9596
"""
9697
struct TBLISBackend <: AbstractBackend end
9798

0 commit comments

Comments
 (0)