Analytics

CuPy – An accelerator of NumPy

Reading Time: 2 mins

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 Logo

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.

Malay Nilabh

Leave a Comment
Share
Published by
Malay Nilabh

Recent Posts

How to Future-Proof Warehouse Operations with Smart Inventory Management?

Effective inventory management is more crucial than ever in today's fast-paced business environment. It directly… Read More

2 weeks ago

Gramener Bags a Spot in AIM’s Top Data Science Service Providers 2024 Penetration-Maturity (PeMa) Quadrant

Gramener - A Straive Company has secured a spot in Analytics India Magazine’s (AIM) Challengers… Read More

3 months ago

Gramener Wins Nasscom AI Gamechangers 2024 Award for Responsible AI

Recently, we won the Nasscom AI Gamechangers Award for Responsible AI, especially for our Fish… Read More

3 months ago

Master Supply Chain Resilience: 5 Powerful Lessons from Our Location Intelligence Webinar

Supply chain disruptions can arise from various sources, such as extreme weather events, geopolitical tensions,… Read More

3 months ago

Gramener’s Doc Genie Wins 2024 AI Breakthrough Award for Best Intelligent Word Recognition Solution

In a remarkable achievement for the Artificial Intelligence (AI) sector, Gramener's flagship GenAI-powered Intelligent Document… Read More

4 months ago

Top 10 Industry 4.0 Companies to Watch in 2024

Did you know that the global Industry 4.0 market size is projected to reach USD… Read More

4 months ago

This website uses cookies.