Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
90021636
提交
90021636
authored
8月 29, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
8月 30, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a Numba implementation for BernoulliRV
上级
febb92ea
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
60 行增加
和
10 行删除
+60
-10
dispatch.py
aesara/link/numba/dispatch.py
+50
-10
test_numba.py
tests/link/test_numba.py
+10
-0
没有找到文件。
aesara/link/numba/dispatch.py
浏览文件 @
90021636
...
@@ -4,7 +4,7 @@ import warnings
...
@@ -4,7 +4,7 @@ import warnings
from
functools
import
reduce
,
singledispatch
from
functools
import
reduce
,
singledispatch
from
numbers
import
Number
from
numbers
import
Number
from
textwrap
import
dedent
,
indent
from
textwrap
import
dedent
,
indent
from
typing
import
List
,
Union
from
typing
import
Any
,
Callable
,
Dict
,
List
,
Optional
,
Union
import
numba
import
numba
import
numpy
as
np
import
numpy
as
np
...
@@ -23,6 +23,7 @@ import aesara.tensor.random.basic as aer
...
@@ -23,6 +23,7 @@ import aesara.tensor.random.basic as aer
from
aesara.compile.ops
import
DeepCopyOp
,
ViewOp
from
aesara.compile.ops
import
DeepCopyOp
,
ViewOp
from
aesara.graph.basic
import
Apply
,
Variable
from
aesara.graph.basic
import
Apply
,
Variable
from
aesara.graph.fg
import
FunctionGraph
from
aesara.graph.fg
import
FunctionGraph
from
aesara.graph.op
import
Op
from
aesara.graph.type
import
Type
from
aesara.graph.type
import
Type
from
aesara.link.utils
import
(
from
aesara.link.utils
import
(
compile_function_src
,
compile_function_src
,
...
@@ -2163,15 +2164,33 @@ def numba_funcify_RandomVariable(op, node, **kwargs):
...
@@ -2163,15 +2164,33 @@ def numba_funcify_RandomVariable(op, node, **kwargs):
return
make_numba_random_fn
(
node
,
np_random_func
)
return
make_numba_random_fn
(
node
,
np_random_func
)
@numba_funcify.register
(
aer
.
HalfNormalRV
)
def
create_numba_random_fn
(
def
numba_funcify_HalfNormalRV
(
op
,
node
,
**
kwargs
):
op
:
Op
,
node
:
Apply
,
scalar_fn
:
Callable
[[
str
],
str
],
global_env
:
Optional
[
Dict
[
str
,
Any
]]
=
None
,
)
->
Callable
:
"""Create a vectorized function from a callable that generates the ``str`` function body.
TODO: This could/should be generalized for other simple function
construction cases that need unique-ified symbol names.
"""
np_random_fn_name
=
f
"aesara_random_{get_name_for_object(op.name)}"
np_random_fn_name
=
f
"aesara_random_{get_name_for_object(op.name)}"
if
global_env
:
np_global_env
=
global_env
.
copy
()
else
:
np_global_env
=
{}
np_global_env
[
"np"
]
=
np
np_global_env
[
"numba_vectorize"
]
=
numba
.
vectorize
unique_names
=
unique_name_generator
(
unique_names
=
unique_name_generator
(
[
[
np_random_fn_name
,
np_random_fn_name
,
"numba_vectorize"
,
]
"np_standard_norm"
,
+
list
(
np_global_env
.
keys
())
+
[
"rng"
,
"rng"
,
"size"
,
"size"
,
"dtype"
,
"dtype"
,
...
@@ -2181,17 +2200,38 @@ def numba_funcify_HalfNormalRV(op, node, **kwargs):
...
@@ -2181,17 +2200,38 @@ def numba_funcify_HalfNormalRV(op, node, **kwargs):
np_names
=
[
unique_names
(
i
,
force_unique
=
True
)
for
i
in
node
.
inputs
[
3
:]]
np_names
=
[
unique_names
(
i
,
force_unique
=
True
)
for
i
in
node
.
inputs
[
3
:]]
np_input_names
=
", "
.
join
(
np_names
)
np_input_names
=
", "
.
join
(
np_names
)
np_global_env
=
{
"np_standard_norm"
:
np
.
random
.
standard_normal
,
"numba_vectorize"
:
numba
.
vectorize
,
}
np_random_fn_src
=
f
"""
np_random_fn_src
=
f
"""
@numba_vectorize
@numba_vectorize
def {np_random_fn_name}({np_input_names}):
def {np_random_fn_name}({np_input_names}):
return {np_names[0]} + {np_names[1]} * abs(np_standard_norm())
{scalar_fn(*np_names)}
"""
"""
np_random_fn
=
compile_function_src
(
np_random_fn
=
compile_function_src
(
np_random_fn_src
,
np_random_fn_name
,
np_global_env
np_random_fn_src
,
np_random_fn_name
,
np_global_env
)
)
return
make_numba_random_fn
(
node
,
np_random_fn
)
return
make_numba_random_fn
(
node
,
np_random_fn
)
@numba_funcify.register
(
aer
.
HalfNormalRV
)
def
numba_funcify_HalfNormalRV
(
op
,
node
,
**
kwargs
):
def
body_fn
(
a
,
b
):
return
f
" return {a} + {b} * abs(np.random.normal(0, 1))"
return
create_numba_random_fn
(
op
,
node
,
body_fn
)
@numba_funcify.register
(
aer
.
BernoulliRV
)
def
numba_funcify_BernoulliRV
(
op
,
node
,
**
kwargs
):
out_dtype
=
node
.
outputs
[
1
]
.
type
.
numpy_dtype
def
body_fn
(
a
):
return
f
"""
if {a} < np.random.uniform(0, 1):
return direct_cast(0, out_dtype)
else:
return direct_cast(1, out_dtype)
"""
return
create_numba_random_fn
(
op
,
node
,
body_fn
,
{
"out_dtype"
:
out_dtype
,
"direct_cast"
:
direct_cast
}
)
tests/link/test_numba.py
浏览文件 @
90021636
...
@@ -2806,6 +2806,16 @@ def test_shared():
...
@@ -2806,6 +2806,16 @@ def test_shared():
],
],
None
,
None
,
),
),
(
aer
.
bernoulli
,
[
set_test_value
(
aet
.
dvector
(),
np
.
array
([
0.1
,
0.9
],
dtype
=
np
.
float64
),
),
],
None
,
),
(
(
aer
.
randint
,
aer
.
randint
,
[
[
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论