Open In App

Python CuPy

Improve
Improve
Like Article
Like
Save
Share
Report

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

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

  • pip
  • python 3
  • Anaconda(optional)
  • 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

Installation 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

  1. Open the Anaconda prompt and enter
conda install -c anaconda cupy
  1. Or
  2. Use Anaconda navigator(GUI) to directly install cupy library.

Basics of cupy.ndarray

  1. 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
  1. Just like Numpy, CuPy also have a ndarray class cupy.ndarray which is compatible GPU alternative of numpy.ndarray.
x_gpu = cp.array([1, 2, 3])
  1. x_gpu in 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). 

Python3




import cupy as cp
import numpy as np
 
x_cpu = np.array([1, 2, 3])
x_gpu = cp.array([1, 2, 3])
 
l2_cpu = np.linalg.norm(x_cpu)
l2_gpu = cp.linalg.norm(x_gpu)
 
print("Using Numpy: ", l2_cpu)
print("\nUsing Cupy: ", l2_gpu)


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. 

Python3




# Python program to
# demonstrate speed comparison
# between cupy and numpy
 
# Importing modules
import cupy as cp
import numpy as np
import time
 
# NumPy and CPU Runtime
s = time.time()
x_cpu = np.ones((1000, 1000, 100))
e = time.time()
print("Time consumed by numpy: ", e - s)
 
# CuPy and GPU Runtime
s = time.time()
x_gpu = cp.ones((1000, 1000, 100))
e = time.time()
print("\nTime consumed by cupy: ", e - s)


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/



Last Updated : 10 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads