Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ec574156
提交
ec574156
authored
11月 30, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
12月 05, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Numba UnravelIndex: Handle arbitrary indices ndim and F-order
上级
41868191
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
71 行增加
和
42 行删除
+71
-42
extra_ops.py
pytensor/link/numba/dispatch/extra_ops.py
+44
-25
extra_ops.py
pytensor/tensor/extra_ops.py
+2
-4
test_extra_ops.py
tests/link/numba/test_extra_ops.py
+25
-13
没有找到文件。
pytensor/link/numba/dispatch/extra_ops.py
浏览文件 @
ec574156
...
@@ -261,41 +261,60 @@ def numba_funcify_Unique(op, node, **kwargs):
...
@@ -261,41 +261,60 @@ def numba_funcify_Unique(op, node, **kwargs):
@register_funcify_and_cache_key
(
UnravelIndex
)
@register_funcify_and_cache_key
(
UnravelIndex
)
def
numba_funcify_UnravelIndex
(
op
,
node
,
**
kwargs
):
def
numba_funcify_UnravelIndex
(
op
,
node
,
**
kwargs
):
order
=
op
.
order
out_ndim
=
node
.
outputs
[
0
]
.
type
.
ndim
if
order
!=
"C"
:
raise
NotImplementedError
(
"Numba does not support the `order` argument in `numpy.unravel_index`"
)
if
len
(
node
.
outputs
)
==
1
:
if
out_ndim
==
0
:
# Creating a tuple of 0d arrays in numba is basically impossible without codegen, so just go to obj_mode
@numba_basic.numba_njit
(
inline
=
"always"
)
return
generate_fallback_impl
(
op
,
node
=
node
),
None
def
maybe_expand_dim
(
arr
):
return
arr
else
:
@numba_basic.numba_njit
(
inline
=
"always"
)
c_order
=
op
.
order
==
"C"
def
maybe_expand_dim
(
arr
):
inp_ndim
=
node
.
inputs
[
0
]
.
type
.
ndim
return
np
.
expand_dims
(
arr
,
1
)
transpose_axes
=
(
inp_ndim
,
*
range
(
inp_ndim
)
)
@numba_basic.numba_njit
@numba_basic.numba_njit
def
unravelindex
(
arr
,
shape
):
def
unravelindex
(
indices
,
shape
):
a
=
np
.
ones
(
len
(
shape
),
dtype
=
np
.
int64
)
a
=
np
.
ones
(
len
(
shape
),
dtype
=
np
.
int64
)
a
[
1
:]
=
shape
[:
0
:
-
1
]
if
c_order
:
a
=
np
.
cumprod
(
a
)[::
-
1
]
# C-Order: Reverse shape (ignore dim0), cumulative product, then reverse back
# Strides: [dim1*dim2, dim2, 1]
a
[
1
:]
=
shape
[:
0
:
-
1
]
a
=
np
.
cumprod
(
a
)[::
-
1
]
else
:
# F-Order: Standard shape, cumulative product
# Strides: [1, dim0, dim0*dim1]
a
[
1
:]
=
shape
[:
-
1
]
a
=
np
.
cumprod
(
a
)
# Broadcast with a and shape on the last axis
unraveled_coords
=
(
indices
[
...
,
None
]
//
a
)
%
shape
#
PyTensor actually returns a `tuple` of these values, instead of an
#
Then transpose it to the front
#
`ndarray`; however, this `ndarray` result should be able to b
e
#
Numba doesn't have moveaxis (why would it), so we use transpos
e
#
unpacked into a `tuple`, so this discrepancy shouldn't really matter
#
res = np.moveaxis(res, -1, 0)
return
((
maybe_expand_dim
(
arr
)
//
a
)
%
shape
)
.
T
unraveled_coords
=
unraveled_coords
.
transpose
(
transpose_axes
)
# This should be a tuple, but the array can be unpacked
# into multiple variables with the same effect by the outer function
# (special case for single entry is handled with an outer function below)
return
unraveled_coords
cache_version
=
1
cache_key
=
sha256
(
cache_key
=
sha256
(
str
((
type
(
op
),
op
.
order
,
len
(
node
.
outputs
)))
.
encode
()
str
((
type
(
op
),
op
.
order
,
len
(
node
.
outputs
)
,
cache_version
))
.
encode
()
)
.
hexdigest
()
)
.
hexdigest
()
return
unravelindex
,
cache_key
if
len
(
node
.
outputs
)
==
1
:
@numba_basic.numba_njit
def
unravel_index_single_item
(
arr
,
shape
):
# Unpack single entry
(
res
,)
=
unravelindex
(
arr
,
shape
)
return
res
return
unravel_index_single_item
,
cache_key
else
:
return
unravelindex
,
cache_key
@register_funcify_default_op_cache_key
(
SearchsortedOp
)
@register_funcify_default_op_cache_key
(
SearchsortedOp
)
...
...
pytensor/tensor/extra_ops.py
浏览文件 @
ec574156
...
@@ -1304,13 +1304,11 @@ class UnravelIndex(Op):
...
@@ -1304,13 +1304,11 @@ class UnravelIndex(Op):
if
dims
.
ndim
!=
1
:
if
dims
.
ndim
!=
1
:
raise
TypeError
(
"dims must be a 1D array"
)
raise
TypeError
(
"dims must be a 1D array"
)
out_type
=
indices
.
type
.
clone
(
dtype
=
"int64"
)
return
Apply
(
return
Apply
(
self
,
self
,
[
indices
,
dims
],
[
indices
,
dims
],
[
[
out_type
()
for
_i
in
range
(
ptb
.
get_vector_length
(
dims
))],
TensorType
(
dtype
=
"int64"
,
shape
=
(
None
,)
*
indices
.
type
.
ndim
)()
for
i
in
range
(
ptb
.
get_vector_length
(
dims
))
],
)
)
def
infer_shape
(
self
,
fgraph
,
node
,
input_shapes
):
def
infer_shape
(
self
,
fgraph
,
node
,
input_shapes
):
...
...
tests/link/numba/test_extra_ops.py
浏览文件 @
ec574156
import
contextlib
import
contextlib
from
contextlib
import
nullcontext
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
...
@@ -295,37 +296,48 @@ def test_Unique(x, axis, return_index, return_inverse, return_counts, exc):
...
@@ -295,37 +296,48 @@ def test_Unique(x, axis, return_index, return_inverse, return_counts, exc):
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"arr, shape,
order, exc
"
,
"arr, shape,
requires_obj_mode
"
,
[
[
(
(
pt
.
lscalar
(),
np
.
array
(
9
,
dtype
=
"int64"
)),
pt
.
as_tensor
([
2
,
3
,
4
]),
True
,
),
(
(
(
pt
.
lvector
(),
np
.
array
([
9
,
15
,
1
],
dtype
=
"int64"
)),
(
pt
.
lvector
(),
np
.
array
([
9
,
15
,
1
],
dtype
=
"int64"
)),
pt
.
as_tensor
([
2
,
3
,
4
]),
pt
.
as_tensor
([
2
,
3
,
4
]),
"C"
,
False
,
None
,
),
),
(
(
(
pt
.
lvector
(),
np
.
array
([
1
,
0
],
dtype
=
"int64"
)),
(
pt
.
lvector
(),
np
.
array
([
1
,
0
],
dtype
=
"int64"
)),
pt
.
as_tensor
([
2
]),
pt
.
as_tensor
([
2
]),
"C"
,
False
,
None
,
),
),
(
(
(
pt
.
l
vector
(),
np
.
array
([
9
,
15
,
1
],
dtype
=
"int64"
)),
(
pt
.
l
matrix
(),
np
.
array
([[
9
,
15
,
1
],
[
1
,
9
,
15
]
],
dtype
=
"int64"
)),
pt
.
as_tensor
([
2
,
3
,
4
]),
pt
.
as_tensor
([
2
,
3
,
4
]),
"F"
,
False
,
NotImplementedError
,
),
),
],
],
)
)
def
test_UnravelIndex
(
arr
,
shape
,
order
,
exc
):
def
test_UnravelIndex
(
arr
,
shape
,
requires_obj_mode
):
arr
,
test_arr
=
arr
arr
,
test_arr
=
arr
g
=
extra_ops
.
UnravelIndex
(
order
)(
arr
,
shape
)
g_c
=
extra_ops
.
UnravelIndex
(
"C"
)(
arr
,
shape
)
g_f
=
extra_ops
.
UnravelIndex
(
"F"
)(
arr
,
shape
)
cm
=
contextlib
.
suppress
()
if
exc
is
None
else
pytest
.
raises
(
exc
)
if
shape
.
type
.
shape
==
(
1
,):
outputs
=
[
g_c
,
g_f
]
else
:
outputs
=
[
*
g_c
,
*
g_f
]
cm
=
(
pytest
.
warns
(
UserWarning
,
match
=
"object mode"
)
if
requires_obj_mode
else
nullcontext
()
)
with
cm
:
with
cm
:
compare_numba_and_py
(
compare_numba_and_py
(
[
arr
],
[
arr
],
g
,
outputs
,
[
test_arr
],
[
test_arr
],
)
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论