Skip to content

Commit 8a247c6

Browse files
committed
Does Enzyme need a custom rule for trace_permute
1 parent cfaa073 commit 8a247c6

1 file changed

Lines changed: 0 additions & 89 deletions

File tree

ext/TensorKitEnzymeExt/tensoroperations.jl

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -117,92 +117,3 @@ function EnzymeRules.forward(
117117
end
118118
end
119119

120-
# tensortrace!
121-
# ------------
122-
123-
function EnzymeRules.augmented_primal(
124-
config::EnzymeRules.RevConfigWidth{1},
125-
func::Const{typeof(TensorKit.trace_permute!)},
126-
::Type{RT},
127-
C::Annotation{<:AbstractTensorMap},
128-
A::Annotation{<:AbstractTensorMap},
129-
p::Const{<:Index2Tuple},
130-
q::Const{<:Index2Tuple},
131-
α::Annotation{<:Number},
132-
β::Annotation{<:Number},
133-
backend::Const,
134-
) where {RT}
135-
C_cache = !isa(β, Const) ? copy(C.val) : nothing
136-
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
137-
At = if !isa(α, Const)
138-
At = TO.tensortrace(A.val, p.val, q.val, false, One(), backend.val)
139-
add!(C.val, At, α.val, β.val)
140-
At
141-
else
142-
TensorKit.trace_permute!(C.val, A.val, p.val, q.val, α.val, β.val, backend.val)
143-
nothing
144-
end
145-
primal = EnzymeRules.needs_primal(config) ? C.val : nothing
146-
shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing
147-
cache = (C_cache, A_cache, At)
148-
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
149-
end
150-
151-
152-
function EnzymeRules.reverse(
153-
config::EnzymeRules.RevConfigWidth{1},
154-
func::Const{typeof(TensorKit.trace_permute!)},
155-
::Type{RT},
156-
cache,
157-
C::Annotation{<:AbstractTensorMap},
158-
A::Annotation{<:AbstractTensorMap},
159-
p::Const{<:Index2Tuple},
160-
q::Const{<:Index2Tuple},
161-
α::Annotation{<:Number},
162-
β::Annotation{<:Number},
163-
backend::Const,
164-
) where {RT}
165-
C_cache, A_cache, At = cache
166-
Aval = something(A_cache, A.val)
167-
Cval = something(C_cache, C.val)
168-
!isa(A, Const) && !isa(C, Const) && TensorKit.trace_permute_pullback_ΔA!(A.dval, C.dval, Aval, p.val, q.val, α.val, backend.val)
169-
Δαr = pullback_dα(α, C, At)
170-
Δβr = pullback_dβ(β, C, Cval)
171-
!isa(C, Const) && pullback_dC!(C.dval, β.val)
172-
return nothing, nothing, nothing, nothing, Δαr, Δβr, nothing
173-
end
174-
175-
function EnzymeRules.forward(
176-
config::EnzymeRules.FwdConfigWidth{1},
177-
func::Const{typeof(TensorKit.trace_permute!)},
178-
::Type{RT},
179-
C::Annotation{<:AbstractTensorMap},
180-
A::Annotation{<:AbstractTensorMap},
181-
p::Annotation{<:Index2Tuple},
182-
q::Annotation{<:Index2Tuple},
183-
α::Annotation{<:Number},
184-
β::Annotation{<:Number},
185-
backend::Const,
186-
) where {RT}
187-
# dD = dα * tr(A) + α * tr(dA) + dβ * C + β * dC
188-
# dC1 = dβ * C + β * dC
189-
if !isa(C, Const)
190-
if isa(β, Const)
191-
scale!(C.dval, β.val)
192-
else
193-
add!(C.dval, C.val, β.dval, β.val)
194-
end
195-
!isa(α, Const) && TensorKit.trace_permute!(C.dval, A.val, p.val, q.val, α.dval, One(), backend.val)
196-
!isa(A, Const) && TensorKit.trace_permute!(C.dval, A.dval, p.val, q.val, α.val, One(), backend.val)
197-
end
198-
TensorKit.trace_permute!(C.val, A.val, p.val, q.val, α.val, β.val, backend.val)
199-
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
200-
return C
201-
elseif EnzymeRules.needs_primal(config)
202-
return C.val
203-
elseif EnzymeRules.needs_shadow(config)
204-
return C.dval
205-
else
206-
return nothing
207-
end
208-
end

0 commit comments

Comments
 (0)