Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions ext/MatrixAlgebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ for (f!, f, adj) in (
(:project_antihermitian!, :project_antihermitian, :project_antihermitian_adjoint),
)
@eval begin
@is_primitive DefaultCtx Mooncake.ReverseMode Tuple{typeof($f!), Any, Any, MatrixAlgebraKit.AbstractAlgorithm}
@is_primitive DefaultCtx Tuple{typeof($f!), Any, Any, MatrixAlgebraKit.AbstractAlgorithm}
function Mooncake.rrule!!(f_df::CoDual{typeof($f!)}, A_dA::CoDual, arg_darg::CoDual, alg_dalg::CoDual{<:MatrixAlgebraKit.AbstractAlgorithm})
A, dA = arrayify(A_dA)
arg, darg = A_dA === arg_darg ? (A, dA) : arrayify(arg_darg)
Expand All @@ -879,8 +879,14 @@ for (f!, f, adj) in (

return arg_darg, $adj
end

@is_primitive DefaultCtx Mooncake.ReverseMode Tuple{typeof($f), Any, MatrixAlgebraKit.AbstractAlgorithm}
function Mooncake.frule!!(f_df::Dual{typeof($f!)}, A_dA::Dual, arg_darg::Dual, alg_dalg::Dual{<:MatrixAlgebraKit.AbstractAlgorithm})
A, dA = arrayify(A_dA)
arg, darg = A_dA === arg_darg ? (A, dA) : arrayify(arg_darg)
$f!(A, arg, Mooncake.primal(alg_dalg))
$f!(dA, darg, Mooncake.primal(alg_dalg))
return arg_darg
end
@is_primitive DefaultCtx Tuple{typeof($f), Any, MatrixAlgebraKit.AbstractAlgorithm}
function Mooncake.rrule!!(f_df::CoDual{typeof($f)}, A_dA::CoDual, alg_dalg::CoDual{<:MatrixAlgebraKit.AbstractAlgorithm})
A, dA = arrayify(A_dA)
output = $f(A, Mooncake.primal(alg_dalg))
Expand All @@ -896,6 +902,14 @@ for (f!, f, adj) in (

return output_doutput, $adj
end
function Mooncake.frule!!(f_df::Dual{typeof($f)}, A_dA::Dual, alg_dalg::Dual{<:MatrixAlgebraKit.AbstractAlgorithm})
A, dA = arrayify(A_dA)
output = $f(A, Mooncake.primal(alg_dalg))
output_doutput = Mooncake.zero_dual(output)
doutput = last(arrayify(output_doutput))
$f!(dA, doutput, Mooncake.primal(alg_dalg))
return output_doutput
end
end
end

Expand Down
14 changes: 10 additions & 4 deletions test/testsuite/enzyme/projections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,45 @@ end
"""
test_enzyme_project_hermitian(T, sz; rng, atol, rtol)

Test the Enzyme reverse-mode AD rule for `project_hermitian` and its in-place variant.
Test the Enzyme forward- and reverse-mode AD rule for `project_hermitian` and its in-place variant.
"""
function test_enzyme_project_hermitian(
T, sz;
rng = Random.default_rng(), atol::Real = 0, rtol::Real = precision(T),
fdm = enzyme_fdm(T)
)
return @testset "project_hermitian reverse: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
return @testset "project_hermitian: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
A = instantiate_matrix(T, sz)
B = instantiate_matrix(T, sz)
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
test_reverse(project_hermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
test_reverse(project_hermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
test_reverse(project_hermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
test_forward(project_hermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
test_forward(project_hermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
test_forward(project_hermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
end
end

"""
test_enzyme_project_antihermitian(T, sz; rng, atol, rtol)

Test the Enzyme reverse-mode AD rule for `project_antihermitian` and its in-place variant.
Test the Enzyme forward- and reverse-mode AD rule for `project_antihermitian` and its in-place variant.
"""
function test_enzyme_project_antihermitian(
T, sz;
rng = Random.default_rng(), atol::Real = 0, rtol::Real = precision(T),
fdm = enzyme_fdm(T)
)
return @testset "project_antihermitian reverse: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
return @testset "project_antihermitian: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
A = instantiate_matrix(T, sz)
B = instantiate_matrix(T, sz)
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
test_reverse(project_antihermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
test_reverse(project_antihermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
test_reverse(project_antihermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
test_forward(project_antihermitian, RT, (A, TA), (alg, Const); atol, rtol, fdm)
test_forward(project_antihermitian!, RT, (A, TA), (B, TA), (alg, Const); atol, rtol, fdm)
test_forward(project_antihermitian_inplace!, RT, (A, TA), (alg, Const); atol, rtol, fdm)
end
end
16 changes: 8 additions & 8 deletions test/testsuite/mooncake/projections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
"""
test_mooncake_project_hermitian(T, sz; rng, atol, rtol)

Test the Mooncake reverse-mode AD rule for `project_hermitian` and its in-place variant.
Test the Mooncake forward- and reverse-mode AD rule for `project_hermitian` and its in-place variant.
"""
function test_mooncake_project_hermitian(
T, sz;
Expand All @@ -27,23 +27,23 @@ function test_mooncake_project_hermitian(
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
Mooncake.TestUtils.test_rule(
rng, project_hermitian, A, alg;
mode = Mooncake.ReverseMode, atol, rtol
atol, rtol
)
Mooncake.TestUtils.test_rule(
rng, project_hermitian!, A, A, alg;
mode = Mooncake.ReverseMode, atol, rtol
atol, rtol
)
Mooncake.TestUtils.test_rule(
rng, project_hermitian!, A, B, alg;
mode = Mooncake.ReverseMode, atol, rtol
atol, rtol
)
end
end

"""
test_mooncake_project_antihermitian(T, sz; rng, atol, rtol)

Test the Mooncake reverse-mode AD rule for `project_antihermitian` and its in-place variant.
Test the Mooncake forward- and reverse-mode AD rule for `project_antihermitian` and its in-place variant.
"""
function test_mooncake_project_antihermitian(
T, sz;
Expand All @@ -55,15 +55,15 @@ function test_mooncake_project_antihermitian(
alg = MatrixAlgebraKit.select_algorithm(project_hermitian, A)
Mooncake.TestUtils.test_rule(
rng, project_antihermitian, A, alg;
mode = Mooncake.ReverseMode, atol, rtol
atol, rtol
)
Mooncake.TestUtils.test_rule(
rng, project_antihermitian!, A, A, alg;
mode = Mooncake.ReverseMode, atol, rtol
atol, rtol
)
Mooncake.TestUtils.test_rule(
rng, project_antihermitian!, A, B, alg;
mode = Mooncake.ReverseMode, atol, rtol
atol, rtol
)
end
end
Loading