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

Top Generative AI Use Cases in Healthcare

The emergence of Generative AI (GenAI) is reshaping healthcare use cases and facilitating the rapid… Read More

3 days ago

Generative AI in Pharma Regulation: Insights from FDA, EMA, and Health Canada

The U.S. Food and Drug Administration's (FDA) stance on GenAI is clear: it's a groundbreaking… Read More

1 week ago

AInonymize – AI for Secure Health Data and Innovation

Executive Summary In healthcare, protecting patient information is not just a legal requirement; it's a… Read More

2 weeks ago

How Demand Forecasting Turns Supply Chains into Mind Readers?

Demand forecasting in the supply chain is crucial for optimizing inventory levels and ensuring efficient… Read More

3 weeks ago

LLM Numerology: We Experimented with 3 LLMs to Find Out Their Favorite Numbers

Hi, I am ChatGPT 3.5 Turbo. Do you know what my favorite number is? Do… Read More

1 month ago

Data-Driven Sustainability: Achieve Business Value from ESG Data

After a successful webinar on digital transformation and sustainability, we organized a sequel titled “Data-Driven… Read More

1 month ago

This website uses cookies.