How to use NVIDIA GPUs for Machine Learning with the new Data Science PC from Maingear | by Déborah Mesquita | Towards Data Science

Deep Learning enables us to perform many human-like tasks, but if you’re a data scientist and you don’t work in a FAANG company (or if you’re not developing the next AI startup) chances are that you still use good and old (ok, maybe not that old) Machine Learning to perform your daily tasks.
One characteristic of Deep Learning is that it’s very computationally intensive, so all the main DL libraries make use of GPUs to improve the processing speed. But if you ever felt left out of the party because you don't work with Deep Learning, those days are over: with the RAPIDS suite of libraries now we can run our data science and analytics pipelines entirely on GPUs.
In this article we’re going to talk about some of these RAPIDS libraries and get to know a little more about the new Data Science PC from Maingear.
Generally speaking, GPUs are fast because they have high-bandwidth memories and hardware that performs floating-point arithmetic at significantly higher rates than conventional CPUs [1]. GPUs' main task is to perform the calculations needed to render 3D computer graphics.
But then in 2007 NVIDIA created CUDA. CUDA is a parallel computing platform that provides an API for developers, allowing them to build tools that can make use of GPUs for general-purpose processing.
GPUs had evolved into highly parallel multi-core systems, allowing very efficient manipulation of large blocks of data. This design is more effective than general-purpose central processing unit (CPUs) for algorithms in situations where processing large blocks of data is done in parallel — CUDA article on Wikipedia [2]
Processing large blocks of data is basically what Machine Learning does, so GPUs come in handy for ML tasks. TensorFlow and Pytorch are examples of libraries that already make use of GPUs. Now with the RAPIDS suite of libraries we can also manipulate dataframes and run machine learning algorithms on GPUs as well.
RAPIDS is a suite of open source libraries that integrates with popular data science libraries and workflows to speed up machine learning [3].
Some RAPIDS projects include cuDF, a pandas-like dataframe manipulation library; cuML, a collection of machine learning libraries that will provide GPU versions of algorithms available in sciKit-learn; cuGraph, a NetworkX-like accelerated graph analytics library [4].
Pandas and sciKit-learn are two of the main data science libraries, so let’s get to know more about cuDF and cuML.
cuDF provides a pandas-like API for dataframe manipulation, so if you know how to use pandas you already know how to use cuDF. There is also the Dask-cuDF library if you want to distribute your workflow across multiple GPUs [5].
We can create series and dataframes just like pandas:
import numpy as npimport cudfs = cudf.Series([1,2,3,None,4])df = cudf.DataFrame([('a', list(range(20))),                     ('b', list(reversed(range(20)))),                     ('c', list(range(20)))])
It’s also possible to convert a pandas dataframe to a cuDF dataframe (but this is not recommended):
import pandas as pdimport cudfdf = pd.DataFrame({'a': [0, 1, 2, 3],'b': [0.1, 0.2, None, 0.3]})gdf = cudf.DataFrame.from_pandas(df)
We can also do the opposite and convert a cuDF dataframe to a pandas dataframe:
import cudfdf = cudf.DataFrame([('a', list(range(20))),                     ('b', list(reversed(range(20)))),                     ('c', list(range(20)))])pandas_df = df.head().to_pandas()
Or convert to numpy arrays:
import cudfdf = cudf.DataFrame([('a', list(range(20))),                     ('b', list(reversed(range(20)))),                     ('c', list(range(20)))])df.as_matrix()df['a'].to_array()
Everything else we do with dataframes (viewing data, sorting, selecting, dealing with missing values, working with csv files and so on) works the same:
import cudfdf = cudf.DataFrame([('a', list(range(20))),                     ('b', list(reversed(range(20)))),                     ('c', list(range(20)))])df.head(2)df.sort_values(by='b')df['a']df.loc[2:5, ['a', 'b']]s = cudf.Series([1,2,3,None,4])s.fillna(999)df = cudf.read_csv('example_output/foo.csv')df.to_csv('example_output/foo.csv', index=False)
About performance, just to give an example, loading a 1gb csv file using pandas took 13 seconds and loading it with cuDF took 2.53 seconds.
cuML integrates with other RAPIDS projects to implement machine learning algorithms and mathematical primitives functions. In most cases, cuML’s Python API matches the API from sciKit-learn. The project still has some limitations (currently the instances of cuML RandomForestClassifier cannot be pickled for example) but they have a short 6-week release schedule so they’re always adding new features.
There are implementations for Regression, Classification, Clustering and Dimensionality Reduction algorithms, among other tools. The API is indeed very consistent with sciKit API:
import cudfimport numpy as npfrom cuml.linear_model import LogisticRegressionX = cudf.DataFrame()X['col1'] = np.array([1,1,2,2], dtype = np.float32)X['col2'] = np.array([1,2,2,3], dtype = np.float32)y = cudf.Series( np.array([0.0, 0.0, 1.0, 1.0], dtype = np.float32) )# trainingreg = LogisticRegression()reg.fit(X,y)print("Coefficients:")print(reg.coef_.copy_to_host())print("Intercept:")print(reg.intercept_.copy_to_host())# making predictionsX_new = cudf.DataFrame()X_new['col1'] = np.array([1,5], dtype = np.float32)X_new['col2'] = np.array([2,5], dtype = np.float32)preds = reg.predict(X_new)print("Predictions:")print(preds)
This is all great, but how can we use these tools? Well, first you need to get an NVIDIA GPU card compatible with RAPIDS. If you don’t want to spend time figuring out the best choices for the hardware specs, NVIDIA is releasing the Data Science PC.
The PC comes with a software stack optimized to run all these libraries for Machine Learning and Deep Learning. It comes with Ubuntu 18.04 and you can use docker containers from NVIDIA GPU Cloud or use the native conda environment. One of the best things about the PC is that you get all the libraries and software fully installed. If you ever had to install NVIDIA drivers on a Linux distro or had to install TensorFlow from source you know how dreamy this is. These are the system specifications:
GPU
NVIDIA Titan RTX with 24 GB of GPU memory or 2-way NVIDIA Titan RTX connected via NVIDIA NVLink offering a combined 48 GB of GPU memory
CPU
Intel Core i7 class CPU or higher
System memory
Minimum of 48 GB DDR4 system memory for single GPU configuration and Minimum of 96 GB of DDR4 system memory for dual GPU configurations
Disk
Minimum of 1 TB SSD
The Maingear VYBE PRO Data Science PC comes with up to two dual NVIDIA TITAN RTX 24GB cards and each PC is hand-assembled.
Training a XGBoost model on a VYBER PRO PC using a dataset with 4,000,000 rows and 1000 columns (this dataframe uses approx. 15 GB of memory) takes 1min 46s on CPU (with a memory increment of 73325 MiB) and only 21.2s on GPUs (with a memory increment of 520 MiB).
With Data Science we are always in need to explore and try new things. Among other Software Engineering challenges that make our workflow difficult, the size and the time it takes to compute our data are two bottlenecks that prevent us from getting to a flow state while running our experiments. Having a PC and tools that can improve this can really speed up our work and help us spot interesting patterns in our data faster. Imagine getting a 40 GB csv file and just simply loading it into memory to see what it is about.
The RAPIDS tools bring to machine learning engineers the GPU processing speed improvements deep learning engineers were already familiar with. To make products that use machine learning we need to iterate and make sure we have solid end to end pipelines, and using GPUs to execute them will hopefully improve our outputs for the projects.