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

Merge pull request #5245 from lamblin/fix_subtensor_setsubtensor

Add tests for local_subtensor_inc_subtensor
...@@ -1882,76 +1882,122 @@ def test_local_subtensor_remove_broadcastable_index(): ...@@ -1882,76 +1882,122 @@ def test_local_subtensor_remove_broadcastable_index():
f2(xn) f2(xn)
def test_subtensor_inc_subtensor(): class Test_subtensor_inc_subtensor(unittest.TestCase):
# basic test @classmethod
x = tensor.matrix('x') def setUpClass(cls):
i = tensor.iscalar('i') cls.mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor')
v = tensor.vector('v')
y = tensor.set_subtensor(x[i], v) def test_basic(self):
z = y[i] # basic test
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') x = tensor.matrix('x')
f = theano.function([x, i, v], z, mode=mode) i = tensor.iscalar('i')
prog = f.maker.fgraph.toposort() v = tensor.vector('v')
assert len(prog) == 1 y = tensor.set_subtensor(x[i], v)
assert isinstance(prog[0].op, DeepCopyOp) z = y[i]
# basic test, numerical check f = theano.function([x, i, v], z, mode=self.mode)
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX) prog = f.maker.fgraph.toposort()
v_ = numpy.random.uniform(size=[4, ]).astype(config.floatX) assert len(prog) == 1
i_ = 1 assert isinstance(prog[0].op, DeepCopyOp)
assert numpy.array_equal(f(x_, i_, v_), v_) # basic test, numerical check
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX)
# complicated test v_ = numpy.random.uniform(size=[4, ]).astype(config.floatX)
x = tensor.tensor4('x') i_ = 1
i1 = tensor.iscalar('i1') assert numpy.array_equal(f(x_, i_, v_), v_)
i2 = tensor.iscalar('i2')
i3 = tensor.iscalar('i3') def test_multiple_idx(self):
i4 = tensor.iscalar('i4') # complicated test
v = tensor.tensor3('v') x = tensor.tensor4('x')
y = tensor.set_subtensor(x[i1, :i2, i3:, ::i4], v) i1 = tensor.iscalar('i1')
z = y[i1, :i2, i3:, ::i4] i2 = tensor.iscalar('i2')
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') i3 = tensor.iscalar('i3')
f = theano.function([x, i1, i2, i3, i4, v], z, mode=mode) i4 = tensor.iscalar('i4')
prog = f.maker.fgraph.toposort() v = tensor.tensor3('v')
assert len(prog) == 1 y = tensor.set_subtensor(x[i1, :i2, i3:, ::i4], v)
assert isinstance(prog[0].op, DeepCopyOp) z = y[i1, :i2, i3:, ::i4]
# complicated test, numerical check f = theano.function([x, i1, i2, i3, i4, v], z, mode=self.mode)
x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX) prog = f.maker.fgraph.toposort()
v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX) assert len(prog) == 1
i1_, i2_, i3_, i4_ = 1, 2, 3, 4 assert isinstance(prog[0].op, DeepCopyOp)
assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), v_) # complicated test, numerical check
x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX)
# case not use this optimization v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX)
z = y[i1, :i3, i2:, ::i4] i1_, i2_, i3_, i4_ = 1, 2, 3, 4
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), v_)
f = theano.function([x, i1, i2, i3, i4, v], z, mode=mode)
prog = f.maker.fgraph.toposort() def test_not_applied(self):
assert len(prog) != 1 # case not use this optimization
assert any(isinstance(x.op, tensor.IncSubtensor) for x in prog) x = tensor.tensor4('x')
assert any(isinstance(x.op, tensor.Subtensor) for x in prog) i1 = tensor.iscalar('i1')
# case not use this optimization, numerical check i2 = tensor.iscalar('i2')
x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX) i3 = tensor.iscalar('i3')
v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX) i4 = tensor.iscalar('i4')
i1_, i2_, i3_, i4_ = 1, 2, 3, 4 v = tensor.tensor3('v')
x_[i1_, :i2_, i3_:, ::i4_] = v_ y = tensor.set_subtensor(x[i1, :i2, i3:, ::i4], v)
assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), x_[i1_, :i3_, i2_:, ::i4_]) z = y[i1, :i3, i2:, ::i4]
f = theano.function([x, i1, i2, i3, i4, v], z, mode=self.mode)
# case when v is broadcastable prog = f.maker.fgraph.toposort()
x = tensor.matrix('x') assert len(prog) != 1
i1 = tensor.iscalar('i') assert any(isinstance(x.op, tensor.IncSubtensor) for x in prog)
i2 = tensor.iscalar('i') assert any(isinstance(x.op, tensor.Subtensor) for x in prog)
v = tensor.vector('v') # case not use this optimization, numerical check
y = tensor.set_subtensor(x[:i1, :i2], v) x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX)
z = y[:i1, :i2] v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX)
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') i1_, i2_, i3_, i4_ = 1, 2, 3, 4
f = theano.function([x, i1, i2, v], z, mode=mode) x_[i1_, :i2_, i3_:, ::i4_] = v_
prog = f.maker.fgraph.toposort() assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), x_[i1_, :i3_, i2_:, ::i4_])
assert any(isinstance(x.op, tensor.Alloc) for x in prog)
# case when v is broadcastable, numerical check def test_fewer_dims(self):
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX) # case when v has fewer dimensions
v_ = numpy.random.uniform(size=[2, ]).astype(config.floatX) x = tensor.matrix('x')
i1_, i2_ = 2, 2 i1 = tensor.iscalar('i')
x_[:i1_, :i2_] = v_ i2 = tensor.iscalar('i')
assert numpy.array_equal(f(x_, i1_, i2_, v_), x_[:i1_, :i2_]) v = tensor.vector('v')
y = tensor.set_subtensor(x[:i1, :i2], v)
z = y[:i1, :i2]
f = theano.function([x, i1, i2, v], z, mode=self.mode)
prog = f.maker.fgraph.toposort()
assert any(isinstance(x.op, tensor.Alloc) for x in prog)
# case when v is broadcastable, numerical check
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX)
v_ = numpy.random.uniform(size=[2, ]).astype(config.floatX)
i1_, i2_ = 2, 2
x_[:i1_, :i2_] = v_
assert numpy.array_equal(f(x_, i1_, i2_, v_), x_[:i1_, :i2_])
def test_broadcasted(self):
# case when v has the same number of dimensions, some broadcastable
x = tensor.matrix('x')
i1 = tensor.iscalar('i')
i2 = tensor.iscalar('i')
v = tensor.col('v')
y = tensor.set_subtensor(x[:i1, :i2], v)
z = y[:i1, :i2]
f = theano.function([x, i1, i2, v], z, mode=self.mode)
prog = f.maker.fgraph.toposort()
assert any(isinstance(x.op, tensor.Alloc) for x in prog)
# case when v is broadcastable, numerical check
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX)
v_ = numpy.random.uniform(size=[2, 1]).astype(config.floatX)
i1_, i2_ = 2, 2
x_[:i1_, :i2_] = v_
assert numpy.array_equal(f(x_, i1_, i2_, v_), x_[:i1_, :i2_])
def test_different_dtypes(self):
# Case when the dtype differs
x = tensor.bmatrix('x')
i = tensor.iscalar('i')
v = tensor.vector('v')
y = tensor.set_subtensor(x[i], v)
z = y[i]
f = theano.function([x, i, v], z, mode=self.mode)
prog = f.maker.fgraph.toposort()
assert len(prog) == 1
assert prog[0].op == tensor.basic._convert_to_int8
# basic test, numerical check
x_ = numpy.random.randint(12, size=[3, 4]).astype('int8')
v_ = numpy.random.uniform(12, size=[4, ]).astype(config.floatX)
i_ = 1
assert numpy.array_equal(f(x_, i_, v_), v_.astype('int8'))
class test_local_subtensor_make_vector(unittest.TestCase): class test_local_subtensor_make_vector(unittest.TestCase):
...@@ -6763,15 +6809,3 @@ def test_local_log_sum_exp3(): ...@@ -6763,15 +6809,3 @@ def test_local_log_sum_exp3():
optimised_ret = f(x_val) optimised_ret = f(x_val)
assert numpy.allclose(optimised_ret, 100.) assert numpy.allclose(optimised_ret, 100.)
if __name__ == '__main__':
t = TestMakeVector('setUp')
t.setUp()
# t.test_perform()
t.test_infer_shape()
test_subtensor_inc_subtensor()
"""
# unittest.main()
test_fusion().tes_memory_leak()
"""
...@@ -46,7 +46,6 @@ whitelist_flake8 = [ ...@@ -46,7 +46,6 @@ whitelist_flake8 = [
"typed_list/tests/__init__.py", "typed_list/tests/__init__.py",
"tensor/__init__.py", "tensor/__init__.py",
"tensor/tests/__init__.py", "tensor/tests/__init__.py",
"tensor/tests/test_subtensor.py",
"tensor/tests/test_utils.py", "tensor/tests/test_utils.py",
"tensor/tests/test_nlinalg.py", "tensor/tests/test_nlinalg.py",
"tensor/tests/test_shared_randomstreams.py", "tensor/tests/test_shared_randomstreams.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论