本文整理汇总了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 ) ) )