Skip to content

Commit 357daa7

Browse files
lkdvosclaude
andcommitted
Build the TBLIS descriptors through the low-level bindings
TBLIS.jl v0.3 regenerated its C bindings, so `tblis_tensor` is now an opaque 64-byte blob with pointer-based accessors rather than a struct with individual fields, and its high-level constructor only accepts `StridedArray`. Initialize the descriptor with `tblis_init_tensor_scaled_*` instead -- which works for any `StridedView` -- patch in the conjugation flag through the generated `setproperty!`, and hand the resulting `Ref` to the low-level `tblis_tensor_add` and `tblis_tensor_mult`. Benchmark numbers re-measured against the same release candidate. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 9d349c9 commit 357daa7

2 files changed

Lines changed: 53 additions & 45 deletions

File tree

‎benchmarks/README.md‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ Julia 1.12.6, `tblis_jll` v1.3.0.
2121

2222
| case | StridedBLAS | StridedNative | TBLIS | speedup |
2323
| :----------------------------------------- | ----------: | ------------: | -------: | ------: |
24-
| matmul 2000³ | 34.25 ms | 4.64 s | 49.22 ms | 0.70× |
25-
| contract aligned (48,48,1024)×(1024,48,48) | 23.56 ms | 3.76 s | 33.13 ms | 0.71× |
26-
| contract permuted (48,1024,48)×(48,1024,48) | 61.15 ms | 3.57 s | 31.42 ms | 1.95× |
27-
| MPS·MPO·env (χ=256, d=4, D=8) | 25.88 ms | 698.69 ms | 13.87 ms | 1.87× |
28-
| permuted add 200³ | 38.83 ms | 38.44 ms | 4.15 ms | 9.26× |
29-
| partial trace (128,32,128,32) | 280.4 μs | 266.4 μs | 99.7 μs | 2.67× |
30-
| contract small 8³×8³ | 17.1 μs | 40.5 μs | 56.5 μs | 0.30× |
24+
| matmul 2000³ | 33.65 ms | 4.44 s | 44.56 ms | 0.76× |
25+
| contract aligned (48,48,1024)×(1024,48,48) | 20.86 ms | 3.61 s | 29.15 ms | 0.72× |
26+
| contract permuted (48,1024,48)×(48,1024,48) | 56.59 ms | 3.49 s | 27.76 ms | 2.04× |
27+
| MPS·MPO·env (χ=256, d=4, D=8) | 22.59 ms | 685.24 ms | 11.61 ms | 1.95× |
28+
| permuted add 200³ | 35.50 ms | 35.47 ms | 3.83 ms | 9.27× |
29+
| partial trace (128,32,128,32) | 270.4 μs | 270.8 μs | 84.8 μs | 3.19× |
30+
| contract small 8³×8³ | 13.8 μs | 36.6 μs | 58.1 μs | 0.24× |
3131

3232
`eltype = ComplexF64`
3333

3434
| case | StridedBLAS | StridedNative | TBLIS | speedup |
3535
| :-------------------------------------------- | ----------: | ------------: | --------: | ------: |
36-
| matmul 2000³ | 133.10 ms | 9.57 s | 2.84 s | 0.05× |
37-
| contract aligned (48,48,1024)×(1024,48,48) | 86.58 ms | 6.69 s | 1.78 s | 0.05× |
38-
| contract permuted (48,1024,48)×(48,1024,48) | 195.12 ms | 6.28 s | 1.79 s | 0.11× |
39-
| MPS·MPO·env (χ=256, d=4, D=8) | 58.09 ms | 1.27 s | 384.48 ms | 0.15× |
40-
| permuted add 200³ | 86.81 ms | 87.04 ms | 16.50 ms | 5.26× |
41-
| partial trace (128,32,128,32) | 527.0 μs | 463.7 μs | 120.9 μs | 3.84× |
42-
| contract small 8³×8³ | 26.8 μs | 62.6 μs | 73.3 μs | 0.37× |
43-
| contract conj(A) (48,1024,48)×(48,1024,48) | 174.06 ms | 6.60 s | 1.75 s | 0.10× |
44-
| contract conj(A)conj(B) (48,1024,48)×(48,1024,48) | 190.03 ms | 7.26 s | 1.79 s | 0.11× |
36+
| matmul 2000³ | 125.49 ms | 9.28 s | 2.55 s | 0.05× |
37+
| contract aligned (48,48,1024)×(1024,48,48) | 100.07 ms | 6.40 s | 1.74 s | 0.06× |
38+
| contract permuted (48,1024,48)×(48,1024,48) | 168.72 ms | 6.15 s | 1.76 s | 0.10× |
39+
| MPS·MPO·env (χ=256, d=4, D=8) | 56.65 ms | 1.24 s | 381.66 ms | 0.15× |
40+
| permuted add 200³ | 81.03 ms | 81.47 ms | 12.37 ms | 6.55× |
41+
| partial trace (128,32,128,32) | 451.5 μs | 452.7 μs | 102.4 μs | 4.41× |
42+
| contract small 8³×8³ | 23.9 μs | 61.5 μs | 68.0 μs | 0.35× |
43+
| contract conj(A) (48,1024,48)×(48,1024,48) | 166.13 ms | 6.71 s | 1.75 s | 0.10× |
44+
| contract conj(A)conj(B) (48,1024,48)×(48,1024,48) | 181.13 ms | 6.84 s | 1.76 s | 0.10× |
4545

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

@@ -52,7 +52,7 @@ Julia 1.12.6, `tblis_jll` v1.3.0.
5252
the strided memory directly instead of materializing permuted copies.
5353
* **Additions and traces** are where TBLIS is furthest ahead (up to 9×), since `Strided.jl`
5454
loses to a blocked, threaded copy for these memory-bound shapes.
55-
* Plain **GEMM-shaped** contractions stay ~30% faster with OpenBLAS, and **small tensors**
55+
* Plain **GEMM-shaped** contractions stay ~25-40% faster with OpenBLAS, and **small tensors**
5656
are dominated by the per-call overhead of building the TBLIS descriptors.
5757
* **Complex contractions are 10-20× slower than BLAS.** This is a property of the `tblis`
5858
binaries rather than of this extension: a bare `tblis_tensor_mult` on a 1000³ complex

‎ext/TensorOperationsTBLISExt.jl‎

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using TensorOperations: add_labels, trace_labels, contract_labels
1111
using TensorOperations: tensoralloc_add, tensorfree!, select_backend
1212

1313
using TBLIS
14-
using TBLIS: len_type, stride_type
14+
using TBLIS: len_type, stride_type, tblis_tensor
1515

1616
const SV = StridedView
1717

@@ -22,48 +22,58 @@ const TBLISFloat = Union{Float32, Float64, ComplexF32, ComplexF64}
2222
#-------------------------------------------------------------------------------------------
2323
# Wrapping Julia arrays as TBLIS tensors
2424
#-------------------------------------------------------------------------------------------
25-
tblis_type(::Type{Float32}) = TBLIS.TYPE_SINGLE
26-
tblis_type(::Type{Float64}) = TBLIS.TYPE_DOUBLE
27-
tblis_type(::Type{ComplexF32}) = TBLIS.TYPE_SCOMPLEX
28-
tblis_type(::Type{ComplexF64}) = TBLIS.TYPE_DCOMPLEX
25+
# TBLIS.jl's own `tblis_tensor` constructor is restricted to `StridedArray`, which excludes
26+
# the `StridedView`s that the tensor operations work with, and it has no way to express the
27+
# conjugation flag. We therefore initialize the descriptor through the low-level bindings and
28+
# patch in the flag afterwards.
29+
for (T, init) in (
30+
(:Float32, :tblis_init_tensor_scaled_s),
31+
(:Float64, :tblis_init_tensor_scaled_d),
32+
(:ComplexF32, :tblis_init_tensor_scaled_c),
33+
(:ComplexF64, :tblis_init_tensor_scaled_z),
34+
)
35+
@eval function init_tensor!(
36+
p::Ptr{tblis_tensor}, A::StridedView{$T, N}, α::$T,
37+
len::Vector{len_type}, stride::Vector{stride_type}
38+
) where {N}
39+
return TBLIS.$init(p, α, Cuint(N), pointer(len), pointer(A), pointer(stride))
40+
end
41+
end
2942

3043
"""
3144
TBLISTensor(A::StridedView, α, isconj)
3245
3346
Owning counterpart of `TBLIS.tblis_tensor`, which itself only stores raw pointers into the
3447
array it views and into the buffers holding its lengths and strides. Rooting a `TBLISTensor`
35-
with `GC.@preserve` also roots all of those, as they are reachable from it.
36-
37-
Use [`unsafe_tblis_tensor`](@ref) to obtain the plain C struct that the library expects, and
38-
keep the `TBLISTensor` alive for at least as long as TBLIS may access it.
48+
with `GC.@preserve` also roots all of those, as they are reachable from it; pass its `ref`
49+
field wherever the library expects a `Ptr{tblis_tensor}`.
3950
"""
4051
struct TBLISTensor{T, N, A <: StridedView{T, N}}
4152
array::A
4253
len::Vector{len_type}
4354
stride::Vector{stride_type}
44-
scalar::T
45-
isconj::Bool
55+
ref::Base.RefValue{tblis_tensor}
4656
end
4757

48-
function TBLISTensor(A::StridedView{T, N}, α::T, isconj::Bool = hasconj(A)) where {T, N}
58+
function TBLISTensor(
59+
A::StridedView{T, N}, α::T, isconj::Bool = hasconj(A)
60+
) where {T <: TBLISFloat, N}
4961
len = collect(len_type, size(A))
5062
stride = collect(stride_type, strides(A))
51-
return TBLISTensor{T, N, typeof(A)}(A, len, stride, α, isconj)
63+
ref = Ref{tblis_tensor}()
64+
GC.@preserve A len stride ref begin
65+
p = Base.unsafe_convert(Ptr{tblis_tensor}, ref)
66+
init_tensor!(p, A, α, len, stride)
67+
isconj && setproperty!(p, :conj, Cint(1))
68+
end
69+
return TBLISTensor{T, N, typeof(A)}(A, len, stride, ref)
5270
end
5371

5472
# A `StridedView` carries its conjugation in an `op` field rather than in the data, which is
5573
# exactly the representation TBLIS wants. For real element types `conj` is the identity, and
5674
# flagging it would only confuse the library.
5775
hasconj(A::StridedView{T}) where {T} = T <: Complex && (A.op === conj || A.op === adjoint)
5876

59-
function unsafe_tblis_tensor(t::TBLISTensor{T, N}) where {T, N}
60-
return TBLIS.tblis_tensor(
61-
tblis_type(T), Cint(t.isconj), TBLIS.tblis_scalar(t.scalar),
62-
Ptr{Cvoid}(pointer(t.array)), Cuint(N),
63-
pointer(t.len), pointer(t.stride)
64-
)
65-
end
66-
6777
# TBLIS takes the index labels as one `char` per dimension; `add_labels` and friends hand us
6878
# `Char` tuples that are guaranteed to be ASCII.
6979
labels(ein::Tuple{Vararg{Char}}) = String(UInt8[c for c in ein])
@@ -183,9 +193,8 @@ function unsafe_add!(
183193
tA = TBLISTensor(A, α)
184194
tC = TBLISTensor(C, β)
185195
GC.@preserve tA tC begin
186-
tblis_tensor_add(
187-
unsafe_tblis_tensor(tA), labels(einA),
188-
unsafe_tblis_tensor(tC), labels(einC)
196+
TBLIS.tblis_tensor_add(
197+
C_NULL, C_NULL, tA.ref, labels(einA), tC.ref, labels(einC)
189198
)
190199
end
191200
return C
@@ -247,10 +256,9 @@ function unsafe_mult!(
247256
tB = TBLISTensor(B, one(T), false)
248257
tC = TBLISTensor(C, β, false)
249258
GC.@preserve tA tB tC begin
250-
tblis_tensor_mult(
251-
unsafe_tblis_tensor(tA), labels(einA),
252-
unsafe_tblis_tensor(tB), labels(einB),
253-
unsafe_tblis_tensor(tC), labels(einC)
259+
TBLIS.tblis_tensor_mult(
260+
C_NULL, C_NULL, tA.ref, labels(einA), tB.ref, labels(einB),
261+
tC.ref, labels(einC)
254262
)
255263
end
256264
return C

0 commit comments

Comments
 (0)