Skip to content

Commit 177e4ac

Browse files
lkdvosclaude
andcommitted
Cache device transformers in separate functions
`treebraider` and `treetransposer` forward device storage types to `device_treebraider` and `device_treetransposer`, whose methods are added by the GPU extension. These have their own MemoizationKit cache, so their size can be configured separately from the host transformers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 85df9e7 commit 177e4ac

5 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
2929
- Extend VectorInterface compat to include v0.7
3030
- Require TensorKitSectors v0.3.10
3131
- 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
32-
- Storage-specific transformers of package extensions are cached by adding `@cached` methods to `treebraider` and `treetransposer`, sharing their cache (internal)
32+
- Storage-specific transformers of package extensions are cached by adding `@cached` methods to `device_treebraider` and `device_treetransposer`, to which `treebraider` and `treetransposer` forward, such that their caches can be sized independently (internal)
3333
- The `TreeTransformer`s used in index manipulations are now constructed and cached per storagetype of the destination tensor, which is also a dispatch point for storage-specific transformers with their own cache (internal)
3434

3535
### Deprecated

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ end
147147
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontreetranspose), ::Any, ::Any) = nothing
148148
@inline EnzymeRules.inactive(::typeof(TensorKit.treetransposer), ::Any...) = nothing
149149
@inline EnzymeRules.inactive(::typeof(TensorKit.treebraider), ::Any...) = nothing
150+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treetransposer), ::Any...) = nothing
151+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treebraider), ::Any...) = nothing
150152
@inline EnzymeRules.inactive(::typeof(TensorKit.insertleftunit), ::HomSpace, ::Any) = nothing
151153
@inline EnzymeRules.inactive(::typeof(TensorKit.insertrightunit), ::HomSpace, ::Any) = nothing
152154
@inline EnzymeRules.inactive(::typeof(TensorKit.removeunit), ::HomSpace, ::Any) = nothing

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,24 @@ function DeviceGenericTreeTransformer(
272272
)
273273
end
274274

275+
TensorKit.treebraider(
276+
A::Type{<:AnyGPUVector}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
277+
conjsrc::Bool, levels::IndexTuple
278+
) = TensorKit.device_treebraider(A, Vdst, Vsrc, p, conjsrc, levels)
279+
TensorKit.treetransposer(
280+
A::Type{<:AnyGPUVector}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple, conjsrc::Bool
281+
) = TensorKit.device_treetransposer(A, Vdst, Vsrc, p, conjsrc)
282+
275283
# Cache the device transformer directly, constructing the cached host version only on a miss.
276-
@cached function TensorKit.treebraider(
284+
@cached function TensorKit.device_treebraider(
277285
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
278286
conjsrc::Bool, levels::IndexTuple
279287
) where {TA <: AnyGPUVector}
280288
host = TensorKit.treebraider(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, levels)
281289
return device_transformer(A, host, linearize(p))
282290
end
283291

284-
@cached function TensorKit.treetransposer(
292+
@cached function TensorKit.device_treetransposer(
285293
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple, conjsrc::Bool
286294
) where {TA <: AnyGPUVector}
287295
host = TensorKit.treetransposer(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc)

‎src/tensors/treetransformers.jl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,22 @@ end
226226
return TreeTransformer(A, fusiontreetransform, p, Vdst, Vsrc, conjsrc)
227227
end
228228

229+
"""
230+
device_treebraider(A, Vdst, Vsrc, p, conjsrc, levels)
231+
device_treetransposer(A, Vdst, Vsrc, p, conjsrc)
232+
233+
Tree transformers for device storage types `A`, to which `treebraider` and `treetransposer`
234+
forward. Their methods are added by package extensions. Being separate functions, the device
235+
transformers have their own cache, which can be configured independently of the host ones.
236+
"""
237+
function device_treebraider end
238+
function device_treetransposer end
239+
@doc (@doc device_treebraider) device_treetransposer
240+
229241
# Keep TensorKit's profiling categories for the phases instrumented by MemoizationKit.
230242
const TensorKitCachedFunction = Union{
231243
typeof(fusiontreebraid), typeof(fusiontreetranspose), typeof(treebraider), typeof(treetransposer),
244+
typeof(device_treebraider), typeof(device_treetransposer),
232245
typeof(sectorstructure), typeof(degeneracystructure),
233246
}
234247
MemoizationKit.instrument_label(f::TensorKitCachedFunction, ::Val{:lookup}) = "bookkeeping: cache $(nameof(f))"

‎test/cuda/tensors.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,17 @@ end
573573
end
574574
end
575575
end
576+
577+
@timedtestset "Device transformers are cached separately" begin
578+
TensorKit.empty_globalcaches!()
579+
V = SU2Space(0 => 2, 1 // 2 => 2)
580+
t = CUDA.rand(Float64, V ⊗ V ← V ⊗ V)
581+
p = ((2, 1), (4, 3))
582+
@test permute(t, p) ≈ adapt(CuArray, permute(ad(t), p))
583+
devicecache = only(TensorKit.MemoizationKit.cache_info(TensorKit.device_treebraider)).second
584+
hostcache = only(TensorKit.MemoizationKit.cache_info(TensorKit.treebraider)).second
585+
@test length(devicecache) == 1
586+
@test all(key -> !(first(key) <: CuArray), keys(hostcache))
587+
TensorKit.empty_globalcaches!()
588+
@test isempty(devicecache)
589+
end

0 commit comments

Comments
 (0)