Repository navigation
Twist indices as a mask, and forward backend/allocator in AD rules - #619
Conversation
d35b3dd to
817ebd5
Compare
| @propagate_inbounds function twist!( | ||
| tdst::AbstractTensorMap, tsrc::AbstractTensorMap, inds, | ||
| α::Number = One(), β::Number = Zero(), | ||
| backend::AbstractBackend = TO.DefaultBackend(), allocator = TO.DefaultAllocator(); | ||
| α::Number = One(), β::Number = Zero(), backend::AbstractBackend = TO.DefaultBackend(); | ||
| inv::Bool = false | ||
| ) | ||
| @boundscheck spacecheck_transform(twist, space(tdst), space(tsrc), inds) | ||
| return twist!(tdst, tsrc, inds, α, β, backend, TO.DefaultAllocator(); inv) | ||
| end | ||
| @propagate_inbounds function twist!( | ||
| tdst::AbstractTensorMap, tsrc::AbstractTensorMap, inds, | ||
| α::Number, β::Number, backend::AbstractBackend, allocator; inv::Bool = false | ||
| ) | ||
| return twist!(tdst, tsrc, _twistmask(tsrc, inds), α, β, backend, allocator; inv) | ||
| end | ||
| @propagate_inbounds function twist!( | ||
| tdst::AbstractTensorMap, tsrc::AbstractTensorMap, mask::Tuple{Vararg{Bool}}, | ||
| α::Number, β::Number, backend::AbstractBackend, allocator; inv::Bool = false | ||
| ) |
There was a problem hiding this comment.
I don't think I fully understand why we need 3 different method definitions for this. I would understand if you want to be able to specialize on the one having inds::Tuple{Vararg{Bool}}, but in the end that method still calls _twistmask on its inds/mask argument, so I am confused.
There was a problem hiding this comment.
the main reason for calling _twistmask again is to detect if the length of the mask is correct, and meanwhile I think Tuple{} matches this signature, so that still needs to be converted to a tuple of falses.
There was a problem hiding this comment.
so the logic is: first fill in all default arguments, then convert to mask, then implement, hence the 3 definitions
There was a problem hiding this comment.
But I don't understand why all the default arguments cannot be filled in a single method definition, i.e. why not
@propagate_inbounds function twist!(
tdst::AbstractTensorMap, tsrc::AbstractTensorMap, inds,
α::Number = One(), β::Number = Zero(),
backend::AbstractBackend = TO.DefaultBackend(), allocator = TO.DefaultAllocator();
α::Number = One(), β::Number = Zero(), backend::AbstractBackend = TO.DefaultBackend();
inv::Bool = false
)
twistmask = _twistmask(tsrc, inds)
@boundscheck spacecheck_transform(twist, space(tdst), space(tsrc), twistmask)
@timeit_debug GLOBAL_TIMER "twist!" begin
p = (codomainind(tsrc), domainind(tsrc))
tdst′, tsrc′, p′, twistmask′, conjsrc′, α′, β′ = unwrap_adjoints(tdst, tsrc, p, twistmask, false, α, β)
# like `α`, the twist is conjugated by an adjoint destination
inv′ = inv ⊻ (tdst isa AdjointTensorMap)
@inbounds _twist!(tdst′, tsrc′, p′, conjsrc′, twistmask′, inv′, α′, β′, backend, allocator)
end
return tdst
endThere was a problem hiding this comment.
Oh that's a good point. I think I was playing around with a bunch of different options and wanted to avoid having to recompile the implementation itself for multiple argument types, but that also doesn't seem to be the case for your implementation, so probably your suggestion is just better
There was a problem hiding this comment.
If I see correctly, it got merged with the three different method signatures after all. I guess my suggestion didn't work then?
There was a problem hiding this comment.
Oh I'm sorry, I somehow read the PR approval and thought you had already pushed this change, that's my bad
Jutho
left a comment
There was a problem hiding this comment.
I haven't checked the tests, but otherwise looks ok aside from the one question I left.
The operand that `blas_contract!` twists for fermionic sectors is always copied first, so the twist is now folded into the recoupling coefficients of that copy through a cached `treepermuter`, instead of a separate `twist!` pass over the copied operand (#595). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…epermuter` `blas_contract!` now computes the twist masks of both operands in `contraction_twistmasks` and always copies through `permute_twist!`, with an all-false mask when no twist is needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
The GPU method still had the old six-argument signature, so the new seven-argument call from `_braid!` fell back to the host `treebraider` and built a host transformer for GPU storage, which `add_transform_kernel!` in the GPU extension does not accept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ules Follow-up to the review comments on #599: - `twist`/`twist!` convert `inds` to an `NTuple{numind, Bool}` mask on entry, dispatching to a mask method, which also removes the ambiguity workaround in `spacecheck_transform` and lets tensors accept a mask like fusion trees do. - Enzyme and Mooncake rules for `permute!`/`transpose!`/`braid!`/`twist!` forward `backend` and `allocator` to the intermediate out-of-place call, which now also always copies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ipulation rules Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…anipulation rules `twist!` fills in its default backend and allocator through an explicit call, such that rules for the full signature also apply to keyword calls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…checks out of CI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
817ebd5 to
9827975
Compare
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
Follow-up to the post-merge review comments on #599.
Changes
twist(t, inds),twist!(t, inds)andtwist!(tdst, tsrc, inds, ...)convertindsto anNTuple{numind(t), Bool}mask on entry (_twistmask), and the three-argumenttwist!has a dedicated method for a mask. Any collection of indices (tuples, vectors, ranges, anInt) is still accepted, but everything after the conversion only depends on the number of indices, so it is compiled once pernumind.Boolmask, just like the fusion treetwist.spacecheck_transform(twist, ...)andhas_shared_twisttake the mask, so the disambiguation method with theinvokecall is gone.ArgumentErrorduring the conversion; aBoolmask of the wrong length now throws (also for fusion trees) instead of being interpreted as indices0/1.backend/allocator. The Enzyme and Mooncake rules ofpermute!,transpose!,braid!andtwist!pass the optionalbackendandallocatoron to the intermediate out-of-place call. That call now also always usescopy = true, since the intermediate is stored for theΔαpullback and should not aliasA(this was already the case for the Enzymepermute!/twist!rules).Testing
tensors/indexmanipulations,tensors/contractions,tensors/diagonal,symmetries/doubletree,other/aqua,mooncake/indexmanipulations,enzyme/{permute,transpose,braid,flip}andchainrules/linalgpass. The final version of the branch was run againsttensors/indexmanipulations,symmetries/doubletree,other/aqua,mooncake/indexmanipulations,enzyme/flipandchainrules/linalg(121810 tests); the remaining files were run on an earlier revision whose only differences are the_twistmaskrename and the mask dispatch.inds, a tensor without indices, error cases, inferability, and the absence of method ambiguities.🤖 Generated with Claude Code