Skip to content

Commit

Permalink
Merge d2a8733 into 2fc41b3
Browse files Browse the repository at this point in the history
  • Loading branch information
unnonouno committed Aug 17, 2017
2 parents 2fc41b3 + d2a8733 commit a3fc918
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ script:
- autopep8 -r . --global-config .pep8 --diff | tee check_autopep8
- test ! -s check_autopep8
- cd tests
- PYTHONWARNINGS='ignore::FutureWarning,module::DeprecationWarning' nosetests -a '!gpu,!slow' --with-doctest chainer_tests
- PYTHONWARNINGS='ignore::FutureWarning,error::DeprecationWarning,ignore::DeprecationWarning:nose.importer,ignore::DeprecationWarning:site,ignore::DeprecaitonWarning:inspect' nosetests -a '!gpu,!slow' --with-doctest chainer_tests
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then
cd ..;
READTHEDOCS=True python setup.py develop;
Expand Down
1 change: 1 addition & 0 deletions chainer/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from chainer.testing import unary_math_function_test # NOQA

from chainer.testing.array import assert_allclose # NOQA
from chainer.testing.helper import assert_warns # NOQA
from chainer.testing.helper import with_requires # NOQA
from chainer.testing.parameterized import parameterize # NOQA
from chainer.testing.parameterized import product # NOQA
Expand Down
21 changes: 21 additions & 0 deletions chainer/testing/helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import contextlib
import pkg_resources
import unittest
import warnings


def with_requires(*requirements):
Expand Down Expand Up @@ -29,3 +31,22 @@ def with_requires(*requirements):

msg = 'requires: {}'.format(','.join(requirements))
return unittest.skipIf(skip, msg)


@contextlib.contextmanager
def assert_warns(expected):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
yield

for m in w:
if not isinstance(m.message, expected):
continue
return

try:
exc_name = expected.__name__
except AttributeError:
exc_name = str(expected)

raise AssertionError('%s not triggerred' % exc_name)
57 changes: 38 additions & 19 deletions tests/chainer_tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class TestLink(unittest.TestCase):
def setUp(self):
x_shape_0 = 2
x_shape_1 = numpy.int64(3)
self.link = chainer.Link(x=((x_shape_0, x_shape_1), 'd'),
u=(None, 'd'))
with testing.assert_warns(DeprecationWarning):
self.link = chainer.Link(x=((x_shape_0, x_shape_1), 'd'),
u=(None, 'd'))
with self.link.init_scope():
self.link.y = chainer.Parameter(shape=(2,))
self.link.v = chainer.Parameter()
Expand Down Expand Up @@ -78,38 +79,45 @@ def test_assign_var_in_init_scope(self):
self.assertTrue(all(p is not param for param in self.link.params()))

def test_add_param(self):
self.link.add_param('z', (2, 3))
with testing.assert_warns(DeprecationWarning):
self.link.add_param('z', (2, 3))
self.check_param_init('z', (2, 3), 'f')

self.link.add_param('w', (2, 3), dtype='d')
with testing.assert_warns(DeprecationWarning):
self.link.add_param('w', (2, 3), dtype='d')
self.check_param_init('w', (2, 3), 'd')

self.link.add_param('r')
with testing.assert_warns(DeprecationWarning):
self.link.add_param('r')
self.check_param_uninit('r')
self.link.r.initialize((2, 3))
self.check_param_init('r', (2, 3), 'f')

self.link.add_param('s', dtype='d')
with testing.assert_warns(DeprecationWarning):
self.link.add_param('s', dtype='d')
self.check_param_uninit('s')
self.link.s.initialize((2, 3))
self.check_param_init('s', (2, 3), 'd')

initializer = initializers.Zero('d')
self.link.add_param('t', initializer=initializer)
with testing.assert_warns(DeprecationWarning):
self.link.add_param('t', initializer=initializer)
self.check_param_uninit('t', initializer)
self.link.t.initialize((2, 3))
self.check_param_init('t', (2, 3), 'd', 0)

def test_add_param_direct_initialization(self):
z = numpy.random.rand(2, 3).astype('f')
self.link.add_param('z', initializer=z)
with testing.assert_warns(DeprecationWarning):
self.link.add_param('z', initializer=z)
self.assertIsInstance(self.link.z.data, numpy.ndarray)
numpy.testing.assert_array_equal(self.link.z.data, z)

def test_add_param_duplicated_with_persistent(self):
self.link.add_persistent('z', 'abc')
with self.assertRaises(AttributeError):
self.link.add_param('z', (2, 3))
with testing.assert_warns(DeprecationWarning):
self.link.add_param('z', (2, 3))

def test_add_persistent(self):
self.assertTrue(hasattr(self.link, 'p'))
Expand Down Expand Up @@ -312,7 +320,8 @@ def test_cleargrads(self):
def test_zerograds(self):
gx_expect = numpy.zeros_like(self.link.x.data)
gy_expect = numpy.zeros_like(self.link.y.data)
self.link.zerograds()
with testing.assert_warns(DeprecationWarning):
self.link.zerograds()
numpy.testing.assert_array_equal(self.link.x.grad, gx_expect)
numpy.testing.assert_array_equal(self.link.y.grad, gy_expect)
self.link.u.initialize((2, 3))
Expand Down Expand Up @@ -433,13 +442,21 @@ def zerograd(self):
class TestChain(unittest.TestCase):

def setUp(self):
self.l1 = chainer.Link(x=(2, 3))
self.l2 = chainer.Link(x=2)
self.l3 = chainer.Link(x=None)
self.l1 = chainer.Link()
with self.l1.init_scope():
self.l1.x = chainer.Parameter(shape=(2, 3))
self.l2 = chainer.Link()
with self.l2.init_scope():
self.l2.x = chainer.Parameter(shape=2)
self.l3 = chainer.Link()
with self.l3.init_scope():
self.l3.x = chainer.Parameter()

self.c1 = chainer.Chain(l1=self.l1)
self.c1.add_link('l2', self.l2)
self.c2 = chainer.Chain(c1=self.c1)
with testing.assert_warns(DeprecationWarning):
self.c1 = chainer.Chain(l1=self.l1)
self.c1.add_link('l2', self.l2)
with testing.assert_warns(DeprecationWarning):
self.c2 = chainer.Chain(c1=self.c1)
with self.c2.init_scope():
self.c2.l3 = self.l3

Expand Down Expand Up @@ -656,7 +673,8 @@ def test_copyparams(self):

def test_zerograds(self):
self.set_count_parameters()
self.c2.zerograds()
with testing.assert_warns(DeprecationWarning):
self.c2.zerograds()
numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
self.assertEqual(self.l1.x.count_zerograd, 1)
Expand Down Expand Up @@ -942,7 +960,8 @@ def test_copyparams(self):
numpy.testing.assert_array_equal(self.l3.x.data, l3.x.data)

def test_zerograds(self):
self.c2.zerograds()
with testing.assert_warns(DeprecationWarning):
self.c2.zerograds()
numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))
Expand All @@ -959,7 +978,7 @@ def test_cleargrads(self):

def test_addgrads(self):
l1 = chainer.Link()
with self.l1.init_scope():
with l1.init_scope():
l1.x = chainer.Parameter(shape=(2, 3))
l1.y = chainer.Parameter(shape=(2, 3))
l2 = chainer.Link()
Expand Down
24 changes: 16 additions & 8 deletions tests/chainer_tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ def check_zerograd(self, a_data, fill=False):
a.grad_var = chainer.Variable(xp.full_like(a_data, np.nan))
a.grad_var.creator_node = chainer.FunctionNode()

a.zerograd()
with testing.assert_warns(DeprecationWarning):
a.zerograd()
self.assertIsNot(a.grad, None)
if fill:
self.assertIsNone(a.grad_var.creator_node)
Expand All @@ -435,7 +436,8 @@ def test_zerograds_multi_gpu(self):
cupy = cuda.cupy
with cuda.get_device_from_id(1):
a = chainer.Variable(cupy.empty(3, dtype=np.float32))
a.zerograd()
with testing.assert_warns(DeprecationWarning):
a.zerograd()
self.assertIsNot(a.grad, None)
self.assertEqual(int(a.grad.device), 1)
with cuda.get_device_from_id(1):
Expand All @@ -448,7 +450,8 @@ def test_zerograds_fill_multi_gpu(self):
with cuda.get_device_from_id(1):
a = chainer.Variable(cupy.empty(3, dtype=np.float32))
a.grad = cupy.empty_like(a.data)
a.zerograd()
with testing.assert_warns(DeprecationWarning):
a.zerograd()
self.assertEqual(int(a.grad.device), 1)
with cuda.get_device_from_id(1):
g_expect = cupy.zeros_like(a.data)
Expand Down Expand Up @@ -788,14 +791,16 @@ def check_zerograd(self, x, xp):

def test_zerograd(self):
x = chainer.Parameter()
x.zerograd()
with testing.assert_warns(DeprecationWarning):
x.zerograd()
x.initialize((3, 2))
self.check_zerograd(x, np)

@attr.gpu
def test_zerograd_to_gpu(self):
x = chainer.Parameter()
x.zerograd()
with testing.assert_warns(DeprecationWarning):
x.zerograd()
x.to_gpu()
x.initialize((3, 2))
self.check_zerograd(x, cuda.cupy)
Expand All @@ -804,13 +809,15 @@ def test_zerograd_to_gpu(self):
def test_to_gpu_zerograd(self):
x = chainer.Parameter()
x.to_gpu()
x.zerograd()
with testing.assert_warns(DeprecationWarning):
x.zerograd()
x.initialize((3, 2))
self.check_zerograd(x, cuda.cupy)

def test_zerograd_dtype(self):
x = chainer.Parameter(initializers.Zero(dtype=np.float16))
x.zerograd()
with testing.assert_warns(DeprecationWarning):
x.zerograd()
x.initialize((3, 2))
self.assertEqual(x.grad.dtype, x.data.dtype)

Expand Down Expand Up @@ -944,7 +951,8 @@ def check_debug_print(self, v, mean, std):
self.assertIn('grad: None', result)

# zero grad
v.zerograd()
with testing.assert_warns(DeprecationWarning):
v.zerograd()
result = v.debug_print()
self.assertIn('grad: 0', result)

Expand Down

0 comments on commit a3fc918

Please sign in to comment.