本文整理匯總了Python中cupy.fromDlpack方法的典型用法代碼示例。如果您正苦於以下問題:Python cupy.fromDlpack方法的具體用法?Python cupy.fromDlpack怎麽用?Python cupy.fromDlpack使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cupy
的用法示例。
在下文中一共展示了cupy.fromDlpack方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_conversion
# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def test_conversion(self):
tensor = self.array.toDlpack()
array = cupy.fromDlpack(tensor)
testing.assert_array_equal(self.array, array)
testing.assert_array_equal(self.array.data.ptr, array.data.ptr)
示例2: torch_to_xp
# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def torch_to_xp(input: torch.Tensor
) -> np.ndarray:
# torch Tensor to numpy/cupy ndarray
if not torch.is_tensor(input):
raise RuntimeError(f'torch_to_numpy expects torch.Tensor as input, but got {type(input)}')
if IS_CUPY_AVAILABLE and input.is_cuda:
return cupy.fromDlpack(to_dlpack(input))
else:
return input.numpy()
示例3: to_cupy
# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def to_cupy(m_tensor):
return cupy.fromDlpack(to_dlpack(m_tensor))
示例4: from_pytorch
# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def from_pytorch(tensor, iscomplex=False): # pragma: no cover
"""Zero-copy conversion from pytorch tensor to numpy/cupy array.
If iscomplex, then tensor must have the last dimension as 2,
and the output will be viewed as a complex valued array.
Args:
tensor (PyTorch tensor): input.
iscomplex (bool): whether input represents complex valued tensor.
Returns:
Numpy/cupy array.
"""
from torch.utils.dlpack import to_dlpack
device = tensor.device
if device.type == 'cpu':
output = tensor.detach().contiguous().numpy()
else:
if config.cupy_enabled:
import cupy as cp
output = cp.fromDlpack(to_dlpack(tensor.contiguous()))
else:
raise TypeError('CuPy not installed, '
'but trying to convert GPU PyTorch Tensor.')
if iscomplex:
if output.shape[-1] != 2:
raise ValueError('shape[-1] must be 2 when iscomplex is '
'specified, but got {}'.format(output.shape))
with backend.get_device(output):
if output.dtype == np.float32:
output = output.view(np.complex64)
elif output.dtype == np.float64:
output = output.view(np.complex128)
output = output.reshape(output.shape[:-1])
return output
示例5: afterOptimizerStep
# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def afterOptimizerStep(self,retrievedPosIndexes , retrievedNegIndexes = None):
torch.cuda.synchronize()
cupy.cuda.Device().synchronize()
reshapedRetrieval = self._getReshapedRetrieval( retrievedPosIndexes, retrievedNegIndexes )
self.CUPYcorpus[ reshapedRetrieval ] = (
cupy.fromDlpack( to_dlpack( self.model_variable.weight.data ) ) )
示例6: insertData
# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def insertData(self, dataObject, indexes):
torch.cuda.synchronize()
cupy.cuda.Device().synchronize()
self.CUPYcorpus[indexes] = cupy.fromDlpack( to_dlpack( dataObject ) )
示例7: afterOptimizerStep
# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def afterOptimizerStep(self,retrievedPosIndexes , retrievedNegIndexes = None):
reshapedRetrieval = self._getReshapedRetrieval( retrievedPosIndexes, retrievedNegIndexes )
self.CUPYmemmap[ reshapedRetrieval ] = (
cupy.fromDlpack( to_dlpack( self.model_variable.weight.data ) ) )