Skip to content

Commit 81435d1

Browse files
Preserve tuple cotangent types in ChainRules pullbacks (#329)
1 parent 0822604 commit 81435d1

1 file changed

Lines changed: 20 additions & 24 deletions

File tree

‎ext/MatrixAlgebraKitChainRulesCoreExt.jl‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ for qr_f in (:qr_compact, :qr_full)
2929
function ChainRulesCore.rrule(::typeof($qr_f!), A, QR, alg)
3030
Ac = copy_input($qr_f, A)
3131
QR = $(qr_f!)(Ac, QR, alg)
32-
function qr_pullback(ΔQR)
32+
function qr_pullback((ΔQ, ΔR))
3333
ΔA = zero(A)
34-
MatrixAlgebraKit.qr_pullback!(ΔA, A, QR, unthunk.(ΔQR))
34+
MatrixAlgebraKit.qr_pullback!(ΔA, A, QR, map(unthunk, (ΔQ, ΔR)))
3535
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
3636
end
3737
function qr_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -61,9 +61,9 @@ for lq_f in (:lq_compact, :lq_full)
6161
function ChainRulesCore.rrule(::typeof($lq_f!), A, LQ, alg)
6262
Ac = copy_input($lq_f, A)
6363
LQ = $(lq_f!)(Ac, LQ, alg)
64-
function lq_pullback(ΔLQ)
64+
function lq_pullback((ΔL, ΔQ))
6565
ΔA = zero(A)
66-
MatrixAlgebraKit.lq_pullback!(ΔA, A, LQ, unthunk.(ΔLQ))
66+
MatrixAlgebraKit.lq_pullback!(ΔA, A, LQ, map(unthunk, (ΔL, ΔQ)))
6767
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
6868
end
6969
function lq_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -107,9 +107,9 @@ for eig in (:eig, :eigh)
107107
function ChainRulesCore.rrule(::typeof($eig_f!), A, DV, alg)
108108
Ac = copy_input($eig_f, A)
109109
DV = $(eig_f!)(Ac, DV, alg)
110-
function $eig_pb(ΔDV)
110+
function $eig_pb((ΔD, ΔV))
111111
ΔA = zero(A)
112-
MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, unthunk.(ΔDV))
112+
MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, map(unthunk, (ΔD, ΔV)))
113113
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
114114
end
115115
function $eig_pb(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -125,13 +125,12 @@ for eig in (:eig, :eigh)
125125
return (DV′..., ϵ), $(_make_eig_t_pb)(A, DV, ind)
126126
end
127127
function $(_make_eig_t_pb)(A, DV, ind)
128-
function $eig_t_pb(ΔDVϵ)
128+
function $eig_t_pb((ΔD, ΔV, Δϵ))
129129
ΔA = zero(A)
130-
ΔD, ΔV, Δϵ = ΔDVϵ
131130
if !MatrixAlgebraKit.iszerotangent(Δϵ) && !iszero(unthunk(Δϵ))
132131
throw(ArgumentError("Pullback for eig_trunc! does not yet support non-zero tangent for the truncation error"))
133132
end
134-
MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, unthunk.((ΔD, ΔV)), ind)
133+
MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, map(unthunk, (ΔD, ΔV)), ind)
135134
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
136135
end
137136
function $eig_t_pb(::Tuple{ZeroTangent, ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -146,10 +145,9 @@ for eig in (:eig, :eigh)
146145
return DV′, $(_make_eig_t_ne_pb)(A, DV, ind)
147146
end
148147
function $(_make_eig_t_ne_pb)(A, DV, ind)
149-
function $eig_t_ne_pb(ΔDV)
148+
function $eig_t_ne_pb((ΔD, ΔV))
150149
ΔA = zero(A)
151-
ΔD, ΔV = ΔDV
152-
MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, unthunk.((ΔD, ΔV)), ind)
150+
MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, map(unthunk, (ΔD, ΔV)), ind)
153151
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
154152
end
155153
function $eig_t_ne_pb(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -178,9 +176,9 @@ for svd_f in (:svd_compact, :svd_full)
178176
function ChainRulesCore.rrule(::typeof($svd_f!), A, USVᴴ, alg)
179177
Ac = copy_input($svd_f, A)
180178
USVᴴ = $(svd_f!)(Ac, USVᴴ, alg)
181-
function svd_pullback(ΔUSVᴴ)
179+
function svd_pullback((ΔU, ΔS, ΔVᴴ))
182180
ΔA = zero(A)
183-
MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, unthunk.(ΔUSVᴴ))
181+
MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, map(unthunk, (ΔU, ΔS, ΔVᴴ)))
184182
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
185183
end
186184
function svd_pullback(::Tuple{ZeroTangent, ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -199,13 +197,12 @@ function ChainRulesCore.rrule(::typeof(svd_trunc!), A, USVᴴ, alg::TruncatedAlg
199197
return (USVᴴ′..., ϵ), _make_svd_trunc_pullback(A, USVᴴ, ind)
200198
end
201199
function _make_svd_trunc_pullback(A, USVᴴ, ind)
202-
function svd_trunc_pullback(ΔUSVᴴϵ)
200+
function svd_trunc_pullback((ΔU, ΔS, ΔVᴴ, Δϵ))
203201
ΔA = zero(A)
204-
ΔU, ΔS, ΔVᴴ, Δϵ = ΔUSVᴴϵ
205202
if !MatrixAlgebraKit.iszerotangent(Δϵ) && !iszero(unthunk(Δϵ))
206203
throw(ArgumentError("Pullback for svd_trunc! does not yet support non-zero tangent for the truncation error"))
207204
end
208-
MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, unthunk.((ΔU, ΔS, ΔVᴴ)), ind)
205+
MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, map(unthunk, (ΔU, ΔS, ΔVᴴ)), ind)
209206
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
210207
end
211208
function svd_trunc_pullback(::Tuple{ZeroTangent, ZeroTangent, ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -221,10 +218,9 @@ function ChainRulesCore.rrule(::typeof(svd_trunc_no_error!), A, USVᴴ, alg::Tru
221218
return USVᴴ′, _make_svd_trunc_no_error_pullback(A, USVᴴ, ind)
222219
end
223220
function _make_svd_trunc_no_error_pullback(A, USVᴴ, ind)
224-
function svd_trunc_pullback(ΔUSVᴴ)
221+
function svd_trunc_pullback((ΔU, ΔS, ΔVᴴ))
225222
ΔA = zero(A)
226-
ΔU, ΔS, ΔVᴴ = ΔUSVᴴ
227-
MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, unthunk.((ΔU, ΔS, ΔVᴴ)), ind)
223+
MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, map(unthunk, (ΔU, ΔS, ΔVᴴ)), ind)
228224
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
229225
end
230226
function svd_trunc_pullback(::Tuple{ZeroTangent, ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -249,9 +245,9 @@ end
249245
function ChainRulesCore.rrule(::typeof(left_polar!), A, WP, alg)
250246
Ac = copy_input(left_polar, A)
251247
WP = left_polar!(Ac, WP, alg)
252-
function left_polar_pullback(ΔWP)
248+
function left_polar_pullback((ΔW, ΔP))
253249
ΔA = zero(A)
254-
MatrixAlgebraKit.left_polar_pullback!(ΔA, A, WP, unthunk.(ΔWP))
250+
MatrixAlgebraKit.left_polar_pullback!(ΔA, A, WP, map(unthunk, (ΔW, ΔP)))
255251
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
256252
end
257253
function left_polar_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful?
@@ -263,9 +259,9 @@ end
263259
function ChainRulesCore.rrule(::typeof(right_polar!), A, PWᴴ, alg)
264260
Ac = copy_input(left_polar, A)
265261
PWᴴ = right_polar!(Ac, PWᴴ, alg)
266-
function right_polar_pullback(ΔPWᴴ)
262+
function right_polar_pullback((ΔP, ΔWᴴ))
267263
ΔA = zero(A)
268-
MatrixAlgebraKit.right_polar_pullback!(ΔA, A, PWᴴ, unthunk.(ΔPWᴴ))
264+
MatrixAlgebraKit.right_polar_pullback!(ΔA, A, PWᴴ, map(unthunk, (ΔP, ΔWᴴ)))
269265
return NoTangent(), ΔA, ZeroTangent(), NoTangent()
270266
end
271267
function right_polar_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful?

0 commit comments

Comments
 (0)