Unlock the Lightning Speed Of RAPIDS cuDF And cuML

Soner Can KALKAN
7 min readFeb 17, 2024

--

Pandas, a powerhouse in Python data analysis, faces challenges with speed and memory on hefty datasets. Enter RAPIDS cuDF, a GPU-accelerated dataframe library offering a lightning-fast alternative to Pandas. Leveraging NVIDIA GPUs and Apache Arrow, RAPIDS cuDF executes data operations up to 150 times faster, without requiring any code changes. Discover how to seamlessly integrate cuDF for superior performance in data analysis tasks, setting the stage for end-to-end GPU-driven data science workflows.

Photo by Julian Hochgesang on Unsplash

Introduction

Pandas is one of the most popular and powerful Python libraries for data analysis and manipulation. However, pandas can also be slow and memory-intensive when working with large datasets or complex operations. What if you could use the same pandas API, but with a massive speed boost and lower memory footprint?

That’s where RAPIDS cuDF comes in. RAPIDS cuDF is a GPU-accelerated dataframe library that offers a pandas-like interface for loading, filtering, and transforming data. RAPIDS cuDF leverages the power of NVIDIA GPUs and Apache Arrow to perform data operations up to 150 times faster than pandas, with zero code changes required.

In this blog post, we will show you how to install and use RAPIDS cuDF, and compare its performance with pandas on some common data analysis tasks. We will also demonstrate how RAPIDS cuDF can integrate seamlessly with other RAPIDS libraries and tools, such as cuML, cuGraph, and Dask, to enable end-to-end GPU data science workflows.

What is RAPIDS ?

RAPIDS is an open-source suite of software libraries and frameworks developed by NVIDIA to accelerate and streamline data science and analytics workflows. At its core, RAPIDS harnesses the computational power of GPUs (Graphics Processing Units) to significantly enhance the performance of various data-related tasks. One of its key components is cuDF, a GPU-accelerated DataFrame library that mirrors the functionality of Pandas but operates at much higher speeds. This allows for rapid data loading, filtering, and transformation with reduced memory usage.

In addition to cuDF, RAPIDS includes cuML, a GPU-accelerated machine learning library, and cuGraph, which accelerates graph analytics tasks. These components collectively enable data scientists and analysts to perform intricate computations on large datasets at speeds up to 150 times faster than traditional CPU-based approaches. Beyond its impressive performance gains, RAPIDS emphasizes ease of integration, making it possible for users to seamlessly incorporate GPU acceleration into their existing data science workflows. Overall, RAPIDS represents a powerful and efficient solution for enhancing the speed and efficiency of end-to-end GPU-driven data science processes.

Lets Try It

Now that we’ve introduced RAPIDS and its powerful capabilities, let’s dive into getting started with RAPIDS cuDF. In this section, we’ll walk through the installation process and provide examples of how to use cuDF for data manipulation tasks.

conda install -c rapidsai -c nvidia -c conda-forge rapids-cudf

Alternatively, you can install it via Docker or from source. Detailed installation instructions can be found on the official RAPIDS documentation page.

Once you have RAPIDS cuDF installed, you can start using it to accelerate your data analysis workflows. The API is designed to be familiar to pandas users, so if you’re already comfortable with pandas, transitioning to cuDF should be relatively seamless.

import cudf

# Load data
df = cudf.read_csv('data.csv')

# Perform some operations
df_filtered = df[df['column'] > 10]
df_grouped = df.groupby('category').agg({'value': 'mean'})

# Display results
print(df_filtered.head())
print(df_grouped.head())

Performance Comparison cuDF

To evaluate the efficiency and speed of data manipulation libraries, particularly in handling large datasets, we embark on a comprehensive performance comparison between two prominent contenders: pandas and RAPIDS cuDF. The objective of this experiment is to quantify the performance benefits offered by RAPIDS cuDF, a GPU-accelerated dataframe library, over pandas, the de facto standard for data analysis in Python.

In this experiment, we’ll create a large synthetic dataset and perform common data manipulation tasks, such as filtering, aggregation, and join operations, using both pandas and RAPIDS cuDF. By meticulously measuring the execution time for each task in both libraries, we’ll gain valuable insights into the performance advantages of cuDF over pandas in real-world scenarios.

import cudf
import time

# Generate a large DataFrame
df_large = pd.DataFrame({'A': range(1000000), 'B': range(1000000)})

# Time pandas operations
start_time = time.time()

# Perform pandas operations: Filtering
df_filtered_cudf = df_large[df_large['A'] > 500000]

# Perform padnas operations: Aggregation
df_grouped_cudf = df_large.groupby('A').agg({'B': 'mean'})

# Perform pdans operations: Join
df_joined_cudf = df_large.merge(df_grouped_cudf, on='A')

end_time = time.time()
pandas_time = end_time - start_time


df_large_cudf = cudf.DataFrame({'A': range(1000000), 'B': range(1000000)})
# Time cuDF operations
start_time = time.time()

# Perform cuDF operations: Filtering
df_filtered_cudf = df_large_cudf[df_large['A'] > 500000]

# Perform cuDF operations: Aggregation
df_grouped_cudf = df_large_cudf.groupby('A').agg({'B': 'mean'})

# Perform cuDF operations: Join
df_joined_cudf = df_large_cudf.merge(df_grouped_cudf, on='A')


end_time = time.time()
cudf_time = end_time - start_time

print(f"Pandas execution time: {pandas_time} seconds")
print(f"cuDF execution time: {cudf_time} seconds")
Pandas execution time: 1.0617659091949463 seconds
cuDF execution time: 0.23813939094543457 seconds

In this code snippet, we’re using cudf to perform the same operations as before: filtering, aggregation, and join. Let's break down each operation:

  1. Filtering: We filter rows where the value in column ‘A’ is greater than 500,000.
  2. Aggregation: We group the DataFrame by column ‘A’ and calculate the mean of values in column ‘B’ for each group.
  3. Join: We merge the original DataFrame with the aggregated DataFrame based on the common column ‘A’.

By timing the execution of these operations using cudf, we can directly compare the performance with the equivalent operations using pandas. This will provide a clear understanding of the speedup achieved by RAPIDS cuDF when working with large datasets.

Need to node that code has been run on the colab T4 Nvidia GPU.

Through this rigorous performance comparison, aim to provide data scientists and analysts with valuable insights into the capabilities of RAPIDS cuDF. By understanding the speed and efficiency gains offered by GPU-accelerated data manipulation, users can make informed decisions when selecting the appropriate tools for their data science workflows.

What about cuML ?

In the realm of machine learning, the ability to train models efficiently on large datasets is paramount. As datasets grow in size and complexity, traditional CPU-based machine learning libraries may struggle to deliver timely results. This necessitates exploring alternative solutions that can leverage the computational power of GPUs to accelerate model training and inference.

Machine learning tasks, such as regression, classification, and clustering, often involve computationally intensive operations that can benefit from parallel processing. GPUs, with their massively parallel architecture, offer a compelling solution for accelerating these tasks, enabling faster model training and inference.

In this experiment, we’ll compare the performance of cuML with a traditional CPU-based machine learning library (e.g., scikit-learn) on a standard machine learning task using a sample dataset. By measuring the time taken to train a machine learning model on both platforms, we’ll quantify the performance benefits offered by cuML and demonstrate its potential to accelerate model training on large datasets.

import numpy as np
import cudf
import cudf as gd
from cuml.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestClassifier as SKRandomForestClassifier
from sklearn.datasets import make_classification
import time

# Generate a synthetic dataset
X, y = make_classification(n_samples=10**6, n_features=20, random_state=42)

# Convert data to cuDF DataFrame
X_cudf = cudf.DataFrame(X)
y_cudf = cudf.Series(y)

# Timing cuML model training
start_time_cuml = time.time()

# Train a Random Forest classifier using cuML
rf_classifier_cuml = RandomForestClassifier()
rf_classifier_cuml.fit(X_cudf, y_cudf)

end_time_cuml = time.time()
cuml_time = end_time_cuml - start_time_cuml
print(f"cuML training time: {cuml_time} seconds")

# Timing scikit-learn model training
start_time_sklearn = time.time()

# Train a Random Forest classifier using scikit-learn
rf_classifier_sklearn = SKRandomForestClassifier()
rf_classifier_sklearn.fit(X, y)

end_time_sklearn = time.time()
sklearn_time = end_time_sklearn - start_time_sklearn
print(f"scikit-learn training time: {sklearn_time} seconds")
cuML training time: 1.0633399486541748 seconds
scikit-learn training time: 57.14509344100952 seconds

The performance comparison between cuML, a GPU-accelerated machine learning library, and scikit-learn, a traditional CPU-based machine learning library, reveals compelling insights into the speed and efficiency gains offered by GPU acceleration.

cuML Training Time: “1.06” seconds

Using cuML’s GPU-accelerated implementation of the Random Forest classifier, the training process completed in 1.06 seconds. Leveraging the parallel processing capabilities of GPUs, cuML demonstrates impressive speed and efficiency in training machine learning models on large datasets.

scikit-learn Training Time: “57.14 seconds

In contrast, training the same Random Forest classifier using scikit-learn, a CPU-based library, took 57.14 seconds. While scikit-learn remains a reliable choice for machine learning tasks, its performance on large datasets is notably slower compared to cuML, especially when dealing with computationally intensive operations.

Performance Gain with cuML: ~57x

By comparing the training times of cuML and scikit-learn, we observe a significant performance gain of approximately 57x with cuML. This remarkable speedup underscores the transformative impact of GPU acceleration on machine learning workflows, enabling data scientists and analysts to train models faster and more efficiently than ever before.

Conclusion:

In wrapping up our exploration of RAPIDS and cuDF, it’s evident that this GPU-accelerated duo represents a transformative leap forward for data science. The seamless integration of cuDF into existing Pandas-based workflows, as demonstrated through the installation process, makes GPU acceleration accessible to anyone seeking to boost their data analysis tasks.

Beyond the remarkable speed enhancements showcased in our performance comparison, RAPIDS introduces a paradigm shift in how we approach data analysis. With its ability to handle memory-intensive tasks and complex operations with ease, RAPIDS empowers data scientists to tackle large-scale datasets and extract insights more efficiently than ever before.

Furthermore, the seamless integration of cuDF with other RAPIDS libraries, such as cuML, cuGraph, and Dask, extends the capabilities of GPU-driven data science workflows. This integration fosters an ecosystem where tasks ranging from data manipulation and analysis to machine learning and graph analytics can be seamlessly executed on GPU-accelerated platforms, enabling end-to-end GPU-driven data science pipelines.

Worth To Look

--

--

Soner Can KALKAN
Soner Can KALKAN

Written by Soner Can KALKAN

📊 Data Scientist | 🌍 Exploring the Data Universe, One Insight at a Time

No responses yet