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

Enhancing Warehouse Accuracy with Computer Vision

In today’s fast-paced world of e-commerce and supply chain logistics, warehouses are more than just… Read More

5 days ago

How AI is Redefining Quality Control and Supercharging OEE Optimization?

What does it mean to redefine the future of manufacturing with AI? At the heart… Read More

3 weeks ago

How is AI Transforming Cold Chain Logistics in Healthcare?

In 2022, Americans spent USD 4.5 trillion on healthcare or USD 13,493 per person, a… Read More

4 weeks ago

How Can CEOs Slash GenAI Costs Without Sacrificing Innovation?

In the rush to adopt generative AI, companies are encountering an unforeseen obstacle: skyrocketing computing… Read More

1 month ago

Top 7 Benefits of Using AI for Quality Control in Manufacturing

AI in Manufacturing: Drastically Boosting Quality Control Imagine the factory floors are active with precision… Read More

1 month ago

10 Key Steps to Build a Smart Factory

Did you know the smart factory market is expected to grow significantly over the next… Read More

1 month ago

This website uses cookies.