提交 212b5fe8 authored 作者: lamblin's avatar lamblin

Merge pull request #848 from nouiz/mixed

Mixed
...@@ -132,6 +132,13 @@ def grad_sources_inputs(sources, graph_inputs, warn_type=True): ...@@ -132,6 +132,13 @@ def grad_sources_inputs(sources, graph_inputs, warn_type=True):
#if all output gradients are None, continue #if all output gradients are None, continue
if all(map(lambda x: x is None, g_outputs)): continue if all(map(lambda x: x is None, g_outputs)): continue
#Disable all grad operation on complex. verify_grad don't
#support them and we don't know we want to handle them.
for var in node.inputs + node.outputs:
if (hasattr(var.type, 'dtype') and "complex" in var.type.dtype):
raise Exception("We do not support grad/Rop/Lop/verify_grad"
" on complex.")
output_arg = g_outputs output_arg = g_outputs
input_arg = node.inputs input_arg = node.inputs
......
...@@ -193,13 +193,20 @@ def print_results(pr, results_urls, unavailable_pythons=unavailable_pythons): ...@@ -193,13 +193,20 @@ def print_results(pr, results_urls, unavailable_pythons=unavailable_pythons):
def dump_results(num, results, pr): def dump_results(num, results, pr):
with open(os.path.join(basedir, 'lastresults.pkl'), 'wb') as f: f = open(os.path.join(basedir, 'lastresults.pkl'), 'wb')
try:
pickle.dump((num, results, pr, unavailable_pythons), f) pickle.dump((num, results, pr, unavailable_pythons), f)
finally:
f.close()
def load_results(): def load_results():
with open(os.path.join(basedir, 'lastresults.pkl'), 'rb') as f: f = open(os.path.join(basedir, 'lastresults.pkl'), 'rb')
return pickle.load(f) try:
ret = pickle.load(f)
finally:
f.close()
return ret
def save_logs(results, pr): def save_logs(results, pr):
...@@ -211,8 +218,11 @@ def save_logs(results, pr): ...@@ -211,8 +218,11 @@ def save_logs(results, pr):
result_locn = os.path.abspath(os.path.join('venv-%s' % py, result_locn = os.path.abspath(os.path.join('venv-%s' % py,
pr['head']['sha'][:7] + ".log")) pr['head']['sha'][:7] + ".log"))
with io.open(result_locn, 'w', encoding='utf-8') as f: f = io.open(result_locn, 'w', encoding='utf-8')
try:
f.write(log) f.write(log)
finally:
f.close()
results_paths.append((py, False, result_locn, missing_libraries)) results_paths.append((py, False, result_locn, missing_libraries))
......
...@@ -251,7 +251,9 @@ class PycudaElemwiseSourceModuleOp(GpuOp): ...@@ -251,7 +251,9 @@ class PycudaElemwiseSourceModuleOp(GpuOp):
#TODO support broadcast! #TODO support broadcast!
#TODO assert all input have the same shape #TODO assert all input have the same shape
z, = out z, = out
if z[0] is None or z[0].shape != inputs[0].shape: if (z[0] is None or
z[0].shape != inputs[0].shape or
not z[0].is_c_contiguous()):
z[0] = theano.sandbox.cuda.CudaNdarray.zeros(inputs[0].shape) z[0] = theano.sandbox.cuda.CudaNdarray.zeros(inputs[0].shape)
if inputs[0].shape != inputs[1].shape: if inputs[0].shape != inputs[1].shape:
raise TypeError("PycudaElemwiseSourceModuleOp:" raise TypeError("PycudaElemwiseSourceModuleOp:"
...@@ -339,7 +341,9 @@ class PycudaElemwiseSourceModuleMakeThunkOp(Op): ...@@ -339,7 +341,9 @@ class PycudaElemwiseSourceModuleMakeThunkOp(Op):
def thunk(): def thunk():
z = outputs[0] z = outputs[0]
if z[0] is None or z[0].shape != inputs[0][0].shape: if (z[0] is None or
z[0].shape != inputs[0][0].shape or
not z[0].is_c_contiguous()):
z[0] = theano.sandbox.cuda.CudaNdarray.zeros( z[0] = theano.sandbox.cuda.CudaNdarray.zeros(
inputs[0][0].shape) inputs[0][0].shape)
if inputs[0][0].shape != inputs[1][0].shape: if inputs[0][0].shape != inputs[1][0].shape:
......
...@@ -60,14 +60,16 @@ nvcc_version = None ...@@ -60,14 +60,16 @@ nvcc_version = None
def is_nvcc_available(): def is_nvcc_available():
"""Return True iff the nvcc compiler is found.""" """Return True iff the nvcc compiler is found."""
try: def set_version():
p = subprocess.Popen(['nvcc', '--version'], stdout=subprocess.PIPE, p = subprocess.Popen([nvcc_path, '--version'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
p.wait() p.wait()
s = p.stdout.readlines()[-1].split(',')[1].strip().split() s = p.stdout.readlines()[-1].split(',')[1].strip().split()
assert s[0] == 'release' assert s[0] == 'release'
global nvcc_version global nvcc_version
nvcc_version = s[1] nvcc_version = s[1]
try:
set_version()
return True return True
except Exception: except Exception:
#try to find nvcc into cuda.root #try to find nvcc into cuda.root
...@@ -75,6 +77,10 @@ def is_nvcc_available(): ...@@ -75,6 +77,10 @@ def is_nvcc_available():
if os.path.exists(p): if os.path.exists(p):
global nvcc_path global nvcc_path
nvcc_path = p nvcc_path = p
try:
set_version()
except Exception:
return False
return True return True
else: else:
return False return False
......
...@@ -279,8 +279,10 @@ class PushOutNonSeqScan(gof.Optimizer): ...@@ -279,8 +279,10 @@ class PushOutNonSeqScan(gof.Optimizer):
# Reconstruct node # Reconstruct node
nwScan = scan_op.Scan(op_ins, op_outs, op.info) nwScan = scan_op.Scan(op_ins, op_outs, op.info)
nw_node = nwScan.make_node(* (node.inputs + nw_outer)) nw_node = nwScan.make_node(* (node.inputs + nw_outer))
fgraph.replace_all_validate(zip(node.outputs, nw_node.outputs), fgraph.replace_all_validate_remove(
reason='scan_push_computation_out') zip(node.outputs, nw_node.outputs),
remove=[node],
reason='scan_push_computation_out')
return True return True
elif to_keep == []: elif to_keep == []:
# Nothing in the inner graph should be kept # Nothing in the inner graph should be kept
...@@ -358,8 +360,9 @@ class ScanInplaceOptimizer(Optimizer): ...@@ -358,8 +360,9 @@ class ScanInplaceOptimizer(Optimizer):
new_outs = new_op.make_node(*inputs).outputs new_outs = new_op.make_node(*inputs).outputs
try: try:
fgraph.replace_all_validate( fgraph.replace_all_validate_remove(
zip(node.outputs, new_outs), zip(node.outputs, new_outs),
remove=[node],
reason=self.__class__.__name__) reason=self.__class__.__name__)
op = new_op op = new_op
node = new_outs[0].owner node = new_outs[0].owner
...@@ -826,7 +829,9 @@ class ScanSaveMem(gof.Optimizer): ...@@ -826,7 +829,9 @@ class ScanSaveMem(gof.Optimizer):
nw_pos = compress_map[idx] nw_pos = compress_map[idx]
old_new += [(o, new_outs[nw_pos])] old_new += [(o, new_outs[nw_pos])]
fgraph.replace_all_validate(old_new, reason='scan_save_mem') fgraph.replace_all_validate_remove(old_new,
remove=[node],
reason='scan_save_mem')
def apply(self, fgraph): def apply(self, fgraph):
...@@ -1021,7 +1026,9 @@ class ScanMerge(gof.Optimizer): ...@@ -1021,7 +1026,9 @@ class ScanMerge(gof.Optimizer):
for subset in all_sets: for subset in all_sets:
if len(subset) > 1: if len(subset) > 1:
proposal = self.merge(subset) proposal = self.merge(subset)
fgraph.replace_all_validate(proposal, reason='scan_merge') fgraph.replace_all_validate_remove(proposal,
remove=subset,
reason='scan_merge')
# after const merge but before stabilize so that we can have identity # after const merge but before stabilize so that we can have identity
......
...@@ -920,8 +920,8 @@ class T_Scan(unittest.TestCase): ...@@ -920,8 +920,8 @@ class T_Scan(unittest.TestCase):
# equivalent is done # equivalent is done
(theano_x0, theano_x1) = f9(vu0, vu1, vu2, vx0, vx1) (theano_x0, theano_x1) = f9(vu0, vu1, vu2, vx0, vx1)
# assert that theano does what it should # assert that theano does what it should
assert numpy.allclose(theano_x0, numpy_x0) assert numpy.allclose(theano_x0, numpy_x0), (theano_x0, numpy_x0)
assert numpy.allclose(theano_x1, numpy_x1) assert numpy.allclose(theano_x1, numpy_x1), (theano_x1, numpy_x1)
# assert that it was done in place # assert that it was done in place
# not that x0 should not be inplace of vu2 because you are using # not that x0 should not be inplace of vu2 because you are using
# past values of u2, and therefore you are not allowed to work # past values of u2, and therefore you are not allowed to work
......
...@@ -1633,15 +1633,16 @@ class Prepend_scalar_constant_to_each_row(gof.Op): ...@@ -1633,15 +1633,16 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
def make_node(self, mat): def make_node(self, mat):
#check type of input #check type of input
if not isinstance(mat,gof.Variable) or not mat.type==tensor.matrix().type:
raise TypeError("Expected a matrix as input")
x = tensor.as_tensor_variable(mat) x = tensor.as_tensor_variable(mat)
if not mat.type.broadcastable == (False, False):
raise TypeError("Expected a matrix as input")
y = tensor.as_tensor_variable(self.val) y = tensor.as_tensor_variable(self.val)
assert y.ndim == 0
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError( TypeError(
"the value to prepend don't have the same type as the matrix") "the value to prepend don't have the same type as the matrix")
node = Apply(op=self, inputs=[mat], outputs=[tensor.matrix()]) node = Apply(op=self, inputs=[mat], outputs=[mat.type()])
return node return node
def perform(self, node, inp, out): def perform(self, node, inp, out):
...@@ -1685,18 +1686,18 @@ class Prepend_scalar_to_each_row(gof.Op): ...@@ -1685,18 +1686,18 @@ class Prepend_scalar_to_each_row(gof.Op):
def make_node(self, val, mat): def make_node(self, val, mat):
#check type of input #check type of input
x = tensor.as_tensor_variable(mat)
if isinstance(val, float): if isinstance(val, float):
val = scalar.constant(val) val = scalar.constant(val)
if (not isinstance(mat, gof.Variable) or if not mat.type.broadcastable == (False, False):
not mat.type == tensor.matrix().type):
raise TypeError("Expected a matrix as input") raise TypeError("Expected a matrix as input")
x = tensor.as_tensor_variable(mat)
y = tensor.as_tensor_variable(val) y = tensor.as_tensor_variable(val)
assert y.ndim == 0
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError( TypeError(
"the value to prepend don't have the same type as the matrix") "the value to prepend don't have the same type as the matrix")
node = Apply(op=self, inputs=[val, mat], outputs=[tensor.matrix()]) node = Apply(op=self, inputs=[val, mat], outputs=[mat.type()])
return node return node
def perform(self, node, inp, out): def perform(self, node, inp, out):
......
...@@ -277,7 +277,7 @@ class T_prepend(utt.InferShapeTester): ...@@ -277,7 +277,7 @@ class T_prepend(utt.InferShapeTester):
x = tensor.matrix('x') x = tensor.matrix('x')
y = Prepend_scalar_constant_to_each_row(4.)(x) y = Prepend_scalar_constant_to_each_row(4.)(x)
f = theano.function([x], y) f = theano.function([x], y)
m = numpy.random.rand(3, 5) m = numpy.random.rand(3, 5).astype(config.floatX)
my = f(m) my = f(m)
self.assertTrue(my.shape == (3, 6), my.shape) self.assertTrue(my.shape == (3, 6), my.shape)
self.assertTrue(numpy.all(my[:, 0] == 4.0)) self.assertTrue(numpy.all(my[:, 0] == 4.0))
......
...@@ -46,6 +46,7 @@ class TestRealImag(unittest.TestCase): ...@@ -46,6 +46,7 @@ class TestRealImag(unittest.TestCase):
assert numpy.all(rval == mval[0]), (rval,mval[0]) assert numpy.all(rval == mval[0]), (rval,mval[0])
assert numpy.all(ival == mval[1]), (ival, mval[1]) assert numpy.all(ival == mval[1]), (ival, mval[1])
@dec.knownfailureif(True,"Complex grads not enabled, see #178")
def test_complex_grads(self): def test_complex_grads(self):
def f(m): def f(m):
c = complex(m[0], m[1]) c = complex(m[0], m[1])
...@@ -103,6 +104,7 @@ class TestRealImag(unittest.TestCase): ...@@ -103,6 +104,7 @@ class TestRealImag(unittest.TestCase):
print e.analytic_grad print e.analytic_grad
raise raise
@dec.knownfailureif(True,"Complex grads not enabled, see #178")
def test_polar_grads(self): def test_polar_grads(self):
def f(m): def f(m):
c = complex_from_polar(abs(m[0]), m[1]) c = complex_from_polar(abs(m[0]), m[1])
...@@ -112,6 +114,7 @@ class TestRealImag(unittest.TestCase): ...@@ -112,6 +114,7 @@ class TestRealImag(unittest.TestCase):
mval = numpy.asarray(rng.randn(2,5)) mval = numpy.asarray(rng.randn(2,5))
utt.verify_grad(f, [mval]) utt.verify_grad(f, [mval])
@dec.knownfailureif(True,"Complex grads not enabled, see #178")
def test_abs_grad(self): def test_abs_grad(self):
def f(m): def f(m):
c = complex(m[0], m[1]) c = complex(m[0], m[1])
......
...@@ -628,6 +628,8 @@ class T_sum_dtype(unittest.TestCase): ...@@ -628,6 +628,8 @@ class T_sum_dtype(unittest.TestCase):
sum_var = x.sum(dtype=output_dtype, axis=axis) sum_var = x.sum(dtype=output_dtype, axis=axis)
assert sum_var.dtype == output_dtype assert sum_var.dtype == output_dtype
if "complex" in input_dtype:
continue
# Check that we can take the gradient # Check that we can take the gradient
grad_var = tensor.grad(sum_var.sum(), x, grad_var = tensor.grad(sum_var.sum(), x,
disconnected_inputs='ignore') disconnected_inputs='ignore')
...@@ -676,6 +678,8 @@ class T_mean_dtype(unittest.TestCase): ...@@ -676,6 +678,8 @@ class T_mean_dtype(unittest.TestCase):
assert mean_var.dtype == sum_dtype, (mean_var.dtype, sum_dtype) assert mean_var.dtype == sum_dtype, (mean_var.dtype, sum_dtype)
# Check that we can take the gradient, when implemented # Check that we can take the gradient, when implemented
if "complex" in mean_var.dtype:
continue
try: try:
grad_var = tensor.grad(mean_var.sum(), x, grad_var = tensor.grad(mean_var.sum(), x,
disconnected_inputs='ignore') disconnected_inputs='ignore')
...@@ -729,6 +733,8 @@ class T_prod_dtype(unittest.TestCase): ...@@ -729,6 +733,8 @@ class T_prod_dtype(unittest.TestCase):
prod_var = x.prod(dtype=output_dtype, axis=axis) prod_var = x.prod(dtype=output_dtype, axis=axis)
assert prod_var.dtype == output_dtype assert prod_var.dtype == output_dtype
if "complex" in output_dtype:
continue
# Check that we can take the gradient # Check that we can take the gradient
grad_var = tensor.grad(prod_var.sum(), x, grad_var = tensor.grad(prod_var.sum(), x,
disconnected_inputs='ignore') disconnected_inputs='ignore')
......
import numpy import numpy
from numpy.testing import dec
import theano import theano
from theano import tensor from theano import tensor
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -37,6 +39,7 @@ class TestFourier(utt.InferShapeTester): ...@@ -37,6 +39,7 @@ class TestFourier(utt.InferShapeTester):
[numpy.random.rand(12, 4), 0], [numpy.random.rand(12, 4), 0],
self.op_class) self.op_class)
@dec.knownfailureif(True, "Complex grads not enabled, see #178")
def test_gradient(self): def test_gradient(self):
def fft_test1(a): def fft_test1(a):
return self.op(a, None, None) return self.op(a, None, None)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论