提交 b4dc02d6 authored 作者: Brandon T. Willard's avatar Brandon T. Willard

Replace theano.tensor alias T with tt in tests.gpuarray

上级 b4c51eb2
import numpy as np import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as tt
class Model(object): class Model(object):
...@@ -125,16 +125,16 @@ class GRU(Layer): ...@@ -125,16 +125,16 @@ class GRU(Layer):
"""step through processed input to create output""" """step through processed input to create output"""
def step(inp, s_prev): def step(inp, s_prev):
i_t = T.nnet.sigmoid( i_t = tt.nnet.sigmoid(
T.dot(inp, self.W_i) + T.dot(s_prev, self.R_i) + self.b_wi + self.b_ru tt.dot(inp, self.W_i) + tt.dot(s_prev, self.R_i) + self.b_wi + self.b_ru
) )
r_t = T.nnet.sigmoid( r_t = tt.nnet.sigmoid(
T.dot(inp, self.W_r) + T.dot(s_prev, self.R_r) + self.b_wr + self.b_rr tt.dot(inp, self.W_r) + tt.dot(s_prev, self.R_r) + self.b_wr + self.b_rr
) )
h_hat_t = T.tanh( h_hat_t = tt.tanh(
T.dot(inp, self.W_h) tt.dot(inp, self.W_h)
+ (r_t * (T.dot(s_prev, self.R_h) + self.b_rh)) + (r_t * (tt.dot(s_prev, self.R_h) + self.b_rh))
+ self.b_wh + self.b_wh
) )
...@@ -229,21 +229,21 @@ class LSTM(Layer): ...@@ -229,21 +229,21 @@ class LSTM(Layer):
"""step through processed input to create output""" """step through processed input to create output"""
def step(x_t, h_tm1, c_tm1): def step(x_t, h_tm1, c_tm1):
i_t = T.nnet.sigmoid( i_t = tt.nnet.sigmoid(
T.dot(x_t, self.W_i) + T.dot(h_tm1, self.R_i) + self.b_wi + self.b_ri tt.dot(x_t, self.W_i) + tt.dot(h_tm1, self.R_i) + self.b_wi + self.b_ri
) )
f_t = T.nnet.sigmoid( f_t = tt.nnet.sigmoid(
T.dot(x_t, self.W_f) + T.dot(h_tm1, self.R_f) + self.b_wf + self.b_rf tt.dot(x_t, self.W_f) + tt.dot(h_tm1, self.R_f) + self.b_wf + self.b_rf
) )
o_t = T.nnet.sigmoid( o_t = tt.nnet.sigmoid(
T.dot(x_t, self.W_o) + T.dot(h_tm1, self.R_o) + self.b_ro + self.b_wo tt.dot(x_t, self.W_o) + tt.dot(h_tm1, self.R_o) + self.b_ro + self.b_wo
) )
c_hat_t = T.tanh( c_hat_t = tt.tanh(
T.dot(x_t, self.W_c) + T.dot(h_tm1, self.R_c) + self.b_wc + self.b_rc tt.dot(x_t, self.W_c) + tt.dot(h_tm1, self.R_c) + self.b_wc + self.b_rc
) )
c_t = f_t * c_tm1 + i_t * c_hat_t c_t = f_t * c_tm1 + i_t * c_hat_t
h_t = o_t * T.tanh(c_t) h_t = o_t * tt.tanh(c_t)
return h_t, c_t return h_t, c_t
...@@ -275,7 +275,7 @@ class FC(Layer): ...@@ -275,7 +275,7 @@ class FC(Layer):
self.b = bias_weights((output_dim,), param_list=self.params, name=name + ".b") self.b = bias_weights((output_dim,), param_list=self.params, name=name + ".b")
def output(self): def output(self):
return T.dot(self.X, self.W) + self.b return tt.dot(self.X, self.W) + self.b
class WrapperLayer(Layer): class WrapperLayer(Layer):
......
...@@ -4,7 +4,7 @@ pygpu = pytest.importorskip("pygpu") ...@@ -4,7 +4,7 @@ pygpu = pytest.importorskip("pygpu")
gpuarray = pygpu.gpuarray gpuarray = pygpu.gpuarray
import numpy as np import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as tt
from theano.tensor import TensorType from theano.tensor import TensorType
from theano.tensor.basic import alloc from theano.tensor.basic import alloc
...@@ -217,7 +217,7 @@ def makeTester( ...@@ -217,7 +217,7 @@ def makeTester(
def test_transfer_cpu_gpu(): def test_transfer_cpu_gpu():
a = T.fmatrix("a") a = tt.fmatrix("a")
g = GpuArrayType(dtype="float32", broadcastable=(False, False))("g") g = GpuArrayType(dtype="float32", broadcastable=(False, False))("g")
av = np.asarray(rng.rand(5, 4), dtype="float32") av = np.asarray(rng.rand(5, 4), dtype="float32")
...@@ -254,7 +254,7 @@ def test_transfer_strided(): ...@@ -254,7 +254,7 @@ def test_transfer_strided():
# This is just to ensure that it works in theano # This is just to ensure that it works in theano
# libgpuarray has a much more comprehensive suit of tests to # libgpuarray has a much more comprehensive suit of tests to
# ensure correctness # ensure correctness
a = T.fmatrix("a") a = tt.fmatrix("a")
g = GpuArrayType(dtype="float32", broadcastable=(False, False))("g") g = GpuArrayType(dtype="float32", broadcastable=(False, False))("g")
av = np.asarray(rng.rand(5, 8), dtype="float32") av = np.asarray(rng.rand(5, 8), dtype="float32")
...@@ -300,7 +300,7 @@ class TestGPUAlloc(TestAlloc): ...@@ -300,7 +300,7 @@ class TestGPUAlloc(TestAlloc):
dtype = "float32" dtype = "float32"
mode = mode_with_gpu mode = mode_with_gpu
shared = staticmethod(gpuarray_shared_constructor) shared = staticmethod(gpuarray_shared_constructor)
allocs = [GpuAlloc(test_ctx_name), GpuAlloc(test_ctx_name), T.Alloc()] allocs = [GpuAlloc(test_ctx_name), GpuAlloc(test_ctx_name), tt.Alloc()]
def test_alloc_empty(): def test_alloc_empty():
...@@ -343,21 +343,21 @@ def test_shape(): ...@@ -343,21 +343,21 @@ def test_shape():
assert np.all(f(v) == (3, 4, 5)) assert np.all(f(v) == (3, 4, 5))
if theano.config.mode != "FAST_COMPILE": if theano.config.mode != "FAST_COMPILE":
assert len(topo) == 4 assert len(topo) == 4
assert isinstance(topo[0].op, T.opt.Shape_i) assert isinstance(topo[0].op, tt.opt.Shape_i)
assert isinstance(topo[1].op, T.opt.Shape_i) assert isinstance(topo[1].op, tt.opt.Shape_i)
assert isinstance(topo[2].op, T.opt.Shape_i) assert isinstance(topo[2].op, tt.opt.Shape_i)
assert isinstance(topo[3].op, T.opt.MakeVector) assert isinstance(topo[3].op, tt.opt.MakeVector)
mode = mode_with_gpu.excluding("local_shape_to_shape_i") mode = mode_with_gpu.excluding("local_shape_to_shape_i")
f = theano.function([x], x.shape, mode=mode) f = theano.function([x], x.shape, mode=mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert np.all(f(v) == (3, 4, 5)) assert np.all(f(v) == (3, 4, 5))
assert len(topo) == 1 assert len(topo) == 1
assert isinstance(topo[0].op, T.Shape) assert isinstance(topo[0].op, tt.Shape)
def test_gpu_contiguous(): def test_gpu_contiguous():
a = T.fmatrix("a") a = tt.fmatrix("a")
i = T.iscalar("i") i = tt.iscalar("i")
a_val = np.asarray(np.random.rand(4, 5), dtype="float32") a_val = np.asarray(np.random.rand(4, 5), dtype="float32")
# The reshape is needed otherwise we make the subtensor on the CPU # The reshape is needed otherwise we make the subtensor on the CPU
# to transfer less data. # to transfer less data.
...@@ -383,8 +383,8 @@ class TestGPUReshape(TestReshape): ...@@ -383,8 +383,8 @@ class TestGPUReshape(TestReshape):
theano.compile.DeepCopyOp, theano.compile.DeepCopyOp,
GpuDimShuffle, GpuDimShuffle,
GpuElemwise, GpuElemwise,
theano.tensor.opt.Shape_i, tt.opt.Shape_i,
theano.tensor.opt.MakeVector, tt.opt.MakeVector,
) )
assert self.op == GpuReshape assert self.op == GpuReshape
...@@ -418,7 +418,7 @@ class TestGPUJoinAndSplit(TestJoinAndSplit): ...@@ -418,7 +418,7 @@ class TestGPUJoinAndSplit(TestJoinAndSplit):
# Also test float16 computation at the same time. # Also test float16 computation at the same time.
rng = np.random.RandomState(seed=utt.fetch_seed()) rng = np.random.RandomState(seed=utt.fetch_seed())
m = self.shared(rng.rand(4, 6).astype("float16")) m = self.shared(rng.rand(4, 6).astype("float16"))
o = T.Split(2)(m, 0, [2, 2]) o = tt.Split(2)(m, 0, [2, 2])
assert o[0].dtype == "float16" assert o[0].dtype == "float16"
f = theano.function([], o, mode=self.mode) f = theano.function([], o, mode=self.mode)
assert any( assert any(
...@@ -433,22 +433,22 @@ class TestGPUJoinAndSplit(TestJoinAndSplit): ...@@ -433,22 +433,22 @@ class TestGPUJoinAndSplit(TestJoinAndSplit):
def test_gpujoin_gpualloc(): def test_gpujoin_gpualloc():
a = T.fmatrix("a") a = tt.fmatrix("a")
a_val = np.asarray(np.random.rand(4, 5), dtype="float32") a_val = np.asarray(np.random.rand(4, 5), dtype="float32")
b = T.fmatrix("b") b = tt.fmatrix("b")
b_val = np.asarray(np.random.rand(3, 5), dtype="float32") b_val = np.asarray(np.random.rand(3, 5), dtype="float32")
f = theano.function( f = theano.function(
[a, b], T.join(0, T.zeros_like(a), T.ones_like(b)) + 4, mode=mode_without_gpu [a, b], tt.join(0, tt.zeros_like(a), tt.ones_like(b)) + 4, mode=mode_without_gpu
) )
f_gpu = theano.function( f_gpu = theano.function(
[a, b], T.join(0, T.zeros_like(a), T.ones_like(b)), mode=mode_with_gpu [a, b], tt.join(0, tt.zeros_like(a), tt.ones_like(b)), mode=mode_with_gpu
) )
f_gpu2 = theano.function( f_gpu2 = theano.function(
[a, b], T.join(0, T.zeros_like(a), T.ones_like(b)) + 4, mode=mode_with_gpu [a, b], tt.join(0, tt.zeros_like(a), tt.ones_like(b)) + 4, mode=mode_with_gpu
) )
assert sum([node.op == T.alloc for node in f.maker.fgraph.toposort()]) == 2 assert sum([node.op == tt.alloc for node in f.maker.fgraph.toposort()]) == 2
assert sum([node.op == T.join_ for node in f.maker.fgraph.toposort()]) == 1 assert sum([node.op == tt.join_ for node in f.maker.fgraph.toposort()]) == 1
assert ( assert (
sum([isinstance(node.op, GpuAlloc) for node in f_gpu.maker.fgraph.toposort()]) sum([isinstance(node.op, GpuAlloc) for node in f_gpu.maker.fgraph.toposort()])
== 2 == 2
...@@ -471,10 +471,10 @@ def test_gpueye(): ...@@ -471,10 +471,10 @@ def test_gpueye():
# allowed. # allowed.
if M is None: if M is None:
M = N M = N
N_symb = T.iscalar() N_symb = tt.iscalar()
M_symb = T.iscalar() M_symb = tt.iscalar()
k_symb = T.iscalar() k_symb = tt.iscalar()
out = T.eye(N_symb, M_symb, k_symb, dtype=dtype) + np.array(1).astype(dtype) out = tt.eye(N_symb, M_symb, k_symb, dtype=dtype) + np.array(1).astype(dtype)
f = theano.function([N_symb, M_symb, k_symb], out, mode=mode_with_gpu) f = theano.function([N_symb, M_symb, k_symb], out, mode=mode_with_gpu)
result = np.asarray(f(N, M, k)) - np.array(1).astype(dtype) result = np.asarray(f(N, M, k)) - np.array(1).astype(dtype)
...@@ -511,7 +511,7 @@ def test_hostfromgpu_shape_i(): ...@@ -511,7 +511,7 @@ def test_hostfromgpu_shape_i():
m = mode_with_gpu.including( m = mode_with_gpu.including(
"local_dot_to_dot22", "local_dot22_to_dot22scalar", "specialize" "local_dot_to_dot22", "local_dot22_to_dot22scalar", "specialize"
) )
a = T.fmatrix("a") a = tt.fmatrix("a")
ca = theano.gpuarray.type.GpuArrayType("float32", (False, False))() ca = theano.gpuarray.type.GpuArrayType("float32", (False, False))()
av = np.asarray(np.random.rand(5, 4), dtype="float32") av = np.asarray(np.random.rand(5, 4), dtype="float32")
cv = gpuarray.asarray( cv = gpuarray.asarray(
...@@ -522,9 +522,9 @@ def test_hostfromgpu_shape_i(): ...@@ -522,9 +522,9 @@ def test_hostfromgpu_shape_i():
assert any(isinstance(x.op, GpuFromHost) for x in f.maker.fgraph.toposort()) assert any(isinstance(x.op, GpuFromHost) for x in f.maker.fgraph.toposort())
f = theano.function([a], GpuFromHost(test_ctx_name)(a).shape, mode=m) f = theano.function([a], GpuFromHost(test_ctx_name)(a).shape, mode=m)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert isinstance(topo[0].op, T.opt.Shape_i) assert isinstance(topo[0].op, tt.opt.Shape_i)
assert isinstance(topo[1].op, T.opt.Shape_i) assert isinstance(topo[1].op, tt.opt.Shape_i)
assert isinstance(topo[2].op, T.opt.MakeVector) assert isinstance(topo[2].op, tt.opt.MakeVector)
assert tuple(f(av)) == (5, 4) assert tuple(f(av)) == (5, 4)
f = theano.function([ca], host_from_gpu(ca), mode=m) f = theano.function([ca], host_from_gpu(ca), mode=m)
...@@ -533,7 +533,7 @@ def test_hostfromgpu_shape_i(): ...@@ -533,7 +533,7 @@ def test_hostfromgpu_shape_i():
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert isinstance(topo[0].op, theano.compile.Shape_i) assert isinstance(topo[0].op, theano.compile.Shape_i)
assert isinstance(topo[1].op, theano.compile.Shape_i) assert isinstance(topo[1].op, theano.compile.Shape_i)
assert isinstance(topo[2].op, theano.tensor.opt.MakeVector) assert isinstance(topo[2].op, tt.opt.MakeVector)
assert tuple(f(cv)) == (5, 4) assert tuple(f(cv)) == (5, 4)
...@@ -544,10 +544,10 @@ def test_Gpujoin_inplace(): ...@@ -544,10 +544,10 @@ def test_Gpujoin_inplace():
# Gpujoin function but all except one of them are empty. In this case # Gpujoin function but all except one of them are empty. In this case
# Gpujoin should work inplace and the output should be the view of the # Gpujoin should work inplace and the output should be the view of the
# non-empty element. # non-empty element.
s = T.lscalar() s = tt.lscalar()
data = np.array([3, 4, 5], dtype=theano.config.floatX) data = np.array([3, 4, 5], dtype=theano.config.floatX)
x = gpuarray_shared_constructor(data, borrow=True) x = gpuarray_shared_constructor(data, borrow=True)
z = T.zeros((s,)) z = tt.zeros((s,))
join = GpuJoin(view=0) join = GpuJoin(view=0)
c = join(0, x, z) c = join(0, x, z)
...@@ -560,11 +560,11 @@ def test_Gpujoin_inplace(): ...@@ -560,11 +560,11 @@ def test_Gpujoin_inplace():
def test_gpu_tril_triu(): def test_gpu_tril_triu():
def check_l(m, k=0): def check_l(m, k=0):
m_symb = T.matrix(dtype=m.dtype) m_symb = tt.matrix(dtype=m.dtype)
k_symb = T.iscalar() k_symb = tt.iscalar()
f = theano.function( f = theano.function(
[m_symb, k_symb], T.tril(m_symb, k_symb), mode=mode_with_gpu [m_symb, k_symb], tt.tril(m_symb, k_symb), mode=mode_with_gpu
) )
result = f(m, k) result = f(m, k)
assert np.allclose(result, np.tril(m, k)) assert np.allclose(result, np.tril(m, k))
...@@ -572,10 +572,10 @@ def test_gpu_tril_triu(): ...@@ -572,10 +572,10 @@ def test_gpu_tril_triu():
assert any([isinstance(node.op, GpuTri) for node in f.maker.fgraph.toposort()]) assert any([isinstance(node.op, GpuTri) for node in f.maker.fgraph.toposort()])
def check_u(m, k=0): def check_u(m, k=0):
m_symb = T.matrix(dtype=m.dtype) m_symb = tt.matrix(dtype=m.dtype)
k_symb = T.iscalar() k_symb = tt.iscalar()
f = theano.function( f = theano.function(
[m_symb, k_symb], T.triu(m_symb, k_symb), mode=mode_with_gpu [m_symb, k_symb], tt.triu(m_symb, k_symb), mode=mode_with_gpu
) )
result = f(m, k) result = f(m, k)
assert np.allclose(result, np.triu(m, k)) assert np.allclose(result, np.triu(m, k))
...@@ -624,10 +624,10 @@ def test_gputri(): ...@@ -624,10 +624,10 @@ def test_gputri():
# allowed. # allowed.
if M is None: if M is None:
M = N M = N
N_symb = T.iscalar() N_symb = tt.iscalar()
M_symb = T.iscalar() M_symb = tt.iscalar()
k_symb = T.iscalar() k_symb = tt.iscalar()
out = T.tri(N_symb, M_symb, k_symb, dtype=dtype) + np.array(1).astype(dtype) out = tt.tri(N_symb, M_symb, k_symb, dtype=dtype) + np.array(1).astype(dtype)
f = theano.function([N_symb, M_symb, k_symb], out, mode=mode_with_gpu) f = theano.function([N_symb, M_symb, k_symb], out, mode=mode_with_gpu)
result = np.asarray(f(N, M, k)) - np.array(1).astype(dtype) result = np.asarray(f(N, M, k)) - np.array(1).astype(dtype)
assert np.allclose(result, np.tri(N, M_, k, dtype=dtype)) assert np.allclose(result, np.tri(N, M_, k, dtype=dtype))
......
...@@ -2,7 +2,7 @@ import pytest ...@@ -2,7 +2,7 @@ import pytest
import numpy as np import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as tt
import theano.gpuarray import theano.gpuarray
from theano.gpuarray.ctc import gpu_ctc, GpuConnectionistTemporalClassification from theano.gpuarray.ctc import gpu_ctc, GpuConnectionistTemporalClassification
...@@ -56,7 +56,7 @@ class TestCTC: ...@@ -56,7 +56,7 @@ class TestCTC:
outputs = [cpu_ctc_cost] outputs = [cpu_ctc_cost]
if compute_grad: if compute_grad:
# Symbolic gradient of CTC cost # Symbolic gradient of CTC cost
cpu_ctc_grad = T.grad(T.mean(cpu_ctc_cost), activations) cpu_ctc_grad = tt.grad(tt.mean(cpu_ctc_cost), activations)
outputs += [cpu_ctc_grad] outputs += [cpu_ctc_grad]
return theano.function([], outputs, mode=mode) return theano.function([], outputs, mode=mode)
...@@ -65,7 +65,7 @@ class TestCTC: ...@@ -65,7 +65,7 @@ class TestCTC:
outputs = [gpu_ctc_cost] outputs = [gpu_ctc_cost]
if compute_grad: if compute_grad:
# Symbolic gradient of CTC cost # Symbolic gradient of CTC cost
gpu_ctc_grad = T.grad(T.mean(gpu_ctc_cost), activations) gpu_ctc_grad = tt.grad(tt.mean(gpu_ctc_cost), activations)
outputs += [gpu_ctc_grad] outputs += [gpu_ctc_grad]
return theano.function([], outputs, mode=mode_with_gpu) return theano.function([], outputs, mode=mode_with_gpu)
......
差异被折叠。
...@@ -3,12 +3,11 @@ import pytest ...@@ -3,12 +3,11 @@ import pytest
import numpy as np import numpy as np
import theano import theano
import theano.tensor as tt
from functools import partial from functools import partial
from itertools import product from itertools import product
from theano import tensor as T
from theano.tensor.extra_ops import CumOp from theano.tensor.extra_ops import CumOp
from theano.gpuarray.extra_ops import GpuCumOp from theano.gpuarray.extra_ops import GpuCumOp
from theano.gpuarray.type import get_context from theano.gpuarray.type import get_context
...@@ -33,13 +32,13 @@ class TestGpuCumOp(TestCumOp): ...@@ -33,13 +32,13 @@ class TestGpuCumOp(TestCumOp):
# The CPU implementation is not so accurate, which throws out DebugMode. # The CPU implementation is not so accurate, which throws out DebugMode.
# Since propagating .tag.values_eq_approx to the output of every # Since propagating .tag.values_eq_approx to the output of every
# GpuFromHost seems overkill, we just relax the rtol for these tests # GpuFromHost seems overkill, we just relax the rtol for these tests
self.old_rtol = theano.tensor.float32_rtol self.old_rtol = tt.float32_rtol
theano.tensor.basic.float32_rtol *= 2 tt.float32_rtol *= 2
def teardown_method(self): def teardown_method(self):
super().teardown_method() super().teardown_method()
# Restore rtol # Restore rtol
theano.tensor.basic.float32_rtol = self.old_rtol tt.float32_rtol = self.old_rtol
@pytest.mark.skipif( @pytest.mark.skipif(
theano.config.floatX != "float32", theano.config.floatX != "float32",
...@@ -48,7 +47,7 @@ class TestGpuCumOp(TestCumOp): ...@@ -48,7 +47,7 @@ class TestGpuCumOp(TestCumOp):
@pytest.mark.parametrized("mode", ["mul", "add"]) @pytest.mark.parametrized("mode", ["mul", "add"])
def test_infer_shape(self, mode): def test_infer_shape(self, mode):
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
x = T.tensor3("x") x = tt.tensor3("x")
a = np.random.random((3, 5, 2)).astype(theano.config.floatX) a = np.random.random((3, 5, 2)).astype(theano.config.floatX)
for axis in range(-len(a.shape), len(a.shape)): for axis in range(-len(a.shape), len(a.shape)):
...@@ -58,7 +57,7 @@ class TestGpuCumOp(TestCumOp): ...@@ -58,7 +57,7 @@ class TestGpuCumOp(TestCumOp):
def test_Strides1D(self, mode): def test_Strides1D(self, mode):
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
np_func = dict(add=np.cumsum, mul=np.cumprod)[mode] np_func = dict(add=np.cumsum, mul=np.cumprod)[mode]
x = T.fvector("x") x = tt.fvector("x")
for axis in [0, None, -1]: for axis in [0, None, -1]:
a = np.random.random((42,)).astype("float32") a = np.random.random((42,)).astype("float32")
...@@ -89,7 +88,7 @@ class TestGpuCumOp(TestCumOp): ...@@ -89,7 +88,7 @@ class TestGpuCumOp(TestCumOp):
def test_Strides2D(self, mode): def test_Strides2D(self, mode):
np_func = dict(add=np.cumsum, mul=np.cumprod)[mode] np_func = dict(add=np.cumsum, mul=np.cumprod)[mode]
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
x = T.fmatrix("x") x = tt.fmatrix("x")
for axis in [0, 1, None, -1, -2]: for axis in [0, 1, None, -1, -2]:
a = np.random.random((42, 30)).astype("float32") a = np.random.random((42, 30)).astype("float32")
...@@ -120,7 +119,7 @@ class TestGpuCumOp(TestCumOp): ...@@ -120,7 +119,7 @@ class TestGpuCumOp(TestCumOp):
def test_Strides3D(self, mode): def test_Strides3D(self, mode):
np_func = dict(add=np.cumsum, mul=np.cumprod)[mode] np_func = dict(add=np.cumsum, mul=np.cumprod)[mode]
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
x = T.ftensor3("x") x = tt.ftensor3("x")
for axis in [0, 1, 2, None, -1, -2, -3]: for axis in [0, 1, 2, None, -1, -2, -3]:
a = np.random.random((42, 30, 25)).astype("float32") a = np.random.random((42, 30, 25)).astype("float32")
...@@ -153,7 +152,7 @@ class TestGpuCumOp(TestCumOp): ...@@ -153,7 +152,7 @@ class TestGpuCumOp(TestCumOp):
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
block_max_size = self.max_threads_dim0 * 2 block_max_size = self.max_threads_dim0 * 2
x = T.fvector("x") x = tt.fvector("x")
f = theano.function([x], op_class(axis=0)(x), mode=self.mode) f = theano.function([x], op_class(axis=0)(x), mode=self.mode)
assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, GpuCumOp)] assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, GpuCumOp)]
...@@ -176,7 +175,7 @@ class TestGpuCumOp(TestCumOp): ...@@ -176,7 +175,7 @@ class TestGpuCumOp(TestCumOp):
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
block_max_size = self.max_threads_dim0 * 2 block_max_size = self.max_threads_dim0 * 2
x = T.fmatrix("x") x = tt.fmatrix("x")
for shape_axis, axis in zip([0, 1, 0, 1, 0], [0, 1, None, -1, -2]): for shape_axis, axis in zip([0, 1, 0, 1, 0], [0, 1, None, -1, -2]):
f = theano.function([x], op_class(axis=axis)(x), mode=self.mode) f = theano.function([x], op_class(axis=axis)(x), mode=self.mode)
assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, GpuCumOp)] assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, GpuCumOp)]
...@@ -217,7 +216,7 @@ class TestGpuCumOp(TestCumOp): ...@@ -217,7 +216,7 @@ class TestGpuCumOp(TestCumOp):
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
block_max_size = self.max_threads_dim0 * 2 block_max_size = self.max_threads_dim0 * 2
x = T.ftensor3("x") x = tt.ftensor3("x")
for shape_axis, axis in zip([0, 1, 2, 0, 2, 1, 0], [0, 1, 2, None, -1, -2, -3]): for shape_axis, axis in zip([0, 1, 2, 0, 2, 1, 0], [0, 1, 2, None, -1, -2, -3]):
f = theano.function([x], op_class(axis=axis)(x), mode=self.mode) f = theano.function([x], op_class(axis=axis)(x), mode=self.mode)
assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, GpuCumOp)] assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, GpuCumOp)]
...@@ -267,6 +266,6 @@ class TestGpuCumOp(TestCumOp): ...@@ -267,6 +266,6 @@ class TestGpuCumOp(TestCumOp):
def test_GpuCumOp4D(self, mode): def test_GpuCumOp4D(self, mode):
op_class = partial(self.op_class, mode=mode) op_class = partial(self.op_class, mode=mode)
# Should not use the GPU version. # Should not use the GPU version.
x = T.ftensor4("x") x = tt.ftensor4("x")
f = theano.function([x], op_class(axis=1)(x), mode=self.mode) f = theano.function([x], op_class(axis=1)(x), mode=self.mode)
assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, CumOp)] assert [n for n in f.maker.fgraph.toposort() if isinstance(n.op, CumOp)]
...@@ -3,7 +3,7 @@ import numpy as np ...@@ -3,7 +3,7 @@ import numpy as np
import pytest import pytest
import theano import theano
import theano.tensor as T import theano.tensor as tt
import theano.gpuarray.fft import theano.gpuarray.fft
from theano.gpuarray.fft import pygpu_available, skcuda_available, pycuda_available from theano.gpuarray.fft import pygpu_available, skcuda_available, pycuda_available
...@@ -27,7 +27,7 @@ class TestFFT: ...@@ -27,7 +27,7 @@ class TestFFT:
def test_1Dfft(self): def test_1Dfft(self):
inputs_val = np.random.random((1, N)).astype("float32") inputs_val = np.random.random((1, N)).astype("float32")
x = T.matrix("x", dtype="float32") x = tt.matrix("x", dtype="float32")
rfft = theano.gpuarray.fft.curfft(x) rfft = theano.gpuarray.fft.curfft(x)
f_rfft = theano.function([x], rfft, mode=mode_with_gpu) f_rfft = theano.function([x], rfft, mode=mode_with_gpu)
res_rfft = f_rfft(inputs_val) res_rfft = f_rfft(inputs_val)
......
import numpy as np import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as tt
import tests.unittest_tools as utt import tests.unittest_tools as utt
from theano.tensor.nnet import crossentropy_softmax_1hot_with_bias_dx
from theano.gpuarray.nnet import ( from theano.gpuarray.nnet import (
GpuCrossentropySoftmaxArgmax1HotWithBias, GpuCrossentropySoftmaxArgmax1HotWithBias,
GpuCrossentropySoftmax1HotWithBiasDx, GpuCrossentropySoftmax1HotWithBiasDx,
GpuSoftmaxWithBias, GpuSoftmaxWithBias,
GpuSoftmax, GpuSoftmax,
) )
from tests.gpuarray.config import mode_with_gpu, mode_without_gpu from tests.gpuarray.config import mode_with_gpu, mode_without_gpu
mode_wo_cudnn = mode_with_gpu.excluding("cudnn") mode_wo_cudnn = mode_with_gpu.excluding("cudnn")
...@@ -29,16 +29,16 @@ def test_GpuCrossentropySoftmaxArgmax1HotWithBias(): ...@@ -29,16 +29,16 @@ def test_GpuCrossentropySoftmaxArgmax1HotWithBias():
n_in = 4098 n_in = 4098
n_out = 4099 n_out = 4099
y = T.lvector("y") y = tt.lvector("y")
b = T.fvector("b") b = tt.fvector("b")
# we precompute the dot with big shape before to allow the test of # we precompute the dot with big shape before to allow the test of
# GpuCrossentropySoftmax1HotWithBiasDx to don't fail with the error # GpuCrossentropySoftmax1HotWithBiasDx to don't fail with the error
# (the launch timed out and was terminated) on GPU card not # (the launch timed out and was terminated) on GPU card not
# powerful enough. We need the big shape to check for corner # powerful enough. We need the big shape to check for corner
# case. # case.
dot_result = T.fmatrix("dot_result") dot_result = tt.fmatrix("dot_result")
# Seed numpy.random with config.unittests.rseed # Seed numpy.random with config.unittests.rseed
utt.seed_rng() utt.seed_rng()
...@@ -50,10 +50,10 @@ def test_GpuCrossentropySoftmaxArgmax1HotWithBias(): ...@@ -50,10 +50,10 @@ def test_GpuCrossentropySoftmaxArgmax1HotWithBias():
dot_value = np.asarray(np.dot(xx, W_values), dtype="float32") dot_value = np.asarray(np.dot(xx, W_values), dtype="float32")
del W_values del W_values
p_y_given_x = T.nnet.softmax(dot_result + b) p_y_given_x = tt.nnet.softmax(dot_result + b)
y_pred = T.argmax(p_y_given_x, axis=-1) y_pred = tt.argmax(p_y_given_x, axis=-1)
loss = -T.mean(T.log(p_y_given_x)[T.arange(y.shape[0]), y]) loss = -tt.mean(tt.log(p_y_given_x)[tt.arange(y.shape[0]), y])
dW = T.grad(loss, dot_result) dW = tt.grad(loss, dot_result)
classify = theano.function( classify = theano.function(
inputs=[y, b, dot_result], outputs=[loss, y_pred, dW], mode=mode_without_gpu inputs=[y, b, dot_result], outputs=[loss, y_pred, dW], mode=mode_without_gpu
) )
...@@ -63,7 +63,7 @@ def test_GpuCrossentropySoftmaxArgmax1HotWithBias(): ...@@ -63,7 +63,7 @@ def test_GpuCrossentropySoftmaxArgmax1HotWithBias():
assert any( assert any(
[ [
isinstance(node.op, T.nnet.CrossentropySoftmaxArgmax1HotWithBias) isinstance(node.op, tt.nnet.CrossentropySoftmaxArgmax1HotWithBias)
for node in classify.maker.fgraph.toposort() for node in classify.maker.fgraph.toposort()
] ]
) )
...@@ -100,11 +100,9 @@ def test_GpuCrossentropySoftmax1HotWithBiasDx(): ...@@ -100,11 +100,9 @@ def test_GpuCrossentropySoftmax1HotWithBiasDx():
dnll_value = np.asarray(np.random.rand(batch_size), dtype="float32") dnll_value = np.asarray(np.random.rand(batch_size), dtype="float32")
y_idx_value = np.random.randint(low=0, high=5, size=batch_size) y_idx_value = np.random.randint(low=0, high=5, size=batch_size)
softmax_output = T.fmatrix() softmax_output = tt.fmatrix()
softmax_output /= softmax_output.sum(axis=1).reshape(softmax_output.shape[1], 1) softmax_output /= softmax_output.sum(axis=1).reshape(softmax_output.shape[1], 1)
op = theano.tensor.nnet.crossentropy_softmax_1hot_with_bias_dx( op = crossentropy_softmax_1hot_with_bias_dx(dnll_value, softmax_output, y_idx_value)
dnll_value, softmax_output, y_idx_value
)
cpu_f = theano.function([softmax_output], op, mode=mode_without_gpu) cpu_f = theano.function([softmax_output], op, mode=mode_without_gpu)
gpu_f = theano.function([softmax_output], op, mode=mode_with_gpu) gpu_f = theano.function([softmax_output], op, mode=mode_with_gpu)
...@@ -113,7 +111,7 @@ def test_GpuCrossentropySoftmax1HotWithBiasDx(): ...@@ -113,7 +111,7 @@ def test_GpuCrossentropySoftmax1HotWithBiasDx():
assert any( assert any(
[ [
isinstance(node.op, T.nnet.CrossentropySoftmax1HotWithBiasDx) isinstance(node.op, tt.nnet.CrossentropySoftmax1HotWithBiasDx)
for node in cpu_f.maker.fgraph.toposort() for node in cpu_f.maker.fgraph.toposort()
] ]
) )
...@@ -156,14 +154,14 @@ def softmax_with_bias_unittest_template(dtypeInput, dtypeBias): ...@@ -156,14 +154,14 @@ def softmax_with_bias_unittest_template(dtypeInput, dtypeBias):
# TODO: check that we loop when there are too many threads. (THIS IS # TODO: check that we loop when there are too many threads. (THIS IS
# NOT IMPLEMENTED) # NOT IMPLEMENTED)
x = T.matrix("x", dtype=dtypeInput) x = tt.matrix("x", dtype=dtypeInput)
b = T.vector("b", dtype=dtypeBias) b = tt.vector("b", dtype=dtypeBias)
z = T.nnet.softmax_with_bias(x, b) z = tt.nnet.softmax_with_bias(x, b)
f = theano.function([x, b], z, mode=mode_without_gpu) f = theano.function([x, b], z, mode=mode_without_gpu)
f_gpu = theano.function([x, b], z, mode=mode_with_gpu) f_gpu = theano.function([x, b], z, mode=mode_with_gpu)
assert f.maker.fgraph.toposort()[-1].op == T.nnet.softmax_with_bias assert f.maker.fgraph.toposort()[-1].op == tt.nnet.softmax_with_bias
assert isinstance(f_gpu.maker.fgraph.toposort()[-2].op, GpuSoftmaxWithBias) assert isinstance(f_gpu.maker.fgraph.toposort()[-2].op, GpuSoftmaxWithBias)
def cmp(n, m): def cmp(n, m):
...@@ -209,12 +207,12 @@ def softmax_unittest_template(dtypeInput): ...@@ -209,12 +207,12 @@ def softmax_unittest_template(dtypeInput):
# We check that we loop when their is too much block # We check that we loop when their is too much block
# We use slower code when there isn't enough shared memory # We use slower code when there isn't enough shared memory
x = T.matrix("x", dtype=dtypeInput) x = tt.matrix("x", dtype=dtypeInput)
z = T.nnet.softmax(x) z = tt.nnet.softmax(x)
f = theano.function([x], z, mode=mode_without_gpu) f = theano.function([x], z, mode=mode_without_gpu)
f_gpu = theano.function([x], z, mode=mode_wo_cudnn) f_gpu = theano.function([x], z, mode=mode_wo_cudnn)
assert f.maker.fgraph.toposort()[-1].op == T.nnet.softmax_op assert f.maker.fgraph.toposort()[-1].op == tt.nnet.softmax_op
assert isinstance(f_gpu.maker.fgraph.toposort()[-2].op, GpuSoftmax) assert isinstance(f_gpu.maker.fgraph.toposort()[-2].op, GpuSoftmax)
def cmp(n, m): def cmp(n, m):
...@@ -256,7 +254,7 @@ class TestSoftMax: ...@@ -256,7 +254,7 @@ class TestSoftMax:
f = theano.function([x], f_z_out, mode=mode_without_gpu) f = theano.function([x], f_z_out, mode=mode_without_gpu)
f_gpu = theano.function([x_gpu], f_gpu_z_out, mode=self.mode) f_gpu = theano.function([x_gpu], f_gpu_z_out, mode=self.mode)
self._check_types(f, f_gpu, T.nnet.Softmax, self.gpu_op) self._check_types(f, f_gpu, tt.nnet.Softmax, self.gpu_op)
# we need to test n>32*1024 to check that we make the block loop. # we need to test n>32*1024 to check that we make the block loop.
cmp(1, 5, f, f_gpu) cmp(1, 5, f, f_gpu)
...@@ -303,16 +301,16 @@ class TestSoftMax: ...@@ -303,16 +301,16 @@ class TestSoftMax:
) )
def test_softmax(self): def test_softmax(self):
x = T.fmatrix("x") x = tt.fmatrix("x")
z = T.nnet.softmax_op z = tt.nnet.softmax_op
f, f_gpu = self._test_softmax(x, x, z, z, self._cmp) f, f_gpu = self._test_softmax(x, x, z, z, self._cmp)
self._cmp(2 << 15, 5, f, f_gpu) self._cmp(2 << 15, 5, f, f_gpu)
def test_softmax_shape_0(self): def test_softmax_shape_0(self):
x = T.fmatrix("x") x = tt.fmatrix("x")
z = T.nnet.softmax_op z = tt.nnet.softmax_op
f, f_gpu = self._test_softmax(x, x, z, z, self._cmp) f, f_gpu = self._test_softmax(x, x, z, z, self._cmp)
# Theano can handle that case, but cudnn can't # Theano can handle that case, but cudnn can't
......
...@@ -5,7 +5,7 @@ import pytest ...@@ -5,7 +5,7 @@ import pytest
import numpy as np import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as tt
from theano.gpuarray import GpuArrayType from theano.gpuarray import GpuArrayType
from theano.gpuarray.reduction import GpuMaxAndArgmax from theano.gpuarray.reduction import GpuMaxAndArgmax
...@@ -96,7 +96,7 @@ class BaseTest: ...@@ -96,7 +96,7 @@ class BaseTest:
def get_host_tensor(self): def get_host_tensor(self):
broadcastable = (False,) * self.tensor_size broadcastable = (False,) * self.tensor_size
return T.tensor(self.dtype, broadcastable) return tt.tensor(self.dtype, broadcastable)
def get_gpu_tensor(self): def get_gpu_tensor(self):
broadcastable = (False,) * self.tensor_size broadcastable = (False,) * self.tensor_size
...@@ -116,7 +116,7 @@ class BaseTest: ...@@ -116,7 +116,7 @@ class BaseTest:
M = self.get_host_tensor() M = self.get_host_tensor()
f = theano.function( f = theano.function(
[M], [M],
[T.max(M, axis=axis), T.argmax(M, axis=axis)], [tt.max(M, axis=axis), tt.argmax(M, axis=axis)],
name="shape:" + str(test_tensor.shape) + "/axis:" + str(axis) + "/HOST", name="shape:" + str(test_tensor.shape) + "/axis:" + str(axis) + "/HOST",
mode=mode_without_gpu, mode=mode_without_gpu,
) )
...@@ -131,7 +131,7 @@ class BaseTest: ...@@ -131,7 +131,7 @@ class BaseTest:
M = self.get_gpu_tensor() M = self.get_gpu_tensor()
f = theano.function( f = theano.function(
[M], [M],
[T.max(M, axis=axis), T.argmax(M, axis=axis)], [tt.max(M, axis=axis), tt.argmax(M, axis=axis)],
name="shape:" + str(test_gpu_tensor.shape) + "/axis:" + str(axis) + "/GPU", name="shape:" + str(test_gpu_tensor.shape) + "/axis:" + str(axis) + "/GPU",
mode=mode_with_gpu, mode=mode_with_gpu,
) )
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论