Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
039ed1cc
提交
039ed1cc
authored
12月 06, 2022
作者:
Brandon T. Willard
提交者:
Thomas Wiecki
12月 10, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for SciPy CSC and CSR sparse types to Numba
上级
151da532
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
183 行增加
和
0 行删除
+183
-0
__init__.py
pytensor/link/numba/dispatch/__init__.py
+1
-0
sparse.py
pytensor/link/numba/dispatch/sparse.py
+142
-0
test_sparse.py
tests/link/numba/test_sparse.py
+40
-0
没有找到文件。
pytensor/link/numba/dispatch/__init__.py
浏览文件 @
039ed1cc
...
@@ -9,5 +9,6 @@ import pytensor.link.numba.dispatch.nlinalg
...
@@ -9,5 +9,6 @@ import pytensor.link.numba.dispatch.nlinalg
import
pytensor.link.numba.dispatch.random
import
pytensor.link.numba.dispatch.random
import
pytensor.link.numba.dispatch.elemwise
import
pytensor.link.numba.dispatch.elemwise
import
pytensor.link.numba.dispatch.scan
import
pytensor.link.numba.dispatch.scan
import
pytensor.link.numba.dispatch.sparse
# isort: on
# isort: on
pytensor/link/numba/dispatch/sparse.py
0 → 100644
浏览文件 @
039ed1cc
import
scipy
as
sp
import
scipy.sparse
from
numba.core
import
cgutils
,
types
from
numba.extending
import
(
NativeValue
,
box
,
make_attribute_wrapper
,
models
,
register_model
,
typeof_impl
,
unbox
,
)
class
CSMatrixType
(
types
.
Type
):
"""A Numba `Type` modeled after the base class `scipy.sparse.compressed._cs_matrix`."""
name
:
str
instance_class
:
type
def
__init__
(
self
,
dtype
):
self
.
dtype
=
dtype
self
.
data
=
types
.
Array
(
dtype
,
1
,
"A"
)
self
.
indices
=
types
.
Array
(
types
.
int32
,
1
,
"A"
)
self
.
indptr
=
types
.
Array
(
types
.
int32
,
1
,
"A"
)
self
.
shape
=
types
.
UniTuple
(
types
.
int64
,
2
)
super
()
.
__init__
(
self
.
name
)
make_attribute_wrapper
(
CSMatrixType
,
"data"
,
"data"
)
make_attribute_wrapper
(
CSMatrixType
,
"indices"
,
"indices"
)
make_attribute_wrapper
(
CSMatrixType
,
"indptr"
,
"indptr"
)
make_attribute_wrapper
(
CSMatrixType
,
"shape"
,
"shape"
)
class
CSRMatrixType
(
CSMatrixType
):
name
=
"csr_matrix"
@staticmethod
def
instance_class
(
data
,
indices
,
indptr
,
shape
):
return
sp
.
sparse
.
csr_matrix
((
data
,
indices
,
indptr
),
shape
,
copy
=
False
)
class
CSCMatrixType
(
CSMatrixType
):
name
=
"csc_matrix"
@staticmethod
def
instance_class
(
data
,
indices
,
indptr
,
shape
):
return
sp
.
sparse
.
csc_matrix
((
data
,
indices
,
indptr
),
shape
,
copy
=
False
)
@typeof_impl.register
(
sp
.
sparse
.
csc_matrix
)
def
typeof_csc_matrix
(
val
,
c
):
data
=
typeof_impl
(
val
.
data
,
c
)
return
CSCMatrixType
(
data
.
dtype
)
@typeof_impl.register
(
sp
.
sparse
.
csr_matrix
)
def
typeof_csr_matrix
(
val
,
c
):
data
=
typeof_impl
(
val
.
data
,
c
)
return
CSRMatrixType
(
data
.
dtype
)
@register_model
(
CSRMatrixType
)
class
CSRMatrixModel
(
models
.
StructModel
):
def
__init__
(
self
,
dmm
,
fe_type
):
members
=
[
(
"data"
,
fe_type
.
data
),
(
"indices"
,
fe_type
.
indices
),
(
"indptr"
,
fe_type
.
indptr
),
(
"shape"
,
fe_type
.
shape
),
]
super
()
.
__init__
(
dmm
,
fe_type
,
members
)
@register_model
(
CSCMatrixType
)
class
CSCMatrixModel
(
models
.
StructModel
):
def
__init__
(
self
,
dmm
,
fe_type
):
members
=
[
(
"data"
,
fe_type
.
data
),
(
"indices"
,
fe_type
.
indices
),
(
"indptr"
,
fe_type
.
indptr
),
(
"shape"
,
fe_type
.
shape
),
]
super
()
.
__init__
(
dmm
,
fe_type
,
members
)
@unbox
(
CSCMatrixType
)
@unbox
(
CSRMatrixType
)
def
unbox_matrix
(
typ
,
obj
,
c
):
struct_ptr
=
cgutils
.
create_struct_proxy
(
typ
)(
c
.
context
,
c
.
builder
)
data
=
c
.
pyapi
.
object_getattr_string
(
obj
,
"data"
)
indices
=
c
.
pyapi
.
object_getattr_string
(
obj
,
"indices"
)
indptr
=
c
.
pyapi
.
object_getattr_string
(
obj
,
"indptr"
)
shape
=
c
.
pyapi
.
object_getattr_string
(
obj
,
"shape"
)
struct_ptr
.
data
=
c
.
unbox
(
typ
.
data
,
data
)
.
value
struct_ptr
.
indices
=
c
.
unbox
(
typ
.
indices
,
indices
)
.
value
struct_ptr
.
indptr
=
c
.
unbox
(
typ
.
indptr
,
indptr
)
.
value
struct_ptr
.
shape
=
c
.
unbox
(
typ
.
shape
,
shape
)
.
value
c
.
pyapi
.
decref
(
data
)
c
.
pyapi
.
decref
(
indices
)
c
.
pyapi
.
decref
(
indptr
)
c
.
pyapi
.
decref
(
shape
)
is_error_ptr
=
cgutils
.
alloca_once_value
(
c
.
builder
,
cgutils
.
false_bit
)
is_error
=
c
.
builder
.
load
(
is_error_ptr
)
res
=
NativeValue
(
struct_ptr
.
_getvalue
(),
is_error
=
is_error
)
return
res
@box
(
CSCMatrixType
)
@box
(
CSRMatrixType
)
def
box_matrix
(
typ
,
val
,
c
):
struct_ptr
=
cgutils
.
create_struct_proxy
(
typ
)(
c
.
context
,
c
.
builder
,
value
=
val
)
data_obj
=
c
.
box
(
typ
.
data
,
struct_ptr
.
data
)
indices_obj
=
c
.
box
(
typ
.
indices
,
struct_ptr
.
indices
)
indptr_obj
=
c
.
box
(
typ
.
indptr
,
struct_ptr
.
indptr
)
shape_obj
=
c
.
box
(
typ
.
shape
,
struct_ptr
.
shape
)
c
.
pyapi
.
incref
(
data_obj
)
c
.
pyapi
.
incref
(
indices_obj
)
c
.
pyapi
.
incref
(
indptr_obj
)
c
.
pyapi
.
incref
(
shape_obj
)
cls_obj
=
c
.
pyapi
.
unserialize
(
c
.
pyapi
.
serialize_object
(
typ
.
instance_class
))
obj
=
c
.
pyapi
.
call_function_objargs
(
cls_obj
,
(
data_obj
,
indices_obj
,
indptr_obj
,
shape_obj
)
)
c
.
pyapi
.
decref
(
data_obj
)
c
.
pyapi
.
decref
(
indices_obj
)
c
.
pyapi
.
decref
(
indptr_obj
)
c
.
pyapi
.
decref
(
shape_obj
)
return
obj
tests/link/numba/test_sparse.py
0 → 100644
浏览文件 @
039ed1cc
import
numba
import
numpy
as
np
import
scipy
as
sp
# Load Numba customizations
import
pytensor.link.numba.dispatch.sparse
# noqa: F401
def
test_sparse_unboxing
():
@numba.njit
def
test_unboxing
(
x
,
y
):
return
x
.
shape
,
y
.
shape
x_val
=
sp
.
sparse
.
csr_matrix
(
np
.
eye
(
100
))
y_val
=
sp
.
sparse
.
csc_matrix
(
np
.
eye
(
101
))
res
=
test_unboxing
(
x_val
,
y_val
)
assert
res
==
(
x_val
.
shape
,
y_val
.
shape
)
def
test_sparse_boxing
():
@numba.njit
def
test_boxing
(
x
,
y
):
return
x
,
y
x_val
=
sp
.
sparse
.
csr_matrix
(
np
.
eye
(
100
))
y_val
=
sp
.
sparse
.
csc_matrix
(
np
.
eye
(
101
))
res_x_val
,
res_y_val
=
test_boxing
(
x_val
,
y_val
)
assert
np
.
array_equal
(
res_x_val
.
data
,
x_val
.
data
)
assert
np
.
array_equal
(
res_x_val
.
indices
,
x_val
.
indices
)
assert
np
.
array_equal
(
res_x_val
.
indptr
,
x_val
.
indptr
)
assert
res_x_val
.
shape
==
x_val
.
shape
assert
np
.
array_equal
(
res_y_val
.
data
,
y_val
.
data
)
assert
np
.
array_equal
(
res_y_val
.
indices
,
y_val
.
indices
)
assert
np
.
array_equal
(
res_y_val
.
indptr
,
y_val
.
indptr
)
assert
res_y_val
.
shape
==
y_val
.
shape
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论