Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3d96ee80
提交
3d96ee80
authored
9月 21, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
10月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix storage handling in numba_funcify_Scan
上级
a2d05adc
全部展开
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
103 行增加
和
5 行删除
+103
-5
scan.py
aesara/link/numba/dispatch/scan.py
+0
-0
test_scan.py
tests/link/numba/test_scan.py
+103
-5
没有找到文件。
aesara/link/numba/dispatch/scan.py
浏览文件 @
3d96ee80
差异被折叠。
点击展开。
tests/link/numba/test_scan.py
浏览文件 @
3d96ee80
import
numpy
as
np
import
pytest
import
aesara.tensor
as
at
from
aesara
import
config
from
aesara
import
config
,
grad
from
aesara.compile.mode
import
Mode
,
get_mode
from
aesara.graph.fg
import
FunctionGraph
from
aesara.scan.basic
import
scan
from
aesara.scan.utils
import
until
from
tests
import
unittest_tools
as
utt
from
tests.link.numba.test_basic
import
compare_numba_and_py
rng
=
np
.
random
.
default_rng
(
42849
)
def
test_scan_multiple_output
():
"""Test a scan implementation of a SEIR model.
SEIR model definition:
S[t+1] = S[t] - B[t]
E[t+1] = E[t] +
B[t] - C[t]
E[t+1] = E[t] +
B[t] - C[t]
I[t+1] = I[t+1] + C[t] - D[t]
B[t] ~ Binom(S[t], beta)
C[t] ~ Binom(E[t], gamma)
D[t] ~ Binom(I[t], delta)
"""
def
binomln
(
n
,
k
):
...
...
@@ -198,3 +200,99 @@ def test_scan_multiple_none_output():
test_input_vals
=
(
np
.
array
([
1.0
,
2.0
]),)
compare_numba_and_py
(
out_fg
,
test_input_vals
)
def
test_scan_save_mem_basic
():
"""Make sure we can handle storage changes caused by the `scan_save_mem` rewrite."""
k
=
at
.
iscalar
(
"k"
)
A
=
at
.
dvector
(
"A"
)
result
,
_
=
scan
(
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
outputs_info
=
at
.
ones_like
(
A
),
non_sequences
=
A
,
n_steps
=
k
,
)
numba_mode
=
get_mode
(
"NUMBA"
)
# .including("scan_save_mem")
py_mode
=
Mode
(
"py"
)
.
including
(
"scan_save_mem"
)
out_fg
=
FunctionGraph
([
A
,
k
],
[
result
])
test_input_vals
=
(
np
.
arange
(
10
,
dtype
=
np
.
int32
),
2
)
compare_numba_and_py
(
out_fg
,
test_input_vals
,
numba_mode
=
numba_mode
,
py_mode
=
py_mode
)
test_input_vals
=
(
np
.
arange
(
10
,
dtype
=
np
.
int32
),
4
)
compare_numba_and_py
(
out_fg
,
test_input_vals
,
numba_mode
=
numba_mode
,
py_mode
=
py_mode
)
@pytest.mark.parametrize
(
"n_steps_val"
,
[
1
,
5
])
def
test_scan_save_mem_2
(
n_steps_val
):
def
f_pow2
(
x_tm2
,
x_tm1
):
return
2
*
x_tm1
+
x_tm2
init_x
=
at
.
dvector
(
"init_x"
)
n_steps
=
at
.
iscalar
(
"n_steps"
)
output
,
_
=
scan
(
f_pow2
,
sequences
=
[],
outputs_info
=
[{
"initial"
:
init_x
,
"taps"
:
[
-
2
,
-
1
]}],
non_sequences
=
[],
n_steps
=
n_steps
,
)
state_val
=
np
.
array
([
1.0
,
2.0
])
numba_mode
=
get_mode
(
"NUMBA"
)
# .including("scan_save_mem")
py_mode
=
Mode
(
"py"
)
.
including
(
"scan_save_mem"
)
out_fg
=
FunctionGraph
([
init_x
,
n_steps
],
[
output
])
test_input_vals
=
(
state_val
,
n_steps_val
)
compare_numba_and_py
(
out_fg
,
test_input_vals
,
numba_mode
=
numba_mode
,
py_mode
=
py_mode
)
def
test_grad_sitsot
():
def
get_sum_of_grad
(
inp
):
scan_outputs
,
updates
=
scan
(
fn
=
lambda
x
:
x
*
2
,
outputs_info
=
[
inp
],
n_steps
=
5
,
mode
=
"NUMBA"
)
return
grad
(
scan_outputs
.
sum
(),
inp
)
.
sum
()
floatX
=
config
.
floatX
inputs_test_values
=
[
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
.
random
(
3
)
.
astype
(
floatX
)
]
utt
.
verify_grad
(
get_sum_of_grad
,
inputs_test_values
,
mode
=
"NUMBA"
)
def
test_mitmots_basic
():
init_x
=
at
.
dvector
()
seq
=
at
.
dvector
()
def
inner_fct
(
seq
,
state_old
,
state_current
):
return
state_old
*
2
+
state_current
+
seq
out
,
_
=
scan
(
inner_fct
,
sequences
=
seq
,
outputs_info
=
{
"initial"
:
init_x
,
"taps"
:
[
-
2
,
-
1
]}
)
g_outs
=
grad
(
out
.
sum
(),
[
seq
,
init_x
])
numba_mode
=
get_mode
(
"NUMBA"
)
.
including
(
"scan_save_mem"
)
py_mode
=
Mode
(
"py"
)
.
including
(
"scan_save_mem"
)
out_fg
=
FunctionGraph
([
seq
,
init_x
],
g_outs
)
seq_val
=
np
.
arange
(
3
)
init_x_val
=
np
.
r_
[
-
2
,
-
1
]
test_input_vals
=
(
seq_val
,
init_x_val
)
compare_numba_and_py
(
out_fg
,
test_input_vals
,
numba_mode
=
numba_mode
,
py_mode
=
py_mode
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论