code: import unittest
  
  import numpy
  
  import chainer
  from chainer import cuda
  from chainer import functions
  from chainer import gradient_check
  from chainer import testing
  from chainer.testing import attr
  
  
  class TestROIPooling2D(unittest.TestCase):
  
      def setUp(self):
          N = 3
          n_channels = 3
          self.x = numpy.arange(
              N * n_channels * 12 * 8,
              dtype=numpy.float32).reshape((N, n_channels, 12, 8))
          numpy.random.shuffle(self.x)
          self.x = 2 * self.x / self.x.size - 1
          self.rois = numpy.array([
              [0, 1, 1, 6, 6],
              [2, 6, 2, 7, 11],
              [1, 3, 1, 5, 10],
              [0, 3, 3, 3, 3]
          ], dtype=numpy.float32)
          n_rois = self.rois.shape[0]
          self.outh, self.outw = 5, 7
          self.spatial_scale = 0.6
          self.gy = numpy.random.uniform(
              -1, 1, (n_rois, n_channels,
                      self.outh, self.outw)).astype(numpy.float32)
          self.check_backward_options = {'atol': 1e-3, 'rtol': 1e-2}
  
      def check_forward(self, x_data, roi_data):
          x = chainer.Variable(x_data)
          rois = chainer.Variable(roi_data)
          y = functions.roi_pooling_2d(
              x, rois, outh=self.outh, outw=self.outw,
              spatial_scale=self.spatial_scale)
          self.assertEqual(y.data.dtype, numpy.float32)
          y_data = cuda.to_cpu(y.data)
  
          self.assertEqual(self.gy.shape, y_data.shape)
  
      def test_forward_cpu(self):
          self.check_forward(self.x, self.rois)
  
      @attr.gpu
      def test_forward_gpu(self):
          self.check_forward(cuda.to_gpu(self.x), cuda.to_gpu(self.rois))
  
      @attr.gpu
      def test_forward_cpu_gpu_equal(self):
          # cpu
          x_cpu = chainer.Variable(self.x)
          rois_cpu = chainer.Variable(self.rois)
          y_cpu = functions.roi_pooling_2d(
              x_cpu, rois_cpu, outh=self.outh, outw=self.outw,
              spatial_scale=self.spatial_scale)
  
          # gpu
          x_gpu = chainer.Variable(cuda.to_gpu(self.x))
          rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
          y_gpu = functions.roi_pooling_2d(
              x_gpu, rois_gpu, outh=self.outh, outw=self.outw,
              spatial_scale=self.spatial_scale)
          testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data))
  
      def check_backward(self, x_data, roi_data, y_grad):
          gradient_check.check_backward(
              functions.ROIPooling2D(outh=self.outh,
                                     outw=self.outw,
                                     spatial_scale=self.spatial_scale),
              (x_data, roi_data), y_grad, no_grads=[False, True],
              **self.check_backward_options)
  
      def test_backward_cpu(self):
          self.check_backward(self.x, self.rois, self.gy)
  
      @attr.gpu
      def test_backward_gpu(self):
          self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.rois),
                              cuda.to_gpu(self.gy))
  
  
  testing.run_module(__name__, __file__)
   import unittest
  
  import mock
  import numpy
  
  import chainer
  from chainer import cuda
  from chainer import functions
  from chainer import gradient_check
  from chainer import testing
  from chainer.testing import attr
  
  
  @testing.parameterize(*testing.product({
      'shape': [(3, 2), ()],
      'dtype': [numpy.float16, numpy.float32, numpy.float64],
  }))
  @testing.fix_random()
  class TestReLU(unittest.TestCase):
  
      def setUp(self):
          # Avoid unstability of numerical grad
          self.x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
          self.x[(-0.1 < self.x) & (self.x < 0.1)] = 0.5
          self.gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
          self.ggx = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
          self.check_backward_options = {}
          self.check_double_backward_options = {}
          if self.dtype == numpy.float16:
              self.check_double_backward_options = {'atol': 1e-3, 'rtol': 1e-2}
  
      def check_forward(self, x_data, use_cudnn='always'):
          x = chainer.Variable(x_data)
          with chainer.using_config('use_cudnn', use_cudnn):
              y = functions.relu(x)
          self.assertEqual(y.data.dtype, self.dtype)
  
          expected = self.x.copy()
          expected[expected < 0] = 0
  
          testing.assert_allclose(expected, y.data)
  
      def test_forward_cpu(self):
          self.check_forward(self.x)
  
      @attr.gpu
      def test_forward_gpu(self):
          self.check_forward(cuda.to_gpu(self.x))
  
      @attr.gpu
      def test_forward_gpu_no_cudnn(self):
          self.check_forward(cuda.to_gpu(self.x), 'never')
  
      def check_backward(self, x_data, y_grad, use_cudnn='always'):
          with chainer.using_config('use_cudnn', use_cudnn):
              gradient_check.check_backward(
                  functions.relu, x_data, y_grad, dtype=numpy.float64,
                  **self.check_backward_options)
  
      def test_backward_cpu(self):
          self.check_backward(self.x, self.gy)
  
      @attr.gpu
      def test_backward_gpu(self):
          self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
  
      @attr.gpu
      def test_backward_gpu_non_contiguous(self):
          self.check_backward(cuda.cupy.asfortranarray(cuda.to_gpu(self.x)),
                              cuda.cupy.asfortranarray(cuda.to_gpu(self.gy)))
  
      @attr.gpu
      def test_backward_cpu_no_cudnn(self):
          self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy), 'never')
  
      def check_double_backward(self, x_data, y_grad, x_grad_grad,
                                use_cudnn='always'):
          def f(x):
              x = functions.relu(x)
              return x * x
  
          with chainer.using_config('use_cudnn', use_cudnn):
              gradient_check.check_double_backward(
                  f, x_data, y_grad, x_grad_grad, dtype=numpy.float64,
                  **self.check_double_backward_options)
  
      def test_double_backward_cpu(self):
          self.check_double_backward(self.x, self.gy, self.ggx)
  
      @attr.gpu
      def test_double_backward_gpu(self):
          self.check_double_backward(cuda.to_gpu(self.x),
                                     cuda.to_gpu(self.gy),
                                     cuda.to_gpu(self.ggx))
  
      @attr.gpu
      def test_double_backward_gpu_non_contiguous(self):
          self.check_double_backward(
              cuda.cupy.asfortranarray(cuda.to_gpu(self.x)),
              cuda.cupy.asfortranarray(cuda.to_gpu(self.gy)),
              cuda.cupy.asfortranarray(cuda.to_gpu(self.ggx)))
  
      @attr.gpu
      def test_double_backward_cpu_no_cudnn(self):
          self.check_double_backward(cuda.to_gpu(self.x),
                                     cuda.to_gpu(self.gy),
                                     cuda.to_gpu(self.ggx),
                                     'never')
  
  
  @testing.parameterize(*testing.product({
      'use_cudnn': ['always', 'auto', 'never'],
      'dtype': [numpy.float16, numpy.float32, numpy.float64],
  }))
  @attr.cudnn
  class TestReLUCudnnCall(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)
          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.relu(x)
  
      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.run_module(__name__, __file__)
   import unittest
  
  import numpy
  from six import moves
  
  from chainer import cuda
  from chainer import testing
  from chainer.testing import attr
  from chainer.utils import conv
  
  
  class TestConv(unittest.TestCase):
  
      def check_conv_outsize(self, size, k, s, p, d):
          # When cover_all == False, `outsize` is the maximum integer that
          # satisfies "(outsize - 1) * s + k <= w"
          w = size + p * 2
          dk = k + (k - 1) * (d - 1)
          outsize = conv.get_conv_outsize(size, k, s, p, cover_all=False, d=d)
          self.assertTrue((outsize - 1) * s + dk <= w < outsize * s + dk)
  
      def check_conv_outsize_cover_all(self, size, k, s, p, d):
          # When cover_all == True, `outsize` is the minimum integer that
          # satisfies "w <= (outsize - 1) * s + k"
          w = size + p * 2
          dk = k + (k - 1) * (d - 1)
          outsize = conv.get_conv_outsize(size, k, s, p, cover_all=True, d=d)
          self.assertTrue((outsize - 2) * s + dk < w <= (outsize - 1) * s + dk)
  
      def test_conv_outsize1(self):
          self.check_conv_outsize(10, 4, 3, 2, 1)
  
      def test_conv_outsize2(self):
          self.check_conv_outsize(10, 4, 4, 2, 1)
  
      def test_conv_outsize3(self):
          self.check_conv_outsize(10, 4, 3, 2, 2)
  
      def test_conv_outsize_cover_all1(self):
          self.check_conv_outsize_cover_all(10, 4, 3, 2, 1)
  
      def test_conv_outsize_cover_all2(self):
          self.check_conv_outsize_cover_all(10, 4, 4, 2, 1)
  
      def test_conv_outsize_cover_all3(self):
          self.check_conv_outsize_cover_all(10, 4, 3, 2, 2)
  
  
  @testing.parameterize(*testing.product({
      'params': [
          (1, 1, 1, 1, 1, 1, 1, 1),
          (2, 2, 2, 2, 2, 2, 2, 2),
          (1, 2, 2, 1, 1, 2, 1, 1),
          (1, 2, 3, 4, 1, 2, 1, 1),
          (1, 2, 3, 4, 4, 5, 2, 3),
          (3, 3, 2, 2, 1, 1, 1, 1),
      ],
  }))
  class TestIm2Col(unittest.TestCase):
  
      def setUp(self):
          self.dtype = numpy.float32
          self.w = 10
          self.h = 8
          shape = (2, 3, self.h, self.w)
          self.img = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
  
      def check_im2col(self, kh, kw, sy, sx, ph, pw, dy, dx, gpu):
          if gpu:
              im2col = conv.im2col_gpu
              img = cuda.to_gpu(self.img)
          else:
              im2col = conv.im2col_cpu
              img = self.img
  
          col = im2col(img, kh, kw, sy, sx, ph, pw, dy=dy, dx=dx)
          col_h = conv.get_conv_outsize(self.h, kh, sy, ph, d=dy)
          col_w = conv.get_conv_outsize(self.w, kw, sx, pw, d=dx)
          self.assertEqual(col.shape, (2, 3, kh, kw, col_h, col_w))
  
          col = cuda.to_cpu(col)
  
          for y in moves.range(col_h):
              for x in moves.range(col_w):
                  for ky in moves.range(kh):
                      for kx in moves.range(kw):
                          oy = y * sy - ph + ky * dy
                          ox = x * sx - pw + kx * dx
                          if 0 <= oy < self.h and 0 <= ox < self.w:
                              testing.assert_allclose(
                                  col[:, :, ky, kx, y, x],
                                  self.img[:, :, oy, ox])
                          else:
                              testing.assert_allclose(
                                  col[:, :, ky, kx, y, x],
                                  numpy.zeros((2, 3), self.dtype))
  
      def test_im2col_cpu(self):
          self.check_im2col(*self.params, gpu=False)
  
      @attr.gpu
      def test_im2col_gpu(self):
          self.check_im2col(*self.params, gpu=True)
  
  
  @testing.parameterize(*testing.product({
      'params': [
          (1, 1, 1, 1, 1, 1, 1, 1),
          (2, 2, 2, 2, 2, 2, 2, 2),
          (1, 2, 2, 1, 1, 2, 1, 1),
          (1, 2, 3, 4, 1, 2, 1, 1),
          (1, 2, 3, 4, 4, 5, 2, 3),
          (3, 3, 2, 2, 1, 1, 1, 1),
      ],
  }))
  class TestCol2Im(unittest.TestCase):
  
      def setUp(self):
          self.dtype = numpy.float32
          self.w = 10
          self.h = 8
  
      def check_col2im(self, kh, kw, sy, sx, ph, pw, dy, dx, gpu):
          col_h = conv.get_conv_outsize(self.h, kh, sy, ph, d=dy)
          col_w = conv.get_conv_outsize(self.w, kw, sx, pw, d=dx)
          shape = (2, 3, kh, kw, col_h, col_w)
          col = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
  
          if gpu:
              col2im = conv.col2im_gpu
              col_data = cuda.to_gpu(col)
          else:
              col2im = conv.col2im_cpu
              col_data = col
  
          img = col2im(col_data, sy, sx, ph, pw, self.h, self.w, dy=dy, dx=dx)
          img = cuda.to_cpu(img)
          self.assertEqual(img.shape, (2, 3, self.h, self.w))
          for y in moves.range(self.h):
              for x in moves.range(self.w):
                  v = numpy.zeros((2, 3), self.dtype)
                  for ky in moves.range(kh):
                      for kx in moves.range(kw):
                          oy = (y + ph - ky * dy) // sy
                          ox = (x + pw - kx * dx) // sx
                          if ((y + ph - ky * dy) % sy == 0 and
                              (x + pw - kx * dx) % sx == 0 and
                                  0 <= oy < col_h and 0 <= ox < col_w):
                              v += col[:, :, ky, kx, oy, ox]
                  testing.assert_allclose(img[:, :, y, x], v)
  
      def test_col2im_cpu(self):
          self.check_col2im(*self.params, gpu=False)
  
      @attr.gpu
      def test_col2im_gpu(self):
          self.check_col2im(*self.params, gpu=True)
  
  
  testing.run_module(__name__, __file__)
  
apis: numpy.array numpy.random.uniform numpy.random.shuffle numpy.arange
apis: numpy.random.uniform
apis: numpy.random.uniform numpy.zeros