Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f0f898b2
提交
f0f898b2
authored
6月 20, 2012
作者:
Nicolas Bouchard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix gh-363 Move optimization from basic.py to opt.py.
上级
822a6648
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
95 行增加
和
90 行删除
+95
-90
basic.py
theano/sparse/basic.py
+2
-76
opt.py
theano/sparse/opt.py
+93
-14
没有找到文件。
theano/sparse/basic.py
浏览文件 @
f0f898b2
...
...
@@ -912,17 +912,11 @@ class CSMGradC(gof.Op):
return
(
3
,)
csm_grad_c
=
CSMGradC
()
@gof.local_optimizer
([
csm_grad
(
None
)])
def
local_csm_grad_c
(
node
):
""" csm_grad(None) -> csm_grad_c """
if
node
.
op
==
csm_grad
(
None
):
return
[
csm_grad_c
(
*
node
.
inputs
)]
return
False
register_specialize
(
local_csm_grad_c
)
#
# Conversion
#
class
DenseFromSparse
(
gof
.
op
.
Op
):
"""
Convert a sparse matrix to an `ndarray`.
...
...
@@ -1960,28 +1954,6 @@ class StructuredDotCSR(gof.Op):
sd_csr
=
StructuredDotCSR
()
# register a specialization to replace StructuredDot -> StructuredDotCSx
@gof.local_optimizer
([
_structured_dot
])
def
local_structured_dot
(
node
):
if
node
.
op
==
_structured_dot
:
a
,
b
=
node
.
inputs
if
a
.
type
.
format
==
'csc'
:
a_val
,
a_ind
,
a_ptr
,
a_shape
=
csm_properties
(
a
)
a_nsparse
=
a_shape
[
0
]
return
[
sd_csc
(
a_val
,
a_ind
,
a_ptr
,
a_nsparse
,
b
)]
if
a
.
type
.
format
==
'csr'
:
a_val
,
a_ind
,
a_ptr
,
a_shape
=
csm_properties
(
a
)
return
[
sd_csr
(
a_val
,
a_ind
,
a_ptr
,
b
)]
return
False
# Commented out because
# a) it is only slightly faster than scipy these days, and sometimes a little
# slower, and
# b) the resulting graphs make it very difficult for an op to do size checking
# on the matrices involved. dimension mismatches are hard to detect sensibly.
#register_specialize(local_structured_dot)
def
structured_dot_grad
(
sparse_A
,
dense_B
,
ga
):
if
sparse_A
.
type
.
format
in
(
'csc'
,
'csr'
):
...
...
@@ -2648,49 +2620,3 @@ class UsmmCscDense(gof.Op):
usmm_csc_dense
=
UsmmCscDense
(
inplace
=
False
)
usmm_csc_dense_inplace
=
UsmmCscDense
(
inplace
=
True
)
local_usmm
=
gof
.
opt
.
PatternSub
(
(
tensor
.
sub
,
'z'
,
(
tensor
.
mul
,
{
'pattern'
:
'alpha'
,
'constraint'
:
lambda
expr
:
numpy
.
all
(
expr
.
type
.
broadcastable
)},
(
_dot
,
'x'
,
'y'
))),
(
usmm
,
(
tensor
.
neg
,
'alpha'
),
'x'
,
'y'
,
'z'
))
register_specialize
(
local_usmm
,
name
=
"local_usmm"
)
@gof.local_optimizer
([
usmm
])
def
local_usmm_csx
(
node
):
""" usmm -> usmm_csc_dense """
if
node
.
op
==
usmm
:
alpha
,
x
,
y
,
z
=
node
.
inputs
x_is_sparse_variable
=
_is_sparse_variable
(
x
)
y_is_sparse_variable
=
_is_sparse_variable
(
y
)
if
x_is_sparse_variable
and
not
y_is_sparse_variable
:
if
x
.
type
.
format
==
'csc'
:
x_val
,
x_ind
,
x_ptr
,
x_shape
=
csm_properties
(
x
)
x_nsparse
=
x_shape
[
0
]
dtype_out
=
scalar
.
upcast
(
alpha
.
type
.
dtype
,
x
.
type
.
dtype
,
y
.
type
.
dtype
,
z
.
type
.
dtype
)
if
dtype_out
not
in
(
'float32'
,
'float64'
):
return
False
# Sparse cast is not implemented.
if
y
.
type
.
dtype
!=
dtype_out
:
return
False
return
[
usmm_csc_dense
(
alpha
,
x_val
,
x_ind
,
x_ptr
,
x_nsparse
,
y
,
z
)]
return
False
register_specialize
(
local_usmm_csx
)
@gof.local_optimizer
([
usmm_csc_dense
])
def
local_usmm_csc_dense_inplace
(
node
):
if
node
.
op
==
usmm_csc_dense
:
return
[
usmm_csc_dense_inplace
(
*
node
.
inputs
)]
register_specialize
(
local_usmm_csc_dense_inplace
,
'inplace'
)
theano/sparse/opt.py
浏览文件 @
f0f898b2
...
...
@@ -3,23 +3,30 @@ from itertools import izip
import
theano
from
theano
import
gof
from
theano.sparse
import
(
CSC
,
CSR
,
csm_properties
,
Remove0
,
register_specialize
)
register_specialize
,
csm_grad
,
csm_grad_c
,
usmm_csc_dense
,
usmm
)
from
basic
import
(
_structured_dot
,
_dot
)
@gof.local_optimizer
([
None
])
def
local_inplace_remove0
(
node
):
"""
Optimization to insert inplace versions of Remove0.
"""
if
isinstance
(
node
.
op
,
Remove0
)
and
not
node
.
op
.
inplace
:
new_op
=
node
.
op
.
__class__
(
inplace
=
True
)
new_node
=
new_op
(
*
node
.
inputs
)
return
[
new_node
]
local_usmm
=
gof
.
opt
.
PatternSub
(
(
theano
.
tensor
.
sub
,
'z'
,
(
theano
.
tensor
.
mul
,
{
'pattern'
:
'alpha'
,
'constraint'
:
lambda
expr
:
numpy
.
all
(
expr
.
type
.
broadcastable
)},
(
_dot
,
'x'
,
'y'
))),
(
usmm
,
(
theano
.
tensor
.
neg
,
'alpha'
),
'x'
,
'y'
,
'z'
))
register_specialize
(
local_usmm
,
name
=
"local_usmm"
)
@gof.local_optimizer
([
csm_grad
(
None
)])
def
local_csm_grad_c
(
node
):
""" csm_grad(None) -> csm_grad_c """
if
node
.
op
==
csm_grad
(
None
):
return
[
csm_grad_c
(
*
node
.
inputs
)]
return
False
theano
.
compile
.
optdb
.
register
(
'local_inplace_remove0'
,
gof
.
TopoOptimizer
(
local_inplace_remove0
,
failure_callback
=
gof
.
TopoOptimizer
.
warn_inplace
),
60
,
'fast_run'
,
'inplace'
)
register_specialize
(
local_csm_grad_c
)
@gof.local_optimizer
([
csm_properties
])
...
...
@@ -37,3 +44,75 @@ def local_csm_properties_csm(node):
return
False
register_specialize
(
local_csm_properties_csm
)
@gof.local_optimizer
([
None
])
def
local_inplace_remove0
(
node
):
"""
Optimization to insert inplace versions of Remove0.
"""
if
isinstance
(
node
.
op
,
Remove0
)
and
not
node
.
op
.
inplace
:
new_op
=
node
.
op
.
__class__
(
inplace
=
True
)
new_node
=
new_op
(
*
node
.
inputs
)
return
[
new_node
]
return
False
theano
.
compile
.
optdb
.
register
(
'local_inplace_remove0'
,
gof
.
TopoOptimizer
(
local_inplace_remove0
,
failure_callback
=
gof
.
TopoOptimizer
.
warn_inplace
),
60
,
'fast_run'
,
'inplace'
)
# register a specialization to replace StructuredDot -> StructuredDotCSx
@gof.local_optimizer
([
_structured_dot
])
def
local_structured_dot
(
node
):
if
node
.
op
==
_structured_dot
:
a
,
b
=
node
.
inputs
if
a
.
type
.
format
==
'csc'
:
a_val
,
a_ind
,
a_ptr
,
a_shape
=
csm_properties
(
a
)
a_nsparse
=
a_shape
[
0
]
return
[
sd_csc
(
a_val
,
a_ind
,
a_ptr
,
a_nsparse
,
b
)]
if
a
.
type
.
format
==
'csr'
:
a_val
,
a_ind
,
a_ptr
,
a_shape
=
csm_properties
(
a
)
return
[
sd_csr
(
a_val
,
a_ind
,
a_ptr
,
b
)]
return
False
# Commented out because
# a) it is only slightly faster than scipy these days, and sometimes a little
# slower, and
# b) the resulting graphs make it very difficult for an op to do size checking
# on the matrices involved. dimension mismatches are hard to detect sensibly.
#register_specialize(local_structured_dot)
@gof.local_optimizer
([
usmm_csc_dense
])
def
local_usmm_csc_dense_inplace
(
node
):
if
node
.
op
==
usmm_csc_dense
:
return
[
usmm_csc_dense_inplace
(
*
node
.
inputs
)]
register_specialize
(
local_usmm_csc_dense_inplace
,
'inplace'
)
@gof.local_optimizer
([
usmm
])
def
local_usmm_csx
(
node
):
""" usmm -> usmm_csc_dense """
if
node
.
op
==
usmm
:
alpha
,
x
,
y
,
z
=
node
.
inputs
x_is_sparse_variable
=
_is_sparse_variable
(
x
)
y_is_sparse_variable
=
_is_sparse_variable
(
y
)
if
x_is_sparse_variable
and
not
y_is_sparse_variable
:
if
x
.
type
.
format
==
'csc'
:
x_val
,
x_ind
,
x_ptr
,
x_shape
=
csm_properties
(
x
)
x_nsparse
=
x_shape
[
0
]
dtype_out
=
scalar
.
upcast
(
alpha
.
type
.
dtype
,
x
.
type
.
dtype
,
y
.
type
.
dtype
,
z
.
type
.
dtype
)
if
dtype_out
not
in
(
'float32'
,
'float64'
):
return
False
# Sparse cast is not implemented.
if
y
.
type
.
dtype
!=
dtype_out
:
return
False
return
[
usmm_csc_dense
(
alpha
,
x_val
,
x_ind
,
x_ptr
,
x_nsparse
,
y
,
z
)]
return
False
register_specialize
(
local_usmm_csx
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论