Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9078756f
提交
9078756f
authored
11月 09, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
11月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Clean up comments in basic_opt and math_opt
上级
ecf0d14a
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
19 行增加
和
53 行删除
+19
-53
basic_opt.py
aesara/tensor/basic_opt.py
+14
-49
math_opt.py
aesara/tensor/math_opt.py
+5
-4
没有找到文件。
aesara/tensor/basic_opt.py
浏览文件 @
9078756f
""" Tensor optimizations addressing the ops in basic.py."""
""" Tensor optimizations addressing the ops in basic.py."""
# TODO: intelligent merge for mul/add
# TODO: 0*x -> 0
import
logging
import
logging
import
sys
import
sys
...
@@ -588,8 +586,9 @@ def register_specialize_device(lopt, *tags, **kwargs):
...
@@ -588,8 +586,9 @@ def register_specialize_device(lopt, *tags, **kwargs):
def
apply_local_dimshuffle_lift
(
fgraph
,
var
):
def
apply_local_dimshuffle_lift
(
fgraph
,
var
):
# return var
"""
# lift recursively
lift recursively
"""
if
not
var
.
owner
:
if
not
var
.
owner
:
return
var
return
var
new
=
local_dimshuffle_lift
.
transform
(
fgraph
,
var
.
owner
)
new
=
local_dimshuffle_lift
.
transform
(
fgraph
,
var
.
owner
)
...
@@ -598,10 +597,12 @@ def apply_local_dimshuffle_lift(fgraph, var):
...
@@ -598,10 +597,12 @@ def apply_local_dimshuffle_lift(fgraph, var):
return
var
return
var
# Checks for two types of useless dimshuffles:
# 1 - dimshuffle all dimensions in order.
# 2 - dimshuffle a broadcastable dimension.
def
is_dimshuffle_useless
(
new_order
,
input
):
def
is_dimshuffle_useless
(
new_order
,
input
):
"""
Checks for two types of useless dimshuffles:
1 - dimshuffle all dimensions in order.
2 - dimshuffle a broadcastable dimension.
"""
is_useless
=
True
is_useless
=
True
if
len
(
new_order
)
==
input
.
type
.
ndim
:
if
len
(
new_order
)
==
input
.
type
.
ndim
:
all_broadcastable_dims
=
[
all_broadcastable_dims
=
[
...
@@ -707,11 +708,6 @@ def local_useless_dimshuffle_in_reshape(fgraph, node):
...
@@ -707,11 +708,6 @@ def local_useless_dimshuffle_in_reshape(fgraph, node):
return
[
ret
]
return
[
ret
]
######################
# Casting operations #
######################
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
@local_optimizer
([
TensorFromScalar
])
@local_optimizer
([
TensorFromScalar
])
...
@@ -740,9 +736,6 @@ def local_scalar_tensor_scalar(fgraph, node):
...
@@ -740,9 +736,6 @@ def local_scalar_tensor_scalar(fgraph, node):
return
[
s
]
return
[
s
]
#####################################
# ShapeFeature, Shape optimizations
#####################################
class
MakeVectorPrinter
:
class
MakeVectorPrinter
:
def
process
(
self
,
r
,
pstate
):
def
process
(
self
,
r
,
pstate
):
if
r
.
owner
is
None
:
if
r
.
owner
is
None
:
...
@@ -1844,7 +1837,6 @@ def local_canonicalize_alloc(fgraph, node):
...
@@ -1844,7 +1837,6 @@ def local_canonicalize_alloc(fgraph, node):
return
[
DimShuffle
(
inner
.
type
.
broadcastable
,
dimshuffle_new_order
)(
inner
)]
return
[
DimShuffle
(
inner
.
type
.
broadcastable
,
dimshuffle_new_order
)(
inner
)]
# Don't register by default.
@local_optimizer
([
AllocEmpty
])
@local_optimizer
([
AllocEmpty
])
def
local_alloc_empty_to_zeros
(
fgraph
,
node
):
def
local_alloc_empty_to_zeros
(
fgraph
,
node
):
"""This convert AllocEmpty to Alloc of 0.
"""This convert AllocEmpty to Alloc of 0.
...
@@ -1884,7 +1876,6 @@ def local_shape_to_shape_i(fgraph, node):
...
@@ -1884,7 +1876,6 @@ def local_shape_to_shape_i(fgraph, node):
return
[
ret
]
return
[
ret
]
# TODO: Not sure what type of node we are expecting here
@register_specialize
@register_specialize
@register_canonicalize
@register_canonicalize
@local_optimizer
([
Shape_i
])
@local_optimizer
([
Shape_i
])
...
@@ -1907,9 +1898,6 @@ def local_track_shape_i(fgraph, node):
...
@@ -1907,9 +1898,6 @@ def local_track_shape_i(fgraph, node):
return
[
shape_feature
.
shape_of
[
replacement
][
node
.
op
.
i
]]
return
[
shape_feature
.
shape_of
[
replacement
][
node
.
op
.
i
]]
# TODO: the other optimization for and, or, xor, le and ge see ticket #496.
@register_useless
@register_useless
@register_canonicalize
(
"fast_compile"
)
@register_canonicalize
(
"fast_compile"
)
@register_specialize
@register_specialize
...
@@ -2160,7 +2148,6 @@ def local_remove_all_assert(fgraph, node):
...
@@ -2160,7 +2148,6 @@ def local_remove_all_assert(fgraph, node):
return
[
node
.
inputs
[
0
]]
return
[
node
.
inputs
[
0
]]
# Disabled by default
compile
.
optdb
[
"canonicalize"
]
.
register
(
compile
.
optdb
[
"canonicalize"
]
.
register
(
"local_remove_all_assert"
,
"local_remove_all_assert"
,
local_remove_all_assert
,
local_remove_all_assert
,
...
@@ -2580,12 +2567,14 @@ def local_useless_switch(fgraph, node):
...
@@ -2580,12 +2567,14 @@ def local_useless_switch(fgraph, node):
return
False
return
False
# Merge add/sub/mul/div/minimum/maximum/... of switches sharing the same
# condition, to enable further simplification of their branches
# Example: switch(c, a, b) + switch(c, x, y) -> switch(c, a+x, b+y)
@register_canonicalize
@register_canonicalize
@local_optimizer
([
Elemwise
])
@local_optimizer
([
Elemwise
])
def
local_merge_switch_same_cond
(
fgraph
,
node
):
def
local_merge_switch_same_cond
(
fgraph
,
node
):
"""
Merge add/sub/mul/div/minimum/maximum/... of switches sharing the same
condition, to enable further simplification of their branches
Example: switch(c, a, b) + switch(c, x, y) -> switch(c, a+x, b+y)
"""
# node must be binary elemwise or add or mul
# node must be binary elemwise or add or mul
if
not
isinstance
(
node
.
op
,
Elemwise
)
or
not
isinstance
(
if
not
isinstance
(
node
.
op
,
Elemwise
)
or
not
isinstance
(
node
.
op
.
scalar_op
,
(
aes
.
BinaryScalarOp
,
aes
.
Add
,
aes
.
Mul
)
node
.
op
.
scalar_op
,
(
aes
.
BinaryScalarOp
,
aes
.
Add
,
aes
.
Mul
)
...
@@ -2613,9 +2602,6 @@ def local_merge_switch_same_cond(fgraph, node):
...
@@ -2613,9 +2602,6 @@ def local_merge_switch_same_cond(fgraph, node):
]
]
#############
# Tile Opts #
#############
@register_useless
@register_useless
@register_canonicalize
@register_canonicalize
@register_stabilize
@register_stabilize
...
@@ -2659,9 +2645,6 @@ def local_useless_tile(fgraph, node):
...
@@ -2659,9 +2645,6 @@ def local_useless_tile(fgraph, node):
return
return
##############
# Split Opts #
##############
@register_useless
@register_useless
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
...
@@ -2685,9 +2668,6 @@ def local_useless_split(fgraph, node):
...
@@ -2685,9 +2668,6 @@ def local_useless_split(fgraph, node):
return
[
out2
]
return
[
out2
]
################
# Flatten Opts #
################
@register_canonicalize
@register_canonicalize
@register_stabilize
@register_stabilize
@local_optimizer
([
Flatten
])
@local_optimizer
([
Flatten
])
...
@@ -2721,11 +2701,6 @@ def local_flatten_lift(fgraph, node):
...
@@ -2721,11 +2701,6 @@ def local_flatten_lift(fgraph, node):
return
[
e
]
return
[
e
]
##################
# Reshape opts #
##################
def
local_reshape_chain
(
op
):
def
local_reshape_chain
(
op
):
@local_optimizer
([
op
])
@local_optimizer
([
op
])
def
f
(
fgraph
,
node
):
def
f
(
fgraph
,
node
):
...
@@ -2865,6 +2840,7 @@ def local_useless_reshape(fgraph, node):
...
@@ -2865,6 +2840,7 @@ def local_useless_reshape(fgraph, node):
# TODO later: if all the shapes except one match, we may want to
# TODO later: if all the shapes except one match, we may want to
# consider it useless as well, like we do in the 1-dim case.
# consider it useless as well, like we do in the 1-dim case.
return
False
@register_canonicalize
@register_canonicalize
...
@@ -2956,10 +2932,6 @@ def local_reshape_lift(fgraph, node):
...
@@ -2956,10 +2932,6 @@ def local_reshape_lift(fgraph, node):
return
[
re
]
return
[
re
]
##################
# Middleman cuts #
##################
register_canonicalize
(
OpRemove
(
tensor_copy
),
name
=
"remove_tensor_copy"
)
register_canonicalize
(
OpRemove
(
tensor_copy
),
name
=
"remove_tensor_copy"
)
...
@@ -3429,13 +3401,6 @@ def local_useless_composite(fgraph, node):
...
@@ -3429,13 +3401,6 @@ def local_useless_composite(fgraph, node):
return
dict
(
zip
([
node
.
outputs
[
i
]
for
i
in
idx
],
e
))
return
dict
(
zip
([
node
.
outputs
[
i
]
for
i
in
idx
],
e
))
# ############################
# # Remove consider_constant #
# ############################
# Although the ops ConsiderConstant, ZeroGrad and DisconnectedGrad
# just returns the input, it should be removed from the graph to
@register_canonicalize
(
"fast_compile"
)
@register_canonicalize
(
"fast_compile"
)
@register_useless
(
"fast_compile"
)
@register_useless
(
"fast_compile"
)
@local_optimizer
(
None
)
@local_optimizer
(
None
)
...
...
aesara/tensor/math_opt.py
浏览文件 @
9078756f
""" Tensor optimizations addressing the ops in math.py."""
""" Tensor optimizations addressing the ops in math.py."""
# TODO: intelligent merge for mul/add
# TODO: 0*x -> 0
import
itertools
import
itertools
import
logging
import
logging
...
@@ -2225,12 +2223,15 @@ def local_log1p(fgraph, node):
...
@@ -2225,12 +2223,15 @@ def local_log1p(fgraph, node):
return
[
log1p
(
neg
(
other
))]
return
[
log1p
(
neg
(
other
))]
# TODO: in canonicalize, change log10 and log2 -> log
@register_stabilize
@register_stabilize
@register_specialize
@register_specialize
@local_optimizer
([
log
])
@local_optimizer
([
log
])
def
local_log_add_exp
(
fgraph
,
node
):
def
local_log_add_exp
(
fgraph
,
node
):
# log(exp(x)+exp(y)+exp(z)) = max + log(x-max, y-max, z-max)
"""
``log(exp(x)+exp(y)+exp(z)) = max + log(x-max, y-max, z-max)``
TODO: in canonicalize, change log10 and log2 -> log
"""
if
node
.
op
==
log
:
if
node
.
op
==
log
:
z
=
node
.
inputs
[
0
]
z
=
node
.
inputs
[
0
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论