-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] Implement GGN as
CurvatureLinearOperator
- Loading branch information
Showing
13 changed files
with
55 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,32 @@ | ||
"""Contains tests for ``curvlinops/ggn``.""" | ||
|
||
from collections.abc import MutableMapping | ||
from test.utils import compare_matmat | ||
|
||
from numpy import random | ||
from pytest import raises | ||
|
||
from curvlinops import GGNLinearOperator | ||
from curvlinops.examples.functorch import functorch_ggn | ||
from curvlinops.examples.utils import report_nonclose | ||
|
||
|
||
def test_GGNLinearOperator_matvec(case, adjoint: bool): | ||
def test_GGNLinearOperator_matvec(case, adjoint: bool, is_vec: bool): | ||
"""Test matrix-matrix multiplication with the GGN. | ||
Args: | ||
case: Tuple of model, loss function, parameters, data, and batch size getter. | ||
adjoint: Whether to test the adjoint operator. | ||
is_vec: Whether to test matrix-vector or matrix-matrix multiplication. | ||
""" | ||
model_func, loss_func, params, data, batch_size_fn = case | ||
|
||
# Test when X is dict-like but batch_size_fn = None (default) | ||
if isinstance(data[0][0], MutableMapping): | ||
with raises(ValueError): | ||
op = GGNLinearOperator(model_func, loss_func, params, data) | ||
|
||
op = GGNLinearOperator( | ||
model_func, loss_func, params, data, batch_size_fn=batch_size_fn | ||
) | ||
op_functorch = ( | ||
functorch_ggn(model_func, loss_func, params, data, input_key="x") | ||
.detach() | ||
.cpu() | ||
.numpy() | ||
) | ||
if adjoint: | ||
op, op_functorch = op.adjoint(), op_functorch.conj().T | ||
_ = GGNLinearOperator(model_func, loss_func, params, data) | ||
|
||
x = random.rand(op.shape[1]) | ||
report_nonclose(op @ x, op_functorch @ x) | ||
|
||
|
||
def test_GGNLinearOperator_matmat(case, adjoint: bool, num_vecs: int = 3): | ||
model_func, loss_func, params, data, batch_size_fn = case | ||
|
||
op = GGNLinearOperator( | ||
G = GGNLinearOperator( | ||
model_func, loss_func, params, data, batch_size_fn=batch_size_fn | ||
) | ||
op_functorch = ( | ||
functorch_ggn(model_func, loss_func, params, data, input_key="x") | ||
.detach() | ||
.cpu() | ||
.numpy() | ||
) | ||
if adjoint: | ||
op, op_functorch = op.adjoint(), op_functorch.conj().T | ||
G_mat = functorch_ggn(model_func, loss_func, params, data, input_key="x") | ||
|
||
X = random.rand(op.shape[1], num_vecs) | ||
report_nonclose(op @ X, op_functorch @ X) | ||
compare_matmat(G, G_mat, adjoint, is_vec, atol=1e-7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters