Blog HPC

Billion Rows in Pandas? CuDF, Modin, Arrow, Spark

Evaluating in-memory tabular processing frameworks including CuDF, Modin, Arrow, and Spark on billion-row taxi ride datasets to find the fastest Pandas alternative.

By
Ash Vardanian
Published
Sep 20, 2022

We are continuing our saga of CPU vs. GPU articles comparing the most common data-processing toolkits, and this time it will be about tabular data. Specifically, we will compare frameworks with Pandas-like Python interfaces on a dataset often used to compare SQL Databases.

TLDR: Use Arrow to parse large datasets and split it in batches to process via CuDF 15x faster!

Accelerated Data-Science Libraries talk at PyData by Ash Vardanian

If you are looking for a broader overview of the general topic - my recent PyData talk on “Accelerated Data-Science Libraries” was just published on YouTube.

§ Setup

Meet our dataset - “New York Taxi Rides”.

  • 125 Parquet files,
  • Totaling at 39.7 GB on disk,
  • Containing 1,547,741,381 rows.

The benchmark includes four queries and we will analyze them one by one: 1, 2, 3, 4. But for most impatient, here is a chart.

Query speedups over Pandas, excluding parsing 1B taxi rides

Q1

Pandas 1.0× Modin 9.3× CuDF 4.2× Spark 1.0× Arrow 2.7×

Q2

Pandas 1.0× Modin 0.7× CuDF 1.9× Spark 0.5× Arrow 0.4×

Q3

Pandas 1.0× Modin 9.0× CuDF 13.7× Spark 2.4× Arrow 4.2×

Q4

Pandas 1.0× Modin 2.5× CuDF 14.9× Spark 1.3× Arrow 0.4×
1B taxi ridesPandasModinCuDFSparkArrow
Q11.0×9.3×4.2×1.0×2.7×
Q21.0×0.7×1.9×0.5×0.4×
Q31.0×9.0×13.7×2.4×4.2×
Q41.0×2.5×14.9×1.3×0.4×

Our baseline is Pandas. Every other bar in each group shows a relative speedup of every one of the following libraries:

  1. Modin - a Python library that uses Dask or Ray to parallelize the evaluation of Pandas queries across processes/workers.
  2. CuDF - a hybrid Python, C++, and CUDA library by Nvidia that backs Pandas API calls with GPU kernels.
  3. Spark - a Java-based big-data processing framework with a Python wrapper and a .pandas interface.
  4. Arrow - a C++ framework for in-memory columnar representations and operations on them, with bindings for Python and many other languages.

§ Frameworks

We also looked at Dask, Dask-CuDF, SQLite, DuckDB, and other alternatives. All those projects are vastly different, so comparing them isn’t precisely Apples to Apples. For now, let’s understand the differences in our original set.

EmbeddedMemory Constraints
PandasRAM
ModinRAM
CuDFVRAM
ArrowRAM
SparkRAM/JVM

Two systems (Modin and Spark) are not “embedded” libraries. They depend on external processes with which they communicate through shared memory or sockets. It may not seem like a problem working with 100 rows, but it is with Terabytes. A typical CPU can perform ~10 Billion operations every second, while few I/O engines can handle even a Million packets on a socket. For reference, Redis, often praised for its performance, hardly reaches that mark while still being ahead of Spark.

Query speedups over Pandas, including parsing 1B taxi rides

Q1

Pandas 1.0× Modin 0.9× CuDF 7.3× Spark 1.8× Arrow 1.6×

Q2

Pandas 1.0× Modin 0.5× CuDF 6.8× Spark 1.6× Arrow 0.7×

Q3

Pandas 1.0× Modin 1.5× CuDF 18.3× Spark 3.2× Arrow 2.4×

Q4

Pandas 1.0× Modin 1.2× CuDF 19.2× Spark 1.7× Arrow 0.5×
1B taxi ridesPandasModinCuDFSparkArrow
Q11.0×0.9×7.3×1.8×1.6×
Q21.0×0.5×6.8×1.6×0.7×
Q31.0×1.5×18.3×3.2×2.4×
Q41.0×1.2×19.2×1.7×0.5×

The size of our dataset also makes it hard to process it in memory. Not every computer has 40 GB of RAM, but even that won’t be enough once you decompress and reserve temporary memory for running analysis. Luckily, our sandbox machine has 1 TB of RAM, but CuDF relies on GPUs internal memory - VRAM. It is smaller and more expensive, and each GPU only has 24 GBs of it. So we hand-crafted a “batching” mechanism tailored for each query. CuDF would operate on batches of 10 files simultaneously instead of looking at 125 at once. Consequently, it had to parse every file four times for every one of the queries. Other frameworks, only limited by the size of RAM, parsed the files just once before running analytics and reused the in-memory representation. CuDF was already up to 15x faster than Pandas, before we included the parsing time into the measurement. The picture becomes even more dramatic once we do.

§ Queries

Enough with drawing. Let’s see what exactly we are comparing.

§ Query 1

The original SQL query has the following structure:

SELECT cab_type,
       count(*)
FROM trips
GROUP BY 1;

Rewriting it with Pandas would look similar to this:

selected_df = trips[['cab_type']]
grouped_df = selected_df.groupby('cab_type')
final_df = grouped_df.size().reset_index(name='counts')

It looks pretty straightforward until you start adjusting it for every engine. Their APIs look similar, but not all arguments match, and some functionality will be missing. We will return to stability and interoperability, but before that, let us organize the measurements.

DesktopServerServerServer
OperationsLoad + EvalLoad + EvalLoadEval
Batch Size210125, full dataset125, full dataset
Pandas608.6 s172.1 s51.7 s69.7 s
Modin743.1 s141.8 s121.2 s7.5 s
CuDF61.6 s16.7 s
Spark159.8 s68.3 s11.7 s158.9 s
Arrow78.6 s69.1 s48.0 s25.8 s

Every measurement here is the number of seconds it took to evaluate the query. We repeated the measurements many times on two machines:

  1. “Desktop”: Intel Core i7 + Titan Volta + 32 GB of DDR4.
  2. “Server” was mentioned before.

As expected, CuDF can’t fit the whole dataset into VRAM, so only batched measurements are present. Still, CuDF takes almost 5 times less to parse batches and evaluate the Q1 than Pandas just evaluating the query on an already parsed dataset.

§ Query 2

SELECT passenger_count,
       avg(total_amount)
FROM trips
GROUP BY 1;

This query is also quite simple to translate:

selected_df = trips[['passenger_count', 'total_amount']]
grouped_df = selected_df.groupby('passenger_count')
final_df = grouped_df.mean().reset_index()

Results:

DesktopServerServerServer
OperationsLoad + EvalLoad + EvalLoadEval
Batch Size210125, full dataset125, full dataset
Pandas535.1 s114.1 s51.7 s20.0 s
Modin669.7 s154.1 s121.2 s27.0 s
CuDF28.4 s10.6 s
Spark73.6 s43.7 s11.7 s∞
Arrow62.7 s54.4 s48.0 s14.4 s

Starting with this query, Spark, SQLite, and a few other backends became unusably slow when evaluating bulk datasets.

§ Query 3

SELECT passenger_count,
       extract(year from pickup_datetime),
       count(*)
FROM trips
GROUP BY 1,
         2;

Queries like these are often harder for databases. They may often have an index ready for single-column group-by operations, but aggregating based on multiple columns may require a full scan. For in-memory processing systems, it is less of an issue.

selected_df = trips[['passenger_count', 'pickup_datetime']]
selected_df['year'] = pd.to_datetime(selected_df.pop('pickup_datetime'), format='%Y-%m-%d %H:%M:%S').dt.year
grouped_df = selected_df.groupby(['passenger_count', 'year'])
final_df = grouped_df.size().reset_index(name='counts')

Results:

DesktopServerServerServer
OperationsLoad + EvalLoad + EvalLoadEval
Batch Size210125, full dataset125, full dataset
Pandas858.5 s247.8 s51.7 s153.0 s
Modin729.1 s153.7 s121.2 s17.0 s
CuDF25.5 s11.2 s
Spark125.1 s63.1 s11.7 s∞
Arrow82.4 s74.5 s48.0 s36.1 s

§ Query 4

SELECT passenger_count,
       extract(year from pickup_datetime),
       round(trip_distance),
       count(*)
FROM trips
GROUP BY 1,
         2,
         3
ORDER BY 2,
         4 desc;

This query is the hardest and comprises everything we have tried before. In Pandas, it looks a bit mouthful:

selected_df = trips[['passenger_count', 'pickup_datetime', 'trip_distance']]
selected_df['trip_distance'] = selected_df['trip_distance'].round().astype(int)
selected_df['year'] = pd.to_datetime(selected_df.pop('pickup_datetime'), format='%Y-%m-%d %H:%M:%S').dt.year
grouped_df = selected_df.groupby(['passenger_count', 'year', 'trip_distance'])
final_df = grouped_df.size().reset_index(name='counts')
final_df = final_df.sort_values(['year', 'counts'], ascending=[True, False])

Results:

DesktopServerServerServer
OperationsLoad + EvalLoad + EvalLoadEval
Batch Size210125, full dataset125, full dataset
Pandas859.0 s470.8 s51.7 s178.4 s
Modin1024.2 s247.5 s121.2 s72.3 s
CuDF26.9 s12.0 s
Spark669.4 s136.3 s11.7 s∞
Arrow95.7 s79.4 s48.0 s43.9 s

§ Findings

§ CuDF

Pros:

Cons:

  • VRAM limits dataset size.
  • Not everyone has a GPU. Why?!
  • Default horizontal scaling methods, namely Dask-CuDF, work horribly, even with RMM pools and NV-Link.

Even current performance is far from saturating the GPU compute capabilities. Parsing & data transfers take more time than the actual kernel execution. CXL, PCI-E 5, and next-gen NV-Link will recompense this, but just partly. This is what GPU utilization looks like in such batched queries on RTX 3090:

Scroll horizontally to explore →

GPU compute and memory utilization of an RTX 3090 during batched CuDF queries.

The dataset was placed on a fast PCI-E Gen4 SSD, but we also tried rerunning benchmarks over datasets stored on a RAID-0 array with 8x even faster Gen4 SSDs. We hoped it would affect the batched workloads but registered no fluctuations in our measurements.

Implementation Source.

§ Spark

Pros:

  • Faster than Pandas.
  • Widespread outside of the Python world.
  • A major part of the Apache ecosystem.

Cons:

  • Memory usage patterns and performance are unpredictable due to JVM.
  • Cumbersome configuration via SparkConf, SparkSession, SparkContext.
  • Some types, like datetime64[s] aren’t supported, while others like datetime64[ns] work.
  • Types like category may be dropping the original string labels and need workarounds.
  • Stability.

Implementation Source.

§ Modin

Pros:

  • API compatibility.

Cons:

  • Poor scaling and over-utilization of hardware resources.
  • Many operations, like masking, still fall back on Pandas’ implementation.

Overall, we are not big fans of Modin’s “using Python to accelerate Python” methodology. We have said it before and will repeat it - Python needs a significant upgrade.

Multi-threading is not usable in Python due to Global Interpreter Lock. That is why PyTorch deprecated DataParallel training in favor of DistributedDataParallel.

Need more than one thread - run multiple processes and multiple Python interpreter instances, all isolated, and pay the inter-process communication costs. Just waiting for those processes to synchronize often takes longer than the task itself, making it meaningless for fine-grained parallelism. This also applies to Ray and Dask.

Implementation Source.

§ Arrow

Pros:

  • Highest speed and efficiency on CPU.
  • Embedded and easy to configure/manage.
  • Growing ecosystem.

Cons:

  • The API is very different from Pandas.

But it may not be much of a problem for some users, as Arrow has awe-inspiring support from all sides of the industry…

Without a shared format, every pair of systems needs its own copy and conversion. With Arrow, each system speaks one in-memory format.

Including both compute and storage. Our Kernel Libraries may not be ready for showtime, but we are slowly joining the ecosystem on the storage side with UStore.

Scroll horizontally to explore →

UKV, now UStore: data modalities and storage engines behind one interface, served over several protocols and wrapped into language SDKs. Filled marks were available at the time of writing, hollow ones planned.

Arrow representations are its first-class residents. Furthermore, it seems to be the first storage layer solution using Apache Arrow Flight RPC for client-server communication, meaning that every Arrow-compatible project can work with UStore without additional bindings. We have just completed benchmarking it on 10 TB document collections with 10x more entries and 100x more data, so we are certain it will be pivotal in taking the next big scale!

Implementation Source.

§ Conclusions

To summarize the results, here are the winners across categories:

  1. Consistent performance: Arrow.
  2. Peak performance: CuDF.
  3. Energy efficiency: CuDF.
  4. Accessibility: Modin.
  5. Ecosystem: Arrow and Spark.

This entire benchmark - UDSB, is publicly available on our corporate GitHub, with the original measurements on different machines and scripts for downloading & preprocessing the data. Don’t forget to ⭐ them on GitHub and join the discussions on Discord!