提交 c36a5ad8 authored 作者: affanv14's avatar affanv14

make grouped convolution cudnn test inherit from abstractconv base test

上级 54663e8c
...@@ -23,9 +23,9 @@ from ..type import gpuarray_shared_constructor ...@@ -23,9 +23,9 @@ from ..type import gpuarray_shared_constructor
from .config import mode_with_gpu, mode_without_gpu, test_ctx_name, ref_cast from .config import mode_with_gpu, mode_without_gpu, test_ctx_name, ref_cast
from . import test_nnet from . import test_nnet
from .rnn_support import Model, GRU, LSTM, WrapperLayer from .rnn_support import Model, GRU, LSTM, WrapperLayer
import unittest
from theano.configdefaults import SUPPORTED_DNN_CONV_ALGO_FWD from theano.configdefaults import SUPPORTED_DNN_CONV_ALGO_FWD
from theano.tensor.nnet.tests.test_abstract_conv import Grouped_conv_noOptim
mode_with_gpu = mode_with_gpu.including() mode_with_gpu = mode_with_gpu.including()
# Globally disabled for mode_without_gpu # Globally disabled for mode_without_gpu
...@@ -2134,134 +2134,32 @@ def test_dnn_rnn_lstm_grad_c(): ...@@ -2134,134 +2134,32 @@ def test_dnn_rnn_lstm_grad_c():
utt.assert_allclose(ref_grads_layer[j], g) utt.assert_allclose(ref_grads_layer[j], g)
class Cudnn_grouped_conv(unittest.TestCase): def dconv2d(border_mode, subsample, filter_dilation, num_groups):
def setUp(self): def dconv(img, kern):
self.num_groups = [3, 2, 4, 4] return dnn.dnn_conv(img, kern, border_mode=border_mode, subsample=subsample, dilation=filter_dilation,
self.border_mode = 'valid' conv_mode='conv', direction_hint='forward', workmem=None,
self.subsample = (1, 1) algo=None, precision=None, num_groups=num_groups)
self.img_shape = [(5, 6, 5, 5), (4, 4, 7, 5), (3, 8, 5, 3), (2, 4, 7, 7)] return dconv
self.kern_shape = [(6, 2, 3, 3), (6, 2, 5, 3), (4, 2, 3, 3), (4, 1, 3, 5)]
self.top_shape = [(5, 6, 3, 3), (4, 6, 3, 3), (3, 4, 3, 1), (2, 4, 5, 3)]
self.filter_dilation = (1, 1) def dconv2dw(border_mode, subsample, filter_dilation, num_groups):
self.mode = mode_with_gpu def dconvw(img, topgrad, kshp):
self.ref_mode = 'FAST_RUN' return dnn.dnn_gradweight(img, topgrad, kshp, border_mode=border_mode, subsample=subsample, dilation=filter_dilation,
conv_mode='conv', precision=None, algo=None, num_groups=num_groups)
def test_fwd(self): return dconvw
img_sym = theano.tensor.tensor4('img')
kern_sym = theano.tensor.tensor4('kern')
for imshp, kshp, tshp, groups in zip(self.img_shape, self.kern_shape, self.top_shape, self.num_groups): def dconv2di(border_mode, subsample, filter_dilation, num_groups):
img = np.random.random(imshp).astype(theano.config.floatX) def dconvi(kern, topgrad, imshp):
kern = np.random.random(kshp).astype(theano.config.floatX) return dnn.dnn_gradinput(kern, topgrad, imshp, border_mode=border_mode, subsample=subsample, dilation=filter_dilation,
top = np.random.random(tshp).astype(theano.config.floatX) conv_mode='conv', precision=None, algo=None, num_groups=num_groups)
return dconvi
split_imgs = np.split(img, groups, axis=1)
split_kern = np.split(kern, groups, axis=0)
class Cudnn_grouped_conv(Grouped_conv_noOptim):
grouped_conv_op = dnn.dnn_conv(img_sym, mode = mode_with_gpu
kern_sym, conv2d = staticmethod(dconv2d)
border_mode=self.border_mode, conv2d_gradw = staticmethod(dconv2dw)
subsample=self.subsample, conv2d_gradi = staticmethod(dconv2di)
dilation=self.filter_dilation, flip_filter = False
num_groups=groups) is_dnn = True
grouped_func = theano.function([img_sym, kern_sym], grouped_conv_op, mode=self.mode)
grouped_output = grouped_func(img, kern)
ref_conv_op = theano.tensor.nnet.corr.CorrMM(border_mode=self.border_mode,
subsample=self.subsample,
filter_dilation=self.filter_dilation)(img_sym, kern_sym[:, :, ::-1, ::-1])
ref_func = theano.function([img_sym, kern_sym], ref_conv_op,
mode=self.ref_mode)
ref_concat_output = [ref_func(img_arr, kern_arr)
for img_arr, kern_arr in zip(split_imgs, split_kern)]
ref_concat_output = np.concatenate(ref_concat_output, axis=1)
utt.assert_allclose(grouped_output, ref_concat_output)
def dconv(img, kern, out):
desc = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(1, 1), dilation=(1, 1),
conv_mode='conv', precision=set_precision(theano.config.floatX))(kern.shape)
return dnn.GpuDnnConv(num_groups=groups)(img, kern, out, desc, alpha=0.5, beta=0.75)
utt.verify_grad(dconv, [img, kern, top], eps=1e-3, mode=mode_with_gpu)
def test_gradweights(self):
img_sym = theano.tensor.tensor4('img')
top_sym = theano.tensor.tensor4('top')
for imshp, kshp, tshp, groups in zip(self.img_shape, self.kern_shape, self.top_shape, self.num_groups):
img = np.random.random(imshp).astype(theano.config.floatX)
kern = np.random.random(kshp).astype(theano.config.floatX)
top = np.random.random(tshp).astype(theano.config.floatX)
split_imgs = np.split(img, groups, axis=1)
split_top = np.split(top, groups, axis=1)
grouped_convgrad_op = dnn.dnn_gradweight(img_sym,
top_sym,
kshp,
border_mode=self.border_mode,
subsample=self.subsample,
dilation=self.filter_dilation,
num_groups=groups)
grouped_func = theano.function([img_sym, top_sym], grouped_convgrad_op, mode=self.mode)
grouped_output = grouped_func(img, top)
ref_conv_op = theano.tensor.nnet.corr.CorrMM_gradWeights(border_mode=self.border_mode,
subsample=self.subsample,
filter_dilation=self.filter_dilation)(img_sym, top_sym, kshp[2:])
ref_conv_op = ref_conv_op[:, :, ::-1, ::-1]
ref_func = theano.function([img_sym, top_sym], ref_conv_op,
mode=self.ref_mode)
ref_concat_output = [ref_func(img_arr, top_arr)
for img_arr, top_arr in zip(split_imgs, split_top)]
ref_concat_output = np.concatenate(ref_concat_output, axis=0)
utt.assert_allclose(grouped_output, ref_concat_output)
def dconvw(img, kern, out):
desc = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(1, 1), dilation=(1, 1),
conv_mode='conv', precision=set_precision(theano.config.floatX))(kern.shape)
return dnn.GpuDnnConvGradW(num_groups=groups)(img, out, kern, desc, alpha=0.75,
beta=-1.0)
utt.verify_grad(dconvw, [img, kern, top], eps=1e-3, mode=mode_with_gpu)
def test_gradinputs(self):
kern_sym = theano.tensor.tensor4('kern')
top_sym = theano.tensor.tensor4('top')
for imshp, kshp, tshp, groups in zip(self.img_shape, self.kern_shape, self.top_shape, self.num_groups):
img = np.random.random(imshp).astype(theano.config.floatX)
kern = np.random.random(kshp).astype(theano.config.floatX)
top = np.random.random(tshp).astype(theano.config.floatX)
split_kerns = np.split(kern, groups, axis=0)
split_top = np.split(top, groups, axis=1)
grouped_convgrad_op = dnn.dnn_gradinput(kern_sym,
top_sym,
imshp,
border_mode=self.border_mode,
subsample=self.subsample,
dilation=self.filter_dilation,
num_groups=groups)
grouped_func = theano.function([kern_sym, top_sym], grouped_convgrad_op, mode=self.mode)
grouped_output = grouped_func(kern, top)
ref_conv_op = theano.tensor.nnet.corr.CorrMM_gradInputs(border_mode=self.border_mode,
subsample=self.subsample,
filter_dilation=self.filter_dilation)(kern_sym[:, :, ::-1, ::-1], top_sym, imshp[2:])
ref_func = theano.function([kern_sym, top_sym], ref_conv_op,
mode=self.ref_mode)
ref_concat_output = [ref_func(kern_arr, top_arr)
for kern_arr, top_arr in zip(split_kerns, split_top)]
ref_concat_output = np.concatenate(ref_concat_output, axis=1)
utt.assert_allclose(grouped_output, ref_concat_output)
def dconvi(img, kern, out):
desc = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(1, 1), dilation=(1, 1),
conv_mode='conv', precision=set_precision(theano.config.floatX))(kern.shape)
return dnn.GpuDnnConvGradI(num_groups=groups)(kern, out, img, desc, alpha=-1.0,
beta=0.0)
utt.verify_grad(dconvi, [img, kern, top], eps=1e-3, mode=mode_with_gpu)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论