Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1977c2c0
提交
1977c2c0
authored
11月 17, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
1月 19, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Numba sparse: Implement SparseDenseMultiply
Co-authored-by:
Jesse Grabowski
<
48652735+jessegrabowski@users.noreply.github.com
>
上级
fbd7a597
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
130 行增加
和
20 行删除
+130
-20
__init__.py
pytensor/link/numba/dispatch/sparse/__init__.py
+1
-1
math.py
pytensor/link/numba/dispatch/sparse/math.py
+90
-0
test_basic.py
tests/link/numba/sparse/test_basic.py
+11
-19
test_math.py
tests/link/numba/sparse/test_math.py
+28
-0
没有找到文件。
pytensor/link/numba/dispatch/sparse/__init__.py
浏览文件 @
1977c2c0
from
pytensor.link.numba.dispatch.sparse
import
basic
,
variable
from
pytensor.link.numba.dispatch.sparse
import
basic
,
math
,
variable
pytensor/link/numba/dispatch/sparse/math.py
0 → 100644
浏览文件 @
1977c2c0
from
pytensor.link.numba.dispatch
import
basic
as
numba_basic
from
pytensor.link.numba.dispatch.basic
import
register_funcify_default_op_cache_key
from
pytensor.sparse
import
SparseDenseMultiply
,
SparseDenseVectorMultiply
@register_funcify_default_op_cache_key
(
SparseDenseMultiply
)
@register_funcify_default_op_cache_key
(
SparseDenseVectorMultiply
)
def
numba_funcify_SparseDenseMultiply
(
op
,
node
,
**
kwargs
):
x
,
y
=
node
.
inputs
[
z
]
=
node
.
outputs
out_dtype
=
z
.
type
.
dtype
format
=
z
.
type
.
format
same_dtype
=
x
.
type
.
dtype
==
out_dtype
if
y
.
ndim
==
0
:
@numba_basic.numba_njit
def
sparse_multiply_scalar
(
x
,
y
):
if
same_dtype
:
z
=
x
.
copy
()
else
:
z
=
x
.
astype
(
out_dtype
)
# Numba doesn't know how to handle in-place mutation / assignment of fields
# z.data *= y
z_data
=
z
.
data
z_data
*=
y
return
z
return
sparse_multiply_scalar
elif
y
.
ndim
==
1
:
@numba_basic.numba_njit
def
sparse_dense_multiply
(
x
,
y
):
assert
x
.
shape
[
1
]
==
y
.
shape
[
0
]
if
same_dtype
:
z
=
x
.
copy
()
else
:
z
=
x
.
astype
(
out_dtype
)
M
,
N
=
x
.
shape
indices
=
x
.
indices
indptr
=
x
.
indptr
z_data
=
z
.
data
if
format
==
"csc"
:
for
j
in
range
(
0
,
N
):
for
i_idx
in
range
(
indptr
[
j
],
indptr
[
j
+
1
]):
z_data
[
i_idx
]
*=
y
[
j
]
return
z
else
:
for
i
in
range
(
0
,
M
):
for
j_idx
in
range
(
indptr
[
i
],
indptr
[
i
+
1
]):
j
=
indices
[
j_idx
]
z_data
[
j_idx
]
*=
y
[
j
]
return
z
return
sparse_dense_multiply
else
:
# y.ndim == 2
@numba_basic.numba_njit
def
sparse_dense_multiply
(
x
,
y
):
assert
x
.
shape
==
y
.
shape
if
same_dtype
:
z
=
x
.
copy
()
else
:
z
=
x
.
astype
(
out_dtype
)
M
,
N
=
x
.
shape
indices
=
x
.
indices
indptr
=
x
.
indptr
z_data
=
z
.
data
if
format
==
"csc"
:
for
j
in
range
(
0
,
N
):
for
i_idx
in
range
(
indptr
[
j
],
indptr
[
j
+
1
]):
i
=
indices
[
i_idx
]
z_data
[
i_idx
]
*=
y
[
i
,
j
]
return
z
else
:
for
i
in
range
(
0
,
M
):
for
j_idx
in
range
(
indptr
[
i
],
indptr
[
i
+
1
]):
j
=
indices
[
j_idx
]
z_data
[
j_idx
]
*=
y
[
i
,
j
]
return
z
return
sparse_dense_multiply
tests/link/numba/sparse/test_basic.py
浏览文件 @
1977c2c0
...
@@ -200,16 +200,12 @@ def test_sparse_constant(format, cache):
...
@@ -200,16 +200,12 @@ def test_sparse_constant(format, cache):
y_test
=
np
.
array
([
np
.
pi
,
np
.
e
,
np
.
euler_gamma
])
y_test
=
np
.
array
([
np
.
pi
,
np
.
e
,
np
.
euler_gamma
])
with
config
.
change_flags
(
numba__cache
=
cache
):
with
config
.
change_flags
(
numba__cache
=
cache
):
with
pytest
.
warns
(
compare_numba_and_py_sparse
(
UserWarning
,
[
y
],
match
=
r"Numba will use object mode to run SparseDenseVectorMultiply's perform method"
,
[
out
],
):
[
y_test
],
compare_numba_and_py_sparse
(
eval_obj_mode
=
False
,
[
y
],
)
[
out
],
[
y_test
],
eval_obj_mode
=
False
,
)
@pytest.mark.parametrize
(
"format"
,
[
"csc"
,
"csr"
])
@pytest.mark.parametrize
(
"format"
,
[
"csc"
,
"csr"
])
...
@@ -254,15 +250,11 @@ def test_simple_graph(format):
...
@@ -254,15 +250,11 @@ def test_simple_graph(format):
x_test
=
sp
.
sparse
.
random
(
3
,
3
,
density
=
0.5
,
format
=
format
,
random_state
=
rng
)
x_test
=
sp
.
sparse
.
random
(
3
,
3
,
density
=
0.5
,
format
=
format
,
random_state
=
rng
)
y_test
=
rng
.
normal
(
size
=
(
3
,))
y_test
=
rng
.
normal
(
size
=
(
3
,))
with
pytest
.
warns
(
compare_numba_and_py_sparse
(
UserWarning
,
[
x
,
y
],
match
=
r"Numba will use object mode to run SparseDenseVectorMultiply's perform method"
,
z
,
):
[
x_test
,
y_test
],
compare_numba_and_py_sparse
(
)
[
x
,
y
],
z
,
[
x_test
,
y_test
],
)
@pytest.mark.parametrize
(
"format"
,
(
"csr"
,
"csc"
))
@pytest.mark.parametrize
(
"format"
,
(
"csr"
,
"csc"
))
...
...
tests/link/numba/sparse/test_math.py
0 → 100644
浏览文件 @
1977c2c0
import
numpy
as
np
import
pytest
import
scipy
import
pytensor.sparse
as
ps
import
pytensor.tensor
as
pt
from
tests.link.numba.sparse.test_basic
import
compare_numba_and_py_sparse
pytestmark
=
pytest
.
mark
.
filterwarnings
(
"error"
)
@pytest.mark.parametrize
(
"format"
,
[
"csr"
,
"csc"
])
@pytest.mark.parametrize
(
"y_ndim"
,
[
0
,
1
,
2
])
def
test_sparse_dense_multiply
(
y_ndim
,
format
):
x
=
ps
.
matrix
(
format
,
name
=
"x"
,
shape
=
(
3
,
3
))
y
=
pt
.
tensor
(
"y"
,
shape
=
(
3
,)
*
y_ndim
)
z
=
x
*
y
rng
=
np
.
random
.
default_rng
((
155
,
y_ndim
,
format
==
"csr"
))
x_test
=
scipy
.
sparse
.
random
(
3
,
3
,
density
=
0.5
,
format
=
format
,
random_state
=
rng
)
y_test
=
rng
.
normal
(
size
=
(
3
,)
*
y_ndim
)
compare_numba_and_py_sparse
(
[
x
,
y
],
z
,
[
x_test
,
y_test
],
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论