Skip to content

Commit

Permalink
Merge pull request #2932 from unnonouno/variable-deprecation
Browse files Browse the repository at this point in the history
Check DeprecationWarning in Variable
  • Loading branch information
niboshi committed Aug 17, 2017
1 parent cb7cc2b commit e10347b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
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
19 changes: 19 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,20 @@ 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

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

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

raise AssertionError('%s not triggerred' % exc_name)
9 changes: 6 additions & 3 deletions tests/chainer_tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,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 @@ -620,7 +621,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 @@ -906,7 +908,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 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 @@ -359,7 +359,8 @@ def check_zerograd(self, a_data, fill=False):
if fill:
a.grad = xp.full_like(a_data, np.nan)

a.zerograd()
with testing.assert_warns(DeprecationWarning):
a.zerograd()
self.assertIsNot(a.grad, None)
g_expect = xp.zeros_like(a.data)
xp.testing.assert_array_equal(a.grad, g_expect)
Expand All @@ -375,7 +376,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 @@ -388,7 +390,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 @@ -728,14 +731,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 @@ -744,13 +749,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 @@ -884,7 +891,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 e10347b

Please sign in to comment.