提交 a2f6c1c9 authored 作者: erakra's avatar erakra

fixing flake8 for test_blas.py

上级 c5cd87fa
...@@ -81,7 +81,7 @@ class t_gemm(TestCase): ...@@ -81,7 +81,7 @@ class t_gemm(TestCase):
f = inplace_func([tz, ta, tx, ty, tb], f = inplace_func([tz, ta, tx, ty, tb],
gemm_inplace(tz, ta, tx, ty, tb), gemm_inplace(tz, ta, tx, ty, tb),
mode=compile.Mode(optimizer=None, linker=l)) mode=compile.Mode(optimizer=None, linker=l))
new_z = f(z, a, x, y, b) f(z, a, x, y, b)
z_after = self._gemm(z_orig, a, x, y, b) z_after = self._gemm(z_orig, a, x, y, b)
# print z_orig, z_after, z, type(z_orig), type(z_after), type(z) # print z_orig, z_after, z, type(z_orig), type(z_after), type(z)
...@@ -96,8 +96,8 @@ class t_gemm(TestCase): ...@@ -96,8 +96,8 @@ class t_gemm(TestCase):
cmp_linker(copy(z), a, x, y, b, 'c|py') cmp_linker(copy(z), a, x, y, b, 'c|py')
cmp_linker(copy(z), a, x, y, b, 'py') cmp_linker(copy(z), a, x, y, b, 'py')
if (not dtype.startswith("complex") if (not dtype.startswith("complex") and
and theano.config.cxx): theano.config.cxx):
# If theano.config.blas.ldflags is empty, Theano will use # If theano.config.blas.ldflags is empty, Theano will use
# a NumPy C implementation of [sd]gemm_. # a NumPy C implementation of [sd]gemm_.
cmp_linker(copy(z), a, x, y, b, 'c') cmp_linker(copy(z), a, x, y, b, 'c')
...@@ -105,7 +105,7 @@ class t_gemm(TestCase): ...@@ -105,7 +105,7 @@ class t_gemm(TestCase):
def test0a(self): def test0a(self):
Gemm.debug = True Gemm.debug = True
try: try:
g = gemm_no_inplace([1.], 1., [1.], [1.], 1.) gemm_no_inplace([1.], 1., [1.], [1.], 1.)
except TypeError as e: except TypeError as e:
if exc_message(e) is Gemm.E_rank: if exc_message(e) is Gemm.E_rank:
return return
...@@ -171,7 +171,6 @@ class t_gemm(TestCase): ...@@ -171,7 +171,6 @@ class t_gemm(TestCase):
def test_factorised_scalar(self): def test_factorised_scalar(self):
a = T.matrix() a = T.matrix()
b = T.matrix() b = T.matrix()
c = T.matrix()
s = theano.shared(np.zeros((5, 5)).astype(config.floatX)) s = theano.shared(np.zeros((5, 5)).astype(config.floatX))
lr1 = T.constant(0.01).astype(config.floatX) lr1 = T.constant(0.01).astype(config.floatX)
...@@ -182,7 +181,7 @@ class t_gemm(TestCase): ...@@ -182,7 +181,7 @@ class t_gemm(TestCase):
f = theano.function([a, b], updates=[(s, lr1 * T.dot(a, b) + f = theano.function([a, b], updates=[(s, lr1 * T.dot(a, b) +
l2_reg * lr2 * s)], l2_reg * lr2 * s)],
mode=mode_not_fast_compile).maker.fgraph.toposort() mode=mode_not_fast_compile).maker.fgraph.toposort()
#[Gemm{inplace}(<TensorType(float64, matrix)>, 0.01, # [Gemm{inplace}(<TensorType(float64, matrix)>, 0.01,
# <TensorType(float64, matrix)>, <TensorType(float64, matrix)>, # <TensorType(float64, matrix)>, <TensorType(float64, matrix)>,
# 2e-06)] # 2e-06)]
assert len(f) == 1 assert len(f) == 1
...@@ -192,7 +191,7 @@ class t_gemm(TestCase): ...@@ -192,7 +191,7 @@ class t_gemm(TestCase):
f = theano.function([a, b], updates=[(s, lr1 * (T.dot(a, b) - f = theano.function([a, b], updates=[(s, lr1 * (T.dot(a, b) -
l2_reg * s))], l2_reg * s))],
mode=mode_not_fast_compile).maker.fgraph.toposort() mode=mode_not_fast_compile).maker.fgraph.toposort()
#[Gemm{inplace}(<TensorType(float64, matrix)>, 0.01, # [Gemm{inplace}(<TensorType(float64, matrix)>, 0.01,
# <TensorType(float64, matrix)>, <TensorType(float64, matrix)>, # <TensorType(float64, matrix)>, <TensorType(float64, matrix)>,
# -2e-06)] # -2e-06)]
assert len(f) == 1 assert len(f) == 1
...@@ -202,7 +201,7 @@ class t_gemm(TestCase): ...@@ -202,7 +201,7 @@ class t_gemm(TestCase):
f = theano.function([a, b], f = theano.function([a, b],
updates=[(s, s - lr1 * (s * .0002 + T.dot(a, b)))], updates=[(s, s - lr1 * (s * .0002 + T.dot(a, b)))],
mode=mode_not_fast_compile).maker.fgraph.toposort() mode=mode_not_fast_compile).maker.fgraph.toposort()
#[Gemm{inplace}(<TensorType(float64, matrix)>, -0.01, # [Gemm{inplace}(<TensorType(float64, matrix)>, -0.01,
# <TensorType(float64, matrix)>, <TensorType(float64, matrix)>, # <TensorType(float64, matrix)>, <TensorType(float64, matrix)>,
# 0.999998)] # 0.999998)]
assert len(f) == 1 assert len(f) == 1
...@@ -270,14 +269,14 @@ class t_gemm(TestCase): ...@@ -270,14 +269,14 @@ class t_gemm(TestCase):
def t(z, x, y, a=1.0, b=0.0, l='c|py', dt='float64'): def t(z, x, y, a=1.0, b=0.0, l='c|py', dt='float64'):
z, a, x, y, b = [theano._asarray(p, dtype=dt) z, a, x, y, b = [theano._asarray(p, dtype=dt)
for p in (z, a, x, y, b)] for p in (z, a, x, y, b)]
z_orig = z.copy() # z_orig = z.copy()
z_after = self._gemm(z, a, x, y, b) z_after = self._gemm(z, a, x, y, b)
tz, ta, tx, ty, tb = [shared(p) for p in (z, a, x, y, b)] tz, ta, tx, ty, tb = [shared(p) for p in (z, a, x, y, b)]
# f = inplace_func([tz,ta,tx,ty,tb], gemm_inplace(tz,ta,tx,ty,tb), # f = inplace_func([tz,ta,tx,ty,tb], gemm_inplace(tz,ta,tx,ty,tb),
# mode = compile.Mode(optimizer = None, linker=l)) # mode = compile.Mode(optimizer = None, linker=l))
#f(z, a, x, y, b) # f(z, a, x, y, b)
f = inplace_func([], gemm_inplace(tz, ta, tx, ty, tb), f = inplace_func([], gemm_inplace(tz, ta, tx, ty, tb),
mode=compile.Mode(optimizer=None, linker=l)) mode=compile.Mode(optimizer=None, linker=l))
f() f()
...@@ -355,8 +354,9 @@ class t_gemm(TestCase): ...@@ -355,8 +354,9 @@ class t_gemm(TestCase):
tz_i = gemm_no_inplace(tz[:, :, i], ta, tx[ tz_i = gemm_no_inplace(tz[:, :, i], ta, tx[
:, :, i], ty[:, :, i], tb) :, :, i], ty[:, :, i], tb)
g_i = theano.function([], tz_i, g_i = theano.function(
updates=[(tz, T.set_subtensor(tz[:, :, i], tz_i))], [], tz_i, updates=[(tz, T.set_subtensor(tz[:, :, i],
tz_i))],
mode=compile.Mode(optimizer=None, linker=l)) mode=compile.Mode(optimizer=None, linker=l))
for j in xrange(3): for j in xrange(3):
g_i() g_i()
...@@ -404,8 +404,8 @@ class t_as_scalar(TestCase): ...@@ -404,8 +404,8 @@ class t_as_scalar(TestCase):
def test1(self): def test1(self):
"""Test that it fails on nonscalar constants""" """Test that it fails on nonscalar constants"""
a = T.constant(np.ones(5)) a = T.constant(np.ones(5))
self.assertTrue(None == _as_scalar(a)) self.assertTrue(_as_scalar(a) is None)
self.assertTrue(None == _as_scalar(T.DimShuffle([False], [0, 'x'])(a))) self.assertTrue(_as_scalar(T.DimShuffle([False], [0, 'x'])(a)) is None)
def test2(self): def test2(self):
"""Test that it works on scalar variables""" """Test that it works on scalar variables"""
...@@ -420,9 +420,9 @@ class t_as_scalar(TestCase): ...@@ -420,9 +420,9 @@ class t_as_scalar(TestCase):
def test3(self): def test3(self):
"""Test that it fails on nonscalar variables""" """Test that it fails on nonscalar variables"""
a = T.matrix() a = T.matrix()
self.assertTrue(None == _as_scalar(a)) self.assertTrue(_as_scalar(a) is None)
self.assertTrue(None == _as_scalar(T.DimShuffle([False, False], self.assertTrue(_as_scalar(T.DimShuffle([False, False],
[0, 'x', 1])(a))) [0, 'x', 1])(a)) is None)
class T_real_matrix(TestCase): class T_real_matrix(TestCase):
...@@ -541,8 +541,8 @@ def test_gemm_opt_double_gemm(): ...@@ -541,8 +541,8 @@ def test_gemm_opt_double_gemm():
ishapes = [(4, 3), (3, 5), (4, 5), (), (), (5, 9), (9, 4), ()] ishapes = [(4, 3), (3, 5), (4, 5), (), (), (5, 9), (9, 4), ()]
i = [X, Y, Z, a, b, R, S, c] i = [X, Y, Z, a, b, R, S, c]
o = [(a * T.dot(X, Y) o = [(a * T.dot(X, Y) +
+ gemm_inplace(Z, b, S.T, R.T, T.constant(1.0).astype(config.floatX)))] gemm_inplace(Z, b, S.T, R.T, T.constant(1.0).astype(config.floatX)))]
try: try:
f = inplace_func([In(ii, mutable=True) for ii in i], o, f = inplace_func([In(ii, mutable=True) for ii in i], o,
mode='FAST_RUN', on_unused_input='ignore') mode='FAST_RUN', on_unused_input='ignore')
...@@ -579,8 +579,7 @@ def test_gemm_opt_double_gemm(): ...@@ -579,8 +579,7 @@ def test_gemm_opt_double_gemm():
def test_gemm_canonicalize(): def test_gemm_canonicalize():
X, Y, Z, a, b = T.matrix('X'), T.matrix('Y'), T.matrix('Z'), T.scalar( X, Y, Z, a, b = T.matrix('X'), T.matrix('Y'), T.matrix('Z'), T.scalar(
'a'), T.scalar('b') 'a'), T.scalar('b')
R, S, U, c, d = T.matrix('R'), T.matrix('S'), T.matrix('U'), T.scalar( c, d = T.scalar('c'), T.scalar('d')
'c'), T.scalar('d')
u = T.row('u') u = T.row('u')
v = T.vector('v') v = T.vector('v')
w = T.col('w') w = T.col('w')
...@@ -631,10 +630,7 @@ def test_gemm_canonicalize(): ...@@ -631,10 +630,7 @@ def test_gemm_canonicalize():
def test_gemm_factor(): def test_gemm_factor():
X, Y, Z, a, b = T.matrix('X'), T.matrix('Y'), T.matrix('Z'), T.scalar( X, Y = T.matrix('X'), T.matrix('Y')
'a'), T.scalar('b')
R, S, U, c, d = T.matrix('R'), T.matrix('S'), T.matrix('U'), T.scalar(
'c'), T.scalar('d')
assert [(1.0, X), (1.0, Y)] == _factor_canonicalized([(1.0, X), (1.0, Y)]) assert [(1.0, X), (1.0, Y)] == _factor_canonicalized([(1.0, X), (1.0, Y)])
assert [(2.0, X)] == _factor_canonicalized([(1.0, X), (1.0, X)]) assert [(2.0, X)] == _factor_canonicalized([(1.0, X), (1.0, X)])
...@@ -653,7 +649,7 @@ def test_upcasting_scalar_nogemm(): ...@@ -653,7 +649,7 @@ def test_upcasting_scalar_nogemm():
f = theano.function([w, v, t, alpha], rval) f = theano.function([w, v, t, alpha], rval)
t = f.maker.fgraph.toposort() t = f.maker.fgraph.toposort()
assert np.sum([isinstance(n.op, Gemm) for n in t]) == 0 assert np.sum([isinstance(n.op, Gemm) for n in t]) == 0
#theano.printing.debugprint(f, print_type=True) # theano.printing.debugprint(f, print_type=True)
v = T.fmatrix('v') v = T.fmatrix('v')
w = T.fmatrix('w') w = T.fmatrix('w')
...@@ -670,7 +666,7 @@ def test_upcasting_scalar_nogemm(): ...@@ -670,7 +666,7 @@ def test_upcasting_scalar_nogemm():
t = f.maker.fgraph.toposort() t = f.maker.fgraph.toposort()
assert np.sum([isinstance(n.op, Gemm) for n in t]) == 0 assert np.sum([isinstance(n.op, Gemm) for n in t]) == 0
#theano.printing.debugprint(f, print_type=True) # theano.printing.debugprint(f, print_type=True)
def test_gemm_nested(): def test_gemm_nested():
...@@ -681,20 +677,20 @@ def test_gemm_nested(): ...@@ -681,20 +677,20 @@ def test_gemm_nested():
just_gemm([X, Y, Z, R, S, U, a, b, c, d], just_gemm([X, Y, Z, R, S, U, a, b, c, d],
[a * Z - b * (c * T.dot(X, Y) + d * Z)], [a * Z - b * (c * T.dot(X, Y) + d * Z)],
ishapes=[(2, 3), (3, 4), (2, 4), (2, 3), (3, 4), ( ishapes=[(2, 3), (3, 4), (2, 4), (2, 3), (3, 4),
2, 4), (), (), (), ()], (2, 4), (), (), (), ()],
max_graphlen=1) max_graphlen=1)
# print "---------------------" # print "---------------------"
just_gemm([X, Y, Z, R, S, U, a, b, c, d], just_gemm([X, Y, Z, R, S, U, a, b, c, d],
[a * Z - b * (c * T.dot(X, Y) + d * Z + c * Z)], [a * Z - b * (c * T.dot(X, Y) + d * Z + c * Z)],
ishapes=[(2, 3), (3, 4), (2, 4), (2, 3), (3, 4), ( ishapes=[(2, 3), (3, 4), (2, 4), (2, 3), (3, 4),
2, 4), (), (), (), ()], (2, 4), (), (), (), ()],
max_graphlen=1) max_graphlen=1)
# print "---------------------" # print "---------------------"
just_gemm([X, Y, Z, R, S, U, a, b, c, d], just_gemm([X, Y, Z, R, S, U, a, b, c, d],
[a * Z - b * (c * T.dot(X, Y) + d * Z + c * U)], [a * Z - b * (c * T.dot(X, Y) + d * Z + c * U)],
ishapes=[(2, 3), (3, 4), (2, 4), (2, 3), (3, 4), ( ishapes=[(2, 3), (3, 4), (2, 4), (2, 3), (3, 4),
2, 4), (), (), (), ()], (2, 4), (), (), (), ()],
max_graphlen=3) max_graphlen=3)
...@@ -720,7 +716,7 @@ def test_gemm_with_vector(): ...@@ -720,7 +716,7 @@ def test_gemm_with_vector():
def my_just_gemm(o): def my_just_gemm(o):
i = [X, Y, Z, a, b, v] i = [X, Y, Z, a, b, v]
ishapes = [(4, 3), (3, 5), (4, 5), (), (), (5, )] ishapes = [(4, 3), (3, 5), (4, 5), (), (), (5, )]
rval = just_gemm(i, o, ishapes=ishapes) just_gemm(i, o, ishapes=ishapes)
my_just_gemm([v + T.dot(X, Y) * a + Z * b]) my_just_gemm([v + T.dot(X, Y) * a + Z * b])
my_just_gemm([v + a * T.dot(X, Y) + b * Z]) my_just_gemm([v + a * T.dot(X, Y) + b * Z])
...@@ -741,7 +737,7 @@ def test_gemm_with_vector(): ...@@ -741,7 +737,7 @@ def test_gemm_with_vector():
def test_gemm_opt_vector_stuff(): def test_gemm_opt_vector_stuff():
X, Y, Z, a, b = T.matrix(), T.matrix(), T.matrix(), T.scalar(), T.scalar() X, Y, a = T.matrix(), T.matrix(), T.scalar()
u, v = T.vector(), T.vector() u, v = T.vector(), T.vector()
f = inplace_func([a, u, v], a + T.dot(u, v), mode='FAST_RUN') f = inplace_func([a, u, v], a + T.dot(u, v), mode='FAST_RUN')
...@@ -771,8 +767,6 @@ def test_gemm_unrolled(): ...@@ -771,8 +767,6 @@ def test_gemm_unrolled():
H = sharedX(np.zeros((batch_size, rep_size)), name='H') H = sharedX(np.zeros((batch_size, rep_size)), name='H')
G = sharedX(np.zeros((batch_size, rep_size)), name='G') G = sharedX(np.zeros((batch_size, rep_size)), name='G')
init_V = sharedX(rng.uniform(0, 1, (batch_size, rep_size)), name='init_V')
init_H = sharedX(rng.uniform(0, 1, (batch_size, rep_size)), name='init_H')
cur_V = V cur_V = V
cur_H = H cur_H = H
...@@ -817,7 +811,7 @@ def test_inplace0(): ...@@ -817,7 +811,7 @@ def test_inplace0():
f = inplace_func([X, Y, Z, a, b, R, S, c], f = inplace_func([X, Y, Z, a, b, R, S, c],
[Z * (c * Z + a * T.dot(X, Y) + b * T.dot(R, S).T)], [Z * (c * Z + a * T.dot(X, Y) + b * T.dot(R, S).T)],
mode='FAST_RUN') mode='FAST_RUN')
if (not gemm_inplace in [n.op for n in f.maker.fgraph.apply_nodes]): if (gemm_inplace not in [n.op for n in f.maker.fgraph.apply_nodes]):
theano.printing.debugprint(f) theano.printing.debugprint(f)
raise Failure('no gemm_inplace in graph') raise Failure('no gemm_inplace in graph')
...@@ -866,7 +860,7 @@ def test_dot22scalar(): ...@@ -866,7 +860,7 @@ def test_dot22scalar():
# TODO: exclude other optimizations in BlasOpt? # TODO: exclude other optimizations in BlasOpt?
# m = theano.compile.get_default_mode().including('local_dot_to_dot22', # m = theano.compile.get_default_mode().including('local_dot_to_dot22',
# 'local_dot22_to_dot22scalar','specialize') # 'local_dot22_to_dot22scalar','specialize')
#m = theano.compile.get_default_mode().including('BlasOpt', 'specialize') # m = theano.compile.get_default_mode().including('BlasOpt', 'specialize')
rng = np.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
for dtype1 in ['complex64', 'complex128']: for dtype1 in ['complex64', 'complex128']:
a = T.matrix('a', dtype=dtype1) a = T.matrix('a', dtype=dtype1)
...@@ -1087,7 +1081,7 @@ def test_dot_w_self(): ...@@ -1087,7 +1081,7 @@ def test_dot_w_self():
class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
def test_dot_vv(self): def test_dot_vv(self):
''' Currently we generate a gemv for that case''' # Currently we generate a gemv for that case
rng = np.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32')) v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
w = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32')) w = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
...@@ -1101,11 +1095,10 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1101,11 +1095,10 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
assert np.allclose(f(), np.dot(v.get_value(), w.get_value())) assert np.allclose(f(), np.dot(v.get_value(), w.get_value()))
def test_dot_vm(self): def test_dot_vm(self):
''' Test vector dot matrix ''' # Test vector dot matrix
rng = np.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32')) v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(np.array(rng.uniform(size=(2, 3)), m = theano.shared(np.array(rng.uniform(size=(2, 3)), dtype='float32'))
dtype='float32'))
f = theano.function([], theano.dot(v, m), mode=mode_blas_opt) f = theano.function([], theano.dot(v, m), mode=mode_blas_opt)
# Assert that the dot was optimized somehow # Assert that the dot was optimized somehow
...@@ -1115,17 +1108,14 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1115,17 +1108,14 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output # Assert they produce the same output
assert np.allclose(f(), np.dot(v.get_value(), m.get_value())) assert np.allclose(f(), np.dot(v.get_value(), m.get_value()))
# Assert it works when m has no contiguous dimension # Assert it works when m has no contiguous dimension
m.set_value( m.set_value(m.get_value(borrow=True)[::-1, ::-1], borrow=True)
m.get_value(borrow=True)[::-1, ::-1],
borrow=True)
assert np.allclose(f(), np.dot(v.get_value(), m.get_value())) assert np.allclose(f(), np.dot(v.get_value(), m.get_value()))
def test_dot_mv(self): def test_dot_mv(self):
''' Test matrix dot vector ''' # Test matrix dot vector
rng = np.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32')) v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(np.array(rng.uniform(size=(3, 2)), m = theano.shared(np.array(rng.uniform(size=(3, 2)), dtype='float32'))
dtype='float32'))
f = theano.function([], theano.dot(m, v), mode=mode_blas_opt) f = theano.function([], theano.dot(m, v), mode=mode_blas_opt)
# Assert that the dot was optimized somehow # Assert that the dot was optimized somehow
...@@ -1135,31 +1125,27 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1135,31 +1125,27 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output # Assert they produce the same output
assert np.allclose(f(), np.dot(m.get_value(), v.get_value())) assert np.allclose(f(), np.dot(m.get_value(), v.get_value()))
# Assert it works when m has no contiguous dimension # Assert it works when m has no contiguous dimension
m.set_value( m.set_value(m.get_value(borrow=True)[::-1, ::-1], borrow=True)
m.get_value(borrow=True)[::-1, ::-1],
borrow=True)
assert np.allclose(f(), np.dot(m.get_value(), v.get_value())) assert np.allclose(f(), np.dot(m.get_value(), v.get_value()))
@staticmethod @staticmethod
def t_gemv1(m_shp): def t_gemv1(m_shp):
''' test vector2+dot(matrix,vector1) ''' # test vector2+dot(matrix,vector1)
rng = np.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(np.array(rng.uniform(size=(m_shp[1],) v1 = theano.shared(np.array(rng.uniform(size=(m_shp[1],)),
), dtype='float32')) dtype='float32'))
v2_orig = np.array(rng.uniform(size=(m_shp[0],)), dtype='float32') v2_orig = np.array(rng.uniform(size=(m_shp[0],)), dtype='float32')
v2 = theano.shared(v2_orig) v2 = theano.shared(v2_orig)
m = theano.shared(np.array(rng.uniform(size=m_shp), m = theano.shared(np.array(rng.uniform(size=m_shp), dtype='float32'))
dtype='float32'))
f = theano.function([], v2 + theano.dot(m, v1), mode=mode_blas_opt) f = theano.function([], v2 + theano.dot(m, v1), mode=mode_blas_opt)
# Assert they produce the same output # Assert they produce the same output
assert np.allclose(f(), assert np.allclose(f(), np.dot(m.get_value(), v1.get_value()) + v2_orig)
np.dot(m.get_value(), v1.get_value()) + v2_orig)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert len(topo) == 1 assert len(topo) == 1
assert isinstance(topo[0].op, Gemv) assert isinstance(topo[0].op, Gemv)
assert topo[0].op.inplace == False assert topo[0].op.inplace is False
# test the inplace version # test the inplace version
g = theano.function([], [], updates=[(v2, v2 + theano.dot(m, v1))], g = theano.function([], [], updates=[(v2, v2 + theano.dot(m, v1))],
...@@ -1167,17 +1153,16 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1167,17 +1153,16 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output # Assert they produce the same output
g() g()
assert np.allclose(v2.get_value(), assert np.allclose(v2.get_value(), np.dot(m.get_value(),
np.dot(m.get_value(), v1.get_value()) + v2_orig) v1.get_value()) + v2_orig)
topo = g.maker.fgraph.toposort() topo = g.maker.fgraph.toposort()
assert len(topo) == 1 assert len(topo) == 1
assert isinstance(topo[0].op, Gemv) assert isinstance(topo[0].op, Gemv)
if config.mode != 'FAST_COMPILE': if config.mode != 'FAST_COMPILE':
assert topo[0].op.inplace == True assert topo[0].op.inplace is True
# Do the same tests with a matrix with strides in both dimensions # Do the same tests with a matrix with strides in both dimensions
m.set_value( m.set_value(m.get_value(borrow=True)[::-1, ::-1],
m.get_value(borrow=True)[::-1, ::-1],
borrow=True) borrow=True)
v2.set_value(v2_orig) v2.set_value(v2_orig)
assert np.allclose(f(), assert np.allclose(f(),
...@@ -1194,7 +1179,7 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1194,7 +1179,7 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self.t_gemv1((0, 0)) self.t_gemv1((0, 0))
def test_gemv2(self): def test_gemv2(self):
''' test vector2+dot(vector1,matrix) ''' # test vector2+dot(vector1,matrix)
rng = np.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(np.array(rng.uniform(size=(2,)), v1 = theano.shared(np.array(rng.uniform(size=(2,)),
dtype='float32')) dtype='float32'))
...@@ -1207,10 +1192,11 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1207,10 +1192,11 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output # Assert they produce the same output
assert np.allclose(f(), assert np.allclose(f(),
np.dot(v1.get_value(), m.get_value()) + v2.get_value()) np.dot(v1.get_value(), m.get_value()) +
v2.get_value())
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1 assert sum(isinstance(node.op, Gemv) for node in topo) == 1
assert topo[-1].op.inplace == False assert topo[-1].op.inplace is False
# test the inplace version # test the inplace version
g = theano.function([], [], updates=[(v2, v2 + theano.dot(v1, m))], g = theano.function([], [], updates=[(v2, v2 + theano.dot(v1, m))],
...@@ -1223,21 +1209,21 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1223,21 +1209,21 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
topo = g.maker.fgraph.toposort() topo = g.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1 assert sum(isinstance(node.op, Gemv) for node in topo) == 1
if config.mode != 'FAST_COMPILE': if config.mode != 'FAST_COMPILE':
assert topo[-1].op.inplace == True assert topo[-1].op.inplace is True
# Do the same tests with a matrix with strides in both dimensions # Do the same tests with a matrix with strides in both dimensions
m.set_value( m.set_value(m.get_value(borrow=True)[::-1, ::-1],
m.get_value(borrow=True)[::-1, ::-1],
borrow=True) borrow=True)
v2.set_value(v2_orig) v2.set_value(v2_orig)
assert np.allclose(f(), assert np.allclose(f(),
np.dot(v1.get_value(), m.get_value()) + v2.get_value()) np.dot(v1.get_value(), m.get_value()) +
v2.get_value())
g() g()
assert np.allclose(v2.get_value(), assert np.allclose(v2.get_value(),
np.dot(v1.get_value(), m.get_value()) + v2_orig) np.dot(v1.get_value(), m.get_value()) + v2_orig)
def test_gemv_broadcast(self): def test_gemv_broadcast(self):
''' test gemv with some broadcasted input ''' # test gemv with some broadcasted input
rng = np.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(np.array(rng.uniform(size=(2,)), v1 = theano.shared(np.array(rng.uniform(size=(2,)),
dtype='float32')) dtype='float32'))
...@@ -1261,7 +1247,7 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1261,7 +1247,7 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
f = theano.function([], o, mode=mode_blas_opt) f = theano.function([], o, mode=mode_blas_opt)
assert np.allclose( assert np.allclose(
f(), f(),
0.5*np.dot(m.get_value(), v1.get_value()) + 0.25*v2.get_value()) 0.5 * np.dot(m.get_value(), v1.get_value()) + 0.25 * v2.get_value())
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1 assert sum(isinstance(node.op, Gemv) for node in topo) == 1
...@@ -1336,8 +1322,8 @@ class BaseGemv(object): ...@@ -1336,8 +1322,8 @@ class BaseGemv(object):
def test_simple(self): def test_simple(self):
alpha, beta, a, x, y = [self.shared(value) alpha, beta, a, x, y = [self.shared(value)
for value in self.get_data()] for value in self.get_data()]
desired_oy = alpha.get_value() * matrixmultiply(a. desired_oy = alpha.get_value() *
get_value(), x.get_value()) + beta.get_value() * y.get_value() matrixmultiply(a.get_value(), x.get_value()) + beta.get_value() * y.get_value()
oy = alpha * T.dot(a, x) + beta * y oy = alpha * T.dot(a, x) + beta * y
...@@ -1406,8 +1392,8 @@ class BaseGemv(object): ...@@ -1406,8 +1392,8 @@ class BaseGemv(object):
alpha_v, beta_v, a_v, x_v, y_v = vs alpha_v, beta_v, a_v, x_v, y_v = vs
alpha, beta, a, x, y = [self.shared(v) for v in vs] alpha, beta, a, x, y = [self.shared(v) for v in vs]
desired_oy = alpha_v * matrixmultiply(transpose(a_v), x_v[:: desired_oy = alpha_v * matrixmultiply(transpose(a_v), x_v[::2]) +
2]) + beta_v * y_v beta_v * y_v
oy = alpha * T.dot(a.T, x[::2]) + beta * y oy = alpha * T.dot(a.T, x[::2]) + beta * y
...@@ -1456,8 +1442,7 @@ class BaseGemv(object): ...@@ -1456,8 +1442,7 @@ class BaseGemv(object):
alpha_v, beta_v, a_v, x_v, y_v = vs alpha_v, beta_v, a_v, x_v, y_v = vs
alpha, beta, a, x, y = [self.shared(v) for v in vs] alpha, beta, a, x, y = [self.shared(v) for v in vs]
a_v = a_v[::-1, ::-1] a_v = a_v[::-1, ::-1]
a.set_value( a.set_value(a.get_value(borrow=True,
a.get_value(borrow=True,
return_internal_type=True)[::-1, ::-1], return_internal_type=True)[::-1, ::-1],
borrow=True) borrow=True)
...@@ -1477,8 +1462,7 @@ class BaseGemv(object): ...@@ -1477,8 +1462,7 @@ class BaseGemv(object):
alpha_v, beta_v, a_v, x_v, y_v = vs alpha_v, beta_v, a_v, x_v, y_v = vs
alpha, beta, a, x, y = [self.shared(v) for v in vs] alpha, beta, a, x, y = [self.shared(v) for v in vs]
a_v = a_v[::-1, ::-1] a_v = a_v[::-1, ::-1]
a.set_value( a.set_value(a.get_value(borrow=True,
a.get_value(borrow=True,
return_internal_type=True)[::-1, ::-1], return_internal_type=True)[::-1, ::-1],
borrow=True) borrow=True)
...@@ -1517,7 +1501,7 @@ class BaseGemv(object): ...@@ -1517,7 +1501,7 @@ class BaseGemv(object):
# done inplace on a temporarily allocated-buffer, which is # done inplace on a temporarily allocated-buffer, which is
# then scaled by alpha and to t with a fused elemwise. # then scaled by alpha and to t with a fused elemwise.
n_gemvs = 0 n_gemvs = 0
#theano.printing.debugprint(f, print_type=True) # theano.printing.debugprint(f, print_type=True)
for node in f.maker.fgraph.toposort(): for node in f.maker.fgraph.toposort():
if node.op == self.gemv_inplace: if node.op == self.gemv_inplace:
n_gemvs += 1 n_gemvs += 1
...@@ -1602,7 +1586,8 @@ class TestGer_make_node(TestCase): ...@@ -1602,7 +1586,8 @@ class TestGer_make_node(TestCase):
ger, self.fm, self.fv1, self.fv, self.fv_2) ger, self.fm, self.fv1, self.fv, self.fv_2)
# actually doing the aforementioned dimshuffle makes it work # actually doing the aforementioned dimshuffle makes it work
self.assertEqual(self.fm.type, self.assertEqual(self.fm.type,
ger(self.fm, self.fv1.dimshuffle(), self.fv, self.fv_2).type) ger(self.fm, self.fv1.dimshuffle(), self.fv,
self.fv_2).type)
def test_fails_for_nonmatrix_A(self): def test_fails_for_nonmatrix_A(self):
self.assertRaises(TypeError, self.assertRaises(TypeError,
...@@ -1610,9 +1595,11 @@ class TestGer_make_node(TestCase): ...@@ -1610,9 +1595,11 @@ class TestGer_make_node(TestCase):
def test_fails_for_nonvector_x_or_y(self): def test_fails_for_nonvector_x_or_y(self):
self.assertRaises(TypeError, self.assertRaises(TypeError,
ger, self.fm, self.fa, self.fv.dimshuffle('x', 0), self.fv_2) ger, self.fm, self.fa,
self.fv.dimshuffle('x', 0), self.fv_2)
self.assertRaises(TypeError, self.assertRaises(TypeError,
ger, self.fm, self.fa, self.fv, self.fv_2.dimshuffle('x', 0)) ger, self.fm, self.fa,
self.fv, self.fv_2.dimshuffle('x', 0))
def test_fails_for_mixed_dtypes(self): def test_fails_for_mixed_dtypes(self):
self.assertRaises(TypeError, ger, self.dm, self.fa, self.fv, self.fv_2) self.assertRaises(TypeError, ger, self.dm, self.fa, self.fv, self.fv_2)
...@@ -1657,29 +1644,25 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1657,29 +1644,25 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
def test_b_0_triggers_ger(self): def test_b_0_triggers_ger(self):
""" test local_gemm_to_ger opt""" """ test local_gemm_to_ger opt"""
assert T.blas.local_gemm_to_ger.transform( assert T.blas.local_gemm_to_ger.transform(
gemm_no_inplace( gemm_no_inplace(self.A, self.a, self.x.dimshuffle(0, 'x'),
self.A, self.a, self.x.dimshuffle(0, 'x'),
self.y.dimshuffle('x', 0), self.b(0)).owner) self.y.dimshuffle('x', 0), self.b(0)).owner)
def test_b_1_triggers_ger(self): def test_b_1_triggers_ger(self):
""" test local_gemm_to_ger opt""" """ test local_gemm_to_ger opt"""
assert T.blas.local_gemm_to_ger.transform( assert T.blas.local_gemm_to_ger.transform(
gemm_no_inplace( gemm_no_inplace(self.A, self.a, self.x.dimshuffle(0, 'x'),
self.A, self.a, self.x.dimshuffle(0, 'x'),
self.y.dimshuffle('x', 0), self.b(1)).owner) self.y.dimshuffle('x', 0), self.b(1)).owner)
def test_b_other_does_not_triggers_ger(self): def test_b_other_does_not_triggers_ger(self):
""" test local_gemm_to_ger opt""" """ test local_gemm_to_ger opt"""
assert not T.blas.local_gemm_to_ger.transform( assert not T.blas.local_gemm_to_ger.transform(
gemm_no_inplace( gemm_no_inplace(self.A, self.a, self.x.dimshuffle(0, 'x'),
self.A, self.a, self.x.dimshuffle(0, 'x'),
self.y.dimshuffle('x', 0), self.b(1.5)).owner) self.y.dimshuffle('x', 0), self.b(1.5)).owner)
def test_b_nonconst_does_not_triggers_ger(self): def test_b_nonconst_does_not_triggers_ger(self):
""" test local_gemm_to_ger opt""" """ test local_gemm_to_ger opt"""
assert not T.blas.local_gemm_to_ger.transform( assert not T.blas.local_gemm_to_ger.transform(
gemm_no_inplace( gemm_no_inplace(self.A, self.a, self.x.dimshuffle(0, 'x'),
self.A, self.a, self.x.dimshuffle(0, 'x'),
self.y.dimshuffle('x', 0), self.a).owner) self.y.dimshuffle('x', 0), self.a).owner)
def test_outer(self): def test_outer(self):
...@@ -1954,28 +1937,36 @@ class TestBlasStrides(TestCase): ...@@ -1954,28 +1937,36 @@ class TestBlasStrides(TestCase):
bt_dev = b_t.get_value(borrow=False, return_internal_type=True) bt_dev = b_t.get_value(borrow=False, return_internal_type=True)
ct_dev = c_t.get_value(borrow=False, return_internal_type=True) ct_dev = c_t.get_value(borrow=False, return_internal_type=True)
f_nnn = theano.function([], [], f_nnn = theano.function(
[], [],
updates=[(a, (l * a + tensor.dot(b, c)))], updates=[(a, (l * a + tensor.dot(b, c)))],
mode=self.mode) mode=self.mode)
f_nnt = theano.function([], [], f_nnt = theano.function(
[], [],
updates=[(a, (l * a + tensor.dot(b, c_t.T)))], updates=[(a, (l * a + tensor.dot(b, c_t.T)))],
mode=self.mode) mode=self.mode)
f_ntn = theano.function([], [], f_ntn = theano.function(
[], [],
updates=[(a, (l * a + tensor.dot(b_t.T, c)))], updates=[(a, (l * a + tensor.dot(b_t.T, c)))],
mode=self.mode) mode=self.mode)
f_ntt = theano.function([], [], f_ntt = theano.function(
[], [],
updates=[(a, (l * a + tensor.dot(b_t.T, c_t.T)))], updates=[(a, (l * a + tensor.dot(b_t.T, c_t.T)))],
mode=self.mode) mode=self.mode)
f_tnn = theano.function([], [], f_tnn = theano.function(
[], [],
updates=[(a_t, (l * a_t + tensor.dot(b, c).T))], updates=[(a_t, (l * a_t + tensor.dot(b, c).T))],
mode=self.mode) mode=self.mode)
f_tnt = theano.function([], [], f_tnt = theano.function(
[], [],
updates=[(a_t, (l * a_t + tensor.dot(b, c_t.T).T))], updates=[(a_t, (l * a_t + tensor.dot(b, c_t.T).T))],
mode=self.mode) mode=self.mode)
f_ttn = theano.function([], [], f_ttn = theano.function(
[], [],
updates=[(a_t, (l * a_t + tensor.dot(b_t.T, c).T))], updates=[(a_t, (l * a_t + tensor.dot(b_t.T, c).T))],
mode=self.mode) mode=self.mode)
f_ttt = theano.function([], [], f_ttt = theano.function(
[], [],
updates=[(a_t, (l * a_t + tensor.dot(b_t.T, c_t.T).T))], updates=[(a_t, (l * a_t + tensor.dot(b_t.T, c_t.T).T))],
mode=self.mode) mode=self.mode)
...@@ -1991,11 +1982,11 @@ class TestBlasStrides(TestCase): ...@@ -1991,11 +1982,11 @@ class TestBlasStrides(TestCase):
c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True) c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True)
# Numpy results # Numpy results
a_n = (l * av[::a_step1, ::a_step2] a_n = (l * av[::a_step1, ::a_step2] +
+ np.dot(bv[::b_step1, ::b_step2], np.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2])) cv[::c_step1, ::c_step2]))
at_n = (l * av[::a_step1, ::a_step2].T at_n = (l * av[::a_step1, ::a_step2].T +
+ np.dot(bv[::b_step1, ::b_step2], np.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2]).T) cv[::c_step1, ::c_step2]).T)
# a's value is updated, so we need to reinitialize it each time # a's value is updated, so we need to reinitialize it each time
...@@ -2085,8 +2076,8 @@ class TestBlasStrides(TestCase): ...@@ -2085,8 +2076,8 @@ class TestBlasStrides(TestCase):
borrow=True) borrow=True)
c.set_value(c_dev.copy()[::c_step], borrow=True) c.set_value(c_dev.copy()[::c_step], borrow=True)
a_n = (av[::a_step] a_n = (av[::a_step] +
+ l * np.dot(bv[::b_step1, ::b_step2], l * np.dot(bv[::b_step1, ::b_step2],
cv[::c_step])) cv[::c_step]))
f_n() f_n()
assert np.allclose(a.get_value(), a_n), (a.get_value(), a_n) assert np.allclose(a.get_value(), a_n), (a.get_value(), a_n)
...@@ -2120,11 +2111,13 @@ class TestBlasStrides(TestCase): ...@@ -2120,11 +2111,13 @@ class TestBlasStrides(TestCase):
b_dev = b.get_value(borrow=False, return_internal_type=True) b_dev = b.get_value(borrow=False, return_internal_type=True)
c_dev = c.get_value(borrow=False, return_internal_type=True) c_dev = c.get_value(borrow=False, return_internal_type=True)
f_n = theano.function([], [], f_n = theano.function(
[], [],
updates=[(a, (a + l * tensor.outer(b, c)))], updates=[(a, (a + l * tensor.outer(b, c)))],
mode=self.mode) mode=self.mode)
f_t = theano.function([], [], f_t = theano.function(
[], [],
updates=[(a_t, (a_t + l * tensor.outer(b, c).T))], updates=[(a_t, (a_t + l * tensor.outer(b, c).T))],
mode=self.mode) mode=self.mode)
...@@ -2141,15 +2134,14 @@ class TestBlasStrides(TestCase): ...@@ -2141,15 +2134,14 @@ class TestBlasStrides(TestCase):
c.set_value(c_dev.copy()[::c_step], borrow=True) c.set_value(c_dev.copy()[::c_step], borrow=True)
f_n() f_n()
n_n = (av[::a_step1, ::a_step2] n_n = (av[::a_step1, ::a_step2] +
+ l * np.outer(bv[::b_step], cv[::c_step])) l * np.outer(bv[::b_step], cv[::c_step]))
assert np.allclose(a.get_value(), n_n), (a.get_value(), n_n) assert np.allclose(a.get_value(), n_n), (a.get_value(), n_n)
f_t() f_t()
n_t = (av.T[::a_step1, ::a_step2] n_t = (av.T[::a_step1, ::a_step2] +
+ l * np.outer(bv[::b_step], cv[::c_step]).T) l * np.outer(bv[::b_step], cv[::c_step]).T)
assert np.allclose(a_t.get_value(), n_t),\ assert np.allclose(a_t.get_value(), n_t), (a_t.get_value(), n_t)
(a_t.get_value(), n_t)
def test_ger_strides(self): def test_ger_strides(self):
self.cmp_ger((3, 5), 3, 5) self.cmp_ger((3, 5), 3, 5)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论