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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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
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)
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
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)
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')
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
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)
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)
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)
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)
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)
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)
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)
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
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
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
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
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)
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)
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)
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
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
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)
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
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)
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