Skip to content
Merged
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
10 changes: 7 additions & 3 deletions ext/TensorKitChainRulesCoreExt/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function ChainRulesCore.rrule(
α::Number, β::Number, ba...
)
C′ = tensoradd!(copy(C), A, pA, conjA, α, β, ba...)
# only keep `C` alive on the tape if `dβ` needs it
C_β = _needs_tangent(β) ? C : nothing

projectA = ProjectTo(A)
projectC = ProjectTo(C)
Expand Down Expand Up @@ -49,7 +51,7 @@ function ChainRulesCore.rrule(
else
ZeroTangent()
end
dβ = _needs_tangent(β) ? @thunk(projectβ(inner(C, ΔC))) : ZeroTangent()
dβ = _needs_tangent(β) ? @thunk(projectβ(inner(C_β, ΔC))) : NoTangent()
dba = map(_ -> NoTangent(), ba)
return NoTangent(), dC, dA, NoTangent(), NoTangent(), dα, dβ, dba...
end
Expand All @@ -66,6 +68,7 @@ function ChainRulesCore.rrule(
α::Number, β::Number, ba...
)
C′ = tensorcontract!(copy(C), A, pA, conjA, B, pB, conjB, pAB, α, β, ba...)
C_β = _needs_tangent(β) ? C : nothing

projectA = ProjectTo(A)
projectB = ProjectTo(B)
Expand Down Expand Up @@ -138,7 +141,7 @@ function ChainRulesCore.rrule(
else
ZeroTangent()
end
dβ = _needs_tangent(β) ? @thunk(projectβ(inner(C, ΔC))) : ZeroTangent()
dβ = _needs_tangent(β) ? @thunk(projectβ(inner(C_β, ΔC))) : NoTangent()
dba = map(_ -> NoTangent(), ba)
return NoTangent(), dC,
dA, NoTangent(), NoTangent(),
Expand All @@ -156,6 +159,7 @@ function ChainRulesCore.rrule(
α::Number, β::Number, ba...
)
C′ = tensortrace!(copy(C), A, p, q, conjA, α, β, ba...)
C_β = _needs_tangent(β) ? C : nothing

projectA = ProjectTo(A)
projectC = ProjectTo(C)
Expand Down Expand Up @@ -190,7 +194,7 @@ function ChainRulesCore.rrule(
else
ZeroTangent()
end
dβ = _needs_tangent(β) ? @thunk(projectβ(inner(C, ΔC))) : ZeroTangent()
dβ = _needs_tangent(β) ? @thunk(projectβ(inner(C_β, ΔC))) : NoTangent()
dba = map(_ -> NoTangent(), ba)
return NoTangent(), dC, dA, NoTangent(), NoTangent(), NoTangent(), dα, dβ, dba...
end
Expand Down
7 changes: 7 additions & 0 deletions test/chainrules/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using TensorKit
using TensorKit: type_repr, SectorDict
using TensorOperations
using ChainRulesCore
using VectorInterface: Zero
using ChainRulesTestUtils
using Random
using LinearAlgebra
Expand Down Expand Up @@ -47,6 +48,7 @@ for V in spacelist
for conjA in (false, true)
C = randn!(TensorOperations.tensoralloc_add(T, A, p, conjA, Val(false)))
test_rrule(tensortrace!, C, A, p, q, conjA, α, β; atol, rtol)
test_rrule(tensortrace!, C, A, p, q, conjA, α, Zero() ⊢ NoTangent(); atol, rtol)
end
end
end
Expand All @@ -65,6 +67,7 @@ for V in spacelist

C2 = randn!(TensorOperations.tensoralloc_add(T, A, p, true, Val(false)))
test_rrule(tensoradd!, C2, A, p, true, α, β; atol, rtol)
test_rrule(tensoradd!, C1, A, p, false, α, Zero() ⊢ NoTangent(); atol, rtol)

A = rand(Bool) ? C1 : C2
end
Expand Down Expand Up @@ -107,6 +110,10 @@ for V in spacelist
tensorcontract!, C, A, pA, conjA, B, pB, conjB, pAB, α, β;
atol, rtol
)
test_rrule(
tensorcontract!, C, A, pA, conjA, B, pB, conjB, pAB, α,
Zero() ⊢ NoTangent(); atol, rtol
)
end
end
end
Expand Down
Loading