This is a blog on optimizing the speed of Python. You can speedup your Python and NumPy codes using CuPy, which is an open-source matrix library accelerated with NVIDIA CUDA.
Many consider that NumPy is the most powerful package in Python. NumPy makes it easy to process vast amounts of data in a matrix format in an efficient way. It also plays a vital role in Artificial Intelligence and Deep Learning model development.
However, if we talk about the processing speed of NumPy, it is already a significant setup from Python. It is always a good idea to move the data processing into NumPy rather than using multiple programming conditional and looping statements. They tend to slow down the processing speed significantly.
Still, even with that speedup, NumPy is only running on the CPU. With consumer CPUs typically having 8 cores or less, the amount of parallel processing is limited. Therefore, the amount of speed optimization is also limited.
That’s where the question arises, how to achieve that optimum speed? And that’s where our new Python invention CuPy comes in!
What is CuPy?
You can easily speedup NumPy codes using CuPy. CuPy is a library that implements NumPy arrays on NVidia GPUs by leveraging the CUDA GPU library. With that implementation, you can achieve superior parallel speedup because of multiple CUDA cores GPU has.
CuPy has the same features as NumPy. Luckily, in most of the cases, you can replace NumPy with CuPy without doing any changes in the code. As a direct replacement step, replace the NumPy code with compatible CuPy code and boom your NumPy code with GPU speed.
Similar to NumPy, CuPy will also support most of the array operations like broadcasting, indexing, arithmetic operations, and transformations.
CuPy also provides a platform for writing custom Python code which leverages CUDA and GPU speedups, in case any operations are not available directly. Custom functions can be in either C++ or Python. CuPy will automatically do the GPU conversion.
You can install CuPy like any other packages in Python using pip function.
pip Install CuPy
Running on GPU with CuPy
Below is the system specification for using CuPy:
- i7–8700k CPU
- 1080 Ti GPU
- 32GB of DDR4 3000MHz RAM
- CUDA 9.0
Post-installation, you can import cuPy using the import function in Python. For the rest of the coding, switching from NumPy to cuPy is as easy as replacing NumPy extension (np) with CuPy extension (cp).
Finally, you can use the code snippet below to track the difference in processing time. With this, you can create an array using NumPy and the processing time to create an array using CuPy.
s = time.time()
x_cpu = np.ones((1000,1000,1000))
e = time.time()
print(e - s)### CuPy and GPU
s = time.time()
x_gpu = cp.ones((1000,1000,1000))
e = time.time()
print(e - s)
I hope this information was helpful.
Note: This blog is written by Malay Nilabh, Associate Lead Data Scientist at Gramener. Malay is a sports geek and loves to analyze sports datasets for interesting and surprising insights.