Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
271c2463
提交
271c2463
authored
6月 07, 2025
作者:
Jesse Grabowski
提交者:
Jesse Grabowski
6月 26, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rewrite scalar solve to division
上级
cf860fa6
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
8 行删除
+81
-8
linalg.py
pytensor/tensor/rewriting/linalg.py
+46
-0
test_linalg.py
tests/tensor/rewriting/test_linalg.py
+35
-8
没有找到文件。
pytensor/tensor/rewriting/linalg.py
浏览文件 @
271c2463
...
@@ -47,8 +47,10 @@ from pytensor.tensor.rewriting.basic import (
...
@@ -47,8 +47,10 @@ from pytensor.tensor.rewriting.basic import (
from
pytensor.tensor.slinalg
import
(
from
pytensor.tensor.slinalg
import
(
BlockDiagonal
,
BlockDiagonal
,
Cholesky
,
Cholesky
,
CholeskySolve
,
Solve
,
Solve
,
SolveBase
,
SolveBase
,
SolveTriangular
,
_bilinear_solve_discrete_lyapunov
,
_bilinear_solve_discrete_lyapunov
,
block_diag
,
block_diag
,
cholesky
,
cholesky
,
...
@@ -908,6 +910,11 @@ def rewrite_cholesky_diag_to_sqrt_diag(fgraph, node):
...
@@ -908,6 +910,11 @@ def rewrite_cholesky_diag_to_sqrt_diag(fgraph, node):
return
None
return
None
[
input
]
=
node
.
inputs
[
input
]
=
node
.
inputs
# Check if input is a (1, 1) matrix
if
all
(
input
.
type
.
broadcastable
[
-
2
:]):
return
[
pt
.
sqrt
(
input
)]
# Check for use of pt.diag first
# Check for use of pt.diag first
if
(
if
(
input
.
owner
input
.
owner
...
@@ -1020,3 +1027,42 @@ def slogdet_specialization(fgraph, node):
...
@@ -1020,3 +1027,42 @@ def slogdet_specialization(fgraph, node):
k
:
slogdet_specialization_map
[
v
]
for
k
,
v
in
dummy_replacements
.
items
()
k
:
slogdet_specialization_map
[
v
]
for
k
,
v
in
dummy_replacements
.
items
()
}
}
return
replacements
return
replacements
@register_stabilize
@register_canonicalize
@node_rewriter
([
Blockwise
])
def
scalar_solve_to_division
(
fgraph
,
node
):
"""
Replace solve(a, b) with b / a if a is a (1, 1) matrix
"""
core_op
=
node
.
op
.
core_op
if
not
isinstance
(
core_op
,
SolveBase
):
return
None
a
,
b
=
node
.
inputs
old_out
=
node
.
outputs
[
0
]
if
not
all
(
a
.
broadcastable
[
-
2
:]):
return
None
# Special handling for different types of solve
match
core_op
:
case
SolveTriangular
():
# Corner case: if user asked for a triangular solve with a unit diagonal, a is taken to be 1
new_out
=
b
/
a
if
not
core_op
.
unit_diagonal
else
b
case
CholeskySolve
():
new_out
=
b
/
a
**
2
case
Solve
():
new_out
=
b
/
a
case
_
:
raise
NotImplementedError
(
f
"Unsupported core_op type: {type(core_op)} in scalar_solve_to_divison"
)
if
core_op
.
b_ndim
==
1
:
new_out
=
new_out
.
squeeze
(
-
1
)
copy_stack_trace
(
old_out
,
new_out
)
return
[
new_out
]
tests/tensor/rewriting/test_linalg.py
浏览文件 @
271c2463
...
@@ -29,6 +29,7 @@ from pytensor.tensor.rewriting.linalg import inv_as_solve
...
@@ -29,6 +29,7 @@ from pytensor.tensor.rewriting.linalg import inv_as_solve
from
pytensor.tensor.slinalg
import
(
from
pytensor.tensor.slinalg
import
(
BlockDiagonal
,
BlockDiagonal
,
Cholesky
,
Cholesky
,
CholeskySolve
,
Solve
,
Solve
,
SolveBase
,
SolveBase
,
SolveTriangular
,
SolveTriangular
,
...
@@ -920,14 +921,6 @@ def test_rewrite_cholesky_diag_to_sqrt_diag_not_applied():
...
@@ -920,14 +921,6 @@ def test_rewrite_cholesky_diag_to_sqrt_diag_not_applied():
nodes
=
f_rewritten
.
maker
.
fgraph
.
apply_nodes
nodes
=
f_rewritten
.
maker
.
fgraph
.
apply_nodes
assert
any
(
isinstance
(
node
.
op
,
Cholesky
)
for
node
in
nodes
)
assert
any
(
isinstance
(
node
.
op
,
Cholesky
)
for
node
in
nodes
)
# Case 2 : eye is degenerate
x
=
pt
.
scalar
(
"x"
)
y
=
pt
.
eye
(
1
)
*
x
z_cholesky
=
pt
.
linalg
.
cholesky
(
y
)
f_rewritten
=
function
([
x
],
z_cholesky
,
mode
=
"FAST_RUN"
)
nodes
=
f_rewritten
.
maker
.
fgraph
.
apply_nodes
assert
any
(
isinstance
(
node
.
op
,
Cholesky
)
for
node
in
nodes
)
def
test_slogdet_specialization
():
def
test_slogdet_specialization
():
x
,
a
=
pt
.
dmatrix
(
"x"
),
np
.
random
.
rand
(
20
,
20
)
x
,
a
=
pt
.
dmatrix
(
"x"
),
np
.
random
.
rand
(
20
,
20
)
...
@@ -993,3 +986,37 @@ def test_slogdet_specialization():
...
@@ -993,3 +986,37 @@ def test_slogdet_specialization():
f
=
function
([
x
],
[
exp_det_x
,
sign_det_x
],
mode
=
"FAST_RUN"
)
f
=
function
([
x
],
[
exp_det_x
,
sign_det_x
],
mode
=
"FAST_RUN"
)
nodes
=
f
.
maker
.
fgraph
.
apply_nodes
nodes
=
f
.
maker
.
fgraph
.
apply_nodes
assert
not
any
(
isinstance
(
node
.
op
,
SLogDet
)
for
node
in
nodes
)
assert
not
any
(
isinstance
(
node
.
op
,
SLogDet
)
for
node
in
nodes
)
@pytest.mark.parametrize
(
"Op, fn"
,
[
(
Solve
,
pt
.
linalg
.
solve
),
(
SolveTriangular
,
pt
.
linalg
.
solve_triangular
),
(
CholeskySolve
,
pt
.
linalg
.
cho_solve
),
],
)
def
test_scalar_solve_to_division_rewrite
(
Op
,
fn
):
rng
=
np
.
random
.
default_rng
(
sum
(
map
(
ord
,
"scalar_solve_to_division_rewrite"
)))
a
=
pt
.
dmatrix
(
"a"
,
shape
=
(
1
,
1
))
b
=
pt
.
dvector
(
"b"
)
if
Op
is
CholeskySolve
:
# cho_solve expects a tuple (c, lower) as the first input
c
=
fn
((
pt
.
linalg
.
cholesky
(
a
),
True
),
b
,
b_ndim
=
1
)
else
:
c
=
fn
(
a
,
b
,
b_ndim
=
1
)
f
=
function
([
a
,
b
],
c
,
mode
=
"FAST_RUN"
)
nodes
=
f
.
maker
.
fgraph
.
apply_nodes
assert
not
any
(
isinstance
(
node
.
op
,
Op
)
for
node
in
nodes
)
a_val
=
rng
.
normal
(
size
=
(
1
,
1
))
.
astype
(
pytensor
.
config
.
floatX
)
b_val
=
rng
.
normal
(
size
=
(
1
,))
.
astype
(
pytensor
.
config
.
floatX
)
c_val
=
np
.
linalg
.
solve
(
a_val
,
b_val
)
np
.
testing
.
assert_allclose
(
f
(
a_val
,
b_val
),
c_val
,
rtol
=
1e-7
if
config
.
floatX
==
"float64"
else
1e-5
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论