Accelerate your ML models using RAPIDS AI NVIDIA's open-source RAPIDS AI framework accelerates machine learning workflows by enabling GPU-based data processing, offering cuDF, a pandas-like library that performs operations on large datasets 100x-1000x faster than CPU-based pandas. The framework includes libraries for data analytics, preprocessing, graph algorithms, and ML, allowing users to speed up tasks such as loading, filtering, grouping, and aggregation on datasets with 10 million rows without changing traditional workflows. I’ve been heavily frustrated and agitated when I have to wait in those situations. Despite my impatience, I can’t explain my happiness when it’s trained properly and works well. I always wondered why it would take so much time to train these models. Only when I dive deeper into how the predictive models work, do I understand the reason and feel it was fair enough. However, a part of me still hated the slow processing and training. There are several ways to optimise the models, both in terms of hardware and software. One such optimisation is to use GPUs for the data preprocessing and model training. With the sophistication of tools provided by NVIDIA, we always have the liberty to use GPUs for deep learning models. However, most machine learning models are trained on CPUs, which is the core reason for the slow training and inference. NVIDIA offers a framework to speed up data processing, ML, and visualization using GPUs, without changing the traditional workflow, and it’s amazing. In this blog, we’ll look into the RAPIDS AI framework and how a data scientist or anybody who works with data can utilise it to optimise their workflows. We’ll also compare this with other optimisation techniques and figure out how to choose the right technique for specific situations. The right understanding of the features and capabilities of RAPIDS AI will help you optimise your process in the data science projects. With the growth of AI and data generation, the demand for faster and efficient computations is also increasing at a higher speed. RAPIDS AI is an open-source framework developed by NVIDIA that provides GPU support for data analytics, data preprocessing, graph algorithms, and ML workflows . It helps users build cloud-based machine learning experiments to build models faster, cheaper, and more easily. It has a GPU-optimized core data frame that helps to build databases and ML models, without changing the traditional workflows. It offers a collection of libraries for different tasks in the data engineering process. The most important libraries of RAPIDS AI are as follows: I’ll walk you through a few of these libraries with examples and code snippets to use in your workflows. Before that, let me explain to you the actual need for such libraries. Before we get started with the RAPIDS AI, it is essential to install the relevant libraries to access the functionality. You can run the following commands to install the required libraries and frameworks. conda create -n rapids python=3.10 -yconda activate rapidsconda install -c rapidsai -c nvidia -c conda-forge \ cudf python=3.10 cudatoolkit=12.0 -y Once you are done with the installation process, let's begin to explore the libraries offered by RAPIDS AI. cuDF is a GPU-accelerated DataFrame library that gives massive speed-ups when working with large datasets. Pandas is great. But it struggles when you have tens of millions of rows, complex group-bys and joins, and repeated feature engineering for ML pipelines. cuDF is essentially the GPU equivalent of pandas in Python, designed to perform pandas-like operations on large datasets 100x-1000x faster by running them on NVIDIA GPUs instead of CPUs. The cuDF offers an implementation that is very close to that of pandas. You can often take the existing pandas code and change just one line and get massive speedups. Before we get to know how to use cuDF, let me show you an example of how cuDF works and how it performs in real-time. To showcase this, I have created a dataset with 10 million rows and saved it as a parquet file. I tried to perform operations such as loading, filtering, grouping and aggregation using pandas and cuDF, which showcased massive differences. python import numpy as npimport pandas as pdimport cudfimport time ------------------------------------------------- 1. Generate dataset 10 M rows -------------------------------------------------N = 10 000 000np.random.seed 42 print f"Generating {N:,} rows..." review options = "Great product ", "Fast shipping", "Poor quality", "As expected", "Love it ", "Too expensive", "Would buy again", None reviews = np.random.choice review options, size=N reviews = np.where pd.isna reviews , "", reviews None → empty stringdata = { "user id": np.random.randint 1, 500 000, N , "product id": np.random.randint 1, 50 000, N , "category": np.random.choice "Electronics","Clothing","Home","Books","Toys" , N , "price": np.random.exponential 50, N .round 2 , "quantity": np.random.randint 1, 20, N , "is returned": np.random.choice False, True , N, p= 0.93, 0.07 , "country": np.random.choice "US","UK","DE","JP","CA","AU" , N , "timestamp": pd.date range "2024-01-01", periods=N, freq="1min" , "rating": np.random.choice 1,2,3,4,5 , N, p= 0.05,0.1,0.15,0.3,0.4 , "review text": reviews,}df pd = pd.DataFrame data .sample frac=1, random state=42 .reset index drop=True df pd.to parquet "sales 10M.parquet" print "Dataset saved" ------------------------------------------------- 2. Load with pandas and cuDF -------------------------------------------------df pandas = pd.read parquet "sales 10M.parquet" df cudf = cudf.read parquet "sales 10M.parquet" Convert ALL object columns to cuDF string dtypefor col in df cudf.select dtypes include= 'object' .columns: df cudf col = df cudf col .astype "string" print "dtypes in cuDF:" print df cudf.dtypes ------------------------------------------------- 3. Analysis functions separate for pandas & cuDF -------------------------------------------------def run analysis pandas df, name : print f"\n=== {name} ===" start = time.time result = df .query "price 10 and not is returned" .assign revenue = lambda x: x.price x.quantity .groupby "country", "category" , as index=False .agg revenue = "revenue", "sum" , avg rating = "rating", "mean" , users = "user id", "nunique" , great count = "review text", lambda x: x.str.contains "Great", case=False .sum .sort values "revenue", ascending=False elapsed = time.time - start print f"{name} took {elapsed:.3f} seconds" print result.head 5 return elapseddef run analysis cudf df, name : print f"\n=== {name} ===" start = time.time Filter and calculate revenue filtered = df.query "price 10 and not is returned" .copy filtered "revenue" = filtered "price" filtered "quantity" Pre-compute the "Great" flag before groupby cuDF requires regex=False when using case=False filtered "has great" = filtered "review text" .str.contains "Great", case=False, regex=False Now groupby with only built-in aggregations result = filtered .groupby "country", "category" , as index=False .agg revenue = "revenue", "sum" , avg rating = "rating", "mean" , users = "user id", "nunique" , great count = "has great", "sum" sum of boolean = count of True .sort values "revenue", ascending=False elapsed = time.time - start print f"{name} took {elapsed:.3f} seconds" print result.head 5 return elapsed ------------------------------------------------- 4. Run -------------------------------------------------t pd = run analysis pandas df pandas, "pandas" t cd = run analysis cudf df cudf, "cuDF" print f"\n{'=' 60}" print f"cuDF was {t pd/t cd:.1f}× faster " print f"{'=' 60}" The output showcased that pandas took 8.909 seconds and cuDF took 0.860 seconds , which is 10.4x faster. Generating 10,000,000 rows...Dataset saveddtypes in cuDF:user id int64product id int64category objectprice float64quantity int64is returned boolcountry objecttimestamp datetime64 ns rating int64review text objectdtype: object=== pandas ===pandas took 8.909 seconds country category revenue avg rating users great count28 US Home 1.534648e+08 3.899226 199265 3172816 JP Clothing 1.529607e+08 3.895079 199083 3133124 UK Toys 1.528698e+08 3.898011 199599 3156323 UK Home 1.527598e+08 3.901141 199196 3183419 JP Toys 1.527436e+08 3.899543 198891 31676=== cuDF ===cuDF took 0.860 seconds country category revenue avg rating users great count28 US Home 1.534648e+08 3.899226 199265 3172816 JP Clothing 1.529607e+08 3.895079 199083 3133124 UK Toys 1.528698e+08 3.898011 199599 3156323 UK Home 1.527598e+08 3.901141 199196 3183419 JP Toys 1.527436e+08 3.899543 198891 31676============================================================cuDF was 10.4× faster ============================================================ cuDF stores the data in the GPU memory and is optimised to parse the large files in parallel. python pandas implementation import pandas as pdpdf= pd.read csv 'data.csv' cuDF implementationimport cudfcdf = cudf.read csv 'data.csv' 2. Data Inspection Most of the methods used in cuDF are similar to the ones used in pandas. pandas pdf.head pdf.shapepdf.dtypes pdf.describe cuDF cdf.head cdf.shapecdf.dtypes cdf.describe 3. Data Selection, Indexing, and Operations The data selection, indexing, and other operations can be done using cuDF, similar to the pandas library. Some of the examples are attached below. The cuDF DataFrames can then be converted into a pandas DataFrame, which will bring the data to the CPU. This has to be done with utmost care, considering the size of the dataset. pdf = cdf.to pandas It is advised to avoid transferring large DataFrames repeatedly, as the copying operation is very expensive. 4. Scaling with dask-cudf If one GPU is not enough, use dask-cudf to partition the data across a cluster or a bunch of GPUs. python import dask cudf ddf = dask cudf.read csv 'bigdata- .csv' agg = ddf.groupby 'col' .sum .compute Modin is another option to optimise pandas. Though cuDF and modin are used for a similar purpose, they differ in how they function. The cuML library provides GPU-accelerated versions of scikit-learn algorithms. It keeps the same APIs fit , predict , transform , the same algorithm names, and often the same hyperparameters. The only difference is that the execution happens on thousands of GPU cores. GPUs excel at parallel numeric computations, so cuML takes common ML tasks and runs them on the GPU. python scikit-learnfrom sklearn.cluster import KMeans cuMLfrom cuml.cluster import KMeans All the other functions and operations remain almost the same in both scikit-learn and cuML. The supported algorithms in cuML are listed below. Let me demonstrate an example using Logistic regression. cuGraph is the GPU-accelerated graph analytics library in the RAPIDS ecosystem. Most of the graph algorithms, such as PageRank, BFS, SSSP, and Community Detection, are expensive. They have to traverse millions or billions of edges, and performing them on a CPU takes a long time to compute. cuGraph solves this by using CUDA and storing the graph data in GPU memory. It helps to perform parallel graph computation, which results in 10x — 500x speedups compared to NetworkX for large graphs. cuGraph integrates with cuDF and cuML for GPU-based DataFrames and Machine Learning. Let’s try to use it on a small dataset for demonstration. Before usage, we must install the packages. To install, use the following command. pip install cugraph-cu12 --extra-index-url=https://pypi.nvidia.com Once installed, we can access the algorithms through the built-in APIs. python import cudfimport cugraph Sample edge listedges = cudf.DataFrame { "src": 0, 1, 2, 3, 4 , "dst": 1, 2, 0, 4, 3 } Create a GraphG = cugraph.Graph G.from cudf edgelist edges, source="src", destination="dst" Run PageRankpagerank scores = cugraph.pagerank G print pagerank scores vertex pagerank0 0 0.21 1 0.22 2 0.23 3 0.24 4 0.2 If you’ve made it this far, you’ve seen how RAPIDS can transform your data workflows. But among the advantages offered by RAPIDS, the exciting thing is that it’s literally the same implementation. Just a few changes in the code. All the functions, APIs, and parameters remain almost the same. If you are someone who works with tabular data, graphs, or machine learning at any non-trivial scale, then favor yourself by choosing RAPIDS AI. Together, these tools form one of the fastest end-to-end data science pipelines available today. Accelerate your processes and build more systems. Do follow me for more such insightful blogs and experiences. I’d love to hear about your experiences in designing and building ML systems in the comments below Raj 🧑🏼💻 Accelerate your ML models using RAPIDS AI https://pub.towardsai.net/accelerate-your-ml-models-using-rapids-ai-ad032d530ae4 was originally published in Towards AI https://pub.towardsai.net on Medium, where people are continuing the conversation by highlighting and responding to this story.