import math
import unittest

import numpy

import chainer
from chainer import cuda
import chainer.functions as F
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr


def _erf_cpu(x):
    return numpy.vectorize(math.erf)(x)


def _erf_gpu(x):
    return cuda.to_gpu(_erf_cpu(cuda.to_cpu(x)))


class UnaryFunctionsTestBase(unittest.TestCase):

    def make_data(self):
        raise NotImplementedError

    def setUp(self):
        self.x, self.gy = self.make_data()
        if self.dtype == numpy.float16:
            self.check_forward_options = {'atol': 1e-3, 'rtol': 1e-3}
            self.check_backward_options = {'atol': 3e-3, 'rtol': 1e-2}
            self.check_double_backward_options = {'atol': 3e-3, 'rtol': 1e-2}
        else:
            self.check_forward_options = {'atol': 1e-7, 'rtol': 1e-7}
            self.check_backward_options = {'atol': 1e-4, 'rtol': 1e-3}
            self.check_double_backward_options = {'atol': 1e-4, 'rtol': 1e-3}

    def check_forward(self, op, op_np, x_data):
        x = chainer.Variable(x_data)
        y = op(x)
        self.assertEqual(x.data.dtype, y.data.dtype)
        testing.assert_allclose(
            op_np(self.x), y.data, **self.check_forward_options)

    def check_forward_cpu(self, op, op_np):
        self.check_forward(op, op_np, self.x)

    def check_forward_gpu(self, op, op_np):
        self.check_forward(op, op_np, cuda.to_gpu(self.x))

    def check_backward(self, op, x_data, y_grad):
        gradient_check.check_backward(
            op, x_data, y_grad, dtype=numpy.float64,
            **self.check_backward_options)

    def check_backward_cpu(self, op):
        self.check_backward(op, self.x, self.gy)

    def check_backward_gpu(self, op):
        self.check_backward(op, cuda.to_gpu(self.x), cuda.to_gpu(self.gy))

    def check_double_backward(self, op, x_data, y_grad, y_grad_grad):
        gradient_check.check_double_backward(
            op, x_data, y_grad, y_grad_grad, dtype=numpy.float64,
            **self.check_double_backward_options)

    def check_double_backward_cpu(self, op):
        self.check_double_backward(op, self.x, self.gy, self.ggy)

    def check_double_backward_gpu(self, op):
        self.check_double_backward(op, cuda.to_gpu(
            self.x), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggy))

    def check_label(self, op, expected):
        self.assertEqual(op().label, expected)


@testing.parameterize(*testing.product({
    'shape': [(3, 2), ()],
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestErf(UnaryFunctionsTestBase):

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

    def test_forward_cpu(self):
        self.check_forward_cpu(F.erf, _erf_cpu)

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward_gpu(F.erf, _erf_gpu)

    def test_backward_cpu(self):
        self.check_backward_cpu(F.erf)

    @attr.gpu
    def test_backward_gpu(self):
        self.check_backward_gpu(F.erf)

    def test_label(self):
        self.check_label(F.Erf, 'erf')


testing.run_module(__name__, __file__)

@@ -40,23 +40,8 @@
 py%0A%0A
-import chainer%0A
 from
@@ -115,83 +115,15 @@
 ort 
-gradient_check%0Afrom chainer import testing%0Afrom chainer.testing import attr
+testing
 %0A%0A%0Ad
@@ -131,24 +131,31 @@
 f _erf_cpu(x
+, dtype
 ):%0A    retur
@@ -180,16 +180,32 @@
 math.erf
+, otypes=%5Bdtype%5D
 )(x)%0A%0A%0Ad
@@ -217,16 +217,23 @@
 rf_gpu(x
+, dtype
 ):%0A    r
@@ -277,2885 +277,298 @@
 u(x)
-))%0A%0A%0Aclass UnaryFunctionsTestBase(unittest.TestCase):%0A%0A    def make_data(self):%0A        raise NotImplementedError%0A%0A    def setUp(self):%0A        self.x, self.gy = self.make_data()%0A        if self.dtype == numpy.float16:%0A            self.check_forward_options = %7B'atol': 1e-3, 'rtol': 1e-3%7D%0A            self.check_backward_options = %7B'atol': 3e-3, 'rtol': 1e-2%7D%0A            self.check_double_backward_options = %7B'atol': 3e-3, 'rtol': 1e-2%7D%0A        else:%0A            self.check_forward_options = %7B'atol': 1e-7, 'rtol': 1e-7%7D%0A            self.check_backward_options = %7B'atol': 1e-4, 'rtol': 1e-3%7D%0A            self.check_double_backward_options = %7B'atol': 1e-4, 'rtol': 1e-3%7D%0A%0A    def check_forward(self, op, op_np, x_data):%0A        x = chainer.Variable(x_data)%0A        y = op(x)%0A        self.assertEqual(x.data.dtype, y.data.dtype)%0A        testing.assert_allclose(%0A            op_np(self.x), y.data, **self.check_forward_options)%0A%0A    def check_forward_cpu(self, op, op_np):%0A        self.check_forward(op, op_np, self.x)%0A%0A    def check_forward_gpu(self, op, op_np):%0A        self.check_forward(op, op_np, cuda.to_gpu(self.x))%0A%0A    def check_backward(self, op, x_data, y_grad):%0A        gradient_check.check_backward(%0A            op, x_data, y_grad, dtype=numpy.float64,%0A            **self.check_backward_options)%0A%0A    def check_backward_cpu(self, op):%0A        self.check_backward(op, self.x, self.gy)%0A%0A    def check_backward_gpu(self, op):%0A        self.check_backward(op, cuda.to_gpu(self.x), cuda.to_gpu(self.gy)
+, dtype))%0A%0A%0Adef _erf_expected(x, dtype):%0A    if cuda.get_array_module(x) is numpy:%0A        return _erf_cpu(x, dtype
 )%0A
-%0A
     
-def check_double_backward(self, op, x_data, y_grad, y_grad_grad):%0A        gradient_check.check_double_backward(%0A            op, x_data, y_grad, y_grad_grad, dtype=numpy.float64,%0A            **self.check_double_backward_options)%0A%0A    def check_double_backward_cpu(self, op):%0A        self.check_double_backward(op, self.x, self.gy, self.ggy)%0A%0A    def check_double_backward_gpu(self, op):%0A        self.check_double_backward(op, cuda.to_gpu(%0A            self.x), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggy))%0A%0A    def check_label(self, op, expected):%0A        self.assertEqual(op().label, expected)%0A%0A%0A@testing.parameterize(*testing.product(%7B%0A    'shape': %5B(3, 2), ()%5D,%0A    'dtype': %5Bnumpy.float16, numpy.float32, numpy.float64%5D,%0A%7D))%0Aclass TestErf(UnaryFunctionsTestBase):%0A%0A    def make_data(self):%0A        x = numpy.random.uniform(-10.0, 10.0, self.shape).astype(self.dtype)%0A        gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)%0A        return x, gy%0A%0A    def test_forward_cpu(self):%0A        self.check_forward_cpu(F.erf, _erf_cpu)%0A%0A    @attr.gpu%0A    def test_forward_gpu(self):%0A        self.check_forward_gpu(F.erf, _erf_gpu)%0A%0A    def test_backward_cpu(self):%0A        self.check_backward_cpu(F.erf)%0A%0A    @attr.gpu%0A    def test_backward_gpu(self):%0A        self.check_backward_gpu(F.erf)%0A%0A    def test_label(self):%0A        self.check_label(F.Erf, 'erf')
+else:%0A        return _erf_gpu(x, dtype)%0A%0A%0A@testing.unary_math_function_unittest(%0A    F.erf,%0A    func_expected=_erf_expected,%0A)%0Aclass TestErf(unittest.TestCase):%0A    pass
 %0A%0A%0At

Use testing.unary_math_function_unittest