@@ -2,7 +2,7 @@ module MatrixAlgebraKitEnzymeExt
22
33using MatrixAlgebraKit
44using MatrixAlgebraKit: copy_input, initialize_output, zero!, has_equal_storage
5- using MatrixAlgebraKit: diagview, inv_safe, truncate
5+ using MatrixAlgebraKit: diagview, inv_safe, truncate, TruncationStrategy
66using MatrixAlgebraKit: qr_pullback!, lq_pullback!
77using MatrixAlgebraKit: qr_pushforward!, lq_pushforward!
88using MatrixAlgebraKit: qr_null_pullback!, lq_null_pullback!
@@ -19,7 +19,7 @@ using Enzyme.EnzymeCore: EnzymeRules
1919using LinearAlgebra
2020
2121@inline EnzymeRules. inactive_type (:: Type{Alg} ) where {Alg <: MatrixAlgebraKit.AbstractAlgorithm } = true
22- @inline EnzymeRules. inactive_type (:: Type{TS} ) where {TS <: MatrixAlgebraKit. TruncationStrategy } = true
22+ @inline EnzymeRules. inactive_type (:: Type{TS} ) where {TS <: TruncationStrategy } = true
2323@inline EnzymeRules. inactive (:: typeof (MatrixAlgebraKit. select_algorithm), func:: F , A:: AbstractMatrix , alg:: Alg ) where {F, Alg} = true
2424@inline EnzymeRules. inactive (:: typeof (MatrixAlgebraKit. default_algorithm), func:: F , A:: AbstractMatrix ) where {F} = true
2525@inline EnzymeRules. inactive (:: typeof (MatrixAlgebraKit. check_input), func:: F , A:: AbstractMatrix , alg:: Alg ) where {F, Alg} = true
@@ -552,4 +552,49 @@ function EnzymeRules.forward(
552552 end
553553end
554554
555+ function EnzymeRules. forward (
556+ config:: EnzymeRules.FwdConfigWidth{1} ,
557+ func:: Const{typeof(truncate)} ,
558+ :: Type{RT} ,
559+ f:: Const{<:Union{typeof(eigh_trunc!), typeof(eig_trunc!)}} ,
560+ DV:: Annotation ,
561+ strategy:: Annotation{<:TruncationStrategy} ,
562+ ) where {RT}
563+ (Dtrunc, Vtrunc), ind = MatrixAlgebraKit. truncate (f. val, DV. val, strategy. val)
564+ dDtrunc = isa (DV, Const) ? nothing : Diagonal (diagview (DV. dval[1 ])[ind])
565+ dVtrunc = isa (DV, Const) ? nothing : DV. dval[2 ][:, ind]
566+ if EnzymeRules. needs_primal (config) && EnzymeRules. needs_shadow (config)
567+ return Duplicated (((Dtrunc, Vtrunc), ind), ((dDtrunc, dVtrunc), make_zero (ind)))
568+ elseif EnzymeRules. needs_primal (config)
569+ return ((Dtrunc, Vtrunc), ind)
570+ elseif EnzymeRules. needs_shadow (config)
571+ return ((dDtrunc, dVtrunc), make_zero (ind))
572+ else
573+ return nothing
574+ end
575+ end
576+
577+ function EnzymeRules. forward (
578+ config:: EnzymeRules.FwdConfigWidth{1} ,
579+ func:: Const{typeof(truncate)} ,
580+ :: Type{RT} ,
581+ f:: Const{typeof(svd_trunc!)} ,
582+ USVᴴ:: Annotation ,
583+ strategy:: Annotation{<:TruncationStrategy} ,
584+ ) where {RT}
585+ (Utrunc, Strunc, Vᴴtrunc), ind = MatrixAlgebraKit. truncate (f. val, USVᴴ. val, strategy. val)
586+ dUtrunc = isa (USVᴴ, Const) ? nothing : USVᴴ. dval[1 ][:, ind]
587+ dStrunc = isa (USVᴴ, Const) ? nothing : Diagonal (diagview (USVᴴ. dval[2 ])[ind])
588+ dVᴴtrunc = isa (USVᴴ, Const) ? nothing : USVᴴ. dval[3 ][ind, :]
589+ if EnzymeRules. needs_primal (config) && EnzymeRules. needs_shadow (config)
590+ return Duplicated (((Utrunc, Strunc, Vᴴtrunc), ind), ((dUtrunc, dStrunc, dVᴴtrunc), make_zero (ind)))
591+ elseif EnzymeRules. needs_primal (config)
592+ return ((Utrunc, Strunc, Vᴴtrunc), ind)
593+ elseif EnzymeRules. needs_shadow (config)
594+ return ((dUtrunc, dStrunc, dVᴴtrunc), make_zero (ind))
595+ else
596+ return nothing
597+ end
598+ end
599+
555600end
0 commit comments