Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4ccf184f
提交
4ccf184f
authored
3月 17, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
3月 18, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Convert SciPy scalar function inputs to acceptable dtypes in Numba implementations
上级
033bf332
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
63 行增加
和
4 行删除
+63
-4
scalar.py
aesara/link/numba/dispatch/scalar.py
+57
-4
test_numba.py
tests/link/test_numba.py
+6
-0
没有找到文件。
aesara/link/numba/dispatch/scalar.py
浏览文件 @
4ccf184f
...
@@ -52,14 +52,67 @@ def numba_funcify_ScalarOp(op, node, **kwargs):
...
@@ -52,14 +52,67 @@ def numba_funcify_ScalarOp(op, node, **kwargs):
[
scalar_op_fn_name
,
"scalar_func"
],
suffix_sep
=
"_"
[
scalar_op_fn_name
,
"scalar_func"
],
suffix_sep
=
"_"
)
)
input_names
=
", "
.
join
([
unique_names
(
v
,
force_unique
=
True
)
for
v
in
node
.
inputs
])
global_env
=
{
"scalar_func"
:
scalar_func
}
global_env
=
{
"scalar_func"
:
scalar_func
}
scalar_op_src
=
f
"""
input_tmp_dtypes
=
None
if
func_package
==
scipy
and
hasattr
(
scalar_func
,
"types"
):
# The `numba-scipy` bindings don't provide implementations for all
# inputs types, so we need to convert the inputs to floats and back.
inp_dtype_kinds
=
tuple
(
np
.
dtype
(
inp
.
type
.
dtype
)
.
kind
for
inp
in
node
.
inputs
)
accepted_inp_kinds
=
tuple
(
sig_type
.
split
(
"->"
)[
0
]
for
sig_type
in
scalar_func
.
types
)
if
not
any
(
all
(
dk
==
ik
for
dk
,
ik
in
zip
(
inp_dtype_kinds
,
ok_kinds
))
for
ok_kinds
in
accepted_inp_kinds
):
# They're usually ordered from lower-to-higher precision, so
# we pick the last acceptable input types
#
# XXX: We should pick the first acceptable float/int types in
# reverse, excluding all the incompatible ones (e.g. `"0"`).
# The assumption is that this is only used by `numba-scipy`-exposed
# functions, although it's possible for this to be triggered by
# something else from the `scipy` package
input_tmp_dtypes
=
tuple
(
np
.
dtype
(
k
)
for
k
in
accepted_inp_kinds
[
-
1
])
if
input_tmp_dtypes
is
None
:
unique_names
=
unique_name_generator
(
[
scalar_op_fn_name
,
"scalar_func"
],
suffix_sep
=
"_"
)
input_names
=
", "
.
join
(
[
unique_names
(
v
,
force_unique
=
True
)
for
v
in
node
.
inputs
]
)
scalar_op_src
=
f
"""
def {scalar_op_fn_name}({input_names}):
def {scalar_op_fn_name}({input_names}):
return scalar_func({input_names})
return scalar_func({input_names})
"""
"""
else
:
global_env
[
"direct_cast"
]
=
numba_basic
.
direct_cast
global_env
[
"output_dtype"
]
=
np
.
dtype
(
node
.
outputs
[
0
]
.
type
.
dtype
)
input_tmp_dtype_names
=
{
f
"inp_tmp_dtype_{i}"
:
i_dtype
for
i
,
i_dtype
in
enumerate
(
input_tmp_dtypes
)
}
global_env
.
update
(
input_tmp_dtype_names
)
unique_names
=
unique_name_generator
(
[
scalar_op_fn_name
,
"scalar_func"
]
+
list
(
global_env
.
keys
()),
suffix_sep
=
"_"
)
input_names
=
[
unique_names
(
v
,
force_unique
=
True
)
for
v
in
node
.
inputs
]
converted_call_args
=
", "
.
join
(
[
f
"direct_cast({i_name}, {i_tmp_dtype_name})"
for
i_name
,
i_tmp_dtype_name
in
zip
(
input_names
,
input_tmp_dtype_names
.
keys
()
)
]
)
scalar_op_src
=
f
"""
def {scalar_op_fn_name}({', '.join(input_names)}):
return direct_cast(scalar_func({converted_call_args}), output_dtype)
"""
scalar_op_fn
=
compile_function_src
(
scalar_op_fn
=
compile_function_src
(
scalar_op_src
,
scalar_op_fn_name
,
{
**
globals
(),
**
global_env
}
scalar_op_src
,
scalar_op_fn_name
,
{
**
globals
(),
**
global_env
}
)
)
...
...
tests/link/test_numba.py
浏览文件 @
4ccf184f
...
@@ -321,6 +321,12 @@ def test_numba_box_unbox(input, wrapper_fn, check_fn):
...
@@ -321,6 +321,12 @@ def test_numba_box_unbox(input, wrapper_fn, check_fn):
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"inputs, input_vals, output_fn, exc"
,
"inputs, input_vals, output_fn, exc"
,
[
[
(
[
at
.
lvector
()],
[
rng
.
poisson
(
10
,
size
=
100
)
.
astype
(
np
.
int64
)],
lambda
x
:
at
.
gammaln
(
x
),
None
,
),
(
(
[
at
.
vector
()],
[
at
.
vector
()],
[
rng
.
standard_normal
(
100
)
.
astype
(
config
.
floatX
)],
[
rng
.
standard_normal
(
100
)
.
astype
(
config
.
floatX
)],
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论