<commit_before>import unittest

import functools
import numpy
from operator import mul

import chainer
from chainer import backend
from chainer.backends import cuda
import chainer.functions as F
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr
from chainer.testing import condition
from chainer.utils import conv


@testing.parameterize(*(testing.product({
    'dims': [(5,), (4, 3), (3, 4, 3)],
    'dilate': [1, 2],
    'groups': [1, 2],
    'cover_all': [True, False],
    'c_contiguous': [True],
    'x_dtype': [numpy.float32],
    'W_dtype': [numpy.float32],
    'autotune': [True, False],
}) + testing.product({
    'dims': [(4,)],
    'dilate': [1],
    'groups': [1],
    'cover_all': [False],
    'c_contiguous': [False],
    'x_dtype': [numpy.float16, numpy.float32, numpy.float64],
    'W_dtype': [numpy.float16, numpy.float32, numpy.float64],
    'autotune': [False],
})))
class TestConvolutionND(unittest.TestCase):

    def setUp(self):
        N = 2
        in_channels = 4
        out_channels = 2
        ndim = len(self.dims)
        ksize = (2,) * ndim
        self.stride = (1,) * ndim
        self.pad = (1,) * ndim
        self.dilate = (self.dilate,) * ndim

        W_scale = numpy.sqrt(1. / functools.reduce(mul, ksize, in_channels))
        W_shape = (out_channels, in_channels // self.groups) + ksize
        self.W = numpy.random.normal(0, W_scale, W_shape).astype(self.W_dtype)
        self.b = numpy.random.uniform(-1, 1, out_channels).astype(self.x_dtype)

        x_shape = (N, in_channels) + self.dims
        self.x = numpy.random.uniform(-1, 1, x_shape).astype(self.x_dtype)
        gy_shape = (N, out_channels) + tuple(
            conv.get_conv_outsize(d, k, s, p, cover_all=self.cover_all, d=di)
            for (d, k, s, p, di)
            in zip(self.dims, ksize, self.stride, self.pad, self.dilate))
        self.gy = numpy.random.uniform(-1, 1, gy_shape).astype(self.x_dtype)

        self.check_forward_options = {}
        self.check_backward_options = {
            'dtype': numpy.float64, 'atol': 3e-5, 'rtol': 3e-4}
        if self.x_dtype == numpy.float16 or self.W_dtype == numpy.float16:
            self.check_forward_options = {'atol': 5e-4, 'rtol': 5e-3}
            self.check_backward_options = {
                'dtype': numpy.float64, 'atol': 2 ** -4, 'rtol': 2 ** -4}

        self.ggx = numpy.random.uniform(-1, 1, self.x.shape).astype(
            self.x_dtype)
        self.ggW = numpy.random.uniform(-1, 1, self.W.shape).astype(
            self.W_dtype)
        self.ggb = numpy.random.uniform(-1, 1, self.b.shape).astype(
            self.x_dtype)

    def check_forward_consistency(
            self, transfer_func, nobias=False, use_cudnn='never'):
        x_cpu = chainer.Variable(self.x)
        W_cpu = chainer.Variable(self.W)
        b_cpu = None if nobias else chainer.Variable(self.b)
        y_cpu = F.convolution_nd(
            x_cpu, W_cpu, b_cpu, stride=self.stride, pad=self.pad,
            cover_all=self.cover_all, dilate=self.dilate,
            groups=self.groups)

        x_gpu = chainer.Variable(transfer_func(self.x))
        W_gpu = chainer.Variable(transfer_func(self.W))
        b_gpu = None if nobias else chainer.Variable(transfer_func(self.b))
        with chainer.using_config('use_cudnn', use_cudnn):
            with chainer.using_config('autotune', self.autotune):
                y_gpu = F.convolution_nd(
                    x_gpu, W_gpu, b_gpu, stride=self.stride, pad=self.pad,
                    cover_all=self.cover_all, dilate=self.dilate,
                    groups=self.groups)

        testing.assert_allclose(
            y_cpu.data, y_gpu.data, **self.check_forward_options)

    def _skip_if_not_chainerx_supported(self):
        # TODO(hvy): chainerx does not support fp16 yet.
        if self.x_dtype is numpy.float16 or self.W_dtype is numpy.float16:
            raise unittest.SkipTest('Not yet supported')

    @attr.chainerx
    def test_forward_chainerx_native(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(backend.to_chainerx, nobias=False)

    def test_forward_chainerx_native_nobias(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(backend.to_chainerx, nobias=True)

    @attr.chainerx
    @attr.gpu
    def test_forward_chainerx_cuda(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(
            lambda xs: backend.to_chainerx(cuda.to_gpu(xs)), nobias=False,
            use_cudnn='always')

    @attr.chainerx
    @attr.gpu
    def test_forward_chainerx_cuda_nobias(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(
            lambda xs: backend.to_chainerx(cuda.to_gpu(xs)), nobias=True,
            use_cudnn='always')

    @attr.cudnn
    def test_forward_consistency(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=False, use_cudnn='always')

    @attr.cudnn
    def test_forward_consistency_nobias(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=True, use_cudnn='always')

    @attr.gpu
    def test_forward_consistency_im2col(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=False, use_cudnn='never')

    @attr.gpu
    def test_forward_consistency_im2col_nobias(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=True, use_cudnn='never')

    def check_forward_consistency_regression(self, nobias=False):
        x = chainer.Variable(self.x)
        W = chainer.Variable(self.W)
        b = None if nobias else chainer.Variable(self.b)

        with chainer.using_config('use_cudnn', 'never'):
            y_nd = F.convolution_nd(
                x, W, b, stride=self.stride, pad=self.pad,
                cover_all=self.cover_all, dilate=self.dilate,<commit_msg>Skip unrelated cudnn argument for chainerx test<commit_after>import unittest

import functools
import numpy
from operator import mul

import chainer
from chainer import backend
from chainer.backends import cuda
import chainer.functions as F
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr
from chainer.testing import condition
from chainer.utils import conv


@testing.parameterize(*(testing.product({
    'dims': [(5,), (4, 3), (3, 4, 3)],
    'dilate': [1, 2],
    'groups': [1, 2],
    'cover_all': [True, False],
    'c_contiguous': [True],
    'x_dtype': [numpy.float32],
    'W_dtype': [numpy.float32],
    'autotune': [True, False],
}) + testing.product({
    'dims': [(4,)],
    'dilate': [1],
    'groups': [1],
    'cover_all': [False],
    'c_contiguous': [False],
    'x_dtype': [numpy.float16, numpy.float32, numpy.float64],
    'W_dtype': [numpy.float16, numpy.float32, numpy.float64],
    'autotune': [False],
})))
class TestConvolutionND(unittest.TestCase):

    def setUp(self):
        N = 2
        in_channels = 4
        out_channels = 2
        ndim = len(self.dims)
        ksize = (2,) * ndim
        self.stride = (1,) * ndim
        self.pad = (1,) * ndim
        self.dilate = (self.dilate,) * ndim

        W_scale = numpy.sqrt(1. / functools.reduce(mul, ksize, in_channels))
        W_shape = (out_channels, in_channels // self.groups) + ksize
        self.W = numpy.random.normal(0, W_scale, W_shape).astype(self.W_dtype)
        self.b = numpy.random.uniform(-1, 1, out_channels).astype(self.x_dtype)

        x_shape = (N, in_channels) + self.dims
        self.x = numpy.random.uniform(-1, 1, x_shape).astype(self.x_dtype)
        gy_shape = (N, out_channels) + tuple(
            conv.get_conv_outsize(d, k, s, p, cover_all=self.cover_all, d=di)
            for (d, k, s, p, di)
            in zip(self.dims, ksize, self.stride, self.pad, self.dilate))
        self.gy = numpy.random.uniform(-1, 1, gy_shape).astype(self.x_dtype)

        self.check_forward_options = {}
        self.check_backward_options = {
            'dtype': numpy.float64, 'atol': 3e-5, 'rtol': 3e-4}
        if self.x_dtype == numpy.float16 or self.W_dtype == numpy.float16:
            self.check_forward_options = {'atol': 5e-4, 'rtol': 5e-3}
            self.check_backward_options = {
                'dtype': numpy.float64, 'atol': 2 ** -4, 'rtol': 2 ** -4}

        self.ggx = numpy.random.uniform(-1, 1, self.x.shape).astype(
            self.x_dtype)
        self.ggW = numpy.random.uniform(-1, 1, self.W.shape).astype(
            self.W_dtype)
        self.ggb = numpy.random.uniform(-1, 1, self.b.shape).astype(
            self.x_dtype)

    def check_forward_consistency(
            self, transfer_func, nobias=False, use_cudnn='never'):
        x_cpu = chainer.Variable(self.x)
        W_cpu = chainer.Variable(self.W)
        b_cpu = None if nobias else chainer.Variable(self.b)
        y_cpu = F.convolution_nd(
            x_cpu, W_cpu, b_cpu, stride=self.stride, pad=self.pad,
            cover_all=self.cover_all, dilate=self.dilate,
            groups=self.groups)

        x_gpu = chainer.Variable(transfer_func(self.x))
        W_gpu = chainer.Variable(transfer_func(self.W))
        b_gpu = None if nobias else chainer.Variable(transfer_func(self.b))
        with chainer.using_config('use_cudnn', use_cudnn):
            with chainer.using_config('autotune', self.autotune):
                y_gpu = F.convolution_nd(
                    x_gpu, W_gpu, b_gpu, stride=self.stride, pad=self.pad,
                    cover_all=self.cover_all, dilate=self.dilate,
                    groups=self.groups)

        testing.assert_allclose(
            y_cpu.data, y_gpu.data, **self.check_forward_options)

    def _skip_if_not_chainerx_supported(self):
        # TODO(hvy): chainerx does not support fp16 yet.
        if self.x_dtype is numpy.float16 or self.W_dtype is numpy.float16:
            raise unittest.SkipTest('Not yet supported')

    @attr.chainerx
    def test_forward_chainerx_native(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(backend.to_chainerx, nobias=False)

    def test_forward_chainerx_native_nobias(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(backend.to_chainerx, nobias=True)

    @attr.chainerx
    @attr.gpu
    def test_forward_chainerx_cuda(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(
            lambda xs: backend.to_chainerx(cuda.to_gpu(xs)), nobias=False)

    @attr.chainerx
    @attr.gpu
    def test_forward_chainerx_cuda_nobias(self):
        self._skip_if_not_chainerx_supported()
        self.check_forward_consistency(
            lambda xs: backend.to_chainerx(cuda.to_gpu(xs)), nobias=True)

    @attr.cudnn
    def test_forward_consistency(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=False, use_cudnn='always')

    @attr.cudnn
    def test_forward_consistency_nobias(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=True, use_cudnn='always')

    @attr.gpu
    def test_forward_consistency_im2col(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=False, use_cudnn='never')

    @attr.gpu
    def test_forward_consistency_im2col_nobias(self):
        self.check_forward_consistency(
            cuda.to_gpu, nobias=True, use_cudnn='never')

    def check_forward_consistency_regression(self, nobias=False):
        x = chainer.Variable(self.x)
        W = chainer.Variable(self.W)
        b = None if nobias else chainer.Variable(self.b)

        with chainer.using_config('use_cudnn', 'never'):
            y_nd = F.convolution_nd(
                x, W, b, stride=self.stride, pad=self.pad,
                cover_all=self.cover_all, dilate=self.dilate,
{'fraction_non_alphanumeric': 0.12215285979416231, 'fraction_numerical': 0.012991395309600135, 'mean_word_length': 2.7432901799810545, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2895285', 'n_tokens_mistral': 4264, 'n_tokens_neox': 4106, 'n_words': 749}