Skip to content

Commit 24ebd18

Browse files
committed
Boil down indexmanip rules to add_transform
1 parent 832ceb7 commit 24ebd18

4 files changed

Lines changed: 73 additions & 95 deletions

File tree

‎ext/TensorKitEnzymeExt/indexmanipulations.jl‎

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,27 @@
1-
for transform in (:permute, :transpose)
2-
transform! = Symbol(transform, :!)
3-
transform_pb = Symbol(transform, :_pullback_dA)
4-
@eval function EnzymeRules.augmented_primal(
5-
config::EnzymeRules.RevConfigWidth{1},
6-
func::Const{typeof(TK.$transform!)},
7-
::Type{RT},
8-
C::Annotation{<:AbstractTensorMap},
9-
A::Annotation{<:AbstractTensorMap},
10-
p::Const{<:Index2Tuple},
11-
α::Annotation{<:Number},
12-
β::Annotation{<:Number},
13-
ba::Const...
14-
) where {RT}
15-
C_cache = !isa(β, Const) ? copy(C.val) : nothing
16-
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
17-
# if we need to compute Δa, it is faster to allocate an intermediate permuted A
18-
# and store that instead of repeating the permutation in the pullback each time.
19-
# effectively, we replace `add_permute` by `add ∘ permute`.
20-
Ap = if !isa(α, Const)
21-
Ap = $transform(A.val, p.val)
22-
add!(C.val, Ap, α.val, β.val)
23-
Ap
24-
else
25-
bavs = map(a -> a.val, ba)
26-
TK.$transform!(C.val, A.val, p.val, α.val, β.val, bavs...)
27-
nothing
28-
end
29-
cache = (C_cache, A_cache, Ap)
30-
primal = EnzymeRules.needs_primal(config) ? C.val : nothing
31-
shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing
32-
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
33-
end
34-
@eval function EnzymeRules.reverse(
35-
config::EnzymeRules.RevConfigWidth{1},
36-
func::Const{typeof(TK.$transform!)},
37-
::Type{RT},
38-
cache,
39-
C::Annotation{<:AbstractTensorMap},
40-
A::Annotation{<:AbstractTensorMap},
41-
p::Const{<:Index2Tuple},
42-
α::Annotation{<:Number},
43-
β::Annotation{<:Number},
44-
ba::Const...
45-
) where {RT}
46-
C_cache, A_cache, Ap = cache
47-
Cval = something(C_cache, C.val)
48-
bavs = map(a -> a.val, ba)
49-
# ΔA
50-
if !isa(A, Const) && !isa(C, Const)
51-
Aval = something(A_cache, A.val)
52-
TK.$transform_pb(A.dval, Aval, C.dval, C.val, p.val, α.val, bavs...)
53-
end
54-
Δα = pullback_dα(α, C, Ap)
55-
Δβ = pullback_dβ(β, C, Cval)
56-
!isa(C, Const) && pullback_dC!(C.dval, β.val)
57-
return nothing, nothing, nothing, Δα, Δβ, map(Returns(nothing), ba)...
58-
end
59-
end
60-
611
function EnzymeRules.augmented_primal(
622
config::EnzymeRules.RevConfigWidth{1},
63-
func::Const{typeof(TK.braid!)},
3+
func::Const{typeof(TK.add_transform!)},
644
::Type{RT},
655
C::Annotation{<:AbstractTensorMap},
666
A::Annotation{<:AbstractTensorMap},
67-
p::Const{<:Index2Tuple},
68-
levels::Const{<:IndexTuple},
7+
p::Annotation{<:Index2Tuple},
8+
transformer::Annotation,
699
α::Annotation{<:Number},
7010
β::Annotation{<:Number},
7111
ba::Const...
7212
) where {RT}
73-
C_cache = !isa(β, Const) ? copy(C.val) : nothing
74-
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
13+
C_cache = !isa(β, Const) ? copy(C.val) : C.val
14+
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : A.val
7515
# if we need to compute Δa, it is faster to allocate an intermediate braided A
7616
# and store that instead of repeating the permutation in the pullback each time.
7717
# effectively, we replace `add_permute` by `add ∘ permute`.
18+
bavs = map(a -> a.val, ba)
7819
Ap = if !isa(α, Const)
79-
Ap = braid(A.val, p.val, levels.val)
20+
Ap = TK.add_transform!(Enzyme.make_zero(C.val), A.val, p.val, transformer.val, One(), Zero(), bavs...)
8021
add!(C.val, Ap, α.val, β.val)
8122
Ap
8223
else
83-
bavs = map(a -> a.val, ba)
84-
TK.braid!(C.val, A.val, p.val, levels.val, α.val, β.val, bavs...)
24+
TK.add_transform!(C.val, A.val, p.val, transformer.val, α.val, β.val, bavs...)
8525
nothing
8626
end
8727
cache = (C_cache, A_cache, Ap)
@@ -91,24 +31,33 @@ function EnzymeRules.augmented_primal(
9131
end
9232
function EnzymeRules.reverse(
9333
config::EnzymeRules.RevConfigWidth{1},
94-
func::Const{typeof(TK.braid!)},
34+
func::Const{typeof(TK.add_transform!)},
9535
::Type{RT},
9636
cache,
9737
C::Annotation{<:AbstractTensorMap},
9838
A::Annotation{<:AbstractTensorMap},
99-
p::Const{<:Index2Tuple},
100-
levels::Const{<:IndexTuple},
39+
p::Annotation{<:Index2Tuple},
40+
transformer::Annotation,
10141
α::Annotation{<:Number},
10242
β::Annotation{<:Number},
10343
ba::Const...
10444
) where {RT}
10545
C_cache, A_cache, Ap = cache
106-
Cval = something(C_cache, C.val)
107-
Aval = something(A_cache, A.val)
46+
Cval = C_cache
47+
Aval = A_cache
10848
bavs = map(a -> a.val, ba)
10949
# ΔA
11050
if !isa(A, Const) && !isa(C, Const)
111-
TK.braid_pb(A.dval, Aval, C.dval, C.val, p.val, levels.val, α.val, bavs...)
51+
ip = invperm(linearize(p.val))
52+
pΔA = TO.repartition(ip, numout(Aval))
53+
TC = VectorInterface.promote_scale(Cval, α.val)
54+
if scalartype(A.dval) <: Real && !(TC <: Real)
55+
ΔAc = TO.tensoralloc_add(TC, C.dval, pΔA, false, Val(false))
56+
TK.add_transform!(ΔAc, C.dval, pΔA, transformer.val, conj(α.val), Zero(), bavs...)
57+
add!(A.dval, real(ΔAc))
58+
else
59+
TK.add_transform!(A.dval, C.dval, pΔA, transformer.val, conj(α.val), One(), bavs...)
60+
end
11261
end
11362
Δαr = pullback_dα(α, C, Ap)
11463
Δβr = pullback_dβ(β, C, Cval)

‎test/enzyme-indexmanipulations-transform/braid.jl‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ spacelist = ad_spacelist(fast_tests)
88
eltypes = (Float64, ComplexF64)
99

1010
is_ci = get(ENV, "CI", "false") == "true"
11-
Tαs = is_ci ? (Active,) : (Active, Const)
12-
Tβs = is_ci ? (Active,) : (Active, Const)
11+
rTαs = is_ci ? (Active,) : (Active, Const)
12+
rTβs = is_ci ? (Active,) : (Active, Const)
13+
fTαs = is_ci ? (Duplicated,) : (Duplicated, Const)
14+
fTβs = is_ci ? (Duplicated,) : (Duplicated, Const)
1315

1416
if !Sys.iswindows() && VERSION > v"1.11.0-rc"
15-
@timedtestset "Enzyme - Index Manipulations (braid!) $(TensorKit.type_repr(sectortype(eltype(V)))) ($T) Tα $Tα Tβ $Tβ" for V in spacelist, T in eltypes, Tα in Tαs, Tβ in Tβs
17+
@timedtestset "Enzyme - Index Manipulations (braid!) $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
1618
atol = default_tol(T)
1719
rtol = default_tol(T)
1820
Vstr = TensorKit.type_repr(sectortype(eltype(V)))
@@ -24,11 +26,21 @@ if !Sys.iswindows() && VERSION > v"1.11.0-rc"
2426
p = randcircshift(numout(A), numin(A))
2527
levels = Tuple(randperm(numind(A)))
2628
C = randn!(transpose(A, p))
27-
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
28-
if !(T <: Real) && !is_ci
29-
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (real(A), Duplicated), (p, Const), (levels, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
30-
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (real(α), Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
31-
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (real(α), Tα), (real(β), Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
29+
@testset for Tα in rTαs, Tβ in rTβs
30+
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
31+
if !(T <: Real) && !is_ci
32+
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (real(A), Duplicated), (p, Const), (levels, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
33+
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (real(α), Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
34+
EnzymeTestUtils.test_reverse(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (real(α), Tα), (real(β), Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
35+
end
36+
end
37+
@testset for Tα in fTαs, Tβ in fTβs
38+
EnzymeTestUtils.test_forward(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
39+
if !(T <: Real) && !is_ci
40+
EnzymeTestUtils.test_forward(TensorKit.braid!, Duplicated, (C, Duplicated), (real(A), Duplicated), (p, Const), (levels, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
41+
EnzymeTestUtils.test_forward(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (real(α), Tα), (β, Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
42+
EnzymeTestUtils.test_forward(TensorKit.braid!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (levels, Const), (real(α), Tα), (real(β), Tβ); atol, rtol, testset_name = "braid! V $Vstr Tα $Tα Tβ $Tβ")
43+
end
3244
end
3345
end
3446
end

‎test/enzyme-indexmanipulations-transform/permute.jl‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,29 @@ spacelist = ad_spacelist(fast_tests)
88
eltypes = (Float64, ComplexF64)
99

1010
is_ci = get(ENV, "CI", "false") == "true"
11-
Tαs = is_ci ? (Active,) : (Active, Const)
12-
Tβs = is_ci ? (Active,) : (Active, Const)
11+
rTαs = is_ci ? (Active,) : (Active, Const)
12+
rTβs = is_ci ? (Active,) : (Active, Const)
13+
fTαs = is_ci ? (Duplicated,) : (Duplicated, Const)
14+
fTβs = is_ci ? (Duplicated,) : (Duplicated, Const)
1315

1416
if VERSION >= v"1.11.0-rc" # segfault issues on 1.10
1517
@timedtestset "Enzyme - Index Manipulations (permute!): $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
16-
println(TensorKit.type_repr(sectortype(eltype(V))))
1718
atol = default_tol(T)
1819
rtol = default_tol(T)
1920
symmetricbraiding = BraidingStyle(sectortype(eltype(V))) isa SymmetricBraiding
21+
A = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
22+
α = randn(T)
23+
β = randn(T)
24+
p = randindextuple(numind(A))
25+
C = randn!(permute(A, p))
2026

21-
symmetricbraiding && @timedtestset "permute! Tα $Tα, Tβ $Tβ" for Tα in Tαs, Tβ in Tβs
22-
A = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
23-
α = randn(T)
24-
β = randn(T)
25-
p = randindextuple(numind(A))
26-
C = randn!(permute(A, p))
27-
EnzymeTestUtils.test_reverse(TensorKit.permute!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol)
27+
symmetricbraiding && @timedtestset "permute!" begin
28+
@testset for Tα in rTαs, Tβ in rTβs
29+
EnzymeTestUtils.test_reverse(TensorKit.permute!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol)
30+
end
31+
@testset for Tα in fTαs, Tβ in fTβs
32+
EnzymeTestUtils.test_forward(TensorKit.permute!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol)
33+
end
2834
end
2935
end
3036
end

‎test/enzyme-indexmanipulations-transform/transpose.jl‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ eltypes = (Float64, ComplexF64)
1010

1111
is_ci = get(ENV, "CI", "false") == "true"
1212

13-
Tαs = is_ci ? (Active,) : (Active, Const)
14-
Tβs = is_ci ? (Active,) : (Active, Const)
13+
rTαs = is_ci ? (Active,) : (Active, Const)
14+
rTβs = is_ci ? (Active,) : (Active, Const)
15+
fTαs = is_ci ? (Duplicated,) : (Duplicated, Const)
16+
fTβs = is_ci ? (Duplicated,) : (Duplicated, Const)
1517

1618
if VERSION > v"1.11.0-rc" # https://github.com/QuantumKitHub/TensorKit.jl/issues/457
1719
@timedtestset "Enzyme - Index Manipulations (transpose!) $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
@@ -25,13 +27,22 @@ if VERSION > v"1.11.0-rc" # https://github.com/QuantumKitHub/TensorKit.jl/issues
2527
p = randcircshift(numout(A), numin(A))
2628
C = randn!(transpose(A, p))
2729
!is_ci && EnzymeTestUtils.test_reverse(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (One(), Const), (Zero(), Const); atol, rtol)
28-
@testset for Tα in Tαs, Tβ in Tβs
30+
!is_ci && EnzymeTestUtils.test_forward(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (One(), Const), (Zero(), Const); atol, rtol)
31+
@testset for Tα in rTαs, Tβ in rTβs
2932
EnzymeTestUtils.test_reverse(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol)
3033
if !(T <: Real) && !is_ci
3134
EnzymeTestUtils.test_reverse(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (real(A), Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol)
3235
EnzymeTestUtils.test_reverse(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (real(α), Tα), (β, Tβ); atol, rtol)
3336
EnzymeTestUtils.test_reverse(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (real(A), Duplicated), (p, Const), (real(α), Tα), (β, Tβ); atol, rtol)
3437
end
3538
end
39+
@testset for Tα in fTαs, Tβ in fTβs
40+
EnzymeTestUtils.test_forward(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol)
41+
if !(T <: Real) && !is_ci
42+
EnzymeTestUtils.test_forward(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (real(A), Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol)
43+
EnzymeTestUtils.test_forward(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (A, Duplicated), (p, Const), (real(α), Tα), (β, Tβ); atol, rtol)
44+
EnzymeTestUtils.test_forward(TensorKit.transpose!, Duplicated, (copy(C), Duplicated), (real(A), Duplicated), (p, Const), (real(α), Tα), (β, Tβ); atol, rtol)
45+
end
46+
end
3647
end
3748
end

0 commit comments

Comments
 (0)