Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
dea9940a
提交
dea9940a
authored
7月 06, 2025
作者:
ricardoV94
提交者:
Ricardo Vieira
7月 27, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Vectorize CumOp and simplify infer_shape for vector case
上级
6f8bb555
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
21 行增加
和
1 行删除
+21
-1
extra_ops.py
pytensor/tensor/extra_ops.py
+21
-1
没有找到文件。
pytensor/tensor/extra_ops.py
浏览文件 @
dea9940a
...
@@ -13,6 +13,7 @@ from pytensor.gradient import (
...
@@ -13,6 +13,7 @@ from pytensor.gradient import (
)
)
from
pytensor.graph.basic
import
Apply
,
Constant
,
Variable
from
pytensor.graph.basic
import
Apply
,
Constant
,
Variable
from
pytensor.graph.op
import
Op
from
pytensor.graph.op
import
Op
from
pytensor.graph.replace
import
_vectorize_node
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.link.c.type
import
EnumList
,
Generic
from
pytensor.link.c.type
import
EnumList
,
Generic
...
@@ -360,7 +361,7 @@ class CumOp(COp):
...
@@ -360,7 +361,7 @@ class CumOp(COp):
)
)
def
infer_shape
(
self
,
fgraph
,
node
,
shapes
):
def
infer_shape
(
self
,
fgraph
,
node
,
shapes
):
if
self
.
axis
is
None
:
if
self
.
axis
is
None
and
len
(
shapes
[
0
])
>
1
:
return
[(
prod
(
shapes
[
0
]),)]
# Flatten
return
[(
prod
(
shapes
[
0
]),)]
# Flatten
return
shapes
return
shapes
...
@@ -473,6 +474,25 @@ def cumprod(x, axis=None):
...
@@ -473,6 +474,25 @@ def cumprod(x, axis=None):
return
CumOp
(
axis
=
axis
,
mode
=
"mul"
)(
x
)
return
CumOp
(
axis
=
axis
,
mode
=
"mul"
)(
x
)
@_vectorize_node.register
(
CumOp
)
def
vectorize_cum_op
(
op
:
CumOp
,
node
:
Apply
,
batch_x
):
"""Vectorize the CumOp to work on a batch of inputs."""
[
original_x
]
=
node
.
inputs
batch_ndim
=
batch_x
.
ndim
-
original_x
.
ndim
axis
=
op
.
axis
if
axis
is
None
and
original_x
.
ndim
==
1
:
axis
=
0
elif
axis
is
not
None
:
axis
=
normalize_axis_index
(
op
.
axis
,
original_x
.
ndim
)
if
axis
is
None
:
# Ravel all unbatched dimensions and perform CumOp on the last axis
batch_x_raveled
=
[
batch_x
.
flatten
(
ndim
=
batch_ndim
+
1
)
for
x
in
batch_x
]
return
type
(
op
)(
axis
=-
1
,
mode
=
op
.
mode
)
.
make_node
(
batch_x_raveled
)
else
:
return
type
(
op
)(
axis
=
axis
+
batch_ndim
,
mode
=
op
.
mode
)
.
make_node
(
batch_x
)
def
diff
(
x
,
n
=
1
,
axis
=-
1
):
def
diff
(
x
,
n
=
1
,
axis
=-
1
):
"""Calculate the `n`-th order discrete difference along the given `axis`.
"""Calculate the `n`-th order discrete difference along the given `axis`.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论