提交 ce92345f authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #6105 from nouiz/tests

Speed up some tests
...@@ -349,8 +349,6 @@ def test_binomial(): ...@@ -349,8 +349,6 @@ def test_binomial():
(sample_size, sample_size, [], []), (sample_size, sample_size, [], []),
(x.shape, sample_size, [x], (x.shape, sample_size, [x],
[np.zeros(sample_size, dtype=config.floatX)]), [np.zeros(sample_size, dtype=config.floatX)]),
((x.shape[0], sample_size[1]), sample_size, [x],
[np.zeros(sample_size, dtype=config.floatX)]),
# test empty size (scalar) # test empty size (scalar)
((), (), [], []), ((), (), [], []),
]: ]:
...@@ -402,12 +400,6 @@ def test_normal0(): ...@@ -402,12 +400,6 @@ def test_normal0():
(x.shape, sample_size, [x], (x.shape, sample_size, [x],
[np.zeros(sample_size, dtype=config.floatX)], [np.zeros(sample_size, dtype=config.floatX)],
-5., default_rtol, default_rtol), -5., default_rtol, default_rtol),
((x.shape[0], sample_size[1]), sample_size, [x],
[np.zeros(sample_size, dtype=config.floatX)],
-5., default_rtol, default_rtol),
# test odd value
(sample_size_odd, sample_size_odd, [], [], -5.,
default_rtol, default_rtol),
# test odd value # test odd value
(x.shape, sample_size_odd, [x], (x.shape, sample_size_odd, [x],
[np.zeros(sample_size_odd, dtype=config.floatX)], [np.zeros(sample_size_odd, dtype=config.floatX)],
...@@ -420,7 +412,6 @@ def test_normal0(): ...@@ -420,7 +412,6 @@ def test_normal0():
((), (), [], [], -5., default_rtol, 0.02), ((), (), [], [], -5., default_rtol, 0.02),
# test with few samples at the same time # test with few samples at the same time
((1,), (1,), [], [], -5., default_rtol, 0.02), ((1,), (1,), [], [], -5., default_rtol, 0.02),
((2,), (2,), [], [], -5., default_rtol, 0.02),
((3,), (3,), [], [], -5., default_rtol, 0.02), ((3,), (3,), [], [], -5., default_rtol, 0.02),
]: ]:
......
...@@ -6,9 +6,10 @@ import theano ...@@ -6,9 +6,10 @@ import theano
from theano.tensor import basic from theano.tensor import basic
from theano.tensor import nlinalg # noqa from theano.tensor import nlinalg # noqa
from theano import gof, scalar from theano import gof, scalar
from theano.gof import Generic from theano.gof import Generic, ParamsType, EnumList
from theano import gradient from theano import gradient
from theano.gradient import DisconnectedType, disconnected_type from theano.gradient import DisconnectedType, disconnected_type
from theano.scalar import int32 as int_t
tensor = basic tensor = basic
...@@ -20,6 +21,7 @@ class CpuContiguous(theano.Op): ...@@ -20,6 +21,7 @@ class CpuContiguous(theano.Op):
__props__ = () __props__ = ()
view_map = {0: [0]} view_map = {0: [0]}
check_input = False
def make_node(self, x): def make_node(self, x):
x_ = theano.tensor.as_tensor_variable(x) x_ = theano.tensor.as_tensor_variable(x)
...@@ -81,6 +83,7 @@ class SearchsortedOp(theano.Op): ...@@ -81,6 +83,7 @@ class SearchsortedOp(theano.Op):
params_type = Generic() params_type = Generic()
__props__ = ("side", ) __props__ = ("side", )
check_input = False
def __init__(self, side='left'): def __init__(self, side='left'):
if side == 'left' or side == 'right': if side == 'left' or side == 'right':
...@@ -245,6 +248,10 @@ class CumOp(theano.Op): ...@@ -245,6 +248,10 @@ class CumOp(theano.Op):
# See function cumsum/cumprod for docstring # See function cumsum/cumprod for docstring
__props__ = ("axis", "mode") __props__ = ("axis", "mode")
check_input = False
params_type = ParamsType(c_axis=int_t,
mode=EnumList(('MODE_ADD', 'add'),
('MODE_MUL', 'mul')))
def __init__(self, axis=None, mode='add'): def __init__(self, axis=None, mode='add'):
if mode not in ('add', 'mul'): if mode not in ('add', 'mul'):
...@@ -252,6 +259,8 @@ class CumOp(theano.Op): ...@@ -252,6 +259,8 @@ class CumOp(theano.Op):
self.axis = axis self.axis = axis
self.mode = mode self.mode = mode
c_axis = property(lambda self: np.MAXDIMS if self.axis is None else self.axis)
def make_node(self, x): def make_node(self, x):
x = basic.as_tensor_variable(x) x = basic.as_tensor_variable(x)
out_type = x.type() out_type = x.type()
...@@ -263,7 +272,7 @@ class CumOp(theano.Op): ...@@ -263,7 +272,7 @@ class CumOp(theano.Op):
return theano.Apply(self, [x], [out_type]) return theano.Apply(self, [x], [out_type])
def perform(self, node, inputs, output_storage): def perform(self, node, inputs, output_storage, params):
x = inputs[0] x = inputs[0]
z = output_storage[0] z = output_storage[0]
z[0] = {'add': np.cumsum, 'mul': np.cumprod}[self.mode](x, axis=self.axis) z[0] = {'add': np.cumsum, 'mul': np.cumprod}[self.mode](x, axis=self.axis)
...@@ -311,49 +320,43 @@ class CumOp(theano.Op): ...@@ -311,49 +320,43 @@ class CumOp(theano.Op):
z, = onames z, = onames
axis = self.axis axis = self.axis
fail = sub['fail'] fail = sub['fail']
func = dict(mul='CumProd', add='CumSum')[self.mode] params = sub['params']
if self.axis is None or (self.axis == 0 and node.inputs[0].ndim == 1):
code = """ code = """
int axis = %(params)s->c_axis;
if (axis == 0 && PyArray_NDIM(%(x)s) == 1)
axis = NPY_MAXDIMS;
npy_intp shape[1] = { PyArray_SIZE(%(x)s) }; npy_intp shape[1] = { PyArray_SIZE(%(x)s) };
if(!(%(z)s && PyArray_DIMS(%(z)s)[0] == shape[0])) if(axis == NPY_MAXDIMS && !(%(z)s && PyArray_DIMS(%(z)s)[0] == shape[0]))
{ {
Py_XDECREF(%(z)s); Py_XDECREF(%(z)s);
%(z)s = (PyArrayObject*) PyArray_SimpleNew(1, shape, PyArray_TYPE((PyArrayObject*) py_%(x)s)); %(z)s = (PyArrayObject*) PyArray_SimpleNew(1, shape, PyArray_TYPE((PyArrayObject*) py_%(x)s));
} }
if (!%(z)s) else if(axis != NPY_MAXDIMS && !(%(z)s && PyArray_CompareLists(PyArray_DIMS(%(z)s), PyArray_DIMS(%(x)s), PyArray_NDIM(%(x)s))))
%(fail)s;
{
PyObject * t = PyArray_%(func)s(
%(x)s, NPY_MAXDIMS,
PyArray_TYPE((PyArrayObject*) py_%(x)s), %(z)s);
if (!t){
%(fail)s;
}
// Because PyArray_%(func)s returns a newly created reference on t.
Py_XDECREF(t);
}
""" % locals()
else:
code = """
if(!(%(z)s && PyArray_CompareLists(PyArray_DIMS(%(z)s), PyArray_DIMS(%(x)s), PyArray_NDIM(%(x)s))))
{ {
Py_XDECREF(%(z)s); Py_XDECREF(%(z)s);
%(z)s = (PyArrayObject*) PyArray_SimpleNew(PyArray_NDIM(%(x)s), PyArray_DIMS(%(x)s), PyArray_TYPE((PyArrayObject*) py_%(x)s)); %(z)s = (PyArrayObject*) PyArray_SimpleNew(PyArray_NDIM(%(x)s), PyArray_DIMS(%(x)s), PyArray_TYPE(%(x)s));
} }
if (!%(z)s) if (!%(z)s)
%(fail)s; %(fail)s;
{ {
PyObject * t = PyArray_%(func)s( PyObject * t = NULL;
%(x)s, %(axis)s, if(%(params)s->mode == MODE_ADD)
PyArray_TYPE((PyArrayObject*) py_%(x)s), %(z)s); t = PyArray_CumSum(
%(x)s, axis,
PyArray_TYPE(%(x)s), %(z)s);
else if(%(params)s->mode == MODE_MUL)
t = PyArray_CumProd(
%(x)s, axis,
PyArray_TYPE(%(x)s), %(z)s);
if (!t){ if (!t){
%(fail)s; %(fail)s;
} }
// Because PyArray_%(func)s returns a newly created reference on t. // Because PyArray_CumSum/CumProd returns a newly created reference on t.
Py_XDECREF(t); Py_XDECREF(t);
} }
""" % locals() """ % locals()
...@@ -361,7 +364,7 @@ class CumOp(theano.Op): ...@@ -361,7 +364,7 @@ class CumOp(theano.Op):
return code return code
def c_code_cache_version(self): def c_code_cache_version(self):
return (7,) return (8,)
def __str__(self): def __str__(self):
return "%s{%s, %s}" % (self.__class__.__name__, self.axis, self.mode) return "%s{%s, %s}" % (self.__class__.__name__, self.axis, self.mode)
......
...@@ -319,8 +319,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -319,8 +319,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
((3, 3), (4, 2, 16, 16)), ((3, 3), (4, 2, 16, 16)),
((3, 2), (4, 2, 16, 16)), ((3, 2), (4, 2, 16, 16)),
((3, 2, 2), (3, 2, 16, 16, 16)), ((3, 2, 2), (3, 2, 16, 16, 16)),
((2, 3, 2), (3, 2, 16, 16, 16)),
((2, 2, 3), (3, 2, 16, 16, 16)),
((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)),
) )
...@@ -371,10 +369,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -371,10 +369,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
# maxpool, stride, ignore_border, input, output sizes # maxpool, stride, ignore_border, input, output sizes
examples = ( examples = (
((1, 1), (1, 1), True, (4, 10, 16, 16), (4, 10, 16, 16)), ((1, 1), (1, 1), True, (4, 10, 16, 16), (4, 10, 16, 16)),
((1, 1), (3, 3), True, (4, 10, 16, 16), (4, 10, 6, 6)),
((1, 1), (5, 7), True, (4, 10, 16, 16), (4, 10, 4, 3)), ((1, 1), (5, 7), True, (4, 10, 16, 16), (4, 10, 4, 3)),
((1, 1), (1, 1), False, (4, 10, 16, 16), (4, 10, 16, 16)), ((1, 1), (1, 1), False, (4, 10, 16, 16), (4, 10, 16, 16)),
((1, 1), (3, 3), False, (4, 10, 16, 16), (4, 10, 6, 6)),
((1, 1), (5, 7), False, (4, 10, 16, 16), (4, 10, 4, 3)), ((1, 1), (5, 7), False, (4, 10, 16, 16), (4, 10, 4, 3)),
((3, 3), (1, 1), True, (4, 10, 16, 16), (4, 10, 14, 14)), ((3, 3), (1, 1), True, (4, 10, 16, 16), (4, 10, 14, 14)),
((3, 3), (3, 3), True, (4, 10, 16, 16), (4, 10, 5, 5)), ((3, 3), (3, 3), True, (4, 10, 16, 16), (4, 10, 5, 5)),
...@@ -389,10 +385,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -389,10 +385,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
((5, 3), (3, 3), False, (4, 10, 16, 16), (4, 10, 5, 6)), ((5, 3), (3, 3), False, (4, 10, 16, 16), (4, 10, 5, 6)),
((5, 3), (5, 7), False, (4, 10, 16, 16), (4, 10, 4, 3)), ((5, 3), (5, 7), False, (4, 10, 16, 16), (4, 10, 4, 3)),
((16, 16), (1, 1), True, (4, 10, 16, 16), (4, 10, 1, 1)), ((16, 16), (1, 1), True, (4, 10, 16, 16), (4, 10, 1, 1)),
((16, 16), (3, 3), True, (4, 10, 16, 16), (4, 10, 1, 1)),
((16, 16), (5, 7), True, (4, 10, 16, 16), (4, 10, 1, 1)), ((16, 16), (5, 7), True, (4, 10, 16, 16), (4, 10, 1, 1)),
((16, 16), (1, 1), False, (4, 10, 16, 16), (4, 10, 1, 1)), ((16, 16), (1, 1), False, (4, 10, 16, 16), (4, 10, 1, 1)),
((16, 16), (3, 3), False, (4, 10, 16, 16), (4, 10, 1, 1)),
((16, 16), (5, 7), False, (4, 10, 16, 16), (4, 10, 1, 1)), ((16, 16), (5, 7), False, (4, 10, 16, 16), (4, 10, 1, 1)),
((3,), (5,), True, (16,), (3,)), ((3,), (5,), True, (16,), (3,)),
((3,), (5,), True, (2, 16,), (2, 3,)), ((3,), (5,), True, (2, 16,), (2, 3,)),
...@@ -473,7 +467,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -473,7 +467,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
examples = ( examples = (
((3,), (2,), (2,), (5,)), ((3,), (2,), (2,), (5,)),
((3,), (2,), (2,), (4, 5)), ((3,), (2,), (2,), (4, 5)),
((3,), (2,), (2,), (4, 2, 5)),
((3,), (2,), (2,), (4, 2, 5, 5)), ((3,), (2,), (2,), (4, 2, 5, 5)),
((3, 3), (2, 2), (2, 2), (4, 2, 5, 5)), ((3, 3), (2, 2), (2, 2), (4, 2, 5, 5)),
((4, 4), (2, 2), (1, 2), (4, 2, 5, 5)), ((4, 4), (2, 2), (1, 2), (4, 2, 5, 5)),
...@@ -541,7 +534,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -541,7 +534,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
((2, 3), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)),
((1, 1, 1), (2, 3, 3)), ((1, 1, 1), (2, 3, 3)),
((3, 2, 2), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)),
((2, 3, 2), (2, 3, 3, 4, 4)),
((2, 2, 3), (2, 3, 3, 4, 4)), ((2, 2, 3), (2, 3, 3, 4, 4)),
) )
...@@ -657,11 +649,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -657,11 +649,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
((1, 1), (2, 3, 3, 4)), ((1, 1), (2, 3, 3, 4)),
((3, 2), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)),
((2, 3), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)),
((1, 1, 1), (2, 3, 3, 4)),
((3, 2, 2), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)),
((2, 3, 2), (2, 3, 3, 4)),
((2, 2, 3), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)),
((2, 2, 3), (2, 1, 3, 3, 4)),
) )
for (avgpoolshp, inputsize) in examples: for (avgpoolshp, inputsize) in examples:
...@@ -751,11 +740,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -751,11 +740,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
((3,), (2,), (2,), (2, 10,)), ((3,), (2,), (2,), (2, 10,)),
((3,), (2,), (2,), (2, 1, 10,)), ((3,), (2,), (2,), (2, 1, 10,)),
((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)),
((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)),
((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)),
((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)),
((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)),
((3, 5, 3), (2, 3, 2), (2, 1, 2), (1, 1, 5, 10, 5)),
((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)),
) )
...@@ -788,12 +774,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -788,12 +774,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
((3,), (2,), (2,), (2, 10,)), ((3,), (2,), (2,), (2, 10,)),
((3,), (2,), (2,), (2, 1, 10,)), ((3,), (2,), (2,), (2, 1, 10,)),
((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)),
((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)),
((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)),
((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)), ((5, 3, 2), (3, 2, 1), (2, 2, 2), (1, 1, 10, 5, 5)),
((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)),
((3, 5, 3), (2, 3, 2), (2, 1, 2), (1, 1, 5, 10, 5)),
((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)),
) )
for (avgpoolshp, stridesize, padsize, inputsize) in examples: for (avgpoolshp, stridesize, padsize, inputsize) in examples:
...@@ -841,11 +823,9 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -841,11 +823,9 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
((3,), (2,), (2,), (2, 10,)), ((3,), (2,), (2,), (2, 10,)),
((3,), (2,), (2,), (2, 1, 10,)), ((3,), (2,), (2,), (2, 1, 10,)),
((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)),
((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)),
((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)),
((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)), ((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)),
((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)),
((3, 5, 3), (2, 3, 2), (2, 1, 2), (1, 1, 5, 10, 5)),
((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)),
) )
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论