提交 3343d912 authored 作者: ballasn's avatar ballasn 提交者: GitHub

Merge pull request #5459 from bscellier/import_numpy_misc

Update "import numpy" to "import numpy as np" (theano/misc)
...@@ -13,7 +13,7 @@ import time ...@@ -13,7 +13,7 @@ import time
from optparse import OptionParser from optparse import OptionParser
import subprocess import subprocess
import numpy import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as T
...@@ -47,10 +47,10 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000, ...@@ -47,10 +47,10 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000,
print() print()
print('Numpy config: (used when the Theano flag' print('Numpy config: (used when the Theano flag'
' "blas.ldflags" is empty)') ' "blas.ldflags" is empty)')
numpy.show_config() np.show_config()
print('Numpy dot module:', numpy.dot.__module__) print('Numpy dot module:', np.dot.__module__)
print('Numpy location:', numpy.__file__) print('Numpy location:', np.__file__)
print('Numpy version:', numpy.__version__) print('Numpy version:', np.__version__)
if (theano.config.device.startswith("gpu") or if (theano.config.device.startswith("gpu") or
theano.config.init_gpu_device.startswith("gpu")): theano.config.init_gpu_device.startswith("gpu")):
print('nvcc version:') print('nvcc version:')
...@@ -58,11 +58,11 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000, ...@@ -58,11 +58,11 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000,
"--version")) "--version"))
print() print()
a = theano.shared(numpy.ones((M, N), dtype=theano.config.floatX, a = theano.shared(np.ones((M, N), dtype=theano.config.floatX,
order=order)) order=order))
b = theano.shared(numpy.ones((N, K), dtype=theano.config.floatX, b = theano.shared(np.ones((N, K), dtype=theano.config.floatX,
order=order)) order=order))
c = theano.shared(numpy.ones((M, K), dtype=theano.config.floatX, c = theano.shared(np.ones((M, K), dtype=theano.config.floatX,
order=order)) order=order))
f = theano.function([], updates=[(c, 0.4 * c + .8 * T.dot(a, b))]) f = theano.function([], updates=[(c, 0.4 * c + .8 * T.dot(a, b))])
......
...@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division ...@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
import threading import threading
import time import time
import numpy import numpy as np
import theano import theano
from theano.gpuarray import init_dev from theano.gpuarray import init_dev
...@@ -21,7 +21,7 @@ def main(dev1, dev2): ...@@ -21,7 +21,7 @@ def main(dev1, dev2):
init_dev(dev2, 'ctx2') init_dev(dev2, 'ctx2')
size = 1024 * 16 size = 1024 * 16
data = numpy.random.randn(size, size).astype('float32') data = np.random.randn(size, size).astype('float32')
val1a = theano.shared(data, target='ctx1') val1a = theano.shared(data, target='ctx1')
val1b = theano.shared(data, target='ctx1') val1b = theano.shared(data, target='ctx1')
val1c = theano.shared(data, target='ctx1') val1c = theano.shared(data, target='ctx1')
......
...@@ -2,18 +2,18 @@ from __future__ import absolute_import, print_function, division ...@@ -2,18 +2,18 @@ from __future__ import absolute_import, print_function, division
import time import time
import numpy import numpy as np
import theano import theano
y = theano.tensor.fvector() y = theano.tensor.fvector()
x = theano.shared(numpy.zeros(1, dtype='float32')) x = theano.shared(np.zeros(1, dtype='float32'))
f1 = theano.function([y], updates={x: y}) f1 = theano.function([y], updates={x: y})
f2 = theano.function([], theano.sandbox.cuda.host_from_gpu(x)) f2 = theano.function([], theano.sandbox.cuda.host_from_gpu(x))
print(f1.maker.fgraph.toposort()) print(f1.maker.fgraph.toposort())
print(f2.maker.fgraph.toposort()) print(f2.maker.fgraph.toposort())
for i in [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000]: for i in [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000]:
o = numpy.zeros(i, dtype='float32') o = np.zeros(i, dtype='float32')
t0 = time.time() t0 = time.time()
f1(o) f1(o)
t1 = time.time() t1 = time.time()
......
...@@ -4,7 +4,7 @@ numpy version support only ndarray. ...@@ -4,7 +4,7 @@ numpy version support only ndarray.
""" """
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
from theano.tensor.basic import TensorType from theano.tensor.basic import TensorType
try: try:
...@@ -42,8 +42,8 @@ else: ...@@ -42,8 +42,8 @@ else:
def may_share_memory(a, b, raise_other_type=True): def may_share_memory(a, b, raise_other_type=True):
a_ndarray = isinstance(a, numpy.ndarray) a_ndarray = isinstance(a, np.ndarray)
b_ndarray = isinstance(b, numpy.ndarray) b_ndarray = isinstance(b, np.ndarray)
if a_ndarray and b_ndarray: if a_ndarray and b_ndarray:
return TensorType.may_share_memory(a, b) return TensorType.may_share_memory(a, b)
a_cuda = _is_cuda(a) a_cuda = _is_cuda(a)
......
...@@ -5,7 +5,7 @@ These pickled graphs can be used, for instance, as cases for ...@@ -5,7 +5,7 @@ These pickled graphs can be used, for instance, as cases for
unit tests or regression tests. unit tests or regression tests.
""" """
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import os import os
import pickle import pickle
import sys import sys
...@@ -188,10 +188,10 @@ class PersistentNdarrayID(object): ...@@ -188,10 +188,10 @@ class PersistentNdarrayID(object):
return name return name
def __call__(self, obj): def __call__(self, obj):
if type(obj) is numpy.ndarray: if type(obj) is np.ndarray:
if id(obj) not in self.seen: if id(obj) not in self.seen:
def write_array(f): def write_array(f):
numpy.lib.format.write_array(f, obj) np.lib.format.write_array(f, obj)
name = self._resolve_name(obj) name = self._resolve_name(obj)
zipadd(write_array, self.zip_file, name) zipadd(write_array, self.zip_file, name)
self.seen[id(obj)] = 'ndarray.{0}'.format(name) self.seen[id(obj)] = 'ndarray.{0}'.format(name)
...@@ -204,7 +204,7 @@ class PersistentCudaNdarrayID(PersistentNdarrayID): ...@@ -204,7 +204,7 @@ class PersistentCudaNdarrayID(PersistentNdarrayID):
type(obj) is cuda_ndarray.cuda_ndarray.CudaNdarray): type(obj) is cuda_ndarray.cuda_ndarray.CudaNdarray):
if id(obj) not in self.seen: if id(obj) not in self.seen:
def write_array(f): def write_array(f):
numpy.lib.format.write_array(f, numpy.asarray(obj)) np.lib.format.write_array(f, np.asarray(obj))
name = self._resolve_name(obj) name = self._resolve_name(obj)
zipadd(write_array, self.zip_file, name) zipadd(write_array, self.zip_file, name)
self.seen[id(obj)] = 'cuda_ndarray.{0}'.format(name) self.seen[id(obj)] = 'cuda_ndarray.{0}'.format(name)
...@@ -283,7 +283,7 @@ class PersistentNdarrayLoad(object): ...@@ -283,7 +283,7 @@ class PersistentNdarrayLoad(object):
if name in self.cache: if name in self.cache:
return self.cache[name] return self.cache[name]
ret = None ret = None
array = numpy.lib.format.read_array(self.zip_file.open(name)) array = np.lib.format.read_array(self.zip_file.open(name))
if array_type == 'cuda_ndarray': if array_type == 'cuda_ndarray':
if config.experimental.unpickle_gpu_on_cpu: if config.experimental.unpickle_gpu_on_cpu:
# directly return numpy array # directly return numpy array
...@@ -335,10 +335,10 @@ def dump(obj, file_handler, protocol=DEFAULT_PROTOCOL, ...@@ -335,10 +335,10 @@ def dump(obj, file_handler, protocol=DEFAULT_PROTOCOL,
>>> foo_1 = theano.shared(0, name='foo') >>> foo_1 = theano.shared(0, name='foo')
>>> foo_2 = theano.shared(1, name='foo') >>> foo_2 = theano.shared(1, name='foo')
>>> with open('model.zip', 'wb') as f: >>> with open('model.zip', 'wb') as f:
... dump((foo_1, foo_2, numpy.array(2)), f) ... dump((foo_1, foo_2, np.array(2)), f)
>>> numpy.load('model.zip').keys() >>> np.load('model.zip').keys()
['foo', 'foo_2', 'array_0', 'pkl'] ['foo', 'foo_2', 'array_0', 'pkl']
>>> numpy.load('model.zip')['foo'] >>> np.load('model.zip')['foo']
array(0) array(0)
>>> with open('model.zip', 'rb') as f: >>> with open('model.zip', 'rb') as f:
... foo_1, foo_2, array = load(f) ... foo_1, foo_2, array = load(f)
......
...@@ -22,7 +22,7 @@ TheanoElementwiseKernel. ...@@ -22,7 +22,7 @@ TheanoElementwiseKernel.
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
from itertools import chain from itertools import chain
import numpy import numpy as np
import theano import theano
from six.moves import xrange from six.moves import xrange
...@@ -257,13 +257,13 @@ class PycudaElemwiseSourceModuleOp(GpuOp): ...@@ -257,13 +257,13 @@ class PycudaElemwiseSourceModuleOp(GpuOp):
" inputs don't have the same shape!") " inputs don't have the same shape!")
if inputs[0].size > 512: if inputs[0].size > 512:
grid = (int(numpy.ceil(inputs[0].size / 512.)), 1) grid = (int(np.ceil(inputs[0].size / 512.)), 1)
block = (512, 1, 1) block = (512, 1, 1)
else: else:
grid = (1, 1) grid = (1, 1)
block = (inputs[0].shape[0], inputs[0].shape[1], 1) block = (inputs[0].shape[0], inputs[0].shape[1], 1)
self.pycuda_fct(inputs[0], inputs[1], z[0], self.pycuda_fct(inputs[0], inputs[1], z[0],
numpy.intc(inputs[1].size), block=block, grid=grid) np.intc(inputs[1].size), block=block, grid=grid)
class PycudaElemwiseSourceModuleMakeThunkOp(Op): class PycudaElemwiseSourceModuleMakeThunkOp(Op):
...@@ -349,13 +349,13 @@ class PycudaElemwiseSourceModuleMakeThunkOp(Op): ...@@ -349,13 +349,13 @@ class PycudaElemwiseSourceModuleMakeThunkOp(Op):
" inputs don't have the same shape!") " inputs don't have the same shape!")
if inputs[0][0].size > 512: if inputs[0][0].size > 512:
grid = (int(numpy.ceil(inputs[0][0].size / 512.)), 1) grid = (int(np.ceil(inputs[0][0].size / 512.)), 1)
block = (512, 1, 1) block = (512, 1, 1)
else: else:
grid = (1, 1) grid = (1, 1)
block = (inputs[0][0].shape[0], inputs[0][0].shape[1], 1) block = (inputs[0][0].shape[0], inputs[0][0].shape[1], 1)
pycuda_fct(inputs[0][0], inputs[1][0], z[0], pycuda_fct(inputs[0][0], inputs[1][0], z[0],
numpy.intc(inputs[1][0].size), block=block, np.intc(inputs[1][0].size), block=block,
grid=grid) grid=grid)
thunk.inputs = inputs thunk.inputs = inputs
thunk.outputs = outputs thunk.outputs = outputs
......
...@@ -3,7 +3,7 @@ Helper function to safely convert an array to a new data type. ...@@ -3,7 +3,7 @@ Helper function to safely convert an array to a new data type.
""" """
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
...@@ -30,8 +30,8 @@ def _asarray(a, dtype, order=None): ...@@ -30,8 +30,8 @@ def _asarray(a, dtype, order=None):
""" """
if str(dtype) == 'floatX': if str(dtype) == 'floatX':
dtype = theano.config.floatX dtype = theano.config.floatX
dtype = numpy.dtype(dtype) # Convert into dtype object. dtype = np.dtype(dtype) # Convert into dtype object.
rval = numpy.asarray(a, dtype=dtype, order=order) rval = np.asarray(a, dtype=dtype, order=order)
# Note that dtype comparison must be done by comparing their `num` # Note that dtype comparison must be done by comparing their `num`
# attribute. One cannot assume that two identical data types are pointers # 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 # towards the same object (e.g. under Windows this appears not to be the
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
from theano.misc.cudamat_utils import cudamat_available from theano.misc.cudamat_utils import cudamat_available
...@@ -20,7 +20,7 @@ def test(shape=(3, 4)): ...@@ -20,7 +20,7 @@ def test(shape=(3, 4)):
U = gpu(theano.tensor.fmatrix('U')) U = gpu(theano.tensor.fmatrix('U'))
ii = theano.function([U], gpu(U + 1)) ii = theano.function([U], gpu(U + 1))
A_cpu = numpy.asarray(numpy.random.rand(*shape), dtype="float32") A_cpu = np.asarray(np.random.rand(*shape), dtype="float32")
A_cnd = theano.sandbox.cuda.CudaNdarray(A_cpu) A_cnd = theano.sandbox.cuda.CudaNdarray(A_cpu)
A_cmat = cudandarray_to_cudamat(A_cnd) A_cmat = cudandarray_to_cudamat(A_cnd)
...@@ -28,9 +28,9 @@ def test(shape=(3, 4)): ...@@ -28,9 +28,9 @@ def test(shape=(3, 4)):
B_cnd = ii(A_cnd) B_cnd = ii(A_cnd)
u = A_cnd.copy() u = A_cnd.copy()
u += theano.sandbox.cuda.CudaNdarray(numpy.asarray([[1]], dtype='float32')) u += theano.sandbox.cuda.CudaNdarray(np.asarray([[1]], dtype='float32'))
u = numpy.asarray(u) u = np.asarray(u)
v = numpy.asarray(B_cnd) v = np.asarray(B_cnd)
w = A_cmat.add(1).asarray() w = A_cmat.add(1).asarray()
assert abs(u - v).max() == 0 assert abs(u - v).max() == 0
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
from theano.misc.gnumpy_utils import gnumpy_available from theano.misc.gnumpy_utils import gnumpy_available
...@@ -31,11 +31,10 @@ def test(shape=(3, 4, 5)): ...@@ -31,11 +31,10 @@ def test(shape=(3, 4, 5)):
B_cnd = ii(A_cnd) B_cnd = ii(A_cnd)
B = cudandarray_to_garray(B_cnd) B = cudandarray_to_garray(B_cnd)
assert A_cnd.shape == A.shape assert A_cnd.shape == A.shape
from numpy import array
u = (A + 1).asarray() u = (A + 1).asarray()
v = B.asarray() v = B.asarray()
w = array(B_cnd) w = np.array(B_cnd)
assert (u == v).all() assert (u == v).all()
assert (u == w).all() assert (u == w).all()
...@@ -49,7 +48,7 @@ def test2(shape=(3, 4, 5)): ...@@ -49,7 +48,7 @@ def test2(shape=(3, 4, 5)):
U = gpu(theano.tensor.ftensor3('U')) U = gpu(theano.tensor.ftensor3('U'))
theano.function([U], gpu(U + 1)) theano.function([U], gpu(U + 1))
A = numpy.random.rand(*shape).astype('float32') A = np.random.rand(*shape).astype('float32')
A_cnd = theano.sandbox.cuda.CudaNdarray(A) A_cnd = theano.sandbox.cuda.CudaNdarray(A)
A_gar = cudandarray_to_garray(A_cnd) A_gar = cudandarray_to_garray(A_cnd)
assert A_cnd.shape == A_gar.shape assert A_cnd.shape == A_gar.shape
...@@ -62,7 +61,7 @@ def test2(shape=(3, 4, 5)): ...@@ -62,7 +61,7 @@ def test2(shape=(3, 4, 5)):
# dtype always float32 # dtype always float32
assert A_cnd._strides == B._strides assert A_cnd._strides == B._strides
assert A_cnd.gpudata == B.gpudata assert A_cnd.gpudata == B.gpudata
v = numpy.asarray(B) v = np.asarray(B)
assert (v == A).all() assert (v == A).all()
......
...@@ -3,7 +3,7 @@ test the tensor and sparse type. The CudaNdarray type is tested in ...@@ -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 sandbox/cuda/tests/test_tensor_op.py.test_may_share_memory_cuda
""" """
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
try: try:
...@@ -16,8 +16,8 @@ from theano.misc.may_share_memory import may_share_memory ...@@ -16,8 +16,8 @@ from theano.misc.may_share_memory import may_share_memory
def test_may_share_memory(): def test_may_share_memory():
a = numpy.random.rand(5, 4) a = np.random.rand(5, 4)
b = numpy.random.rand(5, 4) b = np.random.rand(5, 4)
va = a.view() va = a.view()
vb = b.view() vb = b.view()
ra = a.reshape((4, 5)) ra = a.reshape((4, 5))
......
...@@ -4,8 +4,7 @@ import shutil ...@@ -4,8 +4,7 @@ import shutil
import unittest import unittest
from tempfile import mkdtemp from tempfile import mkdtemp
import numpy import numpy as np
from numpy.testing import assert_allclose
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import theano import theano
...@@ -44,7 +43,7 @@ class T_dump_load(unittest.TestCase): ...@@ -44,7 +43,7 @@ class T_dump_load(unittest.TestCase):
x = load(f) x = load(f)
assert x.name == 'x' assert x.name == 'x'
assert_allclose(x.get_value(), [[1]]) np.testing.assert_allclose(x.get_value(), [[1]])
def test_dump_load_mrg(self): def test_dump_load_mrg(self):
rng = MRG_RandomStreams(use_cuda=cuda_ndarray.cuda_enabled) rng = MRG_RandomStreams(use_cuda=cuda_ndarray.cuda_enabled)
...@@ -62,14 +61,14 @@ class T_dump_load(unittest.TestCase): ...@@ -62,14 +61,14 @@ class T_dump_load(unittest.TestCase):
foo_2 = theano.shared(1, name='foo') foo_2 = theano.shared(1, name='foo')
foo_3 = theano.shared(2, name='foo') foo_3 = theano.shared(2, name='foo')
with open('model.zip', 'wb') as f: with open('model.zip', 'wb') as f:
dump((foo_1, foo_2, foo_3, numpy.array(3)), f) dump((foo_1, foo_2, foo_3, np.array(3)), f)
keys = list(numpy.load('model.zip').keys()) keys = list(np.load('model.zip').keys())
assert keys == ['foo', 'foo_2', 'foo_3', 'array_0', 'pkl'] assert keys == ['foo', 'foo_2', 'foo_3', 'array_0', 'pkl']
foo_3 = numpy.load('model.zip')['foo_3'] foo_3 = np.load('model.zip')['foo_3']
assert foo_3 == numpy.array(2) assert foo_3 == np.array(2)
with open('model.zip', 'rb') as f: with open('model.zip', 'rb') as f:
foo_1, foo_2, foo_3, array = load(f) foo_1, foo_2, foo_3, array = load(f)
assert array == numpy.array(3) assert array == np.array(3)
class TestStripPickler(unittest.TestCase): class TestStripPickler(unittest.TestCase):
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.misc.pycuda_init import theano.misc.pycuda_init
...@@ -58,11 +58,11 @@ def test_pycuda_elemwise_source_module(): ...@@ -58,11 +58,11 @@ def test_pycuda_elemwise_source_module():
PycudaElemwiseSourceModuleMakeThunkOp) PycudaElemwiseSourceModuleMakeThunkOp)
for node in f4.maker.fgraph.toposort()]) for node in f4.maker.fgraph.toposort()])
val1 = numpy.asarray(numpy.random.rand(*shape), dtype='float32') val1 = np.asarray(np.random.rand(*shape), dtype='float32')
val2 = numpy.asarray(numpy.random.rand(*shape), dtype='float32') val2 = np.asarray(np.random.rand(*shape), dtype='float32')
assert numpy.allclose(f(val1, val2), f2(val1, val2)) assert np.allclose(f(val1, val2), f2(val1, val2))
assert numpy.allclose(f(val1, val2), f3(val1, val2)) assert np.allclose(f(val1, val2), f3(val1, val2))
assert numpy.allclose(f(val1, val2), f4(val1, val2)) assert np.allclose(f(val1, val2), f4(val1, val2))
# print f(val1,val2) # print f(val1,val2)
# print f2(val1,val2) # print f2(val1,val2)
...@@ -82,10 +82,10 @@ def test_pycuda_elemwise_kernel(): ...@@ -82,10 +82,10 @@ def test_pycuda_elemwise_kernel():
assert any([isinstance(node.op, PycudaElemwiseKernelOp) assert any([isinstance(node.op, PycudaElemwiseKernelOp)
for node in f2.maker.fgraph.toposort()]) for node in f2.maker.fgraph.toposort()])
val1 = numpy.asarray(numpy.random.rand(5, 5), dtype='float32') val1 = np.asarray(np.random.rand(5, 5), dtype='float32')
val2 = numpy.asarray(numpy.random.rand(5, 5), dtype='float32') val2 = np.asarray(np.random.rand(5, 5), dtype='float32')
#val1 = numpy.ones((5,5)) #val1 = np.ones((5,5))
#val2 = numpy.arange(25).reshape(5,5) #val2 = np.arange(25).reshape(5,5)
assert (f(val1, val2) == f2(val1, val2)).all() assert (f(val1, val2) == f2(val1, val2)).all()
print(f(val1, val2)) print(f(val1, val2))
print(f2(val1, val2)) print(f2(val1, val2))
...@@ -99,8 +99,8 @@ def test_pycuda_elemwise_kernel(): ...@@ -99,8 +99,8 @@ def test_pycuda_elemwise_kernel():
assert any([isinstance(node.op, PycudaElemwiseKernelOp) assert any([isinstance(node.op, PycudaElemwiseKernelOp)
for node in f4.maker.fgraph.toposort()]) for node in f4.maker.fgraph.toposort()])
val1 = numpy.random.rand(2, 2, 2) val1 = np.random.rand(2, 2, 2)
print(val1) print(val1)
print(f4(val1, val1, val1)) print(f4(val1, val1, val1))
assert numpy.allclose(f4(val1, val1, val1), val1 * val1 + val1) assert np.allclose(f4(val1, val1, val1), val1 * val1 + val1)
""" """
...@@ -8,7 +8,7 @@ from __future__ import absolute_import, print_function, division ...@@ -8,7 +8,7 @@ from __future__ import absolute_import, print_function, division
import sys import sys
import numpy import numpy as np
import theano import theano
import theano.sandbox.cuda as cuda_ndarray import theano.sandbox.cuda as cuda_ndarray
...@@ -42,9 +42,9 @@ __global__ void multiply_them(float *dest, float *a, float *b) ...@@ -42,9 +42,9 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them = mod.get_function("multiply_them") multiply_them = mod.get_function("multiply_them")
# Test with pycuda in/out of numpy.ndarray # Test with pycuda in/out of numpy.ndarray
a = numpy.random.randn(100).astype(numpy.float32) a = np.random.randn(100).astype(np.float32)
b = numpy.random.randn(100).astype(numpy.float32) b = np.random.randn(100).astype(np.float32)
dest = numpy.zeros_like(a) dest = np.zeros_like(a)
multiply_them( multiply_them(
drv.Out(dest), drv.In(a), drv.In(b), drv.Out(dest), drv.In(a), drv.In(b),
block=(400, 1, 1), grid=(1, 1)) block=(400, 1, 1), grid=(1, 1))
...@@ -64,8 +64,8 @@ __global__ void multiply_them(float *dest, float *a, float *b) ...@@ -64,8 +64,8 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them = mod.get_function("multiply_them") multiply_them = mod.get_function("multiply_them")
a = numpy.random.randn(100).astype(numpy.float32) a = np.random.randn(100).astype(np.float32)
b = numpy.random.randn(100).astype(numpy.float32) b = np.random.randn(100).astype(np.float32)
# Test with Theano object # Test with Theano object
ga = cuda_ndarray.CudaNdarray(a) ga = cuda_ndarray.CudaNdarray(a)
...@@ -73,7 +73,7 @@ __global__ void multiply_them(float *dest, float *a, float *b) ...@@ -73,7 +73,7 @@ __global__ void multiply_them(float *dest, float *a, float *b)
dest = cuda_ndarray.CudaNdarray.zeros(a.shape) dest = cuda_ndarray.CudaNdarray.zeros(a.shape)
multiply_them(dest, ga, gb, multiply_them(dest, ga, gb,
block=(400, 1, 1), grid=(1, 1)) block=(400, 1, 1), grid=(1, 1))
assert (numpy.asarray(dest) == a * b).all() assert (np.asarray(dest) == a * b).all()
def test_pycuda_memory_to_theano(): def test_pycuda_memory_to_theano():
...@@ -87,7 +87,7 @@ 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("gpuarray ref count before creating a CudaNdarray", end=' ')
print(sys.getrefcount(y)) print(sys.getrefcount(y))
assert sys.getrefcount(y) == initial_refcount assert sys.getrefcount(y) == initial_refcount
rand = numpy.random.randn(*y.shape).astype(numpy.float32) rand = np.random.randn(*y.shape).astype(np.float32)
cuda_rand = cuda_ndarray.CudaNdarray(rand) cuda_rand = cuda_ndarray.CudaNdarray(rand)
strides = [1] strides = [1]
...@@ -102,7 +102,7 @@ def test_pycuda_memory_to_theano(): ...@@ -102,7 +102,7 @@ def test_pycuda_memory_to_theano():
z = cuda_ndarray.from_gpu_pointer(y_ptr, y.shape, strides, y) z = cuda_ndarray.from_gpu_pointer(y_ptr, y.shape, strides, y)
print("gpuarray ref count after creating a CudaNdarray", sys.getrefcount(y)) print("gpuarray ref count after creating a CudaNdarray", sys.getrefcount(y))
assert sys.getrefcount(y) == initial_refcount + 1 assert sys.getrefcount(y) == initial_refcount + 1
assert (numpy.asarray(z) == 0).all() assert (np.asarray(z) == 0).all()
assert z.base is y assert z.base is y
# Test that we can take a view from this cuda view on pycuda memory # Test that we can take a view from this cuda view on pycuda memory
...@@ -112,17 +112,17 @@ def test_pycuda_memory_to_theano(): ...@@ -112,17 +112,17 @@ def test_pycuda_memory_to_theano():
del zz del zz
assert sys.getrefcount(y) == initial_refcount + 1 assert sys.getrefcount(y) == initial_refcount + 1
cuda_ones = cuda_ndarray.CudaNdarray(numpy.asarray([[[1]]], cuda_ones = cuda_ndarray.CudaNdarray(np.asarray([[[1]]],
dtype='float32')) dtype='float32'))
z += cuda_ones z += cuda_ones
assert (numpy.asarray(z) == numpy.ones(y.shape)).all() assert (np.asarray(z) == np.ones(y.shape)).all()
assert (numpy.asarray(z) == 1).all() assert (np.asarray(z) == 1).all()
assert cuda_rand.shape == z.shape assert cuda_rand.shape == z.shape
assert cuda_rand._strides == z._strides, (cuda_rand._strides, z._strides) assert cuda_rand._strides == z._strides, (cuda_rand._strides, z._strides)
assert (numpy.asarray(cuda_rand) == rand).all() assert (np.asarray(cuda_rand) == rand).all()
z += cuda_rand z += cuda_rand
assert (numpy.asarray(z) == (rand + 1)).all() assert (np.asarray(z) == (rand + 1)).all()
# Check that the ref count to the gpuarray is right. # Check that the ref count to the gpuarray is right.
del z del z
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
import theano.misc.pycuda_init import theano.misc.pycuda_init
...@@ -22,30 +22,30 @@ def test_to_gpuarray(): ...@@ -22,30 +22,30 @@ def test_to_gpuarray():
px = to_gpuarray(cx) px = to_gpuarray(cx)
assert isinstance(px, pycuda.gpuarray.GPUArray) assert isinstance(px, pycuda.gpuarray.GPUArray)
cx[0, 0] = numpy.asarray(1, dtype="float32") cx[0, 0] = np.asarray(1, dtype="float32")
# Check that they share the same memory space # Check that they share the same memory space
assert px.gpudata == cx.gpudata assert px.gpudata == cx.gpudata
assert numpy.asarray(cx[0, 0]) == 1 assert np.asarray(cx[0, 0]) == 1
assert numpy.allclose(numpy.asarray(cx), px.get()) assert np.allclose(np.asarray(cx), px.get())
assert px.dtype == cx.dtype assert px.dtype == cx.dtype
assert px.shape == cx.shape assert px.shape == cx.shape
assert all(numpy.asarray(cx._strides) * 4 == px.strides) assert all(np.asarray(cx._strides) * 4 == px.strides)
# Test when the CudaNdarray is strided # Test when the CudaNdarray is strided
cx = cx[::2, ::] cx = cx[::2, ::]
px = to_gpuarray(cx, copyif=True) px = to_gpuarray(cx, copyif=True)
assert isinstance(px, pycuda.gpuarray.GPUArray) assert isinstance(px, pycuda.gpuarray.GPUArray)
cx[0, 0] = numpy.asarray(2, dtype="float32") cx[0, 0] = np.asarray(2, dtype="float32")
# Check that they do not share the same memory space # Check that they do not share the same memory space
assert px.gpudata != cx.gpudata assert px.gpudata != cx.gpudata
assert numpy.asarray(cx[0, 0]) == 2 assert np.asarray(cx[0, 0]) == 2
assert not numpy.allclose(numpy.asarray(cx), px.get()) assert not np.allclose(np.asarray(cx), px.get())
assert px.dtype == cx.dtype assert px.dtype == cx.dtype
assert px.shape == cx.shape assert px.shape == cx.shape
assert not all(numpy.asarray(cx._strides) * 4 == px.strides) assert not all(np.asarray(cx._strides) * 4 == px.strides)
# Test that we return an error # Test that we return an error
try: try:
...@@ -59,11 +59,11 @@ def test_to_cudandarray(): ...@@ -59,11 +59,11 @@ def test_to_cudandarray():
px = pycuda.gpuarray.zeros((3, 4, 5), 'float32') px = pycuda.gpuarray.zeros((3, 4, 5), 'float32')
cx = to_cudandarray(px) cx = to_cudandarray(px)
assert isinstance(cx, cuda.CudaNdarray) assert isinstance(cx, cuda.CudaNdarray)
assert numpy.allclose(px.get(), assert np.allclose(px.get(),
numpy.asarray(cx)) np.asarray(cx))
assert px.dtype == cx.dtype assert px.dtype == cx.dtype
assert px.shape == cx.shape assert px.shape == cx.shape
assert all(numpy.asarray(cx._strides) * 4 == px.strides) assert all(np.asarray(cx._strides) * 4 == px.strides)
try: try:
px = pycuda.gpuarray.zeros((3, 4, 5), 'float64') px = pycuda.gpuarray.zeros((3, 4, 5), 'float64')
...@@ -73,7 +73,7 @@ def test_to_cudandarray(): ...@@ -73,7 +73,7 @@ def test_to_cudandarray():
pass pass
try: try:
to_cudandarray(numpy.zeros(4)) to_cudandarray(np.zeros(4))
assert False assert False
except ValueError: except ValueError:
pass pass
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论