Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a074ec4a
提交
a074ec4a
authored
2月 25, 2015
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2515 from sisp/local_useless_subtensor
AdvancedSubtensor1 support for local_useless_subtensor optimization
上级
8e85dbab
0b03f9d4
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
71 行增加
和
7 行删除
+71
-7
opt.py
theano/tensor/opt.py
+47
-7
test_opt.py
theano/tensor/tests/test_opt.py
+24
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
a074ec4a
...
@@ -1917,16 +1917,21 @@ def local_set_to_inc_subtensor(node):
...
@@ -1917,16 +1917,21 @@ def local_set_to_inc_subtensor(node):
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
@gof.local_optimizer
([
Subtensor
])
@gof.local_optimizer
([
Subtensor
,
AdvancedSubtensor1
])
def
local_useless_subtensor
(
node
):
def
local_useless_subtensor
(
node
):
"""
"""
Remove Subtensor if it takes the full input
Remove Subtensor/AdvancedSubtensor1 if it takes the full input. In the
AdvancedSubtensor1 case, the full input is taken when the indices are
equivalent to `arange(0, input.shape[0], 1)` using either an explicit
list/vector or the ARange op.
"""
"""
# This optimization needs ShapeOpt and fgraph.shape_feature
if
not
hasattr
(
node
.
fgraph
,
'shape_feature'
):
return
shape_of
=
node
.
fgraph
.
shape_feature
.
shape_of
if
isinstance
(
node
.
op
,
Subtensor
):
if
isinstance
(
node
.
op
,
Subtensor
):
# This optimization needs ShapeOpt and fgraph.shape_feature
if
not
hasattr
(
node
.
fgraph
,
'shape_feature'
):
return
shape_of
=
node
.
fgraph
.
shape_feature
.
shape_of
cdata
=
node
.
op
.
get_constant_idx
(
node
.
inputs
,
allow_partial
=
True
)
cdata
=
node
.
op
.
get_constant_idx
(
node
.
inputs
,
allow_partial
=
True
)
for
pos
,
idx
in
enumerate
(
cdata
):
for
pos
,
idx
in
enumerate
(
cdata
):
if
not
isinstance
(
idx
,
slice
):
if
not
isinstance
(
idx
,
slice
):
...
@@ -1985,8 +1990,43 @@ def local_useless_subtensor(node):
...
@@ -1985,8 +1990,43 @@ def local_useless_subtensor(node):
pass
pass
else
:
else
:
return
False
return
False
elif
isinstance
(
node
.
op
,
AdvancedSubtensor1
):
# get length of the indexed tensor along the first axis
try
:
length
=
get_scalar_constant_value
(
shape_of
[
node
.
inputs
[
0
]][
0
])
except
NotScalarConstantError
:
return
False
# get index (which must be a vector by definition)
idx
=
node
.
inputs
[
1
]
# `idx` must be equivalent to [0,1,...,shape[0] - 1] to qualify for
# this optimization
if
isinstance
(
idx
,
T
.
Constant
):
idx
=
idx
.
value
if
len
(
idx
)
!=
length
:
return
False
if
numpy
.
any
(
idx
!=
numpy
.
arange
(
length
)):
return
False
elif
idx
.
owner
is
not
None
and
isinstance
(
idx
.
owner
.
op
,
T
.
ARange
):
try
:
start
,
stop
,
step
=
map
(
get_scalar_constant_value
,
idx
.
owner
.
inputs
)
except
NotScalarConstantError
:
return
False
if
start
!=
0
:
return
False
if
stop
!=
length
:
return
False
if
step
!=
1
:
return
False
else
:
return
False
else
:
return
False
return
[
node
.
inputs
[
0
]]
return
[
node
.
inputs
[
0
]]
@register_canonicalize
@register_canonicalize
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
a074ec4a
...
@@ -45,6 +45,7 @@ from theano.tensor import vector, ivector, lvector, fvector, dvector
...
@@ -45,6 +45,7 @@ from theano.tensor import vector, ivector, lvector, fvector, dvector
from
theano.tensor
import
matrix
,
imatrix
,
lmatrix
,
fmatrix
,
dmatrix
from
theano.tensor
import
matrix
,
imatrix
,
lmatrix
,
fmatrix
,
dmatrix
from
theano.tensor
import
scalars
,
vectors
,
matrices
,
fmatrices
,
dmatrices
from
theano.tensor
import
scalars
,
vectors
,
matrices
,
fmatrices
,
dmatrices
from
theano.tensor
import
(
from
theano.tensor
import
(
AdvancedSubtensor1
,
as_tensor_variable
,
as_tensor_variable
,
inplace
,
inplace
,
Join
,
Join
,
...
@@ -1713,6 +1714,29 @@ def test_local_useless_subtensor():
...
@@ -1713,6 +1714,29 @@ def test_local_useless_subtensor():
f
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
1
)
f
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
1
)
f
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
3
)
f
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
3
)
# Test AdvancedSubtensor1 case when all rows are selected by a list/vector
# or ARange op
for
dims
,
res
in
(([
0
,
1
],
True
),
([
1
,
0
],
False
),
([
0
,
0
],
False
),
([
0
,
0
,
1
],
False
),
(
T
.
arange
(
2
),
True
),
(
T
.
arange
(
0
,
2
),
True
),
(
T
.
arange
(
0
,
2
,
2
),
False
),
(
T
.
arange
(
0
,
2
,
-
1
),
False
),
(
T
.
arange
(
1
,
2
),
False
)):
f
=
function
([
x
],
tensor
.
exp
(
x_c
)
.
__getitem__
(
dims
),
mode
=
mode_opt
)
#theano.printing.debugprint(f)
prog
=
f
.
maker
.
fgraph
.
toposort
()
if
res
:
assert
isinstance
(
prog
[
0
]
.
op
,
theano
.
tensor
.
SpecifyShape
),
dims
assert
prog
[
1
]
.
op
==
tensor
.
exp
,
dims
assert
len
(
prog
)
==
2
,
dims
else
:
assert
any
([
isinstance
(
node
.
op
,
AdvancedSubtensor1
)
for
node
in
prog
])
f
([[
0
,
1
,
2
],
[
3
,
4
,
5
]])
# let debugmode test something
class
test_local_subtensor_make_vector
(
unittest
.
TestCase
):
class
test_local_subtensor_make_vector
(
unittest
.
TestCase
):
def
test_scalar_idx
(
self
):
def
test_scalar_idx
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论