Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
790b46fd
提交
790b46fd
authored
7月 11, 2023
作者:
Adrian Seyboldt
提交者:
Ricardo Vieira
9月 05, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enh: Improve static shape of subtensor
上级
1d9fa843
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
63 行增加
和
19 行删除
+63
-19
subtensor.py
pytensor/tensor/subtensor.py
+48
-19
test_subtensor.py
tests/tensor/test_subtensor.py
+15
-0
没有找到文件。
pytensor/tensor/subtensor.py
浏览文件 @
790b46fd
...
@@ -221,6 +221,16 @@ def get_canonical_form_slice(
...
@@ -221,6 +221,16 @@ def get_canonical_form_slice(
step
,
is_step_constant
=
analyze
(
theslice
.
step
)
step
,
is_step_constant
=
analyze
(
theslice
.
step
)
length
,
is_length_constant
=
analyze
(
length
)
length
,
is_length_constant
=
analyze
(
length
)
if
(
is_start_constant
and
is_stop_constant
and
is_step_constant
and
is_length_constant
):
_start
,
_stop
,
_step
=
slice
(
start
,
stop
,
step
)
.
indices
(
length
)
if
_start
<=
_stop
and
_step
>=
1
:
return
slice
(
_start
,
_stop
,
_step
),
1
if
step
is
None
:
if
step
is
None
:
step
=
1
step
=
1
is_step_constant
=
True
is_step_constant
=
True
...
@@ -722,32 +732,51 @@ class Subtensor(COp):
...
@@ -722,32 +732,51 @@ class Subtensor(COp):
f
"Incompatible types for Subtensor template. Expected {input.type}, got {expected_type}."
f
"Incompatible types for Subtensor template. Expected {input.type}, got {expected_type}."
)
)
# infer the broadcasting pattern
padded
=
[
padded
=
get_constant_idx
(
*
get_idx_list
((
None
,)
+
inputs
,
self
.
idx_list
),
self
.
idx_list
,
(
None
,)
+
inputs
,
allow_partial
=
True
*
[
slice
(
None
,
None
,
None
)]
*
(
x
.
type
.
ndim
-
len
(
idx_list
)),
)
+
[
slice
(
None
,
None
,
None
)]
*
(
x
.
type
.
ndim
-
len
(
idx_list
))
]
out_shape
=
[]
out_shape
=
[]
for
i
,
(
p
,
s
)
in
enumerate
(
zip
(
padded
,
x
.
type
.
shape
)):
if
isinstance
(
p
,
slic
e
):
def
extract_const
(
valu
e
):
if
s
==
1
:
if
value
is
None
:
start
=
p
.
start
return
value
,
True
try
:
try
:
start
=
get_underlying_scalar_constant_value
(
start
)
value
=
get_underlying_scalar_constant_value
(
value
)
return
value
,
True
except
NotScalarConstantError
:
except
NotScalarConstantError
:
pass
return
value
,
False
if
start
is
None
or
start
==
0
:
start
=
p
.
start
for
the_slice
,
length
in
zip
(
padded
,
x
.
type
.
shape
):
if
start
is
None
:
if
not
isinstance
(
the_slice
,
slice
):
start
=
0
if
p
.
stop
is
None
or
(
isinstance
(
p
.
stop
,
(
int
,
np
.
integer
,
np
.
ndarray
))
and
p
.
stop
>
start
):
out_shape
.
append
(
1
)
continue
continue
if
length
is
None
:
out_shape
.
append
(
None
)
out_shape
.
append
(
None
)
continue
start
=
the_slice
.
start
stop
=
the_slice
.
stop
step
=
the_slice
.
step
is_slice_const
=
True
start
,
is_const
=
extract_const
(
start
)
is_slice_const
=
is_slice_const
and
is_const
stop
,
is_const
=
extract_const
(
stop
)
is_slice_const
=
is_slice_const
and
is_const
step
,
is_const
=
extract_const
(
step
)
is_slice_const
=
is_slice_const
and
is_const
if
not
is_slice_const
:
out_shape
.
append
(
None
)
continue
slice_length
=
len
(
range
(
*
slice
(
start
,
stop
,
step
)
.
indices
(
length
)))
out_shape
.
append
(
slice_length
)
return
Apply
(
return
Apply
(
self
,
self
,
...
...
tests/tensor/test_subtensor.py
浏览文件 @
790b46fd
...
@@ -2693,3 +2693,18 @@ def test_index_vars_to_types():
...
@@ -2693,3 +2693,18 @@ def test_index_vars_to_types():
assert
isinstance
(
x
.
type
,
scal
.
ScalarType
)
assert
isinstance
(
x
.
type
,
scal
.
ScalarType
)
res
=
index_vars_to_types
(
x
)
res
=
index_vars_to_types
(
x
)
assert
res
==
x
.
type
assert
res
==
x
.
type
@pytest.mark.parametrize
(
"x_shape, indices, expected"
,
[
[(
None
,),
(
slice
(
None
,
None
),),
(
None
,)],
[(
13
,),
(
slice
(
None
,
100
),),
(
13
,)],
[(
13
,),
(
slice
(
-
1
,
None
),),
(
1
,)],
[(
7
,
13
),
(
slice
(
None
,
None
,
2
),
slice
(
-
1
,
1
,
-
1
)),
(
4
,
11
)],
],
)
def
test_static_shapes
(
x_shape
,
indices
,
expected
):
x
=
at
.
tensor
(
dtype
=
"float64"
,
shape
=
x_shape
)
y
=
x
[
indices
]
assert
y
.
type
.
shape
==
expected
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论