Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0c398e34
提交
0c398e34
authored
4月 08, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
4月 09, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rewrite away blockwise Subtensor in gradient of Blockwise(Conv1d)
上级
a0a494ab
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
55 行增加
和
5 行删除
+55
-5
blockwise.py
pytensor/tensor/rewriting/blockwise.py
+31
-3
test_conv.py
tests/tensor/signal/test_conv.py
+24
-2
没有找到文件。
pytensor/tensor/rewriting/blockwise.py
浏览文件 @
0c398e34
...
@@ -14,7 +14,12 @@ from pytensor.tensor.rewriting.basic import (
...
@@ -14,7 +14,12 @@ from pytensor.tensor.rewriting.basic import (
register_stabilize
,
register_stabilize
,
)
)
from
pytensor.tensor.shape
import
Reshape
from
pytensor.tensor.shape
import
Reshape
from
pytensor.tensor.subtensor
import
AdvancedIncSubtensor
,
AdvancedSubtensor
,
Subtensor
from
pytensor.tensor.subtensor
import
(
AdvancedIncSubtensor
,
AdvancedSubtensor
,
Subtensor
,
indices_from_subtensor
,
)
@node_rewriter
([
Blockwise
])
@node_rewriter
([
Blockwise
])
...
@@ -216,9 +221,9 @@ def local_blockwise_reshape(fgraph, node):
...
@@ -216,9 +221,9 @@ def local_blockwise_reshape(fgraph, node):
Reshape is tricky to vectorize eagerly, because a graph like
Reshape is tricky to vectorize eagerly, because a graph like
`x.reshape([x.shape[0] * x.shape[1], -1])` has many operations
`x.reshape([x.shape[0] * x.shape[1], -1])` has many operations
that must be vectorized before we arri
z
e at the reshape operation.
that must be vectorized before we arri
v
e at the reshape operation.
For the square Reshape case, we must wait for all the intemediate
For the square Reshape case, we must wait for all the inte
r
mediate
operations to be lifted as Allocs
operations to be lifted as Allocs
"""
"""
if
not
isinstance
(
node
.
op
.
core_op
,
Reshape
):
if
not
isinstance
(
node
.
op
.
core_op
,
Reshape
):
...
@@ -234,6 +239,29 @@ def local_blockwise_reshape(fgraph, node):
...
@@ -234,6 +239,29 @@ def local_blockwise_reshape(fgraph, node):
return
[
new_out
]
return
[
new_out
]
@register_stabilize
@register_specialize
@node_rewriter
([
Blockwise
])
def
local_blockwise_of_subtensor
(
fgraph
,
node
):
"""Rewrite Blockwise of Subtensor, where the only batch input is the indexed tensor.
Blockwise(Subtensor{a: b})(x, a, b) -> x[:, a:b] when x has one batch dimension, and a/b none
"""
if
not
isinstance
(
node
.
op
.
core_op
,
Subtensor
):
return
x
,
*
idxs
=
node
.
inputs
if
not
all
(
all
(
idx
.
type
.
broadcastable
)
for
idx
in
idxs
):
return
core_idxs
=
indices_from_subtensor
(
[
idx
.
squeeze
()
for
idx
in
idxs
],
node
.
op
.
core_op
.
idx_list
)
# Add empty slices for the batch dims
none_slices
=
(
slice
(
None
),)
*
node
.
op
.
batch_ndim
(
node
)
return
[
x
[(
*
none_slices
,
*
core_idxs
)]]
@node_rewriter
(
tracks
=
[
Blockwise
],
inplace
=
True
)
@node_rewriter
(
tracks
=
[
Blockwise
],
inplace
=
True
)
def
blockwise_inplace
(
fgraph
,
node
):
def
blockwise_inplace
(
fgraph
,
node
):
blockwise_op
=
node
.
op
blockwise_op
=
node
.
op
...
...
tests/tensor/signal/test_conv.py
浏览文件 @
0c398e34
...
@@ -4,9 +4,11 @@ import numpy as np
...
@@ -4,9 +4,11 @@ import numpy as np
import
pytest
import
pytest
from
scipy.signal
import
convolve
as
scipy_convolve
from
scipy.signal
import
convolve
as
scipy_convolve
from
pytensor
import
config
,
function
from
pytensor
import
config
,
function
,
grad
from
pytensor.graph
import
ancestors
,
rewrite_graph
from
pytensor.tensor
import
matrix
,
vector
from
pytensor.tensor
import
matrix
,
vector
from
pytensor.tensor.signal.conv
import
convolve1d
from
pytensor.tensor.blockwise
import
Blockwise
from
pytensor.tensor.signal.conv
import
Conv1d
,
convolve1d
from
tests
import
unittest_tools
as
utt
from
tests
import
unittest_tools
as
utt
...
@@ -60,3 +62,23 @@ def test_convolve1d_batch_same():
...
@@ -60,3 +62,23 @@ def test_convolve1d_batch_same():
res
=
out
.
eval
({
x
:
x_test
,
y
:
y_test
})
res
=
out
.
eval
({
x
:
x_test
,
y
:
y_test
})
assert
res
.
shape
==
(
2
,
8
)
assert
res
.
shape
==
(
2
,
8
)
@pytest.mark.parametrize
(
"mode"
,
(
"full"
,
"valid"
,
"same"
))
def
test_convolve1d_batch_graph
(
mode
):
"""Test that we don't have slow Blockwise Subtensors in graph of a batched convolve1d"""
x
=
matrix
(
"x"
)
y
=
matrix
(
"y"
)
out
=
convolve1d
(
x
,
y
,
mode
=
mode
)
grads
=
grad
(
out
.
sum
(),
wrt
=
[
x
,
y
])
final_grads
=
rewrite_graph
(
grads
,
include
=
(
"ShapeOpt"
,
"canonicalize"
,
"stabilize"
,
"specialize"
)
)
blockwise_nodes
=
[
var
.
owner
for
var
in
ancestors
(
final_grads
)
if
var
.
owner
is
not
None
and
isinstance
(
var
.
owner
.
op
,
Blockwise
)
]
# Check any Blockwise are just Conv1d
assert
all
(
isinstance
(
node
.
op
.
core_op
,
Conv1d
)
for
node
in
blockwise_nodes
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论