Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
db215037
提交
db215037
authored
2月 19, 2026
作者:
Tomas Capretto
提交者:
Ricardo Vieira
2月 26, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement Diag sparse Op in Numba backend
上级
a332c8b2
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
71 行增加
和
0 行删除
+71
-0
basic.py
pytensor/link/numba/dispatch/sparse/basic.py
+49
-0
test_basic.py
tests/link/numba/sparse/test_basic.py
+22
-0
没有找到文件。
pytensor/link/numba/dispatch/sparse/basic.py
浏览文件 @
db215037
...
@@ -17,6 +17,7 @@ from pytensor.sparse import (
...
@@ -17,6 +17,7 @@ from pytensor.sparse import (
ColScaleCSC
,
ColScaleCSC
,
CSMProperties
,
CSMProperties
,
DenseFromSparse
,
DenseFromSparse
,
Diag
,
GetItem2d
,
GetItem2d
,
GetItem2Lists
,
GetItem2Lists
,
GetItem2ListsGrad
,
GetItem2ListsGrad
,
...
@@ -825,3 +826,51 @@ def numba_funcify_Neg(op, node, **kwargs):
...
@@ -825,3 +826,51 @@ def numba_funcify_Neg(op, node, **kwargs):
return
z
return
z
return
neg
return
neg
@register_funcify_default_op_cache_key
(
Diag
)
def
numba_funcify_Diag
(
op
,
node
,
**
kwargs
):
input_format
=
node
.
inputs
[
0
]
.
type
.
format
out_dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
if
input_format
==
"csr"
:
@numba_basic.numba_njit
def
diag_csr
(
x
):
n_rows
,
n_cols
=
x
.
shape
if
n_rows
!=
n_cols
:
raise
ValueError
(
"Diag only apply on square matrix"
)
indptr
=
x
.
indptr
.
view
(
np
.
uint32
)
indices
=
x
.
indices
.
view
(
np
.
uint32
)
out
=
np
.
zeros
(
n_rows
,
dtype
=
out_dtype
)
for
row_idx
in
range
(
n_rows
):
for
data_idx
in
range
(
indptr
[
row_idx
],
indptr
[
row_idx
+
1
]):
# Duplicate entries on the diagonal must accumulate.
if
indices
[
data_idx
]
==
row_idx
:
out
[
row_idx
]
+=
x
.
data
[
data_idx
]
return
out
return
diag_csr
@numba_basic.numba_njit
def
diag_csc
(
x
):
n_rows
,
n_cols
=
x
.
shape
if
n_rows
!=
n_cols
:
raise
ValueError
(
"Diag only apply on square matrix"
)
indptr
=
x
.
indptr
.
view
(
np
.
uint32
)
indices
=
x
.
indices
.
view
(
np
.
uint32
)
out
=
np
.
zeros
(
n_cols
,
dtype
=
out_dtype
)
for
col_idx
in
range
(
n_cols
):
for
data_idx
in
range
(
indptr
[
col_idx
],
indptr
[
col_idx
+
1
]):
# Duplicate entries on the diagonal must accumulate.
if
indices
[
data_idx
]
==
col_idx
:
out
[
col_idx
]
+=
x
.
data
[
data_idx
]
return
out
return
diag_csc
tests/link/numba/sparse/test_basic.py
浏览文件 @
db215037
...
@@ -664,3 +664,25 @@ def test_sparse_neg(format):
...
@@ -664,3 +664,25 @@ def test_sparse_neg(format):
x_test
=
sp
.
sparse
.
random
(
7
,
6
,
density
=
0.4
,
format
=
format
,
dtype
=
config
.
floatX
)
x_test
=
sp
.
sparse
.
random
(
7
,
6
,
density
=
0.4
,
format
=
format
,
dtype
=
config
.
floatX
)
compare_numba_and_py_sparse
([
x
],
z
,
[
x_test
])
compare_numba_and_py_sparse
([
x
],
z
,
[
x_test
])
@pytest.mark.parametrize
(
"format"
,
(
"csr"
,
"csc"
))
def
test_sparse_diag
(
format
):
x
=
ps
.
matrix
(
format
,
name
=
"x"
,
shape
=
(
8
,
8
),
dtype
=
config
.
floatX
)
z
=
ps
.
diag
(
x
)
x_test
=
sp
.
sparse
.
random
(
8
,
8
,
density
=
0.4
,
format
=
format
,
dtype
=
config
.
floatX
)
compare_numba_and_py_sparse
([
x
],
z
,
[
x_test
])
@pytest.mark.parametrize
(
"format"
,
(
"csr"
,
"csc"
))
def
test_sparse_diag_not_square_raises
(
format
):
x
=
ps
.
matrix
(
format
,
name
=
"x"
,
shape
=
(
8
,
6
),
dtype
=
config
.
floatX
)
z
=
ps
.
diag
(
x
)
fn
=
function
([
x
],
z
,
mode
=
"NUMBA"
)
x_test
=
sp
.
sparse
.
random
(
8
,
6
,
density
=
0.4
,
format
=
format
,
dtype
=
config
.
floatX
)
with
pytest
.
raises
(
ValueError
,
match
=
"Diag only apply on square matrix"
):
fn
(
x_test
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论