Angad Sandhu
Search
Browse
On this page

6.0_cuDF

cuDF

Now let's move onto some more high level APIs, starting with cuDF. Similar to pandas the cudf library is a dataframe package for working with tabular datasets.

Data is loaded onto the GPU and all operations are performed with GPU compute, but the API of cudf should feel very familiar to pandas users.

import cudf

Data loading

This checkout stores pageviews_small.csv beside this notebook, rather than in the absent data/ directory. Run the reads from this workshop directory. The CPU comparison uses Polars (pip install polars); both readers keep the original first physical row as column names. The CPU read infers the full schema and explicitly preserves the original Pandas default missing-value tokens, including the real page title None. Most of this data is too small to really benefit from GPU acceleration, but let's explore it anyway.

import polars as pl
# Preserve the original CSV reader's default missing-value tokens.
df = pl.read_csv(
    "./pageviews_small.csv", separator=" ", infer_schema_length=None,
    null_values=["", "#N/A", "#N/A N/A", "#NA", "-1.#IND", "-1.#QNAN",
                 "-NaN", "-nan", "1.#IND", "1.#QNAN", "<NA>", "N/A", "NA",
                 "NULL", "NaN", "None", "n/a", "nan", "null"],
)
df.head()
pageviews = cudf.read_csv("./pageviews_small.csv", sep=" ")
pageviews.head()

This pageviews.csv file contains just over 1M records of pageview counts from Wikipedia in various languages.

Let's rename the columns and drop the unused x column.

pageviews.columns = ['project', 'page', 'requests', 'x']

pageviews = pageviews.drop('x', axis=1)

pageviews

Next let's count how many english record are in this dataset.

print(pageviews[pageviews.project == 'en'].count())

Then let's perform a groupby where we count all of the pages by language.

grouped_pageviews = pageviews.groupby('project').count().reset_index()
grouped_pageviews

And finally let's have a look at the results for English, French, Chinese and Polish specificallty.

print(grouped_pageviews[grouped_pageviews.project.isin(['en', 'fr', 'zh', 'pl'])])

If you have used pandas before then all fo this syntax should be very familiar to you. In the same way that cupy implements a large portion of the numpy API, cudf implements a large portion of the pandas API.

The only difference is that all of our filtering and groupby operations happened on the GPU instead of the CPU giving much better performance.

Strings

GPUs historically are well known for numerical work and have not been used for working with more complex objects. With cuDF string operations are also acellerated thanks to leveraging cuStrings under the hood.

This means operations like capitalizing strings can be parallelised on the GPU.

pageviews[pageviews.project == 'en'].page.str.upper()
pageviews_en = pageviews[pageviews.project == 'en']
print(pageviews_en.page.str.upper().head())

UDFs

cuDF also has support for user defined functions (UDFs) that can be mapped over a Series or DataFrame in parallel on the GPU.

UDFs can be defined as pure Python functions that take a single value, these will be compiled down by Numba at runtime into something that can run on the GPU when we call .apply().

def udf(x):
    if x < 5:
        return 0
    return x
pageviews.requests = pageviews.requests.apply(udf)
pageviews.requests

It is also possible to use Numba directly to write kernels that take pointers to an input column and an output column along with additional arguments. The kernel can then use cuda.grid the same way we did in chapters 2/3 to get an index to operate on.

We then use .forall() to map our kernel over a column.

pageviews['mul_requests'] = 0.0
from numba import cuda
@cuda.jit
def multiply(in_col, out_col, multiplier):
    i = cuda.grid(1)
    if i < in_col.size: # boundary guard
        out_col[i] = in_col[i] * multiplier
multiply.forall(len(pageviews))(pageviews['requests'], pageviews['mul_requests'], 10.0)
print(pageviews.head())

Rolling windows

In cuDF there is also support for applying kernels over rolling windows. This is effectively a 1D stencil and can allow us to perform operations based on our neigbors.

Image unavailable: (unlabeled image) — original archive file images/rolling-windows.png is absent.

def neigborhood_mean(window):
    c = 0
    for val in window:
        c += val
    return c / len(window)
pageviews.requests.rolling(3, 1, True).apply(neigborhood_mean)

Browse the library