当前位置: 首页>>代码示例>>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;未经允许,请勿转载。