Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4b897162
提交
4b897162
authored
12月 18, 2025
作者:
jessegrabowski
提交者:
Ricardo Vieira
12月 19, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement `Pack` and `Unpack`
上级
a0be97e8
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
189 行增加
和
1 行删除
+189
-1
reshape.py
pytensor/tensor/reshape.py
+0
-0
test_reshape.py
tests/tensor/test_reshape.py
+189
-1
没有找到文件。
pytensor/tensor/reshape.py
浏览文件 @
4b897162
差异被折叠。
点击展开。
tests/tensor/test_reshape.py
浏览文件 @
4b897162
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
import
pytensor
from
pytensor
import
config
,
function
from
pytensor
import
config
,
function
from
pytensor
import
tensor
as
pt
from
pytensor
import
tensor
as
pt
from
pytensor.graph
import
vectorize_graph
from
pytensor.graph
import
rewrite_graph
,
vectorize_graph
from
pytensor.tensor.reshape
import
(
from
pytensor.tensor.reshape
import
(
_analyze_axes_list
,
join_dims
,
join_dims
,
pack
,
split_dims
,
split_dims
,
unpack
,
)
)
...
@@ -95,3 +99,187 @@ def test_split_size_zero_shape():
...
@@ -95,3 +99,187 @@ def test_split_size_zero_shape():
x_split_value
=
fn
(
x_value
)
x_split_value
=
fn
(
x_value
)
np
.
testing
.
assert_allclose
(
x_split_value
,
x_value
.
squeeze
(
0
))
np
.
testing
.
assert_allclose
(
x_split_value
,
x_value
.
squeeze
(
0
))
def
test_make_replacements_with_pack_unpack
():
rng
=
np
.
random
.
default_rng
()
x
=
pt
.
tensor
(
"x"
,
shape
=
())
y
=
pt
.
tensor
(
"y"
,
shape
=
(
5
,))
z
=
pt
.
tensor
(
"z"
,
shape
=
(
3
,
3
))
loss
=
(
x
+
y
.
sum
()
+
z
.
sum
())
**
2
flat_packed
,
packed_shapes
=
pack
(
x
,
y
,
z
,
axes
=
None
)
new_input
=
flat_packed
.
type
()
new_outputs
=
unpack
(
new_input
,
axes
=
None
,
packed_shapes
=
packed_shapes
)
loss
=
pytensor
.
graph
.
graph_replace
(
loss
,
dict
(
zip
([
x
,
y
,
z
],
new_outputs
)))
rewrite_graph
(
loss
,
include
=
(
"ShapeOpt"
,
"specialize"
))
fn
=
pytensor
.
function
([
new_input
],
loss
,
mode
=
"FAST_COMPILE"
)
input_vals
=
[
rng
.
normal
(
size
=
(
var
.
type
.
shape
))
.
astype
(
config
.
floatX
)
for
var
in
[
x
,
y
,
z
]
]
flat_inputs
=
np
.
concatenate
([
input
.
ravel
()
for
input
in
input_vals
],
axis
=
0
)
output_val
=
fn
(
flat_inputs
)
assert
np
.
allclose
(
output_val
,
sum
([
input
.
sum
()
for
input
in
input_vals
])
**
2
)
class
TestPack
:
@pytest.mark.parametrize
(
"axes, expected"
,
[
(
None
,
[
0
,
0
,
0
]),
# '*'
([
0
,
1
],
[
2
,
0
,
2
]),
# 'i j *'
([
-
1
],
[
0
,
1
,
1
]),
# '* k'
([
-
2
,
-
1
],
[
0
,
2
,
2
]),
# '* i j'
([
0
,
-
1
],
[
1
,
1
,
2
]),
# 'i * k'
([
0
,
1
,
2
,
-
1
],
[
3
,
1
,
4
]),
# 'i j k * l'
],
ids
=
[
"ravel_all"
,
"keep_first_two"
,
"keep_last"
,
"ravel_start"
,
"first_and_last"
,
"complex_case"
,
],
)
def
test_analyze_axes_list_valid
(
self
,
axes
,
expected
):
outputs
=
_analyze_axes_list
(
axes
)
names
=
[
"n_before"
,
"n_after"
,
"min_axes"
]
for
out
,
exp
,
name
in
zip
(
outputs
,
expected
,
names
,
strict
=
True
):
assert
out
==
exp
,
f
"Expected {exp}, got {out} for {name}"
def
test_analyze_axes_list_invalid
(
self
):
# Positive only but not contiguous
with
pytest
.
raises
(
ValueError
,
match
=
"Positive axes must be contiguous"
):
_analyze_axes_list
([
1
,
3
])
# Negative only but not contiguous
with
pytest
.
raises
(
ValueError
,
match
=
"Negative axes must be contiguous"
):
_analyze_axes_list
([
-
3
,
-
1
])
# Mixed up positive and negative
with
pytest
.
raises
(
ValueError
,
match
=
"Negative axes must come after positive"
):
_analyze_axes_list
([
0
,
1
,
-
2
,
4
])
# Duplicate axes
with
pytest
.
raises
(
ValueError
,
match
=
"axes must have no duplicates"
):
_analyze_axes_list
([
0
,
0
])
# Not monotonic
with
pytest
.
raises
(
ValueError
,
match
=
"Axes must be strictly increasing"
):
_analyze_axes_list
([
0
,
2
,
1
])
# Negative before positive
with
pytest
.
raises
(
ValueError
,
match
=
"Negative axes must come after positive"
):
_analyze_axes_list
([
-
1
,
0
])
def
test_pack_basic
(
self
):
# rng = np.random.default_rng()
x
=
pt
.
tensor
(
"x"
,
shape
=
())
y
=
pt
.
tensor
(
"y"
,
shape
=
(
5
,))
z
=
pt
.
tensor
(
"z"
,
shape
=
(
3
,
3
))
input_dict
=
{
variable
.
name
:
np
.
zeros
(
variable
.
type
.
shape
,
dtype
=
config
.
floatX
)
for
variable
in
[
x
,
y
,
z
]
}
# Simple case, reduce all axes, equivalent to einops '*'
packed_tensor
,
packed_shapes
=
pack
(
x
,
y
,
z
,
axes
=
None
)
assert
packed_tensor
.
type
.
shape
==
(
15
,)
for
tensor
,
packed_shape
in
zip
([
x
,
y
,
z
],
packed_shapes
):
assert
packed_shape
.
type
.
shape
==
(
tensor
.
ndim
,)
np
.
testing
.
assert_allclose
(
packed_shape
.
eval
(
input_dict
,
on_unused_input
=
"ignore"
),
tensor
.
type
.
shape
,
)
# To preserve an axis, all inputs need at least one dimension, and the preserved axis has to agree.
# x is scalar, so pack will raise:
with
pytest
.
raises
(
ValueError
,
match
=
r"Input 0 \(zero indexed\) to pack has 0 dimensions, but axes=0 assumes at least 1 dimension\."
,
):
pack
(
x
,
y
,
z
,
axes
=
0
)
# With valid x, pack should still raise, because the axis of concatenation doesn't agree across all inputs
x
=
pt
.
tensor
(
"x"
,
shape
=
(
3
,))
input_dict
[
"x"
]
=
np
.
zeros
((
3
,),
dtype
=
config
.
floatX
)
with
pytest
.
raises
(
ValueError
,
match
=
r"all input array dimensions other than the specified `axis` \(1\) must match exactly, or be unknown "
r"\(None\), but along dimension 0, the inputs shapes are incompatible: \[3 5 3\]"
,
):
packed_tensor
,
packed_shapes
=
pack
(
x
,
y
,
z
,
axes
=
0
)
packed_tensor
.
eval
(
input_dict
)
# Valid case, preserve first axis, equivalent to einops 'i *'
y
=
pt
.
tensor
(
"y"
,
shape
=
(
3
,
5
))
z
=
pt
.
tensor
(
"z"
,
shape
=
(
3
,
3
,
3
))
packed_tensor
,
packed_shapes
=
pack
(
x
,
y
,
z
,
axes
=
0
)
input_dict
=
{
variable
.
name
:
np
.
zeros
(
variable
.
type
.
shape
,
dtype
=
config
.
floatX
)
for
variable
in
[
x
,
y
,
z
]
}
assert
packed_tensor
.
type
.
shape
==
(
3
,
15
)
for
tensor
,
packed_shape
in
zip
([
x
,
y
,
z
],
packed_shapes
):
assert
packed_shape
.
type
.
shape
==
(
tensor
.
ndim
-
1
,)
np
.
testing
.
assert_allclose
(
packed_shape
.
eval
(
input_dict
,
on_unused_input
=
"ignore"
),
tensor
.
type
.
shape
[
1
:],
)
# More complex case, preserve last axis implicitly, equivalent to einops 'i * k'. This introduces a max
# dimension condition on the input shapes
x
=
pt
.
tensor
(
"x"
,
shape
=
(
3
,
2
))
y
=
pt
.
tensor
(
"y"
,
shape
=
(
3
,
5
,
2
))
z
=
pt
.
tensor
(
"z"
,
shape
=
(
3
,
1
,
7
,
5
,
2
))
with
pytest
.
raises
(
ValueError
,
match
=
r"Positive axes must be contiguous"
,
):
pack
(
x
,
y
,
z
,
axes
=
[
0
,
3
])
z
=
pt
.
tensor
(
"z"
,
shape
=
(
3
,
1
,
7
,
2
))
packed_tensor
,
packed_shapes
=
pack
(
x
,
y
,
z
,
axes
=
[
0
,
-
1
])
input_dict
=
{
variable
.
name
:
np
.
zeros
(
variable
.
type
.
shape
,
dtype
=
config
.
floatX
)
for
variable
in
[
x
,
y
,
z
]
}
assert
packed_tensor
.
type
.
shape
==
(
3
,
13
,
2
)
for
tensor
,
packed_shape
in
zip
([
x
,
y
,
z
],
packed_shapes
):
assert
packed_shape
.
type
.
shape
==
(
tensor
.
ndim
-
2
,)
np
.
testing
.
assert_allclose
(
packed_shape
.
eval
(
input_dict
,
on_unused_input
=
"ignore"
),
tensor
.
type
.
shape
[
1
:
-
1
],
)
@pytest.mark.parametrize
(
"axes"
,
[
-
1
])
def
test_pack_unpack_round_trip
(
self
,
axes
):
rng
=
np
.
random
.
default_rng
()
x
=
pt
.
tensor
(
"x"
,
shape
=
(
3
,
5
))
y
=
pt
.
tensor
(
"y"
,
shape
=
(
3
,
3
,
5
))
z
=
pt
.
tensor
(
"z"
,
shape
=
(
1
,
3
,
5
))
flat_packed
,
packed_shapes
=
pack
(
x
,
y
,
z
,
axes
=
axes
)
new_outputs
=
unpack
(
flat_packed
,
axes
=
axes
,
packed_shapes
=
packed_shapes
)
fn
=
pytensor
.
function
([
x
,
y
,
z
],
new_outputs
,
mode
=
"FAST_COMPILE"
)
input_dict
=
{
var
.
name
:
rng
.
normal
(
size
=
var
.
type
.
shape
)
.
astype
(
config
.
floatX
)
for
var
in
[
x
,
y
,
z
]
}
output_vals
=
fn
(
**
input_dict
)
for
input_val
,
output_val
in
zip
(
input_dict
.
values
(),
output_vals
,
strict
=
True
):
np
.
testing
.
assert_allclose
(
input_val
,
output_val
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论