Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e93c61d1
提交
e93c61d1
authored
3月 25, 2014
作者:
abergeron
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1779 from nouiz/faster_opt
Faster opt
上级
8cc9395f
08d61b24
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
71 行增加
和
67 行删除
+71
-67
opt.py
theano/gof/opt.py
+21
-18
optdb.py
theano/gof/optdb.py
+14
-4
opt.py
theano/sandbox/cuda/opt.py
+0
-0
blas.py
theano/tensor/blas.py
+36
-45
没有找到文件。
theano/gof/opt.py
浏览文件 @
e93c61d1
...
...
@@ -1047,10 +1047,6 @@ class PatternSub(LocalOptimizer):
self
.
__name__
=
name
self
.
pdb
=
pdb
def
skip_identities
(
self
,
expr
):
if
self
.
skip_identities_fn
:
return
self
.
skip_identities_fn
(
expr
)
def
op_key
(
self
):
return
self
.
op
...
...
@@ -1064,10 +1060,13 @@ class PatternSub(LocalOptimizer):
"""
if
node
.
op
!=
self
.
op
:
return
False
#TODO: if we remove pdb, do this speed things up?
def
match
(
pattern
,
expr
,
u
,
allow_multiple_clients
=
False
,
pdb
=
False
):
#TODO move outside match
def
retry_with_equiv
():
expr_equiv
=
self
.
skip_identities
(
expr
)
if
not
self
.
skip_identities_fn
:
return
False
expr_equiv
=
self
.
skip_identities_fn
(
expr
)
if
expr_equiv
is
None
:
return
False
#TODO: Not sure how to handle multiple_clients flag
...
...
@@ -1126,19 +1125,19 @@ class PatternSub(LocalOptimizer):
pdb
.
set_trace
()
return
u
def
build
(
pattern
,
u
):
if
isinstance
(
pattern
,
(
list
,
tuple
)):
args
=
[
build
(
p
,
u
)
for
p
in
pattern
[
1
:]]
return
pattern
[
0
](
*
args
)
elif
isinstance
(
pattern
,
basestring
):
return
u
[
unify
.
Var
(
pattern
)]
elif
isinstance
(
pattern
,
(
int
,
float
)):
return
pattern
else
:
return
pattern
.
clone
()
u
=
match
(
self
.
in_pattern
,
node
.
out
,
unify
.
Unification
(),
True
,
self
.
pdb
)
if
u
:
def
build
(
pattern
,
u
):
if
isinstance
(
pattern
,
(
list
,
tuple
)):
args
=
[
build
(
p
,
u
)
for
p
in
pattern
[
1
:]]
return
pattern
[
0
](
*
args
)
elif
isinstance
(
pattern
,
basestring
):
return
u
[
unify
.
Var
(
pattern
)]
elif
isinstance
(
pattern
,
(
int
,
float
)):
return
pattern
else
:
return
pattern
.
clone
()
p
=
self
.
out_pattern
new
=
build
(
p
,
u
)
####print "PatternSub matched:", new
...
...
@@ -1520,19 +1519,23 @@ class EquilibriumOptimizer(NavigatorOptimizer):
def
__init__
(
self
,
optimizers
,
failure_callback
=
None
,
ignore_newtrees
=
True
,
max_use_ratio
=
None
):
"""
""" Apply optimizations until equilibrium point.
:param optimizers: list or set of local or global optimizations to
apply until equilibrium.
:param max_use_ratio: each optimizer can be applied at most
(size of graph * this number) times
:param ignore_newtrees: See EquilibriumDB ignore_newtrees
parameter definition
"""
super
(
EquilibriumOptimizer
,
self
)
.
__init__
(
None
,
ignore_newtrees
=
True
,
ignore_newtrees
=
ignore_newtrees
,
failure_callback
=
failure_callback
)
self
.
local_optimizers_map
=
dict
()
self
.
local_optimizers_all
=
[]
...
...
theano/gof/optdb.py
浏览文件 @
e93c61d1
...
...
@@ -179,23 +179,33 @@ class Query(object):
class
EquilibriumDB
(
DB
):
"""
A set of potential optimizations which should be applied in an
"""A set of potential optimizations which should be applied in an
arbitrary order until equilibrium is reached.
Canonicalize, Stabilize, and Specialize are all equilibrium optimizations.
:param ignore_newtrees: If False, we will apply local opt on new
node introduced during local optimization application. This
could result in less fgraph iterations, but this don't mean it
will be faster globally.
.. note::
We can put LocalOptimizer and Optimizer as EquilibriumOptimizer
suppor both.
"""
def
__init__
(
self
,
ignore_newtrees
=
True
):
super
(
EquilibriumDB
,
self
)
.
__init__
()
self
.
ignore_newtrees
=
ignore_newtrees
def
query
(
self
,
*
tags
,
**
kwtags
):
opts
=
super
(
EquilibriumDB
,
self
)
.
query
(
*
tags
,
**
kwtags
)
return
opt
.
EquilibriumOptimizer
(
opts
,
max_use_ratio
=
config
.
optdb
.
max_use_ratio
,
failure_callback
=
opt
.
NavigatorOptimizer
.
warn_inplace
)
return
opt
.
EquilibriumOptimizer
(
opts
,
max_use_ratio
=
config
.
optdb
.
max_use_ratio
,
ignore_newtrees
=
self
.
ignore_newtrees
,
failure_callback
=
opt
.
NavigatorOptimizer
.
warn_inplace
)
class
SequenceDB
(
DB
):
...
...
theano/sandbox/cuda/opt.py
浏览文件 @
e93c61d1
差异被折叠。
点击展开。
theano/tensor/blas.py
浏览文件 @
e93c61d1
...
...
@@ -1190,32 +1190,31 @@ def _beta_L_plus_alpha_M(beta, L, alpha, M, recurse_flip=True):
# it also might be the case that there is a dimshuffle between the +
# and the dot22. local_dot_to_dot22 in particular will put in such things.
if
M
.
owner
and
isinstance
(
M
.
owner
.
op
,
T
.
DimShuffle
):
if
(
M
.
owner
and
isinstance
(
M
.
owner
.
op
,
T
.
DimShuffle
)
and
M
.
owner
.
inputs
[
0
]
.
owner
and
isinstance
(
M
.
owner
.
inputs
[
0
]
.
owner
.
op
,
Dot22
)):
MM
=
M
.
owner
.
inputs
[
0
]
if
tuple
(
M
.
owner
.
op
.
new_order
)
==
(
0
,):
if
M
.
owner
.
op
.
new_order
==
(
0
,):
# it is making a column MM into a vector
if
MM
.
owner
and
MM
.
owner
.
op
==
_dot22
:
MMl
,
MMr
=
MM
.
owner
.
inputs
g
=
gemm_no_inplace
(
L
.
dimshuffle
(
0
,
'x'
),
alpha
,
MMl
,
MMr
,
beta
)
rval
=
[
g
.
dimshuffle
(
0
)]
return
rval
,
MM
if
tuple
(
M
.
owner
.
op
.
new_order
)
==
(
1
,):
MMl
,
MMr
=
MM
.
owner
.
inputs
g
=
gemm_no_inplace
(
L
.
dimshuffle
(
0
,
'x'
),
alpha
,
MMl
,
MMr
,
beta
)
rval
=
[
g
.
dimshuffle
(
0
)]
return
rval
,
MM
if
M
.
owner
.
op
.
new_order
==
(
1
,):
# it is making a row MM into a vector
if
MM
.
owner
and
MM
.
owner
.
op
==
_dot22
:
MMl
,
MMr
=
MM
.
owner
.
inputs
g
=
gemm_no_inplace
(
L
.
dimshuffle
(
'x'
,
0
),
alpha
,
MMl
,
MMr
,
beta
)
rval
=
[
g
.
dimshuffle
(
1
)]
return
rval
,
MM
if
tuple
(
M
.
owner
.
op
.
new_order
)
==
():
MMl
,
MMr
=
MM
.
owner
.
inputs
g
=
gemm_no_inplace
(
L
.
dimshuffle
(
'x'
,
0
),
alpha
,
MMl
,
MMr
,
beta
)
rval
=
[
g
.
dimshuffle
(
1
)]
return
rval
,
MM
if
len
(
M
.
owner
.
op
.
new_order
)
==
0
:
# it is making a row MM into a vector
if
MM
.
owner
and
MM
.
owner
.
op
==
_dot22
:
MMl
,
MMr
=
MM
.
owner
.
inputs
g
=
gemm_no_inplace
(
L
.
dimshuffle
(
'x'
,
'x'
),
alpha
,
MMl
,
MMr
,
beta
)
rval
=
[
g
.
dimshuffle
()]
return
rval
,
MM
MMl
,
MMr
=
MM
.
owner
.
inputs
g
=
gemm_no_inplace
(
L
.
dimshuffle
(
'x'
,
'x'
),
alpha
,
MMl
,
MMr
,
beta
)
rval
=
[
g
.
dimshuffle
()]
return
rval
,
MM
# this is False'd out because of inadequate testing.
# TODO see ticket #237
...
...
@@ -1379,29 +1378,31 @@ def _gemm_from_factored_list(lst):
"""Returns None, or a list to replace node.outputs
"""
# Make every pair in list have matching dtypes
# sM can be a tuple of 2 elements or a theano variable.
# We should not use __len__ as theano variables don't support
# it. I don't want to change this to isinstance(sM, tuple)
# as I'm not able to make a test that triggers this case.
def
is_pair
(
sM
):
try
:
s
,
M
=
sM
return
True
except
Exception
:
return
False
lst2
=
[]
# Remove the tuple that can't be cast correctly.
# This can happen when we try to cast a complex to a real
for
sM
in
lst
:
if
is_pair
(
sM
):
# Make every pair in list have matching dtypes
# sM can be a tuple of 2 elements or a theano variable.
if
isinstance
(
sM
,
tuple
):
sm0
,
sm1
=
sM
sm0
=
T
.
as_tensor_variable
(
sm0
)
if
theano
.
scalar
.
upcast
(
sm0
.
dtype
,
sm1
.
dtype
)
==
sm1
.
dtype
:
lst2
.
append
((
T
.
cast
(
sm0
,
sm1
.
dtype
),
sM
[
1
]))
lst
=
lst2
def
item_to_var
(
t
):
try
:
s
,
M
=
t
except
Exception
:
return
t
if
s
==
1
:
return
M
if
s
==
-
1
:
return
-
M
return
s
*
M
# Try every pair in the sM_list, trying to turn it into a gemm operation
for
i
in
xrange
(
len
(
lst
)
-
1
):
s_i
,
M_i
=
lst
[
i
]
...
...
@@ -1418,16 +1419,6 @@ def _gemm_from_factored_list(lst):
s_j
,
M_j
)
#print 'GOT IT', gemm_of_sM_list
if
gemm_of_sM_list
:
def
item_to_var
(
t
):
try
:
s
,
M
=
t
except
Exception
:
return
t
if
s
==
1
:
return
M
if
s
==
-
1
:
return
-
M
return
s
*
M
assert
len
(
gemm_of_sM_list
)
==
1
add_inputs
=
[
item_to_var
(
input
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论