Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7ba1082f
提交
7ba1082f
authored
8月 03, 2016
作者:
Frederic Bastien
提交者:
sentient07
8月 31, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor local_useless_alloc and local_canonicalize_alloc opt
上级
a6994e93
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
31 行增加
和
10 行删除
+31
-10
opt.py
theano/tensor/opt.py
+24
-3
test_opt.py
theano/tensor/tests/test_opt.py
+7
-7
没有找到文件。
theano/tensor/opt.py
浏览文件 @
7ba1082f
...
@@ -1749,12 +1749,33 @@ def local_useless_fill(node):
...
@@ -1749,12 +1749,33 @@ def local_useless_fill(node):
return
[
v
]
return
[
v
]
@register_useless
@gof.local_optimizer
([
T
.
alloc
])
def
local_useless_alloc
(
node
):
"""
If the input type is the same as the output type (dtype and broadcast)
there is no change in the shape of the input. So this is just a simple copy
of the input. This is not needed.
"""
op
=
node
.
op
if
not
isinstance
(
op
,
Alloc
):
return
False
input
=
node
.
inputs
[
0
]
output
=
node
.
outputs
[
0
]
# Check if dtype and broadcast remain the same.
if
input
.
type
==
output
.
type
:
# We don't need to copy over any stack traces here
return
[
input
]
@register_specialize
@register_specialize
@register_stabilize
@register_stabilize
@register_canonicalize
@register_canonicalize
@register_useless
@gof.local_optimizer
([
T
.
alloc
])
@gof.local_optimizer
([
T
.
alloc
])
def
local_
useless
_alloc
(
node
):
def
local_
canonicalize
_alloc
(
node
):
"""
"""
If the input type is the same as the output type (dtype and broadcast)
If the input type is the same as the output type (dtype and broadcast)
there is no change in the shape of the input. So this is just a simple copy
there is no change in the shape of the input. So this is just a simple copy
...
@@ -1778,8 +1799,8 @@ def local_useless_alloc(node):
...
@@ -1778,8 +1799,8 @@ def local_useless_alloc(node):
for
client
,
i
in
clients
:
for
client
,
i
in
clients
:
if
client
!=
"output"
and
isinstance
(
client
.
op
,
Alloc
):
if
client
!=
"output"
and
isinstance
(
client
.
op
,
Alloc
):
return
return
# Check if alloc adds a broadcastable dimension with shape 1.
# Check if alloc adds a broadcastable dimension with shape 1.
output_shape
=
node
.
inputs
[
1
:]
output_shape
=
node
.
inputs
[
1
:]
num_dims_with_size_1_added_to_left
=
0
num_dims_with_size_1_added_to_left
=
0
for
i
in
range
(
len
(
output_shape
)
-
input
.
ndim
):
for
i
in
range
(
len
(
output_shape
)
-
input
.
ndim
):
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
7ba1082f
...
@@ -3174,7 +3174,7 @@ class Test_local_elemwise_alloc(unittest.TestCase):
...
@@ -3174,7 +3174,7 @@ class Test_local_elemwise_alloc(unittest.TestCase):
# Exclude local_useless_alloc, since it does not introduce
# Exclude local_useless_alloc, since it does not introduce
# assert in all the same cases.
# assert in all the same cases.
self
.
fast_run_mode
=
self
.
fast_run_mode
.
excluding
(
self
.
fast_run_mode
=
self
.
fast_run_mode
.
excluding
(
'local_useless_alloc'
)
'local_useless_alloc'
,
'local_canonicalize_alloc'
)
# No optimization on alloc
# No optimization on alloc
func
=
function
(
func
=
function
(
[
self
.
vec
,
self
.
mat
],
[
self
.
vec
,
self
.
mat
],
...
@@ -3669,7 +3669,7 @@ class Test_local_useless_elemwise_comparison(unittest.TestCase):
...
@@ -3669,7 +3669,7 @@ class Test_local_useless_elemwise_comparison(unittest.TestCase):
self
.
assert_eqs_const
(
f
,
0
)
self
.
assert_eqs_const
(
f
,
0
)
class
Test_local_
useless
_alloc
(
unittest
.
TestCase
):
class
Test_local_
canonicalize
_alloc
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
...
@@ -3691,11 +3691,11 @@ class Test_local_useless_alloc(unittest.TestCase):
...
@@ -3691,11 +3691,11 @@ class Test_local_useless_alloc(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
f
)
self
.
assertRaises
(
ValueError
,
f
)
# No need to check_stack_trace as the optimization
# No need to check_stack_trace as the optimization
# local_
useless
_alloc only removes nodes.
# local_
canonicalize
_alloc only removes nodes.
def
test1
(
self
):
def
test1
(
self
):
# Test that alloc never gets instantiated during optimization
# Test that alloc never gets instantiated during optimization
mode
=
mode_opt
.
excluding
(
'local_
useless
_alloc'
)
mode
=
mode_opt
.
excluding
(
'local_
canonicalize
_alloc'
)
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
xx
=
tensor
.
fill
(
x
,
x
)
xx
=
tensor
.
fill
(
x
,
x
)
...
@@ -3707,11 +3707,11 @@ class Test_local_useless_alloc(unittest.TestCase):
...
@@ -3707,11 +3707,11 @@ class Test_local_useless_alloc(unittest.TestCase):
assert
tensor
.
Alloc
not
in
op_classes
assert
tensor
.
Alloc
not
in
op_classes
# No need to check_stack_trace as the optimization
# No need to check_stack_trace as the optimization
# local_
useless
_alloc only removes nodes.
# local_
canonicalize
_alloc only removes nodes.
def
test2
(
self
):
def
test2
(
self
):
# Test that alloc never gets instantiated during optimization
# Test that alloc never gets instantiated during optimization
mode
=
mode_opt
.
excluding
(
'local_
useless
_alloc'
)
mode
=
mode_opt
.
excluding
(
'local_
canonicalize
_alloc'
)
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
y
=
tensor
.
tile
(
x
,
(
1
,)
*
2
)
y
=
tensor
.
tile
(
x
,
(
1
,)
*
2
)
...
@@ -3729,7 +3729,7 @@ class Test_local_useless_alloc(unittest.TestCase):
...
@@ -3729,7 +3729,7 @@ class Test_local_useless_alloc(unittest.TestCase):
# The correct opt removes nodes, no need for check_stack_trace
# The correct opt removes nodes, no need for check_stack_trace
def
test_useless_alloc_with_shape_one
(
self
):
def
test_useless_alloc_with_shape_one
(
self
):
alloc_lift
=
out2in
(
local_
useless
_alloc
)
alloc_lift
=
out2in
(
local_
canonicalize
_alloc
)
x
=
shared
(
self
.
rng
.
randn
(
2
,))
x
=
shared
(
self
.
rng
.
randn
(
2
,))
y
=
shared
(
self
.
rng
.
randn
())
y
=
shared
(
self
.
rng
.
randn
())
z
=
shared
(
self
.
rng
.
randn
(
1
,
1
))
z
=
shared
(
self
.
rng
.
randn
(
1
,
1
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论