Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
18ba52cd
提交
18ba52cd
authored
10月 06, 2024
作者:
ricardoV94
提交者:
Ricardo Vieira
12月 03, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use infer_shape of core_op to infer Blockwise core shapes
This can only be done when the output of infer_shape of the core_op depends only on the input shapes, and not their values.
上级
ef97287b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
3 行删除
+81
-3
blockwise.py
pytensor/tensor/blockwise.py
+29
-3
test_blockwise.py
tests/tensor/test_blockwise.py
+52
-0
没有找到文件。
pytensor/tensor/blockwise.py
浏览文件 @
18ba52cd
...
@@ -6,7 +6,8 @@ import numpy as np
...
@@ -6,7 +6,8 @@ import numpy as np
from
pytensor
import
config
from
pytensor
import
config
from
pytensor.compile.builders
import
OpFromGraph
from
pytensor.compile.builders
import
OpFromGraph
from
pytensor.gradient
import
DisconnectedType
from
pytensor.gradient
import
DisconnectedType
from
pytensor.graph.basic
import
Apply
,
Constant
from
pytensor.graph
import
FunctionGraph
from
pytensor.graph.basic
import
Apply
,
Constant
,
ancestors
from
pytensor.graph.null_type
import
NullType
from
pytensor.graph.null_type
import
NullType
from
pytensor.graph.op
import
Op
from
pytensor.graph.op
import
Op
from
pytensor.graph.replace
import
(
from
pytensor.graph.replace
import
(
...
@@ -185,15 +186,40 @@ class Blockwise(Op):
...
@@ -185,15 +186,40 @@ class Blockwise(Op):
batch_shape
=
broadcast_shape
(
*
batch_shapes
,
arrays_are_shapes
=
True
)
batch_shape
=
broadcast_shape
(
*
batch_shapes
,
arrays_are_shapes
=
True
)
# Try to extract the core shapes from the core_op
core_op_infer_shape
=
getattr
(
self
.
core_op
,
"infer_shape"
,
None
)
if
core_op_infer_shape
is
not
None
:
dummy_core_node
=
self
.
_create_dummy_core_node
(
node
.
inputs
)
dummy_core_inputs
=
dummy_core_node
.
inputs
dummy_fgraph
=
FunctionGraph
(
outputs
=
dummy_core_node
.
outputs
,
clone
=
False
)
core_input_shapes
=
[
input_shape
[
batch_ndims
:]
for
input_shape
in
input_shapes
]
core_output_shapes
=
core_op_infer_shape
(
dummy_fgraph
,
dummy_core_node
,
core_input_shapes
)
out_shapes
=
[]
out_shapes
=
[]
for
output
,
sig
in
zip
(
node
.
outputs
,
self
.
outputs_sig
,
strict
=
True
):
for
o
,
(
output
,
sig
)
in
enumerate
(
zip
(
node
.
outputs
,
self
.
outputs_sig
,
strict
=
True
)
):
core_out_shape
=
[]
core_out_shape
=
[]
for
i
,
dim_name
in
enumerate
(
sig
):
for
i
,
dim_name
in
enumerate
(
sig
):
# The output dim is the same as another input dim
# The output dim is the same as another input dim
if
dim_name
in
core_dims
:
if
dim_name
in
core_dims
:
core_out_shape
.
append
(
core_dims
[
dim_name
])
core_out_shape
.
append
(
core_dims
[
dim_name
])
else
:
else
:
# TODO: We could try to make use of infer_shape of core_op
if
core_op_infer_shape
is
not
None
:
# If the input values are needed to compute the dimension length, we can't use the infer_shape
# of the core_node as the value is not constant across batch dims of the Blockwise
core_out_dim
=
core_output_shapes
[
o
][
i
]
if
not
(
set
(
dummy_core_inputs
)
&
set
(
ancestors
([
core_out_dim
]))
):
core_out_shape
.
append
(
core_out_dim
)
continue
# Fallback shape requires evaluating the Blockwise Op
core_out_shape
.
append
(
Shape_i
(
batch_ndims
+
i
)(
output
))
core_out_shape
.
append
(
Shape_i
(
batch_ndims
+
i
)(
output
))
out_shapes
.
append
((
*
batch_shape
,
*
core_out_shape
))
out_shapes
.
append
((
*
batch_shape
,
*
core_out_shape
))
...
...
tests/tensor/test_blockwise.py
浏览文件 @
18ba52cd
...
@@ -259,6 +259,58 @@ def test_blockwise_shape():
...
@@ -259,6 +259,58 @@ def test_blockwise_shape():
assert
tuple
(
shape_fn
(
inp1_test
,
inp2_test
)[
1
])
==
(
7
,
5
,
4
)
assert
tuple
(
shape_fn
(
inp1_test
,
inp2_test
)[
1
])
==
(
7
,
5
,
4
)
def
test_blockwise_infer_core_shape
():
class
TestOpWithInferShape
(
Op
):
def
make_node
(
self
,
a
,
b
):
assert
a
.
type
.
ndim
==
1
assert
b
.
type
.
ndim
==
1
c
=
tensor
(
shape
=
(
None
,))
d
=
tensor
(
shape
=
(
None
,))
return
Apply
(
self
,
[
a
,
b
],
[
c
,
d
])
def
perform
(
self
,
node
,
inputs
,
outputs
):
a
,
b
=
inputs
c
,
d
=
outputs
c
[
0
]
=
np
.
arange
(
a
.
size
+
b
.
size
)
d
[
0
]
=
np
.
arange
(
a
.
sum
()
+
b
.
sum
())
def
infer_shape
(
self
,
fgraph
,
node
,
input_shapes
):
# First output shape depends only on input_shapes
# Second output shape depends on input values
x
,
y
=
node
.
inputs
[(
x_shape
,),
(
y_shape
,)]
=
input_shapes
return
(
x_shape
+
y_shape
,),
(
x
.
sum
()
+
y
.
sum
(),)
blockwise_op
=
Blockwise
(
core_op
=
TestOpWithInferShape
(),
signature
=
"(a),(b)->(c),(d)"
)
a
=
tensor
(
"a"
,
shape
=
(
5
,
3
))
b
=
tensor
(
"b"
,
shape
=
(
1
,
4
))
c
,
d
=
blockwise_op
(
a
,
b
)
assert
c
.
type
.
shape
==
(
5
,
None
)
assert
d
.
type
.
shape
==
(
5
,
None
)
c_shape_fn
=
pytensor
.
function
([
a
,
b
],
c
.
shape
)
# c_shape can be computed from the input shapes alone
assert
not
any
(
isinstance
(
getattr
(
n
.
op
,
"core_op"
,
n
.
op
),
TestOpWithInferShape
)
for
n
in
c_shape_fn
.
maker
.
fgraph
.
apply_nodes
)
d_shape_fn
=
pytensor
.
function
([
a
,
b
],
d
.
shape
)
# d_shape cannot be computed from the input shapes alone
assert
any
(
isinstance
(
getattr
(
n
.
op
,
"core_op"
,
n
.
op
),
TestOpWithInferShape
)
for
n
in
d_shape_fn
.
maker
.
fgraph
.
apply_nodes
)
a_test
=
np
.
zeros
(
a
.
type
.
shape
,
dtype
=
a
.
type
.
dtype
)
b_test
=
np
.
zeros
(
b
.
type
.
shape
,
dtype
=
b
.
type
.
dtype
)
assert
tuple
(
c_shape_fn
(
a_test
,
b_test
))
==
(
5
,
7
)
assert
tuple
(
d_shape_fn
(
a_test
,
b_test
))
==
(
5
,
0
)
class
BlockwiseOpTester
:
class
BlockwiseOpTester
:
"""Base class to test Blockwise works for specific Ops"""
"""Base class to test Blockwise works for specific Ops"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论