Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
df1bd762
提交
df1bd762
authored
11月 03, 2014
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2216 from sisp/adv_subtensor_opt
Generalize all [Inc]Subtensor optimization to advanced[inc]subtensor[1]
上级
d47169e0
b5253240
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
183 行增加
和
46 行删除
+183
-46
opt.py
theano/tensor/opt.py
+70
-45
test_opt.py
theano/tensor/tests/test_opt.py
+113
-1
没有找到文件。
theano/tensor/opt.py
浏览文件 @
df1bd762
...
@@ -28,7 +28,8 @@ from theano.tensor.elemwise import Elemwise, DimShuffle
...
@@ -28,7 +28,8 @@ from theano.tensor.elemwise import Elemwise, DimShuffle
from
theano.tensor.subtensor
import
(
get_idx_list
,
get_canonical_form_slice
,
from
theano.tensor.subtensor
import
(
get_idx_list
,
get_canonical_form_slice
,
Subtensor
,
IncSubtensor
,
make_constant
,
Subtensor
,
IncSubtensor
,
make_constant
,
AdvancedIncSubtensor1
,
AdvancedIncSubtensor1
,
AdvancedIncSubtensor
)
AdvancedIncSubtensor
,
AdvancedSubtensor1
)
from
theano
import
scalar
from
theano
import
scalar
from
theano.tensor
import
basic
as
T
from
theano.tensor
import
basic
as
T
from
theano
import
compile
# to register the optimizer built by this file
from
theano
import
compile
# to register the optimizer built by this file
...
@@ -1330,52 +1331,71 @@ def local_track_shape_i(node):
...
@@ -1330,52 +1331,71 @@ def local_track_shape_i(node):
@register_specialize
@register_specialize
@register_canonicalize
(
'fast_compile_gpu'
)
@register_canonicalize
(
'fast_compile_gpu'
)
@gof.local_optimizer
([
Subtensor
])
@gof.local_optimizer
([
Subtensor
,
AdvancedSubtensor1
])
def
local_subtensor_make_vector
(
node
):
def
local_subtensor_make_vector
(
node
):
# replace all subtensor(make_vector) like:
"""
# [a,b,c][0] -> a
replace all subtensor(make_vector) like:
# [a,b,c][0:2] -> [a,b]
[a,b,c][0] -> a
# we can do this for constant indexes
[a,b,c][0:2] -> [a,b]
replace all AdvancedSubtensor1(make_vector) like:
[a,b,c][[0,2]] -> [a,c]
we can do this for constant indexes
"""
x
=
node
.
inputs
[
0
]
if
not
x
.
owner
or
x
.
owner
.
op
!=
make_vector
:
return
if
isinstance
(
node
.
op
,
Subtensor
):
if
isinstance
(
node
.
op
,
Subtensor
):
# This optimization needs ShapeOpt and fgraph.shape_feature
# This optimization needs ShapeOpt and fgraph.shape_feature
x
=
node
.
inputs
[
0
]
try
:
if
x
.
owner
and
x
.
owner
.
op
==
make_vector
:
idx
,
=
node
.
op
.
idx_list
except
Exception
:
#'how can you have multiple indexes into a shape?'
raise
if
isinstance
(
idx
,
(
scalar
.
Scalar
,
T
.
TensorType
)):
# The idx is a Scalar, ie a Type. This means the actual index
# is contained in node.inputs[1]
old_idx
,
idx
=
idx
,
node
.
inputs
[
1
]
assert
idx
.
type
==
old_idx
elif
isinstance
(
node
.
op
,
AdvancedSubtensor1
):
idx
=
node
.
inputs
[
1
]
else
:
return
if
isinstance
(
idx
,
(
int
,
numpy
.
integer
)):
return
[
x
.
owner
.
inputs
[
idx
]]
elif
isinstance
(
idx
,
Variable
):
if
idx
.
ndim
==
0
:
# if it is a constant we can do something with it
try
:
try
:
idx
,
=
node
.
op
.
idx_list
v
=
get_scalar_constant_value
(
idx
)
except
Exception
:
if
isinstance
(
v
,
numpy
.
integer
):
#'how can you have multiple indexes into a shape?'
# Python 2.4 wants to index only with Python integers
raise
v
=
int
(
v
)
return
[
x
.
owner
.
inputs
[
v
]]
if
isinstance
(
idx
,
(
scalar
.
Scalar
,
T
.
TensorType
)):
except
NotScalarConstantError
:
# The idx is a Scalar, ie a Type. This means the actual index
pass
# is contained in node.inputs[1]
elif
idx
.
ndim
==
1
and
isinstance
(
idx
,
T
.
Constant
):
old_idx
,
idx
=
idx
,
node
.
inputs
[
1
]
values
=
map
(
int
,
list
(
idx
.
value
))
assert
idx
.
type
==
old_idx
return
[
make_vector
(
*
[
x
.
owner
.
inputs
[
v
]
for
v
in
values
])]
else
:
if
isinstance
(
idx
,
(
int
,
numpy
.
integer
)):
raise
TypeError
(
'case not expected'
)
return
[
x
.
owner
.
inputs
[
idx
]]
elif
isinstance
(
idx
,
slice
):
elif
isinstance
(
idx
,
Variable
):
# it is a slice of ints and/or Variables
# if it is a constant we can do something with it
# check subtensor to see if it can contain constant variables, and if
try
:
# it can, then try to unpack them.
v
=
get_scalar_constant_value
(
idx
)
try
:
if
isinstance
(
v
,
numpy
.
integer
):
const_slice
=
node
.
op
.
get_constant_idx
(
node
.
inputs
,
# Python 2.4 wants to index only with Python integers
allow_partial
=
False
)[
0
]
v
=
int
(
v
)
return
[
make_vector
(
*
x
.
owner
.
inputs
[
const_slice
])]
return
[
x
.
owner
.
inputs
[
v
]]
except
NotScalarConstantError
:
except
NotScalarConstantError
:
pass
pass
else
:
else
:
raise
TypeError
(
'case not expected'
)
# it is a slice of ints and/or Variables
#TODO: check subtensor to see if it can contain
# constant variables, and if it can, then try to
# unpack them.
try
:
return
[
make_vector
(
*
x
.
owner
.
inputs
.
__getitem__
(
idx
))]
except
TypeError
:
pass
except
Exception
:
_logger
.
error
(
'failed to index with "
%
s"'
%
str
(
idx
))
raise
#TODO: the other optimization for and, or, xor, le and ge see ticket #496.
#TODO: the other optimization for and, or, xor, le and ge see ticket #496.
...
@@ -2373,12 +2393,17 @@ compile.optdb.register('local_inplace_incsubtensor1',
...
@@ -2373,12 +2393,17 @@ compile.optdb.register('local_inplace_incsubtensor1',
# Register old name
# Register old name
@register_canonicalize
(
"local_incsubtensor_of_allocs"
)
@register_canonicalize
(
"local_incsubtensor_of_allocs"
)
@register_stabilize
(
"local_incsubtensor_of_allocs"
)
@register_stabilize
(
"local_incsubtensor_of_allocs"
)
@gof.local_optimizer
([
IncSubtensor
])
@gof.local_optimizer
([
IncSubtensor
,
AdvancedIncSubtensor
,
AdvancedIncSubtensor1
])
def
local_incsubtensor_of_zeros
(
node
):
def
local_incsubtensor_of_zeros
(
node
):
"""
"""
IncSubtensor(x, zeros, idx) -> x
IncSubtensor(x, zeros, idx) -> x
"""
"""
if
isinstance
(
node
.
op
,
IncSubtensor
)
and
not
node
.
op
.
set_instead_of_inc
:
if
(
isinstance
(
node
.
op
,
(
IncSubtensor
,
AdvancedIncSubtensor
,
AdvancedIncSubtensor1
))
and
not
node
.
op
.
set_instead_of_inc
):
x
=
node
.
inputs
[
0
]
x
=
node
.
inputs
[
0
]
y
=
node
.
inputs
[
1
]
y
=
node
.
inputs
[
1
]
replace
=
False
replace
=
False
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
df1bd762
...
@@ -35,7 +35,8 @@ from theano.tensor.opt import (
...
@@ -35,7 +35,8 @@ from theano.tensor.opt import (
out2in
,
out2in
,
Shape_i
,
Shape_i
,
Assert
,
Assert
,
MakeVector
MakeVector
,
make_vector
)
)
from
theano
import
tensor
from
theano
import
tensor
from
theano
import
tensor
as
T
from
theano
import
tensor
as
T
...
@@ -1712,6 +1713,54 @@ def test_local_useless_subtensor():
...
@@ -1712,6 +1713,54 @@ def test_local_useless_subtensor():
f
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
3
)
f
([[
1
,
2
,
3
],
[
4
,
5
,
6
]],
3
)
class
test_local_subtensor_make_vector
(
unittest
.
TestCase
):
def
test_scalar_idx
(
self
):
x
,
y
,
z
=
tensor
.
lscalars
(
'xyz'
)
v
=
make_vector
(
x
,
y
,
z
)
f
=
function
([
x
,
y
,
z
],
v
[
0
],
mode
=
mode_opt
)
prog
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
prog
)
==
1
assert
isinstance
(
prog
[
0
]
.
op
,
theano
.
compile
.
ops
.
DeepCopyOp
)
assert
f
(
0
,
1
,
2
)
==
0
def
test_slice_idx_stop
(
self
):
x
,
y
,
z
=
tensor
.
lscalars
(
'xyz'
)
v
=
make_vector
(
x
,
y
,
z
)
f
=
function
([
x
,
y
,
z
],
v
[:
2
],
mode
=
mode_opt
)
prog
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
prog
)
==
1
assert
isinstance
(
prog
[
0
]
.
op
,
MakeVector
)
assert
len
(
prog
[
0
]
.
inputs
)
==
2
r
=
f
(
0
,
1
,
2
)
assert
r
[
0
]
==
0
and
r
[
1
]
==
1
def
test_slice_idx_step
(
self
):
x
,
y
,
z
=
tensor
.
lscalars
(
'xyz'
)
v
=
make_vector
(
x
,
y
,
z
)
f
=
function
([
x
,
y
,
z
],
v
[::
2
],
mode
=
mode_opt
)
prog
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
prog
)
==
1
assert
isinstance
(
prog
[
0
]
.
op
,
MakeVector
)
assert
len
(
prog
[
0
]
.
inputs
)
==
2
r
=
f
(
0
,
1
,
2
)
assert
r
[
0
]
==
0
and
r
[
1
]
==
2
def
test_AdvancedSubtensor1_idx
(
self
):
x
,
y
,
z
=
tensor
.
lscalars
(
'xyz'
)
v
=
make_vector
(
x
,
y
,
z
)
f
=
function
([
x
,
y
,
z
],
v
[[
0
,
2
]],
mode
=
mode_opt
)
prog
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
prog
)
==
1
assert
isinstance
(
prog
[
0
]
.
op
,
MakeVector
)
assert
len
(
prog
[
0
]
.
inputs
)
==
2
r
=
f
(
0
,
1
,
2
)
assert
r
[
0
]
==
0
and
r
[
1
]
==
2
class
test_local_subtensor_lift
(
unittest
.
TestCase
):
class
test_local_subtensor_lift
(
unittest
.
TestCase
):
def
test0
(
self
):
def
test0
(
self
):
# basic test that the Op works
# basic test that the Op works
...
@@ -2442,6 +2491,69 @@ class Test_alloc_zero(unittest.TestCase):
...
@@ -2442,6 +2491,69 @@ class Test_alloc_zero(unittest.TestCase):
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
IncSubtensor
)
for
x
in
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
IncSubtensor
)
for
x
in
f
.
maker
.
fgraph
.
toposort
()])
f
.
maker
.
fgraph
.
toposort
()])
def
test_advancedincsubtensor1_allocs0
(
self
):
x
=
tensor
.
matrix
()
y
=
tensor
.
matrix
()
y0
=
tensor
.
zeros_like
(
y
)
z
=
tensor
.
inc_subtensor
(
x
[[
0
,
1
,
2
,
3
]],
y0
)
f
=
theano
.
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
AdvancedIncSubtensor1
)
for
x
in
f
.
maker
.
fgraph
.
toposort
()])
def
test_advancedincsubtensor1_allocs0t
(
self
):
x
=
tensor
.
matrix
()
y
=
tensor
.
matrix
()
y0
=
tensor
.
zeros_like
(
y
)
z
=
tensor
.
inc_subtensor
(
x
[[
0
,
1
,
2
,
3
]],
y0
.
T
)
f
=
theano
.
function
([
x
,
y
],
z
,
mode
=
mode_opt
)
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
AdvancedIncSubtensor1
)
for
x
in
f
.
maker
.
fgraph
.
toposort
()])
def
test_advancedincsubtensor1_allocs1
(
self
):
x
=
tensor
.
matrix
()
y0
=
tensor
.
constant
(
numpy
.
asarray
(
numpy
.
zeros_like
((
4
,
4
)),
dtype
=
config
.
floatX
))
z
=
tensor
.
inc_subtensor
(
x
[[
0
,
1
,
2
,
3
]],
y0
)
f
=
theano
.
function
([
x
],
z
,
mode
=
self
.
mode
)
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
AdvancedIncSubtensor1
)
for
x
in
f
.
maker
.
fgraph
.
toposort
()])
def
test_advancedincsubtensor_allocs0
(
self
):
if
tensor
.
inplace_increment
is
None
:
raise
SkipTest
(
'NumPy version >= 1.8 not available'
)
x
=
tensor
.
matrix
()
y
=
tensor
.
matrix
()
y0
=
tensor
.
zeros_like
(
y
)
z
=
tensor
.
inc_subtensor
(
x
[[[
0
,
0
],
[
1
,
1
]],
[[
0
,
1
],
[
0
,
1
]]],
y0
)
f
=
theano
.
function
([
x
,
y
],
z
,
mode
=
self
.
mode
)
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
AdvancedIncSubtensor
)
for
x
in
f
.
maker
.
fgraph
.
toposort
()])
def
test_advancedincsubtensor_allocs0t
(
self
):
if
tensor
.
inplace_increment
is
None
:
raise
SkipTest
(
'NumPy version >= 1.8 not available'
)
x
=
tensor
.
matrix
()
y
=
tensor
.
matrix
()
y0
=
tensor
.
zeros_like
(
y
)
z
=
tensor
.
inc_subtensor
(
x
[[[
0
,
0
],
[
1
,
1
]],
[[
0
,
1
],
[
0
,
1
]]],
y0
.
T
)
f
=
theano
.
function
([
x
,
y
],
z
,
mode
=
mode_opt
)
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
AdvancedIncSubtensor
)
for
x
in
f
.
maker
.
fgraph
.
toposort
()])
def
test_advancedincsubtensor_allocs1
(
self
):
if
tensor
.
inplace_increment
is
None
:
raise
SkipTest
(
'NumPy version >= 1.8 not available'
)
x
=
tensor
.
matrix
()
y0
=
tensor
.
constant
(
numpy
.
asarray
(
numpy
.
zeros_like
((
2
,
2
)),
dtype
=
config
.
floatX
))
z
=
tensor
.
inc_subtensor
(
x
[[[
0
,
0
],
[
1
,
1
]],
[[
0
,
1
],
[
0
,
1
]]],
y0
)
f
=
theano
.
function
([
x
],
z
,
mode
=
self
.
mode
)
assert
numpy
.
all
([
not
isinstance
(
x
.
op
,
tensor
.
AdvancedIncSubtensor
)
for
x
in
f
.
maker
.
fgraph
.
toposort
()])
def
test_dot_allocs_0
(
self
):
def
test_dot_allocs_0
(
self
):
v1
=
tensor
.
vector
(
'v1'
)
v1
=
tensor
.
vector
(
'v1'
)
v2
=
tensor
.
vector
(
'v2'
)
v2
=
tensor
.
vector
(
'v2'
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论