Skip to content

Commit 42e1ae1

Browse files
lkdvosclaude
andcommitted
Avoid boxing the BLAS-contractable inputs in _blas_contract!
`makeblascontractable` returns either its input or a permuted copy, whose types differ when the copy is allocated by an allocator such as Bumper's. The resulting union is not an `isbits` type and was therefore heap allocated on every contraction, even when no copy was required. `_blas_contract!` now makes the inputs BLAS-contractable one at a time, passing them on to the next step in each branch rather than returning them, such that the union is never materialized. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f93724a commit 42e1ae1

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎docs/src/changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
- `ncon` can be differentiated with Zygote, including networks with traces.
2121
- `ncon` accepts traces in networks whose index lists are given as row matrices, e.g. `[1 1 -1]`.
22+
- Contractions using the `StridedBLAS` backend no longer heap allocate a view of an input tensor when an allocator such as Bumper's is used ([#273](https://github.com/QuantumKitHub/TensorOperations.jl/issues/273)).
2223

2324
### Performance
2425

‎src/implementation/blascontract.jl‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,57 @@ end
4343

4444
# implement necessary permutations
4545
function _blas_contract!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
46+
# note: the inputs are made BLAS-contractable one at a time, passing them on to the next
47+
# step rather than returning them, since their types depend on whether a copy was made,
48+
# and returning such a union would require heap allocating the result
49+
isblascontractable(A, pA) && eltype(A) == eltype(C) &&
50+
return _blas_contract_B!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
51+
A_ = _blascontractable_copy(A, pA, eltype(C), backend, allocator)
52+
_blas_contract_B!(C, SV(A_), trivialpermutation(pA), B, pB, pAB, α, β, backend, allocator)
53+
tensorfree!(A_, allocator)
54+
return C
55+
end
56+
function _blas_contract_B!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
57+
isblascontractable(B, pB) && eltype(B) == eltype(C) &&
58+
return _blas_contract_C!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
59+
B_ = _blascontractable_copy(B, pB, eltype(C), backend, allocator)
60+
_blas_contract_C!(C, A, pA, SV(B_), trivialpermutation(pB), pAB, α, β, backend, allocator)
61+
tensorfree!(B_, allocator)
62+
return C
63+
end
64+
function _blas_contract_C!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
4665
TC = eltype(C)
47-
48-
A_, pA, flagA = makeblascontractable(A, pA, TC, backend, allocator)
49-
B_, pB, flagB = makeblascontractable(B, pB, TC, backend, allocator)
50-
5166
# the partition of `ipAB` is required by `isblasdestination` and `tensoralloc_add`, but
5267
# the actual contraction only needs the linearized permutation
5368
ipAB = oindABinC(pAB, pA, pB)
5469
ipAB′ = linearize(ipAB)
5570
flagC = isblasdestination(C, ipAB)
5671
if flagC
57-
C_ = C
58-
_unsafe_blas_contract!(C_, A_, pA, B_, pB, ipAB′, α, β)
72+
_unsafe_blas_contract!(C, A, pA, B, pB, ipAB′, α, β)
5973
else
6074
C_ = SV(tensoralloc_add(TC, C, ipAB, false, Val(true), allocator))
6175
_unsafe_blas_contract!(
62-
C_, A_, pA, B_, pB, trivialpermutation(ipAB′),
76+
C_, A, pA, B, pB, trivialpermutation(ipAB′),
6377
one(TC), zero(TC)
6478
)
6579
# `C` already exists, so only the permutation of `pAB` matters here and it can be
6680
# handed over in the canonical `{N,0}` partition
6781
tensoradd!(C, C_, (pAB, ()), false, α, β, backend, allocator)
6882
tensorfree!(C_.parent, allocator)
6983
end
70-
flagA || tensorfree!(A_.parent, allocator)
71-
flagB || tensorfree!(B_.parent, allocator)
7284
return C
7385
end
7486

87+
# Make a permuted copy of `A` with scalar type `TC`, that can be used directly in a BLAS
88+
# contraction with the trivial partition. The copy should be freed afterwards.
89+
# note: inlined such that the wrapper of the copy does not need to be allocated if it does not
90+
# escape the caller, such as for an `Array` that only serves as the parent of a `StridedView`
91+
@inline function _blascontractable_copy(A, pA, ::Type{TC}, backend, allocator) where {TC}
92+
A_ = tensoralloc_add(TC, A, pA, false, Val(true), allocator)
93+
tensoradd!(SV(A_), A, pA, false, One(), Zero(), backend, allocator)
94+
return A_
95+
end
96+
7597
# perform the actual contraction, assuming it can be done as matrix multiplication by simply
7698
# reshaping without any further allocations
7799
function _unsafe_blas_contract!(

0 commit comments

Comments
 (0)