Skip to content

Commit fef3ee2

Browse files
lkdvosclaude
andcommitted
Take explicit backend and allocator arguments in the Enzyme index manipulation rules
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3351cd5 commit fef3ee2

1 file changed

Lines changed: 30 additions & 45 deletions

File tree

‎ext/TensorKitEnzymeExt/indexmanipulations.jl‎

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for transform in (:permute, :transpose)
1010
p::Const{<:Index2Tuple},
1111
α::Annotation{<:Number},
1212
β::Annotation{<:Number},
13-
ba::Const...
13+
backend::Const, allocator::Const
1414
) where {RT}
1515
C_cache = !isa(β, Const) ? copy(C.val) : nothing
1616
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
@@ -21,14 +21,11 @@ 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-
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)
24+
Ap = $transform(A.val, p.val; copy = true, backend = backend.val, allocator = allocator.val)
2725
add!(C.val, Ap, α.val, β.val)
2826
Ap
2927
else
30-
bavs = map(a -> a.val, ba)
31-
TK.$transform!(C.val, A.val, p.val, α.val, β.val, bavs...)
28+
TK.$transform!(C.val, A.val, p.val, α.val, β.val, backend.val, allocator.val)
3229
nothing
3330
end
3431
cache = (C_cache, A_cache, Ap)
@@ -46,20 +43,19 @@ for transform in (:permute, :transpose)
4643
p::Const{<:Index2Tuple},
4744
α::Annotation{<:Number},
4845
β::Annotation{<:Number},
49-
ba::Const...
46+
backend::Const, allocator::Const
5047
) where {RT}
5148
C_cache, A_cache, Ap = cache
5249
Cval = something(C_cache, C.val)
53-
bavs = map(a -> a.val, ba)
5450
# ΔA
5551
if !isa(A, Const) && !isa(C, Const)
5652
Aval = something(A_cache, A.val)
57-
TK.$transform_pb(A.dval, Aval, C.dval, C.val, p.val, α.val, bavs...)
53+
TK.$transform_pb(A.dval, Aval, C.dval, C.val, p.val, α.val, backend.val, allocator.val)
5854
end
5955
Δα = pullback_dα(α, C, Ap)
6056
Δβ = pullback_dβ(β, C, Cval)
6157
!isa(C, Const) && pullback_dC!(C.dval, β.val)
62-
return nothing, nothing, nothing, Δα, Δβ, map(Returns(nothing), ba)...
58+
return nothing, nothing, nothing, Δα, Δβ, nothing, nothing
6359
end
6460
@eval function EnzymeRules.forward(
6561
config::EnzymeRules.FwdConfigWidth{1},
@@ -70,19 +66,18 @@ for transform in (:permute, :transpose)
7066
p::Annotation{<:Index2Tuple},
7167
α::Annotation{<:Number},
7268
β::Annotation{<:Number},
73-
ba::Const...
69+
backend::Const, allocator::Const
7470
) where {RT}
75-
bavs = map(a -> a.val, ba)
7671
if !isa(C, Const)
7772
if isa(β, Const)
7873
scale!(C.dval, β.val)
7974
else
8075
add!(C.dval, C.val, β.dval, β.val)
8176
end
82-
!isa(α, Const) && TK.$transform!(C.dval, A.val, p.val, α.dval, One(), bavs...)
83-
!isa(A, Const) && TK.$transform!(C.dval, A.dval, p.val, α.val, One(), bavs...)
77+
!isa(α, Const) && TK.$transform!(C.dval, A.val, p.val, α.dval, One(), backend.val, allocator.val)
78+
!isa(A, Const) && TK.$transform!(C.dval, A.dval, p.val, α.val, One(), backend.val, allocator.val)
8479
end
85-
TK.$transform!(C.val, A.val, p.val, α.val, β.val, bavs...)
80+
TK.$transform!(C.val, A.val, p.val, α.val, β.val, backend.val, allocator.val)
8681
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
8782
return C
8883
elseif EnzymeRules.needs_primal(config)
@@ -107,22 +102,19 @@ function EnzymeRules.augmented_primal(
107102
levels::Const{<:IndexTuple},
108103
α::Annotation{<:Number},
109104
β::Annotation{<:Number},
110-
ba::Const...
105+
backend::Const, allocator::Const
111106
) where {RT}
112107
C_cache = !isa(β, Const) ? copy(C.val) : nothing
113108
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
114109
# if we need to compute Δa, it is faster to allocate an intermediate braided A
115110
# and store that instead of repeating the permutation in the pullback each time.
116111
# effectively, we replace `add_permute` by `add ∘ permute`.
117112
Ap = if !isa(α, Const)
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)
113+
Ap = braid(A.val, p.val, levels.val; copy = true, backend = backend.val, allocator = allocator.val)
121114
add!(C.val, Ap, α.val, β.val)
122115
Ap
123116
else
124-
bavs = map(a -> a.val, ba)
125-
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, bavs...)
117+
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, backend.val, allocator.val)
126118
nothing
127119
end
128120
cache = (C_cache, A_cache, Ap)
@@ -141,20 +133,19 @@ function EnzymeRules.reverse(
141133
levels::Const{<:IndexTuple},
142134
α::Annotation{<:Number},
143135
β::Annotation{<:Number},
144-
ba::Const...
136+
backend::Const, allocator::Const
145137
) where {RT}
146138
C_cache, A_cache, Ap = cache
147139
Cval = something(C_cache, C.val)
148140
Aval = something(A_cache, A.val)
149-
bavs = map(a -> a.val, ba)
150141
# ΔA
151142
if !isa(A, Const) && !isa(C, Const)
152-
TK.braid_pb(A.dval, Aval, C.dval, C.val, p.val, levels.val, α.val, bavs...)
143+
TK.braid_pb(A.dval, Aval, C.dval, C.val, p.val, levels.val, α.val, backend.val, allocator.val)
153144
end
154145
Δαr = pullback_dα(α, C, Ap)
155146
Δβr = pullback_dβ(β, C, Cval)
156147
!isa(C, Const) && pullback_dC!(C.dval, β.val)
157-
return nothing, nothing, nothing, nothing, Δαr, Δβr, map(Returns(nothing), ba)...
148+
return nothing, nothing, nothing, nothing, Δαr, Δβr, nothing, nothing
158149
end
159150
function EnzymeRules.forward(
160151
config::EnzymeRules.FwdConfigWidth{1},
@@ -166,19 +157,18 @@ function EnzymeRules.forward(
166157
levels::Annotation{<:IndexTuple},
167158
α::Annotation{<:Number},
168159
β::Annotation{<:Number},
169-
ba::Const...
160+
backend::Const, allocator::Const
170161
) where {RT}
171-
bavs = map(a -> a.val, ba)
172162
if !isa(C, Const)
173163
if isa(β, Const)
174164
scale!(C.dval, β.val)
175165
else
176166
add!(C.dval, C.val, β.dval, β.val)
177167
end
178-
!isa(α, Const) && TK.braid!(C.dval, A.val, p.val, levels.val, α.dval, One(), bavs...)
179-
!isa(A, Const) && TK.braid!(C.dval, A.dval, p.val, levels.val, α.val, One(), bavs...)
168+
!isa(α, Const) && TK.braid!(C.dval, A.val, p.val, levels.val, α.dval, One(), backend.val, allocator.val)
169+
!isa(A, Const) && TK.braid!(C.dval, A.dval, p.val, levels.val, α.val, One(), backend.val, allocator.val)
180170
end
181-
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, bavs...)
171+
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, backend.val, allocator.val)
182172
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
183173
return C
184174
elseif EnzymeRules.needs_primal(config)
@@ -199,7 +189,7 @@ function EnzymeRules.augmented_primal(
199189
inds::Const,
200190
α::Annotation{<:Number},
201191
β::Annotation{<:Number},
202-
ba::Const...;
192+
backend::Const, allocator::Const;
203193
inv::Bool = false
204194
) where {RT}
205195
C_cache = !isa(β, Const) ? copy(C.val) : nothing
@@ -208,14 +198,11 @@ function EnzymeRules.augmented_primal(
208198
# and store that instead of repeating the twist in the pullback each time.
209199
# effectively, we replace `twist!` by `add ∘ twist`.
210200
Ap = if !isa(α, Const)
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)
201+
Ap = twist(A.val, inds.val; inv, copy = true, backend = backend.val, allocator = allocator.val)
214202
add!(C.val, Ap, α.val, β.val)
215203
Ap
216204
else
217-
bavs = map(a -> a.val, ba)
218-
TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv)
205+
TK.twist!(C.val, A.val, inds.val, α.val, β.val, backend.val, allocator.val; inv)
219206
nothing
220207
end
221208
cache = (C_cache, A_cache, Ap)
@@ -233,21 +220,20 @@ function EnzymeRules.reverse(
233220
inds::Const,
234221
α::Annotation{<:Number},
235222
β::Annotation{<:Number},
236-
ba::Const...;
223+
backend::Const, allocator::Const;
237224
inv::Bool = false
238225
) where {RT}
239226
C_cache, A_cache, Ap = cache
240227
Cval = something(C_cache, C.val)
241228
Aval = something(A_cache, A.val)
242-
bavs = map(a -> a.val, ba)
243229
# ΔA
244230
if !isa(A, Const) && !isa(C, Const)
245-
TK.twist_pb(A.dval, Aval, C.dval, C.val, inds.val, α.val, bavs...; inv)
231+
TK.twist_pb(A.dval, Aval, C.dval, C.val, inds.val, α.val, backend.val, allocator.val; inv)
246232
end
247233
Δαr = pullback_dα(α, C, Ap)
248234
Δβr = pullback_dβ(β, C, Cval)
249235
!isa(C, Const) && pullback_dC!(C.dval, β.val)
250-
return nothing, nothing, nothing, Δαr, Δβr, map(Returns(nothing), ba)...
236+
return nothing, nothing, nothing, Δαr, Δβr, nothing, nothing
251237
end
252238
function EnzymeRules.forward(
253239
config::EnzymeRules.FwdConfigWidth{1},
@@ -258,20 +244,19 @@ function EnzymeRules.forward(
258244
inds::Annotation,
259245
α::Annotation{<:Number},
260246
β::Annotation{<:Number},
261-
ba::Const...;
247+
backend::Const, allocator::Const;
262248
inv::Bool = false
263249
) where {RT}
264-
bavs = map(a -> a.val, ba)
265250
if !isa(C, Const)
266251
if isa(β, Const)
267252
scale!(C.dval, β.val)
268253
else
269254
add!(C.dval, C.val, β.dval, β.val)
270255
end
271-
!isa(α, Const) && TK.twist!(C.dval, A.val, inds.val, α.dval, One(), bavs...; inv)
272-
!isa(A, Const) && TK.twist!(C.dval, A.dval, inds.val, α.val, One(), bavs...; inv)
256+
!isa(α, Const) && TK.twist!(C.dval, A.val, inds.val, α.dval, One(), backend.val, allocator.val; inv)
257+
!isa(A, Const) && TK.twist!(C.dval, A.dval, inds.val, α.val, One(), backend.val, allocator.val; inv)
273258
end
274-
TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv)
259+
TK.twist!(C.val, A.val, inds.val, α.val, β.val, backend.val, allocator.val; inv)
275260
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
276261
return C
277262
elseif EnzymeRules.needs_primal(config)

0 commit comments

Comments
 (0)