Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bdb05333
提交
bdb05333
authored
12月 31, 2020
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
12月 31, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use timeit in tests.scan.test_basic.test_speed* tests
上级
eeaa94d6
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
68 行增加
和
91 行删除
+68
-91
test_basic.py
tests/scan/test_basic.py
+68
-91
没有找到文件。
tests/scan/test_basic.py
浏览文件 @
bdb05333
...
@@ -4565,54 +4565,49 @@ class TestScan:
...
@@ -4565,54 +4565,49 @@ 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
)
r
=
np
.
arange
(
10000
)
.
astype
(
config
.
floatX
)
.
reshape
(
1000
,
10
)
python_duration
=
timeit
(
lambda
:
f_py
(),
number
=
n_timeit
)
t0
=
time
.
time
()
r_i
=
iter
(
r
[
1
:])
r_ii
=
iter
(
r
[:
-
1
])
while
True
:
try
:
tmp
=
next
(
r_i
)
tmp
+=
next
(
r_ii
)
except
StopIteration
:
break
t1
=
time
.
time
()
print
(
"python with builtin iterator"
,
t1
-
t0
)
r
=
np
.
arange
(
10000
)
.
astype
(
config
.
floatX
)
.
reshape
(
1000
,
10
)
r
=
np
.
arange
(
10000
)
.
astype
(
config
.
floatX
)
.
reshape
(
1000
,
10
)
s_r
=
tensor
.
matrix
()
s_y
,
updates
=
scan
(
fn
=
lambda
ri
,
rii
:
ri
+
rii
,
sequences
=
[
s_r
[
1
:]],
outputs_info
=
tensor
.
constant
(
r
[
0
]),
mode
=
theano
.
Mode
(
linker
=
"cvm"
),
)
assert
not
updates
f
=
theano
.
function
([
s_r
],
s_y
)
t2
=
time
.
time
()
def
f_py_iter
():
f
(
r
)
r_i
=
iter
(
r
[
1
:])
t3
=
time
.
time
()
r_ii
=
iter
(
r
[:
-
1
])
print
(
"theano (scan, cvm)"
,
t3
-
t2
)
while
True
:
try
:
tmp
=
next
(
r_i
)
tmp
+=
next
(
r_ii
)
except
StopIteration
:
break
python_iter_duration
=
timeit
(
lambda
:
f_py_iter
(),
number
=
n_timeit
)
# r = np.arange(10000).astype(config.floatX).reshape(1000, 10)
# s_r = tensor.matrix()
# s_y, updates = scan(
# fn=lambda ri, rii: ri + rii,
# sequences=[s_r[1:]],
# outputs_info=tensor.constant(r[0]),
# mode=theano.Mode(linker="cvm"),
# )
# assert not updates
#
# f_cvm = theano.function([s_r], s_y)
#
# cvm_duration = timeit(lambda: f_cvm(r), number=n_timeit)
# XXX: Why does this take so much longer than Python?!
# assert cvm_duration - python_duration < python_duration * 0.15
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
approaches to scan.
The computation being tested here is a repeated tanh of a matrix-vector
n_timeit
=
50
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
python_duration
=
timeit
(
lambda
:
f_py
(),
number
=
n_timeit
)
print
(
"python"
,
python_duration
)
# r = np.arange(L * N).astype(config.floatX).reshape(L, N)
r
=
np
.
arange
(
L
*
N
)
.
astype
(
config
.
floatX
)
.
reshape
(
L
,
N
)
# s_r = tensor.matrix()
s_r
=
tensor
.
matrix
()
# s_y, updates = scan(
s_y
,
updates
=
scan
(
# fn=lambda ri, rii: tensor.tanh(tensor.dot(rii, w)),
fn
=
lambda
ri
,
rii
:
tensor
.
tanh
(
tensor
.
dot
(
rii
,
w
)),
# sequences=[s_r[1:]],
sequences
=
[
s_r
[
1
:]],
# outputs_info=tensor.constant(r[0]),
outputs_info
=
tensor
.
constant
(
r
[
0
]),
# mode=theano.Mode(linker="cvm"),
mode
=
theano
.
Mode
(
linker
=
"cvm"
),
# )
)
# assert not updates
assert
not
updates
#
f
=
theano
.
function
([
s_r
],
s_y
,
mode
=
theano
.
Mode
(
linker
=
"cvm"
))
# f_cvm = theano.function([s_r], s_y, mode=theano.Mode(linker="cvm"))
#
t2
=
time
.
time
()
# cvm_duration = timeit(lambda: f_cvm(r), number=n_timeit)
f
(
r
)
t3
=
time
.
time
()
# XXX: Why does this take so much longer than Python?!
cvm_duration
=
t3
-
t2
# assert cvm_duration - python_duration < python_duration * 0.15
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论