Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a1fcb77c
提交
a1fcb77c
authored
11月 15, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
11月 15, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cleanup JAX Scalar dispatch
上级
2c03ecfb
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
49 行增加
和
34 行删除
+49
-34
scalar.py
pytensor/link/jax/dispatch/scalar.py
+48
-34
test_scalar.py
tests/link/jax/test_scalar.py
+1
-0
没有找到文件。
pytensor/link/jax/dispatch/scalar.py
浏览文件 @
a1fcb77c
...
@@ -37,7 +37,7 @@ def try_import_tfp_jax_op(op: ScalarOp, jax_op_name: Optional[str] = None) -> Ca
...
@@ -37,7 +37,7 @@ def try_import_tfp_jax_op(op: ScalarOp, jax_op_name: Optional[str] = None) -> Ca
return
typing
.
cast
(
Callable
,
getattr
(
tfp_jax_math
,
jax_op_name
))
return
typing
.
cast
(
Callable
,
getattr
(
tfp_jax_math
,
jax_op_name
))
def
check_if_inputs_scalars
(
node
):
def
all_inputs_are_scalar
(
node
):
"""Check whether all the inputs of an `Elemwise` are scalar values.
"""Check whether all the inputs of an `Elemwise` are scalar values.
`jax.lax` or `jax.numpy` functions systematically return `TracedArrays`,
`jax.lax` or `jax.numpy` functions systematically return `TracedArrays`,
...
@@ -62,54 +62,68 @@ def check_if_inputs_scalars(node):
...
@@ -62,54 +62,68 @@ def check_if_inputs_scalars(node):
@jax_funcify.register
(
ScalarOp
)
@jax_funcify.register
(
ScalarOp
)
def
jax_funcify_ScalarOp
(
op
,
node
,
**
kwargs
):
def
jax_funcify_ScalarOp
(
op
,
node
,
**
kwargs
):
"""Return JAX function that implements the same computation as the Scalar Op.
This dispatch is expected to return a JAX function that works on Array inputs as Elemwise does,
even though it's dispatched on the Scalar Op.
"""
# We dispatch some PyTensor operators to Python operators
# We dispatch some PyTensor operators to Python operators
# whenever the inputs are all scalars.
# whenever the inputs are all scalars.
are_inputs_scalars
=
check_if_inputs_scalars
(
node
)
if
all_inputs_are_scalar
(
node
):
if
are_inputs_scalars
:
jax_func
=
jax_funcify_scalar_op_via_py_operators
(
op
)
elemwise
=
elemwise_scalar
(
op
)
if
jax_func
is
not
None
:
if
elemwise
is
not
None
:
return
jax_func
return
elemwise
func_name
=
op
.
nfunc_spec
[
0
]
nfunc_spec
=
getattr
(
op
,
"nfunc_spec"
,
None
)
if
nfunc_spec
is
None
:
raise
NotImplementedError
(
f
"Dispatch not implemented for Scalar Op {op}"
)
func_name
=
nfunc_spec
[
0
]
if
"."
in
func_name
:
if
"."
in
func_name
:
j
np
_func
=
functools
.
reduce
(
getattr
,
[
jax
]
+
func_name
.
split
(
"."
))
j
ax
_func
=
functools
.
reduce
(
getattr
,
[
jax
]
+
func_name
.
split
(
"."
))
else
:
else
:
jnp_func
=
getattr
(
jnp
,
func_name
)
jax_func
=
getattr
(
jnp
,
func_name
)
if
hasattr
(
op
,
"nfunc_variadic"
):
if
len
(
node
.
inputs
)
>
op
.
nfunc_spec
[
1
]:
# These are special cases that handle invalid arities due to the broken
# Some Scalar Ops accept multiple number of inputs, behaving as a variadic function,
# PyTensor `Op` type contract (e.g. binary `Op`s that also function as
# even though the base Op from `func_name` is specified as a binary Op.
# their own variadic counterparts--even when those counterparts already
# This happens with `Add`, which can work as a `Sum` for multiple scalars.
# exist as independent `Op`s).
jax_variadic_func
=
getattr
(
jnp
,
op
.
nfunc_variadic
,
None
)
jax_variadic_func
=
getattr
(
jnp
,
op
.
nfunc_variadic
)
if
not
jax_variadic_func
:
raise
NotImplementedError
(
f
"Dispatch not implemented for Scalar Op {op} with {len(node.inputs)} inputs"
)
def
elemwise
(
*
args
):
def
jax_func
(
*
args
):
if
len
(
args
)
>
op
.
nfunc_spec
[
1
]:
return
jax_variadic_func
(
return
jax_variadic_func
(
jnp
.
stack
(
jnp
.
broadcast_arrays
(
*
args
),
axis
=
0
),
axis
=
0
jnp
.
stack
(
jnp
.
broadcast_arrays
(
*
args
),
axis
=
0
),
axis
=
0
)
)
else
:
return
jnp_func
(
*
args
)
return
elemwise
return
jax_func
else
:
return
jnp_func
@functools.singledispatch
@functools.singledispatch
def
elemwise_scalar
(
op
):
def
jax_funcify_scalar_op_via_py_operators
(
op
):
"""Specialized JAX dispatch for Elemwise operations where all inputs are Scalar arrays.
Scalar (constant) arrays in the JAX backend get lowered to the native types (int, floats),
which can perform better with Python operators, and more importantly, avoid upcasting to array types
not supported by some JAX functions.
"""
return
None
return
None
@
elemwise_scalar
.register
(
Add
)
@
jax_funcify_scalar_op_via_py_operators
.register
(
Add
)
def
elemwise_scalar_a
dd
(
op
):
def
jax_funcify_scalar_A
dd
(
op
):
def
elemwise
(
*
inputs
):
def
elemwise
(
*
inputs
):
return
sum
(
inputs
)
return
sum
(
inputs
)
return
elemwise
return
elemwise
@
elemwise_scalar
.register
(
Mul
)
@
jax_funcify_scalar_op_via_py_operators
.register
(
Mul
)
def
elemwise_scalar_m
ul
(
op
):
def
jax_funcify_scalar_M
ul
(
op
):
import
operator
import
operator
from
functools
import
reduce
from
functools
import
reduce
...
@@ -119,24 +133,24 @@ def elemwise_scalar_mul(op):
...
@@ -119,24 +133,24 @@ def elemwise_scalar_mul(op):
return
elemwise
return
elemwise
@
elemwise_scalar
.register
(
Sub
)
@
jax_funcify_scalar_op_via_py_operators
.register
(
Sub
)
def
elemwise_scalar_s
ub
(
op
):
def
jax_funcify_scalar_S
ub
(
op
):
def
elemwise
(
x
,
y
):
def
elemwise
(
x
,
y
):
return
x
-
y
return
x
-
y
return
elemwise
return
elemwise
@
elemwise_scalar
.register
(
IntDiv
)
@
jax_funcify_scalar_op_via_py_operators
.register
(
IntDiv
)
def
elemwise_scalar_intd
iv
(
op
):
def
jax_funcify_scalar_IntD
iv
(
op
):
def
elemwise
(
x
,
y
):
def
elemwise
(
x
,
y
):
return
x
//
y
return
x
//
y
return
elemwise
return
elemwise
@
elemwise_scalar
.register
(
Mod
)
@
jax_funcify_scalar_op_via_py_operators
.register
(
Mod
)
def
elemwise_scalar_m
od
(
op
):
def
jax_funcify_scalar_M
od
(
op
):
def
elemwise
(
x
,
y
):
def
elemwise
(
x
,
y
):
return
x
%
y
return
x
%
y
...
...
tests/link/jax/test_scalar.py
浏览文件 @
a1fcb77c
...
@@ -23,6 +23,7 @@ from pytensor.tensor.math import (
...
@@ -23,6 +23,7 @@ from pytensor.tensor.math import (
psi
,
psi
,
sigmoid
,
sigmoid
,
softplus
,
softplus
,
tri_gamma
,
)
)
from
pytensor.tensor.type
import
matrix
,
scalar
,
vector
from
pytensor.tensor.type
import
matrix
,
scalar
,
vector
from
tests.link.jax.test_basic
import
compare_jax_and_py
from
tests.link.jax.test_basic
import
compare_jax_and_py
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论