Skip to content

Commit

Permalink
Merge pull request #6946 from niboshi/math-test
Browse files Browse the repository at this point in the history
Fix test parameters in ChainerX math tests
  • Loading branch information
asi1024 committed Apr 17, 2019
2 parents c2860ec + 354d165 commit 8fc8e08
Showing 1 changed file with 91 additions and 32 deletions.
123 changes: 91 additions & 32 deletions tests/chainerx_tests/unit_tests/routines_tests/test_math.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

import chainer
import numpy
import pytest
Expand All @@ -21,8 +23,11 @@ def __exit__(self, *args):

class UnaryMathTestBase(object):

input = None

def setup(self):
in_dtype, = self.in_dtypes
in_kind = numpy.dtype(in_dtype).kind

if numpy.dtype(in_dtype).kind != 'f':
self.skip_backward_test = True
Expand All @@ -34,6 +39,14 @@ def setup(self):
self.check_double_backward_options.update(
{'rtol': 1e-2, 'atol': 1e-2})

input = self.input
if (in_kind == 'u'
and isinstance(input, (int, float))
and input < 0):
raise unittest.SkipTest(
'Combination of uint dtype and negative input cannot be '
'tested')

def generate_inputs(self):
in_dtype, = self.in_dtypes
if isinstance(self.input, numpy.ndarray):
Expand Down Expand Up @@ -1699,16 +1712,33 @@ def forward_expected(self, inputs):


@op_utils.op_test(['native:0', 'cuda:0'])
@pytest.mark.parametrize('input', [
numpy.asarray(0.), numpy.asarray(-1.), numpy.asarray(1.), numpy.asarray(
10.), numpy.asarray(float('inf')), numpy.asarray(float('nan')),
numpy.full((), 2.), numpy.full((0,), 2.), numpy.full((2, 3), 2.)
])
@chainer.testing.parameterize(*(
# Differentiable
chainer.testing.product({
'input': [
numpy.asarray(0.),
numpy.asarray(-1.),
numpy.asarray(1.),
numpy.asarray(10.),
numpy.full((), 2.),
numpy.full((0,), 2.),
numpy.full((2, 3), 2.)
]})
+
# Nondifferentiable
chainer.testing.product({
'input': [
numpy.asarray(float('inf')),
numpy.asarray(float('nan')),
],
'skip_backward_test': [True],
'skip_double_backward_test': [True],
})
))
@pytest.mark.parametrize('contiguous', [None, 'C'])
class TestSigmoid(op_utils.NumpyOpTest):

def setup(self, input, contiguous, float_dtype):
self.input = input
def setup(self, contiguous, float_dtype):
self.dtype = float_dtype
self.contiguous = contiguous
self.check_forward_options = {'atol': 5e-3, 'rtol': 5e-3}
Expand Down Expand Up @@ -1780,7 +1810,7 @@ def func(self, xp, a):
return xp.sqrt(a)


_trignometric_hyperpolic_params = \
_trigonometric_hyperbolic_params = \
chainer.testing.product({
'shape': [(), (0,), (1,), (2, 0, 3), (1, 1, 1), (2, 3)],
'in_dtypes,out_dtype': _in_out_dtypes_math_functions,
Expand All @@ -1797,7 +1827,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_trignometric_hyperpolic_params
_trigonometric_hyperbolic_params
))
class TestSinh(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1807,7 +1837,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_trignometric_hyperpolic_params
_trigonometric_hyperbolic_params
))
class TestCosh(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1817,7 +1847,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_trignometric_hyperpolic_params
_trigonometric_hyperbolic_params
))
class TestTanh(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1827,7 +1857,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_trignometric_hyperpolic_params
_trigonometric_hyperbolic_params
))
class TestSin(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1837,7 +1867,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_trignometric_hyperpolic_params
_trigonometric_hyperbolic_params
))
class TestCos(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1847,32 +1877,61 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_trignometric_hyperpolic_params
_trigonometric_hyperbolic_params
))
class TestTan(UnaryMathTestBase, op_utils.NumpyOpTest):

def func(self, xp, a):
return xp.tan(a)


_inverse_trig_hyper_params = \
chainer.testing.product({
'shape': [(), (0,), (1,), (2, 0, 3), (1, 1, 1), (2, 3)],
'in_dtypes,out_dtype': _in_out_dtypes_math_functions,
'input': [-0.75, 0, 0.75],
'contiguous': [None, 'C'],
}) + chainer.testing.product({
'shape': [(2, 3)],
'in_dtypes,out_dtype': _in_out_float_dtypes_math_functions,
'input': [1, -1, float('inf'), -float('inf'), float('nan')],
'skip_backward_test': [True],
'skip_double_backward_test': [True],
})
def _make_inverse_trig_params(name):
# Makes test parameters for inverse trigonometric functions

inverse_trig_differentiable_inputs = {
'arcsin': [-0.9, 0, 0.9],
'arccos': [-0.9, 0, 0.9],
'arctan': [-3, -0.2, 0, 0.2, 3],
'arcsinh': [-3, -0.2, 0, 0.2, 3],
'arccosh': [1.2, 3],
'arctanh': [-0.9, 0, 0.9],
}

inverse_trig_nondifferentiable_inputs = {
'arcsin': [-3, -1, 1, 3],
'arccos': [-3, -1, 1, 3],
'arctan': [],
'arcsinh': [],
'arccosh': [-3, 0, 0.2, 1],
'arctanh': [-3, -1, 1, 3],
}

nonfinite_numbers = [float('inf'), -float('inf'), float('nan')]

return (
# Various shapes and differentiable inputs
chainer.testing.product({
'shape': [(), (0,), (1,), (2, 0, 3), (1, 1, 1), (2, 3)],
'in_dtypes,out_dtype': _in_out_dtypes_math_functions,
'input': inverse_trig_differentiable_inputs[name],
'contiguous': [None, 'C'],
})
+
# Nondifferentiable inputs
chainer.testing.product({
'shape': [(2, 3)],
'in_dtypes,out_dtype': _in_out_float_dtypes_math_functions,
'input': (
inverse_trig_nondifferentiable_inputs[name]
+ nonfinite_numbers),
'skip_backward_test': [True],
'skip_double_backward_test': [True],
}))


@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_inverse_trig_hyper_params
_make_inverse_trig_params('arcsinh')
))
class TestArcsinh(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1882,7 +1941,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_inverse_trig_hyper_params
_make_inverse_trig_params('arccosh')
))
class TestArccosh(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1892,7 +1951,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_inverse_trig_hyper_params
_make_inverse_trig_params('arcsin')
))
class TestArcsin(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1902,7 +1961,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_inverse_trig_hyper_params
_make_inverse_trig_params('arccos')
))
class TestArccos(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand All @@ -1912,7 +1971,7 @@ def func(self, xp, a):

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
_trignometric_hyperpolic_params
_make_inverse_trig_params('arctan')
))
class TestArctan(UnaryMathTestBase, op_utils.NumpyOpTest):

Expand Down

0 comments on commit 8fc8e08

Please sign in to comment.