Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b379b0f2
提交
b379b0f2
authored
1月 16, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
1月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use broadcasted output shape in local_useless_switch optimization
Closes #270
上级
175e7843
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
51 行增加
和
92 行删除
+51
-92
test_opt.py
tests/tensor/test_opt.py
+45
-70
opt.py
theano/tensor/opt.py
+6
-22
没有找到文件。
tests/tensor/test_opt.py
浏览文件 @
b379b0f2
...
@@ -6163,8 +6163,7 @@ class TestLocalUselessSwitch:
...
@@ -6163,8 +6163,7 @@ class TestLocalUselessSwitch:
def
setup_method
(
self
):
def
setup_method
(
self
):
self
.
mode
=
mode_opt
.
excluding
(
"constant_folding"
)
self
.
mode
=
mode_opt
.
excluding
(
"constant_folding"
)
def
test_const0
(
self
):
def
test_const_0
(
self
):
for
dtype1
in
[
"int32"
,
"int64"
]:
for
dtype1
in
[
"int32"
,
"int64"
]:
for
dtype2
in
[
"int32"
,
"int64"
]:
for
dtype2
in
[
"int32"
,
"int64"
]:
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
...
@@ -6186,10 +6185,15 @@ class TestLocalUselessSwitch:
...
@@ -6186,10 +6185,15 @@ class TestLocalUselessSwitch:
)
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
dtype2
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
dtype2
)
assert
np
.
all
(
f
(
vx
,
vy
)
==
vy
)
np_res
=
np
.
where
(
0
,
vx
,
vy
)
assert
np
.
array_equal
(
f
(
vx
,
vy
),
np_res
)
def
test_const1
(
self
):
res_non_bool_np
=
np
.
where
(
np
.
ones
(
10
),
0
,
1
)
non_bool_graph
=
tt
.
switch
(
np
.
ones
(
10
),
0
,
1
)
non_bool_fn
=
function
([],
non_bool_graph
,
mode
=
self
.
mode
)
assert
np
.
array_equal
(
non_bool_fn
(),
res_non_bool_np
)
def
test_const_1
(
self
):
for
dtype1
in
[
"int32"
,
"int64"
]:
for
dtype1
in
[
"int32"
,
"int64"
]:
for
dtype2
in
[
"int32"
,
"int64"
]:
for
dtype2
in
[
"int32"
,
"int64"
]:
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
...
@@ -6211,10 +6215,10 @@ class TestLocalUselessSwitch:
...
@@ -6211,10 +6215,10 @@ class TestLocalUselessSwitch:
)
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
dtype2
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
dtype2
)
assert
np
.
all
(
f
(
vx
,
vy
)
==
vx
)
np_res
=
np
.
where
(
1
,
vx
,
vy
)
assert
np
.
array_equal
(
f
(
vx
,
vy
),
np_res
)
def
test_left_is_right
(
self
):
def
test_left_is_right
(
self
):
for
dtype1
in
[
"int32"
,
"int64"
]:
for
dtype1
in
[
"int32"
,
"int64"
]:
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
varc
=
tt
.
matrix
(
"varc"
,
dtype
=
dtype1
)
varc
=
tt
.
matrix
(
"varc"
,
dtype
=
dtype1
)
...
@@ -6239,12 +6243,11 @@ class TestLocalUselessSwitch:
...
@@ -6239,12 +6243,11 @@ class TestLocalUselessSwitch:
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
vc
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
vc
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
dtype1
)
assert
np
.
a
ll
(
f1
(
vx
)
==
vx
)
assert
np
.
a
rray_equal
(
f1
(
vx
),
vx
)
assert
np
.
a
ll
(
f0
(
vx
)
==
vx
)
assert
np
.
a
rray_equal
(
f0
(
vx
),
vx
)
assert
np
.
a
ll
(
f2
(
vx
,
vc
)
==
vx
)
assert
np
.
a
rray_equal
(
f2
(
vx
,
vc
),
vx
)
def
test_shape_le_0
(
self
):
def
test_shape_le_0
(
self
):
for
dtype1
in
[
"float32"
,
"float64"
]:
for
dtype1
in
[
"float32"
,
"float64"
]:
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
x
=
tt
.
matrix
(
"x"
,
dtype
=
dtype1
)
z0
=
tt
.
switch
(
tt
.
le
(
x
.
shape
[
0
],
0
),
0
,
x
.
shape
[
0
])
z0
=
tt
.
switch
(
tt
.
le
(
x
.
shape
[
0
],
0
),
0
,
x
.
shape
[
0
])
...
@@ -6259,84 +6262,63 @@ class TestLocalUselessSwitch:
...
@@ -6259,84 +6262,63 @@ class TestLocalUselessSwitch:
assert
f0
(
vx
)
==
0
assert
f0
(
vx
)
==
0
assert
f1
(
vx
)
==
5
assert
f1
(
vx
)
==
5
def
test_broadcast1
(
self
):
def
test_broadcast
ing_
1
(
self
):
# test switch(cst, matrix, row)
# test switch(cst, matrix, row)
x
=
tt
.
matrix
(
"x"
,
dtype
=
"int32"
)
x
=
tt
.
matrix
(
"x"
,
dtype
=
"int32"
)
y
=
tt
.
vector
(
"y"
,
dtype
=
"int64"
)
y
=
tt
.
vector
(
"y"
,
dtype
=
"int64"
)
z
=
tt
.
switch
(
1
,
x
,
y
)
z
=
tt
.
switch
(
1
,
x
,
y
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
assert
(
len
(
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
tt
.
Elemwise
)
[
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
.
scalar_op
,
scal
.
basic
.
Cast
)
node
.
op
assert
not
any
(
node
.
op
==
tt
.
switch
for
node
in
f
.
maker
.
fgraph
.
toposort
())
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
tt
.
Elemwise
)
and
not
isinstance
(
node
.
op
.
scalar_op
,
scal
.
basic
.
Cast
)
]
)
==
0
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
"int32"
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
"int32"
)
vy
=
np
.
array
([
10
,
11
,
12
],
dtype
=
"int64"
)
vy
=
np
.
array
([
10
,
11
,
12
],
dtype
=
"int64"
)
assert
np
.
all
(
f
(
vx
,
vy
)
==
vx
)
np_res
=
np
.
where
(
1
,
vx
,
vy
)
assert
np
.
array_equal
(
f
(
vx
,
vy
),
np_res
)
z
=
tt
.
switch
(
0
,
x
,
y
)
z
=
tt
.
switch
(
0
,
x
,
y
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
assert
(
len
(
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
tt
.
Alloc
)
[
assert
f
.
maker
.
fgraph
.
inputs
[
1
]
==
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
inputs
[
0
]
node
.
op
assert
not
any
(
node
.
op
==
tt
.
switch
for
node
in
f
.
maker
.
fgraph
.
toposort
())
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
tt
.
Elemwise
)
]
)
==
0
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
"int32"
)
vx
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
dtype
=
"int32"
)
vy
=
np
.
array
([
10
,
11
,
12
],
dtype
=
"int64"
)
vy
=
np
.
array
([
10
,
11
,
12
],
dtype
=
"int64"
)
assert
np
.
all
(
f
(
vx
,
vy
)
==
vy
)
np_res
=
np
.
where
(
0
,
vx
,
vy
)
assert
np
.
array_equal
(
f
(
vx
,
vy
),
np_res
)
def
test_broadcast2
(
self
):
def
test_broadcast
ing_
2
(
self
):
# test switch(cst, vector, matrix)
# test switch(cst, vector, matrix)
# This case is not optimized for now.
x
=
tt
.
vector
(
"x"
,
dtype
=
"int32"
)
x
=
tt
.
vector
(
"x"
,
dtype
=
"int32"
)
y
=
tt
.
matrix
(
"y"
,
dtype
=
"int64"
)
y
=
tt
.
matrix
(
"y"
,
dtype
=
"int64"
)
z
=
tt
.
switch
(
1
,
x
,
y
)
z
=
tt
.
switch
(
1
,
x
,
y
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
assert
(
len
(
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
tt
.
Alloc
)
[
assert
not
any
(
node
.
op
==
tt
.
switch
for
node
in
f
.
maker
.
fgraph
.
toposort
())
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
tt
.
Elemwise
)
and
not
isinstance
(
node
.
op
.
scalar_op
,
scal
.
basic
.
Cast
)
]
)
==
0
)
vx
=
np
.
array
([
4
,
5
,
6
],
dtype
=
"int32"
)
vx
=
np
.
array
([
4
,
5
,
6
],
dtype
=
"int32"
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
"int64"
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
"int64"
)
assert
np
.
all
(
f
(
vx
,
vy
)
==
vx
)
np_res
=
np
.
where
(
1
,
vx
,
vy
)
assert
np
.
array_equal
(
f
(
vx
,
vy
),
np_res
)
z
=
tt
.
switch
(
0
,
x
,
y
)
z
=
tt
.
switch
(
0
,
x
,
y
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
f
=
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
assert
(
len
(
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
DeepCopyOp
)
[
assert
not
any
(
node
.
op
==
tt
.
switch
for
node
in
f
.
maker
.
fgraph
.
toposort
())
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
tt
.
Elemwise
)
]
)
==
0
)
vx
=
np
.
array
([
4
,
5
,
6
],
dtype
=
"int32"
)
vx
=
np
.
array
([
4
,
5
,
6
],
dtype
=
"int32"
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
"int64"
)
vy
=
np
.
array
([[
7
,
8
,
9
],
[
10
,
11
,
12
]],
dtype
=
"int64"
)
assert
np
.
all
(
f
(
vx
,
vy
)
==
vy
)
np_res
=
np
.
where
(
0
,
vx
,
vy
)
assert
np
.
array_equal
(
f
(
vx
,
vy
),
np_res
)
def
test_broadcast3
(
self
):
def
test_broadcast
ing_
3
(
self
):
# test switch(matrix, same_vector, same_vector)
# test switch(matrix, same_vector, same_vector)
x
=
tt
.
matrix
(
"x"
,
dtype
=
"int32"
)
x
=
tt
.
matrix
(
"x"
,
dtype
=
"int32"
)
...
@@ -6346,16 +6328,9 @@ class TestLocalUselessSwitch:
...
@@ -6346,16 +6328,9 @@ class TestLocalUselessSwitch:
vx
=
np
.
array
([[
0
,
1
],
[
1
,
0
]],
dtype
=
"int32"
)
vx
=
np
.
array
([[
0
,
1
],
[
1
,
0
]],
dtype
=
"int32"
)
vy
=
np
.
array
([
7
,
8
],
dtype
=
"int64"
)
vy
=
np
.
array
([
7
,
8
],
dtype
=
"int64"
)
utt
.
assert_allclose
(
f
(
vx
,
vy
),
np
.
where
(
vx
,
vy
,
vy
))
utt
.
assert_allclose
(
f
(
vx
,
vy
),
np
.
where
(
vx
,
vy
,
vy
))
assert
(
len
(
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
tt
.
Alloc
)
[
assert
not
any
(
node
.
op
==
tt
.
switch
for
node
in
f
.
maker
.
fgraph
.
toposort
())
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
tt
.
Elemwise
)
]
)
==
0
)
class
TestLocalMergeSwitchSameCond
:
class
TestLocalMergeSwitchSameCond
:
...
...
theano/tensor/opt.py
浏览文件 @
b379b0f2
...
@@ -97,6 +97,7 @@ from theano.tensor.elemwise import (
...
@@ -97,6 +97,7 @@ from theano.tensor.elemwise import (
ProdWithoutZeros
,
ProdWithoutZeros
,
Sum
,
Sum
,
)
)
from
theano.tensor.extra_ops
import
broadcast_shape
from
theano.tensor.sort
import
TopKOp
from
theano.tensor.sort
import
TopKOp
from
theano.tensor.subtensor
import
(
from
theano.tensor.subtensor
import
(
AdvancedIncSubtensor
,
AdvancedIncSubtensor
,
...
@@ -4327,7 +4328,9 @@ def local_useless_switch(fgraph, node):
...
@@ -4327,7 +4328,9 @@ def local_useless_switch(fgraph, node):
T.switch(le(shape_i{id}(X), 0), 0, shape_i{id}(X)) -> shape_i{id}(X)
T.switch(le(shape_i{id}(X), 0), 0, shape_i{id}(X)) -> shape_i{id}(X)
"""
"""
if
isinstance
(
node
.
op
,
Elemwise
)
and
isinstance
(
node
.
op
.
scalar_op
,
ts
.
Switch
):
if
isinstance
(
node
.
op
,
Elemwise
)
and
isinstance
(
node
.
op
.
scalar_op
,
ts
.
Switch
):
cond
=
tt
.
extract_constant
(
node
.
inputs
[
0
],
only_process_constants
=
True
)
cond
=
tt
.
extract_constant
(
node
.
inputs
[
0
],
only_process_constants
=
True
)
if
(
isinstance
(
cond
,
np
.
ndarray
)
and
cond
.
ndim
==
0
)
or
isinstance
(
if
(
isinstance
(
cond
,
np
.
ndarray
)
and
cond
.
ndim
==
0
)
or
isinstance
(
cond
,
np
.
number
cond
,
np
.
number
):
):
...
@@ -4336,37 +4339,18 @@ def local_useless_switch(fgraph, node):
...
@@ -4336,37 +4339,18 @@ def local_useless_switch(fgraph, node):
else
:
else
:
correct_out
=
node
.
inputs
[
1
]
correct_out
=
node
.
inputs
[
1
]
if
correct_out
.
ndim
!=
node
.
outputs
[
0
]
.
ndim
:
# TODO: broadcast?
return
False
if
correct_out
.
dtype
!=
node
.
outputs
[
0
]
.
dtype
:
if
correct_out
.
dtype
!=
node
.
outputs
[
0
]
.
dtype
:
out
=
tt
.
cast
(
correct_out
,
node
.
outputs
[
0
]
.
dtype
)
out
=
tt
.
cast
(
correct_out
,
node
.
outputs
[
0
]
.
dtype
)
else
:
else
:
out
=
correct_out
out
=
correct_out
if
out
.
type
.
broadcastable
!=
node
.
outputs
[
0
]
.
type
.
broadcastable
:
out_shape
=
broadcast_shape
(
*
node
.
inputs
)
# We need to copy data to the new dimensions during execution
out
=
alloc
(
out
,
*
out_shape
)
# We should not depend on node.outputs as this would
# make the new node depend on the old one that will
# get optimized again. So this create a cycle.
shps
=
[]
for
idx
,
(
b1
,
b2
),
in
enumerate
(
zip
(
out
.
type
.
broadcastable
,
node
.
outputs
[
0
]
.
type
.
broadcastable
)
):
if
b1
==
b2
:
shps
.
append
(
out
.
shape
[
idx
])
elif
not
node
.
inputs
[
1
]
.
type
.
broadcastable
[
idx
]:
shps
.
append
(
node
.
inputs
[
1
]
.
shape
[
idx
])
else
:
shps
.
append
(
node
.
inputs
[
2
]
.
shape
[
idx
])
out
=
alloc
(
out
,
*
shps
)
else
:
out
=
out
# Copy over stacktrace from selected output to new output
# Copy over stacktrace from selected output to new output
copy_stack_trace
(
node
.
outputs
+
correct_out
,
out
)
copy_stack_trace
(
node
.
outputs
+
correct_out
,
out
)
return
[
out
]
return
[
out
]
# if left is right -> left
# if left is right -> left
if
node
.
inputs
[
1
]
is
node
.
inputs
[
2
]:
if
node
.
inputs
[
1
]
is
node
.
inputs
[
2
]:
# Note: No need to copy over stacktrace, because the input node
# Note: No need to copy over stacktrace, because the input node
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论