Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ext/TensorKitEnzymeExt/indexmanipulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ for transform in (:permute, :transpose)
# and store that instead of repeating the permutation in the pullback each time.
# effectively, we replace `add_permute` by `add ∘ permute`.
Ap = if !isa(α, Const)
Ap = $transform(A.val, p.val)
# without copy=true, this may just alias A if
# the permutation is an identity or the space
# is Trivial and has_shared_permute is true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it help if we instead try to permute the A_cache, which is fine to alias? And if it isn't available it's probably fine to alias A?

Ap = $transform(A.val, p.val; copy = true)
add!(C.val, Ap, α.val, β.val)
Ap
else
Expand Down
9 changes: 3 additions & 6 deletions test/enzyme/inv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ TDs = is_ci ? (Duplicated,) : (Const, Duplicated)
D2 = randn(T, V[1] ⊗ V[2] ← V[1] ⊗ V[2])
EnzymeTestUtils.test_reverse(inv, TD, (D2, TD); atol, rtol)
EnzymeTestUtils.test_forward(inv, TD, (D2, TD); atol, rtol)
# see https://github.com/QuantumKitHub/TensorKit.jl/issues/457
@static if VERSION ≥ v"1.11.0-rc"
D3 = randn(T, V[1] ⊗ V[2] ⊗ V[3] ← V[1] ⊗ V[2] ⊗ V[3])
EnzymeTestUtils.test_reverse(inv, TD, (D3, TD); atol, rtol)
EnzymeTestUtils.test_forward(inv, TD, (D3, TD); atol, rtol)
end
D3 = randn(T, V[1] ⊗ V[2] ⊗ V[3] ← V[1] ⊗ V[2] ⊗ V[3])
EnzymeTestUtils.test_reverse(inv, TD, (D3, TD); atol, rtol)
EnzymeTestUtils.test_forward(inv, TD, (D3, TD); atol, rtol)
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions test/enzyme/norm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ fRTs = is_ci ? (Duplicated,) : (Const, Duplicated)
@timedtestset "$(TensorKit.type_repr(sectortype(eltype(V)))) ($T), TC $TC" for V in spacelist, T in eltypes, TC in (Const, Duplicated)
atol = default_tol(T)
rtol = default_tol(T)
# see https://github.com/QuantumKitHub/TensorKit.jl/issues/457
@static if VERSION < v"1.11.0-rc"
C = randn(T, V[1] ⊗ V[2] ← (V[4] ⊗ V[5])')
else
C = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
end
C = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
for RT in rRTs
EnzymeTestUtils.test_reverse(norm, RT, (C, TC), (2, Const); atol, rtol)
EnzymeTestUtils.test_reverse(norm, RT, (C', TC), (2, Const); atol, rtol)
Expand Down
12 changes: 5 additions & 7 deletions test/enzyme/permute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ fTβs = is_ci ? (Duplicated,) : (Duplicated, Const)
β = randn(T)
p = randindextuple(numind(A))
C = randn!(permute(A, p))
@static if VERSION ≥ v"1.11.0-rc"
for Tα in rTαs, Tβ in rTβs
EnzymeTestUtils.test_reverse(TensorKit.permute!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "permute! reverse Tα: $Tα, Tβ: $Tβ")
end
for Tα in fTαs, Tβ in fTβs
EnzymeTestUtils.test_forward(TensorKit.permute!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "permute! forward Tα: $Tα, Tβ: $Tβ")
end
for Tα in rTαs, Tβ in rTβs
EnzymeTestUtils.test_reverse(TensorKit.permute!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "permute! reverse Tα: $Tα, Tβ: $Tβ")
end
for Tα in fTαs, Tβ in fTβs
EnzymeTestUtils.test_forward(TensorKit.permute!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "permute! forward Tα: $Tα, Tβ: $Tβ")
end
end
end
10 changes: 2 additions & 8 deletions test/enzyme/tr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,12 @@ TDs = is_ci ? (Duplicated,) : (Const, Duplicated)
@testset "tr reverse: RT $RT, TD $TD" for RT in rRTs, TD in TDs
EnzymeTestUtils.test_reverse(tr, RT, (D1, TD); atol, rtol)
EnzymeTestUtils.test_reverse(tr, RT, (D2, TD); atol, rtol)
# see https://github.com/QuantumKitHub/TensorKit.jl/issues/457
@static if VERSION ≥ v"1.11.0-rc"
EnzymeTestUtils.test_reverse(tr, RT, (D3, TD); atol, rtol)
end
EnzymeTestUtils.test_reverse(tr, RT, (D3, TD); atol, rtol)
end
@testset "tr forward: RT $RT, TD $TD" for RT in fRTs, TD in TDs
EnzymeTestUtils.test_forward(tr, RT, (D1, TD); atol, rtol)
EnzymeTestUtils.test_forward(tr, RT, (D2, TD); atol, rtol)
# see https://github.com/QuantumKitHub/TensorKit.jl/issues/457
@static if VERSION ≥ v"1.11.0-rc"
EnzymeTestUtils.test_forward(tr, RT, (D3, TD); atol, rtol)
end
EnzymeTestUtils.test_forward(tr, RT, (D3, TD); atol, rtol)
end
end
end
Loading