Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
67519be2
提交
67519be2
authored
7月 14, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
8月 07, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename broadcast_like to alloc_like
上级
316ce0ba
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
20 行增加
和
19 行删除
+20
-19
basic.py
pytensor/tensor/rewriting/basic.py
+9
-8
elemwise.py
pytensor/tensor/rewriting/elemwise.py
+2
-2
math.py
pytensor/tensor/rewriting/math.py
+9
-9
没有找到文件。
pytensor/tensor/rewriting/basic.py
浏览文件 @
67519be2
...
...
@@ -8,6 +8,7 @@ import numpy as np
import
pytensor.scalar.basic
as
aes
from
pytensor
import
compile
from
pytensor.compile.ops
import
ViewOp
from
pytensor.graph
import
FunctionGraph
from
pytensor.graph.basic
import
Constant
,
Variable
from
pytensor.graph.rewriting.basic
import
(
NodeRewriter
,
...
...
@@ -87,13 +88,13 @@ def merge_broadcastables(broadcastables):
return
[
all
(
bcast
)
for
bcast
in
zip
(
*
broadcastables
)]
def
broadcast_like
(
value
,
template
,
fgraph
,
dtype
=
None
):
"""
Return a Variable with the same shape and dtype as the templat
e,
f
illed by broadcasting value through it. `value` will be cast as
necessary.
"""
def
alloc_like
(
value
:
TensorVariable
,
template
:
TensorVariabl
e
,
f
graph
:
FunctionGraph
,
dtype
=
None
,
)
->
TensorVariable
:
"""
Fill value to the same shape and dtype as the template via alloc."""
value
=
as_tensor_variable
(
value
)
if
value
.
type
.
is_super
(
template
.
type
):
return
value
...
...
@@ -438,7 +439,7 @@ def local_fill_to_alloc(fgraph, node):
# In this case, we assume that some broadcasting is needed (otherwise
# the condition above would've been true), so we replace the `fill`
# with an `Alloc`.
o
=
broadcast
_like
(
values_ref
,
shape_ref
,
fgraph
,
dtype
=
values_ref
.
dtype
)
o
=
alloc
_like
(
values_ref
,
shape_ref
,
fgraph
,
dtype
=
values_ref
.
dtype
)
copy_stack_trace
(
node
.
outputs
[
0
],
o
)
return
[
o
]
...
...
pytensor/tensor/rewriting/elemwise.py
浏览文件 @
67519be2
...
...
@@ -34,7 +34,7 @@ from pytensor.tensor.elemwise import CAReduce, DimShuffle, Elemwise
from
pytensor.tensor.exceptions
import
NotScalarConstantError
from
pytensor.tensor.math
import
exp
from
pytensor.tensor.rewriting.basic
import
(
broadcast
_like
,
alloc
_like
,
register_canonicalize
,
register_specialize
,
)
...
...
@@ -1242,7 +1242,7 @@ def local_inline_composite_constants(fgraph, node):
# Some of the inlined constants were broadcasting the output shape
if
node
.
outputs
[
0
]
.
type
.
broadcastable
!=
new_outputs
[
0
]
.
type
.
broadcastable
:
new_outputs
=
[
broadcast
_like
(
new_out
,
template
=
node
.
outputs
[
0
],
fgraph
=
fgraph
)
alloc
_like
(
new_out
,
template
=
node
.
outputs
[
0
],
fgraph
=
fgraph
)
for
new_out
in
new_outputs
]
...
...
pytensor/tensor/rewriting/math.py
浏览文件 @
67519be2
...
...
@@ -84,7 +84,7 @@ from pytensor.tensor.math import (
from
pytensor.tensor.math
import
sum
as
at_sum
from
pytensor.tensor.math
import
true_div
from
pytensor.tensor.rewriting.basic
import
(
broadcast
_like
,
alloc
_like
,
broadcasted_by
,
local_fill_sink
,
register_canonicalize
,
...
...
@@ -1973,7 +1973,7 @@ def local_div_to_reciprocal(fgraph, node):
new_out
=
cast
(
new_out
,
dtype
=
out
.
dtype
)
# The ones could have forced a specific length
if
not
out
.
type
.
is_super
(
new_out
.
type
):
new_out
=
broadcast
_like
(
new_out
,
out
,
fgraph
)
new_out
=
alloc
_like
(
new_out
,
out
,
fgraph
)
return
[
new_out
]
else
:
return
False
...
...
@@ -1994,9 +1994,9 @@ def local_pow_canonicalize(fgraph, node):
if
node
.
op
==
at_pow
:
cst
=
get_constant
(
node
.
inputs
[
1
])
if
cst
==
0
:
return
[
broadcast
_like
(
1
,
node
.
outputs
[
0
],
fgraph
)]
return
[
alloc
_like
(
1
,
node
.
outputs
[
0
],
fgraph
)]
if
cst
==
1
:
return
[
broadcast
_like
(
node
.
inputs
[
0
],
node
.
outputs
[
0
],
fgraph
)]
return
[
alloc
_like
(
node
.
inputs
[
0
],
node
.
outputs
[
0
],
fgraph
)]
else
:
return
False
...
...
@@ -2033,7 +2033,7 @@ def local_zero_div(fgraph, node):
node
.
op
.
scalar_op
,
(
aes
.
IntDiv
,
aes
.
TrueDiv
)
):
if
get_constant
(
node
.
inputs
[
0
])
==
0
:
ret
=
broadcast
_like
(
0
,
node
.
outputs
[
0
],
fgraph
)
ret
=
alloc
_like
(
0
,
node
.
outputs
[
0
],
fgraph
)
ret
.
tag
.
values_eq_approx
=
values_eq_approx_remove_nan
return
[
ret
]
...
...
@@ -2184,7 +2184,7 @@ def local_mul_specialize(fgraph, node):
has_neg
^=
True
# toggles
elif
y
==
0.0
:
# if we find any zero, we just return right away
return
[
broadcast
_like
(
0
,
node
.
outputs
[
0
],
fgraph
)]
return
[
alloc
_like
(
0
,
node
.
outputs
[
0
],
fgraph
)]
else
:
new_inputs
.
append
(
inp
)
...
...
@@ -2209,14 +2209,14 @@ def local_mul_specialize(fgraph, node):
new_inputs
=
[
m1
]
+
new_inputs
rval
=
mul
(
*
new_inputs
)
return
[
broadcast
_like
(
rval
,
node
.
outputs
[
0
],
fgraph
)]
return
[
alloc
_like
(
rval
,
node
.
outputs
[
0
],
fgraph
)]
else
:
# there are no variable inputs to mul
# N.B. this could have been constant-folded...
if
has_neg
:
return
[
broadcast
_like
(
-
1
,
node
.
outputs
[
0
],
fgraph
)]
return
[
alloc
_like
(
-
1
,
node
.
outputs
[
0
],
fgraph
)]
else
:
return
[
broadcast
_like
(
1
,
node
.
outputs
[
0
],
fgraph
)]
return
[
alloc
_like
(
1
,
node
.
outputs
[
0
],
fgraph
)]
@register_specialize
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论