Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional test settings for non-default hyperparameters #108

Merged
merged 9 commits into from
Sep 20, 2020
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
6 changes: 6 additions & 0 deletions backpack/core/derivatives/conv_transposend.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def _bias_jac_mat_prod(self, module, g_inp, g_out, mat):
return jac_mat.expand(*expand_shape)

def _weight_jac_mat_prod(self, module, g_inp, g_out, mat):
if module.groups != 1:
raise NotImplementedError("Groups greater than 1 are not supported yet")

V = mat.shape[0]
G = module.groups
C_in = module.input0.shape[1]
Expand All @@ -71,6 +74,9 @@ def _weight_jac_mat_prod(self, module, g_inp, g_out, mat):
return self.reshape_like_output(jac_mat, module)

def _weight_jac_t_mat_prod(self, module, g_inp, g_out, mat, sum_batch=True):
if module.groups != 1:
raise NotImplementedError("Groups greater than 1 are not supported yet")

V = mat.shape[0]
G = module.groups
C_in = module.input0.shape[1]
Expand Down
6 changes: 6 additions & 0 deletions backpack/core/derivatives/convnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def _bias_jac_t_mat_prod(self, module, g_inp, g_out, mat, sum_batch=True):
return mat.sum(axes)

def _weight_jac_mat_prod(self, module, g_inp, g_out, mat):
if module.groups != 1:
raise NotImplementedError("Groups greater than 1 are not supported yet")

dims = self.dim_text
dims_joined = dims.replace(",", "")

Expand All @@ -109,6 +112,9 @@ def _weight_jac_mat_prod(self, module, g_inp, g_out, mat):
return self.reshape_like_output(jac_mat, module)

def _weight_jac_t_mat_prod(self, module, g_inp, g_out, mat, sum_batch=True):
if module.groups != 1:
raise NotImplementedError("Groups greater than 1 are not supported yet")

V = mat.shape[0]
N, C_out = module.output_shape[0], module.output_shape[1]
C_in = module.input0_shape[1]
Expand Down
149 changes: 149 additions & 0 deletions test/core/derivatives/convolution_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,152 @@
"id_prefix": "non-default-conv",
},
]

CONVOLUTION_FAIL_SETTINGS = [
# groups - 2
{
"module_fn": lambda: torch.nn.Conv1d(
in_channels=4,
out_channels=6,
kernel_size=2,
padding=0,
dilation=2,
groups=2,
),
"input_fn": lambda: torch.rand(size=(3, 4, 7)),
"id_prefix": "groups-2",
},
{
"module_fn": lambda: torch.nn.Conv2d(
in_channels=4,
out_channels=6,
kernel_size=2,
padding=0,
dilation=2,
groups=2,
),
"input_fn": lambda: torch.rand(size=(3, 4, 7, 7)),
"id_prefix": "groups-2",
},
{
"module_fn": lambda: torch.nn.Conv3d(
in_channels=4,
out_channels=6,
kernel_size=2,
padding=0,
dilation=2,
groups=2,
),
"input_fn": lambda: torch.rand(size=(3, 4, 3, 7, 7)),
"id_prefix": "groups-2",
},
{
"module_fn": lambda: torch.nn.ConvTranspose1d(
in_channels=4,
out_channels=6,
kernel_size=2,
padding=0,
dilation=2,
groups=2,
),
"input_fn": lambda: torch.rand(size=(3, 4, 7)),
"id_prefix": "groups-2",
},
{
"module_fn": lambda: torch.nn.ConvTranspose2d(
in_channels=4,
out_channels=6,
kernel_size=2,
padding=0,
dilation=2,
groups=2,
),
"input_fn": lambda: torch.rand(size=(3, 4, 7, 7)),
"id_prefix": "groups-2",
},
{
"module_fn": lambda: torch.nn.ConvTranspose3d(
in_channels=4,
out_channels=6,
kernel_size=2,
padding=0,
dilation=2,
groups=2,
),
"input_fn": lambda: torch.rand(size=(3, 4, 3, 7, 7)),
"id_prefix": "groups-2",
},
# groups - 3
{
"module_fn": lambda: torch.nn.Conv1d(
in_channels=6,
out_channels=9,
kernel_size=2,
padding=0,
dilation=2,
groups=3,
),
"input_fn": lambda: torch.rand(size=(3, 6, 7)),
"id_prefix": "groups-3",
},
{
"module_fn": lambda: torch.nn.Conv2d(
in_channels=6,
out_channels=9,
kernel_size=2,
padding=0,
dilation=2,
groups=3,
),
"input_fn": lambda: torch.rand(size=(3, 6, 7, 7)),
"id_prefix": "groups-3",
},
{
"module_fn": lambda: torch.nn.Conv3d(
in_channels=6,
out_channels=9,
kernel_size=2,
padding=0,
dilation=2,
groups=3,
),
"input_fn": lambda: torch.rand(size=(3, 6, 3, 7, 7)),
"id_prefix": "groups-3",
},
{
"module_fn": lambda: torch.nn.ConvTranspose1d(
in_channels=6,
out_channels=9,
kernel_size=2,
padding=0,
dilation=2,
groups=3,
),
"input_fn": lambda: torch.rand(size=(3, 6, 7)),
"id_prefix": "groups-3",
},
{
"module_fn": lambda: torch.nn.ConvTranspose2d(
in_channels=6,
out_channels=9,
kernel_size=2,
padding=0,
dilation=2,
groups=3,
),
"input_fn": lambda: torch.rand(size=(3, 6, 7, 7)),
"id_prefix": "groups-3",
},
{
"module_fn": lambda: torch.nn.ConvTranspose3d(
in_channels=6,
out_channels=9,
kernel_size=2,
padding=0,
dilation=2,
groups=3,
),
"input_fn": lambda: torch.rand(size=(3, 6, 3, 7, 7)),
"id_prefix": "groups-3",
},
]
19 changes: 19 additions & 0 deletions test/core/derivatives/derivatives_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from test.core.derivatives.problem import make_test_problems
from test.core.derivatives.settings import SETTINGS
from test.core.derivatives.loss_settings import LOSS_FAIL_SETTINGS
from test.core.derivatives.convolution_settings import CONVOLUTION_FAIL_SETTINGS

import pytest
import torch
Expand All @@ -30,6 +31,9 @@
LOSS_FAIL_PROBLEMS = make_test_problems(LOSS_FAIL_SETTINGS)
LOSS_FAIL_IDS = [problem.make_id() for problem in LOSS_FAIL_PROBLEMS]

CONVOLUTION_FAIL_PROBLEMS = make_test_problems(CONVOLUTION_FAIL_SETTINGS)
CONVOLUTION_FAIL_IDS = [problem.make_id() for problem in CONVOLUTION_FAIL_PROBLEMS]


@pytest.mark.parametrize("problem", NO_LOSS_PROBLEMS, ids=NO_LOSS_IDS)
def test_jac_mat_prod(problem, V=3):
Expand Down Expand Up @@ -200,6 +204,21 @@ def test_sqrt_hessian_squared_equals_hessian(problem):
problem.tear_down()


@pytest.mark.parametrize("problem", CONVOLUTION_FAIL_PROBLEMS, ids=CONVOLUTION_FAIL_IDS)
def test_weight_jac_mat_prod_should_fail(problem):
with pytest.raises(NotImplementedError):
test_weight_jac_mat_prod(problem)


@pytest.mark.parametrize(
"sum_batch", [True, False], ids=["sum_batch=True", "sum_batch=False"]
)
@pytest.mark.parametrize("problem", CONVOLUTION_FAIL_PROBLEMS, ids=CONVOLUTION_FAIL_IDS)
def test_weight_jac_t_mat_prod_should_fail(problem, sum_batch):
with pytest.raises(NotImplementedError):
test_weight_jac_t_mat_prod(problem, sum_batch)


@pytest.mark.parametrize("problem", LOSS_FAIL_PROBLEMS, ids=LOSS_FAIL_IDS)
def test_sqrt_hessian_should_fail(problem):
with pytest.raises(ValueError):
Expand Down
Loading