Skip to content

Commit 61eba8a

Browse files
committed
Working
1 parent 0e8d4b0 commit 61eba8a

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

ext/TensorKitEnzymeExt/TensorKitEnzymeExt.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module TensorKitEnzymeExt
33
using Enzyme
44
using TensorKit
55
import TensorKit as TK
6+
using TensorKit: subblock
67
using VectorInterface
78
using TensorOperations: TensorOperations, IndexTuple, Index2Tuple, linearize
89
import TensorOperations as TO

ext/TensorKitEnzymeExt/tensoroperations.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,3 @@ function EnzymeRules.forward(
116116
return nothing
117117
end
118118
end
119-

ext/TensorKitEnzymeExt/utility.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,68 @@ pullback_dC!(ΔC, β::Number) = scale!(ΔC, conj(β))
1919
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.GenericTreeTransformer}) = true
2020
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.VectorSpace}) = true
2121

22+
function EnzymeRules.augmented_primal(
23+
config::EnzymeRules.RevConfigWidth{1},
24+
::Const{typeof(subblock)},
25+
::Type{RT},
26+
t::Annotation{<:AbstractTensorMap},
27+
f::Annotation,
28+
) where {RT}
29+
ret = EnzymeRules.needs_primal(config) ? subblock(t.val, f.val) : nothing
30+
dret = if !isa(t, Const) && EnzymeRules.needs_shadow(config)
31+
subblock(t.dval, f.val)
32+
elseif EnzymeRules.needs_shadow(config)
33+
Enzyme.make_zero(ret)
34+
else
35+
nothing
36+
end
37+
return EnzymeRules.AugmentedReturn(ret, dret, dret)
38+
end
39+
40+
function EnzymeRules.reverse(
41+
config::EnzymeRules.RevConfigWidth{1},
42+
::Const{typeof(subblock)},
43+
::Type{RT},
44+
cache,
45+
t::Annotation{<:AbstractTensorMap},
46+
f::Annotation,
47+
) where {RT}
48+
dret = cache
49+
if !isnothing(dret) && !isa(t, Const)
50+
subblock(t.dval, f.val) .= dret
51+
end
52+
return (nothing, nothing)
53+
end
54+
55+
function EnzymeRules.forward(
56+
config::EnzymeRules.FwdConfigWidth{1},
57+
::Const{typeof(subblock)},
58+
::Type{RT},
59+
t::Annotation{<:AbstractTensorMap},
60+
f::Annotation,
61+
) where {RT}
62+
ret = EnzymeRules.needs_primal(config) ? subblock(t.val, f.val) : nothing
63+
dret = if !isa(t, Const) && EnzymeRules.needs_shadow(config)
64+
subblock(t.dval, f.val)
65+
elseif EnzymeRules.needs_shadow(config)
66+
Enzyme.make_zero(ret)
67+
else
68+
nothing
69+
end
70+
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
71+
return Duplicated(ret, dret)
72+
elseif EnzymeRules.needs_primal(config)
73+
return ret
74+
elseif EnzymeRules.needs_shadow(config)
75+
return dret
76+
else
77+
return nothing
78+
end
79+
end
80+
81+
@inline EnzymeRules.inactive(::typeof(TensorKit.fsbraid), ::Any) = nothing
82+
@inline EnzymeRules.inactive(::typeof(TensorKit.fsbraid), ::Any, ::Any) = nothing
83+
@inline EnzymeRules.inactive(::typeof(TensorKit.artin_braid), ::Any, ::Any) = nothing
2284
@inline EnzymeRules.inactive(::typeof(TensorKit.sectorstructure), ::Any) = nothing
2385
@inline EnzymeRules.inactive(::typeof(TensorKit.degeneracystructure), ::Any) = nothing
2486
@inline EnzymeRules.inactive(::typeof(TensorKit.select), s::HomSpace, i::Index2Tuple) = nothing

0 commit comments

Comments
 (0)