Skip to content

Commit 9bbb40d

Browse files
authored
Merge branch 'main' into contract-fuse
2 parents 6b51f3e + 7f0c873 commit 9bbb40d

44 files changed

Lines changed: 901 additions & 673 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Project.toml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ projects = ["test", "docs"]
99
[deps]
1010
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
1111
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
12-
LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
1312
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1413
MatrixAlgebraKit = "6c742aac-3347-4629-af66-fc926824e5e4"
14+
MemoizationKit = "1b238080-9255-4fe9-b224-89eb24efe93b"
1515
OhMyThreads = "67456a42-1dca-4109-a031-0a68de7e3ad5"
1616
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1717
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
1818
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
19+
PtrArrays = "43287f4e-b6f4-7ad1-bb20-aadabca52c3d"
1920
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2021
ScopedValues = "7e506255-f358-4e82-b7e4-beb19740aa63"
2122
Strided = "5e0ebb24-38b0-5f93-81fe-25c709ecae67"
@@ -54,15 +55,16 @@ Dictionaries = "0.4"
5455
Enzyme = "0.13.216"
5556
EnzymeTestUtils = "0.2.8"
5657
FiniteDifferences = "0.12"
57-
GPUArrays = "11.4.1"
58-
LRUCache = "1.6"
58+
GPUArrays = "11.4.1, 12.2"
5959
LinearAlgebra = "1"
6060
MatrixAlgebraKit = "0.6.14"
61+
MemoizationKit = "0.1.2"
6162
Mooncake = "0.5.27"
6263
OhMyThreads = "0.8.0"
6364
PrecompileTools = "1"
6465
Preferences = "1"
6566
Printf = "1"
67+
PtrArrays = "1.2"
6668
Random = "1"
6769
ScopedValues = "1.3.0"
6870
Strided = "2.6.1"

‎docs/src/Changelog.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
2929
### Changed
3030

3131
- `planartrace!` precomputes and caches a `TraceTransformer` with the subblock positions and recoupling coefficients, and executes it with a kernel shared by all sectortypes, analogous to the index manipulations; `planar_trace` is now also defined for `FusionTreeBlock`s, and GPU tensors use a dedicated kernel (internal)
32+
- Caching is provided by [MemoizationKit.jl](https://github.com/QuantumKitHub/MemoizationKit.jl), which can inspect, resize and configure TensorKit's caches, also by memory size; this replaces the internal `@cached` machinery and the LRUCache.jl dependency
3233

3334
### Deprecated
3435

@@ -40,6 +41,8 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
4041

4142
- `twist!` and `twist` on a `TensorMap`, a `DiagonalTensorMap` or their adjoints use a cached tree transformer and the index manipulation kernel instead of a loop over all fusion trees, which removes the per-subblock bookkeeping and turns a twist of a GPU tensor, diagonal or not, into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
4243
- Contractions of fermionic tensors fold the twist of one of the operands into the permutation that copies it, instead of twisting the copy in a separate pass ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
44+
- `foreachblock` with several tensors is type stable, which removes the dynamic dispatch on every call to it and to the factorizations that go through it
45+
- `taskforeach` with a single worker is a plain loop without an atomic counter, which lowers the cost per subblock of single-threaded index manipulations and planar traces
4346

4447
## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30
4548

@@ -87,6 +90,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
8790
- Fusion tree block manipulations of (named) product sectors are computed as Kronecker products of the manipulations of their parts, splitting off the `UniqueFusion` sectors, such that they share the caches of the individual sectors ([#592](https://github.com/QuantumKitHub/TensorKit.jl/pull/592))
8891
- The TensorOperations ChainRules rules no longer copy or retain `C` when `β = Zero()`, as is the case for every tensor allocated by `@tensor` ([#555](https://github.com/QuantumKitHub/TensorKit.jl/pull/555))
8992
- The Enzyme `norm` rule reuses the cached primal and the `tr` rule avoids an unnecessary copy ([#554](https://github.com/QuantumKitHub/TensorKit.jl/pull/554))
93+
- Fusion tree block manipulations no longer construct and index the canonical destination block, but keep the trees of the result in the order in which they are produced; the buffers of the intermediate blocks of a braid are recycled, and blocks with a single tree are braided as their tree pair (internal)
9094

9195
## [0.17.2](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.1...v0.17.2) - 2026-09-20
9296

‎docs/src/man/fusiontrees.md‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,49 @@ This is simply given by
260260

261261
The `braid` and `permute` routines for double fusion trees will be the main access point for corresponding manipulations on tensors.
262262
As a consequence, results from these routines are memoized via a `@cached` mechanism.
263-
The caching strategy is controlled by `CacheStyle`: for `FusionStyle(I) isa UniqueFusion` no caching is used (`NoCache()`), since the result is a single cheap scalar coefficient; for `MultipleFusion` or `GenericFusion` sectors, a global LRU cache (`GlobalLRUCache()`) is used, as computing the transformation matrix can be expensive and results are reused frequently.
263+
The caching strategy is controlled by `MemoizationKit.CacheStyle`: for `FusionStyle(I) isa UniqueFusion` no caching is used (`NoCache()`), since the result is a single cheap scalar coefficient; for `MultipleFusion` or `GenericFusion` sectors, `GlobalCache()` uses MemoizationKit.jl's configured container (ClockCache by default), as computing the transformation matrix can be expensive and results are reused frequently.
264+
265+
TensorKit also uses MemoizationKit.jl for sector structures, degeneracy structures and fusion tree transformers.
266+
Sector structures share cache entries across spaces with the same sectors, even if their degeneracy dimensions differ, through `MemoizationKit.cachekey`.
267+
The MemoizationKit APIs can inspect, resize and configure these caches:
268+
269+
```julia
270+
using TensorKit, MemoizationKit
271+
MemoizationKit.cache_info(TensorKit)
272+
MemoizationKit.set_cache_size!(TensorKit.treebraider, 50_000)
273+
MemoizationKit.set_cache_size!(TensorKit.treebraider, 2^30; by = MemoizationKit.cachesize) # 1 GiB of cached values
274+
MemoizationKit.set_cache_preferences!(TensorKit; maxsize = 50_000, measure = "bytes")
275+
MemoizationKit.set_cache_preferences!(; container = "LRU")
276+
```
277+
278+
Preferences are saved in `LocalPreferences.toml` and apply after restarting Julia; a runtime size change applies immediately.
279+
`MemoizationKit.uncached(f, args...)` computes a result without consulting or filling the cache.
280+
TensorKit supplies efficient `MemoizationKit.cachesize` methods for its structure, fusion-tree and transformer results.
281+
These estimate bytes from the fixed-size fields and live array buffers; matrix coefficients and inline sector values are not traversed individually.
282+
Reference-containing scalar or sector arrays fall back to `Base.summarysize`.
283+
Buffers shared between cache entries count toward each entry, while identical source and destination structure buffers within a transformer count once.
284+
The budget excludes cache keys, container and allocation overhead, and spare array capacity; it is a cached-value estimate rather than a limit on total process memory.
285+
Further features, such as the live cache dashboard, disk caching and the full configuration options, are described in the [MemoizationKit.jl documentation](https://quantumkithub.github.io/MemoizationKit.jl/stable/).
286+
287+
For example, `using Tachikoma; MemoizationKit.cache_dashboard()` shows the hit rates and sizes of TensorKit's caches live, here after a few index manipulations on tensors with different symmetries:
288+
289+
```text
290+
[RAM] │ Disk
291+
5 caches · 184 entries · recent hit rate 77.5%
292+
Name ▲ Hit rate Size Kind Activity
293+
▌TensorKit.degeneracystructure █████97%████▋ ▏░░░░░░░12/10k░░░░░░░░ Clock ▇███▇███
294+
TensorKit.fusiontreebraid ░░░░░0%░░░░░░ ▍░░░░░░147/10k░░░░░░░░ Clock
295+
TensorKit.sectorstructure █████96%████▌ ▏░░░░░░░12/10k░░░░░░░░ Clock ████████
296+
TensorKit.treebraider █████81%██▌░░ ▏░░░░░░░9/10k░░░░░░░░░ Clock ███ ███
297+
TensorKit.treetwister █████80%██▍░░ ▏░░░░░░░4/10k░░░░░░░░░ Clock ████████
298+
299+
╭─ TensorKit.degeneracystructure ────────────────────────────────────────────────────────────────╮
300+
│Clock · 12 of 10000 entries · 278 hits · 12 misses │
301+
│hit rate: recent 97.5% · lifetime 95.9% · 23.0 hits/s │
302+
│███▅▇▇▇▅▇▇▇ │
303+
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
304+
⇥ tab ↑↓ select ⏎ resize e empty s sort r reverse / filter g refresh q quit
305+
```
264306

265307
This caching implies that potential inefficiencies in the fusion tree manipulations (which we nonetheless try to avoid) will not seriously affect performance of tensor manipulations.
266308

‎docs/src/man/profiling.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# [Profiling and timers](@id s_profiling)
22

33
TensorKit's index manipulations, tensor contractions and factorizations are instrumented with [TimerOutputs.jl](https://github.com/KristofferC/TimerOutputs.jl) sections that are compiled away by default, so they incur no runtime cost.
4+
Enabling TensorKit's timers also enables MemoizationKit.jl's cache lookup and computation timers, recorded in the same timer tree and categories.
45
They can be enabled to obtain a detailed breakdown of where time is spent inside these operations, in particular the split between the different kinds of work involved in manipulating symmetric tensors:
56

67
| category | contents |

‎ext/TensorKitCUDAExt/TensorKitCUDAExt.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ using Random
1919

2020
include("cutensormap.jl")
2121

22+
function TensorKit._nrm2!(
23+
out::CuVector{R}, x::cuBLAS.StridedCuVecOrDenseMat{T}
24+
) where {R <: Union{Float32, Float64}, T <: Union{R, Complex{R}}}
25+
cuBLAS.nrm2(x, out)
26+
return out
27+
end
28+
2229
end

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pullback_dC!(ΔC, β::Number) = scale!(ΔC, conj(β))
2626
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.GenericTreeTransformer}) = true
2727
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.UniqueTreeTransformer}) = true
2828
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.VectorSpace}) = true
29-
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.LRU}) = true
29+
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.MemoizationKit.AbstractCache}) = true
3030

3131
function EnzymeRules.augmented_primal(
3232
config::EnzymeRules.RevConfigWidth{1},
@@ -142,16 +142,18 @@ end
142142
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any) = nothing
143143
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any, ::Any) = nothing
144144
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any, ::Any, ::Any) = nothing
145-
@inline EnzymeRules.inactive(::typeof(TensorKit.fsbraid), ::Any) = nothing
146-
@inline EnzymeRules.inactive(::typeof(TensorKit.fsbraid), ::Any, ::Any) = nothing
145+
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontreebraid), ::Any, ::Any, ::Any) = nothing
147146
@inline EnzymeRules.inactive(::typeof(TensorKit.artin_braid), ::Any, ::Any) = nothing
148-
@inline EnzymeRules.inactive(::typeof(TensorKit.fstranspose), ::Any) = nothing
149-
@inline EnzymeRules.inactive(::typeof(TensorKit.fstranspose), ::Any, ::Any) = nothing
147+
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontreetranspose), ::Any, ::Any) = nothing
150148
@inline EnzymeRules.inactive(::typeof(TensorKit.product_transform), ::Any...) = nothing
151149
@inline EnzymeRules.inactive(::typeof(TensorKit.treetransposer), ::Any...) = nothing
152150
@inline EnzymeRules.inactive(::typeof(TensorKit.treebraider), ::Any...) = nothing
153151
@inline EnzymeRules.inactive(::typeof(TensorKit.treetracer), ::Any...) = nothing
154152
@inline EnzymeRules.inactive(::typeof(TensorKit.treetwister), ::Any...) = nothing
153+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treetransposer), ::Any...) = nothing
154+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treebraider), ::Any...) = nothing
155+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treetracer), ::Any...) = nothing
156+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treetwister), ::Any...) = nothing
155157
@inline EnzymeRules.inactive(::typeof(TensorKit.insertleftunit), ::HomSpace, ::Any) = nothing
156158
@inline EnzymeRules.inactive(::typeof(TensorKit.insertrightunit), ::HomSpace, ::Any) = nothing
157159
@inline EnzymeRules.inactive(::typeof(TensorKit.removeunit), ::HomSpace, ::Any) = nothing

0 commit comments

Comments
 (0)