Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
29783b30
提交
29783b30
authored
7月 19, 2011
作者:
Razvan Pascanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixed ScanSaveMem optimization to deal with the inputs being allocs, not
set_subtensor. This was the cause of the 3 failing test in scan. I also revised the entire optimization, writing a bit more comments/
上级
5548fee1
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
42 行增加
和
9 行删除
+42
-9
scan_opt.py
theano/scan_module/scan_opt.py
+42
-9
没有找到文件。
theano/scan_module/scan_opt.py
浏览文件 @
29783b30
...
@@ -561,15 +561,37 @@ class ScanSaveMem(gof.Optimizer):
...
@@ -561,15 +561,37 @@ class ScanSaveMem(gof.Optimizer):
# If the memory for this output has been pre-allocated
# If the memory for this output has been pre-allocated
# before going into the scan op (by an alloc node)
# before going into the scan op (by an alloc node)
if
idx
<
op
.
n_mit_sot
+
op
.
n_sit_sot
:
if
idx
<
op
.
n_mit_sot
+
op
.
n_sit_sot
:
# In case the input is still an alloc node
# In case the input is still an alloc node, we
if
nw_inputs
[
offset
+
idx
]
.
owner
:
# actually have two options:
# a) the input is an alloc (due to an optimization
# that converts set_subtensor(0,0) in 0
# b) the input is an set subtensor
if
(
nw_inputs
[
offset
+
idx
]
.
owner
and
isinstance
(
nw_inputs
[
offset
+
idx
]
.
owner
.
op
,
tensor
.
IncSubtensor
)):
_nw_input
=
nw_inputs
[
offset
+
idx
]
.
owner
.
inputs
[
1
]
_nw_input
=
nw_inputs
[
offset
+
idx
]
.
owner
.
inputs
[
1
]
nw_input
=
scan_utils
.
expand
(
_nw_input
,
val
-
init_l
[
i
]
)
tmp
=
pre_greedy_local_optimizer
(
list_opt_slice
,
tensor
.
as_tensor_variable
(
val
-
init_l
[
i
]))
tmp
=
pre_constant_merge
([
tmp
])[
0
]
nw_input
=
scan_utils
.
expand
(
_nw_input
,
tmp
)
# If it is an alloc
elif
(
nw_inputs
[
offset
+
idx
]
.
owner
and
isinstance
(
nw_inputs
[
offset
+
idx
]
.
owner
.
op
,
tensor
.
Alloc
)):
tmp
=
pre_greedy_local_optimizer
(
list_opt_slice
,
tensor
.
as_tensor_variable
(
val
))
tmp
=
pre_constant_merge
([
tmp
])[
0
]
nw_input
=
nw_inputs
[
offset
+
idx
][:
tmp
]
# Else, if it was constant folded to a single value
# Else, if it was constant folded to a single value
elif
isinstance
(
nw_inputs
[
offset
+
idx
],
tensor
.
Constant
):
elif
isinstance
(
nw_inputs
[
offset
+
idx
],
tensor
.
Constant
):
# The hope is that constant folding will fold
# The hope is that constant folding will fold
# this as well
# this as well
nw_input
=
nw_inputs
[
offset
+
idx
][:
val
]
tmp
=
pre_greedy_local_optimizer
(
list_opt_slice
,
tensor
.
as_tensor_variable
(
val
))
tmp
=
pre_constant_merge
([
tmp
])[
0
]
nw_input
=
nw_inputs
[
offset
+
idx
][:
tmp
]
else
:
else
:
raise
Exception
((
'Unforseen case. Please report'
raise
Exception
((
'Unforseen case. Please report'
' to theano-dev with an example'
' to theano-dev with an example'
...
@@ -613,7 +635,17 @@ class ScanSaveMem(gof.Optimizer):
...
@@ -613,7 +635,17 @@ class ScanSaveMem(gof.Optimizer):
# 3.5 Remove unwanted orphane outputs
# 3.5 Remove unwanted orphane outputs
(
inps
,
outs
,
info
,
node_ins
,
compress_map
)
=
\
(
inps
,
outs
,
info
,
node_ins
,
compress_map
)
=
\
scan_utils
.
compress_outs
(
op
,
not_required
,
nw_inputs
)
scan_utils
.
compress_outs
(
op
,
not_required
,
nw_inputs
)
inv_compress_map
=
{}
for
k
,
v
in
compress_map
.
items
():
inv_compress_map
[
v
]
=
k
node_ins
=
[
pre_greedy_local_optimizer
(
list_opt_slice
,
x
)
for
x
in
node_ins
]
node_ins
=
pre_constant_merge
(
node_ins
)
# 3.6 Compose the new scan
# 3.6 Compose the new scan
# I need to make sure I'm not reapplying the same optimization
# twice since bad things usually happen if I do that
info
[
'_scan_merge_visited'
]
=
True
new_outs
=
scan_op
.
Scan
(
inps
new_outs
=
scan_op
.
Scan
(
inps
,
outs
,
outs
,
info
)
.
make_node
(
*
node_ins
)
.
outputs
,
info
)
.
make_node
(
*
node_ins
)
.
outputs
...
@@ -641,7 +673,7 @@ class ScanSaveMem(gof.Optimizer):
...
@@ -641,7 +673,7 @@ class ScanSaveMem(gof.Optimizer):
nw_slice
=
(
fslice
,)
+
tuple
(
old_slices
[
1
:])
nw_slice
=
(
fslice
,)
+
tuple
(
old_slices
[
1
:])
nw_pos
=
compress_map
[
idx
]
nw_pos
=
inv_
compress_map
[
idx
]
nw_out
=
new_outs
[
nw_pos
]
nw_out
=
new_outs
[
nw_pos
]
...
@@ -660,6 +692,7 @@ class ScanSaveMem(gof.Optimizer):
...
@@ -660,6 +692,7 @@ class ScanSaveMem(gof.Optimizer):
# 3.8. Get replace pairs for those outputs that change
# 3.8. Get replace pairs for those outputs that change
# the number of stored intermediate steps
# the number of stored intermediate steps
for
pos
,
old_outs
in
old_outputs
:
for
pos
,
old_outs
in
old_outputs
:
if
len
(
old_outs
)
>
0
:
nw_pos
=
compress_map
[
pos
]
nw_pos
=
compress_map
[
pos
]
nw_out
=
new_outs
[
nw_pos
]
nw_out
=
new_outs
[
nw_pos
]
for
k
,
old
in
enumerate
(
old_outs
):
for
k
,
old
in
enumerate
(
old_outs
):
...
@@ -707,11 +740,11 @@ class ScanSaveMem(gof.Optimizer):
...
@@ -707,11 +740,11 @@ class ScanSaveMem(gof.Optimizer):
def
apply
(
self
,
env
):
def
apply
(
self
,
env
):
nodelist
=
list
(
env
.
toposort
())
old_new
=
[]
nodelist
=
[
x
for
x
in
env
.
toposort
()
if
isinstance
(
x
.
op
,
scan_op
.
Scan
)]
for
node
in
nodelist
:
for
node
in
nodelist
:
op
=
node
.
op
if
not
hasattr
(
node
.
op
,
'_scan_merge_visited'
):
if
isinstance
(
op
,
scan_op
.
Scan
):
self
.
process_node
(
env
,
node
)
self
.
process_node
(
env
,
node
)
# Just before specialize to have the other optimization
# Just before specialize to have the other optimization
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论