Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
31304be2
提交
31304be2
authored
1月 07, 2026
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
1月 07, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Blockwise: Handle integers in signature and core_op.infer_shape
上级
ee47dcc9
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
42 行增加
和
4 行删除
+42
-4
blockwise.py
pytensor/tensor/blockwise.py
+15
-4
test_blockwise.py
tests/tensor/test_blockwise.py
+27
-0
没有找到文件。
pytensor/tensor/blockwise.py
浏览文件 @
31304be2
...
@@ -8,7 +8,7 @@ from pytensor import config
...
@@ -8,7 +8,7 @@ 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
import
FunctionGraph
from
pytensor.graph
import
FunctionGraph
from
pytensor.graph.basic
import
Apply
,
Constant
from
pytensor.graph.basic
import
Apply
,
Constant
,
Variable
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
(
...
@@ -371,8 +371,12 @@ class Blockwise(COp):
...
@@ -371,8 +371,12 @@ class Blockwise(COp):
safe_core_output_shapes
=
[
list
(
shape
)
for
shape
in
core_output_shapes
]
safe_core_output_shapes
=
[
list
(
shape
)
for
shape
in
core_output_shapes
]
for
core_out_shape
in
safe_core_output_shapes
:
for
core_out_shape
in
safe_core_output_shapes
:
for
o
,
core_out_dim
in
enumerate
(
core_out_shape
):
for
o
,
core_out_dim
in
enumerate
(
core_out_shape
):
if
set_dummy_core_inputs
&
set
(
if
(
explicit_graph_inputs
([
core_out_dim
])
# Some Ops return integers / literals from infer_shape...
# If it's not a Variable it can't depend on inputs
isinstance
(
core_out_dim
,
Variable
)
and
set_dummy_core_inputs
&
set
(
explicit_graph_inputs
([
core_out_dim
]))
):
):
core_out_shape
[
o
]
=
None
core_out_shape
[
o
]
=
None
...
@@ -386,9 +390,16 @@ class Blockwise(COp):
...
@@ -386,9 +390,16 @@ class Blockwise(COp):
):
):
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
if
dim_name
in
core_dims
:
if
dim_name
in
core_dims
:
# The output dim is the same as another input dim
core_out_shape
.
append
(
core_dims
[
dim_name
])
core_out_shape
.
append
(
core_dims
[
dim_name
])
elif
str
.
isnumeric
(
dim_name
):
# The core_dim has a constant size
from
pytensor.tensor.basic
import
constant
core_out_shape
.
append
(
constant
(
np
.
array
(
int
(
dim_name
),
dtype
=
"int64"
))
)
else
:
else
:
if
safe_core_out_shape
is
None
:
if
safe_core_out_shape
is
None
:
# Extract the core shape from the core_op infer_shape on demand
# Extract the core shape from the core_op infer_shape on demand
...
...
tests/tensor/test_blockwise.py
浏览文件 @
31304be2
...
@@ -19,6 +19,7 @@ from pytensor.tensor import (
...
@@ -19,6 +19,7 @@ from pytensor.tensor import (
dmatrix
,
dmatrix
,
log
,
log
,
matrices
,
matrices
,
matrix
,
ones_like
,
ones_like
,
scalar
,
scalar
,
tensor
,
tensor
,
...
@@ -352,6 +353,32 @@ def test_blockwise_infer_core_shape():
...
@@ -352,6 +353,32 @@ def test_blockwise_infer_core_shape():
assert
tuple
(
d_shape_fn
(
a_test
,
b_test
))
==
(
5
,
0
)
assert
tuple
(
d_shape_fn
(
a_test
,
b_test
))
==
(
5
,
0
)
def
test_infer_shape_literals
():
# Define a CoreOp whose infer_shape is (symbolic operation on itself, literal)
# Then tell Blockwise that the first dimension is constant.
# The Op has no perform method, so it will fail to evaluate if the infer_shape of that dimension isn't ignored
class
TestCoreOp
(
Op
):
def
make_node
(
self
,
x
):
assert
x
.
type
.
ndim
==
0
return
Apply
(
self
,
[
x
],
[
matrix
()])
def
perform
(
self
,
node
,
inputs
,
outputs
):
raise
NotImplementedError
()
def
infer_shape
(
self
,
fgraph
,
node
,
input_shapes
):
y
=
node
.
outputs
[
0
]
# Apparently it's valid to return integers in infer_shape.
# DimShuffle does this. Modify test if that is no longer allowed.
return
[(
y
[
0
][
0
]
.
astype
(
int
),
3
)]
op
=
Blockwise
(
TestCoreOp
(),
signature
=
"()->(2,a)"
)
x
=
scalar
(
"x"
)
y
=
op
(
x
)
fn
=
function
([
x
],
y
.
shape
)
assert
tuple
(
fn
(
0
))
==
(
2
,
3
)
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论