提交 9be8ca6a authored 作者: Frederic Bastien's avatar Frederic Bastien

Fix import by making a registry system for the c code of DeepCopyOp.

上级 7de7d093
......@@ -502,6 +502,26 @@ Final version
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 have a python implementation that call the object
``copy()`` or``deepcopy()`` method for Theano Type that it don't 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 name of the DeepCopyOp input and output
respectively. See an example for the gpu object in the file
`theano/sandbox/cuda/type.py`.
Output Guard
============
......
......@@ -129,7 +129,21 @@ class AliasedMemoryError(Exception):
### 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 theano class itself and not an
object of the class.
:param code: C code that deep copy the Theano type 'typ'.
use %(iname)s and %(oname)s for the input and output c
variable name respectively.
"""
DeepCopyOp.c_codes[typ] = code
class DeepCopyOp(theano.gof.Op):
c_codes = {} # Theano Type, code
def __init__(self):
pass
......@@ -175,19 +189,8 @@ class DeepCopyOp(theano.gof.Op):
}
"""%locals()
elif isinstance(node.inputs[0].type, theano.sandbox.cuda.CudaNdarrayType):
return """
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()
elif node.inputs[0].type.__class__ in self.c_codes:
return self.c_codes[node.inputs[0].type.__class__] % locals()
else:
super(DeepCopyOp, self).c_code(node, name, inames, onames, sub)
......
"""
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
from theano import tensor
import theano.tensor as T
# Skip test if cuda_ndarray is not available.
from nose.plugins.skip import SkipTest
import theano.sandbox.cuda as cuda
if cuda.cuda_available == False:
raise SkipTest('Optional package cuda disabled')
......@@ -105,3 +106,24 @@ def test_may_share_memory_cuda():
raise Exception("An error was expected")
except TypeError:
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):
# to have OutputGuard generate C code for this type.
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
# But CudaNdarray instances don't compare equal to one another, and what about __hash__ ?
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论