Skip to content

Commit

Permalink
defenses: add unit test for some defense methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cdluminate committed May 12, 2022
1 parent e0fd01a commit 25501d3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/TODO.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
1. transformer backbone?
* transformer backbone?
* deprecate the `wantsgrad` attribute
53 changes: 53 additions & 0 deletions robrank/defenses/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'''
Copyright (C) 2019-2021, Mo Zhou <cdluminate@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import torch as th
from ..losses import ptripletN
from ..configs import rc2f2
import pytorch_lightning as thl
import pytest

class TestNet(thl.LightningModule):
__test__ = False # surpress pytest warning
def __init__(self):
super(TestNet, self).__init__()
self.fc = th.nn.Linear(784, 5)
self.dataset = 'mnist'
self.loss = 'ptripletN'
self.lossfunc = ptripletN()
self.is_advtrain_pnp = True
self.config = rc2f2(self.dataset, self.loss)
self.metric = self.lossfunc.determine_metric()
self.datasetspec = self.lossfunc.datasetspec()
def forward(self, x):
x = th.nn.Flatten()(x)
return self.fc(x)


def test_testnet():
model = TestNet()
images = th.rand(10, 1, 28, 28)
output = model.forward(images)
loss = output.mean()
loss.backward()


@pytest.mark.skip(reason='this is test helper')
def test_xxx_training_step(training_step: callable):
model = TestNet()
images = th.rand(10, 1, 28, 28)
labels = th.stack([th.arange(5), th.arange(5)]).T.flatten()
loss = training_step(model, (images, labels), 0)
loss.backward()
33 changes: 33 additions & 0 deletions robrank/defenses/test_defenses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
Copyright (C) 2019-2021, Mo Zhou <cdluminate@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import torch as th
from ..losses import ptripletN
from . import test_common
from .pnp import *
from .amd import *
from .est import *
from .ses import *
import pytest


@pytest.mark.parametrize('ts', [
pnp_training_step,
est_training_step,
rest_training_step,
ses_training_step,
])
def test_xxx_training_step(ts: callable):
return test_common.test_xxx_training_step(ts)

0 comments on commit 25501d3

Please sign in to comment.