Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
fcbb3e9b
提交
fcbb3e9b
authored
3月 21, 2017
作者:
Frédéric Bastien
提交者:
GitHub
3月 21, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5665 from vikramnitin9/master
Optimization to remove upcast in local_cast_cast
上级
d704a600
e14c00a1
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
80 行增加
和
5 行删除
+80
-5
opt.py
theano/tensor/opt.py
+46
-2
test_opt.py
theano/tensor/tests/test_opt.py
+34
-3
没有找到文件。
theano/tensor/opt.py
浏览文件 @
fcbb3e9b
...
@@ -2204,7 +2204,7 @@ def local_cast_cast(node):
...
@@ -2204,7 +2204,7 @@ def local_cast_cast(node):
when those contrain:
when those contrain:
dtype1 == dtype2
dtype1 == dtype2
TODO:
the base dtype is the same (int, uint, float, complex)
OR
the base dtype is the same (int, uint, float, complex)
and the first cast cause an upcast.
and the first cast cause an upcast.
"""
"""
...
@@ -2216,10 +2216,54 @@ def local_cast_cast(node):
...
@@ -2216,10 +2216,54 @@ def local_cast_cast(node):
not
isinstance
(
x
.
owner
.
op
,
T
.
Elemwise
)
or
not
isinstance
(
x
.
owner
.
op
,
T
.
Elemwise
)
or
not
isinstance
(
x
.
owner
.
op
.
scalar_op
,
scalar
.
Cast
)):
not
isinstance
(
x
.
owner
.
op
.
scalar_op
,
scalar
.
Cast
)):
return
return
if
node
.
op
.
scalar_op
.
o_type
==
x
.
owner
.
op
.
scalar_op
.
o_type
:
type1
=
x
.
owner
.
op
.
scalar_op
.
o_type
type2
=
node
.
op
.
scalar_op
.
o_type
base
=
x
.
owner
.
inputs
[
0
]
if
type1
==
type2
:
# We don't need to copy over any stack traces here
# We don't need to copy over any stack traces here
return
[
x
]
return
[
x
]
if
(
is_an_upcast
(
base
.
dtype
,
type1
.
dtype
)):
# Checking for further redundancy. Eg: int8 -> int32 -> int8
if
(
type2
.
dtype
==
base
.
dtype
):
return
x
.
owner
.
inputs
else
:
# Apply the second cast only
v
=
node
.
op
(
base
)
# Copy stack trace from the output of the original cast
copy_stack_trace
(
node
.
outputs
[
0
],
v
)
return
[
v
]
def
is_an_upcast
(
type1
,
type2
):
"""Given two data types (as strings), check if converting to
type2 from type1 constitutes an upcast.
Differs from theano.scalar.upcast
"""
category
=
{
# The first number in the pair is the dtype (bool, uint, int, float,
# complex). Conversion from higher to lower is never an upcast.
# The second number roughly indicates the precision. Again, conversion
# from higher to lower is never an upcast.
'bool'
:
(
0
,
0
),
'uint8'
:
(
1
,
1
),
'uint16'
:
(
1
,
2
),
'uint32'
:
(
1
,
3
),
'uint64'
:
(
1
,
4
),
'int8'
:
(
2
,
1
),
'int16'
:
(
2
,
2
),
'int32'
:
(
2
,
3
),
'int64'
:
(
2
,
4
),
'float16'
:
(
3
,
1.5
),
'float32'
:
(
3
,
2.5
),
'float64'
:
(
3
,
3.5
),
'complex64'
:
(
4
,
3
),
'complex128'
:
(
4
,
4
)
}
cat1
=
category
[
type1
]
cat2
=
category
[
type2
]
if
(
cat2
[
0
]
>=
cat1
[
0
]
and
cat2
[
1
]
>
cat1
[
1
]):
return
True
else
:
return
False
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
fcbb3e9b
...
@@ -4656,7 +4656,7 @@ class T_cast_cast(unittest.TestCase):
...
@@ -4656,7 +4656,7 @@ class T_cast_cast(unittest.TestCase):
mode
=
theano
.
compile
.
get_default_mode
()
mode
=
theano
.
compile
.
get_default_mode
()
self
.
mode
=
mode
.
including
(
'local_cast_cast'
)
self
.
mode
=
mode
.
including
(
'local_cast_cast'
)
def
test
(
self
):
def
test
_consecutive
(
self
):
x
=
T
.
fmatrix
()
x
=
T
.
fmatrix
()
o
=
T
.
Elemwise
(
scal
.
Cast
(
scal
.
Scalar
(
"float64"
)))(
x
.
astype
(
"float64"
))
o
=
T
.
Elemwise
(
scal
.
Cast
(
scal
.
Scalar
(
"float64"
)))(
x
.
astype
(
"float64"
))
f
=
theano
.
function
([
x
],
o
,
mode
=
self
.
mode
)
f
=
theano
.
function
([
x
],
o
,
mode
=
self
.
mode
)
...
@@ -4664,7 +4664,7 @@ class T_cast_cast(unittest.TestCase):
...
@@ -4664,7 +4664,7 @@ class T_cast_cast(unittest.TestCase):
f
(
dx
)
f
(
dx
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
T
.
Elemwise
)
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scal
.
basic
.
Cast
)
x
=
T
.
dmatrix
()
x
=
T
.
dmatrix
()
o
=
T
.
Elemwise
(
scal
.
Cast
(
scal
.
Scalar
(
"float32"
)))(
x
.
astype
(
"float32"
))
o
=
T
.
Elemwise
(
scal
.
Cast
(
scal
.
Scalar
(
"float32"
)))(
x
.
astype
(
"float32"
))
...
@@ -4673,7 +4673,38 @@ class T_cast_cast(unittest.TestCase):
...
@@ -4673,7 +4673,38 @@ class T_cast_cast(unittest.TestCase):
f
(
dx
)
f
(
dx
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
T
.
Elemwise
)
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scal
.
basic
.
Cast
)
def
test_upcast
(
self
):
# Upcast followed by any other cast
x
=
T
.
fmatrix
()
o
=
T
.
Elemwise
(
scal
.
Cast
(
scal
.
Scalar
(
"complex128"
)))(
x
.
astype
(
"complex64"
))
f
=
theano
.
function
([
x
],
o
,
mode
=
self
.
mode
)
dx
=
numpy
.
random
.
rand
(
5
,
4
)
.
astype
(
"float32"
)
f
(
dx
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scal
.
basic
.
Cast
)
# Upcast followed by a downcast back to the base type
x
=
T
.
fmatrix
()
o
=
T
.
Elemwise
(
scal
.
Cast
(
scal
.
Scalar
(
"float32"
)))(
x
.
astype
(
"float64"
))
f
=
theano
.
function
([
x
],
o
,
mode
=
self
.
mode
)
dx
=
numpy
.
random
.
rand
(
5
,
4
)
.
astype
(
'float32'
)
f
(
dx
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
DeepCopyOp
)
# Downcast followed by an upcast back to the base type
# Optimization shouldn't be applied
x
=
T
.
dmatrix
()
o
=
T
.
Elemwise
(
scal
.
Cast
(
scal
.
Scalar
(
"float64"
)))(
x
.
astype
(
"float32"
))
f
=
theano
.
function
([
x
],
o
,
mode
=
self
.
mode
)
dx
=
numpy
.
random
.
rand
(
5
,
4
)
f
(
dx
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
(
len
(
topo
)
==
1
and
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scal
.
basic
.
Composite
))
or
(
len
(
topo
)
>
1
)
class
T_func_inverse
(
unittest
.
TestCase
):
class
T_func_inverse
(
unittest
.
TestCase
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论