Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b25fb1be
提交
b25fb1be
authored
11月 10, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
11月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Prevent unnecessary shadowing of builtin input
上级
9078756f
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
70 行增加
和
74 行删除
+70
-74
basic_opt.py
aesara/tensor/basic_opt.py
+48
-52
math_opt.py
aesara/tensor/math_opt.py
+22
-22
没有找到文件。
aesara/tensor/basic_opt.py
浏览文件 @
b25fb1be
...
...
@@ -644,10 +644,10 @@ def local_dimshuffle_lift(fgraph, node):
if
not
isinstance
(
op
,
DimShuffle
):
return
False
inp
ut
=
node
.
inputs
[
0
]
inode
=
inp
ut
.
owner
inp
=
node
.
inputs
[
0
]
inode
=
inp
.
owner
new_order
=
op
.
new_order
if
inode
and
isinstance
(
inode
.
op
,
Elemwise
)
and
(
len
(
fgraph
.
clients
[
inp
ut
])
==
1
):
if
inode
and
isinstance
(
inode
.
op
,
Elemwise
)
and
(
len
(
fgraph
.
clients
[
inp
])
==
1
):
# Don't use make_node to have tag.test_value set.
new_inputs
=
[]
for
inp
in
inode
.
inputs
:
...
...
@@ -658,12 +658,12 @@ def local_dimshuffle_lift(fgraph, node):
return
ret
if
inode
and
isinstance
(
inode
.
op
,
DimShuffle
):
new_order
=
[
x
==
"x"
and
"x"
or
inode
.
op
.
new_order
[
x
]
for
x
in
new_order
]
inp
ut
=
inode
.
inputs
[
0
]
inp
=
inode
.
inputs
[
0
]
if
is_dimshuffle_useless
(
new_order
,
inp
ut
):
return
[
inp
ut
]
if
is_dimshuffle_useless
(
new_order
,
inp
):
return
[
inp
]
elif
inode
and
isinstance
(
inode
.
op
,
DimShuffle
):
ret
=
op
.
__class__
(
inp
ut
.
type
.
broadcastable
,
new_order
)(
input
)
ret
=
op
.
__class__
(
inp
.
type
.
broadcastable
,
new_order
)(
inp
)
ret
=
apply_local_dimshuffle_lift
(
fgraph
,
ret
)
copy_stack_trace
(
node
.
outputs
[
0
],
ret
)
return
[
ret
]
...
...
@@ -691,7 +691,7 @@ def local_useless_dimshuffle_in_reshape(fgraph, node):
return
False
new_order
=
node
.
inputs
[
0
]
.
owner
.
op
.
new_order
inp
ut
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
inp
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
broadcastables
=
node
.
inputs
[
0
]
.
broadcastable
new_order_of_nonbroadcast
=
[]
for
i
,
bd
in
zip
(
new_order
,
broadcastables
):
...
...
@@ -703,7 +703,7 @@ def local_useless_dimshuffle_in_reshape(fgraph, node):
)
if
no_change_in_order
:
shape
=
node
.
inputs
[
1
]
ret
=
op
.
__class__
(
node
.
outputs
[
0
]
.
ndim
)(
inp
ut
,
shape
)
ret
=
op
.
__class__
(
node
.
outputs
[
0
]
.
ndim
)(
inp
,
shape
)
copy_stack_trace
(
node
.
outputs
[
0
],
ret
)
return
[
ret
]
...
...
@@ -744,7 +744,7 @@ class MakeVectorPrinter:
old_precedence
=
getattr
(
pstate
,
"precedence"
,
None
)
try
:
pstate
.
precedence
=
1000
s
=
[
pstate
.
pprinter
.
process
(
inp
ut
)
for
input
in
r
.
owner
.
inputs
]
s
=
[
pstate
.
pprinter
.
process
(
inp
)
for
inp
in
r
.
owner
.
inputs
]
finally
:
pstate
.
precedence
=
old_precedence
return
f
"[{', '.join(s)}]"
...
...
@@ -1636,12 +1636,12 @@ def local_fill_sink(fgraph, node):
return
False
models
=
[]
inputs
=
[]
for
inp
ut
in
node
.
inputs
:
if
inp
ut
.
owner
and
input
.
owner
.
op
==
fill
:
models
.
append
(
inp
ut
.
owner
.
inputs
[
0
])
inputs
.
append
(
inp
ut
.
owner
.
inputs
[
1
])
for
inp
in
node
.
inputs
:
if
inp
.
owner
and
inp
.
owner
.
op
==
fill
:
models
.
append
(
inp
.
owner
.
inputs
[
0
])
inputs
.
append
(
inp
.
owner
.
inputs
[
1
])
else
:
inputs
.
append
(
inp
ut
)
inputs
.
append
(
inp
)
if
not
models
:
return
False
c
=
node
.
op
(
*
inputs
)
...
...
@@ -1765,16 +1765,16 @@ def local_useless_alloc(fgraph, node):
if
not
isinstance
(
node
.
op
,
Alloc
):
return
False
inp
ut
=
node
.
inputs
[
0
]
inp
=
node
.
inputs
[
0
]
output
=
node
.
outputs
[
0
]
if
inp
ut
.
type
==
output
.
type
:
if
inp
ut
.
ndim
==
0
:
return
[
inp
ut
]
if
inp
.
type
==
output
.
type
:
if
inp
.
ndim
==
0
:
return
[
inp
]
else
:
return
[
Assert
(
"Shapes must be equal"
)(
inp
ut
,
at_all
(
eq
(
input
.
shape
,
node
.
inputs
[
1
:]))
inp
,
at_all
(
eq
(
inp
.
shape
,
node
.
inputs
[
1
:]))
)
]
...
...
@@ -1799,13 +1799,13 @@ def local_canonicalize_alloc(fgraph, node):
if
not
isinstance
(
op
,
Alloc
):
return
False
inp
ut
=
node
.
inputs
[
0
]
inp
=
node
.
inputs
[
0
]
output
=
node
.
outputs
[
0
]
# Check if dtype and broadcast remain the same.
if
inp
ut
.
type
==
output
.
type
:
if
inp
.
type
==
output
.
type
:
# We don't need to copy over any stack traces here
return
[
inp
ut
]
return
[
inp
]
# Allow local_merge_alloc to do its work first
clients
=
fgraph
.
clients
[
output
]
...
...
@@ -1817,20 +1817,20 @@ def local_canonicalize_alloc(fgraph, node):
output_shape
=
node
.
inputs
[
1
:]
num_dims_with_size_1_added_to_left
=
0
for
i
in
range
(
len
(
output_shape
)
-
inp
ut
.
ndim
):
for
i
in
range
(
len
(
output_shape
)
-
inp
.
ndim
):
if
extract_constant
(
output_shape
[
i
],
only_process_constants
=
True
)
==
1
:
num_dims_with_size_1_added_to_left
+=
1
else
:
break
new_output_shape
=
output_shape
[
num_dims_with_size_1_added_to_left
:]
if
num_dims_with_size_1_added_to_left
>
0
and
len
(
new_output_shape
)
>=
inp
ut
.
ndim
:
if
num_dims_with_size_1_added_to_left
>
0
and
len
(
new_output_shape
)
>=
inp
.
ndim
:
if
(
output
.
broadcastable
[
num_dims_with_size_1_added_to_left
:]
==
inp
ut
.
broadcastable
==
inp
.
broadcastable
):
inner
=
inp
ut
inner
=
inp
else
:
inner
=
op
(
*
([
inp
ut
]
+
new_output_shape
))
inner
=
op
(
*
([
inp
]
+
new_output_shape
))
dimshuffle_new_order
=
[
"x"
]
*
num_dims_with_size_1_added_to_left
+
list
(
range
(
len
(
new_output_shape
))
)
...
...
@@ -2292,14 +2292,14 @@ def local_rebroadcast_lift(fgraph, node):
if
not
isinstance
(
op
,
Rebroadcast
):
return
False
inp
ut
=
node
.
inputs
[
0
]
inode
=
inp
ut
.
owner
inp
=
node
.
inputs
[
0
]
inode
=
inp
.
owner
if
inode
and
isinstance
(
inode
.
op
,
Elemwise
)
and
len
(
inode
.
inputs
)
==
1
:
# It may happen that `input` has no client because this optimization
# is called from `apply_rebroadcast_opt`, which in particular is used
# by the `unbroadcast` function before we are in the actual function
# compilation phase.
if
len
(
fgraph
.
clients
.
get
(
inp
ut
,
()))
==
1
:
if
len
(
fgraph
.
clients
.
get
(
inp
,
()))
==
1
:
rebroadcasted
=
Rebroadcast
(
*
list
(
op
.
axis
.
items
()))(
inode
.
inputs
[
0
])
# Copy over stacktrace from previous output (after rebroadcasting)
# to new output, because an error in the new graph right after
...
...
@@ -2755,28 +2755,24 @@ def local_useless_reshape(fgraph, node):
if
not
isinstance
(
op
,
Reshape
):
return
False
inp
ut
=
node
.
inputs
[
0
]
inp
=
node
.
inputs
[
0
]
output
=
node
.
outputs
[
0
]
output_shape
=
node
.
inputs
[
1
]
if
inp
ut
.
ndim
!=
output
.
ndim
:
if
inp
.
ndim
!=
output
.
ndim
:
return
False
# Simple case: both input and output have a single dimension.
# This could hide errors if the user provides inconsistent shapes.
if
(
input
.
ndim
==
1
and
output
.
ndim
==
1
and
input
.
broadcastable
==
output
.
broadcastable
):
return
[
input
]
if
inp
.
ndim
==
1
and
output
.
ndim
==
1
and
inp
.
broadcastable
==
output
.
broadcastable
:
return
[
inp
]
# Second case: all the shapes match the input shape
# Match Reshape(x, x.shape)
if
output_shape
.
owner
and
isinstance
(
output_shape
.
owner
.
op
,
Shape
):
shape_input
=
output_shape
.
owner
.
inputs
[
0
]
if
shape_input
==
inp
ut
:
return
[
inp
ut
]
if
shape_input
==
inp
:
return
[
inp
]
# Match Reshape(x, [x.shape[0], ..., x.shape[-1]]), accounting for
# broadcastable and constant dimensions
...
...
@@ -2786,15 +2782,15 @@ def local_useless_reshape(fgraph, node):
shape_feature
=
getattr
(
fgraph
,
"shape_feature"
,
None
)
nb_m1
=
0
shape_match
=
[
False
]
*
inp
ut
.
ndim
for
dim
in
range
(
inp
ut
.
ndim
):
shape_match
=
[
False
]
*
inp
.
ndim
for
dim
in
range
(
inp
.
ndim
):
outshp_i
=
output_shape_is
[
dim
]
# Match Shape_i{dim}(input)
if
(
outshp_i
.
owner
and
isinstance
(
outshp_i
.
owner
.
op
,
Shape_i
)
and
outshp_i
.
owner
.
op
.
i
==
dim
and
outshp_i
.
owner
.
inputs
[
0
]
==
inp
ut
and
outshp_i
.
owner
.
inputs
[
0
]
==
inp
):
shape_match
[
dim
]
=
True
continue
...
...
@@ -2809,13 +2805,13 @@ def local_useless_reshape(fgraph, node):
subtensor_inp
=
outshp_i
.
owner
.
inputs
[
0
]
if
subtensor_inp
.
owner
and
isinstance
(
subtensor_inp
.
owner
.
op
,
Shape
):
shape_input_i
=
subtensor_inp
.
owner
.
inputs
[
0
]
if
shape_input_i
==
inp
ut
:
if
shape_input_i
==
inp
:
shape_match
[
dim
]
=
True
continue
# Match 1 if input.broadcastable[dim] is True
cst_outshp_i
=
extract_constant
(
outshp_i
,
only_process_constants
=
1
)
if
inp
ut
.
broadcastable
[
dim
]
and
cst_outshp_i
==
1
:
if
inp
.
broadcastable
[
dim
]
and
cst_outshp_i
==
1
:
shape_match
[
dim
]
=
True
continue
...
...
@@ -2827,7 +2823,7 @@ def local_useless_reshape(fgraph, node):
# Match shape_of[input][dim] or its constant equivalent
if
shape_feature
:
inpshp_i
=
shape_feature
.
get_shape
(
inp
ut
,
dim
)
inpshp_i
=
shape_feature
.
get_shape
(
inp
,
dim
)
if
inpshp_i
==
outshp_i
or
(
extract_constant
(
inpshp_i
,
only_process_constants
=
1
)
==
extract_constant
(
outshp_i
,
only_process_constants
=
1
)
...
...
@@ -2836,7 +2832,7 @@ def local_useless_reshape(fgraph, node):
continue
if
all
(
shape_match
)
and
nb_m1
<=
1
:
return
[
inp
ut
]
return
[
inp
]
# 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.
...
...
@@ -2862,7 +2858,7 @@ def local_reshape_to_dimshuffle(fgraph, node):
if
not
isinstance
(
op
,
Reshape
):
return
False
inp
ut
=
node
.
inputs
[
0
]
inp
=
node
.
inputs
[
0
]
output
=
node
.
outputs
[
0
]
output_shape
=
node
.
inputs
[
1
]
...
...
@@ -2883,7 +2879,7 @@ def local_reshape_to_dimshuffle(fgraph, node):
new_output_shape
.
append
(
dim
)
index
=
index
+
1
if
index
!=
output
.
ndim
:
inner
=
op
.
__class__
(
len
(
new_output_shape
))(
inp
ut
,
new_output_shape
)
inner
=
op
.
__class__
(
len
(
new_output_shape
))(
inp
,
new_output_shape
)
copy_stack_trace
(
output
,
inner
)
new_node
=
[
DimShuffle
(
inner
.
type
.
broadcastable
,
dimshuffle_new_order
)(
inner
)]
copy_stack_trace
(
output
,
new_node
)
...
...
@@ -2937,8 +2933,8 @@ register_canonicalize(OpRemove(tensor_copy), name="remove_tensor_copy")
@local_optimizer
(
None
)
def
constant_folding
(
fgraph
,
node
):
for
inp
ut
in
node
.
inputs
:
if
not
isinstance
(
inp
ut
,
Constant
):
for
inp
in
node
.
inputs
:
if
not
isinstance
(
inp
,
Constant
):
return
False
# condition: all inputs are constant
if
not
node
.
op
.
do_constant_folding
(
fgraph
,
node
):
...
...
aesara/tensor/math_opt.py
浏览文件 @
b25fb1be
...
...
@@ -627,7 +627,7 @@ class AlgebraicCanonizer(LocalOptimizer):
def
tracks
(
self
):
return
[
self
.
main
,
self
.
inverse
,
self
.
reciprocal
]
def
get_num_denum
(
self
,
inp
ut
):
def
get_num_denum
(
self
,
inp
):
r"""
This extract two lists, ``num`` and ``denum``, such that the input is:
``self.inverse(self.main(\*num), self.main(\*denum))``. It returns
...
...
@@ -656,12 +656,12 @@ class AlgebraicCanonizer(LocalOptimizer):
# argument. The leaf-Variables of the graph covered by the
# recursion may be of any Variable type.
if
inp
ut
.
owner
is
None
or
input
.
owner
.
op
not
in
[
if
inp
.
owner
is
None
or
inp
.
owner
.
op
not
in
[
self
.
main
,
self
.
inverse
,
self
.
reciprocal
,
]:
if
inp
ut
.
owner
and
isinstance
(
input
.
owner
.
op
,
DimShuffle
):
if
inp
.
owner
and
isinstance
(
inp
.
owner
.
op
,
DimShuffle
):
# If input is a DimShuffle of some input which does
# something like this:
...
...
@@ -671,7 +671,7 @@ class AlgebraicCanonizer(LocalOptimizer):
# with broadcastable 1s to the *left*
# Then we will simply discard the DimShuffle and return
# the num/denum of its input
dsn
=
inp
ut
.
owner
# dimshuffle node
dsn
=
inp
.
owner
# dimshuffle node
dsop
=
dsn
.
op
# dimshuffle op
# the first input of the dimshuffle i.e. the ndarray to redim
...
...
@@ -687,22 +687,22 @@ class AlgebraicCanonizer(LocalOptimizer):
# different numbers of dimensions (hence why we can
# discard its information - we know we can retrieve it
# later on).
compatible_order
=
(
"x"
,)
*
(
inp
ut
.
type
.
ndim
-
dsi0
.
type
.
ndim
)
+
tuple
(
compatible_order
=
(
"x"
,)
*
(
inp
.
type
.
ndim
-
dsi0
.
type
.
ndim
)
+
tuple
(
range
(
dsi0
.
type
.
ndim
)
)
if
dsop
.
new_order
==
compatible_order
:
# If the "new_order" is the one we recognize,
# we return the num_denum of the dimshuffled input.
return
self
.
get_num_denum
(
inp
ut
.
owner
.
inputs
[
0
])
return
self
.
get_num_denum
(
inp
.
owner
.
inputs
[
0
])
else
:
# This is when the input isn't produced by main,
# inverse or reciprocal.
return
[
inp
ut
],
[]
return
[
inp
],
[]
else
:
return
[
inp
ut
],
[]
return
[
inp
],
[]
num
=
[]
denum
=
[]
parent
=
inp
ut
.
owner
parent
=
inp
.
owner
# We get the (num, denum) pairs for each input
# pairs = [self.get_num_denum(input2) if input2.type.dtype ==
...
...
@@ -1699,22 +1699,22 @@ def local_opt_alloc(fgraph, node):
if
isinstance
(
node
.
op
,
Sum
)
or
isinstance
(
node
.
op
,
Prod
):
(
node_inps
,)
=
node
.
inputs
if
node_inps
.
owner
and
isinstance
(
node_inps
.
owner
.
op
,
Alloc
):
inp
ut
=
node_inps
.
owner
.
inputs
[
0
]
inp
=
node_inps
.
owner
.
inputs
[
0
]
shapes
=
node_inps
.
owner
.
inputs
[
1
:]
try
:
val
=
get_scalar_constant_value
(
inp
ut
,
only_process_constants
=
True
)
val
=
get_scalar_constant_value
(
inp
,
only_process_constants
=
True
)
assert
val
.
size
==
1
val
=
val
.
reshape
(
1
)[
0
]
# check which type of op
size
=
mul
(
*
shapes
)
if
inp
ut
.
dtype
in
[
"float16"
,
"float32"
]:
if
inp
.
dtype
in
[
"float16"
,
"float32"
]:
# shapes are ints and normally int64.
# We don't want to have a float64 upcast
# We don't want to downcast to float16
# as we fear it could loose too much precision
# that will be amplified by the mul/pow below.
size
=
size
.
astype
(
"float32"
)
if
node
.
op
.
axis
is
None
or
node
.
op
.
axis
==
tuple
(
range
(
inp
ut
.
ndim
)):
if
node
.
op
.
axis
is
None
or
node
.
op
.
axis
==
tuple
(
range
(
inp
.
ndim
)):
if
isinstance
(
node
.
op
,
Sum
):
val
=
val
*
size
else
:
...
...
@@ -2010,15 +2010,15 @@ def local_mul_specialize(fgraph, node):
new_inputs
=
[]
nb_neg_node
=
0
nb_cst
=
0
for
inp
ut
in
node
.
inputs
:
for
inp
in
node
.
inputs
:
# remove any neg arguments
while
inp
ut
.
owner
and
input
.
owner
.
op
==
neg
:
while
inp
.
owner
and
inp
.
owner
.
op
==
neg
:
has_neg
^=
True
inp
ut
=
input
.
owner
.
inputs
[
0
]
inp
=
inp
.
owner
.
inputs
[
0
]
nb_neg_node
+=
1
# remove special case arguments of 1, -1 or 0
y
=
local_mul_canonizer
.
get_constant
(
inp
ut
)
y
=
local_mul_canonizer
.
get_constant
(
inp
)
if
y
==
1.0
:
nb_cst
+=
1
elif
y
==
-
1.0
:
...
...
@@ -2028,7 +2028,7 @@ def local_mul_specialize(fgraph, node):
# if we find any zero, we just return right away
return
[
broadcast_like
(
0
,
node
.
outputs
[
0
],
fgraph
)]
else
:
new_inputs
.
append
(
inp
ut
)
new_inputs
.
append
(
inp
)
if
new_inputs
!=
node
.
inputs
:
if
new_inputs
:
...
...
@@ -2072,14 +2072,14 @@ def local_add_specialize(fgraph, node):
# to put in un-necessary fills.
if
node
.
op
==
add
:
new_inputs
=
[]
for
inp
ut
in
node
.
inputs
:
for
inp
in
node
.
inputs
:
try
:
y
=
get_scalar_constant_value
(
inp
ut
)
y
=
get_scalar_constant_value
(
inp
)
except
NotScalarConstantError
:
y
=
inp
ut
y
=
inp
if
np
.
all
(
y
==
0.0
):
continue
new_inputs
.
append
(
inp
ut
)
new_inputs
.
append
(
inp
)
if
len
(
new_inputs
)
<
len
(
node
.
inputs
):
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论