Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3b18fef4
提交
3b18fef4
authored
6月 21, 2010
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added an optimization phase: specialize_device and put the new POW optimization into that phase.
上级
b91efd13
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
45 行增加
和
11 行删除
+45
-11
mode.py
theano/compile/mode.py
+2
-0
opt.py
theano/tensor/opt.py
+29
-9
test_opt.py
theano/tensor/tests/test_opt.py
+14
-2
没有找到文件。
theano/compile/mode.py
浏览文件 @
3b18fef4
...
@@ -137,6 +137,8 @@ optdb.register('stabilize', gof.EquilibriumDB(), # replace unstable s
...
@@ -137,6 +137,8 @@ optdb.register('stabilize', gof.EquilibriumDB(), # replace unstable s
1.5
,
'fast_run'
)
1.5
,
'fast_run'
)
optdb
.
register
(
'specialize'
,
gof
.
EquilibriumDB
(),
# misc special cases for speed
optdb
.
register
(
'specialize'
,
gof
.
EquilibriumDB
(),
# misc special cases for speed
2
,
'fast_run'
)
2
,
'fast_run'
)
optdb
.
register
(
'specialize_device'
,
gof
.
EquilibriumDB
(),
# misc special cases for speed that are dependent on the device.
48.6
,
'fast_run'
)
#must be after gpu stuff at 48.5
optdb
.
register
(
'merge2'
,
gof
.
MergeOptimizer
(),
# especially constant merge
optdb
.
register
(
'merge2'
,
gof
.
MergeOptimizer
(),
# especially constant merge
49
,
'fast_run'
)
49
,
'fast_run'
)
optdb
.
register
(
'add_destroy_handler'
,
AddDestroyHandler
(),
optdb
.
register
(
'add_destroy_handler'
,
AddDestroyHandler
(),
...
...
theano/tensor/opt.py
浏览文件 @
3b18fef4
...
@@ -144,6 +144,11 @@ def register_specialize(lopt, *tags, **kwargs):
...
@@ -144,6 +144,11 @@ def register_specialize(lopt, *tags, **kwargs):
compile
.
optdb
[
'specialize'
]
.
register
(
name
,
lopt
,
'fast_run'
,
*
tags
)
compile
.
optdb
[
'specialize'
]
.
register
(
name
,
lopt
,
'fast_run'
,
*
tags
)
return
lopt
return
lopt
def
register_specialize_device
(
lopt
,
*
tags
,
**
kwargs
):
name
=
(
kwargs
and
kwargs
.
pop
(
'name'
))
or
lopt
.
__name__
compile
.
optdb
[
'specialize_device'
]
.
register
(
name
,
lopt
,
'fast_run'
,
*
tags
)
return
lopt
def
register_stabilize
(
lopt
,
*
tags
,
**
kwargs
):
def
register_stabilize
(
lopt
,
*
tags
,
**
kwargs
):
name
=
(
kwargs
and
kwargs
.
pop
(
'name'
))
or
lopt
.
__name__
name
=
(
kwargs
and
kwargs
.
pop
(
'name'
))
or
lopt
.
__name__
compile
.
optdb
[
'stabilize'
]
.
register
(
name
,
lopt
,
'fast_run'
,
*
tags
)
compile
.
optdb
[
'stabilize'
]
.
register
(
name
,
lopt
,
'fast_run'
,
*
tags
)
...
@@ -1829,9 +1834,31 @@ def local_pow_specialize(node):
...
@@ -1829,9 +1834,31 @@ def local_pow_specialize(node):
rval
=
[
T
.
inv
(
xsym
)]
rval
=
[
T
.
inv
(
xsym
)]
if
N
.
all
(
y
==
-
2
):
if
N
.
all
(
y
==
-
2
):
rval
=
[
T
.
inv
(
T
.
sqr
(
xsym
))]
rval
=
[
T
.
inv
(
T
.
sqr
(
xsym
))]
if
rval
:
rval
[
0
]
=
T
.
cast
(
rval
[
0
],
odtype
)
assert
rval
[
0
]
.
type
==
node
.
outputs
[
0
]
.
type
,
(
rval
,
node
.
outputs
)
return
rval
else
:
return
False
register_specialize
(
local_pow_specialize
)
# Optimize all integral powers in [-RANGE, RANGE]
@register_specialize_device
if
config
.
experimental
.
pow
and
rval
is
None
and
abs
(
y
)
==
int
(
abs
(
y
))
and
abs
(
y
)
<=
512
:
# 512 is too small for the cpu and too big for some gpu!
@gof.local_optimizer
([
T
.
pow
])
def
local_pow_specialize_device
(
node
):
"""
This optimization is not the same on all device. We do it only on cpu here.
"""
if
node
.
op
==
T
.
pow
:
#the idea here is that we have pow(x, y)
odtype
=
node
.
outputs
[
0
]
.
dtype
xsym
=
node
.
inputs
[
0
]
ysym
=
node
.
inputs
[
1
]
y
=
local_mul_canonizer
.
get_constant
(
ysym
)
if
(
y
is
not
None
)
\
and
encompasses_broadcastable
(
xsym
.
type
.
broadcastable
,
ysym
.
type
.
broadcastable
):
rval
=
None
# 512 is too small for the cpu and too big for some gpu!
if
abs
(
y
)
==
int
(
abs
(
y
))
and
abs
(
y
)
<=
512
:
pow2
=
[
xsym
]
pow2
=
[
xsym
]
pow2_scal
=
[
theano
.
scalar
.
Scalar
(
xsym
.
dtype
)()]
pow2_scal
=
[
theano
.
scalar
.
Scalar
(
xsym
.
dtype
)()]
y_to_do
=
abs
(
y
)
y_to_do
=
abs
(
y
)
...
@@ -1861,13 +1888,6 @@ def local_pow_specialize(node):
...
@@ -1861,13 +1888,6 @@ def local_pow_specialize(node):
rval
[
0
]
=
T
.
cast
(
rval
[
0
],
odtype
)
rval
[
0
]
=
T
.
cast
(
rval
[
0
],
odtype
)
assert
rval
[
0
]
.
type
==
node
.
outputs
[
0
]
.
type
,
(
rval
,
node
.
outputs
)
assert
rval
[
0
]
.
type
==
node
.
outputs
[
0
]
.
type
,
(
rval
,
node
.
outputs
)
return
rval
return
rval
else
:
return
False
register_specialize
(
local_pow_specialize
)
theano
.
configparser
.
AddConfigVar
(
'experimental.pow'
,
"Transform a pow to a constant integer to a graph of mul. Fast on cpu, but more work needed for gpu."
,
theano
.
configparser
.
BoolParam
(
False
),
)
@gof.local_optimizer
([
T
.
mul
])
@gof.local_optimizer
([
T
.
mul
])
def
local_mul_specialize
(
node
):
def
local_mul_specialize
(
node
):
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
3b18fef4
...
@@ -1259,6 +1259,7 @@ def test_local_pow_specialize():
...
@@ -1259,6 +1259,7 @@ def test_local_pow_specialize():
v
=
T
.
vector
()
v
=
T
.
vector
()
val
=
numpy
.
arange
(
10
,
dtype
=
theano
.
config
.
floatX
)
val
=
numpy
.
arange
(
10
,
dtype
=
theano
.
config
.
floatX
)
val_no0
=
numpy
.
arange
(
1
,
10
,
dtype
=
theano
.
config
.
floatX
)
val_no0
=
numpy
.
arange
(
1
,
10
,
dtype
=
theano
.
config
.
floatX
)
f
=
function
([
v
],
v
**
0
,
mode
=
mode
)
f
=
function
([
v
],
v
**
0
,
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
assert
nodes
==
[
Shape_i
(
0
),
T
.
alloc
]
assert
nodes
==
[
Shape_i
(
0
),
T
.
alloc
]
...
@@ -1300,8 +1301,19 @@ def test_local_pow_specialize():
...
@@ -1300,8 +1301,19 @@ def test_local_pow_specialize():
# assert nodes == [T.sqrt,T.inv]#Why this don't work?
# assert nodes == [T.sqrt,T.inv]#Why this don't work?
assert
numpy
.
allclose
(
f
(
val_no0
),
val_no0
**
(
-.
5
))
assert
numpy
.
allclose
(
f
(
val_no0
),
val_no0
**
(
-.
5
))
if
config
.
experimental
.
pow
:
def
test_local_pow_specialize_device
():
print
"Test experimental.pow=True"
# test that on cpu we use more agressive optimization
mode
=
theano
.
config
.
mode
if
mode
==
'FAST_COMPILE'
:
mode
=
'FAST_RUN'
mode
=
compile
.
mode
.
get_mode
(
mode
)
mode
=
mode
.
excluding
(
'fusion'
)
.
excluding
(
'gpu'
)
v
=
T
.
vector
()
val
=
numpy
.
arange
(
10
,
dtype
=
theano
.
config
.
floatX
)
val_no0
=
numpy
.
arange
(
1
,
10
,
dtype
=
theano
.
config
.
floatX
)
f
=
function
([
v
],
v
**
(
15
),
mode
=
mode
)
f
=
function
([
v
],
v
**
(
15
),
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
assert
len
(
nodes
)
==
1
assert
len
(
nodes
)
==
1
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论