Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
148477cb
提交
148477cb
authored
5月 23, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
5月 23, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Respect check_finite in LU decomposition rewrites
上级
6fb515d0
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
53 行增加
和
9 行删除
+53
-9
rewriting.py
pytensor/tensor/_linalg/solve/rewriting.py
+19
-8
type.py
pytensor/tensor/type.py
+1
-1
test_rewriting.py
tests/tensor/linalg/test_rewriting.py
+33
-0
没有找到文件。
pytensor/tensor/_linalg/solve/rewriting.py
浏览文件 @
148477cb
...
...
@@ -14,16 +14,22 @@ from pytensor.tensor.slinalg import Solve, lu_factor, lu_solve
from
pytensor.tensor.variable
import
TensorVariable
def
decompose_A
(
A
,
assume_a
):
def
decompose_A
(
A
,
assume_a
,
check_finite
):
if
assume_a
==
"gen"
:
return
lu_factor
(
A
,
check_finite
=
Fals
e
)
return
lu_factor
(
A
,
check_finite
=
check_finit
e
)
else
:
raise
NotImplementedError
def
solve_lu_decomposed_system
(
A_decomp
,
b
,
b_ndim
,
assume_a
,
transposed
=
False
):
if
assume_a
==
"gen"
:
return
lu_solve
(
A_decomp
,
b
,
b_ndim
=
b_ndim
,
trans
=
transposed
)
def
solve_lu_decomposed_system
(
A_decomp
,
b
,
transposed
=
False
,
*
,
core_solve_op
:
Solve
):
if
core_solve_op
.
assume_a
==
"gen"
:
return
lu_solve
(
A_decomp
,
b
,
trans
=
transposed
,
b_ndim
=
core_solve_op
.
b_ndim
,
check_finite
=
core_solve_op
.
check_finite
,
)
else
:
raise
NotImplementedError
...
...
@@ -102,14 +108,19 @@ def _split_lu_solve_steps(
):
return
None
A_decomp
=
decompose_A
(
A
,
assume_a
=
assume_a
)
# If any Op had check_finite=True, we also do it for the LU decomposition
check_finite_decomp
=
False
for
client
,
_
in
A_solve_clients_and_transpose
:
if
client
.
op
.
core_op
.
check_finite
:
check_finite_decomp
=
True
break
A_decomp
=
decompose_A
(
A
,
assume_a
=
assume_a
,
check_finite
=
check_finite_decomp
)
replacements
=
{}
for
client
,
transposed
in
A_solve_clients_and_transpose
:
_
,
b
=
client
.
inputs
b_ndim
=
client
.
op
.
core_op
.
b_ndim
new_x
=
solve_lu_decomposed_system
(
A_decomp
,
b
,
b_ndim
=
b_ndim
,
assume_a
=
assume_a
,
transposed
=
transposed
A_decomp
,
b
,
transposed
=
transposed
,
core_solve_op
=
client
.
op
.
core_op
)
[
old_x
]
=
client
.
outputs
new_x
=
atleast_Nd
(
new_x
,
n
=
old_x
.
type
.
ndim
)
.
astype
(
old_x
.
type
.
dtype
)
...
...
pytensor/tensor/type.py
浏览文件 @
148477cb
...
...
@@ -793,7 +793,7 @@ def tensor(
try
:
# Help catching errors with the new tensor API
# Many single letter strings are valid sctypes
if
str
(
name
)
==
"floatX"
or
(
len
(
str
(
name
))
>
1
and
np
.
dtype
(
name
)
.
type
):
if
str
(
name
)
==
"floatX"
or
(
len
(
str
(
name
))
>
2
and
np
.
dtype
(
name
)
.
type
):
raise
ValueError
(
f
"The first and only positional argument of tensor is now `name`. Got {name}.
\n
"
"This name looks like a dtype, which you should pass as a keyword argument only."
...
...
tests/tensor/linalg/test_rewriting.py
浏览文件 @
148477cb
...
...
@@ -161,3 +161,36 @@ def test_lu_decomposition_reused_scan(transposed):
resx1
=
fn_opt
(
A_test
,
x0_test
)
rtol
=
1e-7
if
config
.
floatX
==
"float64"
else
1e-6
np
.
testing
.
assert_allclose
(
resx0
,
resx1
,
rtol
=
rtol
)
def
test_lu_decomposition_reused_preserves_check_finite
():
# Check that the LU decomposition rewrite preserves the check_finite flag
rewrite_name
=
reuse_lu_decomposition_multiple_solves
.
__name__
A
=
tensor
(
"A"
,
shape
=
(
2
,
2
))
b1
=
tensor
(
"b1"
,
shape
=
(
2
,))
b2
=
tensor
(
"b2"
,
shape
=
(
2
,))
x1
=
solve
(
A
,
b1
,
assume_a
=
"gen"
,
check_finite
=
True
)
x2
=
solve
(
A
,
b2
,
assume_a
=
"gen"
,
check_finite
=
False
)
fn_opt
=
function
(
[
A
,
b1
,
b2
],
[
x1
,
x2
],
mode
=
get_default_mode
()
.
including
(
rewrite_name
)
)
opt_nodes
=
fn_opt
.
maker
.
fgraph
.
apply_nodes
assert
count_vanilla_solve_nodes
(
opt_nodes
)
==
0
assert
count_lu_decom_nodes
(
opt_nodes
)
==
1
assert
count_lu_solve_nodes
(
opt_nodes
)
==
2
# We should get an error if A or b1 is non finite
A_valid
=
np
.
array
([[
1
,
0
],
[
0
,
1
]],
dtype
=
A
.
type
.
dtype
)
b1_valid
=
np
.
array
([
1
,
1
],
dtype
=
b1
.
type
.
dtype
)
b2_valid
=
np
.
array
([
1
,
1
],
dtype
=
b2
.
type
.
dtype
)
assert
fn_opt
(
A_valid
,
b1_valid
,
b2_valid
)
# Fine
assert
fn_opt
(
A_valid
,
b1_valid
,
b2_valid
*
np
.
nan
)
# Should not raise (also fine on most LAPACK implementations?)
with
pytest
.
raises
(
ValueError
,
match
=
"array must not contain infs or NaNs"
):
assert
fn_opt
(
A_valid
,
b1_valid
*
np
.
nan
,
b2_valid
)
with
pytest
.
raises
(
ValueError
,
match
=
"array must not contain infs or NaNs"
):
assert
fn_opt
(
A_valid
*
np
.
nan
,
b1_valid
,
b2_valid
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论