Python cupy.fromDlpack() Examples

The following are 9 code examples of cupy.fromDlpack(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cupy , or try the search function .
Example #1
Source File: test_dlpack.py    From cupy with MIT License 5 votes vote down vote up
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) 
Example #2
Source File: backends.py    From homura with Apache License 2.0 5 votes vote down vote up
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() 
Example #3
Source File: cupy.py    From pytorch-sso with MIT License 5 votes vote down vote up
def to_cupy(m_tensor):
    return cupy.fromDlpack(to_dlpack(m_tensor)) 
Example #4
Source File: pytorch.py    From sigpy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #5
Source File: CUPYLive.py    From SpeedTorch with MIT License 5 votes vote down vote up
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 ) ) ) 
Example #6
Source File: CUPYLive.py    From SpeedTorch with MIT License 5 votes vote down vote up
def afterOptimizerStep(self, retrievedPosIndexes , retrievedNegIndexes = None):
        torch.cuda.synchronize()
        cupy.cuda.Device().synchronize()
        
        reshapedRetrieval = self._getReshapedRetrieval( retrievedPosIndexes, retrievedNegIndexes )

        for idx, optVar in enumerate(self.optVarList):
            self.CUPYcorpi[idx][ reshapedRetrieval ] = (
                cupy.fromDlpack( to_dlpack( self.given_optimizer.state_dict()['state'][ self.optimizerKey ][optVar] ) )  ) 
Example #7
Source File: CUPYLive.py    From SpeedTorch with MIT License 5 votes vote down vote up
def insertData(self, dataObject, indexes):
        torch.cuda.synchronize()
        cupy.cuda.Device().synchronize()
        
        self.CUPYcorpus[indexes] =  cupy.fromDlpack( to_dlpack( dataObject ) ) 
Example #8
Source File: CPUCupyPinned.py    From SpeedTorch with MIT License 5 votes vote down vote up
def afterOptimizerStep(self,retrievedPosIndexes , retrievedNegIndexes = None):
        reshapedRetrieval = self._getReshapedRetrieval( retrievedPosIndexes, retrievedNegIndexes )

        self.CUPYmemmap[ reshapedRetrieval ] = (
            cupy.fromDlpack( to_dlpack( self.model_variable.weight.data ) ) ) 
Example #9
Source File: CPUCupyPinned.py    From SpeedTorch with MIT License 5 votes vote down vote up
def afterOptimizerStep(self, retrievedPosIndexes , retrievedNegIndexes = None):
        reshapedRetrieval = self._getReshapedRetrieval( retrievedPosIndexes, retrievedNegIndexes )

        for idx, optVar in enumerate(self.optVarList):
            self.CUPYmemmap[idx][ reshapedRetrieval ] = (
                cupy.fromDlpack( to_dlpack( self.given_optimizer.state_dict()['state'][ self.optimizerKey ][optVar] ) )  )