Switching from CPUs to GPUs for NYC Taxi Fare Predictions with NVIDIA RAPIDS

0/5 No votes

Report this app



Have you ever ever requested a knowledge scientist in the event that they wished their code to run sooner? You’ll in all probability get a extra assorted response asking if the earth is flat. It actually isn’t any completely different from anything in tech, sooner is nearly all the time higher. Probably the greatest methods to make a considerable enchancment in processing time is to, in the event you haven’t already, switched from CPUs to GPUs. Because of pioneers like Andrew NG and Fei-Fei Li, GPUs have made headlines for performing notably properly with deep studying strategies.

At the moment, deep studying and GPUs are virtually synonymous. Whereas deep studying is a superb use of the processing energy of a graphics card, it’s not the one use. In accordance with a ballot in Kaggle’s State of Machine Studying and Information Science 2020, A Convolutional Neural Community was the most well-liked deep studying algorithm used amongst polled people, however it was not even within the prime 3. The truth is solely 43.2% of respondents reported to make use of CNN’s. Forward of the most well-liked deep studying methodology was (1) Linear or Logistic Regression with 83.7%, (2) Determination Timber or Random Forests with 78.1%, and (3) Gradient Boosting Machines with 61.4%.

Kaggle’s State of Machine Learning and Data Science 2020

Picture Credit score: Kaggle

Let’s revisit our very first query: have you ever ever requested a knowledge scientist in the event that they wished their code to run sooner. We all know that each knowledge scientist desires to spend extra time exploring knowledge and fewer time watching a jupyter cell run, however the overwhelming majority of consumers that we communicate to aren’t even utilizing GPUs when working with the highest 3 hottest algorithms, or the 80% knowledge science that isn’t coaching fashions (google knowledge science 80/20 if that is information to you).

From my expertise there are 3 foremost causes (in addition to the apparent: price) why knowledge scientists don’t use GPUs for workloads exterior of deep studying:

  1. Information is just too small (juice not definitely worth the squeeze)
  2. Time required to configure an setting with GPUs
  3. Time required to refactor CPU code

I want to make one thing very clear. In case your knowledge may be very unlikely to ever attain a row rely within the tens of millions, you’ll be able to in all probability disregard this weblog (that’s, until you wish to study one thing new). Nevertheless, in case you are in reality working with a considerable quantity of information, i.e. row rely > 1M, then the limitations to begin utilizing GPUs to your knowledge science, i.e. causes 2 and three, might be resolved simply with Cloudera Machine Studying and NVIDIA RAPIDS.

Cloudera Machine Studying (CML) is considered one of many Information Companies accessible within the Cloudera Information Platform. CML presents all of the performance you’d count on from a contemporary knowledge science platform, like scalable compute assets and entry to most well-liked instruments, together with the good thing about being managed, ruled, and secured by Cloudera’s Shared Information Expertise, or SDX.

NVIDIA RAPIDS is a set of software program libraries that allows you to run end-to-end knowledge science workflows totally on GPUs. RAPIDS depends on NVIDIA CUDA primitives for low-level compute optimization, however exposes that prime efficiency by way of user-friendly Python interfaces.

Collectively, CML and NVIDIA provide the RAPIDS Version Machine Studying Runtime. ML Runtimes are safe, customizable, and containerized working environments. The RAPIDS Version Runtime is constructed on prime of neighborhood constructed RAPIDS docker photos, enabling knowledge scientists to rise up and working on GPUs with the one click on of a button, with all of the assets and libraries they want at their fingertips. Checkmate purpose 2.

The RAPIDS Edition Runtime

Word: The above picture is the dialogue field for beginning a session in Cloudera Machine Studying. It supplies entry to your organization’s catalogue of ML Runtimes and enabled useful resource profiles. Right here I’ve solely chosen a single GPU, however you’ll be able to choose a couple of if wanted

That also leaves us with purpose 3 why knowledge science practitioners are hesitant to make use of GPUs. Information science is already a subject of many fields. You must be proficient in programming, statistics, math, communication, and the area you’re working in. The very last thing you wish to do is study a bunch of latest libraries, or worse, a brand new programming language! To that finish, let’s discover the Python interfaces that RAPIDS presents.

NVIDIA claims that RAPIDS Python interfaces are user-friendly. However that assertion fails to totally encapsulate simply how pleasant these interfaces are to a seasoned Python knowledge science programmer. RAPIDS libraries like cuDF for dataframes and cuML for machine studying are basically GPU variations of their CPU counterparts pandas and scikit-learn. It’s like shifting to a brand new college and discovering out that your greatest pal’s twin is in your house room.

After I first began working with RAPIDS libraries I used to be skeptical. I assumed that the fundamentals of the syntax can be much like the CPU libraries they goal to hurry up, however removed from carbon copies. So I put it to a take a look at, utilizing solely CPU based mostly Python libraries I imported, cleaned, filtered, featurized, and educated a mannequin utilizing journey knowledge for NYC taxis. I then changed the CPU libraries with their corresponding NVIDIA libraries however left the identify they had been certain to the identical. For instance, as an alternative of import pandas as pd I used import cudf as pd. 

Guess what occurred! It didn’t work… however it ALMOST labored.


In my case, for RAPIDS Launch v0.18, I discovered two edge instances the place cuDF and pandas differed, one involving dealing with date columns (why can’t the world agree on a typical date/time format?) and the opposite making use of a customized perform. I’ll discuss by way of how I dealt with these within the script, however be aware that we solely have to barely alter 3 of the 100+ traces of code.

The foundation explanation for the primary challenge is that cuDF’s parse_dates doesn’t deal with uncommon or non-standard codecs in addition to pandas. The repair is simple sufficient although, simply explicitly specify dtype=’date’ for the date column and also you’ll get the identical datetime64 date kind to your date column as you’d with pandas.

The second challenge is barely extra concerned. cuDF doesn’t provide an actual reproduction for DataFrame.apply prefer it does for different pandas operators. As a substitute, it’s good to use DataFrame.apply_rows. The anticipated enter for these capabilities will not be the identical, however it’s comparable. 

NVIDIA has not too long ago launched a Nightly construct of RAPIDS 21.12 (NVIDIA switched from SemVer to CalVer in August for his or her versioning scheme) that’s supposed to copy the DataFrame.apply performance in Pandas. On the time of publishing I used to be not capable of validate this performance, nevertheless builds submit 21.12 ought to solely require a single minor change to an information kind to benefit from GPU efficiency in CML for this undertaking.

In my case, I used to be making use of a perform to calculate the haversine distance between two lat/lengthy coordinates. Right here is the perform and the way it’s utilized to a dataframe (taxi_df) in pandas, leading to a brand new column (hav_distance):

def haversine_distance(x_1, y_1, x_2, y_2):
    x_1 = pi/180 * x_1
    y_1 = pi/180 * y_1
    x_2 = pi/180 * x_2
    y_2 = pi/180 * y_2
    dlon = y_2 - y_1
    dlat = x_2 - x_1
    a = sin(dlat/2)**2 + cos(x_1) * cos(x_2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers
    return c * r

taxi_df['hav_distance'] = taxi_df.apply(lambda row:haversine_distance(row['pickup_latitude'],

By comparability, right here is the haversine perform utilized in cuDF:

def haversine_distance(pickup_latitude,
    for i, (x_1, y_1, x_2, y_2) in enumerate(zip(pickup_latitude,

        x_1 = pi/180 * x_1
        y_1 = pi/180 * y_1
        x_2 = pi/180 * x_2
        y_2 = pi/180 * y_2

        dlon = y_2 - y_1
        dlat = x_2 - x_1
        a = sin(dlat/2)**2 + cos(x_1) * cos(x_2) * sin(dlon/2)**2

        c = 2 * asin(sqrt(a)) 
        r = 6371 # Radius of earth in kilometers

        hav_distance[i] = c * r

taxi_df = taxi_df.apply_rows(haversine_distance,

The logic of the perform is similar, however the way you deal with the perform inputs and the way person outlined perform is utilized to the cuDF dataframe may be very completely different from pandas. Discover that I needed to zip after which enumerate by way of the arguments throughout the haversine_distance perform. 

Moreover, when making use of this perform to the dataframe, the apply_rows perform has required enter parameters with particular guidelines. For instance, the worth(s) handed to incols are the names of the columns handed to the perform, they need to both match the names of the arguments within the perform, or it’s a must to go a dictionary which matches the column names to their corresponding perform arguments. 

For a extra in depth rationalization of utilizing person outlined capabilities with cuDF dataframes, it’s best to check out the RAPIDS docs.

Quick and Livid Outcomes

So, after a couple of minor modifications, I used to be efficiently capable of run pandas and scikit-learn code on GPUs due to RAPIDS.

And now, with out additional ado, the second you’ve all been ready for. I’ll exhibit the precise velocity enhancements when switching from pandas and scikit-learn to cuDF and cuML by way of a sequence of charts. The primary compares the seconds spent on the shorter duties between GPUs and CPUs. As you’ll be able to see, the scales between CPU and GPU runtimes aren’t actually the identical.

scales between CPU and GPU runtimes

Subsequent up let’s study the runtime, in seconds, of the longer working process. We’re speaking about, you guessed it, the person outlined perform that we all know has historically been a poor performer for pandas dataframes. Discover the EXTREME distinction in efficiency between CPU and GPU. That’s a 99.9% lower in runtime!

examine the runtime

The UDF part of our CPU code performs the worst by far at 526 seconds. The subsequent closest part is “Learn within the csv” which takes 63 seconds. 

The UDF section of our CPU code performs the worst by far

Now examine this to the efficiency of the sections working on GPUs. You’ll discover that “Apply haversine UDF” isn’t the worst performing part anymore. The truth is, it’s FAR from the worst performing part. cuDF FTW!

Final of all, here’s a graph with the complete finish to finish runtime of the experiment working on CPUs after which GPUs. In all, the cuDF and cuML code decreased the runtime by 98%! Better of all, all it took was switching to RAPIDS libraries and altering a couple of traces of code.

the full end to end runtime of the experiment running on CPUs and then GPUs


GPUs aren’t just for deep studying, with RAPIDS libraries GPUs can be utilized to hurry up the efficiency of the complete finish to finish knowledge science lifecycle with minimal modifications to CPU libraries that each one knowledge scientists know and love.

If you want to study extra about this undertaking, it’s best to attend NVIDIA GTC on November 8-11 the place I might be presenting “From CPU to GPU with Cloudera Machine Studying”. Register right this moment to attend this session and others.

Further Sources

Comply with the hyperlinks under if you want to run the experiment for your self:

  • Video – watch a brief demo video protecting this use case
  • Tutorial – comply with step-by-step directions to arrange and run this use case
  • Meetup / Recording – be a part of an interactive meetup live-stream round this use case led by Cloudera consultants
  • Github – see the code for your self

Lastly, don’t overlook to take a look at the customers web page for extra nice technical content material.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.