Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
dbf5f38e
提交
dbf5f38e
authored
2月 10, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
2月 17, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor reshape + dimshuffle rewrites
上级
02545ed5
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
150 行增加
和
171 行删除
+150
-171
basic.py
pytensor/graph/rewriting/basic.py
+0
-10
shape.py
pytensor/tensor/rewriting/shape.py
+150
-161
没有找到文件。
pytensor/graph/rewriting/basic.py
浏览文件 @
dbf5f38e
...
@@ -2800,16 +2800,6 @@ def _check_chain(r, chain):
...
@@ -2800,16 +2800,6 @@ def _check_chain(r, chain):
return
r
is
not
None
return
r
is
not
None
def
check_chain
(
r
,
*
chain
):
"""
WRITEME
"""
if
isinstance
(
r
,
Apply
):
r
=
r
.
outputs
[
0
]
return
_check_chain
(
r
,
reduce
(
list
.
__iadd__
,
([
x
,
0
]
for
x
in
chain
)))
def
pre_greedy_node_rewriter
(
def
pre_greedy_node_rewriter
(
fgraph
:
FunctionGraph
,
rewrites
:
Sequence
[
NodeRewriter
],
out
:
Variable
fgraph
:
FunctionGraph
,
rewrites
:
Sequence
[
NodeRewriter
],
out
:
Variable
)
->
Variable
:
)
->
Variable
:
...
...
pytensor/tensor/rewriting/shape.py
浏览文件 @
dbf5f38e
...
@@ -12,16 +12,17 @@ from pytensor.graph.features import AlreadyThere, Feature
...
@@ -12,16 +12,17 @@ from pytensor.graph.features import AlreadyThere, Feature
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.rewriting.basic
import
(
from
pytensor.graph.rewriting.basic
import
(
GraphRewriter
,
GraphRewriter
,
check_chain
,
copy_stack_trace
,
copy_stack_trace
,
node_rewriter
,
node_rewriter
,
)
)
from
pytensor.graph.utils
import
InconsistencyError
,
get_variable_trace_string
from
pytensor.graph.utils
import
InconsistencyError
,
get_variable_trace_string
from
pytensor.scalar
import
ScalarType
from
pytensor.tensor.basic
import
(
from
pytensor.tensor.basic
import
(
MakeVector
,
MakeVector
,
as_tensor_variable
,
as_tensor_variable
,
cast
,
cast
,
constant
,
constant
,
expand_dims
,
get_scalar_constant_value
,
get_scalar_constant_value
,
register_infer_shape
,
register_infer_shape
,
stack
,
stack
,
...
@@ -47,6 +48,7 @@ from pytensor.tensor.shape import (
...
@@ -47,6 +48,7 @@ from pytensor.tensor.shape import (
from
pytensor.tensor.subtensor
import
Subtensor
,
get_idx_list
from
pytensor.tensor.subtensor
import
Subtensor
,
get_idx_list
from
pytensor.tensor.type
import
TensorType
,
discrete_dtypes
,
integer_dtypes
from
pytensor.tensor.type
import
TensorType
,
discrete_dtypes
,
integer_dtypes
from
pytensor.tensor.type_other
import
NoneConst
,
NoneTypeT
from
pytensor.tensor.type_other
import
NoneConst
,
NoneTypeT
from
pytensor.tensor.variable
import
TensorVariable
class
ShapeFeature
(
Feature
):
class
ShapeFeature
(
Feature
):
...
@@ -755,6 +757,42 @@ pytensor.compile.mode.optdb.register(
...
@@ -755,6 +757,42 @@ pytensor.compile.mode.optdb.register(
pytensor
.
compile
.
mode
.
optdb
.
register
(
"UnShapeOpt"
,
UnShapeOptimizer
(),
position
=
10
)
pytensor
.
compile
.
mode
.
optdb
.
register
(
"UnShapeOpt"
,
UnShapeOptimizer
(),
position
=
10
)
@register_canonicalize
@node_rewriter
([
Reshape
])
def
local_useless_dimshuffle_in_reshape
(
fgraph
,
node
):
"""
Removes useless DimShuffle operation inside Reshape:
reshape(vector.dimshuffle('x', 0), shp) => reshape(vector, shp)
reshape(matrix.dimshuffle('x', 0, 'x', 1), shp) => reshape(matrix, shp)
reshape(row.dimshuffle(1, 'x'), shp) => reshape(row, shp)
reshape(col.dimshuffle(0), shp) => reshape(col, shp)
"""
dimshuffled_x
,
new_shape
=
node
.
inputs
if
not
(
dimshuffled_x
.
owner
is
not
None
and
isinstance
(
dimshuffled_x
.
owner
.
op
,
DimShuffle
)
):
return
False
[
inp
]
=
dimshuffled_x
.
owner
.
inputs
new_order
=
dimshuffled_x
.
owner
.
op
.
new_order
new_order_of_nonbroadcast
=
[]
for
i
,
s
in
zip
(
new_order
,
node
.
inputs
[
0
]
.
type
.
shape
,
strict
=
True
):
if
s
!=
1
:
new_order_of_nonbroadcast
.
append
(
i
)
no_change_in_order
=
all
(
new_order_of_nonbroadcast
[
i
]
<=
new_order_of_nonbroadcast
[
i
+
1
]
for
i
in
range
(
len
(
new_order_of_nonbroadcast
)
-
1
)
)
if
no_change_in_order
:
ret
=
inp
.
reshape
(
new_shape
)
copy_stack_trace
(
node
.
outputs
[
0
],
ret
)
return
[
ret
]
@register_canonicalize
(
"shape_unsafe"
)
@register_canonicalize
(
"shape_unsafe"
)
@register_specialize
(
"shape_unsafe"
)
@register_specialize
(
"shape_unsafe"
)
@node_rewriter
([
Reshape
])
@node_rewriter
([
Reshape
])
...
@@ -763,30 +801,89 @@ def local_reshape_chain(fgraph, node):
...
@@ -763,30 +801,89 @@ def local_reshape_chain(fgraph, node):
Reshape(Reshape(x, shape1),shape2) -> Reshape(x, shape2)
Reshape(Reshape(x, shape1),shape2) -> Reshape(x, shape2)
"""
"""
if
not
check_chain
(
node
,
Reshape
,
Reshape
):
inner_reshape
,
final_shape
=
node
.
inputs
if
not
(
inner_reshape
.
owner
and
isinstance
(
inner_reshape
.
owner
.
op
,
Reshape
)):
return
None
x
,
_
=
inner_reshape
.
owner
.
inputs
new_reshape
=
node
.
op
(
x
,
final_shape
)
copy_stack_trace
(
node
.
outputs
,
new_reshape
)
return
[
new_reshape
]
def
_is_shape_i_of_x
(
var
:
TensorVariable
,
x
:
TensorVariable
,
i
:
int
,
shape_feature
:
ShapeFeature
|
None
=
None
,
)
->
bool
:
if
var
.
type
.
ndim
!=
0
:
return
False
return
False
rval
=
node
.
op
(
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
],
node
.
inputs
[
1
])
constant_var
=
get_scalar_constant_value
(
var
,
# Copy over stacktrace from previous output node, as any error
only_process_constants
=
False
,
# in new computational graph would have been caused by last op
# Don't go through Elemwise to keep things fast
# in the old computational graph.
elemwise
=
False
,
copy_stack_trace
(
node
.
outputs
,
rval
)
raise_not_constant
=
False
,
)
# It might happen that the desired output of this node has a
# broadcastable pattern that does not match that of 'rval'. This is
# Check var is a constant expression with the same value as x.type.shape[i]
# when originally, we were able to figure out that one of the
if
constant_var
==
x
.
type
.
shape
[
i
]:
# dimensions of the reshape is one, but some other transformation
return
True
# replaced the shape by one for which this cannot be guessed.
# We should try to figure out why we lost the information about this
# Match shape_of[x][i] or its constant equivalent
# constant value... but in the meantime, better not apply this
if
shape_feature
is
not
None
:
# rewrite.
i_shape_of_x
=
shape_feature
.
get_shape
(
x
,
i
)
if
rval
.
type
.
ndim
==
node
.
outputs
[
0
]
.
type
.
ndim
and
all
(
if
i_shape_of_x
==
var
or
(
s1
==
s2
isinstance
(
i_shape_of_x
,
Constant
)
and
(
i_shape_of_x
.
data
==
constant_var
)
for
s1
,
s2
in
zip
(
rval
.
type
.
shape
,
node
.
outputs
[
0
]
.
type
.
shape
,
strict
=
True
)
):
if
s1
==
1
or
s2
==
1
return
True
):
return
[
rval
]
if
var
.
owner
is
None
:
# No more constant possibilities
return
False
# Match Shape_i{i}(x)
if
isinstance
(
var
.
owner
.
op
,
Shape_i
):
return
(
var
.
owner
.
op
.
i
==
i
)
and
(
var
.
owner
.
inputs
[
0
]
==
x
)
# type: ignore
# Match Subtensor((ScalarType,))(Shape(input), i)
if
isinstance
(
var
.
owner
.
op
,
Subtensor
):
return
(
# Check we have integer indexing operation
# (and not slice or multiple indexing)
len
(
var
.
owner
.
op
.
idx_list
)
==
1
and
isinstance
(
var
.
owner
.
op
.
idx_list
[
0
],
ScalarType
)
# Check we are indexing on the shape of x
and
var
.
owner
.
inputs
[
0
]
.
owner
is
not
None
and
isinstance
(
var
.
owner
.
inputs
[
0
]
.
owner
.
op
,
Shape
)
and
var
.
owner
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
==
x
# Check that index == i
and
(
get_scalar_constant_value
(
var
.
owner
.
inputs
[
1
],
raise_not_constant
=
False
)
==
i
)
)
return
False
def
_unpack_shape_vector
(
shape
:
TensorVariable
)
->
tuple
[
TensorVariable
,
...
]:
"""Return the elements of a symbolic vector representing a shape.
Handles the most common constant vector or make_vector cases.
Returns tuple(shape) as fallback.
"""
if
isinstance
(
shape
,
Constant
):
return
tuple
(
as_tensor_variable
(
dim
,
ndim
=
0
)
for
dim
in
shape
.
data
)
elif
shape
.
owner
and
isinstance
(
shape
.
owner
.
op
,
MakeVector
):
return
tuple
(
shape
.
owner
.
inputs
)
else
:
return
tuple
(
shape
)
@register_useless
(
"shape_unsafe"
)
@register_useless
(
"shape_unsafe"
)
...
@@ -821,87 +918,30 @@ def local_useless_reshape(fgraph, node):
...
@@ -821,87 +918,30 @@ def local_useless_reshape(fgraph, node):
if
shape_input
==
inp
:
if
shape_input
==
inp
:
return
[
inp
]
return
[
inp
]
# Match Reshape(x, [x.shape[0], ..., x.shape[-1]]), accounting for
shape_feature
=
getattr
(
fgraph
,
"shape_feature"
,
None
)
# broadcastable and constant dimensions
if
isinstance
(
output_shape
,
Constant
)
or
(
output_shape
.
owner
and
isinstance
(
output_shape
.
owner
.
op
,
MakeVector
)
):
if
isinstance
(
output_shape
,
Constant
):
output_shape_is
=
[
as_tensor_variable
(
dim
,
ndim
=
0
)
for
dim
in
output_shape
.
data
]
else
:
output_shape_is
=
output_shape
.
owner
.
inputs
shape_feature
=
getattr
(
fgraph
,
"shape_feature"
,
None
)
nb_m1
=
0
shape_match
=
[
False
]
*
inp
.
type
.
ndim
for
dim
in
range
(
inp
.
type
.
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
):
shape_match
[
dim
]
=
True
continue
# Match Shape(input)[dim]
# Match Reshape(x, [x.shape[0], ..., x.shape[-1]]), accounting for -1
if
(
# or cases where all but one dimension are provably preserved
outshp_i
.
owner
output_shape_is
=
_unpack_shape_vector
(
output_shape
)
and
isinstance
(
outshp_i
.
owner
.
op
,
Subtensor
)
and
len
(
outshp_i
.
owner
.
inputs
)
==
2
and
get_scalar_constant_value
(
outshp_i
.
owner
.
inputs
[
1
],
raise_not_constant
=
False
)
==
dim
):
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
:
shape_match
[
dim
]
=
True
continue
# Match constant if input.type.shape[dim] == constant
cst_outshp_i
=
get_scalar_constant_value
(
outshp_i
,
only_process_constants
=
True
,
raise_not_constant
=
False
)
if
inp
.
type
.
shape
[
dim
]
==
cst_outshp_i
:
shape_match
[
dim
]
=
True
continue
# Match -1
if
cst_outshp_i
==
-
1
:
shape_match
[
dim
]
=
True
nb_m1
+=
1
continue
# Match shape_of[input][dim] or its constant equivalent
nb_m1
=
0
if
shape_feature
:
shape_match
=
[
False
]
*
inp
.
type
.
ndim
inpshp_i
=
shape_feature
.
get_shape
(
inp
,
dim
)
for
dim
in
range
(
inp
.
type
.
ndim
):
if
inpshp_i
==
outshp_i
or
(
outshp_i
=
output_shape_is
[
dim
]
get_scalar_constant_value
(
if
_is_shape_i_of_x
(
outshp_i
,
inp
,
dim
,
shape_feature
=
shape_feature
):
inpshp_i
,
only_process_constants
=
True
,
raise_not_constant
=
False
shape_match
[
dim
]
=
True
)
elif
isinstance
(
outshp_i
,
Constant
)
and
outshp_i
.
data
==
-
1
:
==
get_scalar_constant_value
(
shape_match
[
dim
]
=
True
outshp_i
,
only_process_constants
=
True
,
raise_not_constant
=
False
nb_m1
+=
1
)
):
shape_match
[
dim
]
=
True
continue
if
nb_m1
<=
1
and
all
(
shape_match
):
if
nb_m1
<=
1
and
all
(
shape_match
):
return
[
inp
]
return
[
inp
]
# There is one missing match, but all other dimensions match
# There is one missing match, but all other dimensions match
if
(
nb_m1
==
0
)
and
(
shape_match
.
count
(
False
)
==
1
):
if
(
nb_m1
==
0
)
and
(
shape_match
.
count
(
False
)
==
1
):
return
[
inp
]
return
[
inp
]
return
False
return
False
@register_canonicalize
@register_canonicalize
...
@@ -915,39 +955,26 @@ def local_reshape_to_dimshuffle(fgraph, node):
...
@@ -915,39 +955,26 @@ def local_reshape_to_dimshuffle(fgraph, node):
For example:
For example:
- reshape(x, (1, n)) -> DimShuffle{x,0}(Reshape(x, (n,))
- reshape(x, (1, n)) -> DimShuffle{x,0}(Reshape(x, (n,))
- reshape(x, (1, m, 1, n, 1, 1))
- reshape(x, (1, m, 1, n, 1, 1)) -> DimShuffle{x,0,x,1,x,x}(Reshape(x, (m, n)))
-> DimShuffle{x,0,x,1,x,x}(Reshape(x, (m, n)))
"""
"""
op
=
node
.
op
inp
,
output_shape
=
node
.
inputs
inp
,
output_shape
=
node
.
inputs
[
output
]
=
node
.
outputs
[
output
]
=
node
.
outputs
dimshuffle_new_order
=
[]
unpacked_shape
=
_unpack_shape_vector
(
output_shape
)
expand_axes
=
[]
new_output_shape
=
[]
new_output_shape
=
[]
index
=
0
# index over the output of the new reshape
for
i
,
dim
in
enumerate
(
unpacked_shape
):
for
i
in
range
(
output
.
ndim
):
if
isinstance
(
dim
,
Constant
)
and
dim
.
data
==
1
:
# Since output_shape is a symbolic vector, we trust get_scalar_constant_value
expand_axes
.
append
(
i
)
# to go through however it is formed to see if its i-th element is 1.
# We need only_process_constants=False for that.
dim
=
get_scalar_constant_value
(
output_shape
[
i
],
only_process_constants
=
False
,
elemwise
=
False
,
raise_not_constant
=
False
,
)
if
dim
==
1
:
dimshuffle_new_order
.
append
(
"x"
)
else
:
else
:
dimshuffle_new_order
.
append
(
index
)
new_output_shape
.
append
(
dim
)
new_output_shape
.
append
(
dim
)
index
=
index
+
1
if
index
!=
output
.
type
.
ndim
:
if
len
(
new_output_shape
)
!=
output
.
type
.
ndim
:
inner
=
op
.
__class__
(
len
(
new_output_shape
))(
inp
,
new_output_shape
)
inner
=
inp
.
reshape
(
new_output_shape
)
copy_stack_trace
(
output
,
inner
)
copy_stack_trace
(
output
,
inner
)
new_
node
=
[
inner
.
dimshuffle
(
dimshuffle_new_order
)]
new_
out
=
expand_dims
(
inner
,
expand_axes
)
copy_stack_trace
(
output
,
new_
node
)
copy_stack_trace
(
output
,
new_
out
)
return
new_node
return
[
new_out
]
@register_canonicalize
@register_canonicalize
...
@@ -1187,44 +1214,6 @@ def local_track_shape_i(fgraph, node):
...
@@ -1187,44 +1214,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
]]
@register_canonicalize
@node_rewriter
([
Reshape
])
def
local_useless_dimshuffle_in_reshape
(
fgraph
,
node
):
"""
Removes useless DimShuffle operation inside Reshape:
reshape(vector.dimshuffle('x', 0), shp) => reshape(vector, shp)
reshape(matrix.dimshuffle('x', 0, 'x', 1), shp) => reshape(matrix, shp)
reshape(row.dimshuffle(1, 'x'), shp) => reshape(row, shp)
reshape(col.dimshuffle(0), shp) => reshape(col, shp)
"""
op
=
node
.
op
if
not
isinstance
(
op
,
Reshape
):
return
False
if
not
(
node
.
inputs
[
0
]
.
owner
is
not
None
and
isinstance
(
node
.
inputs
[
0
]
.
owner
.
op
,
DimShuffle
)
):
return
False
new_order
=
node
.
inputs
[
0
]
.
owner
.
op
.
new_order
inp
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
new_order_of_nonbroadcast
=
[]
for
i
,
s
in
zip
(
new_order
,
node
.
inputs
[
0
]
.
type
.
shape
,
strict
=
True
):
if
s
!=
1
:
new_order_of_nonbroadcast
.
append
(
i
)
no_change_in_order
=
all
(
new_order_of_nonbroadcast
[
i
]
<=
new_order_of_nonbroadcast
[
i
+
1
]
for
i
in
range
(
len
(
new_order_of_nonbroadcast
)
-
1
)
)
if
no_change_in_order
:
shape
=
node
.
inputs
[
1
]
ret
=
op
.
__class__
(
node
.
outputs
[
0
]
.
ndim
)(
inp
,
shape
)
copy_stack_trace
(
node
.
outputs
[
0
],
ret
)
return
[
ret
]
@register_useless
@register_useless
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论