Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5f2ae036
提交
5f2ae036
authored
6月 30, 2015
作者:
Caglar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
changed the order in the if condition and remove the while loop.
上级
6fe5f212
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
55 行删除
+46
-55
scan_opt.py
theano/scan_module/scan_opt.py
+46
-55
没有找到文件。
theano/scan_module/scan_opt.py
浏览文件 @
5f2ae036
...
@@ -206,7 +206,10 @@ def remove_constants_and_unused_inputs_scan(node):
...
@@ -206,7 +206,10 @@ def remove_constants_and_unused_inputs_scan(node):
# This is a global opt for historical reason
# This is a global opt for historical reason
# It should be possible to change it to a local opt.
# It should be possible to change it to a local opt.
class
PushOutNonSeqScan
(
gof
.
Optimizer
):
class
PushOutNonSeqScan
(
gof
.
Optimizer
):
"""
A global optimizer for pushing out the variables from scan that are not
used inside the scan.
"""
def
__init__
(
self
):
def
__init__
(
self
):
gof
.
Optimizer
.
__init__
(
self
)
gof
.
Optimizer
.
__init__
(
self
)
...
@@ -220,24 +223,31 @@ class PushOutNonSeqScan(gof.Optimizer):
...
@@ -220,24 +223,31 @@ class PushOutNonSeqScan(gof.Optimizer):
self
.
process_node
(
fgraph
,
node
)
self
.
process_node
(
fgraph
,
node
)
def
process_node
(
self
,
fgraph
,
node
):
def
process_node
(
self
,
fgraph
,
node
):
"""
Important note: This function uses set and dictionary data structure.
By default they are not ordered for efficiency reasons. Take care and make
sure of changing them to Ordered versions if you need to iterate over those
variables.
"""
# this flag tells if there was any change during the last iterations
# this flag tells if there was any change during the last iterations
changed
=
True
clean_inputs
,
clean_outputs
=
scan_utils
.
reconstruct_graph
(
clean_inputs
,
clean_outputs
=
scan_utils
.
reconstruct_graph
(
node
.
op
.
inputs
,
node
.
op
.
outputs
)
node
.
op
.
inputs
,
node
.
op
.
outputs
)
local_fgraph
=
gof
.
FunctionGraph
(
clean_inputs
,
clean_outputs
,
clone
=
False
)
local_fgraph
=
gof
.
FunctionGraph
(
clean_inputs
,
clean_outputs
,
clone
=
False
)
local_fgraph_topo
=
local_fgraph
.
toposort
()
local_fgraph_topo
=
local_fgraph
.
toposort
()
local_fgraph_outs_set
=
set
(
local_fgraph
.
outputs
)
local_fgraph_outs_set
=
set
(
local_fgraph
.
outputs
)
local_fgraph_outs_map
=
dict
([(
v
,
k
)
for
k
,
v
in
enumerate
(
local_fgraph
.
outputs
)])
local_fgraph_outs_map
=
dict
([(
v
,
k
)
for
k
,
v
in
enumerate
(
local_fgraph
.
outputs
)])
max_iterations
=
2
*
len
(
local_fgraph_topo
)
+
3
counts
=
0
to_remove_set
=
set
()
to_remove_set
=
set
()
to_remove_add
=
to_remove_set
.
add
to_remove_add
=
to_remove_set
.
add
to_replace_set
=
set
()
to_replace_set
=
set
()
to_replace_add
=
to_replace_set
.
add
to_replace_add
=
to_replace_set
.
add
to_replace_map
=
{}
to_replace_map
=
OrderedDict
()
nto_replace
=
0
nto_replace
=
0
def
add_to_replace
(
y
,
nto_replace
):
def
add_to_replace
(
y
,
nto_replace
):
to_replace_add
(
y
)
to_replace_add
(
y
)
to_replace_map
[
y
]
=
nto_replace
to_replace_map
[
y
]
=
nto_replace
...
@@ -245,6 +255,7 @@ class PushOutNonSeqScan(gof.Optimizer):
...
@@ -245,6 +255,7 @@ class PushOutNonSeqScan(gof.Optimizer):
replace_with_in
=
[]
replace_with_in
=
[]
replace_with_out
=
[]
replace_with_out
=
[]
op
=
node
.
op
op
=
node
.
op
# Construct the list of non_sequences to simplify a few things
# Construct the list of non_sequences to simplify a few things
inner_non_seqs
=
op
.
inner_non_seqs
(
clean_inputs
)
inner_non_seqs
=
op
.
inner_non_seqs
(
clean_inputs
)
...
@@ -259,22 +270,18 @@ class PushOutNonSeqScan(gof.Optimizer):
...
@@ -259,22 +270,18 @@ class PushOutNonSeqScan(gof.Optimizer):
assert
len
(
inner_non_seqs
)
==
len
(
outer_non_seqs
)
assert
len
(
inner_non_seqs
)
==
len
(
outer_non_seqs
)
assert
len
(
inner_seqs
)
==
len
(
outer_seqs
)
assert
len
(
inner_seqs
)
==
len
(
outer_seqs
)
while
changed
and
counts
<
max_iterations
:
counts
+=
1
changed
=
False
for
nd
in
local_fgraph_topo
:
for
nd
in
local_fgraph_topo
:
if
(
all
([(
x
in
inner_non_seqs_set
)
or
if
(
# we haven't already looked at this node
nd
not
in
to_remove_set
and
all
([((
x
in
inner_non_seqs_set
)
or
(
x
.
owner
in
to_remove_set
)
or
(
x
.
owner
in
to_remove_set
)
or
isinstance
(
x
,
tensor
.
Constant
)
isinstance
(
x
,
tensor
.
Constant
)
)
for
x
in
nd
.
inputs
])
and
for
x
in
nd
.
inputs
])
and
# we can do this because the assumption is that a
# we can do this because the assumption is that a
# viewOp or deepCopyOp will be just at the end of the
# viewOp or deepCopyOp will be just at the end of the
# function and not somewhere in the middle ..
# function and not somewhere in the middle ..
not
isinstance
(
nd
.
op
,
theano
.
compile
.
ViewOp
)
and
not
isinstance
(
nd
.
op
,
theano
.
compile
.
ViewOp
)
and
not
isinstance
(
nd
.
op
,
theano
.
compile
.
DeepCopyOp
)
and
not
isinstance
(
nd
.
op
,
theano
.
compile
.
DeepCopyOp
)):
# and we didn't already looked at this node
nd
not
in
to_remove_set
):
# We have a candidate node to removable
# We have a candidate node to removable
# Step 1. Reconstruct it on outside
# Step 1. Reconstruct it on outside
...
@@ -310,14 +317,8 @@ class PushOutNonSeqScan(gof.Optimizer):
...
@@ -310,14 +317,8 @@ class PushOutNonSeqScan(gof.Optimizer):
assert
isinstance
(
y
,
type
(
nw_outer_node
.
outputs
[
idx
]))
assert
isinstance
(
y
,
type
(
nw_outer_node
.
outputs
[
idx
]))
replace_with_out
.
append
(
nw_outer_node
.
outputs
[
idx
])
replace_with_out
.
append
(
nw_outer_node
.
outputs
[
idx
])
changed
=
True
if
counts
>=
max_iterations
:
raise
Exception
(
'Error in the `scan_pushout_non_seq_operations`.'
' The optimization exhausted the maximal number '
'of iterations allowed!'
)
# We need to check all candidate replacements and choose those that
# We need to check all candidate replacements and choose those that
# make sense for us
# make sense for us
# Step 1. which elements of `to_replace` are used by remaining
# Step 1. which elements of `to_replace` are used by remaining
# components of the inner function
# components of the inner function
clean_to_replace
=
[]
clean_to_replace
=
[]
...
@@ -330,11 +331,11 @@ class PushOutNonSeqScan(gof.Optimizer):
...
@@ -330,11 +331,11 @@ class PushOutNonSeqScan(gof.Optimizer):
to_keep_set
=
set
(
to_keep
)
to_keep_set
=
set
(
to_keep
)
for
out
,
idx
in
to_replace_map
.
items
():
for
out
,
idx
in
to_replace_map
.
items
():
if
(
out
in
to_keep_set
if
(
# If types are different, conversion Op will be inserted,
and
out
.
owner
not
in
existent_nodes_set
# If types are different, conversion Op will be inserted,
# and it may trigger an infinite loop.
# and it may trigger an infinite loop.
and
replace_with_in
[
idx
]
.
type
==
out
.
type
):
replace_with_in
[
idx
]
.
type
==
out
.
type
and
out
in
to_keep_set
and
out
.
owner
not
in
existent_nodes_set
):
clean_to_replace
.
append
(
out
)
clean_to_replace
.
append
(
out
)
clean_replace_with_in
.
append
(
replace_with_in
[
idx
])
clean_replace_with_in
.
append
(
replace_with_in
[
idx
])
clean_replace_with_out
.
append
(
replace_with_out
[
idx
])
clean_replace_with_out
.
append
(
replace_with_out
[
idx
])
...
@@ -356,6 +357,7 @@ class PushOutNonSeqScan(gof.Optimizer):
...
@@ -356,6 +357,7 @@ class PushOutNonSeqScan(gof.Optimizer):
_op_outs
=
scan_utils
.
clone
(
clean_outputs
,
_op_outs
=
scan_utils
.
clone
(
clean_outputs
,
replace
=
givens
)
replace
=
givens
)
_op_ins
=
clean_inputs
+
nw_inner
_op_ins
=
clean_inputs
+
nw_inner
op_ins
,
op_outs
=
scan_utils
.
reconstruct_graph
(
_op_ins
,
_op_outs
)
op_ins
,
op_outs
=
scan_utils
.
reconstruct_graph
(
_op_ins
,
_op_outs
)
# Reconstruct node
# Reconstruct node
...
@@ -398,7 +400,10 @@ class PushOutNonSeqScan(gof.Optimizer):
...
@@ -398,7 +400,10 @@ class PushOutNonSeqScan(gof.Optimizer):
# This is a global opt for historical reason
# This is a global opt for historical reason
# It should be possible to change it to a local opt.
# It should be possible to change it to a local opt.
class
PushOutSeqScan
(
gof
.
Optimizer
):
class
PushOutSeqScan
(
gof
.
Optimizer
):
"""
A global optimizer for pushing out the input variables that are not being
used inside the scan and provided in the sequences.
"""
def
__init__
(
self
):
def
__init__
(
self
):
gof
.
Optimizer
.
__init__
(
self
)
gof
.
Optimizer
.
__init__
(
self
)
...
@@ -412,8 +417,13 @@ class PushOutSeqScan(gof.Optimizer):
...
@@ -412,8 +417,13 @@ class PushOutSeqScan(gof.Optimizer):
self
.
process_node
(
fgraph
,
node
)
self
.
process_node
(
fgraph
,
node
)
def
process_node
(
self
,
fgraph
,
node
):
def
process_node
(
self
,
fgraph
,
node
):
"""
Important note: This function uses set and dictionary data structure.
By default they are not ordered for efficiency reasons. Take care and make
sure of changing them to Ordered versions if you need to iterate over those
variables.
"""
# this flag tells if there was any change during the last iterations
# this flag tells if there was any change during the last iterations
changed
=
True
clean_inputs
,
clean_outputs
=
scan_utils
.
reconstruct_graph
(
clean_inputs
,
clean_outputs
=
scan_utils
.
reconstruct_graph
(
node
.
op
.
inputs
,
node
.
op
.
outputs
)
node
.
op
.
inputs
,
node
.
op
.
outputs
)
...
@@ -422,14 +432,11 @@ class PushOutSeqScan(gof.Optimizer):
...
@@ -422,14 +432,11 @@ class PushOutSeqScan(gof.Optimizer):
local_fgraph_outs_set
=
set
(
local_fgraph
.
outputs
)
local_fgraph_outs_set
=
set
(
local_fgraph
.
outputs
)
local_fgraph_outs_map
=
dict
([(
v
,
k
)
for
k
,
v
in
enumerate
(
local_fgraph
.
outputs
)])
local_fgraph_outs_map
=
dict
([(
v
,
k
)
for
k
,
v
in
enumerate
(
local_fgraph
.
outputs
)])
max_iterations
=
2
*
len
(
local_fgraph_topo
)
+
3
counts
=
0
to_remove_set
=
set
()
to_remove_set
=
set
()
to_remove_add
=
to_remove_set
.
add
to_remove_add
=
to_remove_set
.
add
to_replace_set
=
set
()
to_replace_set
=
set
()
to_replace_add
=
to_replace_set
.
add
to_replace_add
=
to_replace_set
.
add
to_replace_map
=
{}
to_replace_map
=
OrderedDict
()
nto_replace
=
0
nto_replace
=
0
def
add_to_replace
(
y
,
nto_replace
):
def
add_to_replace
(
y
,
nto_replace
):
...
@@ -455,18 +462,14 @@ class PushOutSeqScan(gof.Optimizer):
...
@@ -455,18 +462,14 @@ class PushOutSeqScan(gof.Optimizer):
assert
len
(
inner_non_seqs
)
==
len
(
outer_non_seqs
)
assert
len
(
inner_non_seqs
)
==
len
(
outer_non_seqs
)
assert
len
(
inner_seqs
)
==
len
(
outer_seqs
)
assert
len
(
inner_seqs
)
==
len
(
outer_seqs
)
while
changed
and
counts
<
max_iterations
:
counts
+=
1
changed
=
False
for
nd
in
local_fgraph_topo
:
for
nd
in
local_fgraph_topo
:
if
(
isinstance
(
nd
.
op
,
theano
.
tensor
.
Elemwise
)
and
if
(
nd
not
in
to_remove_set
and
all
([(
x
in
inner_non_seqs_set
)
or
all
([(
x
in
inner_non_seqs_set
)
or
(
x
.
owner
in
to_remove_set
)
or
(
x
.
owner
in
to_remove_set
)
or
isinstance
(
x
,
tensor
.
Constant
)
or
isinstance
(
x
,
tensor
.
Constant
)
or
(
x
in
inner_seqs_set
)
(
x
in
inner_seqs_set
)
for
x
in
nd
.
inputs
])
and
for
x
in
nd
.
inputs
])
and
isinstance
(
nd
.
op
,
theano
.
tensor
.
Elemwise
)):
nd
not
in
to_remove_set
):
to_remove_add
(
nd
)
to_remove_add
(
nd
)
outside_ins
=
[]
outside_ins
=
[]
depends_on_seqs
=
False
depends_on_seqs
=
False
...
@@ -511,12 +514,11 @@ class PushOutSeqScan(gof.Optimizer):
...
@@ -511,12 +514,11 @@ class PushOutSeqScan(gof.Optimizer):
replace_with_in
.
append
(
y_place_holder
)
replace_with_in
.
append
(
y_place_holder
)
replace_with_out
.
append
(
nw_outer_node
.
outputs
[
idx
])
replace_with_out
.
append
(
nw_outer_node
.
outputs
[
idx
])
changed
=
True
elif
(
nd
not
in
to_remove_set
and
isinstance
(
nd
.
op
,
theano
.
tensor
.
DimShuffle
)
and
elif
(
isinstance
(
nd
.
op
,
theano
.
tensor
.
DimShuffle
)
and
(
nd
.
inputs
[
0
]
in
inner_seqs_set
or
(
nd
.
inputs
[
0
]
in
inner_seqs_set
or
nd
.
inputs
[
0
]
.
owner
in
to_remove_set
)
and
nd
.
inputs
[
0
]
.
owner
in
to_remove_set
)):
not
nd
in
to_remove_set
):
to_remove_add
(
nd
)
to_remove_add
(
nd
)
x
=
nd
.
inputs
[
0
]
x
=
nd
.
inputs
[
0
]
if
x
in
inner_seqs_set
:
if
x
in
inner_seqs_set
:
...
@@ -542,15 +544,8 @@ class PushOutSeqScan(gof.Optimizer):
...
@@ -542,15 +544,8 @@ class PushOutSeqScan(gof.Optimizer):
ref_sh
+=
nd
.
outputs
[
0
]
.
tag
.
test_value
.
shape
ref_sh
+=
nd
.
outputs
[
0
]
.
tag
.
test_value
.
shape
assert
new_sh
==
ref_sh
assert
new_sh
==
ref_sh
changed
=
True
if
counts
>=
max_iterations
:
raise
Exception
(
'Error in the `scan_pushout_seq_operations`.'
' The optimization exhausted the maximal number '
'of iterations allowed!'
)
# We need to check all candidate replacements and choose those that
# We need to check all candidate replacements and choose those that
# make sense for us
# make sense for us
# Step 1. which elements of `to_replace` are used by remaining
# Step 1. which elements of `to_replace` are used by remaining
# components of the inner function
# components of the inner function
clean_to_replace
=
[]
clean_to_replace
=
[]
...
@@ -684,10 +679,8 @@ class PushOutScanOutput(gof.Optimizer):
...
@@ -684,10 +679,8 @@ class PushOutScanOutput(gof.Optimizer):
new_scan_node
=
None
new_scan_node
=
None
local_fgraph_topo
=
local_fgraph
.
toposort
()
local_fgraph_topo
=
local_fgraph
.
toposort
()
for
nd
in
local_fgraph_topo
:
for
nd
in
local_fgraph_topo
:
if
(
isinstance
(
nd
.
op
,
theano
.
tensor
.
Dot
)
and
if
(
isinstance
(
nd
.
op
,
theano
.
tensor
.
Dot
)
and
nd
.
out
in
args
.
inner_out_nit_sot
):
nd
.
out
in
args
.
inner_out_nit_sot
):
"""
"""
The following optimization involves pushing out, after the
The following optimization involves pushing out, after the
scan, a Dot whose output is nitsot (not feed back to the inner
scan, a Dot whose output is nitsot (not feed back to the inner
...
@@ -857,9 +850,7 @@ class PushOutScanOutput(gof.Optimizer):
...
@@ -857,9 +850,7 @@ class PushOutScanOutput(gof.Optimizer):
outer_var
=
scan_args
.
outer_out_sit_sot
[
idx
]
outer_var
=
scan_args
.
outer_out_sit_sot
[
idx
]
if
len
(
outer_var
.
clients
)
==
1
:
if
len
(
outer_var
.
clients
)
==
1
:
client
=
outer_var
.
clients
[
0
][
0
]
client
=
outer_var
.
clients
[
0
][
0
]
if
(
client
!=
'output'
and
if
(
client
!=
'output'
and
isinstance
(
client
.
op
,
theano
.
tensor
.
Subtensor
)):
isinstance
(
client
.
op
,
theano
.
tensor
.
Subtensor
)):
lst
=
theano
.
tensor
.
subtensor
.
get_idx_list
(
lst
=
theano
.
tensor
.
subtensor
.
get_idx_list
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论