NumPy surely is the most compatible library for any pythonic code to process large amounts of data in the form of
numpy.ndarray(n-dimensional matrix array). However, as soon as we increase the size of data processed to a significant amount the multicore CPU cannot process data as efficiently as required because Numpy is only running on CPU (mostly having 4-8 cores on consumer end) having the limited parallel processing power.
This is where a new nice python library comes in
CuPy. CuPy is a NumPy compatible library for GPU.
CuPy is an open-source matrix library accelerated with NVIDIA CUDA. It also uses CUDA-related libraries including cuBLAS, cuDNN, cuRand, cuSolver, cuSPARSE, cuFFT, and NCCL to make full use of the GPU architecture.
It is an implementation of a NumPy-compatible multi-dimensional array on CUDA. CuPy consists of
cupy.ndarray, the core multi-dimensional array class, and many functions on it. It supports a subset of
numpy.ndarray interface. And it can also accelerate the existing NumPy code through GPU and CUDA libraries.
Hardware and Software Setup
- python 3
- CUDA X.0 (depending upon the hardware)
- CPU: 2x Intel Xeon E5–2698 v4 @ 2.20GHz
- Main memory: 1 TB
- GPU: NVIDIA Tesla V100 32 GB
- OS – Windows/Linux
Download the compatible version of the CUDA setup on your device and install it. To install it open the terminal and enter
pip install cupy-cuda(version)
where version will be the version of CUDA installed on your device. For example-
(For CUDA 10.0)
pip install cupy-cuda100
If your device does not support CUDA then you can install CuPy in Anaconda and use it for CPU based computing. Alternatively, Anaconda works fine with CUDA too.
To install it on Anaconda –
- Open the Anaconda prompt and enter
conda install -c anaconda cupy
- Use Anaconda navigator(GUI) to directly install cupy library.
Basics of cupy.ndarray
- Importing – In the following code, cp is an abbreviation of cupy, as np is numpy as is customarily done.
import numpy as np import cupy as cp
- Just like Numpy, CuPy also have a
cupy.ndarraywhich is compatible GPU alternative of
x_gpu = cp.array([1, 2, 3])
x_gpuin the above example is an instance of
cupy.ndarray. You can see its creation of identical to NumPy’s one, except that numpy is replaced with cupy.
Example – Take the Euclidean norm (a.k.a L2 norm).
Using Numpy: 3.7416573867739413 Using Cupy: array(3.74165739)
Numpy vs Cupy
CuPy is a NumPy compatible library for GPU. It is more efficient as compared to numpy because array operations with NVIDIA GPUs can provide considerable speedups over CPU computing.
Note- The configurations used here are for CPU is intel i7-7700 HQ and GPU is Geforce GTX 1050 4GB using CUDA 9.0.
Time consumed by numpy: 0.4238910675048828 Time consumed by cupy: 0.0010099411010742188
Here, we can see that CuPy could work way faster than NumPy.
CuPy implements many functions on cupy.ndarray objects. See the reference for the supported subset of NumPy API. Understanding NumPy might help utilizing most features of CuPy. So, it is recommend you to read the NumPy documentation.
References – https://cupy.chainer.org/