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
21 changes: 21 additions & 0 deletions ext/TensorKitChainRulesCoreExt/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,24 @@
end
return a_imag, imag_pullback
end

function ChainRulesCore.rrule(cfg::RuleConfig, ::typeof(exp), A::AbstractTensorMap)
domain(A) == codomain(A) ||

Check warning on line 111 in ext/TensorKitChainRulesCoreExt/linalg.jl

View check run for this annotation

Codecov / codecov/patch

ext/TensorKitChainRulesCoreExt/linalg.jl#L110-L111

Added lines #L110 - L111 were not covered by tests
error("Exponential of a tensor only exist when domain == codomain.")
P_A = ProjectTo(A)
C = similar(A)
pullbacks = map(blocks(A)) do (c, b)
expB, pullback = rrule_via_ad(cfg, exp, b)
copy!(block(C, c), expB)
return c => pullback

Check warning on line 118 in ext/TensorKitChainRulesCoreExt/linalg.jl

View check run for this annotation

Codecov / codecov/patch

ext/TensorKitChainRulesCoreExt/linalg.jl#L113-L118

Added lines #L113 - L118 were not covered by tests
end
function exp_pullback(ΔC_)
ΔC = unthunk(ΔC_)
dA = similar(A)
for (c, pb) in pullbacks
copy!(block(dA, c), last(pb(block(ΔC, c))))
end
return NoTangent(), P_A(dA)

Check warning on line 126 in ext/TensorKitChainRulesCoreExt/linalg.jl

View check run for this annotation

Codecov / codecov/patch

ext/TensorKitChainRulesCoreExt/linalg.jl#L120-L126

Added lines #L120 - L126 were not covered by tests
end
return C, exp_pullback

Check warning on line 128 in ext/TensorKitChainRulesCoreExt/linalg.jl

View check run for this annotation

Codecov / codecov/patch

ext/TensorKitChainRulesCoreExt/linalg.jl#L128

Added line #L128 was not covered by tests
end
1 change: 1 addition & 0 deletions test/ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Vlist = ((ℂ^2, (ℂ^3)', ℂ^3, ℂ^2, (ℂ^2)'),
for i in 1:3
E = randn(T, ⊗(V[1:i]...) ← ⊗(V[1:i]...))
test_rrule(LinearAlgebra.tr, E)
test_rrule(exp, E; check_inferred=false)
end

A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
Expand Down
Loading