Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
97bd1ac6
提交
97bd1ac6
authored
12月 07, 2022
作者:
Rémi Louf
提交者:
Ricardo Vieira
2月 07, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use constants or raise in JAX `Arange` implementation
上级
a824aee4
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
41 行增加
和
7 行删除
+41
-7
tensor_basic.py
pytensor/link/jax/dispatch/tensor_basic.py
+30
-3
test_tensor_basic.py
tests/link/jax/test_tensor_basic.py
+11
-4
没有找到文件。
pytensor/link/jax/dispatch/tensor_basic.py
浏览文件 @
97bd1ac6
...
@@ -3,6 +3,7 @@ import warnings
...
@@ -3,6 +3,7 @@ import warnings
import
jax.numpy
as
jnp
import
jax.numpy
as
jnp
import
numpy
as
np
import
numpy
as
np
from
pytensor.graph.basic
import
Constant
from
pytensor.link.jax.dispatch.basic
import
jax_funcify
from
pytensor.link.jax.dispatch.basic
import
jax_funcify
from
pytensor.tensor
import
get_vector_length
from
pytensor.tensor
import
get_vector_length
from
pytensor.tensor.basic
import
(
from
pytensor.tensor.basic
import
(
...
@@ -22,6 +23,15 @@ from pytensor.tensor.basic import (
...
@@ -22,6 +23,15 @@ from pytensor.tensor.basic import (
from
pytensor.tensor.exceptions
import
NotScalarConstantError
from
pytensor.tensor.exceptions
import
NotScalarConstantError
ARANGE_CONCRETE_VALUE_ERROR
=
"""JAX requires the arguments of `jax.numpy.arange`
to be constants. The graph that you defined thus cannot be JIT-compiled
by JAX. An example of a graph that can be compiled to JAX:
>>> import pytensor.tensor basic
>>> at.arange(1, 10, 2)
"""
@jax_funcify.register
(
AllocDiag
)
@jax_funcify.register
(
AllocDiag
)
def
jax_funcify_AllocDiag
(
op
,
**
kwargs
):
def
jax_funcify_AllocDiag
(
op
,
**
kwargs
):
offset
=
op
.
offset
offset
=
op
.
offset
...
@@ -50,9 +60,26 @@ def jax_funcify_Alloc(op, **kwargs):
...
@@ -50,9 +60,26 @@ def jax_funcify_Alloc(op, **kwargs):
@jax_funcify.register
(
ARange
)
@jax_funcify.register
(
ARange
)
def
jax_funcify_ARange
(
op
,
**
kwargs
):
def
jax_funcify_ARange
(
op
,
node
,
**
kwargs
):
# XXX: This currently requires concrete arguments.
"""Register a JAX implementation for `ARange`.
def
arange
(
start
,
stop
,
step
):
`jax.numpy.arange` requires concrete values for its arguments. Here we check
that the arguments are constant, and raise otherwise.
TODO: Handle other situations in which values are concrete (shape of an array).
"""
arange_args
=
node
.
inputs
constant_args
=
[]
for
arg
in
arange_args
:
if
not
isinstance
(
arg
,
Constant
):
raise
NotImplementedError
(
ARANGE_CONCRETE_VALUE_ERROR
)
constant_args
.
append
(
arg
.
value
)
start
,
stop
,
step
=
constant_args
def
arange
(
*
_
):
return
jnp
.
arange
(
start
,
stop
,
step
,
dtype
=
op
.
dtype
)
return
jnp
.
arange
(
start
,
stop
,
step
,
dtype
=
op
.
dtype
)
return
arange
return
arange
...
...
tests/link/jax/test_tensor_basic.py
浏览文件 @
97bd1ac6
...
@@ -54,15 +54,22 @@ def test_jax_MakeVector():
...
@@ -54,15 +54,22 @@ def test_jax_MakeVector():
compare_jax_and_py
(
x_fg
,
[])
compare_jax_and_py
(
x_fg
,
[])
@pytest.mark.xfail
(
reason
=
"jax.numpy.arange requires concrete inputs"
)
def
test_arange
():
out
=
at
.
arange
(
1
,
10
,
2
)
fgraph
=
FunctionGraph
([],
[
out
])
compare_jax_and_py
(
fgraph
,
[])
def
test_arange_nonconcrete
():
def
test_arange_nonconcrete
():
"""JAX cannot JIT-compile `jax.numpy.arange` when arguments are not concrete values."""
a
=
scalar
(
"a"
)
a
=
scalar
(
"a"
)
a
.
tag
.
test_value
=
10
a
.
tag
.
test_value
=
10
out
=
at
.
arange
(
a
)
out
=
at
.
arange
(
a
)
fgraph
=
FunctionGraph
([
a
],
[
out
])
compare_jax_and_py
(
fgraph
,
[
get_test_value
(
i
)
for
i
in
fgraph
.
inputs
])
with
pytest
.
raises
(
NotImplementedError
):
fgraph
=
FunctionGraph
([
a
],
[
out
])
compare_jax_and_py
(
fgraph
,
[
get_test_value
(
i
)
for
i
in
fgraph
.
inputs
])
def
test_jax_Join
():
def
test_jax_Join
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论