Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
884dee90
提交
884dee90
authored
2月 03, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
2月 03, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Group numba benchmark tests in same class
上级
2f2d0d34
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
64 行增加
和
66 行删除
+64
-66
test_elemwise.py
tests/link/numba/test_elemwise.py
+64
-66
没有找到文件。
tests/link/numba/test_elemwise.py
浏览文件 @
884dee90
...
@@ -130,25 +130,6 @@ def test_elemwise_runtime_broadcast():
...
@@ -130,25 +130,6 @@ def test_elemwise_runtime_broadcast():
check_elemwise_runtime_broadcast
(
get_mode
(
"NUMBA"
))
check_elemwise_runtime_broadcast
(
get_mode
(
"NUMBA"
))
def
test_elemwise_speed
(
benchmark
):
x
=
pt
.
dmatrix
(
"y"
)
y
=
pt
.
dvector
(
"z"
)
out
=
np
.
exp
(
2
*
x
*
y
+
y
)
rng
=
np
.
random
.
default_rng
(
42
)
x_val
=
rng
.
normal
(
size
=
(
200
,
500
))
y_val
=
rng
.
normal
(
size
=
500
)
func
=
function
([
x
,
y
],
out
,
mode
=
"NUMBA"
)
func
=
func
.
vm
.
jit_fn
(
out
,)
=
func
(
x_val
,
y_val
)
np
.
testing
.
assert_allclose
(
np
.
exp
(
2
*
x_val
*
y_val
+
y_val
),
out
)
benchmark
(
func
,
x_val
,
y_val
)
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"v, new_order"
,
"v, new_order"
,
[
[
...
@@ -631,27 +612,55 @@ def test_Argmax(x, axes, exc):
...
@@ -631,27 +612,55 @@ def test_Argmax(x, axes, exc):
)
)
@pytest.mark.parametrize
(
"size"
,
[(
10
,
10
),
(
1000
,
1000
),
(
10000
,
10000
)])
def
test_elemwise_out_type
():
@pytest.mark.parametrize
(
"axis"
,
[
0
,
1
])
# Create a graph with an elemwise
def
test_logsumexp_benchmark
(
size
,
axis
,
benchmark
):
# Ravel failes if the elemwise output type is reported incorrectly
X
=
pt
.
matrix
(
"X"
)
x
=
pt
.
matrix
()
X_max
=
pt
.
max
(
X
,
axis
=
axis
,
keepdims
=
True
)
y
=
(
2
*
x
)
.
ravel
()
X_max
=
pt
.
switch
(
pt
.
isinf
(
X_max
),
0
,
X_max
)
X_lse
=
pt
.
log
(
pt
.
sum
(
pt
.
exp
(
X
-
X_max
),
axis
=
axis
,
keepdims
=
True
))
+
X_max
rng
=
np
.
random
.
default_rng
(
23920
)
# Pass in the input as mutable, to trigger the inplace rewrites
X_val
=
rng
.
normal
(
size
=
size
)
func
=
pytensor
.
function
([
pytensor
.
In
(
x
,
mutable
=
True
)],
y
,
mode
=
"NUMBA"
)
X_lse_fn
=
pytensor
.
function
([
X
],
X_lse
,
mode
=
"NUMBA"
)
# Apply it to a numpy array that is neither C or F contigous
x_val
=
np
.
broadcast_to
(
np
.
zeros
((
3
,)),
(
6
,
3
))
assert
func
(
x_val
)
.
shape
==
(
18
,)
# JIT compile first
res
=
X_lse_fn
(
X_val
)
exp_res
=
scipy
.
special
.
logsumexp
(
X_val
,
axis
=
axis
,
keepdims
=
True
)
np
.
testing
.
assert_array_almost_equal
(
res
,
exp_res
)
benchmark
(
X_lse_fn
,
X_val
)
def
test_scalar_loop
():
a
=
float64
(
"a"
)
scalar_loop
=
pytensor
.
scalar
.
ScalarLoop
([
a
],
[
a
+
a
])
def
test_fused_elemwise_benchmark
(
benchmark
):
x
=
pt
.
tensor
(
"x"
,
shape
=
(
3
,))
elemwise_loop
=
Elemwise
(
scalar_loop
)(
3
,
x
)
with
pytest
.
warns
(
UserWarning
,
match
=
"object mode"
):
compare_numba_and_py
(
([
x
],
[
elemwise_loop
]),
(
np
.
array
([
1
,
2
,
3
],
dtype
=
"float64"
),),
)
class
TestsBenchmark
:
def
test_elemwise_speed
(
self
,
benchmark
):
x
=
pt
.
dmatrix
(
"y"
)
y
=
pt
.
dvector
(
"z"
)
out
=
np
.
exp
(
2
*
x
*
y
+
y
)
rng
=
np
.
random
.
default_rng
(
42
)
x_val
=
rng
.
normal
(
size
=
(
200
,
500
))
y_val
=
rng
.
normal
(
size
=
500
)
func
=
function
([
x
,
y
],
out
,
mode
=
"NUMBA"
)
func
=
func
.
vm
.
jit_fn
(
out
,)
=
func
(
x_val
,
y_val
)
np
.
testing
.
assert_allclose
(
np
.
exp
(
2
*
x_val
*
y_val
+
y_val
),
out
)
benchmark
(
func
,
x_val
,
y_val
)
def
test_fused_elemwise_benchmark
(
self
,
benchmark
):
rng
=
np
.
random
.
default_rng
(
123
)
rng
=
np
.
random
.
default_rng
(
123
)
size
=
100
_000
size
=
100
_000
x
=
pytensor
.
shared
(
rng
.
normal
(
size
=
size
),
name
=
"x"
)
x
=
pytensor
.
shared
(
rng
.
normal
(
size
=
size
),
name
=
"x"
)
...
@@ -665,47 +674,36 @@ def test_fused_elemwise_benchmark(benchmark):
...
@@ -665,47 +674,36 @@ def test_fused_elemwise_benchmark(benchmark):
func
()
func
()
benchmark
(
func
)
benchmark
(
func
)
@pytest.mark.parametrize
(
"size"
,
[(
10
,
10
),
(
1000
,
1000
),
(
10000
,
10000
)])
@pytest.mark.parametrize
(
"axis"
,
[
0
,
1
])
def
test_logsumexp_benchmark
(
self
,
size
,
axis
,
benchmark
):
X
=
pt
.
matrix
(
"X"
)
X_max
=
pt
.
max
(
X
,
axis
=
axis
,
keepdims
=
True
)
X_max
=
pt
.
switch
(
pt
.
isinf
(
X_max
),
0
,
X_max
)
X_lse
=
pt
.
log
(
pt
.
sum
(
pt
.
exp
(
X
-
X_max
),
axis
=
axis
,
keepdims
=
True
))
+
X_max
def
test_elemwise_out_type
():
rng
=
np
.
random
.
default_rng
(
23920
)
# Create a graph with an elemwise
X_val
=
rng
.
normal
(
size
=
size
)
# Ravel failes if the elemwise output type is reported incorrectly
x
=
pt
.
matrix
()
y
=
(
2
*
x
)
.
ravel
()
# Pass in the input as mutable, to trigger the inplace rewrites
func
=
pytensor
.
function
([
pytensor
.
In
(
x
,
mutable
=
True
)],
y
,
mode
=
"NUMBA"
)
# Apply it to a numpy array that is neither C or F contigous
x_val
=
np
.
broadcast_to
(
np
.
zeros
((
3
,)),
(
6
,
3
))
assert
func
(
x_val
)
.
shape
==
(
18
,
)
X_lse_fn
=
pytensor
.
function
([
X
],
X_lse
,
mode
=
"NUMBA"
)
# JIT compile first
res
=
X_lse_fn
(
X_val
)
exp_res
=
scipy
.
special
.
logsumexp
(
X_val
,
axis
=
axis
,
keepdims
=
True
)
np
.
testing
.
assert_array_almost_equal
(
res
,
exp_res
)
benchmark
(
X_lse_fn
,
X_val
)
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"axis"
,
"axis"
,
(
0
,
1
,
2
,
(
0
,
1
),
(
0
,
2
),
(
1
,
2
),
None
),
(
0
,
1
,
2
,
(
0
,
1
),
(
0
,
2
),
(
1
,
2
),
None
),
ids
=
lambda
x
:
f
"axis={x}"
,
ids
=
lambda
x
:
f
"axis={x}"
,
)
)
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"c_contiguous"
,
"c_contiguous"
,
(
True
,
False
),
(
True
,
False
),
ids
=
lambda
x
:
f
"c_contiguous={x}"
,
ids
=
lambda
x
:
f
"c_contiguous={x}"
,
)
)
def
test_numba_careduce_benchmark
(
axis
,
c_contiguous
,
benchmark
):
def
test_numba_careduce_benchmark
(
self
,
axis
,
c_contiguous
,
benchmark
):
return
careduce_benchmark_tester
(
return
careduce_benchmark_tester
(
axis
,
c_contiguous
,
mode
=
"NUMBA"
,
benchmark
=
benchmark
axis
,
c_contiguous
,
mode
=
"NUMBA"
,
benchmark
=
benchmark
)
)
def
test_scalar_loop
():
a
=
float64
(
"a"
)
scalar_loop
=
pytensor
.
scalar
.
ScalarLoop
([
a
],
[
a
+
a
])
x
=
pt
.
tensor
(
"x"
,
shape
=
(
3
,))
elemwise_loop
=
Elemwise
(
scalar_loop
)(
3
,
x
)
with
pytest
.
warns
(
UserWarning
,
match
=
"object mode"
):
compare_numba_and_py
(
([
x
],
[
elemwise_loop
]),
(
np
.
array
([
1
,
2
,
3
],
dtype
=
"float64"
),),
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论