From 04cfea3d646789066ce74972030102f3f0baba26 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 24 Nov 2026 10:27:36 -0500 Subject: [PATCH 1/3] Add rrule for normalize on AbstractTensorMap --- ext/TensorKitChainRulesCoreExt/linalg.jl | 14 ++++++++++++++ test/chainrules/linalg.jl | 1 + 2 files changed, 15 insertions(+) diff --git a/ext/TensorKitChainRulesCoreExt/linalg.jl b/ext/TensorKitChainRulesCoreExt/linalg.jl index b092f0576..51dbb5b66 100644 --- a/ext/TensorKitChainRulesCoreExt/linalg.jl +++ b/ext/TensorKitChainRulesCoreExt/linalg.jl @@ -116,6 +116,20 @@ function ChainRulesCore.rrule(::typeof(norm), a::AbstractTensorMap, p::Real = 2) return n, norm_pullback end +# needed since GPU tensors have a specialized `normalize` that AD cannot trace through; +# the forward pass reuses it, the norm is only computed in the pullback +function ChainRulesCore.rrule(::typeof(normalize), a::AbstractTensorMap, p::Real = 2) + p == 2 || error("currently only implemented for p = 2") + y = normalize(a, p) + function normalize_pullback(Δy_) + Δy = unthunk(Δy_) + Δa = (Δy - y * real(dot(y, Δy))) / norm(a, p) + return NoTangent(), ProjectTo(a)(Δa), NoTangent() + end + normalize_pullback(::AbstractZero) = NoTangent(), ZeroTangent(), NoTangent() + return y, normalize_pullback +end + function ChainRulesCore.rrule(::typeof(inv), A::AbstractTensorMap) Ainv = inv(A) inv_pullback = let Ainv = Ainv diff --git a/test/chainrules/linalg.jl b/test/chainrules/linalg.jl index 3e81dc2ad..472d7edc1 100644 --- a/test/chainrules/linalg.jl +++ b/test/chainrules/linalg.jl @@ -163,6 +163,7 @@ for V in spacelist A = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])') test_rrule(LinearAlgebra.adjoint, A; atol, rtol) test_rrule(LinearAlgebra.norm, A, 2; atol, rtol) + test_rrule(LinearAlgebra.normalize, A, 2; atol, rtol) B = randn(T, space(A)) test_rrule(LinearAlgebra.dot, A, B; atol, rtol) From 00860d5d7e63d82e27a3f042a3137db218802262 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 24 Nov 2026 15:11:09 -0500 Subject: [PATCH 2/3] Compute norm only once --- ext/TensorKitChainRulesCoreExt/linalg.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ext/TensorKitChainRulesCoreExt/linalg.jl b/ext/TensorKitChainRulesCoreExt/linalg.jl index 51dbb5b66..51011c2e4 100644 --- a/ext/TensorKitChainRulesCoreExt/linalg.jl +++ b/ext/TensorKitChainRulesCoreExt/linalg.jl @@ -117,13 +117,16 @@ function ChainRulesCore.rrule(::typeof(norm), a::AbstractTensorMap, p::Real = 2) end # needed since GPU tensors have a specialized `normalize` that AD cannot trace through; -# the forward pass reuses it, the norm is only computed in the pullback +# the norm is computed once in the forward pass and reused in the pullback +# TODO revisit this once VectorInterface supports supplying a GPU vector for the output +# of norm and inner function ChainRulesCore.rrule(::typeof(normalize), a::AbstractTensorMap, p::Real = 2) p == 2 || error("currently only implemented for p = 2") - y = normalize(a, p) + n = norm(a, p) + y = scale(a, inv(n)) function normalize_pullback(Δy_) Δy = unthunk(Δy_) - Δa = (Δy - y * real(dot(y, Δy))) / norm(a, p) + Δa = add(Δy, y, -real(dot(y, Δy))) / n return NoTangent(), ProjectTo(a)(Δa), NoTangent() end normalize_pullback(::AbstractZero) = NoTangent(), ZeroTangent(), NoTangent() From c48b6c04c39b7ff2569f8c5a1f52ae1268bbdcee Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 24 Nov 2026 21:22:19 +0100 Subject: [PATCH 3/3] Update ext/TensorKitChainRulesCoreExt/linalg.jl Co-authored-by: Lukas Devos --- ext/TensorKitChainRulesCoreExt/linalg.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/TensorKitChainRulesCoreExt/linalg.jl b/ext/TensorKitChainRulesCoreExt/linalg.jl index 51011c2e4..80b3d780a 100644 --- a/ext/TensorKitChainRulesCoreExt/linalg.jl +++ b/ext/TensorKitChainRulesCoreExt/linalg.jl @@ -126,7 +126,7 @@ function ChainRulesCore.rrule(::typeof(normalize), a::AbstractTensorMap, p::Real y = scale(a, inv(n)) function normalize_pullback(Δy_) Δy = unthunk(Δy_) - Δa = add(Δy, y, -real(dot(y, Δy))) / n + Δa = add(Δy, y, -real(dot(y, Δy)) / n, inv(n)) return NoTangent(), ProjectTo(a)(Δa), NoTangent() end normalize_pullback(::AbstractZero) = NoTangent(), ZeroTangent(), NoTangent()