Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a8b455fc
提交
a8b455fc
authored
4月 08, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
corrected env bug, activated constant folding and subgraph merging, added Op.desc and Result.desc
上级
c8c2c4f7
隐藏空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
37 行增加
和
27 行删除
+37
-27
base_tensor.py
base_tensor.py
+2
-2
compile.py
compile.py
+2
-2
elemwise2.py
elemwise2.py
+8
-5
_test_opt.py
gof/_test_opt.py
+1
-1
env.py
gof/env.py
+3
-1
ext.py
gof/ext.py
+3
-0
op.py
gof/op.py
+3
-0
opt.py
gof/opt.py
+12
-16
result.py
gof/result.py
+3
-0
没有找到文件。
base_tensor.py
浏览文件 @
a8b455fc
...
@@ -97,9 +97,9 @@ class BaseTensor(ResultBase):
...
@@ -97,9 +97,9 @@ class BaseTensor(ResultBase):
raise
TypeError
(
"Unsupported dtype for
%
s:
%
s"
%
(
self
.
__class__
.
__name__
,
self
.
dtype
))
raise
TypeError
(
"Unsupported dtype for
%
s:
%
s"
%
(
self
.
__class__
.
__name__
,
self
.
dtype
))
#
#
#
Hash
for constant folding
#
Description
for constant folding
#
#
def
hash
(
self
):
def
desc
(
self
):
if
self
.
data
is
not
None
:
if
self
.
data
is
not
None
:
return
(
BaseTensor
,
self
.
dtype
,
self
.
broadcastable
,
self
.
data
.
data
[:])
return
(
BaseTensor
,
self
.
dtype
,
self
.
broadcastable
,
self
.
data
.
data
[:])
else
:
else
:
...
...
compile.py
浏览文件 @
a8b455fc
...
@@ -16,8 +16,8 @@ exec_opt.optimizer = None
...
@@ -16,8 +16,8 @@ exec_opt.optimizer = None
def
default_optimizer
(
env
):
def
default_optimizer
(
env
):
#TODO: pass tests with these un-commented
#TODO: pass tests with these un-commented
#
default_optimizer.const(env)
default_optimizer
.
const
(
env
)
#
default_optimizer.merge(env)
default_optimizer
.
merge
(
env
)
pass
pass
default_optimizer
.
merge
=
gof
.
opt
.
MergeOptimizer
()
default_optimizer
.
merge
=
gof
.
opt
.
MergeOptimizer
()
default_optimizer
.
const
=
gof
.
opt
.
ConstantFinder
()
default_optimizer
.
const
=
gof
.
opt
.
ConstantFinder
()
...
...
elemwise2.py
浏览文件 @
a8b455fc
...
@@ -7,7 +7,7 @@ from base_tensor import BaseTensor as Tensor
...
@@ -7,7 +7,7 @@ from base_tensor import BaseTensor as Tensor
from
scalar
import
upcast
,
Scalar
from
scalar
import
upcast
,
Scalar
import
scalar_ops
import
scalar_ops
import
gof
import
gof
from
gof.python25
import
all
def
astensor
(
data
):
def
astensor
(
data
):
assert
isinstance
(
data
,
Tensor
)
assert
isinstance
(
data
,
Tensor
)
...
@@ -68,6 +68,9 @@ class DimShuffle(Op, Viewer):
...
@@ -68,6 +68,9 @@ class DimShuffle(Op, Viewer):
else
:
else
:
return
{}
return
{}
def
desc
(
self
):
return
(
self
.
__class__
,
tuple
(
self
.
new_order
))
def
perform
(
self
):
def
perform
(
self
):
res
=
self
.
inputs
[
0
]
.
data
res
=
self
.
inputs
[
0
]
.
data
shape
=
list
(
res
.
shape
)
shape
=
list
(
res
.
shape
)
...
@@ -153,8 +156,8 @@ class Broadcast(Op, Destroyer):
...
@@ -153,8 +156,8 @@ class Broadcast(Op, Destroyer):
def
clone_with_new_inputs
(
self
,
*
new_inputs
):
def
clone_with_new_inputs
(
self
,
*
new_inputs
):
return
Broadcast
(
self
.
scalar_opclass
,
new_inputs
,
self
.
inplace_pattern
)
return
Broadcast
(
self
.
scalar_opclass
,
new_inputs
,
self
.
inplace_pattern
)
def
id
(
self
):
def
desc
(
self
):
return
(
self
.
__class__
,
self
.
scalar_opclass
,
self
.
inplace_pattern
)
return
(
self
.
__class__
,
self
.
scalar_opclass
,
tuple
(
self
.
inplace_pattern
.
items
())
)
def
destroy_map
(
self
):
def
destroy_map
(
self
):
ret
=
{}
ret
=
{}
...
@@ -388,8 +391,8 @@ class CAReduce(Op):
...
@@ -388,8 +391,8 @@ class CAReduce(Op):
self
.
shadow
=
scalar_opclass
(
*
[
Scalar
(
dtype
=
inputs
[
0
]
.
dtype
)
for
i
in
xrange
(
scalar_opclass
.
nin
)])
self
.
shadow
=
scalar_opclass
(
*
[
Scalar
(
dtype
=
inputs
[
0
]
.
dtype
)
for
i
in
xrange
(
scalar_opclass
.
nin
)])
self
.
ufunc
=
numpy
.
frompyfunc
(
self
.
shadow
.
impl
,
scalar_opclass
.
nin
,
scalar_opclass
.
nout
)
self
.
ufunc
=
numpy
.
frompyfunc
(
self
.
shadow
.
impl
,
scalar_opclass
.
nin
,
scalar_opclass
.
nout
)
def
id
(
self
):
def
desc
(
self
):
return
(
self
.
__class__
,
self
.
scalar_opclass
,
self
.
dimensions_to_reduce
)
return
(
self
.
__class__
,
self
.
scalar_opclass
,
tuple
(
self
.
dimensions_to_reduce
)
)
def
clone_with_new_inputs
(
self
,
*
new_inputs
):
def
clone_with_new_inputs
(
self
,
*
new_inputs
):
return
CAReduce
(
self
.
scalar_opclass
,
new_inputs
,
self
.
dimensions_to_reduce
)
return
CAReduce
(
self
.
scalar_opclass
,
new_inputs
,
self
.
dimensions_to_reduce
)
...
...
gof/_test_opt.py
浏览文件 @
a8b455fc
...
@@ -21,7 +21,7 @@ class MyResult(ResultBase):
...
@@ -21,7 +21,7 @@ class MyResult(ResultBase):
def
__repr__
(
self
):
def
__repr__
(
self
):
return
self
.
name
return
self
.
name
def
hash
(
self
):
def
desc
(
self
):
return
self
.
data
return
self
.
data
...
...
gof/env.py
浏览文件 @
a8b455fc
...
@@ -400,7 +400,7 @@ class Env(graph.Graph):
...
@@ -400,7 +400,7 @@ class Env(graph.Graph):
# in new ops, so we use all results we know of as if they were the input set.
# in new ops, so we use all results we know of as if they were the input set.
# (the functions in the graph module only use the input set to
# (the functions in the graph module only use the input set to
# know where to stop going down)
# know where to stop going down)
new_ops
=
graph
.
io_toposort
(
self
.
results
(),
op
.
outputs
)
new_ops
=
graph
.
io_toposort
(
self
.
results
()
.
difference
(
self
.
orphans
())
,
op
.
outputs
)
for
op
in
new_ops
:
for
op
in
new_ops
:
...
@@ -443,6 +443,8 @@ class Env(graph.Graph):
...
@@ -443,6 +443,8 @@ class Env(graph.Graph):
# Cannot prune an op which is an output or used somewhere
# Cannot prune an op which is an output or used somewhere
if
self
.
clients
(
output
)
or
output
in
self
.
outputs
:
#output in self.outputs or self.clients(output):
if
self
.
clients
(
output
)
or
output
in
self
.
outputs
:
#output in self.outputs or self.clients(output):
return
return
if
op
not
in
self
.
_ops
:
# this can happen from replacing an orphan
return
self
.
_ops
.
remove
(
op
)
self
.
_ops
.
remove
(
op
)
self
.
_results
.
difference_update
(
op
.
outputs
)
self
.
_results
.
difference_update
(
op
.
outputs
)
for
listener
in
self
.
_listeners
.
values
():
for
listener
in
self
.
_listeners
.
values
():
...
...
gof/ext.py
浏览文件 @
a8b455fc
...
@@ -217,6 +217,9 @@ class DestroyHandler(Listener, Constraint, Orderings, Tool):
...
@@ -217,6 +217,9 @@ class DestroyHandler(Listener, Constraint, Orderings, Tool):
self
.
seen
.
add
(
op
)
self
.
seen
.
add
(
op
)
view_map
,
destroy_map
=
self
.
get_maps
(
op
)
view_map
,
destroy_map
=
self
.
get_maps
(
op
)
for
input
in
op
.
inputs
:
self
.
children
.
setdefault
(
input
,
set
())
for
i
,
output
in
enumerate
(
op
.
outputs
):
for
i
,
output
in
enumerate
(
op
.
outputs
):
views
=
view_map
.
get
(
output
,
None
)
views
=
view_map
.
get
(
output
,
None
)
destroyed
=
destroy_map
.
get
(
output
,
None
)
destroyed
=
destroy_map
.
get
(
output
,
None
)
...
...
gof/op.py
浏览文件 @
a8b455fc
...
@@ -73,6 +73,9 @@ class Op(object):
...
@@ -73,6 +73,9 @@ class Op(object):
self
.
_hash_id
=
utils
.
hashgen
()
self
.
_hash_id
=
utils
.
hashgen
()
return
self
.
_hash_id
return
self
.
_hash_id
def
desc
(
self
):
return
self
.
__class__
#
#
#
#
#
#
...
...
gof/opt.py
浏览文件 @
a8b455fc
...
@@ -310,7 +310,7 @@ class PatternOptimizer(OpSpecificOptimizer):
...
@@ -310,7 +310,7 @@ class PatternOptimizer(OpSpecificOptimizer):
and
getattr
(
pattern
,
'constant'
,
False
)
\
and
getattr
(
pattern
,
'constant'
,
False
)
\
and
isinstance
(
expr
,
ResultBase
)
\
and
isinstance
(
expr
,
ResultBase
)
\
and
getattr
(
expr
,
'constant'
,
False
)
\
and
getattr
(
expr
,
'constant'
,
False
)
\
and
pattern
.
hash
()
==
expr
.
hash
():
and
pattern
.
desc
()
==
expr
.
desc
():
return
u
return
u
else
:
else
:
return
False
return
False
...
@@ -371,7 +371,7 @@ class ConstantFinder(Optimizer):
...
@@ -371,7 +371,7 @@ class ConstantFinder(Optimizer):
for
r
in
env
.
inputs
:
for
r
in
env
.
inputs
:
r
.
indestructible
=
True
r
.
indestructible
=
True
import
graph
class
MergeOptimizer
(
Optimizer
):
class
MergeOptimizer
(
Optimizer
):
"""
"""
Merges parts of the graph that are identical, i.e. parts that
Merges parts of the graph that are identical, i.e. parts that
...
@@ -381,11 +381,11 @@ class MergeOptimizer(Optimizer):
...
@@ -381,11 +381,11 @@ class MergeOptimizer(Optimizer):
"""
"""
def
apply
(
self
,
env
):
def
apply
(
self
,
env
):
cid
=
{}
#result -> result.
hash
() (for constants)
cid
=
{}
#result -> result.
desc
() (for constants)
inv_cid
=
{}
#
hash
-> result (for constants)
inv_cid
=
{}
#
desc
-> result (for constants)
for
i
,
r
in
enumerate
(
env
.
orphans
()
.
union
(
env
.
inputs
)):
for
i
,
r
in
enumerate
(
env
.
orphans
()
.
union
(
env
.
inputs
)):
if
getattr
(
r
,
'constant'
,
False
)
and
hasattr
(
r
,
'hash'
)
:
if
getattr
(
r
,
'constant'
,
False
):
ref
=
(
'const'
,
r
.
hash
())
ref
=
(
'const'
,
r
.
desc
())
other_r
=
inv_cid
.
get
(
ref
,
None
)
other_r
=
inv_cid
.
get
(
ref
,
None
)
if
other_r
is
not
None
:
if
other_r
is
not
None
:
env
.
replace
(
r
,
other_r
)
env
.
replace
(
r
,
other_r
)
...
@@ -397,20 +397,16 @@ class MergeOptimizer(Optimizer):
...
@@ -397,20 +397,16 @@ class MergeOptimizer(Optimizer):
inv_cid
[
i
]
=
r
inv_cid
[
i
]
=
r
for
op
in
env
.
io_toposort
():
for
op
in
env
.
io_toposort
():
# this could be made more robust by having an op.hash() that
op_cid
=
(
op
.
desc
(),
tuple
([
cid
[
input
]
for
input
in
op
.
inputs
]))
# doesn't depend on the inputs but can depend on additional properties
# of the op.
op_cid
=
(
op
.
__class__
,
tuple
([
cid
[
input
]
for
input
in
op
.
inputs
]))
dup
=
inv_cid
.
get
(
op_cid
,
None
)
dup
=
inv_cid
.
get
(
op_cid
,
None
)
success
=
False
success
=
False
if
dup
is
not
None
:
if
dup
is
not
None
:
success
=
True
success
=
True
for
output
,
other_output
in
zip
(
op
.
outputs
,
dup
.
outputs
):
d
=
dict
(
zip
(
op
.
outputs
,
dup
.
outputs
))
try
:
try
:
env
.
replace
(
output
,
other_output
)
env
.
replace_all
(
d
)
except
:
except
Exception
,
e
:
success
=
False
success
=
False
break
if
not
success
:
if
not
success
:
cid
[
op
]
=
op_cid
cid
[
op
]
=
op_cid
inv_cid
[
op_cid
]
=
op
inv_cid
[
op_cid
]
=
op
...
...
gof/result.py
浏览文件 @
a8b455fc
...
@@ -77,6 +77,9 @@ class ResultBase(object):
...
@@ -77,6 +77,9 @@ class ResultBase(object):
def
__hash__
(
self
):
def
__hash__
(
self
):
return
self
.
_hash_id
return
self
.
_hash_id
def
desc
(
self
):
return
id
(
self
)
#
#
# role
# role
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论