Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
815671d5
提交
815671d5
authored
7月 02, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
7月 02, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix shape errors in `scalar_solve_to_division`
上级
80acf202
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
62 行增加
和
17 行删除
+62
-17
linalg.py
pytensor/tensor/rewriting/linalg.py
+6
-1
test_linalg.py
tests/tensor/rewriting/test_linalg.py
+56
-16
没有找到文件。
pytensor/tensor/rewriting/linalg.py
浏览文件 @
815671d5
...
@@ -1046,11 +1046,15 @@ def scalar_solve_to_division(fgraph, node):
...
@@ -1046,11 +1046,15 @@ def scalar_solve_to_division(fgraph, node):
if
not
all
(
a
.
broadcastable
[
-
2
:]):
if
not
all
(
a
.
broadcastable
[
-
2
:]):
return
None
return
None
if
core_op
.
b_ndim
==
1
:
# Convert b to a column matrix
b
=
b
[
...
,
None
]
# Special handling for different types of solve
# Special handling for different types of solve
match
core_op
:
match
core_op
:
case
SolveTriangular
():
case
SolveTriangular
():
# Corner case: if user asked for a triangular solve with a unit diagonal, a is taken to be 1
# 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
new_out
=
b
/
a
if
not
core_op
.
unit_diagonal
else
pt
.
second
(
a
,
b
)
case
CholeskySolve
():
case
CholeskySolve
():
new_out
=
b
/
a
**
2
new_out
=
b
/
a
**
2
case
Solve
():
case
Solve
():
...
@@ -1061,6 +1065,7 @@ def scalar_solve_to_division(fgraph, node):
...
@@ -1061,6 +1065,7 @@ def scalar_solve_to_division(fgraph, node):
)
)
if
core_op
.
b_ndim
==
1
:
if
core_op
.
b_ndim
==
1
:
# Squeeze away the column dimension added earlier
new_out
=
new_out
.
squeeze
(
-
1
)
new_out
=
new_out
.
squeeze
(
-
1
)
copy_stack_trace
(
old_out
,
new_out
)
copy_stack_trace
(
old_out
,
new_out
)
...
...
tests/tensor/rewriting/test_linalg.py
浏览文件 @
815671d5
...
@@ -10,6 +10,7 @@ from pytensor import function
...
@@ -10,6 +10,7 @@ from pytensor import function
from
pytensor
import
tensor
as
pt
from
pytensor
import
tensor
as
pt
from
pytensor.compile
import
get_default_mode
from
pytensor.compile
import
get_default_mode
from
pytensor.configdefaults
import
config
from
pytensor.configdefaults
import
config
from
pytensor.graph
import
ancestors
from
pytensor.graph.rewriting.utils
import
rewrite_graph
from
pytensor.graph.rewriting.utils
import
rewrite_graph
from
pytensor.tensor
import
swapaxes
from
pytensor.tensor
import
swapaxes
from
pytensor.tensor.blockwise
import
Blockwise
from
pytensor.tensor.blockwise
import
Blockwise
...
@@ -989,34 +990,73 @@ def test_slogdet_specialization():
...
@@ -989,34 +990,73 @@ def test_slogdet_specialization():
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"Op, fn"
,
"a_batch_shape"
,
[(),
(
5
,)],
ids
=
lambda
x
:
f
"a_batch_shape={x}"
)
@pytest.mark.parametrize
(
"b_batch_shape"
,
[(),
(
5
,)],
ids
=
lambda
x
:
f
"b_batch_shape={x}"
)
@pytest.mark.parametrize
(
"b_ndim"
,
(
1
,
2
),
ids
=
lambda
x
:
f
"b_ndim={x}"
)
@pytest.mark.parametrize
(
"op, fn, extra_kwargs"
,
[
[
(
Solve
,
pt
.
linalg
.
solve
),
(
Solve
,
pt
.
linalg
.
solve
,
{}),
(
SolveTriangular
,
pt
.
linalg
.
solve_triangular
),
(
SolveTriangular
,
pt
.
linalg
.
solve_triangular
,
{}),
(
CholeskySolve
,
pt
.
linalg
.
cho_solve
),
(
SolveTriangular
,
pt
.
linalg
.
solve_triangular
,
{
"unit_diagonal"
:
True
}),
(
CholeskySolve
,
pt
.
linalg
.
cho_solve
,
{}),
],
],
)
)
def
test_scalar_solve_to_division_rewrite
(
Op
,
fn
):
def
test_scalar_solve_to_division_rewrite
(
rng
=
np
.
random
.
default_rng
(
sum
(
map
(
ord
,
"scalar_solve_to_division_rewrite"
)))
op
,
fn
,
extra_kwargs
,
b_ndim
,
a_batch_shape
,
b_batch_shape
):
def
solve_op_in_graph
(
graph
):
return
any
(
isinstance
(
var
.
owner
.
op
,
SolveBase
)
or
(
isinstance
(
var
.
owner
.
op
,
Blockwise
)
and
isinstance
(
var
.
owner
.
op
.
core_op
,
SolveBase
)
)
for
var
in
ancestors
(
graph
)
if
var
.
owner
)
a
=
pt
.
dmatrix
(
"a"
,
shape
=
(
1
,
1
))
rng
=
np
.
random
.
default_rng
(
b
=
pt
.
dvector
(
"b"
)
[
sum
(
map
(
ord
,
"scalar_solve_to_division_rewrite"
)),
b_ndim
,
*
a_batch_shape
,
1
,
*
b_batch_shape
,
]
)
a
=
pt
.
tensor
(
"a"
,
shape
=
(
*
a_batch_shape
,
1
,
1
),
dtype
=
"float64"
)
b
=
pt
.
tensor
(
"b"
,
shape
=
(
*
b_batch_shape
,
*
([
None
]
*
b_ndim
)),
dtype
=
"float64"
)
if
O
p
is
CholeskySolve
:
if
o
p
is
CholeskySolve
:
# cho_solve expects a tuple (c, lower) as the first input
# cho_solve expects a tuple (c, lower) as the first input
c
=
fn
((
pt
.
linalg
.
cholesky
(
a
),
True
),
b
,
b_ndim
=
1
)
c
=
fn
((
pt
.
linalg
.
cholesky
(
a
),
True
),
b
,
b_ndim
=
b_ndim
,
**
extra_kwargs
)
else
:
else
:
c
=
fn
(
a
,
b
,
b_ndim
=
1
)
c
=
fn
(
a
,
b
,
b_ndim
=
b_ndim
,
**
extra_kwargs
)
assert
solve_op_in_graph
([
c
])
f
=
function
([
a
,
b
],
c
,
mode
=
"FAST_RUN"
)
f
=
function
([
a
,
b
],
c
,
mode
=
"FAST_RUN"
)
nodes
=
f
.
maker
.
fgraph
.
apply_nodes
assert
not
solve_op_in_graph
(
f
.
maker
.
fgraph
.
outputs
)
a_val
=
rng
.
normal
(
size
=
(
*
a_batch_shape
,
1
,
1
))
.
astype
(
pytensor
.
config
.
floatX
)
b_core_shape
=
(
1
,
5
)
if
b_ndim
==
2
else
(
1
,)
b_val
=
rng
.
normal
(
size
=
(
*
b_batch_shape
,
*
b_core_shape
))
.
astype
(
pytensor
.
config
.
floatX
)
assert
not
any
(
isinstance
(
node
.
op
,
Op
)
for
node
in
nodes
)
if
op
is
CholeskySolve
:
# Avoid sign ambiguity in solve
a_val
=
a_val
**
2
a_val
=
rng
.
normal
(
size
=
(
1
,
1
))
.
astype
(
pytensor
.
config
.
floatX
)
if
extra_kwargs
.
get
(
"unit_diagonal"
,
False
):
b_val
=
rng
.
normal
(
size
=
(
1
,))
.
astype
(
pytensor
.
config
.
floatX
)
a_val
=
np
.
ones_like
(
a_val
)
c_val
=
np
.
linalg
.
solve
(
a_val
,
b_val
)
signature
=
"(n,m),(m)->(n)"
if
b_ndim
==
1
else
"(n,m),(m,k)->(n,k)"
c_val
=
np
.
vectorize
(
np
.
linalg
.
solve
,
signature
=
signature
)(
a_val
,
b_val
)
np
.
testing
.
assert_allclose
(
np
.
testing
.
assert_allclose
(
f
(
a_val
,
b_val
),
c_val
,
rtol
=
1e-7
if
config
.
floatX
==
"float64"
else
1e-5
f
(
a_val
,
b_val
),
c_val
,
rtol
=
1e-7
if
config
.
floatX
==
"float64"
else
1e-5
)
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论