Skip to content

Commit 183370d

Browse files
authored
Forward rules and tests for project functions (#244)
* Forward rules and tests for project functions * Fix Mooncake
1 parent 584e948 commit 183370d

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎ext/MatrixAlgebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ for (f!, f, adj) in (
858858
(:project_antihermitian!, :project_antihermitian, :project_antihermitian_adjoint),
859859
)
860860
@eval begin
861-
@is_primitive DefaultCtx Mooncake.ReverseMode Tuple{typeof($f!), Any, Any, MatrixAlgebraKit.AbstractAlgorithm}
861+
@is_primitive DefaultCtx Tuple{typeof($f!), Any, Any, MatrixAlgebraKit.AbstractAlgorithm}
862862
function Mooncake.rrule!!(f_df::CoDual{typeof($f!)}, A_dA::CoDual, arg_darg::CoDual, alg_dalg::CoDual{<:MatrixAlgebraKit.AbstractAlgorithm})
863863
A, dA = arrayify(A_dA)
864864
arg, darg = A_dA === arg_darg ? (A, dA) : arrayify(arg_darg)
@@ -879,8 +879,14 @@ for (f!, f, adj) in (
879879

880880
return arg_darg, $adj
881881
end
882-
883-
@is_primitive DefaultCtx Mooncake.ReverseMode Tuple{typeof($f), Any, MatrixAlgebraKit.AbstractAlgorithm}
882+
function Mooncake.frule!!(f_df::Dual{typeof($f!)}, A_dA::Dual, arg_darg::Dual, alg_dalg::Dual{<:MatrixAlgebraKit.AbstractAlgorithm})
883+
A, dA = arrayify(A_dA)
884+
arg, darg = A_dA === arg_darg ? (A, dA) : arrayify(arg_darg)
885+
$f!(A, arg, Mooncake.primal(alg_dalg))
886+
$f!(dA, darg, Mooncake.primal(alg_dalg))
887+
return arg_darg
888+
end
889+
@is_primitive DefaultCtx Tuple{typeof($f), Any, MatrixAlgebraKit.AbstractAlgorithm}
884890
function Mooncake.rrule!!(f_df::CoDual{typeof($f)}, A_dA::CoDual, alg_dalg::CoDual{<:MatrixAlgebraKit.AbstractAlgorithm})
885891
A, dA = arrayify(A_dA)
886892
output = $f(A, Mooncake.primal(alg_dalg))
@@ -896,6 +902,14 @@ for (f!, f, adj) in (
896902

897903
return output_doutput, $adj
898904
end
905+
function Mooncake.frule!!(f_df::Dual{typeof($f)}, A_dA::Dual, alg_dalg::Dual{<:MatrixAlgebraKit.AbstractAlgorithm})
906+
A, dA = arrayify(A_dA)
907+
output = $f(A, Mooncake.primal(alg_dalg))
908+
output_doutput = Mooncake.zero_dual(output)
909+
doutput = last(arrayify(output_doutput))
910+
$f!(dA, doutput, Mooncake.primal(alg_dalg))
911+
return output_doutput
912+
end
899913
end
900914
end
901915

‎test/testsuite/enzyme/projections.jl‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,45 @@ end
1515
"""
1616
test_enzyme_project_hermitian(T, sz; rng, atol, rtol)
1717
18-
Test the Enzyme reverse-mode AD rule for `project_hermitian` and its in-place variant.
18+
Test the Enzyme forward- and reverse-mode AD rule for `project_hermitian` and its in-place variant.
1919
"""
2020
function test_enzyme_project_hermitian(
2121
T, sz;
2222
rng = Random.default_rng(), atol::Real = 0, rtol::Real = precision(T),
2323
fdm = enzyme_fdm(T)
2424
)
25-
return @testset "project_hermitian reverse: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
25+
return @testset "project_hermitian: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
2626
A = instantiate_matrix(T, sz)
2727
B = instantiate_matrix(T, sz)
2828
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
2929
test_reverse(project_hermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
3030
test_reverse(project_hermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
3131
test_reverse(project_hermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
32+
test_forward(project_hermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
33+
test_forward(project_hermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
34+
test_forward(project_hermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
3235
end
3336
end
3437

3538
"""
3639
test_enzyme_project_antihermitian(T, sz; rng, atol, rtol)
3740
38-
Test the Enzyme reverse-mode AD rule for `project_antihermitian` and its in-place variant.
41+
Test the Enzyme forward- and reverse-mode AD rule for `project_antihermitian` and its in-place variant.
3942
"""
4043
function test_enzyme_project_antihermitian(
4144
T, sz;
4245
rng = Random.default_rng(), atol::Real = 0, rtol::Real = precision(T),
4346
fdm = enzyme_fdm(T)
4447
)
45-
return @testset "project_antihermitian reverse: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
48+
return @testset "project_antihermitian: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
4649
A = instantiate_matrix(T, sz)
4750
B = instantiate_matrix(T, sz)
4851
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
4952
test_reverse(project_antihermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
5053
test_reverse(project_antihermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
5154
test_reverse(project_antihermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
55+
test_forward(project_antihermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
56+
test_forward(project_antihermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
57+
test_forward(project_antihermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
5258
end
5359
end

‎test/testsuite/mooncake/projections.jl‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515
"""
1616
test_mooncake_project_hermitian(T, sz; rng, atol, rtol)
1717
18-
Test the Mooncake reverse-mode AD rule for `project_hermitian` and its in-place variant.
18+
Test the Mooncake forward- and reverse-mode AD rule for `project_hermitian` and its in-place variant.
1919
"""
2020
function test_mooncake_project_hermitian(
2121
T, sz;
@@ -27,23 +27,23 @@ function test_mooncake_project_hermitian(
2727
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
2828
Mooncake.TestUtils.test_rule(
2929
rng, project_hermitian, A, alg;
30-
mode = Mooncake.ReverseMode, atol, rtol
30+
atol, rtol
3131
)
3232
Mooncake.TestUtils.test_rule(
3333
rng, project_hermitian!, A, A, alg;
34-
mode = Mooncake.ReverseMode, atol, rtol
34+
atol, rtol
3535
)
3636
Mooncake.TestUtils.test_rule(
3737
rng, project_hermitian!, A, B, alg;
38-
mode = Mooncake.ReverseMode, atol, rtol
38+
atol, rtol
3939
)
4040
end
4141
end
4242

4343
"""
4444
test_mooncake_project_antihermitian(T, sz; rng, atol, rtol)
4545
46-
Test the Mooncake reverse-mode AD rule for `project_antihermitian` and its in-place variant.
46+
Test the Mooncake forward- and reverse-mode AD rule for `project_antihermitian` and its in-place variant.
4747
"""
4848
function test_mooncake_project_antihermitian(
4949
T, sz;
@@ -55,15 +55,15 @@ function test_mooncake_project_antihermitian(
5555
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
5656
Mooncake.TestUtils.test_rule(
5757
rng, project_antihermitian, A, alg;
58-
mode = Mooncake.ReverseMode, atol, rtol
58+
atol, rtol
5959
)
6060
Mooncake.TestUtils.test_rule(
6161
rng, project_antihermitian!, A, A, alg;
62-
mode = Mooncake.ReverseMode, atol, rtol
62+
atol, rtol
6363
)
6464
Mooncake.TestUtils.test_rule(
6565
rng, project_antihermitian!, A, B, alg;
66-
mode = Mooncake.ReverseMode, atol, rtol
66+
atol, rtol
6767
)
6868
end
6969
end

0 commit comments

Comments
 (0)