Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0fced9aa
提交
0fced9aa
authored
6月 12, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
6月 13, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Detect cases where `boolean_indexing_sum` does not apply
上级
62f84f53
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
27 行增加
和
5 行删除
+27
-5
jax.py
pytensor/tensor/rewriting/jax.py
+12
-3
test_subtensor.py
tests/link/jax/test_subtensor.py
+15
-2
没有找到文件。
pytensor/tensor/rewriting/jax.py
浏览文件 @
0fced9aa
...
...
@@ -48,7 +48,7 @@ optdb.register(
@node_rewriter
([
Sum
])
def
boolean_indexing_sum
(
fgraph
,
node
):
"""Replace the sum of `AdvancedSubtensor` with boolean indexing.
"""Replace the sum of `AdvancedSubtensor` with
exclusively
boolean indexing.
JAX cannot JIT-compile functions that use boolean indexing, but can compile
those expressions that can be re-expressed using `jax.numpy.where`. This
...
...
@@ -61,14 +61,21 @@ def boolean_indexing_sum(fgraph, node):
if
not
isinstance
(
operand
,
TensorVariable
):
return
# If it's not a scalar reduction, it couldn't have been a pure boolean mask
if
node
.
outputs
[
0
]
.
ndim
!=
0
:
return
if
operand
.
owner
is
None
:
return
if
not
isinstance
(
operand
.
owner
.
op
,
AdvancedSubtensor
):
return
x
=
operand
.
owner
.
inputs
[
0
]
cond
=
operand
.
owner
.
inputs
[
1
]
# Get out if AdvancedSubtensor has more than a single indexing operation
if
len
(
operand
.
owner
.
inputs
)
>
2
:
return
[
x
,
cond
]
=
operand
.
owner
.
inputs
if
not
isinstance
(
cond
,
TensorVariable
):
return
...
...
@@ -76,6 +83,8 @@ def boolean_indexing_sum(fgraph, node):
if
not
cond
.
type
.
dtype
==
"bool"
:
return
# Output must be a scalar, since pure boolean indexing returns a vector
# No need to worry about axis
out
=
at
.
sum
(
at
.
where
(
cond
,
x
,
0
))
return
out
.
owner
.
outputs
...
...
tests/link/jax/test_subtensor.py
浏览文件 @
0fced9aa
...
...
@@ -5,6 +5,7 @@ import pytensor.tensor as at
from
pytensor.configdefaults
import
config
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.tensor
import
subtensor
as
at_subtensor
from
pytensor.tensor.rewriting.jax
import
boolean_indexing_sum
from
tests.link.jax.test_basic
import
compare_jax_and_py
...
...
@@ -93,10 +94,22 @@ def test_jax_Subtensor_boolean_mask_reexpressible():
improvement over its user interface.
"""
x_at
=
at
.
vector
(
"x"
)
x_at
=
at
.
matrix
(
"x"
)
out_at
=
x_at
[
x_at
<
0
]
.
sum
()
out_fg
=
FunctionGraph
([
x_at
],
[
out_at
])
compare_jax_and_py
(
out_fg
,
[
np
.
arange
(
-
5
,
5
)
.
astype
(
config
.
floatX
)])
compare_jax_and_py
(
out_fg
,
[
np
.
arange
(
25
)
.
reshape
(
5
,
5
)
.
astype
(
config
.
floatX
)])
def
test_boolean_indexing_sum_not_applicable
():
"""Test that boolean_indexing_sum does not return an invalid replacement in cases where it doesn't apply."""
x
=
at
.
matrix
(
"x"
)
out
=
x
[
x
[:,
0
]
<
0
,
:]
.
sum
(
axis
=-
1
)
fg
=
FunctionGraph
([
x
],
[
out
])
assert
boolean_indexing_sum
.
transform
(
fg
,
fg
.
outputs
[
0
]
.
owner
)
is
None
out
=
x
[
x
[:,
0
]
<
0
,
0
]
.
sum
()
fg
=
FunctionGraph
([
x
],
[
out
])
assert
boolean_indexing_sum
.
transform
(
fg
,
fg
.
outputs
[
0
]
.
owner
)
is
None
def
test_jax_IncSubtensor
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论