Skip to content

Commit b4ccaab

Browse files
committed
Integrate Cached timers with TensorKit profiling
Enable and disable Cached instrumentation with TensorKit timers, using the shared timer and existing profiling categories. Replace the unconditional transformer construction timestamps with Cached computation timers. Verify lookup and computation sections are recorded and that disabling timers stops recording.
1 parent 87a9b6f commit b4ccaab

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎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 Cached.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 |

‎src/auxiliary/timers.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ host-side dispatch of asynchronous kernels unless the workload is explicitly syn
6161
"""
6262
function enable_timers!()
6363
TimerOutputs.enable_debug_timings(TensorKit)
64+
Cached.enable_cache_timers!(TensorKit, GLOBAL_TIMER)
6465
return nothing
6566
end
6667

@@ -72,6 +73,7 @@ Also triggers recompilation of the instrumented methods on first use.
7273
"""
7374
function disable_timers!()
7475
TimerOutputs.disable_debug_timings(TensorKit)
76+
Cached.disable_cache_timers!(TensorKit)
7577
return nothing
7678
end
7779

‎src/tensors/treetransformers.jl‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ struct GenericTreeTransformer{T, N} <: TreeTransformer
4848
end
4949

5050
function UniqueTreeTransformer(transform, p, Vdst, Vsrc, conjsrc::Bool)
51-
t₀ = Base.time()
52-
5351
spacecheck_transform(permute, Vdst, Vsrc, p, conjsrc)
5452

5553
src_trees, dst_trees = fusiontrees(Vsrc), fusiontrees(Vdst)
@@ -66,14 +64,12 @@ function UniqueTreeTransformer(transform, p, Vdst, Vsrc, conjsrc::Bool)
6664
structure_src = degeneracystructure(Vsrc).subblockstructure
6765
transformer = UniqueTreeTransformer(data, structure_dst, structure_src)
6866

69-
Δt = Base.time() - t₀
70-
@debug(lazy"Treetransformer for $Vsrc to $Vdst via $p", conjsrc, nblocks = length(data), Δt)
67+
@debug(lazy"Treetransformer for $Vsrc to $Vdst via $p", conjsrc, nblocks = length(data))
7168

7269
return transformer
7370
end
7471

7572
function GenericTreeTransformer(transform, p, Vdst, Vsrc, conjsrc::Bool)
76-
t₀ = Base.time()
7773
spacecheck_transform(permute, Vdst, Vsrc, p, conjsrc)
7874
# the fusion blocks that are transformed are those of the adjoint space for a conjugated source
7975
Vsrc′ = conjsrc ? Vsrc' : Vsrc
@@ -118,13 +114,11 @@ function GenericTreeTransformer(transform, p, Vdst, Vsrc, conjsrc::Bool)
118114
@timeit_debug GLOBAL_TIMER "bookkeeping: sort" Base.permute!(data, sortperm(weights; rev = true))
119115
transformer = GenericTreeTransformer(data, structure_dst, structure_src)
120116

121-
Δt = Base.time() - t₀
122117
@debug(
123118
lazy"TreeTransformer for $Vsrc to $Vdst via $p", conjsrc,
124119
nblocks = nblocks,
125120
sz_median = nblocks > 0 ? size(data[cld(end, 2)][1], 1) : 0,
126-
sz_max = nblocks > 0 ? size(data[1][1], 1) : 0,
127-
Δt
121+
sz_max = nblocks > 0 ? size(data[1][1], 1) : 0
128122
)
129123

130124
return transformer
@@ -189,6 +183,14 @@ end
189183
return TreeTransformer(fusiontreetransform, p, Vdst, Vsrc, conjsrc)
190184
end
191185

186+
# Keep TensorKit's profiling categories for the phases instrumented by Cached.
187+
const TensorKitCachedFunction = Union{
188+
typeof(fsbraid), typeof(fstranspose), typeof(treebraider), typeof(treetransposer),
189+
typeof(sectorstructure), typeof(degeneracystructure),
190+
}
191+
Cached.instrument_label(f::TensorKitCachedFunction, ::Val{:lookup}) = "bookkeeping: cache $(nameof(f))"
192+
Cached.instrument_label(f::TensorKitCachedFunction, ::Val{:compute}) = "symmetry: compute $(nameof(f))"
193+
192194
# For CPU arrays the recoupling matrix can be used as is, also when the scalar types
193195
# do not match, since Strided handles mixed-eltype mul! without the copy that
194196
# Adapt.adapt would make (which additionally dispatches dynamically). Other storage

‎test/other/timers.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ end
6565
# also verifies that `enable_timers!` reached the Factorizations submodule
6666
@test "svd_compact!" in names
6767

68+
sections = collect(TensorKit.timer().root.children)
69+
for section in sections
70+
append!(sections, section.children)
71+
end
72+
@test any(s -> s.name == "bookkeeping: cache treebraider", sections)
73+
@test any(s -> s.name == "symmetry: compute treebraider", sections)
74+
6875
summary = TensorKit.timer_summary(nothing)
6976
@test summary[:dense].ncalls > 0
7077
@test summary[:symmetry].ncalls > 0
@@ -77,4 +84,8 @@ end
7784
TensorKit.disable_timers!()
7885
end
7986
@test TensorKit.timeit_debug_enabled() === false
87+
TensorKit.reset_timers!()
88+
V = SU2Space(0 => 2, 1 // 2 => 2)
89+
permute(rand(V ⊗ V ← V ⊗ V), ((2, 1), (4, 3)))
90+
@test isempty(TensorKit.timer().root.children)
8091
end

0 commit comments

Comments
 (0)