Skip to content

Commit ca2502e

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 11c00d6 commit ca2502e

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
@@ -25,7 +25,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
2525
### Changed
2626

2727
- 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
28-
- Storage-specific transformers of package extensions are cached by adding `@cached` methods to `treebraider` and `treetransposer`, sharing their cache (internal)
28+
- 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)
2929

3030
### Deprecated
3131

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ end
148148
@inline EnzymeRules.inactive(::typeof(TensorKit.product_transform), ::Any...) = nothing
149149
@inline EnzymeRules.inactive(::typeof(TensorKit.treetransposer), ::Any...) = nothing
150150
@inline EnzymeRules.inactive(::typeof(TensorKit.treebraider), ::Any...) = nothing
151+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treetransposer), ::Any...) = nothing
152+
@inline EnzymeRules.inactive(::typeof(TensorKit.device_treebraider), ::Any...) = nothing
151153
@inline EnzymeRules.inactive(::typeof(TensorKit.insertleftunit), ::HomSpace, ::Any) = nothing
152154
@inline EnzymeRules.inactive(::typeof(TensorKit.insertrightunit), ::HomSpace, ::Any) = nothing
153155
@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
@@ -218,9 +218,22 @@ end
218218
return TreeTransformer(A, fusiontreetransform, p, Vdst, Vsrc, conjsrc)
219219
end
220220

221+
"""
222+
device_treebraider(A, Vdst, Vsrc, p, conjsrc, levels)
223+
device_treetransposer(A, Vdst, Vsrc, p, conjsrc)
224+
225+
Tree transformers for device storage types `A`, to which `treebraider` and `treetransposer`
226+
forward. Their methods are added by package extensions. Being separate functions, the device
227+
transformers have their own cache, which can be configured independently of the host ones.
228+
"""
229+
function device_treebraider end
230+
function device_treetransposer end
231+
@doc (@doc device_treebraider) device_treetransposer
232+
221233
# Keep TensorKit's profiling categories for the phases instrumented by MemoizationKit.
222234
const TensorKitCachedFunction = Union{
223235
typeof(fusiontreebraid), typeof(fusiontreetranspose), typeof(treebraider), typeof(treetransposer),
236+
typeof(device_treebraider), typeof(device_treetransposer),
224237
typeof(sectorstructure), typeof(degeneracystructure),
225238
}
226239
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
@@ -650,3 +650,17 @@ end
650650
@test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1])
651651
end
652652
end
653+
654+
@timedtestset "Device transformers are cached separately" begin
655+
TensorKit.empty_globalcaches!()
656+
V = SU2Space(0 => 2, 1 // 2 => 2)
657+
t = CUDA.rand(Float64, V ⊗ V ← V ⊗ V)
658+
p = ((2, 1), (4, 3))
659+
@test permute(t, p) ≈ adapt(CuArray, permute(ad(t), p))
660+
devicecache = only(TensorKit.MemoizationKit.cache_info(TensorKit.device_treebraider)).second
661+
hostcache = only(TensorKit.MemoizationKit.cache_info(TensorKit.treebraider)).second
662+
@test length(devicecache) == 1
663+
@test all(key -> !(first(key) <: CuArray), keys(hostcache))
664+
TensorKit.empty_globalcaches!()
665+
@test isempty(devicecache)
666+
end

0 commit comments

Comments
 (0)