Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9ae07ab0
Unverified
提交
9ae07ab0
authored
5月 10, 2023
作者:
jessegrabowski
提交者:
GitHub
5月 10, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix JAX Scan for output ndim > 1 (#288)
上级
cb417fe5
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
106 行增加
和
4 行删除
+106
-4
scan.py
pytensor/link/jax/dispatch/scan.py
+4
-3
test_scan.py
tests/link/jax/test_scan.py
+102
-1
没有找到文件。
pytensor/link/jax/dispatch/scan.py
浏览文件 @
9ae07ab0
...
...
@@ -154,10 +154,11 @@ def jax_funcify_Scan(op: Scan, **kwargs):
for
init_state
,
trace
,
buffer
in
zip
(
init_states
,
traces
,
buffers
):
if
init_state
is
not
None
:
# MIT-SOT and SIT-SOT: The final output should be as long as the input buffer
full_trace
=
jnp
.
concatenate
(
[
jnp
.
atleast_1d
(
init_state
),
jnp
.
atleast_1d
(
trace
)],
axis
=
0
,
trace
=
jnp
.
atleast_1d
(
trace
)
init_state
=
jnp
.
expand_dims
(
init_state
,
range
(
trace
.
ndim
-
init_state
.
ndim
)
)
full_trace
=
jnp
.
concatenate
([
init_state
,
trace
],
axis
=
0
)
buffer_size
=
buffer
.
shape
[
0
]
else
:
# NIT-SOT: Buffer is just the number of entries that should be returned
...
...
tests/link/jax/test_scan.py
浏览文件 @
9ae07ab0
...
...
@@ -13,7 +13,7 @@ from pytensor.scan.basic import scan
from
pytensor.scan.op
import
Scan
from
pytensor.tensor
import
random
from
pytensor.tensor.math
import
gammaln
,
log
from
pytensor.tensor.type
import
lscalar
,
scalar
,
vector
from
pytensor.tensor.type
import
dmatrix
,
dvector
,
lscalar
,
scalar
,
vector
from
tests.link.jax.test_basic
import
compare_jax_and_py
...
...
@@ -317,3 +317,104 @@ def test_scan_mitsot_with_nonseq():
test_input_vals
=
[
np
.
array
(
10.0
)
.
astype
(
config
.
floatX
)]
compare_jax_and_py
(
out_fg
,
test_input_vals
)
@pytest.mark.parametrize
(
"x0_func"
,
[
dvector
,
dmatrix
])
@pytest.mark.parametrize
(
"A_func"
,
[
dmatrix
,
dmatrix
])
def
test_nd_scan_sit_sot
(
x0_func
,
A_func
):
x0
=
x0_func
(
"x0"
)
A
=
A_func
(
"A"
)
n_steps
=
3
k
=
3
# Must specify mode = JAX for the inner func to avoid a GEMM Op in the JAX graph
xs
,
_
=
scan
(
lambda
X
,
A
:
A
@
X
,
non_sequences
=
[
A
],
outputs_info
=
[
x0
],
n_steps
=
n_steps
,
mode
=
get_mode
(
"JAX"
),
)
x0_val
=
(
np
.
arange
(
k
,
dtype
=
config
.
floatX
)
if
x0
.
ndim
==
1
else
np
.
diag
(
np
.
arange
(
k
,
dtype
=
config
.
floatX
))
)
A_val
=
np
.
eye
(
k
,
dtype
=
config
.
floatX
)
fg
=
FunctionGraph
([
x0
,
A
],
[
xs
])
test_input_vals
=
[
x0_val
,
A_val
]
compare_jax_and_py
(
fg
,
test_input_vals
)
def
test_nd_scan_sit_sot_with_seq
():
n_steps
=
3
k
=
3
x
=
at
.
matrix
(
"x0"
,
shape
=
(
n_steps
,
k
))
A
=
at
.
matrix
(
"A"
,
shape
=
(
k
,
k
))
# Must specify mode = JAX for the inner func to avoid a GEMM Op in the JAX graph
xs
,
_
=
scan
(
lambda
X
,
A
:
A
@
X
,
non_sequences
=
[
A
],
sequences
=
[
x
],
n_steps
=
n_steps
,
mode
=
get_mode
(
"JAX"
),
)
x_val
=
np
.
arange
(
n_steps
*
k
,
dtype
=
config
.
floatX
)
.
reshape
(
n_steps
,
k
)
A_val
=
np
.
eye
(
k
,
dtype
=
config
.
floatX
)
fg
=
FunctionGraph
([
x
,
A
],
[
xs
])
test_input_vals
=
[
x_val
,
A_val
]
compare_jax_and_py
(
fg
,
test_input_vals
)
def
test_nd_scan_mit_sot
():
x0
=
at
.
matrix
(
"x0"
,
shape
=
(
3
,
3
))
A
=
at
.
matrix
(
"A"
,
shape
=
(
3
,
3
))
B
=
at
.
matrix
(
"B"
,
shape
=
(
3
,
3
))
# Must specify mode = JAX for the inner func to avoid a GEMM Op in the JAX graph
xs
,
_
=
scan
(
lambda
xtm3
,
xtm1
,
A
,
B
:
A
@
xtm3
+
B
@
xtm1
,
outputs_info
=
[{
"initial"
:
x0
,
"taps"
:
[
-
3
,
-
1
]}],
non_sequences
=
[
A
,
B
],
n_steps
=
10
,
mode
=
get_mode
(
"JAX"
),
)
fg
=
FunctionGraph
([
x0
,
A
,
B
],
[
xs
])
x0_val
=
np
.
arange
(
9
,
dtype
=
config
.
floatX
)
.
reshape
(
3
,
3
)
A_val
=
np
.
eye
(
3
,
dtype
=
config
.
floatX
)
B_val
=
np
.
eye
(
3
,
dtype
=
config
.
floatX
)
test_input_vals
=
[
x0_val
,
A_val
,
B_val
]
compare_jax_and_py
(
fg
,
test_input_vals
)
def
test_nd_scan_sit_sot_with_carry
():
x0
=
at
.
vector
(
"x0"
,
shape
=
(
3
,))
A
=
at
.
matrix
(
"A"
,
shape
=
(
3
,
3
))
def
step
(
x
,
A
):
return
A
@
x
,
x
.
sum
()
# Must specify mode = JAX for the inner func to avoid a GEMM Op in the JAX graph
xs
,
_
=
scan
(
step
,
outputs_info
=
[
x0
,
None
],
non_sequences
=
[
A
],
n_steps
=
10
,
mode
=
get_mode
(
"JAX"
),
)
fg
=
FunctionGraph
([
x0
,
A
],
xs
)
x0_val
=
np
.
arange
(
3
,
dtype
=
config
.
floatX
)
A_val
=
np
.
eye
(
3
,
dtype
=
config
.
floatX
)
test_input_vals
=
[
x0_val
,
A_val
]
compare_jax_and_py
(
fg
,
test_input_vals
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论