Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
be0e13a9
提交
be0e13a9
authored
5月 31, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
6月 06, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reduce number of access to self.clients in FunctionGraph
To speedup hot rewrite loops
上级
36c55f5b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
13 行增加
和
14 行删除
+13
-14
fg.py
pytensor/graph/fg.py
+13
-14
没有找到文件。
pytensor/graph/fg.py
浏览文件 @
be0e13a9
...
@@ -232,13 +232,13 @@ class FunctionGraph(MetaObject):
...
@@ -232,13 +232,13 @@ class FunctionGraph(MetaObject):
entry for `var` in `self.clients`.
entry for `var` in `self.clients`.
"""
"""
clients
=
self
.
clients
removal_stack
=
[(
var
,
client_to_remove
)]
removal_stack
=
[(
var
,
client_to_remove
)]
while
removal_stack
:
while
removal_stack
:
var
,
client_to_remove
=
removal_stack
.
pop
()
var
,
client_to_remove
=
removal_stack
.
pop
()
try
:
try
:
var_clients
=
self
.
clients
[
var
]
var_clients
=
clients
[
var
]
var_clients
.
remove
(
client_to_remove
)
var_clients
.
remove
(
client_to_remove
)
except
ValueError
:
except
ValueError
:
# In this case, the original `var` could've been removed from
# In this case, the original `var` could've been removed from
...
@@ -256,9 +256,7 @@ class FunctionGraph(MetaObject):
...
@@ -256,9 +256,7 @@ class FunctionGraph(MetaObject):
self
.
variables
.
remove
(
var
)
self
.
variables
.
remove
(
var
)
else
:
else
:
apply_node
=
var
.
owner
apply_node
=
var
.
owner
if
not
any
(
if
not
any
(
clients
[
output
]
for
output
in
apply_node
.
outputs
):
output
for
output
in
apply_node
.
outputs
if
self
.
clients
[
output
]
):
# The `Apply` node is not used and is not an output, so we
# The `Apply` node is not used and is not an output, so we
# remove it and its outputs
# remove it and its outputs
if
not
hasattr
(
apply_node
.
tag
,
"removed_by"
):
if
not
hasattr
(
apply_node
.
tag
,
"removed_by"
):
...
@@ -276,7 +274,7 @@ class FunctionGraph(MetaObject):
...
@@ -276,7 +274,7 @@ class FunctionGraph(MetaObject):
removal_stack
.
append
((
in_var
,
(
apply_node
,
i
)))
removal_stack
.
append
((
in_var
,
(
apply_node
,
i
)))
if
remove_if_empty
:
if
remove_if_empty
:
del
self
.
clients
[
var
]
del
clients
[
var
]
def
import_var
(
def
import_var
(
self
,
var
:
Variable
,
reason
:
str
|
None
=
None
,
import_missing
:
bool
=
False
self
,
var
:
Variable
,
reason
:
str
|
None
=
None
,
import_missing
:
bool
=
False
...
@@ -563,10 +561,11 @@ class FunctionGraph(MetaObject):
...
@@ -563,10 +561,11 @@ class FunctionGraph(MetaObject):
node
.
tag
.
removed_by
.
append
(
str
(
reason
))
node
.
tag
.
removed_by
.
append
(
str
(
reason
))
# Remove the outputs of the node (i.e. everything "below" it)
# Remove the outputs of the node (i.e. everything "below" it)
clients
=
self
.
clients
for
out
in
node
.
outputs
:
for
out
in
node
.
outputs
:
self
.
variables
.
remove
(
out
)
self
.
variables
.
remove
(
out
)
out_clients
=
self
.
clients
.
get
(
out
,
())
out_clients
=
clients
.
get
(
out
,
())
while
out_clients
:
while
out_clients
:
out_client
,
out_idx
=
out_clients
.
pop
()
out_client
,
out_idx
=
out_clients
.
pop
()
...
@@ -590,13 +589,12 @@ class FunctionGraph(MetaObject):
...
@@ -590,13 +589,12 @@ class FunctionGraph(MetaObject):
assert
isinstance
(
out_client
,
Apply
)
assert
isinstance
(
out_client
,
Apply
)
self
.
remove_node
(
out_client
,
reason
=
reason
)
self
.
remove_node
(
out_client
,
reason
=
reason
)
if
out
in
self
.
clients
:
clients
.
pop
(
out
,
None
)
del
self
.
clients
[
out
]
# Remove all the arrows pointing to this `node`, and any orphaned
# Remove all the arrows pointing to this `node`, and any orphaned
# variables created by removing those arrows
# variables created by removing those arrows
for
inp_idx
,
inp
in
enumerate
(
node
.
inputs
):
for
inp_idx
,
inp
in
enumerate
(
node
.
inputs
):
inp_clients
:
list
[
ClientType
]
=
self
.
clients
.
get
(
inp
,
[])
inp_clients
:
list
[
ClientType
]
=
clients
.
get
(
inp
,
[])
arrow
=
(
node
,
inp_idx
)
arrow
=
(
node
,
inp_idx
)
...
@@ -810,12 +808,13 @@ class FunctionGraph(MetaObject):
...
@@ -810,12 +808,13 @@ class FunctionGraph(MetaObject):
raise
Exception
(
raise
Exception
(
f
"The following nodes are inappropriately cached:
\n
missing: {nodes_missing}
\n
in excess: {nodes_excess}"
f
"The following nodes are inappropriately cached:
\n
missing: {nodes_missing}
\n
in excess: {nodes_excess}"
)
)
clients
=
self
.
clients
for
node
in
nodes
:
for
node
in
nodes
:
for
i
,
variable
in
enumerate
(
node
.
inputs
):
for
i
,
variable
in
enumerate
(
node
.
inputs
):
clients
=
self
.
clients
[
variable
]
var_clients
=
clients
[
variable
]
if
(
node
,
i
)
not
in
clients
:
if
(
node
,
i
)
not
in
var_
clients
:
raise
Exception
(
raise
Exception
(
f
"Inconsistent clients list {(node, i)} in {clients}"
f
"Inconsistent clients list {(node, i)} in {
var_
clients}"
)
)
variables
=
set
(
vars_between
(
self
.
inputs
,
self
.
outputs
))
variables
=
set
(
vars_between
(
self
.
inputs
,
self
.
outputs
))
if
set
(
self
.
variables
)
!=
variables
:
if
set
(
self
.
variables
)
!=
variables
:
...
@@ -831,7 +830,7 @@ class FunctionGraph(MetaObject):
...
@@ -831,7 +830,7 @@ class FunctionGraph(MetaObject):
and
not
isinstance
(
variable
,
AtomicVariable
)
and
not
isinstance
(
variable
,
AtomicVariable
)
):
):
raise
Exception
(
f
"Undeclared input: {variable}"
)
raise
Exception
(
f
"Undeclared input: {variable}"
)
for
cl_node
,
i
in
self
.
clients
[
variable
]:
for
cl_node
,
i
in
clients
[
variable
]:
if
cl_node
==
"output"
:
if
cl_node
==
"output"
:
if
self
.
outputs
[
i
]
is
not
variable
:
if
self
.
outputs
[
i
]
is
not
variable
:
raise
Exception
(
raise
Exception
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论