Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5008fab7
提交
5008fab7
authored
2月 21, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
2月 25, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplify and speedup numba DimShuffle implementation
上级
28fa7b76
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
58 行增加
和
95 行删除
+58
-95
elemwise.py
pytensor/link/numba/dispatch/elemwise.py
+25
-77
test_elemwise.py
tests/link/numba/test_elemwise.py
+6
-1
test_elemwise.py
tests/tensor/test_elemwise.py
+27
-17
没有找到文件。
pytensor/link/numba/dispatch/elemwise.py
浏览文件 @
5008fab7
...
...
@@ -4,6 +4,7 @@ from textwrap import dedent, indent
import
numba
import
numpy
as
np
from
numba.core.extending
import
overload
from
numpy.lib.stride_tricks
import
as_strided
from
pytensor.graph.op
import
Op
from
pytensor.link.numba.dispatch
import
basic
as
numba_basic
...
...
@@ -411,91 +412,38 @@ def numba_funcify_CAReduce(op, node, **kwargs):
@numba_funcify.register
(
DimShuffle
)
def
numba_funcify_DimShuffle
(
op
,
node
,
**
kwargs
):
shuffle
=
tuple
(
op
.
shuffle
)
transposition
=
tuple
(
op
.
transposition
)
augment
=
tuple
(
op
.
augment
)
# 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.
new_order
=
tuple
(
op
.
_new_order
)
shape_template
=
(
1
,)
*
node
.
outputs
[
0
]
.
ndim
strides_template
=
(
0
,)
*
node
.
outputs
[
0
]
.
ndim
ndim_new_shape
=
len
(
shuffle
)
+
len
(
augment
)
no_transpose
=
all
(
i
==
j
for
i
,
j
in
enumerate
(
transposition
))
if
no_transpose
:
@numba_basic.numba_njit
def
transpose
(
x
):
return
x
else
:
@numba_basic.numba_njit
def
transpose
(
x
):
return
np
.
transpose
(
x
,
transposition
)
shape_template
=
(
1
,)
*
ndim_new_shape
# When `len(shuffle) == 0`, the `shuffle_shape[j]` expression below
# is typed as `getitem(Tuple(), int)`, which has no implementation
# (since getting an item from an empty sequence doesn't make sense).
# To avoid this compile-time error, we omit the expression altogether.
if
len
(
shuffle
)
>
0
:
# Use the statically known shape if available
if
all
(
length
is
not
None
for
length
in
node
.
outputs
[
0
]
.
type
.
shape
):
shape
=
node
.
outputs
[
0
]
.
type
.
shape
@numba_basic.numba_njit
def
find_shape
(
array_shape
):
return
shape
else
:
@numba_basic.numba_njit
def
find_shape
(
array_shape
):
shape
=
shape_template
j
=
0
for
i
in
range
(
ndim_new_shape
):
if
i
not
in
augment
:
length
=
array_shape
[
j
]
shape
=
numba_basic
.
tuple_setitem
(
shape
,
i
,
length
)
j
=
j
+
1
return
shape
else
:
@numba_basic.numba_njit
def
find_shape
(
array_shape
):
return
shape_template
if
ndim_new_shape
>
0
:
if
new_order
==
():
# Special case needed because of https://github.com/numba/numba/issues/9933
@numba_basic.numba_njit
def
dimshuffle_inner
(
x
,
shuffle
):
x
=
transpose
(
x
)
shuffle_shape
=
x
.
shape
[:
len
(
shuffle
)]
new_shape
=
find_shape
(
shuffle_shape
)
def
squeeze_to_0d
(
x
):
return
as_strided
(
x
,
shape
=
(),
strides
=
())
# FIXME: Numba's `array.reshape` only accepts C arrays.
return
np
.
reshape
(
np
.
ascontiguousarray
(
x
),
new_shape
)
return
squeeze_to_0d
else
:
@numba_basic.numba_njit
def
dimshuffle_inner
(
x
,
shuffle
):
return
np
.
reshape
(
np
.
ascontiguousarray
(
x
),
())
# Without the following wrapper function we would see this error:
# E No implementation of function Function(<built-in function getitem>) found for signature:
# E
# E >>> getitem(UniTuple(int64 x 2), slice<a:b>)
# E
# E There are 22 candidate implementations:
# E - Of which 22 did not match due to:
# E Overload of function 'getitem': File: <numerous>: Line N/A.
# E With argument(s): '(UniTuple(int64 x 2), slice<a:b>)':
# E No match.
# ...(on this line)...
# E shuffle_shape = res.shape[: len(shuffle)]
@numba_basic.numba_njit
(
inline
=
"always"
)
def
dimshuffle
(
x
):
return
dimshuffle_inner
(
np
.
asarray
(
x
),
shuffle
)
def
dimshuffle
(
x
):
old_shape
=
x
.
shape
old_strides
=
x
.
strides
new_shape
=
shape_template
new_strides
=
strides_template
for
i
,
o
in
enumerate
(
new_order
):
if
o
!=
-
1
:
new_shape
=
numba_basic
.
tuple_setitem
(
new_shape
,
i
,
old_shape
[
o
])
new_strides
=
numba_basic
.
tuple_setitem
(
new_strides
,
i
,
old_strides
[
o
]
)
return
as_strided
(
x
,
shape
=
new_shape
,
strides
=
new_strides
)
return
dimshuffle
...
...
tests/link/numba/test_elemwise.py
浏览文件 @
5008fab7
...
...
@@ -23,6 +23,7 @@ from tests.link.numba.test_basic import (
from
tests.tensor.test_elemwise
import
(
careduce_benchmark_tester
,
check_elemwise_runtime_broadcast
,
dimshuffle_benchmark
,
)
...
...
@@ -201,7 +202,7 @@ def test_Dimshuffle_returns_array():
def
test_Dimshuffle_non_contiguous
():
"""The numba impl of reshape doesn't work with
non-contiguous arrays, make sure we work around th
p
t."""
non-contiguous arrays, make sure we work around th
a
t."""
x
=
pt
.
dvector
()
idx
=
pt
.
vector
(
dtype
=
"int64"
)
op
=
DimShuffle
(
input_ndim
=
1
,
new_order
=
[])
...
...
@@ -643,3 +644,7 @@ class TestsBenchmark:
return
careduce_benchmark_tester
(
axis
,
c_contiguous
,
mode
=
"NUMBA"
,
benchmark
=
benchmark
)
@pytest.mark.parametrize
(
"c_contiguous"
,
(
True
,
False
))
def
test_dimshuffle
(
self
,
c_contiguous
,
benchmark
):
dimshuffle_benchmark
(
"NUMBA"
,
c_contiguous
,
benchmark
)
tests/tensor/test_elemwise.py
浏览文件 @
5008fab7
...
...
@@ -66,6 +66,30 @@ def reduce_bitwise_and(x, axis=-1, dtype="int8"):
return
np
.
apply_along_axis
(
custom_reduce
,
axis
,
x
)
def
dimshuffle_benchmark
(
mode
,
c_contiguous
,
benchmark
):
x
=
tensor3
(
"x"
)
if
c_contiguous
:
x_val
=
np
.
random
.
random
((
2
,
3
,
4
))
.
astype
(
config
.
floatX
)
else
:
x_val
=
np
.
random
.
random
((
200
,
300
,
400
))
.
transpose
(
1
,
2
,
0
)
ys
=
[
x
.
transpose
(
t
)
for
t
in
itertools
.
permutations
((
0
,
1
,
2
))]
ys
+=
[
x
[
None
],
x
[:,
None
],
x
[:,
:,
None
],
x
[:,
:,
:,
None
],
]
# Borrow to avoid deepcopy overhead
fn
=
pytensor
.
function
(
[
In
(
x
,
borrow
=
True
)],
[
Out
(
y
,
borrow
=
True
)
for
y
in
ys
],
mode
=
mode
,
)
fn
.
trust_input
=
True
fn
(
x_val
)
# JIT compile for JIT backends
benchmark
(
fn
,
x_val
)
class
TestDimShuffle
(
unittest_tools
.
InferShapeTester
):
op
=
DimShuffle
type
=
TensorType
...
...
@@ -218,23 +242,9 @@ class TestDimShuffle(unittest_tools.InferShapeTester):
with
pytest
.
raises
(
TypeError
,
match
=
"input_ndim must be an integer"
):
DimShuffle
(
input_ndim
=
(
True
,
False
),
new_order
=
(
1
,
0
))
def
test_benchmark
(
self
,
benchmark
):
x
=
tensor3
(
"x"
)
x_val
=
np
.
random
.
random
((
2
,
3
,
4
))
.
astype
(
config
.
floatX
)
ys
=
[
x
.
transpose
(
t
)
for
t
in
itertools
.
permutations
((
0
,
1
,
2
))]
ys
+=
[
x
[
None
],
x
[:,
None
],
x
[:,
:,
None
],
x
[:,
:,
:,
None
],
]
# Borrow to avoid deepcopy overhead
fn
=
pytensor
.
function
(
[
In
(
x
,
borrow
=
True
)],
[
Out
(
y
,
borrow
=
True
)
for
y
in
ys
],
)
fn
.
trust_input
=
True
benchmark
(
fn
,
x_val
)
@pytest.mark.parametrize
(
"c_contiguous"
,
[
True
,
False
])
def
test_benchmark
(
self
,
c_contiguous
,
benchmark
):
dimshuffle_benchmark
(
"FAST_RUN"
,
c_contiguous
,
benchmark
)
class
TestBroadcast
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论