Skip to content

Commit bb80d5e

Browse files
committed
Allow not checking hermiticity in eigh functions
1 parent 2ecf979 commit bb80d5e

2 files changed

Lines changed: 117 additions & 31 deletions

File tree

‎ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl‎

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ for (f, pb) in (
5555
(qr_compact!, qr_pullback!),
5656
(lq_compact!, lq_pullback!),
5757
(eig_full!, eig_pullback!),
58-
(eigh_full!, eigh_pullback!),
5958
(left_polar!, left_polar_pullback!),
6059
(right_polar!, right_polar_pullback!),
6160
)
@@ -121,7 +120,6 @@ end
121120
for (f, pf) in (
122121
(:right_polar!, :right_polar_pushforward!),
123122
(:left_polar!, :left_polar_pushforward!),
124-
(:eigh_full!, :eigh_pushforward!),
125123
(:eig_full!, :eig_pushforward!),
126124
(:qr_full!, :qr_pushforward!),
127125
(:lq_full!, :lq_pushforward!),
@@ -158,6 +156,91 @@ for (f, pf) in (
158156
end
159157
end
160158

159+
function EnzymeRules.augmented_primal(
160+
config::EnzymeRules.RevConfigWidth{1},
161+
func::Const{typeof(eigh_full!)},
162+
::Type{RT},
163+
A::Annotation,
164+
arg::Annotation{Tuple{TA, TB}},
165+
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm};
166+
kwargs...
167+
) where {RT, TA, TB}
168+
# A is overwritten in the primal, but not used in the pullback, so we do not need to cache it.
169+
ret = func.val(A.val, arg.val, alg.val; kwargs...)
170+
# if arg.val === ret, the annotation must be Duplicated or DuplicatedNoNeed
171+
# if arg isa Const, ret may still be modified further down the call graph so we should
172+
# copy it to protect ourselves
173+
A_is_arg1 = !isa(A, Const) && has_equal_storage(A.val, arg.val[1])
174+
A_is_arg2 = !isa(A, Const) && has_equal_storage(A.val, arg.val[2])
175+
A_is_arg = A_is_arg1 || A_is_arg2
176+
cache_arg = arg.val !== ret || A_is_arg || EnzymeRules.overwritten(config)[3] ? copy.(ret) : ret
177+
dret = if EnzymeRules.needs_shadow(config) && ((TA == Nothing && TB == Nothing) || isa(arg, Const))
178+
make_zero.(ret)
179+
elseif EnzymeRules.needs_shadow(config)
180+
arg.dval
181+
else
182+
nothing
183+
end
184+
primal = EnzymeRules.needs_primal(config) ? ret : nothing
185+
return EnzymeRules.AugmentedReturn(primal, dret, (cache_arg, dret))
186+
end
187+
function EnzymeRules.reverse(
188+
config::EnzymeRules.RevConfigWidth{1},
189+
func::Const{typeof(eigh_full!)},
190+
::Type{RT},
191+
cache,
192+
A::Annotation,
193+
arg::Annotation{Tuple{TA, TB}},
194+
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm};
195+
kwargs...
196+
) where {RT, TA, TB}
197+
cache_arg, darg = cache
198+
# A is not used in the pullback; since we have destroyed A, we insert Aval = nothing
199+
# to trigger an error in case the pushfward is modified to directly use A
200+
Aval = nothing
201+
A_is_arg1 = !isa(A, Const) && has_equal_storage(A.dval, arg.dval[1])
202+
A_is_arg2 = !isa(A, Const) && has_equal_storage(A.dval, arg.dval[2])
203+
A_is_arg = A_is_arg1 || A_is_arg2
204+
argval = cache_arg
205+
if !isa(A, Const)
206+
ΔA = A_is_arg ? make_zero(A.dval) : A.dval
207+
eigh_pullback!(ΔA, Aval, argval, darg)
208+
A_is_arg && (A.dval .= ΔA)
209+
end
210+
if !isa(arg, Const)
211+
A_is_arg1 || make_zero!(arg.dval[1])
212+
A_is_arg2 || make_zero!(arg.dval[2])
213+
end
214+
return (nothing, nothing, nothing)
215+
end
216+
function EnzymeRules.forward(
217+
config::EnzymeRules.FwdConfigWidth{1},
218+
func::Const{typeof(eigh_full!)},
219+
::Type{RT},
220+
A::Annotation,
221+
arg::Annotation,
222+
alg::Annotation{<:MatrixAlgebraKit.AbstractAlgorithm};
223+
kwargs...
224+
) where {RT}
225+
A_is_arg1 = !isa(A, Const) && has_equal_storage(A.val, arg.val[1])
226+
A_is_arg2 = !isa(A, Const) && has_equal_storage(A.val, arg.val[2])
227+
A_is_arg = A_is_arg1 || A_is_arg2
228+
eigh_full!(A.val, arg.val, alg.val)
229+
if !isa(A, Const) && !isa(arg, Const)
230+
eigh_pushforward!(A.dval, A.val, arg.val, arg.dval)
231+
end
232+
!A_is_arg && make_zero!(A.dval)
233+
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
234+
return arg
235+
elseif EnzymeRules.needs_primal(config)
236+
return arg.val
237+
elseif EnzymeRules.needs_shadow(config)
238+
return arg.dval
239+
else
240+
return nothing
241+
end
242+
end
243+
161244
for (f, pb, pf) in (
162245
(qr_null!, qr_null_pullback!, qr_null_pushforward!),
163246
(lq_null!, lq_null_pullback!, lq_null_pushforward!),
@@ -415,10 +498,11 @@ for (f!, f_full!, pb!, pf!) in (
415498
::Type{RT},
416499
A::Annotation,
417500
D::Annotation{TD},
418-
alg::Annotation{<:MatrixAlgebraKit.AbstractAlgorithm},
501+
alg::Annotation{<:MatrixAlgebraKit.AbstractAlgorithm};
502+
kwargs...
419503
) where {RT, TD}
420504
# A is overwritten in the primal, but not used in the pullback, so we do not need to cache it.
421-
nD, V = $f_full!(A.val, alg.val)
505+
nD, V = $f_full!(A.val, alg.val; kwargs...)
422506
ret = TD == Nothing ? diagview(nD) : copy!(D.val, diagview(nD))
423507
cache_D = (D.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy(ret) : nothing
424508
primal = EnzymeRules.needs_primal(config) ? ret : nothing
@@ -435,7 +519,8 @@ for (f!, f_full!, pb!, pf!) in (
435519
cache,
436520
A::Annotation{TA},
437521
D::Annotation,
438-
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
522+
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm};
523+
kwargs...
439524
) where {RT, TA}
440525
cache_D, dD, V = cache
441526
Dval = something(cache_D, D.val)
@@ -457,10 +542,11 @@ for (f!, f_full!, pb!, pf!) in (
457542
::Type{RT},
458543
A::Annotation{TA},
459544
D::Annotation,
460-
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
545+
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm};
546+
kwargs...
461547
) where {RT, TA}
462548
A_is_arg = !isa(A, Const) && has_equal_storage(A.dval, D.dval)
463-
DV = $f_full!(A.val, alg.val)
549+
DV = $f_full!(A.val, alg.val; kwargs...)
464550
Dval, V = DV
465551
if !isa(A, Const) && !isa(D, Const)
466552
ΔD = A_is_arg ? make_zero(D.dval) : D.dval

‎src/implementations/eigh.jl‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function check_hermitian(A; atol::Real = default_hermitian_tol(A), rtol::Real =
1717
return nothing
1818
end
1919

20-
function check_input(::typeof(eigh_full!), A::AbstractMatrix, DV, alg::AbstractAlgorithm)
21-
check_hermitian(A, alg)
20+
function check_input(::typeof(eigh_full!), A::AbstractMatrix, DV, alg::AbstractAlgorithm; check_hermiticity::Bool = true)
21+
check_hermiticity && check_hermitian(A, alg)
2222
D, V = DV
2323
m = size(A, 1)
2424
@assert D isa Diagonal && V isa AbstractMatrix
@@ -28,17 +28,17 @@ function check_input(::typeof(eigh_full!), A::AbstractMatrix, DV, alg::AbstractA
2828
@check_scalar(V, A)
2929
return nothing
3030
end
31-
function check_input(::typeof(eigh_vals!), A::AbstractMatrix, D, alg::AbstractAlgorithm)
32-
check_hermitian(A, alg)
31+
function check_input(::typeof(eigh_vals!), A::AbstractMatrix, D, alg::AbstractAlgorithm; check_hermiticity::Bool = true)
32+
check_hermiticity && check_hermitian(A, alg)
3333
m = size(A, 1)
3434
@assert D isa AbstractVector
3535
@check_size(D, (m,))
3636
@check_scalar(D, A, real)
3737
return nothing
3838
end
3939

40-
function check_input(::typeof(eigh_full!), A::AbstractMatrix, DV, alg::DiagonalAlgorithm)
41-
check_hermitian(A, alg)
40+
function check_input(::typeof(eigh_full!), A::AbstractMatrix, DV, alg::DiagonalAlgorithm; check_hermiticity::Bool = true)
41+
check_hermiticity && check_hermitian(A, alg)
4242
@assert isdiag(A)
4343
m = size(A, 1)
4444
D, V = DV
@@ -50,8 +50,8 @@ function check_input(::typeof(eigh_full!), A::AbstractMatrix, DV, alg::DiagonalA
5050
return nothing
5151
end
5252

53-
function check_input(::typeof(eigh_vals!), A::AbstractMatrix, D, alg::DiagonalAlgorithm)
54-
check_hermitian(A, alg)
53+
function check_input(::typeof(eigh_vals!), A::AbstractMatrix, D, alg::DiagonalAlgorithm; check_hermiticity::Bool = true)
54+
check_hermiticity && check_hermitian(A, alg)
5555
@assert isdiag(A)
5656
m = size(A, 1)
5757
@assert D isa AbstractVector
@@ -87,11 +87,11 @@ end
8787
# DefaultAlgorithm intercepts
8888
# ---------------------------
8989
for f! in (:eigh_full!, :eigh_vals!, :eigh_trunc!, :eigh_trunc_no_error!)
90-
@eval function $f!(A::AbstractMatrix, alg::DefaultAlgorithm)
91-
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
90+
@eval function $f!(A::AbstractMatrix, alg::DefaultAlgorithm; check_hermiticity::Bool = true)
91+
return $f!(A, select_algorithm($f!, A, nothing; check_hermiticity, alg.kwargs...))
9292
end
93-
@eval function $f!(A::AbstractMatrix, out, alg::DefaultAlgorithm)
94-
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
93+
@eval function $f!(A::AbstractMatrix, out, alg::DefaultAlgorithm; check_hermiticity::Bool = true)
94+
return $f!(A, out, select_algorithm($f!, A, nothing; check_hermiticity, alg.kwargs...))
9595
end
9696
end
9797

@@ -120,13 +120,13 @@ for (f, f_lapack!, Alg) in (
120120

121121
# MatrixAlgebraKit wrappers
122122
@eval begin
123-
function eigh_full!(A::AbstractMatrix, DV, alg::$Alg)
124-
check_input(eigh_full!, A, DV, alg)
123+
function eigh_full!(A::AbstractMatrix, DV, alg::$Alg; check_hermiticity::Bool = true)
124+
check_input(eigh_full!, A, DV, alg; check_hermiticity)
125125
$eigh_full_f!(A, DV; alg.kwargs...)
126126
return DV
127127
end
128-
function eigh_vals!(A::AbstractMatrix, D, alg::$Alg)
129-
check_input(eigh_vals!, A, D, alg)
128+
function eigh_vals!(A::AbstractMatrix, D, alg::$Alg; check_hermiticity::Bool = true)
129+
check_input(eigh_vals!, A, D, alg; check_hermiticity)
130130
$eigh_vals_f!(A, D; alg.kwargs...)
131131
return D
132132
end
@@ -162,22 +162,22 @@ for (f, f_lapack!, Alg) in (
162162
end
163163
end
164164

165-
function eigh_trunc!(A, DV, alg::TruncatedAlgorithm)
166-
D, V = eigh_full!(A, DV, alg.alg)
165+
function eigh_trunc!(A, DV, alg::TruncatedAlgorithm; check_hermiticity::Bool = true)
166+
D, V = eigh_full!(A, DV, alg.alg; check_hermiticity)
167167
DVtrunc, ind = truncate(eigh_trunc!, (D, V), alg.trunc)
168168
return DVtrunc..., truncation_error!(diagview(D), ind)
169169
end
170170

171-
function eigh_trunc_no_error!(A, DV, alg::TruncatedAlgorithm)
172-
D, V = eigh_full!(A, DV, alg.alg)
171+
function eigh_trunc_no_error!(A, DV, alg::TruncatedAlgorithm; check_hermiticity::Bool = true)
172+
D, V = eigh_full!(A, DV, alg.alg; check_hermiticity)
173173
DVtrunc, ind = truncate(eigh_trunc!, (D, V), alg.trunc)
174174
return DVtrunc
175175
end
176176

177177
# Diagonal logic
178178
# --------------
179-
function eigh_full!(A::Diagonal, DV, alg::DiagonalAlgorithm)
180-
check_input(eigh_full!, A, DV, alg)
179+
function eigh_full!(A::Diagonal, DV, alg::DiagonalAlgorithm; check_hermiticity::Bool = true)
180+
check_input(eigh_full!, A, DV, alg; check_hermiticity)
181181
D, V = DV
182182
diagA = diagview(A)
183183
I = sortperm(diagA; by = real)
@@ -193,8 +193,8 @@ function eigh_full!(A::Diagonal, DV, alg::DiagonalAlgorithm)
193193
return D, V
194194
end
195195

196-
function eigh_vals!(A::Diagonal, D, alg::DiagonalAlgorithm)
197-
check_input(eigh_vals!, A, D, alg)
196+
function eigh_vals!(A::Diagonal, D, alg::DiagonalAlgorithm; check_hermiticity::Bool = true)
197+
check_input(eigh_vals!, A, D, alg; check_hermiticity)
198198
Ad = diagview(A)
199199
if D === Ad
200200
sort!(Ad)

0 commit comments

Comments
 (0)