提交 eace991b authored 作者: nouiz's avatar nouiz

Merge pull request #562 from lamblin/test_preallocated_output_rebase

Checks for preallocated output memory, take 2
...@@ -380,7 +380,7 @@ import theano and print the config variable, as in: ...@@ -380,7 +380,7 @@ import theano and print the config variable, as in:
.. attribute:: config.DebugMode.check_preallocated_output .. attribute:: config.DebugMode.check_preallocated_output
Default: ``'ALL'`` Default: ``''``
A list of kinds of preallocated memory to use as output buffers for A list of kinds of preallocated memory to use as output buffers for
each Op's computations, separated by ``:``. Implemented modes are: each Op's computations, separated by ``:``. Implemented modes are:
...@@ -388,6 +388,8 @@ import theano and print the config variable, as in: ...@@ -388,6 +388,8 @@ import theano and print the config variable, as in:
* ``"previous"``: reuse previously-returned memory, * ``"previous"``: reuse previously-returned memory,
* ``"c_contiguous"``: newly-allocated C-contiguous memory, * ``"c_contiguous"``: newly-allocated C-contiguous memory,
* ``"f_contiguous"``: newly-allocated Fortran-contiguous memory, * ``"f_contiguous"``: newly-allocated Fortran-contiguous memory,
* ``"strided"``: non-contiguous memory with various stride patterns,
* ``"wrong_size"``: memory with bigger or smaller dimensions,
* ``"ALL"``: placeholder for all of the above. * ``"ALL"``: placeholder for all of the above.
In order not to test with preallocated memory, use an empty string, ``""``. In order not to test with preallocated memory, use an empty string, ``""``.
......
from theano import gof from theano import gof
from theano import gradient as G from theano import gradient as G
from function_module import orig_function from theano.compile.function_module import orig_function
from theano.gof import ops_with_inner_function
class OpFromGraph(gof.Op): class OpFromGraph(gof.Op):
...@@ -99,3 +100,7 @@ class OpFromGraph(gof.Op): ...@@ -99,3 +100,7 @@ class OpFromGraph(gof.Op):
return [go(*(inputs + output_grads)) for go in self.grad_ops] return [go(*(inputs + output_grads)) for go in self.grad_ops]
else: else:
raise NotImplementedError raise NotImplementedError
# Since OpFromGraph contains a Theano compiled function, we should let
# DebugMode know about it
ops_with_inner_function[OpFromGraph] = 'fn'
...@@ -264,7 +264,10 @@ def test_stochasticoptimization(): ...@@ -264,7 +264,10 @@ def test_stochasticoptimization():
try: try:
theano.function([a, b], theano.function([a, b],
theano.tensor.add(a, b), theano.tensor.add(a, b),
mode=debugmode.DebugMode(optimizer=opt, check_c_code=True)) mode=debugmode.DebugMode(
optimizer=opt,
check_c_code=True,
stability_patience=max(2, config.DebugMode.patience)))
except debugmode.StochasticOrder: except debugmode.StochasticOrder:
return # TEST PASS return # TEST PASS
assert False assert False
......
...@@ -18,7 +18,7 @@ from link import \ ...@@ -18,7 +18,7 @@ from link import \
Container, Linker, LocalLinker, PerformLinker, WrapLinker, WrapLinkerMany Container, Linker, LocalLinker, PerformLinker, WrapLinker, WrapLinkerMany
from op import \ from op import \
Op, PureOp Op, PureOp, ops_with_inner_function
from opt import (Optimizer, optimizer, SeqOptimizer, from opt import (Optimizer, optimizer, SeqOptimizer,
MergeOptimizer, MergeOptMerge, MergeOptimizer, MergeOptMerge,
......
...@@ -717,3 +717,17 @@ def get_debug_values(*args): ...@@ -717,3 +717,17 @@ def get_debug_values(*args):
return rval return rval
return [tuple(rval)] return [tuple(rval)]
ops_with_inner_function = {}
"""
Registry of Ops that have an inner compiled Theano function.
The keys are Op classes (not instances), and values are the name of the
attribute that contains the function. For instance, if the function is
self.fn, the value will be 'fn'.
We need that to be able not to run debug checks a number of times that is
exponential in the nesting level of those ops.
For instance, Scan will be registered here.
"""
...@@ -37,11 +37,6 @@ def my_rand(*shape): ...@@ -37,11 +37,6 @@ def my_rand(*shape):
return theano._asarray(numpy.random.rand(*shape), dtype='float32') return theano._asarray(numpy.random.rand(*shape), dtype='float32')
def transpose(cuda_mat):
# The easiest way to transpose a cuda matrix for now
return tcn.dimshuffle(cuda_mat, [1, 0])
def test_dot22(): def test_dot22():
def cmp(a_shp, b_shp): def cmp(a_shp, b_shp):
a0 = my_rand(*a_shp) a0 = my_rand(*a_shp)
......
...@@ -54,6 +54,11 @@ class CudaNdarrayType(Type): ...@@ -54,6 +54,11 @@ class CudaNdarrayType(Type):
A cyclic dependency is avoided by not hardcoding this class. A cyclic dependency is avoided by not hardcoding this class.
""" """
value_zeros = staticmethod(cuda.CudaNdarray.zeros)
"""
Create an CudaNdarray full of 0 values
"""
def __init__(self, broadcastable, name=None, dtype=None): def __init__(self, broadcastable, name=None, dtype=None):
if dtype != None and dtype != 'float32': if dtype != None and dtype != 'float32':
raise TypeError('%s only supports dtype float32 for now. Tried ' raise TypeError('%s only supports dtype float32 for now. Tried '
......
...@@ -278,8 +278,8 @@ class Scan(PureOp): ...@@ -278,8 +278,8 @@ class Scan(PureOp):
str(outer_mitsot), str(outer_mitsot),
argoffset + idx, argoffset + idx,
outer_mitsot.type.dtype, outer_mitsot.type.dtype,
otuer_mitsot.type.ndim, outer_mitsot.type.ndim,
str(inner_mitsot[ipos + k]), str(inner_mitsots[ipos + k]),
inner_mitsots[ipos + k].type.dtype, inner_mitsots[ipos + k].type.dtype,
inner_mitsots[ipos + k].type.ndim)) inner_mitsots[ipos + k].type.ndim))
ipos += len(itaps) ipos += len(itaps)
...@@ -1676,6 +1676,11 @@ class Scan(PureOp): ...@@ -1676,6 +1676,11 @@ class Scan(PureOp):
return final_outs return final_outs
# Since Scan is an op that contains a Theano compiled function, it is
# useful to let DebugMode know about it.
gof.ops_with_inner_function[Scan] = 'fn'
@theano.compile.profilemode.register_profiler_printer @theano.compile.profilemode.register_profiler_printer
def profile_printer(fct_name, compile_time, fct_call_time, fct_call, def profile_printer(fct_name, compile_time, fct_call_time, fct_call,
apply_time, apply_cimpl, message, outputs_size, apply_time, apply_cimpl, message, outputs_size,
......
...@@ -1024,6 +1024,13 @@ class TensorType(Type): ...@@ -1024,6 +1024,13 @@ class TensorType(Type):
else: else:
return () return ()
def value_zeros(self, shape):
"""
Create an numpy ndarray full of 0 values.
"""
return numpy.zeros(shape, dtype=self.dtype)
# Register CudaNdarrayType to the OutputGuard list of known types # Register CudaNdarrayType to the OutputGuard list of known types
# to have OutputGuard generate C code for this type. # to have OutputGuard generate C code for this type.
theano.compile.mode.register_OutputGuard_c_code(TensorType) theano.compile.mode.register_OutputGuard_c_code(TensorType)
......
...@@ -742,34 +742,45 @@ class Elemwise(Op): ...@@ -742,34 +742,45 @@ class Elemwise(Op):
raise ValueError('\n'.join(msg_chunks)) raise ValueError('\n'.join(msg_chunks))
else: else:
raise ValueError(base_exc_str) raise ValueError(base_exc_str)
# Other mismatches will be caught by the ufunc
# Determine the shape of outputs
out_shape = []
for values in zip(*[input.shape for input in inputs]):
if numpy.prod(values) == 0:
# All non-broadcasted dimensions should be zero
assert max(values) <= 1
out_shape.append(0)
else:
out_shape.append(max(values))
out_shape = tuple(out_shape)
if not self.inplace_pattern: if not self.inplace_pattern:
for output, storage in zip(node.outputs, output_storage): for output, storage in zip(node.outputs, output_storage):
odat = storage[0] odat = storage[0]
shape = [max(values)
for values in zip(*[input.shape for input in inputs])]
if odat is not None: if odat is not None:
# reuse storage if we can if odat.shape != out_shape:
odat.resize(shape, refcheck=0) # It is unsafe to try to resize odat,
else: # we have to allocate output storage.
odat = numpy.ndarray(shape, dtype=output.type.dtype) odat = None
if odat is None:
odat = numpy.ndarray(out_shape, dtype=output.type.dtype)
storage[0] = odat storage[0] = odat
else: else:
for i, (output, storage) in enumerate(zip(node.outputs, for i, (output, storage) in enumerate(
output_storage)): zip(node.outputs, output_storage)):
#i is an output idx #i is an output idx
if i in self.inplace_pattern: if i in self.inplace_pattern:
odat = inputs[self.inplace_pattern[i]] odat = inputs[self.inplace_pattern[i]]
else: else:
odat = storage[0] odat = storage[0]
shape = [max(values)
for values in zip(*[input.shape
for input in inputs])]
if odat is not None: if odat is not None:
odat.resize(shape, refcheck=0) if odat.shape != out_shape:
else: # It is unsafe to try to resize odat,
odat = numpy.ndarray(shape, dtype=output.type.dtype) # we have to allocate output storage.
odat = None
if odat is None:
odat = numpy.ndarray(out_shape,
dtype=output.type.dtype)
storage[0] = odat storage[0] = odat
ufunc_args = inputs # + output_storage ufunc_args = inputs # + output_storage
...@@ -825,21 +836,16 @@ class Elemwise(Op): ...@@ -825,21 +836,16 @@ class Elemwise(Op):
# always return an ndarray with dtype object # always return an ndarray with dtype object
variable = numpy.asarray(variable, dtype=nout.dtype) variable = numpy.asarray(variable, dtype=nout.dtype)
if (hasattr(variable, 'shape') # The storage has been resized earlier.
and storage[0].shape != variable.shape): if hasattr(variable, 'shape'):
if numpy.prod(variable.shape) == 0: assert storage[0].shape == variable.shape
# numpy don't resize from a shape (1,5) to (0,5)
# This bypass the inplace...
# But I it is important in this case.
storage[0] = variable
continue
storage[0].resize(variable.shape)
if storage[0].shape:
storage[0][:] = variable
else: else:
storage[0].itemset(variable) # If variable has not shape, then it is a scalar.
assert numpy.prod(storage[0].shape) == 1
storage[0][...] = variable
assert str(storage[0].dtype) != 'object' assert str(storage[0].dtype) != 'object'
# the following should be used instead of the previous loop, # the following should be used instead of the previous loop,
# unfortunately it tends to segfault # unfortunately it tends to segfault
# self.ufunc(*(ufunc_args+[s[0] for s in output_storage])) # self.ufunc(*(ufunc_args+[s[0] for s in output_storage]))
......
...@@ -521,7 +521,7 @@ class MakeVector(T.Op): ...@@ -521,7 +521,7 @@ class MakeVector(T.Op):
def perform(self, node, inputs, out_): def perform(self, node, inputs, out_):
out, = out_ out, = out_
# not calling theano._asarray as optimization # not calling theano._asarray as optimization
if out[0] is None: if (out[0] is None) or (out[0].size != len(inputs)):
out[0] = theano._asarray(inputs, dtype=node.outputs[0].dtype) out[0] = theano._asarray(inputs, dtype=node.outputs[0].dtype)
else: else:
# assume that out has correct dtype. there is no cheap way to check # assume that out has correct dtype. there is no cheap way to check
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论