提交 bdb05333 authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Use timeit in tests.scan.test_basic.test_speed* tests

上级 eeaa94d6
...@@ -4565,28 +4565,22 @@ class TestScan: ...@@ -4565,28 +4565,22 @@ class TestScan:
not config.cxx, reason="G++ not available, so we need to skip this test." not config.cxx, reason="G++ not available, so we need to skip this test."
) )
def test_speed(): def test_speed():
""" from timeit import timeit
This function prints out the speed of very simple recurrent
calculations implemented in various ways. In DebugMode this will
test the correctness of the optimizations applied, but generally
correctness-testing is not the goal of this test.
To be honest, it isn't really a unit test so much as a tool for testing n_timeit = 50
approaches to scan.
The computation being tested here is a recurrent addition.
"""
# We need the CVM for this speed test # We need the CVM for this speed test
r = np.arange(10000).astype(config.floatX).reshape(1000, 10) r = np.arange(10000).astype(config.floatX).reshape(1000, 10)
t0 = time.time() def f_py():
for i in range(1, 1000): for i in range(1, 1000):
r[i] += r[i - 1] r[i] += r[i - 1]
t1 = time.time()
print("python", t1 - t0) python_duration = timeit(lambda: f_py(), number=n_timeit)
r = np.arange(10000).astype(config.floatX).reshape(1000, 10) r = np.arange(10000).astype(config.floatX).reshape(1000, 10)
t0 = time.time()
def f_py_iter():
r_i = iter(r[1:]) r_i = iter(r[1:])
r_ii = iter(r[:-1]) r_ii = iter(r[:-1])
while True: while True:
...@@ -4595,24 +4589,25 @@ def test_speed(): ...@@ -4595,24 +4589,25 @@ def test_speed():
tmp += next(r_ii) tmp += next(r_ii)
except StopIteration: except StopIteration:
break break
t1 = time.time()
print("python with builtin iterator", t1 - t0)
r = np.arange(10000).astype(config.floatX).reshape(1000, 10) python_iter_duration = timeit(lambda: f_py_iter(), number=n_timeit)
s_r = tensor.matrix()
s_y, updates = scan( # r = np.arange(10000).astype(config.floatX).reshape(1000, 10)
fn=lambda ri, rii: ri + rii, # s_r = tensor.matrix()
sequences=[s_r[1:]], # s_y, updates = scan(
outputs_info=tensor.constant(r[0]), # fn=lambda ri, rii: ri + rii,
mode=theano.Mode(linker="cvm"), # sequences=[s_r[1:]],
) # outputs_info=tensor.constant(r[0]),
assert not updates # mode=theano.Mode(linker="cvm"),
f = theano.function([s_r], s_y) # )
# assert not updates
#
# f_cvm = theano.function([s_r], s_y)
#
# cvm_duration = timeit(lambda: f_cvm(r), number=n_timeit)
t2 = time.time() # XXX: Why does this take so much longer than Python?!
f(r) # assert cvm_duration - python_duration < python_duration * 0.15
t3 = time.time()
print("theano (scan, cvm)", t3 - t2)
r = np.arange(10000).astype(config.floatX).reshape(-1, 10) r = np.arange(10000).astype(config.floatX).reshape(-1, 10)
shared_r = theano.shared(r) shared_r = theano.shared(r)
...@@ -4620,39 +4615,28 @@ def test_speed(): ...@@ -4620,39 +4615,28 @@ def test_speed():
s_rinc = tensor.inc_subtensor( s_rinc = tensor.inc_subtensor(
shared_r[s_i], shared_r[s_i - 1], tolerate_inplace_aliasing=True shared_r[s_i], shared_r[s_i - 1], tolerate_inplace_aliasing=True
) )
# theano.printing.debugprint(s_rinc)
f = theano.function( f_cvm_shared = theano.function(
[], [],
[], [],
updates=OrderedDict([(s_i, s_i + 1), (shared_r, s_rinc)]), updates=OrderedDict([(s_i, s_i + 1), (shared_r, s_rinc)]),
mode=theano.Mode(linker="cvm"), mode=theano.Mode(linker="cvm"),
) )
f._check_for_aliased_inputs = False f_cvm_shared._check_for_aliased_inputs = False
t2 = time.time()
f_fn = f.fn cvm_shared_duration = timeit(lambda: f_cvm_shared(), number=n_timeit)
for i in range(998):
f_fn() assert cvm_shared_duration < python_duration
f() # 999 to update the profiling timers assert cvm_shared_duration < python_iter_duration
t3 = time.time()
print("theano (updates, cvm)", t3 - t2)
@pytest.mark.skipif( @pytest.mark.skipif(
not config.cxx, reason="G++ not available, so we need to skip this test." not config.cxx, reason="G++ not available, so we need to skip this test."
) )
def test_speed_rnn(): def test_speed_rnn():
""" from timeit import timeit
This function prints out the speed of recurrent neural network
calculations implemented in various ways. In DebugMode this will
test the correctness of the optimizations applied, but generally
correctness-testing is not the goal of this test.
To be honest, it isn't really a unit test so much as a tool for testing n_timeit = 50
approaches to scan.
The computation being tested here is a repeated tanh of a matrix-vector
multiplication - the heart of an ESN or RNN.
"""
L = 10000 L = 10000
N = 50 N = 50
...@@ -4660,29 +4644,28 @@ def test_speed_rnn(): ...@@ -4660,29 +4644,28 @@ def test_speed_rnn():
r = np.arange(L * N).astype(config.floatX).reshape(L, N) r = np.arange(L * N).astype(config.floatX).reshape(L, N)
w = np.random.randn(N, N).astype(config.floatX) w = np.random.randn(N, N).astype(config.floatX)
t0 = time.time() def f_py():
for i in range(1, L): for i in range(1, L):
r[i] = np.tanh(np.dot(r[i - 1], w)) r[i] = np.tanh(np.dot(r[i - 1], w))
t1 = time.time()
python_duration = t1 - t0
print("python", python_duration)
r = np.arange(L * N).astype(config.floatX).reshape(L, N) python_duration = timeit(lambda: f_py(), number=n_timeit)
s_r = tensor.matrix()
s_y, updates = scan( # r = np.arange(L * N).astype(config.floatX).reshape(L, N)
fn=lambda ri, rii: tensor.tanh(tensor.dot(rii, w)), # s_r = tensor.matrix()
sequences=[s_r[1:]], # s_y, updates = scan(
outputs_info=tensor.constant(r[0]), # fn=lambda ri, rii: tensor.tanh(tensor.dot(rii, w)),
mode=theano.Mode(linker="cvm"), # sequences=[s_r[1:]],
) # outputs_info=tensor.constant(r[0]),
assert not updates # mode=theano.Mode(linker="cvm"),
f = theano.function([s_r], s_y, mode=theano.Mode(linker="cvm")) # )
# assert not updates
#
# f_cvm = theano.function([s_r], s_y, mode=theano.Mode(linker="cvm"))
#
# cvm_duration = timeit(lambda: f_cvm(r), number=n_timeit)
t2 = time.time() # XXX: Why does this take so much longer than Python?!
f(r) # assert cvm_duration - python_duration < python_duration * 0.15
t3 = time.time()
cvm_duration = t3 - t2
print("theano (cvm)", cvm_duration)
r = np.arange(L * N).astype(config.floatX).reshape(L, N) r = np.arange(L * N).astype(config.floatX).reshape(L, N)
shared_r = theano.shared(r) shared_r = theano.shared(r)
...@@ -4692,20 +4675,14 @@ def test_speed_rnn(): ...@@ -4692,20 +4675,14 @@ def test_speed_rnn():
theano.tensor.tanh(theano.tensor.dot(shared_r[s_i - 1], w)), theano.tensor.tanh(theano.tensor.dot(shared_r[s_i - 1], w)),
tolerate_inplace_aliasing=True, tolerate_inplace_aliasing=True,
) )
f = theano.function( f_cvm_shared = theano.function(
[], [],
[], [],
updates=OrderedDict([(s_i, s_i + 1), (shared_r, s_rinc)]), updates=OrderedDict([(s_i, s_i + 1), (shared_r, s_rinc)]),
mode=theano.Mode(linker="cvm"), mode=theano.Mode(linker="cvm"),
) )
f_fn = f.fn cvm_shared_duration = timeit(lambda: f_cvm_shared(), number=n_timeit)
t4 = time.time()
f_fn(n_calls=L - 2)
f() # 999 to update the profiling timers
t5 = time.time()
cvm_shared_duration = t5 - t4
print("theano (updates, cvm)", cvm_shared_duration)
assert cvm_shared_duration < python_duration assert cvm_shared_duration < python_duration
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论