提交 74138d60 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Prettify things and add a full example based on the tests.

上级 17712dfc
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
Extending Theano with a GPU Op Extending Theano with a GPU Op
============================== ==============================
.. note::
This covers the :ref:`gpuarray <gpuarray>` back-end for the GPU.
This tutorial covers how to extend Theano with an op that offers a GPU This tutorial covers how to extend Theano with an op that offers a GPU
implementation. It assumes you are familiar with how to write new implementation. It assumes you are familiar with how to write new
Theano ops. If that is not the case you should probably follow the Theano ops. If that is not the case you should probably follow the
...@@ -15,7 +19,7 @@ Writing a new GPU op can be done in python for some simple tasks, but ...@@ -15,7 +19,7 @@ Writing a new GPU op can be done in python for some simple tasks, but
will usually done in C to access the complete API and avoid paying the will usually done in C to access the complete API and avoid paying the
overhead of a python function call. overhead of a python function call.
Dealing with the context Dealing With the Context
======================== ========================
One of the major differences with GPU ops is that they require a One of the major differences with GPU ops is that they require a
...@@ -82,7 +86,7 @@ the example of :class:`theano.gpuarray.basic_ops.GpuFromHost` or ...@@ -82,7 +86,7 @@ the example of :class:`theano.gpuarray.basic_ops.GpuFromHost` or
:class:`theano.gpuarray.basic_ops.GpuEye`. This is not a case that :class:`theano.gpuarray.basic_ops.GpuEye`. This is not a case that
you should encounter often, so it will not be covered further. you should encounter often, so it will not be covered further.
Defining new kernels Defining New Kernels
==================== ====================
If your op needs to do some transformation on the data, chances are If your op needs to do some transformation on the data, chances are
...@@ -161,7 +165,39 @@ An example calling the above kernel would be:: ...@@ -161,7 +165,39 @@ An example calling the above kernel would be::
// ... // ...
Wrapping exisiting libraries A Complete Example
==================
This is a complete example using both approches for a implementation
of the Eye operation.
GpuKernelBase
-------------
Python File
~~~~~~~~~~~
.. literalinclude:: ../../theano/gpuarray/basic_ops.py
:language: python
:pyobject: GpuEye
CGpuKernelBase
--------------
Python File
~~~~~~~~~~~
.. literalinclude:: ../../theano/gpuarray/tests/test_cgpukernelbase.py
:language: python
:pyobject: GpuEye
``tstgpueye.c``
~~~~~~~~~~~~~~~
.. literalinclude:: ../../theano/gpuarray/tests/tstgpueye.c
:language: C
Wrapping Exisiting Libraries
============================ ============================
PyCUDA PyCUDA
......
...@@ -3,7 +3,7 @@ import numpy ...@@ -3,7 +3,7 @@ import numpy
from six.moves import xrange from six.moves import xrange
import theano import theano
from theano import tensor, config, Apply from theano import tensor, config, Apply, Op
from theano.gradient import grad_undefined from theano.gradient import grad_undefined
from .config import mode_with_gpu, test_ctx_name from .config import mode_with_gpu, test_ctx_name
...@@ -15,7 +15,7 @@ from ..type import GpuArrayType, get_context ...@@ -15,7 +15,7 @@ from ..type import GpuArrayType, get_context
from pygpu.gpuarray import dtype_to_typecode from pygpu.gpuarray import dtype_to_typecode
class TSTGpuEye(CGpuKernelBase): class GpuEye(CGpuKernelBase, Op):
""" """
Eye for GPU. Eye for GPU.
...@@ -61,7 +61,7 @@ class TSTGpuEye(CGpuKernelBase): ...@@ -61,7 +61,7 @@ class TSTGpuEye(CGpuKernelBase):
def test_cgpukernelbase(): def test_cgpukernelbase():
op = TSTGpuEye(dtype='int32', context_name=test_ctx_name) op = GpuEye(dtype='int32', context_name=test_ctx_name)
f = theano.function([], op(4, 5), mode=mode_with_gpu) f = theano.function([], op(4, 5), mode=mode_with_gpu)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论