Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3343d912
提交
3343d912
authored
1月 27, 2017
作者:
ballasn
提交者:
GitHub
1月 27, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5459 from bscellier/import_numpy_misc
Update "import numpy" to "import numpy as np" (theano/misc)
上级
ccf6deb0
9c0a9989
隐藏空白字符变更
内嵌
并排
正在显示
14 个修改的文件
包含
94 行增加
和
96 行删除
+94
-96
check_blas.py
theano/misc/check_blas.py
+11
-11
check_multi_gpu.py
theano/misc/check_multi_gpu.py
+2
-2
latence_gpu_transfert.py
theano/misc/latence_gpu_transfert.py
+3
-3
may_share_memory.py
theano/misc/may_share_memory.py
+3
-3
pkl_utils.py
theano/misc/pkl_utils.py
+8
-8
pycuda_example.py
theano/misc/pycuda_example.py
+5
-5
safe_asarray.py
theano/misc/safe_asarray.py
+3
-3
test_cudamat_utils.py
theano/misc/tests/test_cudamat_utils.py
+5
-5
test_gnumpy_utils.py
theano/misc/tests/test_gnumpy_utils.py
+4
-5
test_may_share_memory.py
theano/misc/tests/test_may_share_memory.py
+3
-3
test_pkl_utils.py
theano/misc/tests/test_pkl_utils.py
+7
-8
test_pycuda_example.py
theano/misc/tests/test_pycuda_example.py
+12
-12
test_pycuda_theano_simple.py
theano/misc/tests/test_pycuda_theano_simple.py
+15
-15
test_pycuda_utils.py
theano/misc/tests/test_pycuda_utils.py
+13
-13
没有找到文件。
theano/misc/check_blas.py
浏览文件 @
3343d912
...
...
@@ -13,7 +13,7 @@ import time
from
optparse
import
OptionParser
import
subprocess
import
numpy
import
numpy
as
np
import
theano
import
theano.tensor
as
T
...
...
@@ -47,10 +47,10 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000,
print
()
print
(
'Numpy config: (used when the Theano flag'
' "blas.ldflags" is empty)'
)
n
umpy
.
show_config
()
print
(
'Numpy dot module:'
,
n
umpy
.
dot
.
__module__
)
print
(
'Numpy location:'
,
n
umpy
.
__file__
)
print
(
'Numpy version:'
,
n
umpy
.
__version__
)
n
p
.
show_config
()
print
(
'Numpy dot module:'
,
n
p
.
dot
.
__module__
)
print
(
'Numpy location:'
,
n
p
.
__file__
)
print
(
'Numpy version:'
,
n
p
.
__version__
)
if
(
theano
.
config
.
device
.
startswith
(
"gpu"
)
or
theano
.
config
.
init_gpu_device
.
startswith
(
"gpu"
)):
print
(
'nvcc version:'
)
...
...
@@ -58,12 +58,12 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000,
"--version"
))
print
()
a
=
theano
.
shared
(
n
umpy
.
ones
((
M
,
N
),
dtype
=
theano
.
config
.
floatX
,
order
=
order
))
b
=
theano
.
shared
(
n
umpy
.
ones
((
N
,
K
),
dtype
=
theano
.
config
.
floatX
,
order
=
order
))
c
=
theano
.
shared
(
n
umpy
.
ones
((
M
,
K
),
dtype
=
theano
.
config
.
floatX
,
order
=
order
))
a
=
theano
.
shared
(
n
p
.
ones
((
M
,
N
),
dtype
=
theano
.
config
.
floatX
,
order
=
order
))
b
=
theano
.
shared
(
n
p
.
ones
((
N
,
K
),
dtype
=
theano
.
config
.
floatX
,
order
=
order
))
c
=
theano
.
shared
(
n
p
.
ones
((
M
,
K
),
dtype
=
theano
.
config
.
floatX
,
order
=
order
))
f
=
theano
.
function
([],
updates
=
[(
c
,
0.4
*
c
+
.
8
*
T
.
dot
(
a
,
b
))])
if
any
([
x
.
op
.
__class__
.
__name__
==
'Gemm'
for
x
in
...
...
theano/misc/check_multi_gpu.py
浏览文件 @
3343d912
...
...
@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
import
threading
import
time
import
numpy
import
numpy
as
np
import
theano
from
theano.gpuarray
import
init_dev
...
...
@@ -21,7 +21,7 @@ def main(dev1, dev2):
init_dev
(
dev2
,
'ctx2'
)
size
=
1024
*
16
data
=
n
umpy
.
random
.
randn
(
size
,
size
)
.
astype
(
'float32'
)
data
=
n
p
.
random
.
randn
(
size
,
size
)
.
astype
(
'float32'
)
val1a
=
theano
.
shared
(
data
,
target
=
'ctx1'
)
val1b
=
theano
.
shared
(
data
,
target
=
'ctx1'
)
val1c
=
theano
.
shared
(
data
,
target
=
'ctx1'
)
...
...
theano/misc/latence_gpu_transfert.py
浏览文件 @
3343d912
...
...
@@ -2,18 +2,18 @@ from __future__ import absolute_import, print_function, division
import
time
import
numpy
import
numpy
as
np
import
theano
y
=
theano
.
tensor
.
fvector
()
x
=
theano
.
shared
(
n
umpy
.
zeros
(
1
,
dtype
=
'float32'
))
x
=
theano
.
shared
(
n
p
.
zeros
(
1
,
dtype
=
'float32'
))
f1
=
theano
.
function
([
y
],
updates
=
{
x
:
y
})
f2
=
theano
.
function
([],
theano
.
sandbox
.
cuda
.
host_from_gpu
(
x
))
print
(
f1
.
maker
.
fgraph
.
toposort
())
print
(
f2
.
maker
.
fgraph
.
toposort
())
for
i
in
[
1
,
10
,
100
,
1000
,
10000
,
100000
,
1000000
,
10000000
]:
o
=
n
umpy
.
zeros
(
i
,
dtype
=
'float32'
)
o
=
n
p
.
zeros
(
i
,
dtype
=
'float32'
)
t0
=
time
.
time
()
f1
(
o
)
t1
=
time
.
time
()
...
...
theano/misc/may_share_memory.py
浏览文件 @
3343d912
...
...
@@ -4,7 +4,7 @@ numpy version support only ndarray.
"""
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
theano.tensor.basic
import
TensorType
try
:
...
...
@@ -42,8 +42,8 @@ else:
def
may_share_memory
(
a
,
b
,
raise_other_type
=
True
):
a_ndarray
=
isinstance
(
a
,
n
umpy
.
ndarray
)
b_ndarray
=
isinstance
(
b
,
n
umpy
.
ndarray
)
a_ndarray
=
isinstance
(
a
,
n
p
.
ndarray
)
b_ndarray
=
isinstance
(
b
,
n
p
.
ndarray
)
if
a_ndarray
and
b_ndarray
:
return
TensorType
.
may_share_memory
(
a
,
b
)
a_cuda
=
_is_cuda
(
a
)
...
...
theano/misc/pkl_utils.py
浏览文件 @
3343d912
...
...
@@ -5,7 +5,7 @@ These pickled graphs can be used, for instance, as cases for
unit tests or regression tests.
"""
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
os
import
pickle
import
sys
...
...
@@ -188,10 +188,10 @@ class PersistentNdarrayID(object):
return
name
def
__call__
(
self
,
obj
):
if
type
(
obj
)
is
n
umpy
.
ndarray
:
if
type
(
obj
)
is
n
p
.
ndarray
:
if
id
(
obj
)
not
in
self
.
seen
:
def
write_array
(
f
):
n
umpy
.
lib
.
format
.
write_array
(
f
,
obj
)
n
p
.
lib
.
format
.
write_array
(
f
,
obj
)
name
=
self
.
_resolve_name
(
obj
)
zipadd
(
write_array
,
self
.
zip_file
,
name
)
self
.
seen
[
id
(
obj
)]
=
'ndarray.{0}'
.
format
(
name
)
...
...
@@ -204,7 +204,7 @@ class PersistentCudaNdarrayID(PersistentNdarrayID):
type
(
obj
)
is
cuda_ndarray
.
cuda_ndarray
.
CudaNdarray
):
if
id
(
obj
)
not
in
self
.
seen
:
def
write_array
(
f
):
n
umpy
.
lib
.
format
.
write_array
(
f
,
numpy
.
asarray
(
obj
))
n
p
.
lib
.
format
.
write_array
(
f
,
np
.
asarray
(
obj
))
name
=
self
.
_resolve_name
(
obj
)
zipadd
(
write_array
,
self
.
zip_file
,
name
)
self
.
seen
[
id
(
obj
)]
=
'cuda_ndarray.{0}'
.
format
(
name
)
...
...
@@ -283,7 +283,7 @@ class PersistentNdarrayLoad(object):
if
name
in
self
.
cache
:
return
self
.
cache
[
name
]
ret
=
None
array
=
n
umpy
.
lib
.
format
.
read_array
(
self
.
zip_file
.
open
(
name
))
array
=
n
p
.
lib
.
format
.
read_array
(
self
.
zip_file
.
open
(
name
))
if
array_type
==
'cuda_ndarray'
:
if
config
.
experimental
.
unpickle_gpu_on_cpu
:
# directly return numpy array
...
...
@@ -335,10 +335,10 @@ def dump(obj, file_handler, protocol=DEFAULT_PROTOCOL,
>>> foo_1 = theano.shared(0, name='foo')
>>> foo_2 = theano.shared(1, name='foo')
>>> with open('model.zip', 'wb') as f:
... dump((foo_1, foo_2, n
umpy
.array(2)), f)
>>> n
umpy
.load('model.zip').keys()
... dump((foo_1, foo_2, n
p
.array(2)), f)
>>> n
p
.load('model.zip').keys()
['foo', 'foo_2', 'array_0', 'pkl']
>>> n
umpy
.load('model.zip')['foo']
>>> n
p
.load('model.zip')['foo']
array(0)
>>> with open('model.zip', 'rb') as f:
... foo_1, foo_2, array = load(f)
...
...
theano/misc/pycuda_example.py
浏览文件 @
3343d912
...
...
@@ -22,7 +22,7 @@ TheanoElementwiseKernel.
from
__future__
import
absolute_import
,
print_function
,
division
from
itertools
import
chain
import
numpy
import
numpy
as
np
import
theano
from
six.moves
import
xrange
...
...
@@ -257,13 +257,13 @@ class PycudaElemwiseSourceModuleOp(GpuOp):
" inputs don't have the same shape!"
)
if
inputs
[
0
]
.
size
>
512
:
grid
=
(
int
(
n
umpy
.
ceil
(
inputs
[
0
]
.
size
/
512.
)),
1
)
grid
=
(
int
(
n
p
.
ceil
(
inputs
[
0
]
.
size
/
512.
)),
1
)
block
=
(
512
,
1
,
1
)
else
:
grid
=
(
1
,
1
)
block
=
(
inputs
[
0
]
.
shape
[
0
],
inputs
[
0
]
.
shape
[
1
],
1
)
self
.
pycuda_fct
(
inputs
[
0
],
inputs
[
1
],
z
[
0
],
n
umpy
.
intc
(
inputs
[
1
]
.
size
),
block
=
block
,
grid
=
grid
)
n
p
.
intc
(
inputs
[
1
]
.
size
),
block
=
block
,
grid
=
grid
)
class
PycudaElemwiseSourceModuleMakeThunkOp
(
Op
):
...
...
@@ -349,13 +349,13 @@ class PycudaElemwiseSourceModuleMakeThunkOp(Op):
" inputs don't have the same shape!"
)
if
inputs
[
0
][
0
]
.
size
>
512
:
grid
=
(
int
(
n
umpy
.
ceil
(
inputs
[
0
][
0
]
.
size
/
512.
)),
1
)
grid
=
(
int
(
n
p
.
ceil
(
inputs
[
0
][
0
]
.
size
/
512.
)),
1
)
block
=
(
512
,
1
,
1
)
else
:
grid
=
(
1
,
1
)
block
=
(
inputs
[
0
][
0
]
.
shape
[
0
],
inputs
[
0
][
0
]
.
shape
[
1
],
1
)
pycuda_fct
(
inputs
[
0
][
0
],
inputs
[
1
][
0
],
z
[
0
],
n
umpy
.
intc
(
inputs
[
1
][
0
]
.
size
),
block
=
block
,
n
p
.
intc
(
inputs
[
1
][
0
]
.
size
),
block
=
block
,
grid
=
grid
)
thunk
.
inputs
=
inputs
thunk
.
outputs
=
outputs
...
...
theano/misc/safe_asarray.py
浏览文件 @
3343d912
...
...
@@ -3,7 +3,7 @@ Helper function to safely convert an array to a new data type.
"""
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
...
...
@@ -30,8 +30,8 @@ def _asarray(a, dtype, order=None):
"""
if
str
(
dtype
)
==
'floatX'
:
dtype
=
theano
.
config
.
floatX
dtype
=
n
umpy
.
dtype
(
dtype
)
# Convert into dtype object.
rval
=
n
umpy
.
asarray
(
a
,
dtype
=
dtype
,
order
=
order
)
dtype
=
n
p
.
dtype
(
dtype
)
# Convert into dtype object.
rval
=
n
p
.
asarray
(
a
,
dtype
=
dtype
,
order
=
order
)
# Note that dtype comparison must be done by comparing their `num`
# attribute. One cannot assume that two identical data types are pointers
# towards the same object (e.g. under Windows this appears not to be the
...
...
theano/misc/tests/test_cudamat_utils.py
浏览文件 @
3343d912
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
from
theano.misc.cudamat_utils
import
cudamat_available
...
...
@@ -20,7 +20,7 @@ def test(shape=(3, 4)):
U
=
gpu
(
theano
.
tensor
.
fmatrix
(
'U'
))
ii
=
theano
.
function
([
U
],
gpu
(
U
+
1
))
A_cpu
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
"float32"
)
A_cpu
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
"float32"
)
A_cnd
=
theano
.
sandbox
.
cuda
.
CudaNdarray
(
A_cpu
)
A_cmat
=
cudandarray_to_cudamat
(
A_cnd
)
...
...
@@ -28,9 +28,9 @@ def test(shape=(3, 4)):
B_cnd
=
ii
(
A_cnd
)
u
=
A_cnd
.
copy
()
u
+=
theano
.
sandbox
.
cuda
.
CudaNdarray
(
n
umpy
.
asarray
([[
1
]],
dtype
=
'float32'
))
u
=
n
umpy
.
asarray
(
u
)
v
=
n
umpy
.
asarray
(
B_cnd
)
u
+=
theano
.
sandbox
.
cuda
.
CudaNdarray
(
n
p
.
asarray
([[
1
]],
dtype
=
'float32'
))
u
=
n
p
.
asarray
(
u
)
v
=
n
p
.
asarray
(
B_cnd
)
w
=
A_cmat
.
add
(
1
)
.
asarray
()
assert
abs
(
u
-
v
)
.
max
()
==
0
...
...
theano/misc/tests/test_gnumpy_utils.py
浏览文件 @
3343d912
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
from
theano.misc.gnumpy_utils
import
gnumpy_available
...
...
@@ -31,11 +31,10 @@ def test(shape=(3, 4, 5)):
B_cnd
=
ii
(
A_cnd
)
B
=
cudandarray_to_garray
(
B_cnd
)
assert
A_cnd
.
shape
==
A
.
shape
from
numpy
import
array
u
=
(
A
+
1
)
.
asarray
()
v
=
B
.
asarray
()
w
=
array
(
B_cnd
)
w
=
np
.
array
(
B_cnd
)
assert
(
u
==
v
)
.
all
()
assert
(
u
==
w
)
.
all
()
...
...
@@ -49,7 +48,7 @@ def test2(shape=(3, 4, 5)):
U
=
gpu
(
theano
.
tensor
.
ftensor3
(
'U'
))
theano
.
function
([
U
],
gpu
(
U
+
1
))
A
=
n
umpy
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
A
=
n
p
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
A_cnd
=
theano
.
sandbox
.
cuda
.
CudaNdarray
(
A
)
A_gar
=
cudandarray_to_garray
(
A_cnd
)
assert
A_cnd
.
shape
==
A_gar
.
shape
...
...
@@ -62,7 +61,7 @@ def test2(shape=(3, 4, 5)):
# dtype always float32
assert
A_cnd
.
_strides
==
B
.
_strides
assert
A_cnd
.
gpudata
==
B
.
gpudata
v
=
n
umpy
.
asarray
(
B
)
v
=
n
p
.
asarray
(
B
)
assert
(
v
==
A
)
.
all
()
...
...
theano/misc/tests/test_may_share_memory.py
浏览文件 @
3343d912
...
...
@@ -3,7 +3,7 @@ test the tensor and sparse type. The CudaNdarray type is tested in
sandbox/cuda/tests/test_tensor_op.py.test_may_share_memory_cuda
"""
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
try
:
...
...
@@ -16,8 +16,8 @@ from theano.misc.may_share_memory import may_share_memory
def
test_may_share_memory
():
a
=
n
umpy
.
random
.
rand
(
5
,
4
)
b
=
n
umpy
.
random
.
rand
(
5
,
4
)
a
=
n
p
.
random
.
rand
(
5
,
4
)
b
=
n
p
.
random
.
rand
(
5
,
4
)
va
=
a
.
view
()
vb
=
b
.
view
()
ra
=
a
.
reshape
((
4
,
5
))
...
...
theano/misc/tests/test_pkl_utils.py
浏览文件 @
3343d912
...
...
@@ -4,8 +4,7 @@ import shutil
import
unittest
from
tempfile
import
mkdtemp
import
numpy
from
numpy.testing
import
assert_allclose
import
numpy
as
np
from
nose.plugins.skip
import
SkipTest
import
theano
...
...
@@ -44,7 +43,7 @@ class T_dump_load(unittest.TestCase):
x
=
load
(
f
)
assert
x
.
name
==
'x'
assert_allclose
(
x
.
get_value
(),
[[
1
]])
np
.
testing
.
assert_allclose
(
x
.
get_value
(),
[[
1
]])
def
test_dump_load_mrg
(
self
):
rng
=
MRG_RandomStreams
(
use_cuda
=
cuda_ndarray
.
cuda_enabled
)
...
...
@@ -62,14 +61,14 @@ class T_dump_load(unittest.TestCase):
foo_2
=
theano
.
shared
(
1
,
name
=
'foo'
)
foo_3
=
theano
.
shared
(
2
,
name
=
'foo'
)
with
open
(
'model.zip'
,
'wb'
)
as
f
:
dump
((
foo_1
,
foo_2
,
foo_3
,
n
umpy
.
array
(
3
)),
f
)
keys
=
list
(
n
umpy
.
load
(
'model.zip'
)
.
keys
())
dump
((
foo_1
,
foo_2
,
foo_3
,
n
p
.
array
(
3
)),
f
)
keys
=
list
(
n
p
.
load
(
'model.zip'
)
.
keys
())
assert
keys
==
[
'foo'
,
'foo_2'
,
'foo_3'
,
'array_0'
,
'pkl'
]
foo_3
=
n
umpy
.
load
(
'model.zip'
)[
'foo_3'
]
assert
foo_3
==
n
umpy
.
array
(
2
)
foo_3
=
n
p
.
load
(
'model.zip'
)[
'foo_3'
]
assert
foo_3
==
n
p
.
array
(
2
)
with
open
(
'model.zip'
,
'rb'
)
as
f
:
foo_1
,
foo_2
,
foo_3
,
array
=
load
(
f
)
assert
array
==
n
umpy
.
array
(
3
)
assert
array
==
n
p
.
array
(
3
)
class
TestStripPickler
(
unittest
.
TestCase
):
...
...
theano/misc/tests/test_pycuda_example.py
浏览文件 @
3343d912
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
import
theano.misc.pycuda_init
...
...
@@ -58,11 +58,11 @@ def test_pycuda_elemwise_source_module():
PycudaElemwiseSourceModuleMakeThunkOp
)
for
node
in
f4
.
maker
.
fgraph
.
toposort
()])
val1
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
)
val2
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
)
assert
n
umpy
.
allclose
(
f
(
val1
,
val2
),
f2
(
val1
,
val2
))
assert
n
umpy
.
allclose
(
f
(
val1
,
val2
),
f3
(
val1
,
val2
))
assert
n
umpy
.
allclose
(
f
(
val1
,
val2
),
f4
(
val1
,
val2
))
val1
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
)
val2
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
)
assert
n
p
.
allclose
(
f
(
val1
,
val2
),
f2
(
val1
,
val2
))
assert
n
p
.
allclose
(
f
(
val1
,
val2
),
f3
(
val1
,
val2
))
assert
n
p
.
allclose
(
f
(
val1
,
val2
),
f4
(
val1
,
val2
))
# print f(val1,val2)
# print f2(val1,val2)
...
...
@@ -82,10 +82,10 @@ def test_pycuda_elemwise_kernel():
assert any([isinstance(node.op, PycudaElemwiseKernelOp)
for node in f2.maker.fgraph.toposort()])
val1 = n
umpy.asarray(numpy
.random.rand(5, 5), dtype='float32')
val2 = n
umpy.asarray(numpy
.random.rand(5, 5), dtype='float32')
#val1 = n
umpy
.ones((5,5))
#val2 = n
umpy
.arange(25).reshape(5,5)
val1 = n
p.asarray(np
.random.rand(5, 5), dtype='float32')
val2 = n
p.asarray(np
.random.rand(5, 5), dtype='float32')
#val1 = n
p
.ones((5,5))
#val2 = n
p
.arange(25).reshape(5,5)
assert (f(val1, val2) == f2(val1, val2)).all()
print(f(val1, val2))
print(f2(val1, val2))
...
...
@@ -99,8 +99,8 @@ def test_pycuda_elemwise_kernel():
assert any([isinstance(node.op, PycudaElemwiseKernelOp)
for node in f4.maker.fgraph.toposort()])
val1 = n
umpy
.random.rand(2, 2, 2)
val1 = n
p
.random.rand(2, 2, 2)
print(val1)
print(f4(val1, val1, val1))
assert n
umpy
.allclose(f4(val1, val1, val1), val1 * val1 + val1)
assert n
p
.allclose(f4(val1, val1, val1), val1 * val1 + val1)
"""
theano/misc/tests/test_pycuda_theano_simple.py
浏览文件 @
3343d912
...
...
@@ -8,7 +8,7 @@ from __future__ import absolute_import, print_function, division
import
sys
import
numpy
import
numpy
as
np
import
theano
import
theano.sandbox.cuda
as
cuda_ndarray
...
...
@@ -42,9 +42,9 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them
=
mod
.
get_function
(
"multiply_them"
)
# Test with pycuda in/out of numpy.ndarray
a
=
n
umpy
.
random
.
randn
(
100
)
.
astype
(
numpy
.
float32
)
b
=
n
umpy
.
random
.
randn
(
100
)
.
astype
(
numpy
.
float32
)
dest
=
n
umpy
.
zeros_like
(
a
)
a
=
n
p
.
random
.
randn
(
100
)
.
astype
(
np
.
float32
)
b
=
n
p
.
random
.
randn
(
100
)
.
astype
(
np
.
float32
)
dest
=
n
p
.
zeros_like
(
a
)
multiply_them
(
drv
.
Out
(
dest
),
drv
.
In
(
a
),
drv
.
In
(
b
),
block
=
(
400
,
1
,
1
),
grid
=
(
1
,
1
))
...
...
@@ -64,8 +64,8 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them
=
mod
.
get_function
(
"multiply_them"
)
a
=
n
umpy
.
random
.
randn
(
100
)
.
astype
(
numpy
.
float32
)
b
=
n
umpy
.
random
.
randn
(
100
)
.
astype
(
numpy
.
float32
)
a
=
n
p
.
random
.
randn
(
100
)
.
astype
(
np
.
float32
)
b
=
n
p
.
random
.
randn
(
100
)
.
astype
(
np
.
float32
)
# Test with Theano object
ga
=
cuda_ndarray
.
CudaNdarray
(
a
)
...
...
@@ -73,7 +73,7 @@ __global__ void multiply_them(float *dest, float *a, float *b)
dest
=
cuda_ndarray
.
CudaNdarray
.
zeros
(
a
.
shape
)
multiply_them
(
dest
,
ga
,
gb
,
block
=
(
400
,
1
,
1
),
grid
=
(
1
,
1
))
assert
(
n
umpy
.
asarray
(
dest
)
==
a
*
b
)
.
all
()
assert
(
n
p
.
asarray
(
dest
)
==
a
*
b
)
.
all
()
def
test_pycuda_memory_to_theano
():
...
...
@@ -87,7 +87,7 @@ def test_pycuda_memory_to_theano():
print
(
"gpuarray ref count before creating a CudaNdarray"
,
end
=
' '
)
print
(
sys
.
getrefcount
(
y
))
assert
sys
.
getrefcount
(
y
)
==
initial_refcount
rand
=
n
umpy
.
random
.
randn
(
*
y
.
shape
)
.
astype
(
numpy
.
float32
)
rand
=
n
p
.
random
.
randn
(
*
y
.
shape
)
.
astype
(
np
.
float32
)
cuda_rand
=
cuda_ndarray
.
CudaNdarray
(
rand
)
strides
=
[
1
]
...
...
@@ -102,7 +102,7 @@ def test_pycuda_memory_to_theano():
z
=
cuda_ndarray
.
from_gpu_pointer
(
y_ptr
,
y
.
shape
,
strides
,
y
)
print
(
"gpuarray ref count after creating a CudaNdarray"
,
sys
.
getrefcount
(
y
))
assert
sys
.
getrefcount
(
y
)
==
initial_refcount
+
1
assert
(
n
umpy
.
asarray
(
z
)
==
0
)
.
all
()
assert
(
n
p
.
asarray
(
z
)
==
0
)
.
all
()
assert
z
.
base
is
y
# Test that we can take a view from this cuda view on pycuda memory
...
...
@@ -112,17 +112,17 @@ def test_pycuda_memory_to_theano():
del
zz
assert
sys
.
getrefcount
(
y
)
==
initial_refcount
+
1
cuda_ones
=
cuda_ndarray
.
CudaNdarray
(
n
umpy
.
asarray
([[[
1
]]],
dtype
=
'float32'
))
cuda_ones
=
cuda_ndarray
.
CudaNdarray
(
n
p
.
asarray
([[[
1
]]],
dtype
=
'float32'
))
z
+=
cuda_ones
assert
(
n
umpy
.
asarray
(
z
)
==
numpy
.
ones
(
y
.
shape
))
.
all
()
assert
(
n
umpy
.
asarray
(
z
)
==
1
)
.
all
()
assert
(
n
p
.
asarray
(
z
)
==
np
.
ones
(
y
.
shape
))
.
all
()
assert
(
n
p
.
asarray
(
z
)
==
1
)
.
all
()
assert
cuda_rand
.
shape
==
z
.
shape
assert
cuda_rand
.
_strides
==
z
.
_strides
,
(
cuda_rand
.
_strides
,
z
.
_strides
)
assert
(
n
umpy
.
asarray
(
cuda_rand
)
==
rand
)
.
all
()
assert
(
n
p
.
asarray
(
cuda_rand
)
==
rand
)
.
all
()
z
+=
cuda_rand
assert
(
n
umpy
.
asarray
(
z
)
==
(
rand
+
1
))
.
all
()
assert
(
n
p
.
asarray
(
z
)
==
(
rand
+
1
))
.
all
()
# Check that the ref count to the gpuarray is right.
del
z
...
...
theano/misc/tests/test_pycuda_utils.py
浏览文件 @
3343d912
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano.sandbox.cuda
as
cuda
import
theano.misc.pycuda_init
...
...
@@ -22,30 +22,30 @@ def test_to_gpuarray():
px
=
to_gpuarray
(
cx
)
assert
isinstance
(
px
,
pycuda
.
gpuarray
.
GPUArray
)
cx
[
0
,
0
]
=
n
umpy
.
asarray
(
1
,
dtype
=
"float32"
)
cx
[
0
,
0
]
=
n
p
.
asarray
(
1
,
dtype
=
"float32"
)
# Check that they share the same memory space
assert
px
.
gpudata
==
cx
.
gpudata
assert
n
umpy
.
asarray
(
cx
[
0
,
0
])
==
1
assert
n
p
.
asarray
(
cx
[
0
,
0
])
==
1
assert
n
umpy
.
allclose
(
numpy
.
asarray
(
cx
),
px
.
get
())
assert
n
p
.
allclose
(
np
.
asarray
(
cx
),
px
.
get
())
assert
px
.
dtype
==
cx
.
dtype
assert
px
.
shape
==
cx
.
shape
assert
all
(
n
umpy
.
asarray
(
cx
.
_strides
)
*
4
==
px
.
strides
)
assert
all
(
n
p
.
asarray
(
cx
.
_strides
)
*
4
==
px
.
strides
)
# Test when the CudaNdarray is strided
cx
=
cx
[::
2
,
::]
px
=
to_gpuarray
(
cx
,
copyif
=
True
)
assert
isinstance
(
px
,
pycuda
.
gpuarray
.
GPUArray
)
cx
[
0
,
0
]
=
n
umpy
.
asarray
(
2
,
dtype
=
"float32"
)
cx
[
0
,
0
]
=
n
p
.
asarray
(
2
,
dtype
=
"float32"
)
# Check that they do not share the same memory space
assert
px
.
gpudata
!=
cx
.
gpudata
assert
n
umpy
.
asarray
(
cx
[
0
,
0
])
==
2
assert
not
n
umpy
.
allclose
(
numpy
.
asarray
(
cx
),
px
.
get
())
assert
n
p
.
asarray
(
cx
[
0
,
0
])
==
2
assert
not
n
p
.
allclose
(
np
.
asarray
(
cx
),
px
.
get
())
assert
px
.
dtype
==
cx
.
dtype
assert
px
.
shape
==
cx
.
shape
assert
not
all
(
n
umpy
.
asarray
(
cx
.
_strides
)
*
4
==
px
.
strides
)
assert
not
all
(
n
p
.
asarray
(
cx
.
_strides
)
*
4
==
px
.
strides
)
# Test that we return an error
try
:
...
...
@@ -59,11 +59,11 @@ def test_to_cudandarray():
px
=
pycuda
.
gpuarray
.
zeros
((
3
,
4
,
5
),
'float32'
)
cx
=
to_cudandarray
(
px
)
assert
isinstance
(
cx
,
cuda
.
CudaNdarray
)
assert
n
umpy
.
allclose
(
px
.
get
(),
numpy
.
asarray
(
cx
))
assert
n
p
.
allclose
(
px
.
get
(),
np
.
asarray
(
cx
))
assert
px
.
dtype
==
cx
.
dtype
assert
px
.
shape
==
cx
.
shape
assert
all
(
n
umpy
.
asarray
(
cx
.
_strides
)
*
4
==
px
.
strides
)
assert
all
(
n
p
.
asarray
(
cx
.
_strides
)
*
4
==
px
.
strides
)
try
:
px
=
pycuda
.
gpuarray
.
zeros
((
3
,
4
,
5
),
'float64'
)
...
...
@@ -73,7 +73,7 @@ def test_to_cudandarray():
pass
try
:
to_cudandarray
(
n
umpy
.
zeros
(
4
))
to_cudandarray
(
n
p
.
zeros
(
4
))
assert
False
except
ValueError
:
pass
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论