Python chainer.testing.attr.gpu() Examples

The following are 30 code examples of chainer.testing.attr.gpu(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.testing.attr , or try the search function .
Example #1
Source File: test_roi_align_2d.py    From chainer-mask-rcnn with MIT License 6 votes vote down vote up
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        y_cpu = functions.roi_align_2d(
            x_cpu, rois_cpu, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        y_gpu = functions.roi_align_2d(
            x_gpu, rois_gpu, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
Example #2
Source File: test_roi_average_align_2d.py    From chainer with MIT License 6 votes vote down vote up
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        roi_indices_cpu = chainer.Variable(self.roi_indices)
        y_cpu = functions.roi_average_align_2d(
            x_cpu, rois_cpu, roi_indices_cpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        roi_indices_gpu = chainer.Variable(cuda.to_gpu(self.roi_indices))
        y_gpu = functions.roi_average_align_2d(
            x_gpu, rois_gpu, roi_indices_gpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
Example #3
Source File: test_roi_max_pooling_2d.py    From chainer with MIT License 6 votes vote down vote up
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        roi_indices_cpu = chainer.Variable(self.roi_indices)
        y_cpu = functions.roi_max_pooling_2d(
            x_cpu, rois_cpu, roi_indices_cpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale)

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        roi_indices_gpu = chainer.Variable(cuda.to_gpu(self.roi_indices))
        y_gpu = functions.roi_max_pooling_2d(
            x_gpu, rois_gpu, roi_indices_gpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale)
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
Example #4
Source File: test_mlp_bn.py    From chainerrl with MIT License 6 votes vote down vote up
def _test_call(self, gpu):
        nonlinearity = getattr(F, self.nonlinearity)
        mlp = chainerrl.links.MLPBN(
            in_size=self.in_size,
            out_size=self.out_size,
            hidden_sizes=self.hidden_sizes,
            normalize_input=self.normalize_input,
            normalize_output=self.normalize_output,
            nonlinearity=nonlinearity,
            last_wscale=self.last_wscale,
        )
        batch_size = 7
        x = np.random.rand(batch_size, self.in_size).astype(np.float32)
        if gpu >= 0:
            mlp.to_gpu(gpu)
            x = chainer.cuda.to_gpu(x)
        y = mlp(x)
        self.assertEqual(y.shape, (batch_size, self.out_size))
        self.assertEqual(chainer.cuda.get_array_module(y),
                         chainer.cuda.get_array_module(x)) 
Example #5
Source File: test_roi_average_pooling_2d.py    From chainer with MIT License 6 votes vote down vote up
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        roi_indices_cpu = chainer.Variable(self.roi_indices)
        y_cpu = functions.roi_average_pooling_2d(
            x_cpu, rois_cpu, roi_indices_cpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale)

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        roi_indices_gpu = chainer.Variable(cuda.to_gpu(self.roi_indices))
        y_gpu = functions.roi_average_pooling_2d(
            x_gpu, rois_gpu, roi_indices_gpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale)
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
Example #6
Source File: backend.py    From chainer with MIT License 6 votes vote down vote up
def get_pytest_marks(self):
        marks = []
        if self.use_chainerx:
            marks.append(attr.chainerx)
            backend_name, device_index = self.chainerx_device.split(':')
            device_index = int(device_index)
            if backend_name == 'cuda':
                marks.append(attr.gpu)
                if device_index >= 1:
                    marks.append(attr.multi_gpu(device_index + 1))
        elif self.use_cuda:
            marks.append(attr.gpu)
            if self.use_cudnn != 'never':
                marks.append(attr.cudnn)
            if self.cuda_device >= 1:
                marks.append(attr.multi_gpu(self.cuda_device + 1))
        else:
            if self.use_ideep != 'never':
                marks.append(attr.ideep)

        assert all(callable(_) for _ in marks)
        return marks 
Example #7
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def test_im2col_nd_1_cpu(self):
        ndim = len(self.dims)
        ksize = (1,) * ndim
        stride = (1,) * ndim
        pad = (1,) * ndim
        self.check_im2col_nd(ksize, stride, pad, gpu=False) 
Example #8
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def test_col2im_1_cpu(self):
        ndim = len(self.dims)
        ksize = (1,) * ndim
        stride = (1,) * ndim
        pad = (1,) * ndim
        self.check_col2im_nd(ksize, stride, pad, gpu=False) 
Example #9
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def check_col2im_nd(self, ksize, stride, pad, gpu):
        dims = self.dims
        outs = tuple(conv_nd.get_conv_outsize(d, k, s, p)
                     for (d, k, s, p) in zip(dims, ksize, stride, pad))
        col_shape = (2, 3) + ksize + outs
        col = numpy.random.uniform(-1, 1, col_shape).astype(numpy.float32)

        if gpu:
            col_data = cuda.to_gpu(col)
        else:
            col_data = col

        img = conv_nd.col2im_nd(col_data, stride, pad, dims)
        img = cuda.to_cpu(img)
        img_shape = (2, 3) + dims
        self.assertEqual(img.shape, img_shape)
        for n in moves.range(2):
            for c in moves.range(3):
                for xs in itertools.product(
                        *[moves.range(d) for d in dims]):
                    v = numpy.float32(0.0)
                    for dxs in itertools.product(
                            *[moves.range(k) for k in ksize]):
                        oxs = tuple((x + p - dx) // s
                                    for (x, p, dx, s)
                                    in zip(xs, pad, dxs, stride))
                        if all((x + p - dx) % s == 0
                               for (x, p, dx, s)
                               in zip(xs, pad, dxs, stride)) and \
                            all(0 <= ox < out
                                for (ox, out) in zip(oxs, outs)):
                            col_index = (n, c) + dxs + oxs
                            v += col[col_index]
                    img_index = (n, c) + xs
                    self.assertAlmostEqual(img[img_index], v) 
Example #10
Source File: test_conv.py    From chainer with MIT License 5 votes vote down vote up
def test_im2col_cpu(self):
        self.check_im2col(*self.params, gpu=False) 
Example #11
Source File: test_conv.py    From chainer with MIT License 5 votes vote down vote up
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:
            col_data = cuda.to_gpu(col)
        else:
            col_data = col

        img = conv.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) 
Example #12
Source File: test_conv.py    From chainer with MIT License 5 votes vote down vote up
def test_col2im_cpu(self):
        self.check_col2im(*self.params, gpu=False) 
Example #13
Source File: test_conv.py    From chainer with MIT License 5 votes vote down vote up
def test_col2im_gpu(self):
        self.check_col2im(*self.params, gpu=True) 
Example #14
Source File: test_backprop_utils.py    From chainer with MIT License 5 votes vote down vote up
def _get_method(self, prefix, gpu):
        suffix = 'gpu' if gpu else 'cpu'
        return getattr(self.f, prefix + '_' + suffix) 
Example #15
Source File: test_weight_standardization.py    From chainer with MIT License 5 votes vote down vote up
def check_weight_is_parameter(self, gpu):
        layer, hook = self._init_layer()
        if gpu:
            with testing.assert_warns(DeprecationWarning):
                layer = layer.to_gpu()
        source_weight = getattr(layer, hook.weight_name)
        x = cuda.to_gpu(self.x) if gpu else self.x
        layer(x)
        assert getattr(layer, hook.weight_name) is source_weight 
Example #16
Source File: test_affine_channel_2d.py    From chainer-mask-rcnn with MIT License 5 votes vote down vote up
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        W = chainer.Variable(self.W)
        b = chainer.Variable(self.b)
        y_cpu = functions.affine_channel_2d(x_cpu, W, b)

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        W = chainer.Variable(cuda.to_gpu(self.W))
        b = chainer.Variable(cuda.to_gpu(self.b))
        y_gpu = functions.affine_channel_2d(x_gpu, W, b)
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
Example #17
Source File: test_conv.py    From chainer with MIT License 5 votes vote down vote up
def check_im2col(self, kh, kw, sy, sx, ph, pw, dy, dx, gpu):
        if gpu:
            img = cuda.to_gpu(self.img)
        else:
            img = self.img

        col = conv.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)) 
Example #18
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def test_im2col_nd_3_cpu(self):
        ndim = len(self.dims)
        ksize = (1, 2, 1)[:ndim]
        stride = (2, 1, 2)[:ndim]
        pad = (1, 2, 1)[:ndim]
        self.check_im2col_nd(ksize, stride, pad, gpu=False) 
Example #19
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def test_im2col_nd_2_cpu(self):
        ndim = len(self.dims)
        ksize = (2,) * ndim
        stride = (2,) * ndim
        pad = (2,) * ndim
        self.check_im2col_nd(ksize, stride, pad, gpu=False) 
Example #20
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def test_det_identity_gpu(self):
        self.det_identity(gpu=True) 
Example #21
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def check_im2col_nd(self, ksize, stride, pad, gpu):
        dims = self.dims
        if gpu:
            img = cuda.to_gpu(self.img)
        else:
            img = self.img

        col = conv_nd.im2col_nd(img, ksize, stride, pad)
        outs = tuple(conv_nd.get_conv_outsize(d, k, s, p)
                     for (d, k, s, p) in zip(dims, ksize, stride, pad))
        expected_shape = (2, 3) + ksize + outs
        self.assertEqual(col.shape, expected_shape)

        col = cuda.to_cpu(col)

        for n in moves.range(2):
            for c in moves.range(3):
                for xs in itertools.product(
                        *[moves.range(out) for out in outs]):
                    for dxs in itertools.product(
                            *[moves.range(k) for k in ksize]):
                        oxs = tuple(x * s - p + dx
                                    for (x, s, p, dx)
                                    in zip(xs, stride, pad, dxs))
                        if all(0 <= ox < d for (ox, d) in zip(oxs, dims)):
                            col_index = (n, c) + dxs + xs
                            img_index = (n, c) + oxs
                            self.assertEqual(
                                col[col_index], self.img[img_index])
                        else:
                            col_index = (n, c) + dxs + xs
                            self.assertEqual(col[col_index], 0) 
Example #22
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def test_det_scaling_gpu(self):
        self.det_scaling(gpu=True) 
Example #23
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def test_det_transpose_cpu(self):
        self.det_transpose(gpu=False) 
Example #24
Source File: test_roi_pooling_2d.py    From chainer with MIT License 5 votes vote down vote up
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)) 
Example #25
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def det_identity(self, gpu=False):
        if self.batched:
            chk = numpy.ones(len(self.x), dtype=self.dtype)
            dt = numpy.identity(self.x.shape[1], dtype=self.dtype)
            idt = numpy.repeat(dt[None], len(self.x), axis=0)
        else:
            idt = numpy.identity(self.x.shape[1], dtype=self.dtype)
            chk = numpy.ones(1, dtype=self.dtype)
        if gpu:
            chk = cuda.to_gpu(chk)
            idt = cuda.to_gpu(idt)
        idtv = chainer.Variable(idt)
        idtd = self.det(idtv)
        testing.assert_allclose(idtd.data, chk, **self.check_forward_options) 
Example #26
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def test_answer_gpu_cpu(self):
        x = cuda.to_gpu(self.x)
        y = F.batch_det(chainer.Variable(x))
        gpu = cuda.to_cpu(y.data)
        if self.dtype == numpy.float16:
            cpu = numpy.linalg.det(
                self.x.astype(numpy.float32)).astype(numpy.float16)
            testing.assert_allclose(gpu, cpu, atol=5e-3, rtol=5e-3)
        else:
            cpu = numpy.linalg.det(self.x)
            testing.assert_allclose(gpu, cpu) 
Example #27
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def test_answer_gpu_cpu(self):
        x = cuda.to_gpu(self.x)
        y = F.det(chainer.Variable(x))
        gpu = cuda.to_cpu(y.data)
        if self.dtype == numpy.float16:
            cpu = numpy.linalg.det(
                self.x.astype(numpy.float32)).astype(numpy.float16)
            testing.assert_allclose(gpu, cpu, atol=5e-3, rtol=5e-3)
        else:
            cpu = numpy.linalg.det(self.x)
            testing.assert_allclose(gpu, cpu) 
Example #28
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def test_det_product_cpu(self):
        self.det_product(gpu=False) 
Example #29
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def det_product(self, gpu=False):
        if gpu:
            cx = cuda.to_gpu(self.x)
            cy = cuda.to_gpu(self.y)
        else:
            cx = self.x
            cy = self.y
        vx = chainer.Variable(cx)
        vy = chainer.Variable(cy)
        dxy1 = self.det(self.matmul(vx, vy))
        dxy2 = self.det(vx) * self.det(vy)
        testing.assert_allclose(
            dxy1.data, dxy2.data, **self.check_forward_options) 
Example #30
Source File: test_det.py    From chainer with MIT License 5 votes vote down vote up
def test_det_identity_cpu(self):
        self.det_identity(gpu=False)