當前位置: 首頁>>代碼示例>>Python>>正文


Python cupy.fromDlpack方法代碼示例

本文整理匯總了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) 
開發者ID:cupy,項目名稱:cupy,代碼行數:7,代碼來源:test_dlpack.py

示例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() 
開發者ID:moskomule,項目名稱:homura,代碼行數:12,代碼來源:backends.py

示例3: to_cupy

# 需要導入模塊: import cupy [as 別名]
# 或者: from cupy import fromDlpack [as 別名]
def to_cupy(m_tensor):
    return cupy.fromDlpack(to_dlpack(m_tensor)) 
開發者ID:cybertronai,項目名稱:pytorch-sso,代碼行數:4,代碼來源:cupy.py

示例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 
開發者ID:mikgroup,項目名稱:sigpy,代碼行數:43,代碼來源:pytorch.py

示例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 ) ) ) 
開發者ID:Santosh-Gupta,項目名稱:SpeedTorch,代碼行數:10,代碼來源:CUPYLive.py

示例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 ) ) 
開發者ID:Santosh-Gupta,項目名稱:SpeedTorch,代碼行數:7,代碼來源:CUPYLive.py

示例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 ) ) ) 
開發者ID:Santosh-Gupta,項目名稱:SpeedTorch,代碼行數:7,代碼來源:CPUCupyPinned.py


注:本文中的cupy.fromDlpack方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。