diff --git a/ext/MatrixAlgebraKitChainRulesCoreExt.jl b/ext/MatrixAlgebraKitChainRulesCoreExt.jl index acfe0d837..9933bd3c5 100644 --- a/ext/MatrixAlgebraKitChainRulesCoreExt.jl +++ b/ext/MatrixAlgebraKitChainRulesCoreExt.jl @@ -29,9 +29,9 @@ for qr_f in (:qr_compact, :qr_full) function ChainRulesCore.rrule(::typeof($qr_f!), A, QR, alg) Ac = copy_input($qr_f, A) QR = $(qr_f!)(Ac, QR, alg) - function qr_pullback(ΔQR) + function qr_pullback((ΔQ, ΔR)) ΔA = zero(A) - MatrixAlgebraKit.qr_pullback!(ΔA, A, QR, unthunk.(ΔQR)) + MatrixAlgebraKit.qr_pullback!(ΔA, A, QR, map(unthunk, (ΔQ, ΔR))) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end function qr_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful? @@ -61,9 +61,9 @@ for lq_f in (:lq_compact, :lq_full) function ChainRulesCore.rrule(::typeof($lq_f!), A, LQ, alg) Ac = copy_input($lq_f, A) LQ = $(lq_f!)(Ac, LQ, alg) - function lq_pullback(ΔLQ) + function lq_pullback((ΔL, ΔQ)) ΔA = zero(A) - MatrixAlgebraKit.lq_pullback!(ΔA, A, LQ, unthunk.(ΔLQ)) + MatrixAlgebraKit.lq_pullback!(ΔA, A, LQ, map(unthunk, (ΔL, ΔQ))) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end function lq_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful? @@ -107,9 +107,9 @@ for eig in (:eig, :eigh) function ChainRulesCore.rrule(::typeof($eig_f!), A, DV, alg) Ac = copy_input($eig_f, A) DV = $(eig_f!)(Ac, DV, alg) - function $eig_pb(ΔDV) + function $eig_pb((ΔD, ΔV)) ΔA = zero(A) - MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, unthunk.(ΔDV)) + MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, map(unthunk, (ΔD, ΔV))) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end function $eig_pb(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful? @@ -125,13 +125,12 @@ for eig in (:eig, :eigh) return (DV′..., ϵ), $(_make_eig_t_pb)(A, DV, ind) end function $(_make_eig_t_pb)(A, DV, ind) - function $eig_t_pb(ΔDVϵ) + function $eig_t_pb((ΔD, ΔV, Δϵ)) ΔA = zero(A) - ΔD, ΔV, Δϵ = ΔDVϵ if !MatrixAlgebraKit.iszerotangent(Δϵ) && !iszero(unthunk(Δϵ)) throw(ArgumentError("Pullback for eig_trunc! does not yet support non-zero tangent for the truncation error")) end - MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, unthunk.((ΔD, ΔV)), ind) + MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, map(unthunk, (ΔD, ΔV)), ind) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end function $eig_t_pb(::Tuple{ZeroTangent, ZeroTangent, ZeroTangent}) # is this extra definition useful? @@ -146,10 +145,9 @@ for eig in (:eig, :eigh) return DV′, $(_make_eig_t_ne_pb)(A, DV, ind) end function $(_make_eig_t_ne_pb)(A, DV, ind) - function $eig_t_ne_pb(ΔDV) + function $eig_t_ne_pb((ΔD, ΔV)) ΔA = zero(A) - ΔD, ΔV = ΔDV - MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, unthunk.((ΔD, ΔV)), ind) + MatrixAlgebraKit.$eig_pb!(ΔA, A, DV, map(unthunk, (ΔD, ΔV)), ind) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end 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) function ChainRulesCore.rrule(::typeof($svd_f!), A, USVᴴ, alg) Ac = copy_input($svd_f, A) USVᴴ = $(svd_f!)(Ac, USVᴴ, alg) - function svd_pullback(ΔUSVᴴ) + function svd_pullback((ΔU, ΔS, ΔVᴴ)) ΔA = zero(A) - MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, unthunk.(ΔUSVᴴ)) + MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, map(unthunk, (ΔU, ΔS, ΔVᴴ))) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end 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 return (USVᴴ′..., ϵ), _make_svd_trunc_pullback(A, USVᴴ, ind) end function _make_svd_trunc_pullback(A, USVᴴ, ind) - function svd_trunc_pullback(ΔUSVᴴϵ) + function svd_trunc_pullback((ΔU, ΔS, ΔVᴴ, Δϵ)) ΔA = zero(A) - ΔU, ΔS, ΔVᴴ, Δϵ = ΔUSVᴴϵ if !MatrixAlgebraKit.iszerotangent(Δϵ) && !iszero(unthunk(Δϵ)) throw(ArgumentError("Pullback for svd_trunc! does not yet support non-zero tangent for the truncation error")) end - MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, unthunk.((ΔU, ΔS, ΔVᴴ)), ind) + MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, map(unthunk, (ΔU, ΔS, ΔVᴴ)), ind) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end 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 return USVᴴ′, _make_svd_trunc_no_error_pullback(A, USVᴴ, ind) end function _make_svd_trunc_no_error_pullback(A, USVᴴ, ind) - function svd_trunc_pullback(ΔUSVᴴ) + function svd_trunc_pullback((ΔU, ΔS, ΔVᴴ)) ΔA = zero(A) - ΔU, ΔS, ΔVᴴ = ΔUSVᴴ - MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, unthunk.((ΔU, ΔS, ΔVᴴ)), ind) + MatrixAlgebraKit.svd_pullback!(ΔA, A, USVᴴ, map(unthunk, (ΔU, ΔS, ΔVᴴ)), ind) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end function svd_trunc_pullback(::Tuple{ZeroTangent, ZeroTangent, ZeroTangent}) # is this extra definition useful? @@ -249,9 +245,9 @@ end function ChainRulesCore.rrule(::typeof(left_polar!), A, WP, alg) Ac = copy_input(left_polar, A) WP = left_polar!(Ac, WP, alg) - function left_polar_pullback(ΔWP) + function left_polar_pullback((ΔW, ΔP)) ΔA = zero(A) - MatrixAlgebraKit.left_polar_pullback!(ΔA, A, WP, unthunk.(ΔWP)) + MatrixAlgebraKit.left_polar_pullback!(ΔA, A, WP, map(unthunk, (ΔW, ΔP))) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end function left_polar_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful? @@ -263,9 +259,9 @@ end function ChainRulesCore.rrule(::typeof(right_polar!), A, PWᴴ, alg) Ac = copy_input(left_polar, A) PWᴴ = right_polar!(Ac, PWᴴ, alg) - function right_polar_pullback(ΔPWᴴ) + function right_polar_pullback((ΔP, ΔWᴴ)) ΔA = zero(A) - MatrixAlgebraKit.right_polar_pullback!(ΔA, A, PWᴴ, unthunk.(ΔPWᴴ)) + MatrixAlgebraKit.right_polar_pullback!(ΔA, A, PWᴴ, map(unthunk, (ΔP, ΔWᴴ))) return NoTangent(), ΔA, ZeroTangent(), NoTangent() end function right_polar_pullback(::Tuple{ZeroTangent, ZeroTangent}) # is this extra definition useful?