@@ -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,56 @@ 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:: Const{<:TruncationStrategy} ,
562+ ) where {RT}
563+ D, V = DV. val
564+ ind = MatrixAlgebraKit. findtruncated (diagview (D), strategy. val)
565+ Dtrunc = Diagonal (diagview (D)[ind])
566+ Vtrunc = V[:, ind]
567+ dDtrunc = isa (DV, Const) ? nothing : Diagonal (diagview (DV. dval[1 ])[ind])
568+ dVtrunc = isa (DV, Const) ? nothing : DV. dval[2 ][:, ind]
569+ if EnzymeRules. needs_primal (config) && EnzymeRules. needs_shadow (config)
570+ return Duplicated (((Dtrunc, Vtrunc), ind), ((dDtrunc, dVtrunc), make_zero (ind)))
571+ elseif EnzymeRules. needs_primal (config)
572+ return ((Dtrunc, Vtrunc), ind)
573+ elseif EnzymeRules. needs_shadow (config)
574+ return ((dDtrunc, dVtrunc), make_zero (ind))
575+ else
576+ return nothing
577+ end
578+ end
579+
580+ function EnzymeRules. forward (
581+ config:: EnzymeRules.FwdConfigWidth{1} ,
582+ func:: Const{typeof(truncate)} ,
583+ :: Type{RT} ,
584+ f:: Const{typeof(svd_trunc!)} ,
585+ USVᴴ:: Annotation ,
586+ strategy:: Const{<:TruncationStrategy} ,
587+ ) where {RT}
588+ U, S, Vᴴ = USVᴴ. val
589+ ind = MatrixAlgebraKit. findtruncated_svd (diagview (S), strategy. val)
590+ Utrunc = U[:, ind]
591+ Strunc = Diagonal (diagview (S)[ind])
592+ Vᴴtrunc = Vᴴ[ind, :]
593+ dUtrunc = isa (USVᴴ, Const) ? nothing : USVᴴ. dval[1 ][:, ind]
594+ dStrunc = isa (USVᴴ, Const) ? nothing : Diagonal (diagview (USVᴴ. dval[2 ])[ind])
595+ dVᴴtrunc = isa (USVᴴ, Const) ? nothing : USVᴴ. dval[3 ][ind, :]
596+ if EnzymeRules. needs_primal (config) && EnzymeRules. needs_shadow (config)
597+ return Duplicated (((Utrunc, Strunc, Vᴴtrunc), ind), ((dUtrunc, dStrunc, dVᴴtrunc), make_zero (ind)))
598+ elseif EnzymeRules. needs_primal (config)
599+ return ((Utrunc, Strunc, Vᴴtrunc), ind)
600+ elseif EnzymeRules. needs_shadow (config)
601+ return ((dUtrunc, dStrunc, dVᴴtrunc), make_zero (ind))
602+ else
603+ return nothing
604+ end
605+ end
606+
555607end
0 commit comments