Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
00e3d80b
提交
00e3d80b
authored
2月 25, 2011
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
white space fix.
上级
80ccfeff
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
23 行增加
和
24 行删除
+23
-24
destroyhandler.py
theano/gof/destroyhandler.py
+23
-24
没有找到文件。
theano/gof/destroyhandler.py
浏览文件 @
00e3d80b
"""WRITEME"""
"""WRITEME"""
import
sys
import
sys
if
sys
.
version_info
[:
2
]
>=
(
2
,
5
):
if
sys
.
version_info
[:
2
]
>=
(
2
,
5
):
from
collections
import
defaultdict
from
collections
import
defaultdict
# otherwise it's implemented in python25.py
# otherwise it's implemented in python25.py
...
@@ -12,7 +12,7 @@ from theano.gof import deque
...
@@ -12,7 +12,7 @@ from theano.gof import deque
from
env
import
InconsistencyError
from
env
import
InconsistencyError
class
ProtocolError
(
Exception
):
class
ProtocolError
(
Exception
):
"""WRITEME"""
"""WRITEME"""
pass
pass
...
@@ -35,7 +35,7 @@ class DestroyHandler(object):
...
@@ -35,7 +35,7 @@ class DestroyHandler(object):
def
on_prune
(
self
,
env
,
op
):
def
on_prune
(
self
,
env
,
op
):
self
.
map
[
env
]
.
on_prune
(
env
,
op
)
self
.
map
[
env
]
.
on_prune
(
env
,
op
)
def
on_change_input
(
self
,
env
,
node
,
i
,
r
,
new_r
):
def
on_change_input
(
self
,
env
,
node
,
i
,
r
,
new_r
):
self
.
map
[
env
]
.
on_change_input
(
env
,
node
,
i
,
r
,
new_r
)
self
.
map
[
env
]
.
on_change_input
(
env
,
node
,
i
,
r
,
new_r
)
...
@@ -65,10 +65,10 @@ def _dfs_toposort(i, r_out, orderings):
...
@@ -65,10 +65,10 @@ def _dfs_toposort(i, r_out, orderings):
iset
=
set
(
i
)
iset
=
set
(
i
)
if
0
:
if
0
:
def
expand
(
obj
):
def
expand
(
obj
):
rval
=
[]
rval
=
[]
if
obj
not
in
iset
:
if
obj
not
in
iset
:
if
isinstance
(
obj
,
graph
.
Variable
):
if
isinstance
(
obj
,
graph
.
Variable
):
if
obj
.
owner
:
if
obj
.
owner
:
rval
=
[
obj
.
owner
]
rval
=
[
obj
.
owner
]
if
isinstance
(
obj
,
graph
.
Apply
):
if
isinstance
(
obj
,
graph
.
Apply
):
...
@@ -137,7 +137,7 @@ def getroot(r, view_i):
...
@@ -137,7 +137,7 @@ def getroot(r, view_i):
For views: Return non-view variable which is ultimatly viewed by r.
For views: Return non-view variable which is ultimatly viewed by r.
For non-views: return self.
For non-views: return self.
"""
"""
try
:
try
:
return
getroot
(
view_i
[
r
],
view_i
)
return
getroot
(
view_i
[
r
],
view_i
)
except
KeyError
:
except
KeyError
:
return
r
return
r
...
@@ -170,7 +170,7 @@ def fast_inplace_check(inputs):
...
@@ -170,7 +170,7 @@ def fast_inplace_check(inputs):
protected_inputs
=
sum
(
protected_inputs
,[])
#flatten the list
protected_inputs
=
sum
(
protected_inputs
,[])
#flatten the list
protected_inputs
.
extend
(
env
.
outputs
)
protected_inputs
.
extend
(
env
.
outputs
)
inputs
=
[
i
for
i
in
inputs
if
inputs
=
[
i
for
i
in
inputs
if
not
isinstance
(
i
,
graph
.
Constant
)
not
isinstance
(
i
,
graph
.
Constant
)
and
not
env
.
destroyers
(
i
)
and
not
env
.
destroyers
(
i
)
and
i
not
in
protected_inputs
]
and
i
not
in
protected_inputs
]
...
@@ -185,7 +185,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -185,7 +185,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
When an Op uses its view_map property to declare that an output may be aliased
When an Op uses its view_map property to declare that an output may be aliased
to an input, then if that output is destroyed, the input is also considering to be
to an input, then if that output is destroyed, the input is also considering to be
destroyed. The view_maps of several Ops can feed into one another and form a directed graph.
destroyed. The view_maps of several Ops can feed into one another and form a directed graph.
The consequence of destroying any variable in such a graph is that all variables in the graph
The consequence of destroying any variable in such a graph is that all variables in the graph
must be considered to be destroyed, because they could all be refering to the same
must be considered to be destroyed, because they could all be refering to the same
underlying storage. In the current implementation, that graph is a tree, and the root of
underlying storage. In the current implementation, that graph is a tree, and the root of
...
@@ -195,7 +195,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -195,7 +195,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
the foundation of that variable as being destroyed, with the `root_destroyer` property.
the foundation of that variable as being destroyed, with the `root_destroyer` property.
"""
"""
droot
=
{}
droot
=
{}
"""
"""
destroyed view + nonview variables -> foundation
destroyed view + nonview variables -> foundation
"""
"""
...
@@ -237,7 +237,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -237,7 +237,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
self
.
view_i
=
{}
# variable -> variable used in calculation
self
.
view_i
=
{}
# variable -> variable used in calculation
self
.
view_o
=
{}
# variable -> set of variables that use this one as a direct input
self
.
view_o
=
{}
# variable -> set of variables that use this one as a direct input
#clients: how many times does an apply use a given variable
#clients: how many times does an apply use a given variable
self
.
clients
=
{}
# variable -> apply -> ninputs
self
.
clients
=
{}
# variable -> apply -> ninputs
self
.
stale_droot
=
True
self
.
stale_droot
=
True
self
.
debug_all_apps
=
set
()
self
.
debug_all_apps
=
set
()
...
@@ -290,7 +290,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -290,7 +290,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
delattr
(
self
.
env
,
'destroyers'
)
delattr
(
self
.
env
,
'destroyers'
)
delattr
(
self
.
env
,
'destroy_handler'
)
delattr
(
self
.
env
,
'destroy_handler'
)
self
.
env
=
None
self
.
env
=
None
def
on_import
(
self
,
env
,
app
):
def
on_import
(
self
,
env
,
app
):
"""Add Apply instance to set which must be computed"""
"""Add Apply instance to set which must be computed"""
...
@@ -318,7 +318,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -318,7 +318,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
for
i
,
output
in
enumerate
(
app
.
outputs
):
for
i
,
output
in
enumerate
(
app
.
outputs
):
self
.
clients
.
setdefault
(
output
,
{})
self
.
clients
.
setdefault
(
output
,
{})
self
.
stale_droot
=
True
self
.
stale_droot
=
True
def
on_prune
(
self
,
env
,
app
):
def
on_prune
(
self
,
env
,
app
):
...
@@ -350,18 +350,18 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -350,18 +350,18 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
self
.
view_o
[
i
]
.
remove
(
o
)
self
.
view_o
[
i
]
.
remove
(
o
)
if
not
self
.
view_o
[
i
]:
if
not
self
.
view_o
[
i
]:
del
self
.
view_o
[
i
]
del
self
.
view_o
[
i
]
self
.
stale_droot
=
True
self
.
stale_droot
=
True
def
on_change_input
(
self
,
env
,
app
,
i
,
old_r
,
new_r
):
def
on_change_input
(
self
,
env
,
app
,
i
,
old_r
,
new_r
):
"""app.inputs[i] changed from old_r to new_r """
"""app.inputs[i] changed from old_r to new_r """
if
app
==
'output'
:
if
app
==
'output'
:
# app == 'output' is special key that means Env is redefining which nodes are being
# app == 'output' is special key that means Env is redefining which nodes are being
# considered 'outputs' of the graph.
# considered 'outputs' of the graph.
pass
pass
else
:
else
:
if
app
not
in
self
.
debug_all_apps
:
raise
ProtocolError
(
"change without import"
)
if
app
not
in
self
.
debug_all_apps
:
raise
ProtocolError
(
"change without import"
)
#UPDATE self.clients
#UPDATE self.clients
self
.
clients
[
old_r
][
app
]
-=
1
self
.
clients
[
old_r
][
app
]
-=
1
if
self
.
clients
[
old_r
][
app
]
==
0
:
if
self
.
clients
[
old_r
][
app
]
==
0
:
...
@@ -388,7 +388,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -388,7 +388,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
del
self
.
view_o
[
old_r
]
del
self
.
view_o
[
old_r
]
self
.
view_o
.
setdefault
(
new_r
,
set
())
.
add
(
output
)
self
.
view_o
.
setdefault
(
new_r
,
set
())
.
add
(
output
)
self
.
stale_droot
=
True
self
.
stale_droot
=
True
def
validate
(
self
,
env
):
def
validate
(
self
,
env
):
...
@@ -400,7 +400,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -400,7 +400,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
"""
"""
#print '\nVALIDATE'
#print '\nVALIDATE'
if
self
.
destroyers
:
if
self
.
destroyers
:
try
:
try
:
ords
=
self
.
orderings
(
env
)
ords
=
self
.
orderings
(
env
)
except
Exception
,
e
:
except
Exception
,
e
:
...
@@ -423,18 +423,18 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -423,18 +423,18 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
pass
pass
return
True
return
True
def
orderings
(
self
,
env
):
def
orderings
(
self
,
env
):
"""Return orderings induced by destructive operations.
"""Return orderings induced by destructive operations.
Raise InconsistencyError when
Raise InconsistencyError when
a) attempting to destroy indestructable variable, or
a) attempting to destroy indestructable variable, or
b) attempting to destroy a value multiple times, or
b) attempting to destroy a value multiple times, or
c) an Apply destroys (illegally) one of its own inputs by aliasing
c) an Apply destroys (illegally) one of its own inputs by aliasing
"""
"""
rval
=
{}
rval
=
{}
if
self
.
destroyers
:
if
self
.
destroyers
:
# BUILD DATA STRUCTURES
# BUILD DATA STRUCTURES
# CHECK for multiple destructions during construction of variables
# CHECK for multiple destructions during construction of variables
...
@@ -444,7 +444,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -444,7 +444,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
#print "view_i", self.view_i
#print "view_i", self.view_i
#print "view_o", self.view_o
#print "view_o", self.view_o
# check for destruction of constants
# check for destruction of constants
illegal_destroy
=
[
r
for
r
in
droot
if
\
illegal_destroy
=
[
r
for
r
in
droot
if
\
getattr
(
r
.
tag
,
'indestructible'
,
False
)
or
\
getattr
(
r
.
tag
,
'indestructible'
,
False
)
or
\
isinstance
(
r
,
graph
.
Constant
)]
isinstance
(
r
,
graph
.
Constant
)]
...
@@ -472,7 +472,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -472,7 +472,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
# add_inplace(x, x). An Op that can still work in this case should declare
# add_inplace(x, x). An Op that can still work in this case should declare
# so via the 'tolerate_same' attribute
# so via the 'tolerate_same' attribute
#
#
# tolerate_same should be a list of pairs of the form
# tolerate_same should be a list of pairs of the form
# [(idx0, idx1), (idx0, idx2), ...]
# [(idx0, idx1), (idx0, idx2), ...]
# The first element of each pair is the index of a destroyed
# The first element of each pair is the index of a destroyed
# variable.
# variable.
...
@@ -491,7 +491,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -491,7 +491,7 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
for
i
,
input
in
enumerate
(
app
.
inputs
):
for
i
,
input
in
enumerate
(
app
.
inputs
):
if
input
in
root_impact
\
if
input
in
root_impact
\
and
(
i
not
in
tolerated
or
input
is
not
destroyed_variable
):
and
(
i
not
in
tolerated
or
input
is
not
destroyed_variable
):
raise
InconsistencyError
(
"Input aliasing:
%
s (
%
i,
%
i)"
raise
InconsistencyError
(
"Input aliasing:
%
s (
%
i,
%
i)"
%
(
app
,
destroyed_idx
,
i
))
%
(
app
,
destroyed_idx
,
i
))
# add the rule: app must be preceded by all other Apply instances that
# add the rule: app must be preceded by all other Apply instances that
...
@@ -505,4 +505,3 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
...
@@ -505,4 +505,3 @@ class DestroyHandlerHelper2(toolbox.Bookkeeper):
rval
[
app
]
=
root_clients
rval
[
app
]
=
root_clients
return
rval
return
rval
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论