Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3ff76039
提交
3ff76039
authored
11月 25, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
12月 05, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Numba DimShuffle: validate squeeze
上级
4298b761
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
27 行增加
和
2 行删除
+27
-2
elemwise.py
pytensor/link/numba/dispatch/elemwise.py
+15
-2
test_elemwise.py
tests/link/numba/test_elemwise.py
+12
-0
没有找到文件。
pytensor/link/numba/dispatch/elemwise.py
浏览文件 @
3ff76039
...
@@ -397,10 +397,11 @@ def numba_funcify_CAReduce(op, node, **kwargs):
...
@@ -397,10 +397,11 @@ def numba_funcify_CAReduce(op, node, **kwargs):
@register_funcify_default_op_cache_key
(
DimShuffle
)
@register_funcify_default_op_cache_key
(
DimShuffle
)
def
numba_funcify_DimShuffle
(
op
,
node
,
**
kwargs
):
def
numba_funcify_DimShuffle
(
op
:
DimShuffle
,
node
,
**
kwargs
):
# We use `as_strided` to achieve the DimShuffle behavior of transposing and expanding/squezing dimensions in one call
# We use `as_strided` to achieve the DimShuffle behavior of transposing and expanding/squezing dimensions in one call
# Numba doesn't currently support multiple expand/squeeze, and reshape is limited to contiguous arrays.
# Numba doesn't currently support multiple expand/squeeze, and reshape is limited to contiguous arrays.
new_order
=
tuple
(
op
.
_new_order
)
new_order
=
tuple
(
op
.
_new_order
)
drop
=
tuple
(
op
.
drop
)
shape_template
=
(
1
,)
*
node
.
outputs
[
0
]
.
ndim
shape_template
=
(
1
,)
*
node
.
outputs
[
0
]
.
ndim
strides_template
=
(
0
,)
*
node
.
outputs
[
0
]
.
ndim
strides_template
=
(
0
,)
*
node
.
outputs
[
0
]
.
ndim
...
@@ -409,6 +410,11 @@ def numba_funcify_DimShuffle(op, node, **kwargs):
...
@@ -409,6 +410,11 @@ def numba_funcify_DimShuffle(op, node, **kwargs):
@numba_basic.numba_njit
@numba_basic.numba_njit
def
squeeze_to_0d
(
x
):
def
squeeze_to_0d
(
x
):
if
not
x
.
size
==
1
:
raise
ValueError
(
"DimShuffle: Attempting to squeeze axes with size not equal to one"
)
assert
x
.
size
==
1
return
as_strided
(
x
,
shape
=
(),
strides
=
())
return
as_strided
(
x
,
shape
=
(),
strides
=
())
return
squeeze_to_0d
return
squeeze_to_0d
...
@@ -428,10 +434,17 @@ def numba_funcify_DimShuffle(op, node, **kwargs):
...
@@ -428,10 +434,17 @@ def numba_funcify_DimShuffle(op, node, **kwargs):
new_strides
=
numba_basic
.
tuple_setitem
(
new_strides
=
numba_basic
.
tuple_setitem
(
new_strides
,
i
,
old_strides
[
o
]
new_strides
,
i
,
old_strides
[
o
]
)
)
if
drop
:
for
dropped_dim
in
drop
:
if
old_shape
[
dropped_dim
]
!=
1
:
raise
ValueError
(
"DimShuffle: Attempting to squeeze axes with size not equal to one"
)
return
as_strided
(
x
,
shape
=
new_shape
,
strides
=
new_strides
)
return
as_strided
(
x
,
shape
=
new_shape
,
strides
=
new_strides
)
return
dimshuffle
cache_version
=
1
return
dimshuffle
,
cache_version
@register_funcify_default_op_cache_key
(
Softmax
)
@register_funcify_default_op_cache_key
(
Softmax
)
...
...
tests/link/numba/test_elemwise.py
浏览文件 @
3ff76039
...
@@ -19,6 +19,7 @@ from pytensor.tensor.math import All, Any, Max, Min, Prod, ProdWithoutZeros, Sum
...
@@ -19,6 +19,7 @@ from pytensor.tensor.math import All, Any, Max, Min, Prod, ProdWithoutZeros, Sum
from
pytensor.tensor.special
import
LogSoftmax
,
Softmax
,
SoftmaxGrad
from
pytensor.tensor.special
import
LogSoftmax
,
Softmax
,
SoftmaxGrad
from
tests.link.numba.test_basic
import
(
from
tests.link.numba.test_basic
import
(
compare_numba_and_py
,
compare_numba_and_py
,
numba_mode
,
scalar_my_multi_out
,
scalar_my_multi_out
,
)
)
from
tests.tensor.test_elemwise
import
(
from
tests.tensor.test_elemwise
import
(
...
@@ -217,6 +218,17 @@ def test_Dimshuffle_non_contiguous():
...
@@ -217,6 +218,17 @@ def test_Dimshuffle_non_contiguous():
assert
func
(
np
.
zeros
(
3
),
np
.
array
([
1
]))
.
ndim
==
0
assert
func
(
np
.
zeros
(
3
),
np
.
array
([
1
]))
.
ndim
==
0
def
test_Dimshuffle_squeeze_errors
():
x
=
pt
.
tensor3
(
"x"
,
shape
=
(
4
,
None
,
5
))
out
=
pt
.
squeeze
(
x
,
axis
=
1
)
assert
out
.
type
.
shape
==
(
4
,
5
)
fn
=
function
([
x
],
out
,
mode
=
numba_mode
)
with
pytest
.
raises
(
ValueError
,
match
=
"Attempting to squeeze axes with size not equal to one"
):
fn
(
np
.
zeros
((
4
,
2
,
5
)))
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"careduce_fn, axis, v"
,
"careduce_fn, axis, v"
,
[
[
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论