Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ba51e7d2
提交
ba51e7d2
authored
12月 07, 2022
作者:
Brandon T. Willard
提交者:
Ricardo Vieira
3月 22, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix PyTensor-to-Numba type resolution for sparse variables
上级
e31655fb
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
43 行增加
和
3 行删除
+43
-3
basic.py
pytensor/link/numba/dispatch/basic.py
+11
-0
sparse.py
pytensor/link/numba/dispatch/sparse.py
+8
-2
test_sparse.py
tests/link/numba/test_sparse.py
+24
-1
没有找到文件。
pytensor/link/numba/dispatch/basic.py
浏览文件 @
ba51e7d2
...
...
@@ -25,6 +25,7 @@ from pytensor.graph.basic import Apply, NoParams
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.type
import
Type
from
pytensor.ifelse
import
IfElse
from
pytensor.link.numba.dispatch.sparse
import
CSCMatrixType
,
CSRMatrixType
from
pytensor.link.utils
import
(
compile_function_src
,
fgraph_to_python
,
...
...
@@ -32,6 +33,7 @@ from pytensor.link.utils import (
)
from
pytensor.scalar.basic
import
ScalarType
from
pytensor.scalar.math
import
Softplus
from
pytensor.sparse
import
SparseTensorType
from
pytensor.tensor.blas
import
BatchedDot
from
pytensor.tensor.math
import
Dot
from
pytensor.tensor.shape
import
Reshape
,
Shape
,
Shape_i
,
SpecifyShape
...
...
@@ -105,6 +107,15 @@ def get_numba_type(
dtype
=
np
.
dtype
(
pytensor_type
.
dtype
)
numba_dtype
=
numba
.
from_dtype
(
dtype
)
return
numba_dtype
elif
isinstance
(
pytensor_type
,
SparseTensorType
):
dtype
=
pytensor_type
.
numpy_dtype
numba_dtype
=
numba
.
from_dtype
(
dtype
)
if
pytensor_type
.
format
==
"csr"
:
return
CSRMatrixType
(
numba_dtype
)
if
pytensor_type
.
format
==
"csc"
:
return
CSCMatrixType
(
numba_dtype
)
raise
NotImplementedError
()
else
:
raise
NotImplementedError
(
f
"Numba type not implemented for {pytensor_type}"
)
...
...
pytensor/link/numba/dispatch/sparse.py
浏览文件 @
ba51e7d2
...
...
@@ -19,7 +19,10 @@ class CSMatrixType(types.Type):
"""A Numba `Type` modeled after the base class `scipy.sparse.compressed._cs_matrix`."""
name
:
str
instance_class
:
type
@staticmethod
def
instance_class
(
data
,
indices
,
indptr
,
shape
):
raise
NotImplementedError
()
def
__init__
(
self
,
dtype
):
self
.
dtype
=
dtype
...
...
@@ -29,6 +32,10 @@ class CSMatrixType(types.Type):
self
.
shape
=
types
.
UniTuple
(
types
.
int64
,
2
)
super
()
.
__init__
(
self
.
name
)
@property
def
key
(
self
):
return
(
self
.
name
,
self
.
dtype
)
make_attribute_wrapper
(
CSMatrixType
,
"data"
,
"data"
)
make_attribute_wrapper
(
CSMatrixType
,
"indices"
,
"indices"
)
...
...
@@ -152,7 +159,6 @@ def overload_sparse_shape(x):
@overload_attribute
(
CSMatrixType
,
"ndim"
)
def
overload_sparse_ndim
(
inst
):
if
not
isinstance
(
inst
,
CSMatrixType
):
return
...
...
tests/link/numba/test_sparse.py
浏览文件 @
ba51e7d2
import
numba
import
numpy
as
np
import
pytest
import
scipy
as
sp
#
Load Numba customizations
#
Make sure the Numba customizations are loaded
import
pytensor.link.numba.dispatch.sparse
# noqa: F401
from
pytensor
import
config
from
pytensor.sparse
import
Dot
,
SparseTensorType
from
tests.link.numba.test_basic
import
compare_numba_and_py
pytestmark
=
pytest
.
mark
.
filterwarnings
(
"error"
)
def
test_sparse_unboxing
():
...
...
@@ -62,3 +69,19 @@ def test_sparse_ndim():
res
=
test_fn
(
x_val
)
assert
res
==
2
def
test_sparse_objmode
():
x
=
SparseTensorType
(
"csc"
,
dtype
=
config
.
floatX
)()
y
=
SparseTensorType
(
"csc"
,
dtype
=
config
.
floatX
)()
out
=
Dot
()(
x
,
y
)
x_val
=
sp
.
sparse
.
random
(
2
,
2
,
density
=
0.25
,
dtype
=
config
.
floatX
)
y_val
=
sp
.
sparse
.
random
(
2
,
2
,
density
=
0.25
,
dtype
=
config
.
floatX
)
with
pytest
.
warns
(
UserWarning
,
match
=
"Numba will use object mode to run SparseDot's perform method"
,
):
compare_numba_and_py
(((
x
,
y
),
(
out
,)),
[
x_val
,
y_val
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论