Skip to content

Commit 3351cd5

Browse files
lkdvosclaude
andcommitted
Write out backend/allocator keywords in the AD rules instead of a helper
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bd0ee71 commit 3351cd5

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

‎ext/TensorKitEnzymeExt/indexmanipulations.jl‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ for transform in (:permute, :transpose)
2121
# without copy=true, this may just alias A if
2222
# the permutation is an identity or the space
2323
# is Trivial and has_shared_permute is true
24-
bavs = map(a -> a.val, ba)
25-
Ap = $transform(A.val, p.val; copy = true, TK.backend_kwargs(bavs...)...)
24+
backend = get(ba, 1, Const(TO.DefaultBackend())).val
25+
allocator = get(ba, 2, Const(TO.DefaultAllocator())).val
26+
Ap = $transform(A.val, p.val; copy = true, backend, allocator)
2627
add!(C.val, Ap, α.val, β.val)
2728
Ap
2829
else
@@ -114,8 +115,9 @@ function EnzymeRules.augmented_primal(
114115
# and store that instead of repeating the permutation in the pullback each time.
115116
# effectively, we replace `add_permute` by `add ∘ permute`.
116117
Ap = if !isa(α, Const)
117-
bavs = map(a -> a.val, ba)
118-
Ap = braid(A.val, p.val, levels.val; copy = true, TK.backend_kwargs(bavs...)...)
118+
backend = get(ba, 1, Const(TO.DefaultBackend())).val
119+
allocator = get(ba, 2, Const(TO.DefaultAllocator())).val
120+
Ap = braid(A.val, p.val, levels.val; copy = true, backend, allocator)
119121
add!(C.val, Ap, α.val, β.val)
120122
Ap
121123
else
@@ -206,8 +208,9 @@ function EnzymeRules.augmented_primal(
206208
# and store that instead of repeating the twist in the pullback each time.
207209
# effectively, we replace `twist!` by `add ∘ twist`.
208210
Ap = if !isa(α, Const)
209-
bavs = map(a -> a.val, ba)
210-
Ap = twist(A.val, inds.val; inv, copy = true, TK.backend_kwargs(bavs...)...)
211+
backend = get(ba, 1, Const(TO.DefaultBackend())).val
212+
allocator = get(ba, 2, Const(TO.DefaultAllocator())).val
213+
Ap = twist(A.val, inds.val; inv, copy = true, backend, allocator)
211214
add!(C.val, Ap, α.val, β.val)
212215
Ap
213216
else

‎ext/TensorKitMooncakeExt/indexmanipulations.jl‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ for transform in (:permute, :transpose)
3333
# and store that instead of repeating the permutation in the pullback each time.
3434
# effectively, we replace `permute!/transpose!` by `add ∘ permute/transpose`.
3535
Ap = if _needs_tangent(α)
36-
Ap = $transform(A, p; copy = true, TK.backend_kwargs(ba...)...)
36+
backend = get(ba, 1, TO.DefaultBackend())
37+
allocator = get(ba, 2, TO.DefaultAllocator())
38+
Ap = $transform(A, p; copy = true, backend, allocator)
3739
add!(C, Ap, α, β)
3840
Ap
3941
else
@@ -91,7 +93,9 @@ function Mooncake.rrule!!(
9193
# and store that instead of repeating the permutation in the pullback each time.
9294
# effectively, we replace `braid!` by `add ∘ braid`.
9395
Ap = if _needs_tangent(α)
94-
Ap = braid(A, p, levels; copy = true, TK.backend_kwargs(ba...)...)
96+
backend = get(ba, 1, TO.DefaultBackend())
97+
allocator = get(ba, 2, TO.DefaultAllocator())
98+
Ap = braid(A, p, levels; copy = true, backend, allocator)
9599
add!(C, Ap, α, β)
96100
Ap
97101
else
@@ -222,7 +226,9 @@ function twist!_rrule(C_ΔC, A_ΔA, inds_Δinds, inv, args_Δargs)
222226
# and store that instead of repeating the twist in the pullback each time.
223227
# effectively, we replace `twist!` by `add ∘ twist`.
224228
Ap = if _needs_tangent(α)
225-
Ap = twist(A, inds; inv, copy = true, TK.backend_kwargs(ba...)...)
229+
backend = get(ba, 1, TO.DefaultBackend())
230+
allocator = get(ba, 2, TO.DefaultAllocator())
231+
Ap = twist(A, inds; inv, copy = true, backend, allocator)
226232
add!(C, Ap, α, β)
227233
Ap
228234
else

‎src/pullbacks/indexmanipulations.jl‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# the optional positional `backend` and `allocator` as keywords for the out-of-place methods
2-
backend_kwargs() = (;)
3-
backend_kwargs(backend) = (; backend)
4-
backend_kwargs(backend, allocator) = (; backend, allocator)
5-
61
for transform in (:permute, :transpose)
72
transform! = Symbol(transform, :!)
83
transform_pb = Symbol(transform, :_pullback_dA)

0 commit comments

Comments
 (0)