Skip to content

Commit ae74eaa

Browse files
kshyattlkdvos
andauthored
Reuse truncate
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
1 parent 9da171f commit ae74eaa

1 file changed

Lines changed: 2 additions & 9 deletions

File tree

‎ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,7 @@ function EnzymeRules.forward(
560560
DV::Annotation,
561561
strategy::Annotation{<:TruncationStrategy},
562562
) 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]
563+
(Dtrunc, Vtrunc), ind = MatrixAlgebraKit.truncate(f.val, DV.val, strategy.val)
567564
dDtrunc = isa(DV, Const) ? nothing : Diagonal(diagview(DV.dval[1])[ind])
568565
dVtrunc = isa(DV, Const) ? nothing : DV.dval[2][:, ind]
569566
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
@@ -585,11 +582,7 @@ function EnzymeRules.forward(
585582
USVᴴ::Annotation,
586583
strategy::Annotation{<:TruncationStrategy},
587584
) 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, :]
585+
(Utrunc, Strunc, Vᴴtrunc), ind = MatrixAlgebraKit.truncate(f.val, USVᴴ.val, strategy.val)
593586
dUtrunc = isa(USVᴴ, Const) ? nothing : USVᴴ.dval[1][:, ind]
594587
dStrunc = isa(USVᴴ, Const) ? nothing : Diagonal(diagview(USVᴴ.dval[2])[ind])
595588
dVᴴtrunc = isa(USVᴴ, Const) ? nothing : USVᴴ.dval[3][ind, :]

0 commit comments

Comments
 (0)