Testing CuPy and GPU: A Practical Guide
In this article
To test whether CuPy is using your GPU, run a small matrix multiplication and check that cupy.cuda.runtime.getDeviceCount() returns at least 1 and that the operation completes without falling back to CPU. The simplest reliable test is import cupy; print(cupy.cuda.runtime.getDeviceCount()) – if it prints 1 or more, CuPy sees your GPU; then run a = cupy.zeros((1000,1000)); a = a @ a and verify it executes quickly. This article explains the background, how CuPy works, how to set up a proper test, common pitfalls, and how to compare GPU vs CPU performance.
Background: What CuPy Does
CuPy is a NumPy/SciPy-compatible library that uses NVIDIA CUDA to run array operations on a GPU. It mirrors NumPy’s API, so code written with NumPy can often run with CuPy by simply replacing import numpy as np with import cupy as cp. The key difference is that CuPy arrays live in GPU memory, and operations on them are executed as CUDA kernels. This can dramatically speed up large-scale numerical computations, especially matrix multiplications, convolutions, and element-wise operations on large arrays.
How CuPy Uses the GPU
When you create a CuPy array, memory is allocated on the GPU via CUDA. Operations like cp.dot(a, b) are compiled into CUDA kernels at runtime (using a just-in-time compiler) and executed on the GPU. The result stays in GPU memory until you explicitly move it back to CPU with cupy.asnumpy(). CuPy also supports multiple GPUs, and you can select a device using cupy.cuda.Device(id). Under the hood, CuPy uses the CUDA runtime API and cuBLAS for linear algebra, so it is tightly integrated with NVIDIA’s drivers and toolkit.
Prerequisites for Testing CuPy
Before you test, ensure you have:
- An NVIDIA GPU with a compute capability of 3.0 or higher (most GPUs from 2012 onward).
- NVIDIA drivers installed that support the CUDA version you plan to use.
- CUDA Toolkit installed (or at least the runtime libraries) – CuPy can use a bundled version if you install the right wheel.
- CuPy installed via
pip install cupy-cuda12x(replace with your CUDA version, e.g.,cupy-cuda11x).
If you are unsure about your CUDA version, run nvcc --version in a terminal. If you do not have the toolkit, you can install a CuPy wheel that bundles the CUDA runtime, such as cupy-cuda12x for CUDA 12.x.
Step-by-Step: Testing CuPy and GPU
Here is a minimal but thorough test procedure:
- Check CuPy installation: Run
python -c "import cupy; print(cupy.__version__)"– this should print a version number without errors. - Check GPU visibility: Run
python -c "import cupy; print(cupy.cuda.runtime.getDeviceCount())". If it prints 0, CuPy cannot see your GPU – check drivers and CUDA version. - Run a simple operation: Execute
python -c "import cupy as cp; a = cp.ones((1000,1000)); b = cp.dot(a,a); print(b.sum())". This should complete in a few seconds and print a number. - Verify GPU usage: While the script runs, use
nvidia-smiin another terminal to see the Python process using GPU memory. You should see a process with a memory usage value. - Compare with CPU: Time the same operation with NumPy and CuPy to see the speed difference. Use
timein a script or%timeitin a Jupyter notebook.
If all steps pass, CuPy is working correctly on your GPU.
Common Problems and Solutions
Several issues can arise when testing CuPy:
- CuPy does not see the GPU (getDeviceCount returns 0): This usually means the CUDA driver is not installed or is incompatible. Reinstall the NVIDIA driver and ensure the CUDA version matches the CuPy wheel.
- Out-of-memory errors: Large arrays can exceed GPU memory. Check available memory with
nvidia-smiand reduce array sizes or usecupy.cuda.set_allocatorto manage memory. - Slow performance compared to CPU: For small arrays, the overhead of GPU kernel launch can make CuPy slower. Use larger arrays (e.g., 1000x1000 or bigger) to see benefits.
- CUDA driver version mismatch: CuPy wheels are compiled for a specific CUDA version. If you have CUDA 11 and install
cupy-cuda12x, you may get errors. Match the wheel to your CUDA version.
Comparing CuPy to Other GPU Libraries
CuPy is not the only GPU array library. Alternatives include:
- PyTorch and TensorFlow: These deep learning frameworks also provide GPU tensor operations, but they are more heavyweight and designed for neural networks. CuPy is lighter and closer to NumPy.
- Numba: Numba can compile Python functions to CUDA kernels, but it requires writing custom kernels. CuPy provides high-level array operations out of the box.
- JAX: JAX offers GPU acceleration with a NumPy-like API, but it uses XLA compilation and has a different memory model. CuPy is often more straightforward for existing NumPy code.
Choose CuPy when you want a drop-in replacement for NumPy with minimal code changes. Choose PyTorch if you are building neural networks. Choose Numba if you need custom kernels.
Safety and Accuracy Considerations
When testing CuPy, remember:
- Always unplug the PC and ground yourself before opening the case if you are installing a GPU. Static discharge can damage components.
- Never open a power supply – it contains high-voltage capacitors.
- Overclocking or undervolting the GPU can cause instability and may void the warranty. Do not attempt unless you understand the risks, and avoid sharing specific voltage numbers.
- Ensure your power supply can handle the GPU’s power draw. Check the GPU’s power requirements from the manufacturer.
These precautions are essential for any hardware work, not just for CuPy testing.
Frequently Asked Questions
Why does CuPy not see my GPU even though I have an NVIDIA card?
This usually means the NVIDIA driver is missing or incompatible. Install the latest driver from NVIDIA’s website, then install a CuPy wheel that matches your CUDA version. Run nvidia-smi to confirm the driver sees the GPU.
Can I use CuPy with an AMD GPU?
No, CuPy is designed for NVIDIA CUDA. For AMD GPUs, you would need to use ROCm libraries or other frameworks like PyTorch with ROCm support.
How do I know if CuPy is actually using the GPU and not CPU?
Use nvidia-smi while running a CuPy script. If you see the Python process listed with GPU memory usage, CuPy is using the GPU. Also, compare execution time; GPU operations are much faster for large arrays.
Why is CuPy slower than NumPy for small arrays?
GPU operations have launch overhead. For arrays smaller than a few thousand elements, the overhead of copying data to the GPU and launching a kernel can exceed the computation time. Use CuPy for large arrays (e.g., 1000x1000 or larger) to see speedups.
Can I use CuPy in a Jupyter notebook?
Yes, CuPy works in Jupyter notebooks. Just import it and run operations. You can also use %timeit to benchmark GPU vs CPU.