<commit_before>import unittest

import mock
import numpy
import six

import chainer
from chainer import cuda
from chainer import functions
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr
from chainer.testing import condition


@testing.parameterize(*testing.product({
    'cover_all': [True, False],
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestMaxPooling2D(unittest.TestCase):

    def setUp(self):
        # Avoid unstability of numerical gradient
        self.x = numpy.arange(
            2 * 3 * 4 * 3, dtype=self.dtype).reshape(2, 3, 4, 3)
        numpy.random.shuffle(self.x)
        self.x = 2 * self.x / self.x.size - 1
        if self.cover_all:
            self.gy = numpy.random.uniform(
                -1, 1, (2, 3, 3, 2)).astype(self.dtype)
        else:
            self.gy = numpy.random.uniform(
                -1, 1, (2, 3, 2, 2)).astype(self.dtype)
        self.check_backward_options = {'eps': 2.0 ** -8}

    def check_forward(self, x_data, use_cudnn=True):
        x = chainer.Variable(x_data)
        y = functions.max_pooling_2d(x, 3, stride=2, pad=1,
                                     cover_all=self.cover_all,
                                     use_cudnn=use_cudnn)
        self.assertEqual(y.data.dtype, self.dtype)
        y_data = cuda.to_cpu(y.data)

        self.assertEqual(self.gy.shape, y_data.shape)
        for k in six.moves.range(2):
            for c in six.moves.range(3):
                x = self.x[k, c]
                if self.cover_all:
                    expect = numpy.array([
                        [x[0:2, 0:2].max(), x[0:2, 1:3].max()],
                        [x[1:4, 0:2].max(), x[1:4, 1:3].max()],
                        [x[3:4, 0:2].max(), x[3:4, 1:3].max()]])
                else:
                    expect = numpy.array([
                        [x[0:2, 0:2].max(), x[0:2, 1:3].max()],
                        [x[1:4, 0:2].max(), x[1:4, 1:3].max()]])
                testing.assert_allclose(expect, y_data[k, c])

    @condition.retry(3)
    def test_forward_cpu(self):
        self.check_forward(self.x)

    def test_forward_cpu_wide(self):  # see #120
        x_data = numpy.random.rand(2, 3, 15, 15).astype(self.dtype)
        x = chainer.Variable(x_data)
        functions.max_pooling_2d(x, 6, stride=6, pad=0)

    def test_forward_output_size_zero_cpu(self):
        with self.assertRaisesRegexp(
                AssertionError, 'Height in the output should be positive.'):
            x_data = numpy.random.rand(4, 4, 1, 4).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2)
        with self.assertRaisesRegexp(
                AssertionError, 'Width in the output should be positive.'):
            x_data = numpy.random.rand(4, 4, 4, 1).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2)

    @attr.gpu
    @condition.retry(3)
    def test_forward_gpu(self):
        self.check_forward(cuda.to_gpu(self.x))

    @attr.gpu
    @condition.retry(3)
    def test_forward_gpu_no_cudnn(self):
        self.check_forward(cuda.to_gpu(self.x), False)

    def test_forward_output_size_zero_gpu(self):
        with self.assertRaisesRegexp(
                AssertionError, 'Height in the output should be positive.'):
            x_data = cuda.cupy.random.rand(4, 4, 1, 4).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2, use_cudnn=False)
        with self.assertRaisesRegexp(
                AssertionError, 'Width in the output should be positive.'):
            x_data = cuda.cupy.random.rand(4, 4, 4, 1).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2, use_cudnn=False)

    def test_forward_output_size_zero_cudnn(self):
        with self.assertRaisesRegexp(
                AssertionError, 'Height in the output should be positive.'):<commit_msg>re-introduce gpu and cudnn decorators<commit_after>import unittest

import mock
import numpy
import six

import chainer
from chainer import cuda
from chainer import functions
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr
from chainer.testing import condition


@testing.parameterize(*testing.product({
    'cover_all': [True, False],
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestMaxPooling2D(unittest.TestCase):

    def setUp(self):
        # Avoid unstability of numerical gradient
        self.x = numpy.arange(
            2 * 3 * 4 * 3, dtype=self.dtype).reshape(2, 3, 4, 3)
        numpy.random.shuffle(self.x)
        self.x = 2 * self.x / self.x.size - 1
        if self.cover_all:
            self.gy = numpy.random.uniform(
                -1, 1, (2, 3, 3, 2)).astype(self.dtype)
        else:
            self.gy = numpy.random.uniform(
                -1, 1, (2, 3, 2, 2)).astype(self.dtype)
        self.check_backward_options = {'eps': 2.0 ** -8}

    def check_forward(self, x_data, use_cudnn=True):
        x = chainer.Variable(x_data)
        y = functions.max_pooling_2d(x, 3, stride=2, pad=1,
                                     cover_all=self.cover_all,
                                     use_cudnn=use_cudnn)
        self.assertEqual(y.data.dtype, self.dtype)
        y_data = cuda.to_cpu(y.data)

        self.assertEqual(self.gy.shape, y_data.shape)
        for k in six.moves.range(2):
            for c in six.moves.range(3):
                x = self.x[k, c]
                if self.cover_all:
                    expect = numpy.array([
                        [x[0:2, 0:2].max(), x[0:2, 1:3].max()],
                        [x[1:4, 0:2].max(), x[1:4, 1:3].max()],
                        [x[3:4, 0:2].max(), x[3:4, 1:3].max()]])
                else:
                    expect = numpy.array([
                        [x[0:2, 0:2].max(), x[0:2, 1:3].max()],
                        [x[1:4, 0:2].max(), x[1:4, 1:3].max()]])
                testing.assert_allclose(expect, y_data[k, c])

    @condition.retry(3)
    def test_forward_cpu(self):
        self.check_forward(self.x)

    def test_forward_cpu_wide(self):  # see #120
        x_data = numpy.random.rand(2, 3, 15, 15).astype(self.dtype)
        x = chainer.Variable(x_data)
        functions.max_pooling_2d(x, 6, stride=6, pad=0)

    def test_forward_output_size_zero_cpu(self):
        with self.assertRaisesRegexp(
                AssertionError, 'Height in the output should be positive.'):
            x_data = numpy.random.rand(4, 4, 1, 4).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2)
        with self.assertRaisesRegexp(
                AssertionError, 'Width in the output should be positive.'):
            x_data = numpy.random.rand(4, 4, 4, 1).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2)

    @attr.gpu
    @condition.retry(3)
    def test_forward_gpu(self):
        self.check_forward(cuda.to_gpu(self.x))

    @attr.gpu
    @condition.retry(3)
    def test_forward_gpu_no_cudnn(self):
        self.check_forward(cuda.to_gpu(self.x), False)

    @attr.gpu
    def test_forward_output_size_zero_gpu(self):
        with self.assertRaisesRegexp(
                AssertionError, 'Height in the output should be positive.'):
            x_data = cuda.cupy.random.rand(4, 4, 1, 4).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2, use_cudnn=False)
        with self.assertRaisesRegexp(
                AssertionError, 'Width in the output should be positive.'):
            x_data = cuda.cupy.random.rand(4, 4, 4, 1).astype(self.dtype)
            x = chainer.Variable(x_data)
            functions.max_pooling_2d(x, 3, stride=2, use_cudnn=False)

    @attr.cudnn
    def test_forward_output_size_zero_cudnn(self):
        with self.assertRaisesRegexp(
                AssertionError, 'Height in the output should be positive.'):
{'fraction_non_alphanumeric': 0.12699386503067484, 'fraction_numerical': 0.029938650306748468, 'mean_word_length': 2.1015981735159817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4232078', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2765, 'n_words': 561}