• Theano2.1.13-基础知识之PyCUDA、CUDAMat、Gnumpy的兼容


    来自:http://deeplearning.net/software/theano/tutorial/gpu_data_convert.html

    PyCUDA/CUDAMat/Gnumpy compatibility

    一、PyCUDA

         当前,PyCUDA 和Theano使用不同的对象来存储GPU数据。这两种实现支持的是不同的特征集。 Theano的实现是叫做CudaNdarray ,并且支持strides。 同时只支持float32 dtype。 PyCUDA的实现叫做 GPUArray 而且不支持strides。 然而,它可以处理所有的NumPy 和CUDA dtypes。

        我们现在来介绍下如何工作在这两个都有的基对象上,而且也在模仿NumPy。下面有一些资料关于如何在同一个脚本中使用这两个对象。

    1.1 迁移

          你可以使用 theano.misc.pycuda_utils 模块来对 GPUArray和CudaNdarray之间进行转换。函数 to_cudandarray(x,copyif=False) 和 to_gpuarray(x) 返回一个新的对象,该对象占据着和原始对象同一块内存空间。不过它会抛出一个值错误(ValueError)的异常。因为GPUArrays不支持strides,如果CudaNdarray 是strided,那么我们需要对它进行non-strided复制。生成的GPUArray不会在共享同一片内存区域。如果你想要这种行为,那么可以在to_gpuarray中设置 copyif=True 。 

    1.2 用PyCUDA 来编译

          你可以使用 PyCUDA来编译直接工作在CudaNdarrays上的 CUDA 函数。这里是来自文件theano/misc/tests/test_pycuda_theano_simple.py中的例子:

    import sys
    import numpy
    import theano
    import theano.sandbox.cuda as cuda_ndarray
    import theano.misc.pycuda_init
    import pycuda
    import pycuda.driver as drv
    import pycuda.gpuarray
    
    
    def test_pycuda_theano():
        """Simple example with pycuda function and Theano CudaNdarray object."""
        from pycuda.compiler import SourceModule
        mod = SourceModule("""
    __global__ void multiply_them(float *dest, float *a, float *b)
    {
      const int i = threadIdx.x;
      dest[i] = a[i] * b[i];
    }
    """)
    
        multiply_them = mod.get_function("multiply_them")
    
        a = numpy.random.randn(100).astype(numpy.float32)
        b = numpy.random.randn(100).astype(numpy.float32)
    
        # Test with Theano object
        ga = cuda_ndarray.CudaNdarray(a)
        gb = cuda_ndarray.CudaNdarray(b)
        dest = cuda_ndarray.CudaNdarray.zeros(a.shape)
        multiply_them(dest, ga, gb,
                      block=(400, 1, 1), grid=(1, 1))
        assert (numpy.asarray(dest) == a * b).all()

    1.3 Theano 操作,使用一个PyCUDA函数

        你可以在theano op中使用 用PyCUDA编译好的GPU函数:

    import numpy, theano
    import theano.misc.pycuda_init
    from pycuda.compiler import SourceModule
    import theano.sandbox.cuda as cuda
    
    class PyCUDADoubleOp(theano.Op):
        def __eq__(self, other):
            return type(self) == type(other)
        def __hash__(self):
            return hash(type(self))
        def __str__(self):
            return self.__class__.__name__
        def make_node(self, inp):
            inp = cuda.basic_ops.gpu_contiguous(
               cuda.basic_ops.as_cuda_ndarray_variable(inp))
            assert inp.dtype == "float32"
            return theano.Apply(self, [inp], [inp.type()])
        def make_thunk(self, node, storage_map, _, _2):
            mod = SourceModule("""
        __global__ void my_fct(float * i0, float * o0, int size) {
        int i = blockIdx.x * blockDim.x + threadIdx.x;
        if(i<size){
            o0[i] = i0[i] * 2;
        }
      }""")
            pycuda_fct = mod.get_function("my_fct")
            inputs = [ storage_map[v] for v in node.inputs]
            outputs = [ storage_map[v] for v in node.outputs]
            def thunk():
                z = outputs[0]
                if z[0] is None or z[0].shape!=inputs[0][0].shape:
                    z[0] = cuda.CudaNdarray.zeros(inputs[0][0].shape)
                grid = (int(numpy.ceil(inputs[0][0].size / 512.)),1)
                pycuda_fct(inputs[0][0], z[0], numpy.intc(inputs[0][0].size),
                           block=(512, 1, 1), grid=grid)
            thunk.lazy = False
            return thunk

    二、CUDAMat

            这里的函数是用来在CUDAMat对象和 Theano的 CudaNdArray对象之间进行转换的。 它们遵循和theano的PyCUDA函数一样的原则,可以查阅 theano.misc.cudamat_utils.py.

        WARNING: 在这些转换器上,会有一个与stride/shape相关的特殊的问题。为了能够work,需要 transposereshape.等操作..

    三、Gnumpy

       这是介于Gnumpy garray 对象和 Theano CudaNdArray 对象之间的转换函数。也同样相似于 Theano的 PyCUDA 函数,可查阅: theano.misc.gnumpy_utils.py.


    参考资料:

    [1] 官网:http://deeplearning.net/software/theano/tutorial/gpu_data_convert.html

  • 相关阅读:
    CSS:Tutorial three
    CSS:Tutorial two
    CSS:Tutorial one
    JavaScript: 代码简洁之道
    python数据结构:pandas(1)
    python数据结构:numpy
    python函数之format()
    算法(1)
    python基础面试题:(1)
    python学习笔记(13):python并发编程以及系统常用模块
  • 原文地址:https://www.cnblogs.com/shouhuxianjian/p/4590223.html
Copyright © 2020-2023  润新知