Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
faa4175b
提交
faa4175b
authored
10月 16, 2025
作者:
Eby Elanjikal
提交者:
Ricardo Vieira
1月 08, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add fuse_blockdiagonal rewrite and corresponding test for nested BlockDiagonal
上级
ac11da62
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
68 行增加
和
0 行删除
+68
-0
linalg.py
pytensor/tensor/rewriting/linalg.py
+25
-0
test_linalg.py
tests/tensor/rewriting/test_linalg.py
+43
-0
没有找到文件。
pytensor/tensor/rewriting/linalg.py
浏览文件 @
faa4175b
...
@@ -60,11 +60,36 @@ from pytensor.tensor.slinalg import (
...
@@ -60,11 +60,36 @@ from pytensor.tensor.slinalg import (
solve_triangular
,
solve_triangular
,
)
)
from
pytensor.tensor.slinalg
import
BlockDiagonal
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
MATRIX_INVERSE_OPS
=
(
MatrixInverse
,
MatrixPinv
)
MATRIX_INVERSE_OPS
=
(
MatrixInverse
,
MatrixPinv
)
from
pytensor.tensor.slinalg
import
BlockDiagonal
from
pytensor.graph
import
Apply
def
fuse_blockdiagonal
(
node
):
# Only process if this node is a BlockDiagonal
if
not
isinstance
(
node
.
owner
.
op
,
BlockDiagonal
):
return
node
new_inputs
=
[]
changed
=
False
for
inp
in
node
.
owner
.
inputs
:
# If input is itself a BlockDiagonal, flatten its inputs
if
inp
.
owner
and
isinstance
(
inp
.
owner
.
op
,
BlockDiagonal
):
new_inputs
.
extend
(
inp
.
owner
.
inputs
)
changed
=
True
else
:
new_inputs
.
append
(
inp
)
if
changed
:
# Return a new fused BlockDiagonal with all inputs
return
BlockDiagonal
(
len
(
new_inputs
))(
*
new_inputs
)
return
node
def
is_matrix_transpose
(
x
:
TensorVariable
)
->
bool
:
def
is_matrix_transpose
(
x
:
TensorVariable
)
->
bool
:
"""Check if a variable corresponds to a transpose of the last two axes"""
"""Check if a variable corresponds to a transpose of the last two axes"""
node
=
x
.
owner
node
=
x
.
owner
...
...
tests/tensor/rewriting/test_linalg.py
浏览文件 @
faa4175b
...
@@ -43,6 +43,49 @@ from pytensor.tensor.type import dmatrix, matrix, tensor, vector
...
@@ -43,6 +43,49 @@ from pytensor.tensor.type import dmatrix, matrix, tensor, vector
from
tests
import
unittest_tools
as
utt
from
tests
import
unittest_tools
as
utt
from
tests.test_rop
import
break_op
from
tests.test_rop
import
break_op
from
pytensor.tensor.rewriting.linalg
import
fuse_blockdiagonal
def
test_nested_blockdiag_fusion
():
# Create matrix variables
x
=
pt
.
matrix
(
"x"
)
y
=
pt
.
matrix
(
"y"
)
z
=
pt
.
matrix
(
"z"
)
# Nested BlockDiagonal
inner
=
BlockDiagonal
(
2
)(
x
,
y
)
outer
=
BlockDiagonal
(
2
)(
inner
,
z
)
# Count number of BlockDiagonal ops before fusion
nodes_before
=
ancestors
([
outer
])
initial_count
=
sum
(
1
for
node
in
nodes_before
if
getattr
(
node
,
"owner"
,
None
)
and
isinstance
(
node
.
owner
.
op
,
BlockDiagonal
)
)
assert
initial_count
>
1
,
"Setup failed: should have nested BlockDiagonal"
# Apply the rewrite
fused
=
fuse_blockdiagonal
(
outer
)
# Count number of BlockDiagonal ops after fusion
nodes_after
=
ancestors
([
fused
])
fused_count
=
sum
(
1
for
node
in
nodes_after
if
getattr
(
node
,
"owner"
,
None
)
and
isinstance
(
node
.
owner
.
op
,
BlockDiagonal
)
)
assert
fused_count
==
1
,
"Nested BlockDiagonal ops were not fused"
# Check that all original inputs are preserved
fused_inputs
=
[
inp
for
node
in
ancestors
([
fused
])
if
getattr
(
node
,
"owner"
,
None
)
and
isinstance
(
node
.
owner
.
op
,
BlockDiagonal
)
for
inp
in
node
.
owner
.
inputs
]
assert
set
(
fused_inputs
)
==
{
x
,
y
,
z
},
"Inputs were not correctly fused"
def
test_matrix_inverse_rop_lop
():
def
test_matrix_inverse_rop_lop
():
rtol
=
1e-7
if
config
.
floatX
==
"float64"
else
1e-5
rtol
=
1e-7
if
config
.
floatX
==
"float64"
else
1e-5
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论