pip install cupy
np.
全部替换为cp.
(import cupy as cp
)import cupy as cp
import numpy as np
#cupy > numpy
numpy_data = cp.asnumpy(cupy_data)
#numpy > cupy
cupy_data = cp.asarray(numpy_data)
from cupy.core.dlpack import toDlpack
from cupy.core.dlpack import fromDlpack
from torch.utils.dlpack import to_dlpack
from torch.utils.dlpack import from_dlpack
import torch
#tensor > cupy
cupy_data = fromDlpack(to_dlpack(tensor_data))
#cupy > tensor
tensor_data = from_dlpack(toDlpack(cupy_data))
import numpy as np
import torch
#tensor > numpy
numpy_data = tensor_data.numpy()
#numpy > tensor
tensor_data = torch.from_numpy(numpy_data)
# tensor <-> cupy
def tensor2cupy(tensor_data):
return fromDlpack(to_dlpack(tensor_data))
def cupy2tensor(cupy_data):
return from_dlpack(toDlpack(cupy_data))
# cupy <-> numpy
def cupy2numpy(cupy_data):
return cp.asnumpy(cupy_data)
def numpy2cupy(numpy_data):
return cp.asarray(numpy_data)
# numpy <-> tensor
def numpy2tensor(numpy_data):
return torch.from_numpy(numpy_data)
def tensor2numpy(tensor_data):
return tensor_data.numpy()
## 太麻烦了,全部换成torch写!
即ECO的做法!