Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
76a6cd53
提交
76a6cd53
authored
3月 09, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
new GEMM optimization algorithm
上级
3128a44c
全部展开
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
7 行删除
+81
-7
blas.py
theano/tensor/blas.py
+0
-0
test_blas.py
theano/tensor/tests/test_blas.py
+81
-7
没有找到文件。
theano/tensor/blas.py
浏览文件 @
76a6cd53
差异被折叠。
点击展开。
theano/tensor/tests/test_blas.py
浏览文件 @
76a6cd53
from
nose.plugins.skip
import
SkipTest
import
traceback
import
traceback
import
theano.tensor
as
T
import
theano.tensor
as
T
from
theano.gof
import
Env
from
theano.gof
import
Env
from
theano.printing
import
pp
from
theano.printing
import
pp
import
numpy
,
theano
import
numpy
,
theano
from
theano.tensor.blas
import
*
from
theano.tensor.blas
import
*
from
theano.tensor.blas
import
_dot22
,
_dot22scalar
,
res_is_a
,
_as_scalar
,
_is_real_matrix
from
theano.tensor.blas
import
(
_dot22
,
_dot22scalar
,
res_is_a
,
_as_scalar
,
_is_real_matrix
,
_gemm_canonicalize
,
_factor_canonicalized
)
from
unittest
import
TestCase
from
unittest
import
TestCase
from
theano.tests
import
unittest_tools
from
theano.tests
import
unittest_tools
from
copy
import
copy
from
copy
import
copy
...
@@ -267,16 +269,24 @@ class Failure(Exception):
...
@@ -267,16 +269,24 @@ class Failure(Exception):
class
Warning
(
Exception
):
class
Warning
(
Exception
):
pass
pass
def
just_gemm
(
i
,
o
,
ishapes
=
[(
4
,
3
),
(
3
,
5
),
(
4
,
5
),
(),
()]):
def
just_gemm
(
i
,
o
,
ishapes
=
[(
4
,
3
),
(
3
,
5
),
(
4
,
5
),
(),
()]
,
max_graphlen
=
0
):
try
:
try
:
f
=
inplace_func
([
Param
(
ii
,
mutable
=
True
)
for
ii
in
i
],
o
,
mode
=
'FAST_RUN'
)
f
=
inplace_func
([
Param
(
ii
,
mutable
=
True
)
for
ii
in
i
],
o
,
mode
=
'FAST_RUN'
)
at_least_one_gemm
=
False
for
node
in
f
.
maker
.
env
.
nodes
:
for
node
in
f
.
maker
.
env
.
nodes
:
if
node
.
op
==
T
.
dot
:
raise
Warning
(
'dot not changed to gemm_inplace in graph'
)
if
node
.
op
==
T
.
dot
:
raise
Warning
(
'dot not changed to gemm_inplace in graph'
)
if
node
.
op
==
_dot22
:
raise
Warning
(
'_dot22 not changed to gemm_inplace in graph'
)
if
node
.
op
==
_dot22
:
raise
Warning
(
'_dot22 not changed to gemm_inplace in graph'
)
if
node
.
op
==
gemm_inplace
:
at_least_one_gemm
=
True
assert
at_least_one_gemm
g
=
inplace_func
(
i
,
o
,
mode
=
compile
.
Mode
(
linker
=
'py'
,
optimizer
=
None
))
g
=
inplace_func
(
i
,
o
,
mode
=
compile
.
Mode
(
linker
=
'py'
,
optimizer
=
None
))
for
node
in
g
.
maker
.
env
.
nodes
:
for
node
in
g
.
maker
.
env
.
nodes
:
if
node
.
op
==
gemm_inplace
:
raise
Exception
(
'gemm_inplace in original graph'
)
if
node
.
op
==
gemm_inplace
:
raise
Exception
(
'gemm_inplace in original graph'
)
graphlen
=
len
(
f
.
maker
.
env
.
toposort
())
if
max_graphlen
and
(
graphlen
<=
max_graphlen
):
theano
.
printing
.
debugprint
(
f
)
assert
False
,
'graphlen=
%
i>
%
i'
%
(
graphlen
,
max_graphlen
)
rng
=
numpy
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
(
234
))
rng
=
numpy
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
(
234
))
r0
=
f
(
*
[
rng
.
randn
(
*
sh
)
for
sh
in
ishapes
])
r0
=
f
(
*
[
rng
.
randn
(
*
sh
)
for
sh
in
ishapes
])
rng
=
numpy
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
(
234
))
rng
=
numpy
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
(
234
))
...
@@ -353,12 +363,76 @@ def test_gemm_opt_double_gemm():
...
@@ -353,12 +363,76 @@ def test_gemm_opt_double_gemm():
print
'GRAPH'
,
node
print
'GRAPH'
,
node
raise
raise
def
wishlist_gemm_opt
():
def
test_gemm_canonicalize
():
X
,
Y
,
Z
,
a
,
b
=
T
.
dmatrix
(
'X'
),
T
.
dmatrix
(
'Y'
),
T
.
dmatrix
(
'Z'
),
T
.
dscalar
(
'a'
),
T
.
dscalar
(
'b'
)
R
,
S
,
U
,
c
,
d
=
T
.
dmatrix
(
'R'
),
T
.
dmatrix
(
'S'
),
T
.
dmatrix
(
'U'
),
T
.
dscalar
(
'c'
),
T
.
dscalar
(
'd'
)
u
=
T
.
row
(
'u'
)
can
=
[]
_gemm_canonicalize
(
X
+
Y
+
Z
,
1.0
,
can
,
0
)
assert
can
==
[(
1.0
,
X
),
(
1.0
,
Y
),
(
1.0
,
Z
)]
can
=
[]
_gemm_canonicalize
(
X
+
Y
+
u
,
1.0
,
can
,
0
)
assert
can
==
[(
1.0
,
X
),
(
1.0
,
Y
),
u
]
can
=
[]
_gemm_canonicalize
(
a
*
X
+
Y
-
b
*
Z
*
c
,
1.0
,
can
,
0
)
assert
can
[
0
]
==
(
a
,
X
)
assert
can
[
1
]
==
(
1.0
,
Y
)
assert
can
[
2
][
0
]
.
owner
.
op
==
T
.
mul
assert
can
[
2
][
0
]
.
owner
.
inputs
[
0
]
.
owner
.
op
==
T
.
neg
assert
can
[
2
][
0
]
.
owner
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
==
c
assert
can
[
2
][
0
]
.
owner
.
inputs
[
1
]
==
b
can
=
[]
_gemm_canonicalize
((
-
d
)
*
X
-
(
a
*
X
+
Y
-
b
*
Z
*
c
),
1.0
,
can
,
0
)
print
can
assert
can
[
0
][
0
]
.
owner
.
op
==
T
.
neg
assert
can
[
0
][
0
]
.
owner
.
inputs
[
0
]
==
d
assert
can
[
0
][
1
]
==
X
assert
can
[
1
][
0
]
.
owner
.
op
==
T
.
neg
assert
can
[
1
][
0
]
.
owner
.
inputs
[
0
]
==
a
assert
can
[
2
]
==
(
-
1.0
,
Y
)
assert
can
[
3
][
0
]
.
owner
.
op
==
T
.
mul
assert
can
[
3
][
0
]
.
owner
.
inputs
==
[
c
,
b
]
def
test_gemm_factor
():
X
,
Y
,
Z
,
a
,
b
=
T
.
dmatrix
(
'X'
),
T
.
dmatrix
(
'Y'
),
T
.
dmatrix
(
'Z'
),
T
.
dscalar
(
'a'
),
T
.
dscalar
(
'b'
)
R
,
S
,
U
,
c
,
d
=
T
.
dmatrix
(
'R'
),
T
.
dmatrix
(
'S'
),
T
.
dmatrix
(
'U'
),
T
.
dscalar
(
'c'
),
T
.
dscalar
(
'd'
)
u
=
T
.
row
(
'u'
)
assert
[(
1.0
,
X
),
(
1.0
,
Y
),
u
]
==
_factor_canonicalized
([(
1.0
,
X
),
(
1.0
,
Y
),
u
])
assert
[(
2.0
,
X
),
u
]
==
_factor_canonicalized
([(
1.0
,
X
),(
1.0
,
X
),
u
])
def
test_gemm_nested
():
X
,
Y
,
Z
,
a
,
b
=
T
.
dmatrix
(
'X'
),
T
.
dmatrix
(
'Y'
),
T
.
dmatrix
(
'Z'
),
T
.
dscalar
(
'a'
),
T
.
dscalar
(
'b'
)
R
,
S
,
U
,
c
,
d
=
T
.
dmatrix
(
'R'
),
T
.
dmatrix
(
'S'
),
T
.
dmatrix
(
'U'
),
T
.
dscalar
(
'c'
),
T
.
dscalar
(
'd'
)
u
=
T
.
row
(
'u'
)
just_gemm
([
X
,
Y
,
Z
,
R
,
S
,
U
,
a
,
b
,
c
,
d
],
[
a
*
Z
-
b
*
(
c
*
T
.
dot
(
X
,
Y
)
+
d
*
Z
)],
ishapes
=
[(
2
,
3
),(
3
,
4
),(
2
,
4
),(
2
,
3
),(
3
,
4
),(
2
,
4
),(),(),(),()],
max_graphlen
=
1
)
print
"---------------------"
just_gemm
([
X
,
Y
,
Z
,
R
,
S
,
U
,
a
,
b
,
c
,
d
],
[
a
*
Z
-
b
*
(
c
*
T
.
dot
(
X
,
Y
)
+
d
*
Z
+
c
*
Z
)],
ishapes
=
[(
2
,
3
),(
3
,
4
),(
2
,
4
),(
2
,
3
),(
3
,
4
),(
2
,
4
),(),(),(),()],
max_graphlen
=
1
)
print
"---------------------"
just_gemm
([
X
,
Y
,
Z
,
R
,
S
,
U
,
a
,
b
,
c
,
d
],
[
a
*
Z
-
b
*
(
c
*
T
.
dot
(
X
,
Y
)
+
d
*
Z
+
c
*
U
)],
ishapes
=
[(
2
,
3
),(
3
,
4
),(
2
,
4
),(
2
,
3
),(
3
,
4
),(
2
,
4
),(),(),(),()],
max_graphlen
=
3
)
def
test_gemm_opt_wishlist
():
X
,
Y
,
Z
,
a
,
b
=
T
.
dmatrix
(),
T
.
dmatrix
(),
T
.
dmatrix
(),
T
.
dscalar
(),
T
.
dscalar
()
X
,
Y
,
Z
,
a
,
b
=
T
.
dmatrix
(),
T
.
dmatrix
(),
T
.
dmatrix
(),
T
.
dscalar
(),
T
.
dscalar
()
#with >2 additions of the same T.dot(X,Y term
#with >2 additions of the same T.dot(X,Y term
just_gemm
([
X
,
Y
,
Z
,
a
,
b
],
[
Z
+
T
.
dot
(
X
,
Y
)
+
T
.
dot
(
X
,
Y
)])
just_gemm
([
X
,
Y
,
Z
,
a
,
b
],
[(
b
*
b
)
*
Z
*
a
+
(
a
*
a
)
*
T
.
dot
(
X
,
Y
)
+
b
*
T
.
dot
(
X
,
Y
)])
just_gemm
([
X
,
Y
,
Z
,
a
,
b
],
[(
b
*
b
)
*
Z
*
a
+
(
a
*
a
)
*
T
.
dot
(
X
,
Y
)
+
b
*
T
.
dot
(
X
,
Y
)])
just_gemm
([
X
,
Y
,
Z
,
a
,
b
],
[
Z
+
T
.
dot
(
X
,
Y
)
+
T
.
dot
(
X
,
Y
)])
def
test_gemm_with_vector
():
def
test_gemm_with_vector
():
"""Many subgraphs whose dots can be eliminated.
"""Many subgraphs whose dots can be eliminated.
...
@@ -423,9 +497,9 @@ def test_inplace1():
...
@@ -423,9 +497,9 @@ def test_inplace1():
# with > 2 terms in the overall addition
# with > 2 terms in the overall addition
f
=
inplace_func
([
X
,
Y
,
Z
,
a
,
b
],
f
=
inplace_func
([
X
,
Y
,
Z
,
a
,
b
],
[
Z
+
Z
+
T
.
dot
(
X
,
Y
)],
mode
=
'FAST_RUN'
)
[
Z
+
Z
+
T
.
dot
(
X
,
Y
)],
mode
=
'FAST_RUN'
)
# gemm_inplace should operate in-place on (Z+Z
)
theano
.
printing
.
debugprint
(
f
)
if
(
not
gemm_inplace
in
[
n
.
op
for
n
in
f
.
maker
.
env
.
nodes
]):
# it doesn't work inplace because we didn't mark Z as mutable input
raise
Failure
(
'no gemm_inplace in graph'
)
assert
[
n
.
op
for
n
in
f
.
maker
.
env
.
nodes
]
==
[
gemm_no_inplace
]
def
test_dot22
():
def
test_dot22
():
if
config
.
mode
==
'FAST_COMPILE'
:
if
config
.
mode
==
'FAST_COMPILE'
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论