import unittest

import mock
import numpy

import chainer
from chainer.backends import cuda
from chainer import functions
from chainer import testing
from chainer.testing import attr
from chainer import utils


@testing.parameterize(*testing.product({
    'shape': [(3, 2), ()],
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
@testing.fix_random()
@testing.inject_backend_tests(
    None,
    [
        # CPU tests
        {},
        # GPU tests
        {'use_cuda': True, 'use_cudnn': 'never'},
        {'use_cuda': True, 'use_cudnn': 'always'},
        # ChainerX tests
        {'use_chainerx': True, 'chainerx_device': 'native:0'},
        {'use_chainerx': True, 'chainerx_device': 'cuda:0'},
    ])
class TestClippedReLU(testing.FunctionTestCase):

    dodge_nondifferentiable = True

    z = 0.75

    def generate_inputs(self):
        x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        return x,

    def forward(self, inputs, device):
        x, = inputs
        y = functions.clipped_relu(x, self.z)
        return y,

    def forward_expected(self, inputs):
        x, = inputs
        y = utils.force_array(x.clip(0, self.z))
        return y,


@testing.parameterize(*testing.product({
    'use_cudnn': ['always', 'auto', 'never'],
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
@attr.cudnn
class TestClippedReLUCudnnCall(unittest.TestCase):

    def setUp(self):
        self.x = cuda.cupy.random.uniform(-1, 1, (2, 3)).astype(self.dtype)
        self.gy = cuda.cupy.random.uniform(-1, 1, (2, 3)).astype(self.dtype)
        self.z = 0.75
        with chainer.using_config('use_cudnn', self.use_cudnn):
            self.expect = chainer.should_use_cudnn('==always')

    def forward(self):
        x = chainer.Variable(self.x)
        return functions.clipped_relu(x, self.z)

    def test_call_cudnn_forward(self):
        default_func = cuda.cupy.cudnn.activation_forward
        with chainer.using_config('use_cudnn', self.use_cudnn):
            with mock.patch('cupy.cudnn.activation_forward') as func:
                func.side_effect = default_func
                self.forward()
                self.assertEqual(func.called, self.expect)

    def test_call_cudnn_backward(self):
        with chainer.using_config('use_cudnn', self.use_cudnn):
            y = self.forward()
            y.grad = self.gy
            default_func = cuda.cupy.cudnn.activation_backward
            with mock.patch('cupy.cudnn.activation_backward') as func:
                func.side_effect = default_func
                y.backward()
                self.assertEqual(func.called, self.expect)


@testing.parameterize(*testing.product({
    'shape': [(3, 2), ()],
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
@testing.fix_random()
@testing.inject_backend_tests(
    None,
    [
        # CPU tests
        {},
        # GPU tests
        {'use_cuda': True, 'use_cudnn': 'never'},
        {'use_cuda': True, 'use_cudnn': 'always'},
        # ChainerX tests
        {'use_chainerx': True, 'chainerx_device': 'native:0'},
        {'use_chainerx': True, 'chainerx_device': 'cuda:0'},
    ])
class TestReLU6(testing.FunctionTestCase):

    dodge_nondifferentiable = True

    def generate_inputs(self):
        x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        return x,

    def forward(self, inputs, device):
        x, = inputs
        y = functions.relu6(x)
        return y,

    def forward_expected(self, inputs):
        x, = inputs
        y = utils.force_array(x.clip(0, 6.0))
        return y,


testing.run_module(__name__, __file__)

@@ -1166,16 +1166,25 @@
  self.z)
+, x.dtype
 )%0A      
@@ -3576,16 +3576,25 @@
 (0, 6.0)
+, x.dtype
 )%0A      

Fix F.clipped_relu test for NumPy 1.17