Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec

### Changed

- `twist` and `twist!` on tensors also accept a `Bool` mask over all indices, like the fusion tree `twist`; any collection of indices is converted to such a mask first, so the rest of the implementation is compiled once per number of indices
- `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)
- 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

Expand All @@ -37,9 +38,13 @@ When releasing a new version, move the "Unreleased" changes to a new version sec

### Fixed

- The Enzyme and Mooncake rules of `permute!`, `transpose!`, `braid!` and `twist!` forward the `backend` and `allocator` to the intermediate out-of-place call, which now also always copies
- `twist` of a fusion tree pair or block throws for a `Bool` mask of the wrong length, instead of interpreting it as indices

### Performance

- `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))
- 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))
- `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
- `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
- Tree transformers for sectors with multiple fusion channels share equal recoupling matrices, which only depend on the sectors, instead of storing one copy per transformer
Expand Down
69 changes: 30 additions & 39 deletions ext/TensorKitEnzymeExt/indexmanipulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ for transform in (:permute, :transpose)
p::Const{<:Index2Tuple},
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...
backend::Const, allocator::Const
) where {RT}
C_cache = !isa(β, Const) ? copy(C.val) : nothing
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
Expand All @@ -21,12 +21,11 @@ for transform in (:permute, :transpose)
# without copy=true, this may just alias A if
# the permutation is an identity or the space
# is Trivial and has_shared_permute is true
Ap = $transform(A.val, p.val; copy = true)
Ap = $transform(A.val, p.val; copy = true, backend = backend.val, allocator = allocator.val)
add!(C.val, Ap, α.val, β.val)
Ap
else
bavs = map(a -> a.val, ba)
TK.$transform!(C.val, A.val, p.val, α.val, β.val, bavs...)
TK.$transform!(C.val, A.val, p.val, α.val, β.val, backend.val, allocator.val)
nothing
end
cache = (C_cache, A_cache, Ap)
Expand All @@ -44,20 +43,19 @@ for transform in (:permute, :transpose)
p::Const{<:Index2Tuple},
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...
backend::Const, allocator::Const
) where {RT}
C_cache, A_cache, Ap = cache
Cval = something(C_cache, C.val)
bavs = map(a -> a.val, ba)
# ΔA
if !isa(A, Const) && !isa(C, Const)
Aval = something(A_cache, A.val)
TK.$transform_pb(A.dval, Aval, C.dval, C.val, p.val, α.val, bavs...)
TK.$transform_pb(A.dval, Aval, C.dval, C.val, p.val, α.val, backend.val, allocator.val)
end
Δα = pullback_dα(α, C, Ap)
Δβ = pullback_dβ(β, C, Cval)
!isa(C, Const) && pullback_dC!(C.dval, β.val)
return nothing, nothing, nothing, Δα, Δβ, map(Returns(nothing), ba)...
return nothing, nothing, nothing, Δα, Δβ, nothing, nothing
end
@eval function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
Expand All @@ -68,19 +66,18 @@ for transform in (:permute, :transpose)
p::Annotation{<:Index2Tuple},
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...
backend::Const, allocator::Const
) where {RT}
bavs = map(a -> a.val, ba)
if !isa(C, Const)
if isa(β, Const)
scale!(C.dval, β.val)
else
add!(C.dval, C.val, β.dval, β.val)
end
!isa(α, Const) && TK.$transform!(C.dval, A.val, p.val, α.dval, One(), bavs...)
!isa(A, Const) && TK.$transform!(C.dval, A.dval, p.val, α.val, One(), bavs...)
!isa(α, Const) && TK.$transform!(C.dval, A.val, p.val, α.dval, One(), backend.val, allocator.val)
!isa(A, Const) && TK.$transform!(C.dval, A.dval, p.val, α.val, One(), backend.val, allocator.val)
end
TK.$transform!(C.val, A.val, p.val, α.val, β.val, bavs...)
TK.$transform!(C.val, A.val, p.val, α.val, β.val, backend.val, allocator.val)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C
elseif EnzymeRules.needs_primal(config)
Expand All @@ -105,20 +102,19 @@ function EnzymeRules.augmented_primal(
levels::Const{<:IndexTuple},
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...
backend::Const, allocator::Const
) where {RT}
C_cache = !isa(β, Const) ? copy(C.val) : nothing
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
# if we need to compute Δa, it is faster to allocate an intermediate braided A
# and store that instead of repeating the permutation in the pullback each time.
# effectively, we replace `add_permute` by `add ∘ permute`.
Ap = if !isa(α, Const)
Ap = braid(A.val, p.val, levels.val)
Ap = braid(A.val, p.val, levels.val; copy = true, backend = backend.val, allocator = allocator.val)
add!(C.val, Ap, α.val, β.val)
Ap
else
bavs = map(a -> a.val, ba)
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, bavs...)
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, backend.val, allocator.val)
nothing
end
cache = (C_cache, A_cache, Ap)
Expand All @@ -137,20 +133,19 @@ function EnzymeRules.reverse(
levels::Const{<:IndexTuple},
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...
backend::Const, allocator::Const
) where {RT}
C_cache, A_cache, Ap = cache
Cval = something(C_cache, C.val)
Aval = something(A_cache, A.val)
bavs = map(a -> a.val, ba)
# ΔA
if !isa(A, Const) && !isa(C, Const)
TK.braid_pb(A.dval, Aval, C.dval, C.val, p.val, levels.val, α.val, bavs...)
TK.braid_pb(A.dval, Aval, C.dval, C.val, p.val, levels.val, α.val, backend.val, allocator.val)
end
Δαr = pullback_dα(α, C, Ap)
Δβr = pullback_dβ(β, C, Cval)
!isa(C, Const) && pullback_dC!(C.dval, β.val)
return nothing, nothing, nothing, nothing, Δαr, Δβr, map(Returns(nothing), ba)...
return nothing, nothing, nothing, nothing, Δαr, Δβr, nothing, nothing
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
Expand All @@ -162,19 +157,18 @@ function EnzymeRules.forward(
levels::Annotation{<:IndexTuple},
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...
backend::Const, allocator::Const
) where {RT}
bavs = map(a -> a.val, ba)
if !isa(C, Const)
if isa(β, Const)
scale!(C.dval, β.val)
else
add!(C.dval, C.val, β.dval, β.val)
end
!isa(α, Const) && TK.braid!(C.dval, A.val, p.val, levels.val, α.dval, One(), bavs...)
!isa(A, Const) && TK.braid!(C.dval, A.dval, p.val, levels.val, α.val, One(), bavs...)
!isa(α, Const) && TK.braid!(C.dval, A.val, p.val, levels.val, α.dval, One(), backend.val, allocator.val)
!isa(A, Const) && TK.braid!(C.dval, A.dval, p.val, levels.val, α.val, One(), backend.val, allocator.val)
end
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, bavs...)
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, backend.val, allocator.val)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C
elseif EnzymeRules.needs_primal(config)
Expand All @@ -195,7 +189,7 @@ function EnzymeRules.augmented_primal(
inds::Const,
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...;
backend::Const, allocator::Const;
inv::Bool = false
) where {RT}
C_cache = !isa(β, Const) ? copy(C.val) : nothing
Expand All @@ -204,12 +198,11 @@ function EnzymeRules.augmented_primal(
# and store that instead of repeating the twist in the pullback each time.
# effectively, we replace `twist!` by `add ∘ twist`.
Ap = if !isa(α, Const)
Ap = twist(A.val, inds.val; inv, copy = true)
Ap = twist(A.val, inds.val; inv, copy = true, backend = backend.val, allocator = allocator.val)
add!(C.val, Ap, α.val, β.val)
Ap
else
bavs = map(a -> a.val, ba)
TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv)
TK.twist!(C.val, A.val, inds.val, α.val, β.val, backend.val, allocator.val; inv)
nothing
end
cache = (C_cache, A_cache, Ap)
Expand All @@ -227,21 +220,20 @@ function EnzymeRules.reverse(
inds::Const,
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...;
backend::Const, allocator::Const;
inv::Bool = false
) where {RT}
C_cache, A_cache, Ap = cache
Cval = something(C_cache, C.val)
Aval = something(A_cache, A.val)
bavs = map(a -> a.val, ba)
# ΔA
if !isa(A, Const) && !isa(C, Const)
TK.twist_pb(A.dval, Aval, C.dval, C.val, inds.val, α.val, bavs...; inv)
TK.twist_pb(A.dval, Aval, C.dval, C.val, inds.val, α.val, backend.val, allocator.val; inv)
end
Δαr = pullback_dα(α, C, Ap)
Δβr = pullback_dβ(β, C, Cval)
!isa(C, Const) && pullback_dC!(C.dval, β.val)
return nothing, nothing, nothing, Δαr, Δβr, map(Returns(nothing), ba)...
return nothing, nothing, nothing, Δαr, Δβr, nothing, nothing
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
Expand All @@ -252,20 +244,19 @@ function EnzymeRules.forward(
inds::Annotation,
α::Annotation{<:Number},
β::Annotation{<:Number},
ba::Const...;
backend::Const, allocator::Const;
inv::Bool = false
) where {RT}
bavs = map(a -> a.val, ba)
if !isa(C, Const)
if isa(β, Const)
scale!(C.dval, β.val)
else
add!(C.dval, C.val, β.dval, β.val)
end
!isa(α, Const) && TK.twist!(C.dval, A.val, inds.val, α.dval, One(), bavs...; inv)
!isa(A, Const) && TK.twist!(C.dval, A.dval, inds.val, α.val, One(), bavs...; inv)
!isa(α, Const) && TK.twist!(C.dval, A.val, inds.val, α.dval, One(), backend.val, allocator.val; inv)
!isa(A, Const) && TK.twist!(C.dval, A.dval, inds.val, α.val, One(), backend.val, allocator.val; inv)
end
TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv)
TK.twist!(C.val, A.val, inds.val, α.val, β.val, backend.val, allocator.val; inv)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C
elseif EnzymeRules.needs_primal(config)
Expand Down
20 changes: 10 additions & 10 deletions ext/TensorKitGPUArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ end

TensorKit.treebraider(
A::Type{<:AnyGPUVector}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
conjsrc::Bool, levels::IndexTuple
) = TensorKit.device_treebraider(A, Vdst, Vsrc, p, conjsrc, levels)
conjsrc::Bool, levels::IndexTuple, twistmask::NTuple{N, Bool}
) where {N} = TensorKit.device_treebraider(A, Vdst, Vsrc, p, conjsrc, levels, twistmask)
TensorKit.treetransposer(
A::Type{<:AnyGPUVector}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple, conjsrc::Bool
) = TensorKit.device_treetransposer(A, Vdst, Vsrc, p, conjsrc)
Expand All @@ -390,9 +390,9 @@ TensorKit.treetwister(
# Cache the device transformer directly, constructing the cached host version only on a miss.
@cached function TensorKit.device_treebraider(
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
conjsrc::Bool, levels::IndexTuple
) where {TA <: AnyGPUVector}
host = TensorKit.treebraider(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, levels)
conjsrc::Bool, levels::IndexTuple, twistmask::NTuple{N, Bool}
) where {TA <: AnyGPUVector, N}
host = TensorKit.treebraider(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, levels, twistmask)
return device_transformer(A, host, linearize(p))
end

Expand Down Expand Up @@ -721,9 +721,9 @@ end
# Transformers for diagonal sources share the cache of the other device transformers.
@cached function TensorKit.device_treebraider(
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
conjsrc::Bool, levels::IndexTuple, dstdiagonal::Bool
) where {TA <: AnyGPUVector}
host = TensorKit.treebraider(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, levels)
conjsrc::Bool, levels::IndexTuple, twistmask::NTuple{N, Bool}, dstdiagonal::Bool
) where {TA <: AnyGPUVector, N}
host = TensorKit.treebraider(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, levels, twistmask)
return diagonal_device_transformer(A, host, dstdiagonal)
end

Expand Down Expand Up @@ -756,12 +756,12 @@ end

Base.@propagate_inbounds function TensorKit._braid!(
tdst::Union{GPUTensorMap, GPUDiagonalTensorMap}, tsrc::GPUDiagonalTensorMap,
p::Index2Tuple, conjsrc::Bool, levels::IndexTuple, α, β, backend, allocator
p::Index2Tuple, conjsrc::Bool, levels::IndexTuple, twistmask::Tuple{Vararg{Bool}}, α, β, backend, allocator
)
Vdst, Vsrc = space(tdst), space(tsrc)
@boundscheck TensorKit.spacecheck_transform(permute, Vdst, Vsrc, p, conjsrc)
transformer = TensorKit.device_treebraider(
storagetype(tdst), Vdst, Vsrc, p, conjsrc, levels, tdst isa DiagonalTensorMap
storagetype(tdst), Vdst, Vsrc, p, conjsrc, levels, twistmask, tdst isa DiagonalTensorMap
)
return _add_diagonal!(tdst, tsrc, conjsrc, transformer, α, β)
end
Expand Down
Loading
Loading