NumPy
NumPy is the fundamental package for numerical computing in Python. While NumPy arrays reside on the
CPU, you can transfer them to GPU using any of the GPU-accelerated frameworks mentioned above. The
numpy_interop.py example demonstrates four different methods.
Required Imports:
import numpy as np
import cvcuda
Method 1: Via CUDA Python
numpy_array = np.random.randn(10, 10).astype(np.float32)
cuda_buffer = CudaBuffer(numpy_array.shape, numpy_array.dtype)
cuda_memcpy_h2d(numpy_array, cuda_buffer.ptr)
cvcuda_tensor = cvcuda.as_tensor(cuda_buffer)
This method gives you the most control over memory allocation and transfer.
Method 2: Via PyTorch
numpy_array = np.random.randn(10, 10).astype(np.float32)
torch_tensor = torch.from_numpy(numpy_array).cuda()
cvcuda_tensor = cvcuda.as_tensor(torch_tensor)
PyTorch provides a convenient torch.from_numpy() method that creates a tensor sharing memory with
the NumPy array (on CPU), then .cuda() transfers it to GPU.
Method 3: Via CuPy
numpy_array = np.random.randn(10, 10).astype(np.float32)
cupy_array = cp.asarray(numpy_array)
cvcuda_tensor = cvcuda.as_tensor(cupy_array)
CuPy’s cp.asarray() directly transfers NumPy arrays to GPU with NumPy-compatible semantics.
Method 4: Via PyCUDA
numpy_array = np.random.randn(10, 10).astype(np.float32)
pycuda_array = gpuarray.to_gpu(numpy_array)
cvcuda_tensor = cvcuda.as_tensor(pycuda_array)
PyCUDA provides gpuarray.to_gpu() for straightforward CPU-to-GPU transfer.
Choosing a Method:
PyTorch - Easy to integrate with existing PyTorch workflows, but has a large download size
CuPy - NumPy-like syntax for GPU operations, but requires building during installation
PyCUDA - Good if you are already using PyCUDA in your pipelines, but requires building during installation
CUDA Python - Best for maximum control and custom CUDA integration, but requires low-level management and CUDA knowledge
Complete Example: See samples/interoperability/numpy_interop.py