Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e3f1e040
Unverified
提交
e3f1e040
authored
3月 14, 2026
作者:
Alessandro Gentili
提交者:
GitHub
3月 14, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove Iv core Op in favor of Ive and add rewrite rule for log of it (#1929)
上级
4273eb87
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
43 行增加
和
42 行删除
+43
-42
scalar.py
pytensor/link/jax/dispatch/scalar.py
+0
-11
math.py
pytensor/scalar/math.py
+1
-26
math.py
pytensor/tensor/math.py
+7
-2
math.py
pytensor/tensor/rewriting/math.py
+15
-0
math.py
pytensor/xtensor/math.py
+6
-2
test_scalar.py
tests/link/jax/test_scalar.py
+1
-1
test_math.py
tests/tensor/rewriting/test_math.py
+13
-0
没有找到文件。
pytensor/link/jax/dispatch/scalar.py
浏览文件 @
e3f1e040
...
@@ -29,7 +29,6 @@ from pytensor.scalar.math import (
...
@@ -29,7 +29,6 @@ from pytensor.scalar.math import (
Erfinv
,
Erfinv
,
GammaIncCInv
,
GammaIncCInv
,
GammaIncInv
,
GammaIncInv
,
Iv
,
Ive
,
Ive
,
Kve
,
Kve
,
Log1mexp
,
Log1mexp
,
...
@@ -277,16 +276,6 @@ def jax_funcify_from_tfp(op, **kwargs):
...
@@ -277,16 +276,6 @@ def jax_funcify_from_tfp(op, **kwargs):
return
tfp_jax_op
return
tfp_jax_op
@jax_funcify.register
(
Iv
)
def
jax_funcify_Iv
(
op
,
**
kwargs
):
ive
=
try_import_tfp_jax_op
(
op
,
jax_op_name
=
"bessel_ive"
)
def
iv
(
v
,
x
):
return
ive
(
v
,
x
)
/
jnp
.
exp
(
-
jnp
.
abs
(
jnp
.
real
(
x
)))
return
iv
@jax_funcify.register
(
Ive
)
@jax_funcify.register
(
Ive
)
def
jax_funcify_Ive
(
op
,
**
kwargs
):
def
jax_funcify_Ive
(
op
,
**
kwargs
):
return
try_import_tfp_jax_op
(
op
,
jax_op_name
=
"bessel_ive"
)
return
try_import_tfp_jax_op
(
op
,
jax_op_name
=
"bessel_ive"
)
...
...
pytensor/scalar/math.py
浏览文件 @
e3f1e040
...
@@ -1073,31 +1073,6 @@ class J0(UnaryScalarOp):
...
@@ -1073,31 +1073,6 @@ class J0(UnaryScalarOp):
j0
=
J0
(
upgrade_to_float
,
name
=
"j0"
)
j0
=
J0
(
upgrade_to_float
,
name
=
"j0"
)
class
Iv
(
BinaryScalarOp
):
"""
Modified Bessel function of the first kind of order v (real).
"""
nfunc_spec
=
(
"scipy.special.iv"
,
2
,
1
)
def
impl
(
self
,
v
,
x
):
return
special
.
iv
(
v
,
x
)
def
grad
(
self
,
inputs
,
grads
):
v
,
x
=
inputs
(
gz
,)
=
grads
return
[
grad_not_implemented
(
self
,
0
,
v
),
gz
*
(
iv
(
v
-
1
,
x
)
+
iv
(
v
+
1
,
x
))
/
2.0
,
]
def
c_code
(
self
,
*
args
,
**
kwargs
):
raise
NotImplementedError
()
iv
=
Iv
(
upgrade_to_float
,
name
=
"iv"
)
class
I1
(
UnaryScalarOp
):
class
I1
(
UnaryScalarOp
):
"""
"""
Modified Bessel function of the first kind of order 1.
Modified Bessel function of the first kind of order 1.
...
@@ -1111,7 +1086,7 @@ class I1(UnaryScalarOp):
...
@@ -1111,7 +1086,7 @@ class I1(UnaryScalarOp):
def
grad
(
self
,
inputs
,
grads
):
def
grad
(
self
,
inputs
,
grads
):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
gz
,)
=
grads
(
gz
,)
=
grads
return
[
gz
*
(
i0
(
x
)
+
iv
(
2
,
x
))
/
2.0
]
return
[
gz
*
(
i0
(
x
)
+
iv
e
(
2
,
x
)
*
exp
(
abs
(
x
)
))
/
2.0
]
def
c_code
(
self
,
*
args
,
**
kwargs
):
def
c_code
(
self
,
*
args
,
**
kwargs
):
raise
NotImplementedError
()
raise
NotImplementedError
()
...
...
pytensor/tensor/math.py
浏览文件 @
e3f1e040
...
@@ -2429,9 +2429,14 @@ def i1(x):
...
@@ -2429,9 +2429,14 @@ def i1(x):
"""Modified Bessel function of the first kind of order 1."""
"""Modified Bessel function of the first kind of order 1."""
@scalar_elemwise
def
iv
(
v
,
x
):
def
iv
(
v
,
x
):
"""Modified Bessel function of the first kind of order v (real)."""
"""Modified Bessel function of the first kind of order v (real).
Computed as ``ive(v, x) * exp(abs(x))`` for numerical consistency with
``ive``. For large ``x``, prefer working in log-space:
``log(iv(v, x)) == log(ive(v, x)) + abs(x)`` to avoid overflow.
"""
return
ive
(
v
,
x
)
*
exp
(
abs
(
x
))
@scalar_elemwise
@scalar_elemwise
...
...
pytensor/tensor/rewriting/math.py
浏览文件 @
e3f1e040
...
@@ -63,6 +63,7 @@ from pytensor.tensor.math import (
...
@@ -63,6 +63,7 @@ from pytensor.tensor.math import (
ge
,
ge
,
int_div
,
int_div
,
isinf
,
isinf
,
ive
,
kve
,
kve
,
le
,
le
,
log
,
log
,
...
@@ -3888,3 +3889,17 @@ local_log_kv = PatternNodeRewriter(
...
@@ -3888,3 +3889,17 @@ local_log_kv = PatternNodeRewriter(
)
)
register_stabilize
(
local_log_kv
)
register_stabilize
(
local_log_kv
)
local_log_iv
=
PatternNodeRewriter
(
# Rewrite log(iv(v, x)) = log(ive(v, x) * exp(abs(x))) -> log(ive(v, x)) + abs(x)
(
log
,
(
mul
,
(
ive
,
"v"
,
"x"
),
(
exp
,
(
pt_abs
,
"x"
)))),
(
add
,
(
log
,
(
ive
,
"v"
,
"x"
)),
(
pt_abs
,
"x"
)),
allow_multiple_clients
=
True
,
name
=
"local_log_iv"
,
# Start the rewrite from the less likely ive node
tracks
=
[
ive
],
get_nodes
=
get_clients_at_depth2
,
)
register_stabilize
(
local_log_iv
)
pytensor/xtensor/math.py
浏览文件 @
e3f1e040
...
@@ -259,8 +259,12 @@ def isinf(): ...
...
@@ -259,8 +259,12 @@ def isinf(): ...
def
isnan
():
...
def
isnan
():
...
@_as_xelemwise
(
ps
.
iv
)
def
iv
(
v
,
x
):
def
iv
():
...
"""Modified Bessel function of the first kind of order v (real).
Computed as ``ive(v, x) * exp(abs(x))`` for numerical consistency.
"""
return
ive
(
v
,
x
)
*
exp
(
abs
(
x
))
@_as_xelemwise
(
ps
.
ive
)
@_as_xelemwise
(
ps
.
ive
)
...
...
tests/link/jax/test_scalar.py
浏览文件 @
e3f1e040
...
@@ -38,7 +38,7 @@ from pytensor.link.jax.dispatch import jax_funcify
...
@@ -38,7 +38,7 @@ from pytensor.link.jax.dispatch import jax_funcify
try
:
try
:
pass
import
tensorflow_probability.substrates.jax.math
# noqa: F401
TFP_INSTALLED
=
True
TFP_INSTALLED
=
True
except
ModuleNotFoundError
:
except
ModuleNotFoundError
:
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
e3f1e040
...
@@ -4785,6 +4785,19 @@ def test_log_kv_stabilization():
...
@@ -4785,6 +4785,19 @@ def test_log_kv_stabilization():
)
)
def
test_log_iv_stabilization
():
x
=
pt
.
scalar
(
"x"
)
out
=
log
(
pt
.
iv
(
4.5
,
x
))
# Expression would overflow to inf without rewrite
mode
=
get_default_mode
()
.
including
(
"stabilize"
)
# Reference value log(ive(4.5, 1000.0)) + 1000.0
np
.
testing
.
assert_allclose
(
out
.
eval
({
x
:
1000.0
},
mode
=
mode
),
995.6171788390135
,
)
@pytest.mark.parametrize
(
"shape"
,
[(),
(
4
,
5
,
6
)],
ids
=
[
"scalar"
,
"tensor"
])
@pytest.mark.parametrize
(
"shape"
,
[(),
(
4
,
5
,
6
)],
ids
=
[
"scalar"
,
"tensor"
])
def
test_pow_1_rewrite
(
shape
):
def
test_pow_1_rewrite
(
shape
):
x
=
pt
.
tensor
(
"x"
,
shape
=
shape
)
x
=
pt
.
tensor
(
"x"
,
shape
=
shape
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论