我正在使用PyTorch,并希望在PyCUDA的帮助下对Tensor数据进行一些算术运算。我可以通过t.data_ptr()
获取cuda tensor t
的内存地址。我能否利用这个地址和我的大小和数据类型的知识来初始化一个GPUArray
?我希望避免复制数据,但这也是一种选择。
我正在使用PyTorch,并希望在PyCUDA的帮助下对Tensor数据进行一些算术运算。我可以通过t.data_ptr()
获取cuda tensor t
的内存地址。我能否利用这个地址和我的大小和数据类型的知识来初始化一个GPUArray
?我希望避免复制数据,但这也是一种选择。
class Holder(PointerHolderBase):
def __init__(self, tensor):
super().__init__()
self.tensor = tensor
self.gpudata = tensor.data_ptr()
def get_pointer(self):
return self.tensor.data_ptr()
def __int__(self):
return self.__index__()
# without an __index__ method, arithmetic calls to the GPUArray backed by this pointer fail
# not sure why, this needs to return some integer, apparently
def __index__(self):
return self.gpudata
GPUArray
。该代码使用的是Reikna数组,它是一个子类,但也应该适用于pycuda
数组。def tensor_to_gpuarray(tensor, context=pycuda.autoinit.context):
'''Convert a :class:`torch.Tensor` to a :class:`pycuda.gpuarray.GPUArray`. The underlying
storage will be shared, so that modifications to the array will reflect in the tensor object.
Parameters
----------
tensor : torch.Tensor
Returns
-------
pycuda.gpuarray.GPUArray
Raises
------
ValueError
If the ``tensor`` does not live on the gpu
'''
if not tensor.is_cuda:
raise ValueError('Cannot convert CPU tensor to GPUArray (call `cuda()` on it)')
else:
thread = cuda.cuda_api().Thread(context)
return reikna.cluda.cuda.Array(thread, tensor.shape, dtype=torch_dtype_to_numpy(tensor.dtype), base_data=Holder(tensor))
def gpuarray_to_tensor(gpuarray, context=pycuda.autoinit.context):
'''Convert a :class:`pycuda.gpuarray.GPUArray` to a :class:`torch.Tensor`. The underlying
storage will NOT be shared, since a new copy must be allocated.
Parameters
----------
gpuarray : pycuda.gpuarray.GPUArray
Returns
-------
torch.Tensor
'''
shape = gpuarray.shape
dtype = gpuarray.dtype
out_dtype = numpy_dtype_to_torch(dtype)
out = torch.zeros(shape, dtype=out_dtype).cuda()
gpuarray_copy = tensor_to_gpuarray(out, context=context)
byte_size = gpuarray.itemsize * gpuarray.size
pycuda.driver.memcpy_dtod(gpuarray_copy.gpudata, gpuarray.gpudata, byte_size)
return out
from pycuda.gpuarray import GPUArray
def torch_dtype_to_numpy(dtype):
dtype_name = str(dtype)[6:] # remove 'torch.'
return getattr(np, dtype_name)
def tensor_to_gpuarray(tensor):
if not tensor.is_cuda:
raise ValueError('Cannot convert CPU tensor to GPUArray (call `cuda()` on it)')
else:
array = GPUArray(tensor.shape, dtype=torch_dtype_to_numpy(tensor.dtype),
gpudata=tensor.data_ptr())
return array.copy()
现在如何从原始数据返回到张量是另一回事。