コード例 #1
0
    def test_call_cudnn_backward(self):
        with chainer.using_config('use_cudnn', self.use_cudnn):
            # This test includes the case in which cudnn < v3
            # and cudnn_deterministic is True, in which
            # the backward method of chainer.functions.Deconvolution2D
            # must raise an error.
            # But in this case, its forward method should raise
            # an error as well.
            # Therefore, we intentionally set cudnn_deterministic
            # to False so that the forward method does not
            # raise an error.
            with chainer.using_config('cudnn_deterministic', False):
                y = self.forward()
        y.grad = self.gy

        data_func_name = 'cupy.cuda.cudnn.convolutionForward'
        filter_func_name = 'cupy.cuda.cudnn.convolutionBackwardFilter_v3'

        with chainer.using_config('use_cudnn', self.use_cudnn):
            with chainer.using_config('cudnn_deterministic',
                                      self.cudnn_deterministic):
                with testing.patch(data_func_name) as data_func,\
                        testing.patch(filter_func_name) as filter_func:
                    y.backward()
                    self.assertEqual(data_func.called, self.should_call_cudnn)
                    self.assertEqual(filter_func.called,
                                     self.should_call_cudnn)
コード例 #2
0
ファイル: test_deconvolution_2d.py プロジェクト: tkng/chainer
    def test_call_cudnn_backward(self):
        with chainer.using_config('use_cudnn', self.use_cudnn):
            # This test includes the case in which cudnn < v3
            # and cudnn_deterministic is True, in which
            # the backward method of chainer.functions.Deconvolution2D
            # must raise an error.
            # But in this case, its forward method should raise
            # an error as well.
            # Therefore, we intentionally set cudnn_deterministic
            # to False so that the forward method does not
            # raise an error.
            with chainer.using_config('cudnn_deterministic', False):
                y = self.forward()
        y.grad = self.gy

        data_func_name = 'cupy.cuda.cudnn.convolutionForward'
        filter_func_name = 'cupy.cuda.cudnn.convolutionBackwardFilter_v3'

        with chainer.using_config('use_cudnn', self.use_cudnn):
            with chainer.using_config('cudnn_deterministic',
                                      self.cudnn_deterministic):
                with testing.patch(data_func_name) as data_func,\
                        testing.patch(filter_func_name) as filter_func:
                    y.backward()
                    self.assertEqual(
                        data_func.called, self.should_call_cudnn)
                    self.assertEqual(
                        filter_func.called, self.should_call_cudnn)
コード例 #3
0
 def test_call_cudnn_forward(self):
     name = 'cupy.cuda.cudnn.convolutionForward'
     name2 = 'chainer.functions.connection.convolution_2d' \
         '.Convolution2DFunction._tensor_core_adjust_algo'
     with chainer.using_config('use_cudnn', self.use_cudnn), \
             chainer.using_config('cudnn_deterministic',
                                  self.cudnn_deterministic), \
             testing.patch(name) as func, \
             testing.patch(name2) as tensor_core_adjust_algo:
         self.forward()
         self.assertEqual(func.called, self.should_call_cudnn)
         if not self.can_use_tensor_core:
             self.assertEqual(tensor_core_adjust_algo.called, False)
コード例 #4
0
    def test_call_cudnn_backward(self):
        data_func_name = 'cupy.cudnn.convolution_forward'
        filter_func_name = 'cupy.cudnn.convolution_backward_filter'

        with chainer.using_config('use_cudnn', self.use_cudnn):
            with chainer.using_config('cudnn_deterministic',
                                      self.cudnn_deterministic):
                y = self.forward()
                y.grad = self.gy
                with testing.patch(data_func_name) as data_func, \
                        testing.patch(filter_func_name) as filter_func:
                    y.backward()
                    self.assertEqual(data_func.called, self.should_call_cudnn)
                    self.assertEqual(filter_func.called,
                                     self.should_call_cudnn)
コード例 #5
0
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with chainer.using_config('cudnn_deterministic',
                                   self.cudnn_deterministic):
             with testing.patch('cupy.cudnn.convolution_forward') as func:
                 self.forward()
                 self.assertEqual(func.called, self.should_call_cudnn)
コード例 #6
0
ファイル: test_tanh.py プロジェクト: z331565360/chainer
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         default_func = cuda.cupy.cudnn.activation_forward
         with testing.patch('cupy.cudnn.activation_forward') as func:
             func.side_effect = default_func
             self.forward()
             self.assertEqual(func.called, self.expect)
コード例 #7
0
ファイル: test_log_softmax.py プロジェクト: Fhrozen/chainer
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         y = self.forward()
         y.grad = self.gy
         with testing.patch('cupy.cuda.cudnn.softmaxBackward') as func:
             y.backward()
             self.assertEqual(func.called, self.expect)
コード例 #8
0
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         y = self.forward()
         y.grad = self.gy
         with testing.patch('cupy.cudnn.convolution_forward') as func:
             y.backward()
             self.assertEqual(func.called, self.expected)
コード例 #9
0
    def test_call_cudnn_backward(self):
        data_func_name = 'cupy.cudnn.convolution_forward'
        filter_func_name = 'cupy.cudnn.convolution_backward_filter'

        with chainer.using_config('use_cudnn', self.use_cudnn):
            with chainer.using_config('cudnn_deterministic',
                                      self.cudnn_deterministic):
                y = self.forward()
                y.grad = self.gy
                with testing.patch(data_func_name) as data_func, \
                        testing.patch(filter_func_name) as filter_func:
                    y.backward()
                    self.assertEqual(
                        data_func.called, self.should_call_cudnn)
                    self.assertEqual(
                        filter_func.called, self.should_call_cudnn)
コード例 #10
0
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch(
                 'cupy.cuda.cudnn.batchNormalizationForwardTraining'
         ) as func:
             self.forward()
             self.assertEqual(func.called, self.expect)
コード例 #11
0
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch('cupy.cuda.cudnn.poolingForward') as func:
             self.forward()
             self.assertEqual(func.called,
                              chainer.should_use_cudnn('>=auto') and
                              self.ndim > 1)
コード例 #12
0
ファイル: test_tanh.py プロジェクト: okuta/chainer
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         default_func = cuda.cupy.cudnn.activation_forward
         with testing.patch('cupy.cudnn.activation_forward') as func:
             func.side_effect = default_func
             self.forward()
             self.assertEqual(func.called, self.expect)
コード例 #13
0
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch(
                 'cupy.cuda.cudnn.batchNormalizationForwardTraining'
         ) as func:
             self.forward()
             self.assertEqual(func.called, self.expect)
コード例 #14
0
ファイル: test_convolution_2d.py プロジェクト: mitmul/chainer
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with chainer.using_config('cudnn_deterministic',
                                   self.cudnn_deterministic):
             with testing.patch('cupy.cudnn.convolution_forward') as func:
                 self.forward()
                 self.assertEqual(func.called, self.should_call_cudnn)
コード例 #15
0
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch('cupy.cudnn.pooling_forward') as func:
             self.forward()
             self.assertEqual(
                 func.called,
                 chainer.should_use_cudnn('>=auto') and self.ndim > 1)
コード例 #16
0
    def test_called(self):
        with testing.patch(
                'cupy.cudnn.convolution_backward_filter', autospec=True) as f:
            # cuDNN version >= v3 supports `cudnn_deterministic` option
            self._run()

            # in Convolution2DFunction.backward_gpu()
            assert f.called == self.should_call_cudnn
コード例 #17
0
ファイル: test_n_step_gru.py プロジェクト: asi1024/chainer
 def check_call_cudnn_backward(self, use_cudnn):
     with chainer.using_config('use_cudnn', use_cudnn):
         expect = chainer.should_use_cudnn('>=auto', 5000)
         hy, ys = self.call_forward(True)
         hy.grad = _to_gpu(self.dhy)
         with testing.patch('cupy.cudnn.rnn_backward_weights') as func:
             hy.backward()
         assert func.called == expect
コード例 #18
0
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         y = self.forward()
         y.grad = self.gy
         name = 'cupy.cuda.cudnn.convolutionBackwardData_v3'
         with testing.patch(name) as func:
             y.backward()
             self.assertEqual(func.called, self.expect)
コード例 #19
0
 def check_call_cudnn_backward(self, use_cudnn):
     with chainer.using_config('use_cudnn', use_cudnn):
         expect = chainer.should_use_cudnn('>=auto', 5000)
         hy, ys = self.call_forward(True)
         hy.grad = _to_gpu(self.dhy)
         with testing.patch('cupy.cuda.cudnn.RNNBackwardWeights') as func:
             hy.backward()
         assert func.called == expect
コード例 #20
0
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         y = self.forward()
         y.grad = self.gy
         name = 'cupy.cudnn.convolution_backward_data'
         with testing.patch(name) as func:
             y.backward()
             self.assertEqual(func.called, self.expect)
コード例 #21
0
ファイル: test_dropout.py プロジェクト: zwh930712/chainer
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         y = self.forward()
         y.grad = self.gy
         with testing.patch(
                 'chainer.backends.cuda.get_cudnn_dropout_states') as func:
             y.backward()
             assert func.called == (self.use_cudnn == 'always')
コード例 #22
0
ファイル: test_convolution_2d.py プロジェクト: mitmul/chainer
    def test_called(self):
        with testing.patch(
                'cupy.cudnn.convolution_backward_filter', autospec=True) as f:
            # cuDNN version >= v3 supports `cudnn_deterministic` option
            self._run()

            # in Convolution2DFunction.backward_gpu()
            assert f.called == self.should_call_cudnn
コード例 #23
0
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         expect = chainer.should_use_cudnn('>=auto') and self.ndim > 1
         y = self.forward()
     # should be consistent to forward regardless of use_cudnn config
     y.grad = self.gy
     with testing.patch('cupy.cuda.cudnn.poolingBackward') as func:
         y.backward()
         self.assertEqual(func.called, expect)
コード例 #24
0
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         expect = chainer.should_use_cudnn('>=auto') and self.ndim > 1
         y = self.forward()
     # should be consistent to forward regardless of use_cudnn config
     y.grad = self.gy
     with testing.patch('cupy.cudnn.pooling_backward') as func:
         y.backward()
         self.assertEqual(func.called, expect)
コード例 #25
0
 def test_pooling_nd_kernel_backward_memo(self):
     ndim = self.ndim
     with testing.patch(
             'chainer.functions.pooling.pooling_nd_kernel.'
             'PoolingNDKernelBackward._generate', wraps=None) as m:
         pooling_nd_kernel.PoolingNDKernelBackward.generate(ndim)
         m.assert_called_once_with(ndim)
         pooling_nd_kernel.PoolingNDKernelBackward.generate(ndim)
         # Check that the mocked _generate() function is called just once
         # because the result of generate() function is cached.
         m.assert_called_once_with(ndim)
コード例 #26
0
 def test_pooling_nd_kernel_backward_memo(self):
     ndim = self.ndim
     with testing.patch(
             'chainer.functions.pooling.pooling_nd_kernel.'
             'PoolingNDKernelBackward._generate',
             wraps=None) as m:
         pooling_nd_kernel.PoolingNDKernelBackward.generate(ndim)
         m.assert_called_once_with(ndim)
         pooling_nd_kernel.PoolingNDKernelBackward.generate(ndim)
         # Check that the mocked _generate() function is called just once
         # because the result of generate() function is cached.
         m.assert_called_once_with(ndim)
コード例 #27
0
    def test_called(self):
        with testing.patch(
                'chainer.functions.connection.convolution_2d.libcudnn',
                autospec=True) as mlibcudnn_conv, testing.patch(
                    'chainer.functions.connection.deconvolution_2d.libcudnn',
                    autospec=True) as mlibcudnn_deconv:

            # cuDNN version >= v3 supports `cudnn_deterministic` option
            x, W, b, y = self._run()

            # in Convolution2DFunction.backward_gpu()
            self.assertFalse(
                mlibcudnn_conv.getConvolutionBackwardFilterAlgorithm.called)
            self.assertEqual(
                mlibcudnn_conv.convolutionBackwardFilter_v3.call_count,
                self.should_call_cudnn)
            self.assertFalse(
                mlibcudnn_deconv.getConvolutionBackwardDataAlgorithm.called)
            self.assertEqual(
                mlibcudnn_deconv.convolutionBackwardData_v3.call_count,
                self.should_call_cudnn)
コード例 #28
0
    def test_called(self):
        with testing.patch(
                'chainer.functions.connection.deconvolution_2d.libcudnn'
        ) as mlibcudnn:
            # cuDNN version >= v3 supports `cudnn_deterministic` option
            x, W, b, y = self._run()

            # in Deconvolution2DFunction.forward_gpu()
            self.assertFalse(
                mlibcudnn.getConvolutionBackwardDataAlgorithm.called)

            # in Deconvolution2DFunction.backward_gpu()
            self.assertFalse(
                mlibcudnn.getConvolutionBackwardFilterAlgorithm.called)
コード例 #29
0
ファイル: test_deconvolution_2d.py プロジェクト: tkng/chainer
    def test_called(self):
        with testing.patch(
                'chainer.functions.connection.deconvolution_2d.libcudnn'
        ) as mlibcudnn:
            # cuDNN version >= v3 supports `cudnn_deterministic` option
            x, W, b, y = self._run()

            # in Deconvolution2DFunction.forward_gpu()
            self.assertFalse(
                mlibcudnn.getConvolutionBackwardDataAlgorithm.called)

            # in Deconvolution2DFunction.backward_gpu()
            self.assertFalse(
                mlibcudnn.getConvolutionBackwardFilterAlgorithm.called)
コード例 #30
0
ファイル: test_n_step_rnn.py プロジェクト: asi1024/chainer
    def check_call_cudnn_backward_inference(self, use_cudnn):
        with chainer.using_config('use_cudnn', use_cudnn), \
                chainer.using_config('train', False):
            hx = _wrap_variable(_to_gpu(self.hx))
            xs = _wrap_variable(_to_gpu(self.xs))
            ws = _wrap_variable(_to_gpu(self.ws))
            bs = _wrap_variable(_to_gpu(self.bs))
            hy, ys = functions.n_step_rnn(
                self.n_layers, self.dropout, hx, ws, bs, xs)

            hy.grad = _to_gpu(self.dhy)
            if chainer.should_use_cudnn('>=auto', 5000):
                with self.assertRaises(RuntimeError):
                    hy.backward()
            else:
                with testing.patch('cupy.cudnn.rnn_backward_weights') as func:
                    hy.backward()
                assert not func.called
コード例 #31
0
    def check_call_cudnn_backward_inference(self, use_cudnn):
        with chainer.using_config('use_cudnn', use_cudnn), \
                chainer.using_config('train', False):
            hx = _wrap_variable(_to_gpu(self.hx))
            xs = _wrap_variable(_to_gpu(self.xs))
            ws = _wrap_variable(_to_gpu(self.ws))
            bs = _wrap_variable(_to_gpu(self.bs))
            hy, ys = functions.n_step_rnn(self.n_layers, self.dropout, hx, ws,
                                          bs, xs)

            hy.grad = _to_gpu(self.dhy)
            if chainer.should_use_cudnn('>=auto', 5000):
                with self.assertRaises(RuntimeError):
                    hy.backward()
            else:
                with testing.patch('cupy.cudnn.rnn_backward_weights') as func:
                    hy.backward()
                assert not func.called
コード例 #32
0
    def call_link_with_samples(self, samples, func):
        # Call the link with given `samples` array.
        # `func` is a function in which the link is called.

        # mock sampler that returns the saved samples
        def mock_sample(shape):
            assert samples.shape == shape
            return samples.copy()

        # Wrap F.negative_sampling to replace sampler with the mock
        orig_negative_sampling = chainer.functions.negative_sampling

        def wrap_negative_sampling(*args, **kwargs):
            args = args[:3] + (mock_sample,) + args[4:]
            return orig_negative_sampling(*args, **kwargs)

        with testing.patch(
                'chainer.functions.loss.negative_sampling.negative_sampling',
                wraps=wrap_negative_sampling) as m:
            ret = func()
            assert m.call_count == 1

        return ret
コード例 #33
0
    def call_link_with_samples(self, samples, func):
        # Call the link with given `samples` array.
        # `func` is a function in which the link is called.

        # mock sampler that returns the saved samples
        def mock_sample(shape):
            assert samples.shape == shape
            return samples.copy()

        # Wrap F.negative_sampling to replace sampler with the mock
        orig_negative_sampling = chainer.functions.negative_sampling

        def wrap_negative_sampling(*args, **kwargs):
            args = args[:3] + (mock_sample, ) + args[4:]
            return orig_negative_sampling(*args, **kwargs)

        with testing.patch(
                'chainer.functions.loss.negative_sampling.negative_sampling',
                wraps=wrap_negative_sampling) as m:
            ret = func()
            assert m.call_count == 1

        return ret
コード例 #34
0
ファイル: test_softmax.py プロジェクト: z331565360/chainer
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch('cupy.cuda.cudnn.softmaxForward') as func:
             self.forward()
             self.assertEqual(func.called, self.expect)
コード例 #35
0
ファイル: test_log_softmax.py プロジェクト: Fhrozen/chainer
 def test_call_cudnn_forward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch('cupy.cuda.cudnn.softmaxForward') as func:
             self.forward()
             self.assertEqual(func.called, self.expect)
コード例 #36
0
 def test_call_cudnn_forward(self):
     name = 'cupy.cudnn.convolution_backward_data'
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch(name) as func:
             self.forward()
             self.assertEqual(func.called, self.expected)
コード例 #37
0
 def check_call_cudnn_forward_training(self, use_cudnn):
     with chainer.using_config('use_cudnn', use_cudnn):
         expect = chainer.should_use_cudnn('>=auto', 5000)
         with testing.patch('cupy.cudnn.rnn_forward_training') as func:
             self.call_forward(True)
         assert func.called == expect
コード例 #38
0
ファイル: test_n_step_gru.py プロジェクト: Fhrozen/chainer
 def check_call_cudnn_forward_training(self, use_cudnn):
     with chainer.using_config('use_cudnn', use_cudnn):
         expect = chainer.should_use_cudnn('>=auto', 5000)
         with testing.patch('cupy.cuda.cudnn.RNNForwardTraining') as func:
             self.call_forward(True)
         assert func.called == expect
コード例 #39
0
ファイル: test_deconvolution_nd.py プロジェクト: tkng/chainer
 def test_call_cudnn_forward(self):
     name = 'cupy.cuda.cudnn.convolutionBackwardData_v3'
     with chainer.using_config('use_cudnn', self.use_cudnn):
         with testing.patch(name) as func:
             self.forward()
             self.assertEqual(func.called, self.expected)
コード例 #40
0
 def check_call_cudnn_forward_inference(self, use_cudnn):
     with chainer.using_config('use_cudnn', use_cudnn):
         expect = chainer.should_use_cudnn('>=auto', 5000)
         with testing.patch('cupy.cuda.cudnn.RNNForwardInference') as func:
             self.call_forward(False)
         assert func.called == expect
コード例 #41
0
 def test_call_cudnn_backward(self):
     with chainer.using_config('use_cudnn', self.use_cudnn):
         y = self.forward()
         with testing.patch('cupy.cudnn.activation_forward') as func:
             y.backward()
             self.assertEqual(func.called, self.expect)
コード例 #42
0
ファイル: test_n_step_gru.py プロジェクト: asi1024/chainer
 def check_call_cudnn_forward_inference(self, use_cudnn):
     with chainer.using_config('use_cudnn', use_cudnn):
         expect = chainer.should_use_cudnn('>=auto', 5000)
         with testing.patch('cupy.cudnn.rnn_forward_inference') as func:
             self.call_forward(False)
         assert func.called == expect