提交 4a167629 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merge pull request #289 from nouiz/fix_import_floatX

Fix an import case and made more test run in floatX
...@@ -502,6 +502,26 @@ Final version ...@@ -502,6 +502,26 @@ Final version
double = Double() double = Double()
DeepCopyOp
==========
We have an internal Op called DeepCopyOp. It is used to make sure we
respect the user vs Theano memory region as described in the :ref:`tutorial
<aliasing>`. Theano has a Python implementation that calls the object's
``copy()`` or ``deepcopy()`` method for Theano types for which it does not
know how to generate C code.
You can implement c_code for this op. You register it like this:
.. code-block:: python
theano.compile.function_module.register_DeepCopyOp_c_code(YOUR_TYPE_CLASS, THE_C_CODE)
In your C code, you should use %(iname)s and %(oname)s to represent
the C variable names of the DeepCopyOp input and output
respectively. See an example for the type ``CudaNdarrayType`` (GPU array)
in the file `theano/sandbox/cuda/type.py`.
Output Guard Output Guard
============ ============
......
...@@ -129,7 +129,21 @@ class AliasedMemoryError(Exception): ...@@ -129,7 +129,21 @@ class AliasedMemoryError(Exception):
### Function ### Function
### ###
def register_DeepCopyOp_c_code(typ, code):
""" Tell DeepCopyOp how to generate C code for a Theano Type
:param typ: A Theano type. It must be the Theano class itself and not an
instance of the class.
:param code: C code that deep copies the Theano type 'typ'.
Use %(iname)s and %(oname)s for the input and output C
variable names respectively.
"""
DeepCopyOp.c_codes[typ] = code
class DeepCopyOp(theano.gof.Op): class DeepCopyOp(theano.gof.Op):
c_codes = {} # Theano Type, code
def __init__(self): def __init__(self):
pass pass
...@@ -175,19 +189,8 @@ class DeepCopyOp(theano.gof.Op): ...@@ -175,19 +189,8 @@ class DeepCopyOp(theano.gof.Op):
} }
"""%locals() """%locals()
elif isinstance(node.inputs[0].type, theano.sandbox.cuda.CudaNdarrayType): elif node.inputs[0].type.__class__ in self.c_codes:
return """ return self.c_codes[node.inputs[0].type.__class__] % locals()
Py_XDECREF(%(oname)s);
%(oname)s = (CudaNdarray*)CudaNdarray_Copy(%(iname)s);
if (!%(oname)s)
{
PyErr_SetString(PyExc_ValueError, "DeepCopyOp: the copy failed!");
%(fail)s;
}
"""%locals()
else: else:
super(DeepCopyOp, self).c_code(node, name, inames, onames, sub) super(DeepCopyOp, self).c_code(node, name, inames, onames, sub)
......
""" """
This file test tensor op that should also operate on CudaNdaray. This file test tensor op that should also operate on CudaNdaray.
""" """
import numpy import copy
from nose.plugins.skip import SkipTest
from theano import tensor import numpy
import theano import theano
from theano import tensor
import theano.tensor as T import theano.tensor as T
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
from nose.plugins.skip import SkipTest
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
if cuda.cuda_available == False: if cuda.cuda_available == False:
raise SkipTest('Optional package cuda disabled') raise SkipTest('Optional package cuda disabled')
...@@ -105,3 +106,24 @@ def test_may_share_memory_cuda(): ...@@ -105,3 +106,24 @@ def test_may_share_memory_cuda():
raise Exception("An error was expected") raise Exception("An error was expected")
except TypeError: except TypeError:
pass pass
def test_deepcopy():
a = cuda.fmatrix()
a_v = cuda.CudaNdarray(numpy.zeros((3, 4), dtype='float32'))
# We force the c code to check that we generate c code
mode = theano.Mode("c", mode_with_gpu.optimizer)
f = theano.function([a], a, mode=mode)
theano.printing.debugprint(f)
out = f(a_v)
assert out is not a_v
assert numpy.allclose(numpy.asarray(a_v), numpy.asarray(out))
# We force the python linker as the default code should work for this op
mode = theano.Mode("py", mode_with_gpu.optimizer)
f = theano.function([a], a, mode=mode)
theano.printing.debugprint(f)
out = f(a_v)
assert out is not a_v
assert numpy.allclose(numpy.asarray(a_v), numpy.asarray(out))
...@@ -356,6 +356,19 @@ class CudaNdarrayType(Type): ...@@ -356,6 +356,19 @@ class CudaNdarrayType(Type):
# 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(CudaNdarrayType) theano.compile.mode.register_OutputGuard_c_code(CudaNdarrayType)
# Register CudaNdarrayType to the DeepCopyOp list of types with c code.
theano.compile.function_module.register_DeepCopyOp_c_code(CudaNdarrayType, """
Py_XDECREF(%(oname)s);
%(oname)s = (CudaNdarray*)CudaNdarray_Copy(%(iname)s);
if (!%(oname)s)
{
PyErr_SetString(PyExc_ValueError, "DeepCopyOp: the copy failed!");
%(fail)s;
}
""")
# THIS WORKS # THIS WORKS
# But CudaNdarray instances don't compare equal to one another, and what about __hash__ ? # But CudaNdarray instances don't compare equal to one another, and what about __hash__ ?
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论