Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
87d55476
提交
87d55476
authored
10月 19, 2015
作者:
carriepl
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make ScanInplaceOptimizer handle GpuAlloc and GpuAllocEmpty from both backends
上级
e6aecd41
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
29 行增加
和
9 行删除
+29
-9
scan_opt.py
theano/scan_module/scan_opt.py
+29
-9
没有找到文件。
theano/scan_module/scan_opt.py
浏览文件 @
87d55476
...
@@ -1010,7 +1010,7 @@ class ScanInplaceOptimizer(Optimizer):
...
@@ -1010,7 +1010,7 @@ class ScanInplaceOptimizer(Optimizer):
fgraph
.
attach_feature
(
toolbox
.
ReplaceValidate
())
fgraph
.
attach_feature
(
toolbox
.
ReplaceValidate
())
fgraph
.
attach_feature
(
DestroyHandler
())
fgraph
.
attach_feature
(
DestroyHandler
())
def
attempt_scan_inplace
(
self
,
fgraph
,
node
,
output_indices
):
def
attempt_scan_inplace
(
self
,
fgraph
,
node
,
output_indices
,
alloc_ops
):
"""Attempts to replace a Scan node by one which computes the specified
"""Attempts to replace a Scan node by one which computes the specified
outputs inplace.
outputs inplace.
...
@@ -1022,6 +1022,10 @@ class ScanInplaceOptimizer(Optimizer):
...
@@ -1022,6 +1022,10 @@ class ScanInplaceOptimizer(Optimizer):
Scan node to replace by an inplace version
Scan node to replace by an inplace version
output_indices : list of integers
output_indices : list of integers
Indices of the outputs to attempt to compute inplace
Indices of the outputs to attempt to compute inplace
alloc_ops : list of Op classes
Classes that represent operation that allocate new memory and
that the optimization should duplicate so it can operate inplace
on them.
"""
"""
op
=
node
.
op
op
=
node
.
op
...
@@ -1043,11 +1047,11 @@ class ScanInplaceOptimizer(Optimizer):
...
@@ -1043,11 +1047,11 @@ class ScanInplaceOptimizer(Optimizer):
ls_end
+=
op
.
outer_non_seqs
(
node
.
inputs
)
ls_end
+=
op
.
outer_non_seqs
(
node
.
inputs
)
# In `ls`, duplicate any input which has more then one client and is
# In `ls`, duplicate any input which has more then one client and is
#
an Alloc or an AllocEmpty
#
the output of an eligible allocation op
for
i
in
range
(
len
(
ls
)):
for
i
in
range
(
len
(
ls
)):
inp
=
ls
[
i
]
inp
=
ls
[
i
]
if
(
len
(
inp
.
clients
)
>
1
and
inp
.
owner
and
if
(
len
(
inp
.
clients
)
>
1
and
inp
.
owner
and
isinstance
(
inp
.
owner
.
op
,
(
Alloc
,
AllocEmpty
)
)):
isinstance
(
inp
.
owner
.
op
,
alloc_ops
)):
ls
[
i
]
=
inp
.
owner
.
op
(
*
inp
.
owner
.
inputs
)
ls
[
i
]
=
inp
.
owner
.
op
(
*
inp
.
owner
.
inputs
)
n_outs
=
len
(
ls
)
n_outs
=
len
(
ls
)
...
@@ -1080,6 +1084,21 @@ class ScanInplaceOptimizer(Optimizer):
...
@@ -1080,6 +1084,21 @@ class ScanInplaceOptimizer(Optimizer):
def
apply
(
self
,
fgraph
):
def
apply
(
self
,
fgraph
):
# Depending on the values of gpu_flag and gpua_flag, get the list of
# memory allocation ops that the optimization should be able to handle
alloc_ops
=
(
Alloc
,
AllocEmpty
)
if
self
.
gpu_flag
:
alloc_ops
+=
(
theano
.
sandbox
.
cuda
.
GpuAlloc
,
theano
.
sandbox
.
cuda
.
GpuAllocEmpty
)
if
self
.
gpua_flag
:
# gpuarray might be imported but not its GpuAlloc and
# GpuAllopEmpty ops.
try
:
alloc_ops
+=
(
theano
.
sandbox
.
gpuarray
.
GpuAlloc
,
theano
.
sandbox
.
gpuarray
.
GpuAllocEmpty
)
except
:
pass
nodes
=
fgraph
.
toposort
()[::
-
1
]
nodes
=
fgraph
.
toposort
()[::
-
1
]
scan_nodes
=
[
x
for
x
in
nodes
scan_nodes
=
[
x
for
x
in
nodes
if
(
isinstance
(
x
.
op
,
scan_op
.
Scan
)
and
if
(
isinstance
(
x
.
op
,
scan_op
.
Scan
)
and
...
@@ -1104,14 +1123,14 @@ class ScanInplaceOptimizer(Optimizer):
...
@@ -1104,14 +1123,14 @@ class ScanInplaceOptimizer(Optimizer):
inp_idx
=
1
+
op
.
n_seqs
+
out_idx
inp_idx
=
1
+
op
.
n_seqs
+
out_idx
inp
=
original_node
.
inputs
[
inp_idx
]
inp
=
original_node
.
inputs
[
inp_idx
]
# If the input is
an Alloc or an AllocEmpty, attempt to be
# If the input is
from an eligible allocation node, attempt to
# inplace on it, even if other nodes are modifying it
#
be
inplace on it, even if other nodes are modifying it
# inplace.
# inplace.
if
inp
.
owner
and
isinstance
(
inp
.
owner
.
op
,
(
Alloc
,
AllocEmpty
)
):
if
inp
.
owner
and
isinstance
(
inp
.
owner
.
op
,
alloc_ops
):
out_indices
.
append
(
out_idx
)
out_indices
.
append
(
out_idx
)
continue
continue
# If the input is n
either an Alloc or an AllocEmpty
, only
# If the input is n
ot from an eligible allocation node
, only
# attempt to be inplace on it if nothing else is currently
# attempt to be inplace on it if nothing else is currently
# inplace on it.
# inplace on it.
input_used_inplace
=
False
input_used_inplace
=
False
...
@@ -1134,14 +1153,15 @@ class ScanInplaceOptimizer(Optimizer):
...
@@ -1134,14 +1153,15 @@ class ScanInplaceOptimizer(Optimizer):
out_indices
.
append
(
out_idx
)
out_indices
.
append
(
out_idx
)
node
=
self
.
attempt_scan_inplace
(
fgraph
,
scan_nodes
[
scan_idx
],
node
=
self
.
attempt_scan_inplace
(
fgraph
,
scan_nodes
[
scan_idx
],
out_indices
)
out_indices
,
alloc_ops
)
if
node
is
original_node
:
if
node
is
original_node
:
# Making the scan compute all plausible recurrent outputs
# Making the scan compute all plausible recurrent outputs
# inplace has failed. Attempt all plausible recurrent output
# inplace has failed. Attempt all plausible recurrent output
# individually.
# individually.
for
pos
in
out_indices
:
for
pos
in
out_indices
:
node
=
self
.
attempt_scan_inplace
(
fgraph
,
node
,
[
pos
])
node
=
self
.
attempt_scan_inplace
(
fgraph
,
node
,
[
pos
],
alloc_ops
)
class
ScanSaveMem
(
gof
.
Optimizer
):
class
ScanSaveMem
(
gof
.
Optimizer
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论