Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a824aee4
提交
a824aee4
authored
12月 07, 2022
作者:
Rémi Louf
提交者:
Ricardo Vieira
2月 07, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Raise when an array is resized with a boolean mask
上级
aa7ce086
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
40 行增加
和
9 行删除
+40
-9
subtensor.py
pytensor/link/jax/dispatch/subtensor.py
+24
-1
test_subtensor.py
pytensor/link/jax/dispatch/test_subtensor.py
+16
-8
没有找到文件。
pytensor/link/jax/dispatch/subtensor.py
浏览文件 @
a824aee4
...
@@ -13,10 +13,33 @@ from pytensor.tensor.subtensor import (
...
@@ -13,10 +13,33 @@ from pytensor.tensor.subtensor import (
from
pytensor.tensor.type_other
import
MakeSlice
from
pytensor.tensor.type_other
import
MakeSlice
BOOLEAN_MASK_ERROR
=
"""JAX does not support resizing arrays with boolean
masks. In some cases, however, it is possible to re-express your model
in a form that JAX can compile:
>>> import pytensor.tensor as at
>>> x_at = at.vector('x')
>>> y_at = x_at[x_at > 0].sum()
can be re-expressed as:
>>> import pytensor.tensor as at
>>> x_at = at.vector('x')
>>> y_at = at.where(x_at > 0, x_at, 0).sum()
"""
def
assert_indices_jax_compatible
(
node
):
ilist
=
node
.
inputs
[
1
]
if
ilist
.
type
.
dtype
==
"bool"
:
raise
NotImplementedError
(
BOOLEAN_MASK_ERROR
)
@jax_funcify.register
(
Subtensor
)
@jax_funcify.register
(
Subtensor
)
@jax_funcify.register
(
AdvancedSubtensor
)
@jax_funcify.register
(
AdvancedSubtensor
)
@jax_funcify.register
(
AdvancedSubtensor1
)
@jax_funcify.register
(
AdvancedSubtensor1
)
def
jax_funcify_Subtensor
(
op
,
**
kwargs
):
def
jax_funcify_Subtensor
(
op
,
node
,
**
kwargs
):
assert_indices_jax_compatible
(
node
)
idx_list
=
getattr
(
op
,
"idx_list"
,
None
)
idx_list
=
getattr
(
op
,
"idx_list"
,
None
)
...
...
pytensor/link/jax/dispatch/test_subtensor.py
浏览文件 @
a824aee4
...
@@ -47,16 +47,24 @@ def test_jax_Subtensors():
...
@@ -47,16 +47,24 @@ def test_jax_Subtensors():
compare_jax_and_py
(
out_fg
,
[])
compare_jax_and_py
(
out_fg
,
[])
@pytest.mark.xfail
(
def
test_jax_Subtensor_boolean_mask
():
version_parse
(
jax
.
__version__
)
>=
version_parse
(
"0.2.12"
),
"""JAX does not support resizing arrays with boolean masks."""
reason
=
"Omnistaging cannot be disabled"
,
x_at
=
at
.
arange
(
-
5
,
5
)
)
def
test_jax_Subtensors_omni
():
x_at
=
at
.
arange
(
3
*
4
*
5
)
.
reshape
((
3
,
4
,
5
))
# Boolean indices
out_at
=
x_at
[
x_at
<
0
]
out_at
=
x_at
[
x_at
<
0
]
assert
isinstance
(
out_at
.
owner
.
op
,
at_subtensor
.
AdvancedSubtensor
)
assert
isinstance
(
out_at
.
owner
.
op
,
at_subtensor
.
AdvancedSubtensor
)
with
pytest
.
raises
(
NotImplementedError
):
out_fg
=
FunctionGraph
([],
[
out_at
])
compare_jax_and_py
(
out_fg
,
[])
@pytest.mark.xfail
(
reason
=
"Re-expressible boolean logic. We need a rewrite PyTensor-side."
)
def
test_jax_Subtensor_boolean_mask_reexpressible
():
"""Some boolean logic can be re-expressed and JIT-compiled"""
x_at
=
at
.
arange
(
-
5
,
5
)
out_at
=
x_at
[
x_at
<
0
]
.
sum
()
out_fg
=
FunctionGraph
([],
[
out_at
])
out_fg
=
FunctionGraph
([],
[
out_at
])
compare_jax_and_py
(
out_fg
,
[])
compare_jax_and_py
(
out_fg
,
[])
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论