Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0666fd56
提交
0666fd56
authored
1月 12, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
1月 12, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix bug in storage_input alignment of the JAX backend
When replacing the Shared RNG variables, the input order of the FunctionGraph was not explicitly aligned with the input storage of the function being compiled.
上级
3170c7d8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
46 行增加
和
3 行删除
+46
-3
linker.py
pytensor/link/jax/linker.py
+9
-2
test_random.py
tests/link/jax/test_random.py
+37
-1
没有找到文件。
pytensor/link/jax/linker.py
浏览文件 @
0666fd56
...
@@ -23,7 +23,7 @@ class JAXLinker(JITLinker):
...
@@ -23,7 +23,7 @@ class JAXLinker(JITLinker):
# Replace any shared RNG inputs so that their values can be updated in place
# Replace any shared RNG inputs so that their values can be updated in place
# without affecting the original RNG container. This is necessary because
# without affecting the original RNG container. This is necessary because
# JAX does not accept RandomState/Generators as inputs, and they will have to
# JAX does not accept RandomState/Generators as inputs, and they will have to
# be t
y
pyfied
# be t
i
pyfied
if
shared_rng_inputs
:
if
shared_rng_inputs
:
warnings
.
warn
(
warnings
.
warn
(
f
"The RandomType SharedVariables {shared_rng_inputs} will not be used "
f
"The RandomType SharedVariables {shared_rng_inputs} will not be used "
...
@@ -52,9 +52,16 @@ class JAXLinker(JITLinker):
...
@@ -52,9 +52,16 @@ class JAXLinker(JITLinker):
else
:
# no break
else
:
# no break
raise
ValueError
()
raise
ValueError
()
input_storage
[
input_storage_idx
]
=
new_inp_storage
input_storage
[
input_storage_idx
]
=
new_inp_storage
# We need to change the order of the inputs of the FunctionGraph
# so that the new input is in the same position as to old one,
# to align with the storage_map. We hope this is safe!
old_inp_fgrap_index
=
fgraph
.
inputs
.
index
(
old_inp
)
fgraph
.
remove_input
(
fgraph
.
remove_input
(
fgraph
.
inputs
.
index
(
old_inp
),
reason
=
"JAXLinker.fgraph_convert"
old_inp_fgrap_index
,
reason
=
"JAXLinker.fgraph_convert"
,
)
)
fgraph
.
inputs
.
remove
(
new_inp
)
fgraph
.
inputs
.
insert
(
old_inp_fgrap_index
,
new_inp
)
return
jax_funcify
(
return
jax_funcify
(
fgraph
,
input_storage
=
input_storage
,
storage_map
=
storage_map
,
**
kwargs
fgraph
,
input_storage
=
input_storage
,
storage_map
=
storage_map
,
**
kwargs
...
...
tests/link/jax/test_random.py
浏览文件 @
0666fd56
...
@@ -10,6 +10,7 @@ from pytensor.compile.sharedvalue import SharedVariable, shared
...
@@ -10,6 +10,7 @@ from pytensor.compile.sharedvalue import SharedVariable, shared
from
pytensor.graph.basic
import
Constant
from
pytensor.graph.basic
import
Constant
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.tensor.random.basic
import
RandomVariable
from
pytensor.tensor.random.basic
import
RandomVariable
from
pytensor.tensor.random.type
import
RandomType
from
pytensor.tensor.random.utils
import
RandomStream
from
pytensor.tensor.random.utils
import
RandomStream
from
tests.link.jax.test_basic
import
compare_jax_and_py
,
jax_mode
,
set_test_value
from
tests.link.jax.test_basic
import
compare_jax_and_py
,
jax_mode
,
set_test_value
...
@@ -58,7 +59,42 @@ def test_random_updates(rng_ctor):
...
@@ -58,7 +59,42 @@ def test_random_updates(rng_ctor):
)
)
def
test_random_updates_input_storage_order
():
@pytest.mark.parametrize
(
"noise_first"
,
(
False
,
True
))
def
test_replaced_shared_rng_storage_order
(
noise_first
):
# Test that replacing the RNG variable in the linker does not cause
# a disalignment between the compiled graph and the storage_map.
mu
=
pytensor
.
shared
(
np
.
array
(
1.0
),
name
=
"mu"
)
rng
=
pytensor
.
shared
(
np
.
random
.
default_rng
(
123
))
next_rng
,
noise
=
pt
.
random
.
normal
(
rng
=
rng
)
.
owner
.
outputs
if
noise_first
:
out
=
noise
*
mu
else
:
out
=
mu
*
noise
updates
=
{
mu
:
pt
.
grad
(
out
,
mu
),
rng
:
next_rng
,
}
f
=
compile_random_function
([],
[
out
],
updates
=
updates
,
mode
=
"JAX"
)
# The bug was found when noise used to be the first input of the fgraph
# If this changes, the test may need to be tweaked to keep the save coverage
assert
isinstance
(
f
.
input_storage
[
1
-
noise_first
]
.
type
,
RandomType
),
"Test may need to be tweaked"
# Confirm that input_storage type and fgraph input order are aligned
for
storage
,
fgrapn_input
in
zip
(
f
.
input_storage
,
f
.
maker
.
fgraph
.
inputs
):
assert
storage
.
type
==
fgrapn_input
.
type
assert
mu
.
get_value
()
==
1
f
()
assert
mu
.
get_value
()
!=
1
def
test_replaced_shared_rng_storage_ordering_equality
():
"""Test case described in issue #314.
"""Test case described in issue #314.
This happened when we tried to update the input storage after we clone the shared RNG.
This happened when we tried to update the input storage after we clone the shared RNG.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论