Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a2f6c1c9
提交
a2f6c1c9
authored
7月 13, 2017
作者:
erakra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixing flake8 for test_blas.py
上级
c5cd87fa
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
108 行增加
和
116 行删除
+108
-116
test_blas.py
theano/tensor/tests/test_blas.py
+108
-116
没有找到文件。
theano/tensor/tests/test_blas.py
浏览文件 @
a2f6c1c9
...
...
@@ -81,7 +81,7 @@ class t_gemm(TestCase):
f
=
inplace_func
([
tz
,
ta
,
tx
,
ty
,
tb
],
gemm_inplace
(
tz
,
ta
,
tx
,
ty
,
tb
),
mode
=
compile
.
Mode
(
optimizer
=
None
,
linker
=
l
))
new_z
=
f
(
z
,
a
,
x
,
y
,
b
)
f
(
z
,
a
,
x
,
y
,
b
)
z_after
=
self
.
_gemm
(
z_orig
,
a
,
x
,
y
,
b
)
# print z_orig, z_after, z, type(z_orig), type(z_after), type(z)
...
...
@@ -96,8 +96,8 @@ class t_gemm(TestCase):
cmp_linker
(
copy
(
z
),
a
,
x
,
y
,
b
,
'c|py'
)
cmp_linker
(
copy
(
z
),
a
,
x
,
y
,
b
,
'py'
)
if
(
not
dtype
.
startswith
(
"complex"
)
and
theano
.
config
.
cxx
):
if
(
not
dtype
.
startswith
(
"complex"
)
and
theano
.
config
.
cxx
):
# If theano.config.blas.ldflags is empty, Theano will use
# a NumPy C implementation of [sd]gemm_.
cmp_linker
(
copy
(
z
),
a
,
x
,
y
,
b
,
'c'
)
...
...
@@ -105,7 +105,7 @@ class t_gemm(TestCase):
def
test0a
(
self
):
Gemm
.
debug
=
True
try
:
g
=
g
emm_no_inplace
([
1.
],
1.
,
[
1.
],
[
1.
],
1.
)
gemm_no_inplace
([
1.
],
1.
,
[
1.
],
[
1.
],
1.
)
except
TypeError
as
e
:
if
exc_message
(
e
)
is
Gemm
.
E_rank
:
return
...
...
@@ -171,7 +171,6 @@ class t_gemm(TestCase):
def
test_factorised_scalar
(
self
):
a
=
T
.
matrix
()
b
=
T
.
matrix
()
c
=
T
.
matrix
()
s
=
theano
.
shared
(
np
.
zeros
((
5
,
5
))
.
astype
(
config
.
floatX
))
lr1
=
T
.
constant
(
0.01
)
.
astype
(
config
.
floatX
)
...
...
@@ -182,7 +181,7 @@ class t_gemm(TestCase):
f
=
theano
.
function
([
a
,
b
],
updates
=
[(
s
,
lr1
*
T
.
dot
(
a
,
b
)
+
l2_reg
*
lr2
*
s
)],
mode
=
mode_not_fast_compile
)
.
maker
.
fgraph
.
toposort
()
#[Gemm{inplace}(<TensorType(float64, matrix)>, 0.01,
#
[Gemm{inplace}(<TensorType(float64, matrix)>, 0.01,
# <TensorType(float64, matrix)>, <TensorType(float64, matrix)>,
# 2e-06)]
assert
len
(
f
)
==
1
...
...
@@ -192,7 +191,7 @@ class t_gemm(TestCase):
f
=
theano
.
function
([
a
,
b
],
updates
=
[(
s
,
lr1
*
(
T
.
dot
(
a
,
b
)
-
l2_reg
*
s
))],
mode
=
mode_not_fast_compile
)
.
maker
.
fgraph
.
toposort
()
#[Gemm{inplace}(<TensorType(float64, matrix)>, 0.01,
#
[Gemm{inplace}(<TensorType(float64, matrix)>, 0.01,
# <TensorType(float64, matrix)>, <TensorType(float64, matrix)>,
# -2e-06)]
assert
len
(
f
)
==
1
...
...
@@ -202,7 +201,7 @@ class t_gemm(TestCase):
f
=
theano
.
function
([
a
,
b
],
updates
=
[(
s
,
s
-
lr1
*
(
s
*
.
0002
+
T
.
dot
(
a
,
b
)))],
mode
=
mode_not_fast_compile
)
.
maker
.
fgraph
.
toposort
()
#[Gemm{inplace}(<TensorType(float64, matrix)>, -0.01,
#
[Gemm{inplace}(<TensorType(float64, matrix)>, -0.01,
# <TensorType(float64, matrix)>, <TensorType(float64, matrix)>,
# 0.999998)]
assert
len
(
f
)
==
1
...
...
@@ -270,14 +269,14 @@ class t_gemm(TestCase):
def
t
(
z
,
x
,
y
,
a
=
1.0
,
b
=
0.0
,
l
=
'c|py'
,
dt
=
'float64'
):
z
,
a
,
x
,
y
,
b
=
[
theano
.
_asarray
(
p
,
dtype
=
dt
)
for
p
in
(
z
,
a
,
x
,
y
,
b
)]
z_orig
=
z
.
copy
()
#
z_orig = z.copy()
z_after
=
self
.
_gemm
(
z
,
a
,
x
,
y
,
b
)
tz
,
ta
,
tx
,
ty
,
tb
=
[
shared
(
p
)
for
p
in
(
z
,
a
,
x
,
y
,
b
)]
# f = inplace_func([tz,ta,tx,ty,tb], gemm_inplace(tz,ta,tx,ty,tb),
# mode = compile.Mode(optimizer = None, linker=l))
#f(z, a, x, y, b)
#
f(z, a, x, y, b)
f
=
inplace_func
([],
gemm_inplace
(
tz
,
ta
,
tx
,
ty
,
tb
),
mode
=
compile
.
Mode
(
optimizer
=
None
,
linker
=
l
))
f
()
...
...
@@ -355,8 +354,9 @@ class t_gemm(TestCase):
tz_i
=
gemm_no_inplace
(
tz
[:,
:,
i
],
ta
,
tx
[
:,
:,
i
],
ty
[:,
:,
i
],
tb
)
g_i
=
theano
.
function
([],
tz_i
,
updates
=
[(
tz
,
T
.
set_subtensor
(
tz
[:,
:,
i
],
tz_i
))],
g_i
=
theano
.
function
(
[],
tz_i
,
updates
=
[(
tz
,
T
.
set_subtensor
(
tz
[:,
:,
i
],
tz_i
))],
mode
=
compile
.
Mode
(
optimizer
=
None
,
linker
=
l
))
for
j
in
xrange
(
3
):
g_i
()
...
...
@@ -404,8 +404,8 @@ class t_as_scalar(TestCase):
def
test1
(
self
):
"""Test that it fails on nonscalar constants"""
a
=
T
.
constant
(
np
.
ones
(
5
))
self
.
assertTrue
(
None
==
_as_scalar
(
a
)
)
self
.
assertTrue
(
None
==
_as_scalar
(
T
.
DimShuffle
([
False
],
[
0
,
'x'
])(
a
))
)
self
.
assertTrue
(
_as_scalar
(
a
)
is
None
)
self
.
assertTrue
(
_as_scalar
(
T
.
DimShuffle
([
False
],
[
0
,
'x'
])(
a
))
is
None
)
def
test2
(
self
):
"""Test that it works on scalar variables"""
...
...
@@ -420,9 +420,9 @@ class t_as_scalar(TestCase):
def
test3
(
self
):
"""Test that it fails on nonscalar variables"""
a
=
T
.
matrix
()
self
.
assertTrue
(
None
==
_as_scalar
(
a
)
)
self
.
assertTrue
(
None
==
_as_scalar
(
T
.
DimShuffle
([
False
,
False
],
[
0
,
'x'
,
1
])(
a
))
)
self
.
assertTrue
(
_as_scalar
(
a
)
is
None
)
self
.
assertTrue
(
_as_scalar
(
T
.
DimShuffle
([
False
,
False
],
[
0
,
'x'
,
1
])(
a
))
is
None
)
class
T_real_matrix
(
TestCase
):
...
...
@@ -541,8 +541,8 @@ def test_gemm_opt_double_gemm():
ishapes
=
[(
4
,
3
),
(
3
,
5
),
(
4
,
5
),
(),
(),
(
5
,
9
),
(
9
,
4
),
()]
i
=
[
X
,
Y
,
Z
,
a
,
b
,
R
,
S
,
c
]
o
=
[(
a
*
T
.
dot
(
X
,
Y
)
+
gemm_inplace
(
Z
,
b
,
S
.
T
,
R
.
T
,
T
.
constant
(
1.0
)
.
astype
(
config
.
floatX
)))]
o
=
[(
a
*
T
.
dot
(
X
,
Y
)
+
gemm_inplace
(
Z
,
b
,
S
.
T
,
R
.
T
,
T
.
constant
(
1.0
)
.
astype
(
config
.
floatX
)))]
try
:
f
=
inplace_func
([
In
(
ii
,
mutable
=
True
)
for
ii
in
i
],
o
,
mode
=
'FAST_RUN'
,
on_unused_input
=
'ignore'
)
...
...
@@ -579,8 +579,7 @@ def test_gemm_opt_double_gemm():
def
test_gemm_canonicalize
():
X
,
Y
,
Z
,
a
,
b
=
T
.
matrix
(
'X'
),
T
.
matrix
(
'Y'
),
T
.
matrix
(
'Z'
),
T
.
scalar
(
'a'
),
T
.
scalar
(
'b'
)
R
,
S
,
U
,
c
,
d
=
T
.
matrix
(
'R'
),
T
.
matrix
(
'S'
),
T
.
matrix
(
'U'
),
T
.
scalar
(
'c'
),
T
.
scalar
(
'd'
)
c
,
d
=
T
.
scalar
(
'c'
),
T
.
scalar
(
'd'
)
u
=
T
.
row
(
'u'
)
v
=
T
.
vector
(
'v'
)
w
=
T
.
col
(
'w'
)
...
...
@@ -631,10 +630,7 @@ def test_gemm_canonicalize():
def
test_gemm_factor
():
X
,
Y
,
Z
,
a
,
b
=
T
.
matrix
(
'X'
),
T
.
matrix
(
'Y'
),
T
.
matrix
(
'Z'
),
T
.
scalar
(
'a'
),
T
.
scalar
(
'b'
)
R
,
S
,
U
,
c
,
d
=
T
.
matrix
(
'R'
),
T
.
matrix
(
'S'
),
T
.
matrix
(
'U'
),
T
.
scalar
(
'c'
),
T
.
scalar
(
'd'
)
X
,
Y
=
T
.
matrix
(
'X'
),
T
.
matrix
(
'Y'
)
assert
[(
1.0
,
X
),
(
1.0
,
Y
)]
==
_factor_canonicalized
([(
1.0
,
X
),
(
1.0
,
Y
)])
assert
[(
2.0
,
X
)]
==
_factor_canonicalized
([(
1.0
,
X
),
(
1.0
,
X
)])
...
...
@@ -653,7 +649,7 @@ def test_upcasting_scalar_nogemm():
f
=
theano
.
function
([
w
,
v
,
t
,
alpha
],
rval
)
t
=
f
.
maker
.
fgraph
.
toposort
()
assert
np
.
sum
([
isinstance
(
n
.
op
,
Gemm
)
for
n
in
t
])
==
0
#theano.printing.debugprint(f, print_type=True)
#
theano.printing.debugprint(f, print_type=True)
v
=
T
.
fmatrix
(
'v'
)
w
=
T
.
fmatrix
(
'w'
)
...
...
@@ -670,7 +666,7 @@ def test_upcasting_scalar_nogemm():
t
=
f
.
maker
.
fgraph
.
toposort
()
assert
np
.
sum
([
isinstance
(
n
.
op
,
Gemm
)
for
n
in
t
])
==
0
#theano.printing.debugprint(f, print_type=True)
#
theano.printing.debugprint(f, print_type=True)
def
test_gemm_nested
():
...
...
@@ -681,20 +677,20 @@ def test_gemm_nested():
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
),
(),
(),
(),
()],
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
),
(),
(),
(),
()],
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
),
(),
(),
(),
()],
ishapes
=
[(
2
,
3
),
(
3
,
4
),
(
2
,
4
),
(
2
,
3
),
(
3
,
4
),
(
2
,
4
),
(),
(),
(),
()],
max_graphlen
=
3
)
...
...
@@ -720,7 +716,7 @@ def test_gemm_with_vector():
def
my_just_gemm
(
o
):
i
=
[
X
,
Y
,
Z
,
a
,
b
,
v
]
ishapes
=
[(
4
,
3
),
(
3
,
5
),
(
4
,
5
),
(),
(),
(
5
,
)]
rval
=
just_gemm
(
i
,
o
,
ishapes
=
ishapes
)
just_gemm
(
i
,
o
,
ishapes
=
ishapes
)
my_just_gemm
([
v
+
T
.
dot
(
X
,
Y
)
*
a
+
Z
*
b
])
my_just_gemm
([
v
+
a
*
T
.
dot
(
X
,
Y
)
+
b
*
Z
])
...
...
@@ -741,7 +737,7 @@ def test_gemm_with_vector():
def
test_gemm_opt_vector_stuff
():
X
,
Y
,
Z
,
a
,
b
=
T
.
matrix
(),
T
.
matrix
(),
T
.
matrix
(),
T
.
scalar
(),
T
.
scalar
()
X
,
Y
,
a
=
T
.
matrix
(),
T
.
matrix
(),
T
.
scalar
()
u
,
v
=
T
.
vector
(),
T
.
vector
()
f
=
inplace_func
([
a
,
u
,
v
],
a
+
T
.
dot
(
u
,
v
),
mode
=
'FAST_RUN'
)
...
...
@@ -771,8 +767,6 @@ def test_gemm_unrolled():
H
=
sharedX
(
np
.
zeros
((
batch_size
,
rep_size
)),
name
=
'H'
)
G
=
sharedX
(
np
.
zeros
((
batch_size
,
rep_size
)),
name
=
'G'
)
init_V
=
sharedX
(
rng
.
uniform
(
0
,
1
,
(
batch_size
,
rep_size
)),
name
=
'init_V'
)
init_H
=
sharedX
(
rng
.
uniform
(
0
,
1
,
(
batch_size
,
rep_size
)),
name
=
'init_H'
)
cur_V
=
V
cur_H
=
H
...
...
@@ -817,7 +811,7 @@ def test_inplace0():
f
=
inplace_func
([
X
,
Y
,
Z
,
a
,
b
,
R
,
S
,
c
],
[
Z
*
(
c
*
Z
+
a
*
T
.
dot
(
X
,
Y
)
+
b
*
T
.
dot
(
R
,
S
)
.
T
)],
mode
=
'FAST_RUN'
)
if
(
not
gemm_inplace
in
[
n
.
op
for
n
in
f
.
maker
.
fgraph
.
apply_nodes
]):
if
(
gemm_inplace
not
in
[
n
.
op
for
n
in
f
.
maker
.
fgraph
.
apply_nodes
]):
theano
.
printing
.
debugprint
(
f
)
raise
Failure
(
'no gemm_inplace in graph'
)
...
...
@@ -866,7 +860,7 @@ def test_dot22scalar():
# TODO: exclude other optimizations in BlasOpt?
# m = theano.compile.get_default_mode().including('local_dot_to_dot22',
# 'local_dot22_to_dot22scalar','specialize')
#m = theano.compile.get_default_mode().including('BlasOpt', 'specialize')
#
m = theano.compile.get_default_mode().including('BlasOpt', 'specialize')
rng
=
np
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
for
dtype1
in
[
'complex64'
,
'complex128'
]:
a
=
T
.
matrix
(
'a'
,
dtype
=
dtype1
)
...
...
@@ -1087,7 +1081,7 @@ def test_dot_w_self():
class
TestGemv
(
TestCase
,
unittest_tools
.
TestOptimizationMixin
):
def
test_dot_vv
(
self
):
''' Currently we generate a gemv for that case'''
# Currently we generate a gemv for that case
rng
=
np
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
v
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
'float32'
))
w
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
'float32'
))
...
...
@@ -1101,11 +1095,10 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
assert
np
.
allclose
(
f
(),
np
.
dot
(
v
.
get_value
(),
w
.
get_value
()))
def
test_dot_vm
(
self
):
''' Test vector dot matrix '''
# Test vector dot matrix
rng
=
np
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
v
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
'float32'
))
m
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,
3
)),
dtype
=
'float32'
))
m
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,
3
)),
dtype
=
'float32'
))
f
=
theano
.
function
([],
theano
.
dot
(
v
,
m
),
mode
=
mode_blas_opt
)
# Assert that the dot was optimized somehow
...
...
@@ -1115,17 +1108,14 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output
assert
np
.
allclose
(
f
(),
np
.
dot
(
v
.
get_value
(),
m
.
get_value
()))
# Assert it works when m has no contiguous dimension
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
assert
np
.
allclose
(
f
(),
np
.
dot
(
v
.
get_value
(),
m
.
get_value
()))
def
test_dot_mv
(
self
):
''' Test matrix dot vector '''
# Test matrix dot vector
rng
=
np
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
v
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
'float32'
))
m
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
3
,
2
)),
dtype
=
'float32'
))
m
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
3
,
2
)),
dtype
=
'float32'
))
f
=
theano
.
function
([],
theano
.
dot
(
m
,
v
),
mode
=
mode_blas_opt
)
# Assert that the dot was optimized somehow
...
...
@@ -1135,31 +1125,27 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output
assert
np
.
allclose
(
f
(),
np
.
dot
(
m
.
get_value
(),
v
.
get_value
()))
# Assert it works when m has no contiguous dimension
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
assert
np
.
allclose
(
f
(),
np
.
dot
(
m
.
get_value
(),
v
.
get_value
()))
@staticmethod
def
t_gemv1
(
m_shp
):
''' test vector2+dot(matrix,vector1) '''
# test vector2+dot(matrix,vector1)
rng
=
np
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
v1
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
m_shp
[
1
],)
),
dtype
=
'float32'
))
v1
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
m_shp
[
1
],)
),
dtype
=
'float32'
))
v2_orig
=
np
.
array
(
rng
.
uniform
(
size
=
(
m_shp
[
0
],)),
dtype
=
'float32'
)
v2
=
theano
.
shared
(
v2_orig
)
m
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
m_shp
),
dtype
=
'float32'
))
m
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
m_shp
),
dtype
=
'float32'
))
f
=
theano
.
function
([],
v2
+
theano
.
dot
(
m
,
v1
),
mode
=
mode_blas_opt
)
# Assert they produce the same output
assert
np
.
allclose
(
f
(),
np
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
assert
np
.
allclose
(
f
(),
np
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
Gemv
)
assert
topo
[
0
]
.
op
.
inplace
==
False
assert
topo
[
0
]
.
op
.
inplace
is
False
# test the inplace version
g
=
theano
.
function
([],
[],
updates
=
[(
v2
,
v2
+
theano
.
dot
(
m
,
v1
))],
...
...
@@ -1167,17 +1153,16 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output
g
()
assert
np
.
allclose
(
v2
.
get_value
(),
np
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
assert
np
.
allclose
(
v2
.
get_value
(),
np
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
topo
=
g
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
Gemv
)
if
config
.
mode
!=
'FAST_COMPILE'
:
assert
topo
[
0
]
.
op
.
inplace
==
True
assert
topo
[
0
]
.
op
.
inplace
is
True
# Do the same tests with a matrix with strides in both dimensions
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
v2
.
set_value
(
v2_orig
)
assert
np
.
allclose
(
f
(),
...
...
@@ -1194,7 +1179,7 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self
.
t_gemv1
((
0
,
0
))
def
test_gemv2
(
self
):
''' test vector2+dot(vector1,matrix) '''
# test vector2+dot(vector1,matrix)
rng
=
np
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
v1
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
'float32'
))
...
...
@@ -1207,10 +1192,11 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output
assert
np
.
allclose
(
f
(),
np
.
dot
(
v1
.
get_value
(),
m
.
get_value
())
+
v2
.
get_value
())
np
.
dot
(
v1
.
get_value
(),
m
.
get_value
())
+
v2
.
get_value
())
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
sum
(
isinstance
(
node
.
op
,
Gemv
)
for
node
in
topo
)
==
1
assert
topo
[
-
1
]
.
op
.
inplace
==
False
assert
topo
[
-
1
]
.
op
.
inplace
is
False
# test the inplace version
g
=
theano
.
function
([],
[],
updates
=
[(
v2
,
v2
+
theano
.
dot
(
v1
,
m
))],
...
...
@@ -1223,21 +1209,21 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
topo
=
g
.
maker
.
fgraph
.
toposort
()
assert
sum
(
isinstance
(
node
.
op
,
Gemv
)
for
node
in
topo
)
==
1
if
config
.
mode
!=
'FAST_COMPILE'
:
assert
topo
[
-
1
]
.
op
.
inplace
==
True
assert
topo
[
-
1
]
.
op
.
inplace
is
True
# Do the same tests with a matrix with strides in both dimensions
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
m
.
set_value
(
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
v2
.
set_value
(
v2_orig
)
assert
np
.
allclose
(
f
(),
np
.
dot
(
v1
.
get_value
(),
m
.
get_value
())
+
v2
.
get_value
())
np
.
dot
(
v1
.
get_value
(),
m
.
get_value
())
+
v2
.
get_value
())
g
()
assert
np
.
allclose
(
v2
.
get_value
(),
np
.
dot
(
v1
.
get_value
(),
m
.
get_value
())
+
v2_orig
)
def
test_gemv_broadcast
(
self
):
''' test gemv with some broadcasted input '''
# test gemv with some broadcasted input
rng
=
np
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
v1
=
theano
.
shared
(
np
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
'float32'
))
...
...
@@ -1261,7 +1247,7 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
f
=
theano
.
function
([],
o
,
mode
=
mode_blas_opt
)
assert
np
.
allclose
(
f
(),
0.5
*
np
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
0.25
*
v2
.
get_value
())
0.5
*
np
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
0.25
*
v2
.
get_value
())
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
sum
(
isinstance
(
node
.
op
,
Gemv
)
for
node
in
topo
)
==
1
...
...
@@ -1336,8 +1322,8 @@ class BaseGemv(object):
def
test_simple
(
self
):
alpha
,
beta
,
a
,
x
,
y
=
[
self
.
shared
(
value
)
for
value
in
self
.
get_data
()]
desired_oy
=
alpha
.
get_value
()
*
matrixmultiply
(
a
.
get_value
(),
x
.
get_value
())
+
beta
.
get_value
()
*
y
.
get_value
()
desired_oy
=
alpha
.
get_value
()
*
matrixmultiply
(
a
.
get_value
(),
x
.
get_value
())
+
beta
.
get_value
()
*
y
.
get_value
()
oy
=
alpha
*
T
.
dot
(
a
,
x
)
+
beta
*
y
...
...
@@ -1406,8 +1392,8 @@ class BaseGemv(object):
alpha_v
,
beta_v
,
a_v
,
x_v
,
y_v
=
vs
alpha
,
beta
,
a
,
x
,
y
=
[
self
.
shared
(
v
)
for
v
in
vs
]
desired_oy
=
alpha_v
*
matrixmultiply
(
transpose
(
a_v
),
x_v
[::
2
])
+
beta_v
*
y_v
desired_oy
=
alpha_v
*
matrixmultiply
(
transpose
(
a_v
),
x_v
[::
2
])
+
beta_v
*
y_v
oy
=
alpha
*
T
.
dot
(
a
.
T
,
x
[::
2
])
+
beta
*
y
...
...
@@ -1456,8 +1442,7 @@ class BaseGemv(object):
alpha_v
,
beta_v
,
a_v
,
x_v
,
y_v
=
vs
alpha
,
beta
,
a
,
x
,
y
=
[
self
.
shared
(
v
)
for
v
in
vs
]
a_v
=
a_v
[::
-
1
,
::
-
1
]
a
.
set_value
(
a
.
get_value
(
borrow
=
True
,
a
.
set_value
(
a
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
...
...
@@ -1477,8 +1462,7 @@ class BaseGemv(object):
alpha_v
,
beta_v
,
a_v
,
x_v
,
y_v
=
vs
alpha
,
beta
,
a
,
x
,
y
=
[
self
.
shared
(
v
)
for
v
in
vs
]
a_v
=
a_v
[::
-
1
,
::
-
1
]
a
.
set_value
(
a
.
get_value
(
borrow
=
True
,
a
.
set_value
(
a
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
...
...
@@ -1517,7 +1501,7 @@ class BaseGemv(object):
# done inplace on a temporarily allocated-buffer, which is
# then scaled by alpha and to t with a fused elemwise.
n_gemvs
=
0
#theano.printing.debugprint(f, print_type=True)
#
theano.printing.debugprint(f, print_type=True)
for
node
in
f
.
maker
.
fgraph
.
toposort
():
if
node
.
op
==
self
.
gemv_inplace
:
n_gemvs
+=
1
...
...
@@ -1602,7 +1586,8 @@ class TestGer_make_node(TestCase):
ger
,
self
.
fm
,
self
.
fv1
,
self
.
fv
,
self
.
fv_2
)
# actually doing the aforementioned dimshuffle makes it work
self
.
assertEqual
(
self
.
fm
.
type
,
ger
(
self
.
fm
,
self
.
fv1
.
dimshuffle
(),
self
.
fv
,
self
.
fv_2
)
.
type
)
ger
(
self
.
fm
,
self
.
fv1
.
dimshuffle
(),
self
.
fv
,
self
.
fv_2
)
.
type
)
def
test_fails_for_nonmatrix_A
(
self
):
self
.
assertRaises
(
TypeError
,
...
...
@@ -1610,9 +1595,11 @@ class TestGer_make_node(TestCase):
def
test_fails_for_nonvector_x_or_y
(
self
):
self
.
assertRaises
(
TypeError
,
ger
,
self
.
fm
,
self
.
fa
,
self
.
fv
.
dimshuffle
(
'x'
,
0
),
self
.
fv_2
)
ger
,
self
.
fm
,
self
.
fa
,
self
.
fv
.
dimshuffle
(
'x'
,
0
),
self
.
fv_2
)
self
.
assertRaises
(
TypeError
,
ger
,
self
.
fm
,
self
.
fa
,
self
.
fv
,
self
.
fv_2
.
dimshuffle
(
'x'
,
0
))
ger
,
self
.
fm
,
self
.
fa
,
self
.
fv
,
self
.
fv_2
.
dimshuffle
(
'x'
,
0
))
def
test_fails_for_mixed_dtypes
(
self
):
self
.
assertRaises
(
TypeError
,
ger
,
self
.
dm
,
self
.
fa
,
self
.
fv
,
self
.
fv_2
)
...
...
@@ -1657,29 +1644,25 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
def
test_b_0_triggers_ger
(
self
):
""" test local_gemm_to_ger opt"""
assert
T
.
blas
.
local_gemm_to_ger
.
transform
(
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
self
.
y
.
dimshuffle
(
'x'
,
0
),
self
.
b
(
0
))
.
owner
)
def
test_b_1_triggers_ger
(
self
):
""" test local_gemm_to_ger opt"""
assert
T
.
blas
.
local_gemm_to_ger
.
transform
(
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
self
.
y
.
dimshuffle
(
'x'
,
0
),
self
.
b
(
1
))
.
owner
)
def
test_b_other_does_not_triggers_ger
(
self
):
""" test local_gemm_to_ger opt"""
assert
not
T
.
blas
.
local_gemm_to_ger
.
transform
(
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
self
.
y
.
dimshuffle
(
'x'
,
0
),
self
.
b
(
1.5
))
.
owner
)
def
test_b_nonconst_does_not_triggers_ger
(
self
):
""" test local_gemm_to_ger opt"""
assert
not
T
.
blas
.
local_gemm_to_ger
.
transform
(
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
gemm_no_inplace
(
self
.
A
,
self
.
a
,
self
.
x
.
dimshuffle
(
0
,
'x'
),
self
.
y
.
dimshuffle
(
'x'
,
0
),
self
.
a
)
.
owner
)
def
test_outer
(
self
):
...
...
@@ -1954,28 +1937,36 @@ class TestBlasStrides(TestCase):
bt_dev
=
b_t
.
get_value
(
borrow
=
False
,
return_internal_type
=
True
)
ct_dev
=
c_t
.
get_value
(
borrow
=
False
,
return_internal_type
=
True
)
f_nnn
=
theano
.
function
([],
[],
f_nnn
=
theano
.
function
(
[],
[],
updates
=
[(
a
,
(
l
*
a
+
tensor
.
dot
(
b
,
c
)))],
mode
=
self
.
mode
)
f_nnt
=
theano
.
function
([],
[],
f_nnt
=
theano
.
function
(
[],
[],
updates
=
[(
a
,
(
l
*
a
+
tensor
.
dot
(
b
,
c_t
.
T
)))],
mode
=
self
.
mode
)
f_ntn
=
theano
.
function
([],
[],
f_ntn
=
theano
.
function
(
[],
[],
updates
=
[(
a
,
(
l
*
a
+
tensor
.
dot
(
b_t
.
T
,
c
)))],
mode
=
self
.
mode
)
f_ntt
=
theano
.
function
([],
[],
f_ntt
=
theano
.
function
(
[],
[],
updates
=
[(
a
,
(
l
*
a
+
tensor
.
dot
(
b_t
.
T
,
c_t
.
T
)))],
mode
=
self
.
mode
)
f_tnn
=
theano
.
function
([],
[],
f_tnn
=
theano
.
function
(
[],
[],
updates
=
[(
a_t
,
(
l
*
a_t
+
tensor
.
dot
(
b
,
c
)
.
T
))],
mode
=
self
.
mode
)
f_tnt
=
theano
.
function
([],
[],
f_tnt
=
theano
.
function
(
[],
[],
updates
=
[(
a_t
,
(
l
*
a_t
+
tensor
.
dot
(
b
,
c_t
.
T
)
.
T
))],
mode
=
self
.
mode
)
f_ttn
=
theano
.
function
([],
[],
f_ttn
=
theano
.
function
(
[],
[],
updates
=
[(
a_t
,
(
l
*
a_t
+
tensor
.
dot
(
b_t
.
T
,
c
)
.
T
))],
mode
=
self
.
mode
)
f_ttt
=
theano
.
function
([],
[],
f_ttt
=
theano
.
function
(
[],
[],
updates
=
[(
a_t
,
(
l
*
a_t
+
tensor
.
dot
(
b_t
.
T
,
c_t
.
T
)
.
T
))],
mode
=
self
.
mode
)
...
...
@@ -1991,11 +1982,11 @@ class TestBlasStrides(TestCase):
c_t
.
set_value
(
ct_dev
.
copy
()[::
c_step2
,
::
c_step1
],
borrow
=
True
)
# Numpy results
a_n
=
(
l
*
av
[::
a_step1
,
::
a_step2
]
+
np
.
dot
(
bv
[::
b_step1
,
::
b_step2
],
a_n
=
(
l
*
av
[::
a_step1
,
::
a_step2
]
+
np
.
dot
(
bv
[::
b_step1
,
::
b_step2
],
cv
[::
c_step1
,
::
c_step2
]))
at_n
=
(
l
*
av
[::
a_step1
,
::
a_step2
]
.
T
+
np
.
dot
(
bv
[::
b_step1
,
::
b_step2
],
at_n
=
(
l
*
av
[::
a_step1
,
::
a_step2
]
.
T
+
np
.
dot
(
bv
[::
b_step1
,
::
b_step2
],
cv
[::
c_step1
,
::
c_step2
])
.
T
)
# a's value is updated, so we need to reinitialize it each time
...
...
@@ -2085,8 +2076,8 @@ class TestBlasStrides(TestCase):
borrow
=
True
)
c
.
set_value
(
c_dev
.
copy
()[::
c_step
],
borrow
=
True
)
a_n
=
(
av
[::
a_step
]
+
l
*
np
.
dot
(
bv
[::
b_step1
,
::
b_step2
],
a_n
=
(
av
[::
a_step
]
+
l
*
np
.
dot
(
bv
[::
b_step1
,
::
b_step2
],
cv
[::
c_step
]))
f_n
()
assert
np
.
allclose
(
a
.
get_value
(),
a_n
),
(
a
.
get_value
(),
a_n
)
...
...
@@ -2120,11 +2111,13 @@ class TestBlasStrides(TestCase):
b_dev
=
b
.
get_value
(
borrow
=
False
,
return_internal_type
=
True
)
c_dev
=
c
.
get_value
(
borrow
=
False
,
return_internal_type
=
True
)
f_n
=
theano
.
function
([],
[],
f_n
=
theano
.
function
(
[],
[],
updates
=
[(
a
,
(
a
+
l
*
tensor
.
outer
(
b
,
c
)))],
mode
=
self
.
mode
)
f_t
=
theano
.
function
([],
[],
f_t
=
theano
.
function
(
[],
[],
updates
=
[(
a_t
,
(
a_t
+
l
*
tensor
.
outer
(
b
,
c
)
.
T
))],
mode
=
self
.
mode
)
...
...
@@ -2141,15 +2134,14 @@ class TestBlasStrides(TestCase):
c
.
set_value
(
c_dev
.
copy
()[::
c_step
],
borrow
=
True
)
f_n
()
n_n
=
(
av
[::
a_step1
,
::
a_step2
]
+
l
*
np
.
outer
(
bv
[::
b_step
],
cv
[::
c_step
]))
n_n
=
(
av
[::
a_step1
,
::
a_step2
]
+
l
*
np
.
outer
(
bv
[::
b_step
],
cv
[::
c_step
]))
assert
np
.
allclose
(
a
.
get_value
(),
n_n
),
(
a
.
get_value
(),
n_n
)
f_t
()
n_t
=
(
av
.
T
[::
a_step1
,
::
a_step2
]
+
l
*
np
.
outer
(
bv
[::
b_step
],
cv
[::
c_step
])
.
T
)
assert
np
.
allclose
(
a_t
.
get_value
(),
n_t
),
\
(
a_t
.
get_value
(),
n_t
)
n_t
=
(
av
.
T
[::
a_step1
,
::
a_step2
]
+
l
*
np
.
outer
(
bv
[::
b_step
],
cv
[::
c_step
])
.
T
)
assert
np
.
allclose
(
a_t
.
get_value
(),
n_t
),
(
a_t
.
get_value
(),
n_t
)
def
test_ger_strides
(
self
):
self
.
cmp_ger
((
3
,
5
),
3
,
5
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论