Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
aa89e44b
提交
aa89e44b
authored
4月 23, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
5月 29, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor helper to create safe gufunc signature
上级
b6874c66
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
28 行增加
和
20 行删除
+28
-20
blockwise.py
pytensor/tensor/blockwise.py
+6
-19
utils.py
pytensor/tensor/utils.py
+22
-1
没有找到文件。
pytensor/tensor/blockwise.py
浏览文件 @
aa89e44b
...
...
@@ -6,7 +6,7 @@ import numpy as np
from
pytensor
import
config
from
pytensor.gradient
import
DisconnectedType
from
pytensor.graph.basic
import
Apply
,
Constant
,
Variable
from
pytensor.graph.basic
import
Apply
,
Constant
from
pytensor.graph.null_type
import
NullType
from
pytensor.graph.op
import
Op
from
pytensor.graph.replace
import
(
...
...
@@ -22,27 +22,11 @@ from pytensor.tensor.utils import (
_parse_gufunc_signature
,
broadcast_static_dim_lengths
,
import_func_from_string
,
safe_signature
,
)
from
pytensor.tensor.variable
import
TensorVariable
def
safe_signature
(
core_inputs
:
Sequence
[
Variable
],
core_outputs
:
Sequence
[
Variable
],
)
->
str
:
def
operand_sig
(
operand
:
Variable
,
prefix
:
str
)
->
str
:
operands
=
","
.
join
(
f
"{prefix}{i}"
for
i
in
range
(
operand
.
type
.
ndim
))
return
f
"({operands})"
inputs_sig
=
","
.
join
(
operand_sig
(
i
,
prefix
=
f
"i{n}"
)
for
n
,
i
in
enumerate
(
core_inputs
)
)
outputs_sig
=
","
.
join
(
operand_sig
(
o
,
prefix
=
f
"o{n}"
)
for
n
,
o
in
enumerate
(
core_outputs
)
)
return
f
"{inputs_sig}->{outputs_sig}"
class
Blockwise
(
Op
):
"""Generalizes a core `Op` to work with batched dimensions.
...
...
@@ -385,7 +369,10 @@ def vectorize_node_fallback(op: Op, node: Apply, *bached_inputs) -> Apply:
else
:
# TODO: This is pretty bad for shape inference and merge optimization!
# Should get better as we add signatures to our Ops
signature
=
safe_signature
(
node
.
inputs
,
node
.
outputs
)
signature
=
safe_signature
(
[
inp
.
type
.
ndim
for
inp
in
node
.
inputs
],
[
out
.
type
.
ndim
for
out
in
node
.
outputs
],
)
return
cast
(
Apply
,
Blockwise
(
op
,
signature
=
signature
)
.
make_node
(
*
bached_inputs
))
...
...
pytensor/tensor/utils.py
浏览文件 @
aa89e44b
...
...
@@ -172,7 +172,11 @@ _ARGUMENT_LIST = f"{_ARGUMENT}(?:,{_ARGUMENT})*"
_SIGNATURE
=
f
"^{_ARGUMENT_LIST}->{_ARGUMENT_LIST}$"
def
_parse_gufunc_signature
(
signature
):
def
_parse_gufunc_signature
(
signature
,
)
->
tuple
[
list
[
tuple
[
str
,
...
]],
...
]:
# mypy doesn't know it's alwayl a length two tuple
"""
Parse string signatures for a generalized universal function.
...
...
@@ -198,3 +202,20 @@ def _parse_gufunc_signature(signature):
]
for
arg_list
in
signature
.
split
(
"->"
)
)
def
safe_signature
(
core_inputs_ndim
:
Sequence
[
int
],
core_outputs_ndim
:
Sequence
[
int
],
)
->
str
:
def
operand_sig
(
operand_ndim
:
int
,
prefix
:
str
)
->
str
:
operands
=
","
.
join
(
f
"{prefix}{i}"
for
i
in
range
(
operand_ndim
))
return
f
"({operands})"
inputs_sig
=
","
.
join
(
operand_sig
(
ndim
,
prefix
=
f
"i{n}"
)
for
n
,
ndim
in
enumerate
(
core_inputs_ndim
)
)
outputs_sig
=
","
.
join
(
operand_sig
(
ndim
,
prefix
=
f
"o{n}"
)
for
n
,
ndim
in
enumerate
(
core_outputs_ndim
)
)
return
f
"{inputs_sig}->{outputs_sig}"
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论