Skip to content

Commit 717487d

Browse files
committed
Tests
1 parent 60c8533 commit 717487d

7 files changed

Lines changed: 149 additions & 7 deletions

File tree

‎src/common/pullbacks.jl‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ function _sylvester_psd(U, d, C, tol)
2121
return U * Y * U', Δ
2222
end
2323

24+
# Remove from `X` its block within the null space of the Hermitian positive semidefinite
25+
# `P = U * Diagonal(d) * U'`, i.e. the eigenvectors with eigenvalues smaller than `tol`.
26+
function _remove_null_block!(X, U, d, tol)
27+
isnull = abs.(d) .< tol
28+
any(isnull) || return X
29+
Y = U' * X * U
30+
Y .*= isnull .& transpose(isnull)
31+
return mul!(X, U, Y * U', -1, 1)
32+
end
33+
2434
"""
2535
select_indices(r::AbstractRange, ind)
2636

‎src/pullbacks/polar.jl‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ cotangent `ΔWP` of `left_polar(A)`.
1010
1111
Eigenvalues of `P` smaller than `rank_atol` are treated as zero, in which case `A` is rank
1212
deficient and `W` is not uniquely determined on the null space of `P`. Components of `ΔW`
13-
along this null space are discarded.
13+
along this null space are discarded. The block of `ΔP` within the null space, where `P`
14+
is not differentiable, is discarded as well.
1415
"""
1516
function left_polar_pullback!(
1617
ΔA::AbstractMatrix, A, WP, ΔWP;
@@ -22,11 +23,12 @@ function left_polar_pullback!(
2223

2324
# Extract and check the cotangents
2425
ΔW, ΔP = ΔWP
26+
D, U = eigh_full(P)
27+
d = diagview(D)
2528
if !iszerotangent(ΔP)
2629
ΔP = project_hermitian(ΔP)
30+
ΔP = _remove_null_block!(ΔP, U, d, rank_atol)
2731
end
28-
D, U = eigh_full(P)
29-
d = diagview(D)
3032
M = zero(P)
3133
!iszerotangent(ΔW) && mul!(M, W', ΔW, 1, 1)
3234
!iszerotangent(ΔP) && mul!(M, ΔP, P, -1, 1)
@@ -59,7 +61,8 @@ and cotangent `ΔPWᴴ` of `right_polar(A)`.
5961
6062
Eigenvalues of `P` smaller than `rank_atol` are treated as zero, in which case `A` is rank
6163
deficient and `Wᴴ` is not uniquely determined on the null space of `P`. Components of `ΔWᴴ`
62-
along this null space are discarded.
64+
along this null space are discarded. The block of `ΔP` within the null space, where `P` is
65+
not differentiable, is discarded as well.
6366
"""
6467
function right_polar_pullback!(
6568
ΔA::AbstractMatrix, A, PWᴴ, ΔPWᴴ;
@@ -71,11 +74,12 @@ function right_polar_pullback!(
7174

7275
# Extract and check the cotangents
7376
ΔP, ΔWᴴ = ΔPWᴴ
77+
D, U = eigh_full(P)
78+
d = diagview(D)
7479
if !iszerotangent(ΔP)
7580
ΔP = project_hermitian(ΔP)
81+
ΔP = _remove_null_block!(ΔP, U, d, rank_atol)
7682
end
77-
D, U = eigh_full(P)
78-
d = diagview(D)
7983
M = zero(P)
8084
!iszerotangent(ΔWᴴ) && mul!(M, ΔWᴴ, Wᴴ', 1, 1)
8185
!iszerotangent(ΔP) && mul!(M, P, ΔP, -1, 1)

‎src/pushforwards/polar.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function left_polar_pushforward!(
1515
mul!(ΔW, W, WᴴdAiP, -1, +1)
1616
ΔW = mul!(ΔW, W, K̇, +1, +1)
1717
ΔP = mul!(ΔP, K̇, P, -1, +1)
18+
ΔP = _remove_null_block!(ΔP, U, d, rank_atol)
1819
return (ΔW, ΔP)
1920
end
2021

@@ -35,5 +36,6 @@ function right_polar_pushforward!(
3536
mul!(ΔWᴴ, iPdAW, Wᴴ, -1, +1)
3637
ΔWᴴ = mul!(ΔWᴴ, K̇, Wᴴ, +1, +1)
3738
ΔP = mul!(ΔP, P, K̇, -1, +1)
39+
ΔP = _remove_null_block!(ΔP, U, d, rank_atol)
3840
return (ΔWᴴ, ΔP)
3941
end

‎test/testsuite/ad_utils.jl‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ project_antihermitian_inplace!(A, alg) = project_antihermitian!(A, A, alg)
4141

4242

4343
enzyme_fdm(T) = eltype(T) <: Union{Float32, ComplexF32} ? EnzymeTestUtils.FiniteDifferences.central_fdm(5, 1, max_range = 1.0e-2) : EnzymeTestUtils.FiniteDifferences.central_fdm(5, 1)
44-
44+
const rank_deficient_fdm = EnzymeTestUtils.FiniteDifferences.central_fdm(5, 1, max_range = 1.0e-4)
4545

4646
"""
4747
eigh_wrapper(f, A, alg)
@@ -376,6 +376,54 @@ function ad_right_polar_setup(A)
376376
return PWᴴ, ΔPWᴴ
377377
end
378378

379+
"""
380+
left_polar_rank_deficient_wrapper(f, L, R, alg)
381+
382+
Wrapper that calls `W, P = f(L * R, alg)` for a rank-deficient product `L * R` and returns
383+
`W * P` and `P`. Parametrizing the input by its factors restricts FD to rank-preserving directions.
384+
"""
385+
left_polar_rank_deficient_wrapper(f, L, R, alg) = left_polar_gauge_invariant_part(f(L * R, alg))
386+
387+
"""
388+
left_polar!_rank_deficient_wrapper(f!, L, R, alg)
389+
390+
In-place variant of [`left_polar_rank_deficient_wrapper`](@ref).
391+
"""
392+
left_polar!_rank_deficient_wrapper(f!, L, R, alg) =
393+
left_polar_gauge_invariant_part(call_and_zero!(f!, L * R, alg))
394+
395+
left_polar_gauge_invariant_part((W, P)) = (W * P, P)
396+
397+
"""
398+
right_polar_rank_deficient_wrapper(f, L, R, alg)
399+
400+
Wrapper that calls `P, Wᴴ = f(L * R, alg)` for a rank-deficient product `L * R` and returns
401+
`P` and `P * Wᴴ`. Parametrizing the input by its factors restricts FD to rank-preserving directions.
402+
"""
403+
right_polar_rank_deficient_wrapper(f, L, R, alg) = right_polar_gauge_invariant_part(f(L * R, alg))
404+
405+
"""
406+
right_polar!_rank_deficient_wrapper(f!, L, R, alg)
407+
408+
In-place variant of [`right_polar_rank_deficient_wrapper`](@ref).
409+
"""
410+
right_polar!_rank_deficient_wrapper(f!, L, R, alg) =
411+
right_polar_gauge_invariant_part(call_and_zero!(f!, L * R, alg))
412+
413+
right_polar_gauge_invariant_part((P, Wᴴ)) = (P, P * Wᴴ)
414+
415+
"""
416+
polar_P_wrapper(f, A, alg)
417+
418+
Wrapper that returns only the positive factor `P` of the polar decomposition `f(A, alg)`.
419+
"""
420+
polar_P_wrapper(f, A, alg) = (F = f(A, alg); f === left_polar ? F[2] : F[1])
421+
422+
function rank_deficient_factors(T, (m, n))
423+
r = div(min(m, n), 2)
424+
return instantiate_matrix(T, (m, r)), instantiate_matrix(T, (r, n))
425+
end
426+
379427
function ad_left_orth_setup(A)
380428
VC = left_orth(A)
381429
ΔVC = structured_randn!.(similar.(VC))

‎test/testsuite/chainrules.jl‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,17 @@ function test_chainrules_polar(
691691
atol = atol, rtol = rtol, rrule_f = rrule_via_ad, check_inferred = false
692692
)
693693
@test isequal(ΔWP, ΔWP_copy)
694+
695+
Ar = instantiate_rank_deficient_matrix(T, sz)
696+
test_rrule(
697+
config, polar_P_wrapper, left_polar ⊢ NoTangent(), Ar, alg ⊢ NoTangent();
698+
atol = atol, rtol = rtol, rrule_f = rrule_via_ad, check_inferred = false
699+
)
700+
L, R = rank_deficient_factors(T, sz)
701+
test_rrule(
702+
config, left_polar_rank_deficient_wrapper, left_polar ⊢ NoTangent(), L, R, alg ⊢ NoTangent();
703+
atol = atol, rtol = rtol, rrule_f = rrule_via_ad, check_inferred = false
704+
)
694705
end
695706
end
696707
@testset "right_polar" begin
@@ -708,6 +719,17 @@ function test_chainrules_polar(
708719
atol = atol, rtol = rtol, rrule_f = rrule_via_ad, check_inferred = false
709720
)
710721
@test isequal(ΔPWᴴ, ΔPWᴴ_copy)
722+
723+
Ar = instantiate_rank_deficient_matrix(T, sz)
724+
test_rrule(
725+
config, polar_P_wrapper, right_polar ⊢ NoTangent(), Ar, alg ⊢ NoTangent();
726+
atol = atol, rtol = rtol, rrule_f = rrule_via_ad, check_inferred = false
727+
)
728+
L, R = rank_deficient_factors(T, sz)
729+
test_rrule(
730+
config, right_polar_rank_deficient_wrapper, right_polar ⊢ NoTangent(), L, R, alg ⊢ NoTangent();
731+
atol = atol, rtol = rtol, rrule_f = rrule_via_ad, check_inferred = false
732+
)
711733
end
712734
end
713735
end

‎test/testsuite/enzyme/polar.jl‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ function test_enzyme_left_polar(
3232
A = instantiate_matrix(T, sz)
3333
test_forward(left_polar, RT, (A, TA), (alg, Const); atol, rtol)
3434
test_forward(call_and_zero!, RT, (left_polar!, Const), (A, TA), (alg, Const); atol, rtol)
35+
36+
A = instantiate_rank_deficient_matrix(T, sz)
37+
test_reverse(polar_P_wrapper, RT, (left_polar, Const), (A, TA), (alg, Const); atol, rtol, fdm = rank_deficient_fdm)
38+
test_forward(polar_P_wrapper, RT, (left_polar, Const), (A, TA), (alg, Const); atol, rtol, fdm = rank_deficient_fdm)
39+
# TODO: Enzyme can't diff through complex BLAS gemm
40+
if eltype(T) <: Real
41+
L, R = rank_deficient_factors(T, sz)
42+
for wrapper in (left_polar_rank_deficient_wrapper, left_polar!_rank_deficient_wrapper)
43+
f = wrapper === left_polar_rank_deficient_wrapper ? left_polar : left_polar!
44+
test_reverse(wrapper, RT, (f, Const), (L, TA), (R, TA), (alg, Const); atol, rtol)
45+
test_forward(wrapper, RT, (f, Const), (L, TA), (R, TA), (alg, Const); atol, rtol)
46+
end
47+
end
3548
end
3649
end
3750
end
@@ -57,6 +70,19 @@ function test_enzyme_right_polar(
5770
A = instantiate_matrix(T, sz)
5871
test_forward(right_polar, RT, (A, TA), (alg, Const); atol, rtol)
5972
test_forward(call_and_zero!, RT, (right_polar!, Const), (A, TA), (alg, Const); atol, rtol)
73+
74+
A = instantiate_rank_deficient_matrix(T, sz)
75+
test_reverse(polar_P_wrapper, RT, (right_polar, Const), (A, TA), (alg, Const); atol, rtol, fdm = rank_deficient_fdm)
76+
test_forward(polar_P_wrapper, RT, (right_polar, Const), (A, TA), (alg, Const); atol, rtol, fdm = rank_deficient_fdm)
77+
# TODO: Enzyme can't diff through complex BLAS gemm
78+
if eltype(T) <: Real
79+
L, R = rank_deficient_factors(T, sz)
80+
for wrapper in (right_polar_rank_deficient_wrapper, right_polar!_rank_deficient_wrapper)
81+
f = wrapper === right_polar_rank_deficient_wrapper ? right_polar : right_polar!
82+
test_reverse(wrapper, RT, (f, Const), (L, TA), (R, TA), (alg, Const); atol, rtol)
83+
test_forward(wrapper, RT, (f, Const), (L, TA), (R, TA), (alg, Const); atol, rtol)
84+
end
85+
end
6086
end
6187
end
6288
end

‎test/testsuite/mooncake/polar.jl‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ function test_mooncake_left_polar(
3737
rng, call_and_zero!, left_polar!, A, alg;
3838
output_tangent, atol, rtol, is_primitive = false
3939
)
40+
41+
A = instantiate_rank_deficient_matrix(T, (m, n))
42+
Mooncake.TestUtils.test_rule(
43+
rng, polar_P_wrapper, left_polar, A, alg;
44+
atol, rtol, is_primitive = false
45+
)
46+
L, R = rank_deficient_factors(T, (m, n))
47+
Mooncake.TestUtils.test_rule(
48+
rng, left_polar_rank_deficient_wrapper, left_polar, L, R, alg;
49+
atol, rtol, is_primitive = false
50+
)
51+
Mooncake.TestUtils.test_rule(
52+
rng, left_polar!_rank_deficient_wrapper, left_polar!, L, R, alg;
53+
atol, rtol, is_primitive = false
54+
)
4055
end
4156
end
4257

@@ -66,5 +81,20 @@ function test_mooncake_right_polar(
6681
rng, call_and_zero!, right_polar!, A, alg;
6782
output_tangent, atol, rtol, is_primitive = false
6883
)
84+
85+
A = instantiate_rank_deficient_matrix(T, (m, n))
86+
Mooncake.TestUtils.test_rule(
87+
rng, polar_P_wrapper, right_polar, A, alg;
88+
atol, rtol, is_primitive = false
89+
)
90+
L, R = rank_deficient_factors(T, (m, n))
91+
Mooncake.TestUtils.test_rule(
92+
rng, right_polar_rank_deficient_wrapper, right_polar, L, R, alg;
93+
atol, rtol, is_primitive = false
94+
)
95+
Mooncake.TestUtils.test_rule(
96+
rng, right_polar!_rank_deficient_wrapper, right_polar!, L, R, alg;
97+
atol, rtol, is_primitive = false
98+
)
6999
end
70100
end

0 commit comments

Comments
 (0)