Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0bb15f9d
提交
0bb15f9d
authored
6月 26, 2025
作者:
ricardoV94
提交者:
Ricardo Vieira
6月 26, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix xtensor Transpose with ellipsis
上级
e03605e4
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
26 行增加
和
20 行删除
+26
-20
shape.py
pytensor/xtensor/shape.py
+20
-14
type.py
pytensor/xtensor/type.py
+3
-3
test_shape.py
tests/xtensor/test_shape.py
+3
-3
没有找到文件。
pytensor/xtensor/shape.py
浏览文件 @
0bb15f9d
...
@@ -189,7 +189,7 @@ class Transpose(XOp):
...
@@ -189,7 +189,7 @@ class Transpose(XOp):
def
transpose
(
def
transpose
(
x
,
x
,
*
dim
s
:
str
|
EllipsisType
,
*
dim
:
str
|
EllipsisType
,
missing_dims
:
Literal
[
"raise"
,
"warn"
,
"ignore"
]
=
"raise"
,
missing_dims
:
Literal
[
"raise"
,
"warn"
,
"ignore"
]
=
"raise"
,
):
):
"""Transpose dimensions of the tensor.
"""Transpose dimensions of the tensor.
...
@@ -198,7 +198,7 @@ def transpose(
...
@@ -198,7 +198,7 @@ def transpose(
----------
----------
x : XTensorVariable
x : XTensorVariable
Input tensor to transpose.
Input tensor to transpose.
*dim
s
: str
*dim : str
Dimensions to transpose to. Can include ellipsis (...) to represent
Dimensions to transpose to. Can include ellipsis (...) to represent
remaining dimensions in their original order.
remaining dimensions in their original order.
missing_dims : {"raise", "warn", "ignore"}, optional
missing_dims : {"raise", "warn", "ignore"}, optional
...
@@ -220,7 +220,7 @@ def transpose(
...
@@ -220,7 +220,7 @@ def transpose(
# Validate dimensions
# Validate dimensions
x
=
as_xtensor
(
x
)
x
=
as_xtensor
(
x
)
x_dims
=
x
.
type
.
dims
x_dims
=
x
.
type
.
dims
invalid_dims
=
set
(
dim
s
)
-
{
...
,
*
x_dims
}
invalid_dims
=
set
(
dim
)
-
{
...
,
*
x_dims
}
if
invalid_dims
:
if
invalid_dims
:
if
missing_dims
!=
"ignore"
:
if
missing_dims
!=
"ignore"
:
msg
=
f
"Dimensions {invalid_dims} do not exist. Expected one or more of: {x_dims}"
msg
=
f
"Dimensions {invalid_dims} do not exist. Expected one or more of: {x_dims}"
...
@@ -229,21 +229,27 @@ def transpose(
...
@@ -229,21 +229,27 @@ def transpose(
else
:
else
:
warnings
.
warn
(
msg
)
warnings
.
warn
(
msg
)
# Handle missing dimensions if not raising
# Handle missing dimensions if not raising
dims
=
tuple
(
d
for
d
in
dims
if
d
in
x_dims
or
d
is
...
)
dim
=
tuple
(
d
for
d
in
dim
if
d
in
x_dims
or
d
is
...
)
if
dims
==
()
or
dims
==
(
...
,):
if
dim
==
():
dims
=
tuple
(
reversed
(
x_dims
))
dim
=
tuple
(
reversed
(
x_dims
))
elif
...
in
dims
:
elif
dim
==
(
...
,):
if
dims
.
count
(
...
)
>
1
:
dim
=
x_dims
elif
...
in
dim
:
if
dim
.
count
(
...
)
>
1
:
raise
ValueError
(
"Ellipsis (...) can only appear once in the dimensions"
)
raise
ValueError
(
"Ellipsis (...) can only appear once in the dimensions"
)
# Handle ellipsis expansion
# Handle ellipsis expansion
ellipsis_idx
=
dim
s
.
index
(
...
)
ellipsis_idx
=
dim
.
index
(
...
)
pre
=
dim
s
[:
ellipsis_idx
]
pre
=
dim
[:
ellipsis_idx
]
post
=
dim
s
[
ellipsis_idx
+
1
:]
post
=
dim
[
ellipsis_idx
+
1
:]
middle
=
[
d
for
d
in
x_dims
if
d
not
in
pre
+
post
]
middle
=
[
d
for
d
in
x_dims
if
d
not
in
pre
+
post
]
dims
=
(
*
pre
,
*
middle
,
*
post
)
dim
=
(
*
pre
,
*
middle
,
*
post
)
if
dim
==
x_dims
:
# No-op transpose
return
x
return
Transpose
(
typing
.
cast
(
tuple
[
str
],
dims
))(
x
)
return
Transpose
(
dims
=
typing
.
cast
(
tuple
[
str
],
dim
))(
x
)
class
Concat
(
XOp
):
class
Concat
(
XOp
):
...
...
pytensor/xtensor/type.py
浏览文件 @
0bb15f9d
...
@@ -691,14 +691,14 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
...
@@ -691,14 +691,14 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
# https://docs.xarray.dev/en/latest/api.html#id8
# https://docs.xarray.dev/en/latest/api.html#id8
def
transpose
(
def
transpose
(
self
,
self
,
*
dim
s
:
str
|
EllipsisType
,
*
dim
:
str
|
EllipsisType
,
missing_dims
:
Literal
[
"raise"
,
"warn"
,
"ignore"
]
=
"raise"
,
missing_dims
:
Literal
[
"raise"
,
"warn"
,
"ignore"
]
=
"raise"
,
):
):
"""Transpose dimensions of the tensor.
"""Transpose dimensions of the tensor.
Parameters
Parameters
----------
----------
*dim
s
: str | Ellipsis
*dim : str | Ellipsis
Dimensions to transpose. If empty, performs a full transpose.
Dimensions to transpose. If empty, performs a full transpose.
Can use ellipsis (...) to represent remaining dimensions.
Can use ellipsis (...) to represent remaining dimensions.
missing_dims : {"raise", "warn", "ignore"}, default="raise"
missing_dims : {"raise", "warn", "ignore"}, default="raise"
...
@@ -718,7 +718,7 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
...
@@ -718,7 +718,7 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
If missing_dims="raise" and any dimensions don't exist.
If missing_dims="raise" and any dimensions don't exist.
If multiple ellipsis are provided.
If multiple ellipsis are provided.
"""
"""
return
px
.
shape
.
transpose
(
self
,
*
dim
s
,
missing_dims
=
missing_dims
)
return
px
.
shape
.
transpose
(
self
,
*
dim
,
missing_dims
=
missing_dims
)
def
stack
(
self
,
dim
,
**
dims
):
def
stack
(
self
,
dim
,
**
dims
):
return
px
.
shape
.
stack
(
self
,
dim
,
**
dims
)
return
px
.
shape
.
stack
(
self
,
dim
,
**
dims
)
...
...
tests/xtensor/test_shape.py
浏览文件 @
0bb15f9d
...
@@ -15,7 +15,6 @@ from pytensor.tensor import scalar
...
@@ -15,7 +15,6 @@ from pytensor.tensor import scalar
from
pytensor.xtensor.shape
import
(
from
pytensor.xtensor.shape
import
(
concat
,
concat
,
stack
,
stack
,
transpose
,
unstack
,
unstack
,
)
)
from
pytensor.xtensor.type
import
xtensor
from
pytensor.xtensor.type
import
xtensor
...
@@ -46,13 +45,14 @@ def test_transpose():
...
@@ -46,13 +45,14 @@ def test_transpose():
permutations
=
[
permutations
=
[
(
a
,
b
,
c
,
d
,
e
),
# identity
(
a
,
b
,
c
,
d
,
e
),
# identity
(
e
,
d
,
c
,
b
,
a
),
# full tranpose
(
e
,
d
,
c
,
b
,
a
),
# full tranpose
(),
# eqivalent to full transpose
(),
# eq
u
ivalent to full transpose
(
a
,
b
,
c
,
e
,
d
),
# swap last two dims
(
a
,
b
,
c
,
e
,
d
),
# swap last two dims
(
...
,
d
,
c
),
# equivalent to (a, b, e, d, c)
(
...
,
d
,
c
),
# equivalent to (a, b, e, d, c)
(
b
,
a
,
...
,
e
,
d
),
# equivalent to (b, a, c, d, e)
(
b
,
a
,
...
,
e
,
d
),
# equivalent to (b, a, c, d, e)
(
c
,
a
,
...
),
# equivalent to (c, a, b, d, e)
(
c
,
a
,
...
),
# equivalent to (c, a, b, d, e)
(
...
,),
# no op
]
]
outs
=
[
transpose
(
x
,
*
perm
)
for
perm
in
permutations
]
outs
=
[
x
.
transpose
(
*
perm
)
for
perm
in
permutations
]
fn
=
xr_function
([
x
],
outs
)
fn
=
xr_function
([
x
],
outs
)
x_test
=
xr_arange_like
(
x
)
x_test
=
xr_arange_like
(
x
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论