cuDF plugin for Apache Spark

Build a Spark 3.5 image with the cuDF plugin, run the branch roll-up on a GPU in OpenShift, and read the operators that stayed on the CPU.

Updated CompTech Team

Spark stays the engine. The cuDF plugin for Apache Spark (the plugin formerly called the RAPIDS Accelerator for Apache Spark) replaces supported SQL operators with GPU operators, one at a time, and leaves the rest on the CPU. This lab builds the image, submits the job on OpenShift, and times a CPU run against a GPU run. The jars are still rapids-4-spark_2.13-26.08.1. We use the CUDA 13 build, because the lab GPU is an A2 with driver 580.

Versions on our cluster: OpenShift 4.22, GPU Operator 26.7.2, Stackable Spark operator 26.7, Spark 3.5.5 built for Scala 2.13, plugin 26.08.1. The single-machine pandas timing for this same roll-up is in the cuDF lab and the overview. A Spark duration is a different measurement. Record it. Do not borrow the 6.10 s figure.

Prerequisites

  • The GPU worker from the overview, so oc describe node shows nvidia.com/gpu
  • oc, a namespace rapids, and the Stackable operator (oc get crd sparkapplications.spark.stackable.tech)
  • Spark 3.5.5 Scala 2.13 unpacked on the build machine, Java 17, and Podman
  • The plugin jar rapids-4-spark_2.13-26.08.1-cuda13.jar (Maven classifier cuda13)
curl -fL -o rapids-4-spark_2.13-26.08.1-cuda13.jar \
  https://repo1.maven.org/maven2/com/nvidia/rapids-4-spark_2.13/26.08.1/rapids-4-spark_2.13-26.08.1-cuda13.jar
tar -xzf spark-3.5.5-bin-hadoop3-scala2.13.tgz

The Scala version of the jar and the Spark distribution have to match. The plain spark-3.5.5-bin-hadoop3.tgz is Scala 2.12 and needs rapids-4-spark_2.12 instead.

The image

Dockerfile starts from the CUDA 13 runtime on UBI 9, adds Java 17 and Spark, and copies the plugin next to Spark's own GPU discovery script:

FROM nvidia/cuda:13.0.1-runtime-ubi9

USER 0
RUN dnf install -y java-17-openjdk-headless python3 && dnf clean all \
 && ln -sf /usr/bin/python3 /usr/bin/python
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk
ENV SPARK_HOME=/opt/spark
ENV PATH=$SPARK_HOME/bin:$PATH

COPY spark-3.5.5-bin-hadoop3-scala2.13/ /opt/spark/
COPY rapids-4-spark_2.13-26.08.1-cuda13.jar /opt/sparkRapidsPlugin/rapids-4-spark.jar
COPY spark-3.5.5-bin-hadoop3-scala2.13/examples/src/main/scripts/getGpusResources.sh \
     /opt/sparkRapidsPlugin/getGpusResources.sh
COPY spark-3.5.5-bin-hadoop3-scala2.13/kubernetes/dockerfiles/spark/entrypoint.sh /opt/entrypoint.sh
COPY pipeline.py /opt/spark/work-dir/pipeline.py
RUN chmod 755 /opt/sparkRapidsPlugin/getGpusResources.sh /opt/entrypoint.sh \
 && chgrp -R 0 /opt/spark /opt/sparkRapidsPlugin && chmod -R g=u /opt/spark /opt/sparkRapidsPlugin

WORKDIR /opt/spark/work-dir
ENTRYPOINT ["/opt/nvidia/nvidia_entrypoint.sh", "/opt/entrypoint.sh"]
podman build -t spark-cudf:3.5.5 .

The discovery script runs nvidia-smi and prints {"name":"gpu","addresses":["0"]}. The device plugin mounts nvidia-smi. Spark allows one GPU per executor, shared by several tasks. The image runs as root, which is why the lab grants anyuid.

The job

pipeline.py is the cuDF lab roll-up in Spark SQL. With no arguments it builds a fixed synthetic set (5 million transactions, 500,000 accounts) so the job runs with no bucket. Pass two Parquet paths to use the Lab 1 files.

import sys
from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("branch-spend").getOrCreate()
spark.conf.set("spark.sql.session.timeZone", "UTC")

if len(sys.argv) >= 3:
    spark.read.parquet(sys.argv[1]).createOrReplaceTempView("tx")
    spark.read.parquet(sys.argv[2]).createOrReplaceTempView("acc")
else:
    spark.range(0, 5_000_000).selectExpr(
        "id as tx_id",
        "cast(id % 500000 as long) as account_id",
        "case when id % 20 = 0 then 'void' else 'posted' end as status",
        "cast((id % 5000) / 100.0 as double) as amount",
        "timestampadd(SECOND, cast(id % 31536000 as int), timestamp'2025-01-01') as ts",
    ).createOrReplaceTempView("tx")
    spark.range(0, 500_000).selectExpr(
        "id as account_id",
        "cast(id % 200 as int) as branch_id",
        "concat('S', cast(id % 4 as string)) as segment",
    ).createOrReplaceTempView("acc")

top = spark.sql("""
SELECT month, branch_id, segment, spend, accounts FROM (
  SELECT month, branch_id, segment, spend, accounts,
         row_number() OVER (PARTITION BY month ORDER BY spend DESC) AS rk
  FROM (
    SELECT a.branch_id, a.segment, m.month,
           sum(m.spend) AS spend, count(DISTINCT m.account_id) AS accounts
    FROM (
      SELECT account_id, year(ts) * 100 + month(ts) AS month, sum(amount) AS spend
      FROM tx WHERE status = 'posted' AND amount > 0
      GROUP BY account_id, year(ts) * 100 + month(ts)
    ) m
    JOIN acc a ON m.account_id = a.account_id
    GROUP BY a.branch_id, a.segment, m.month
  )
) WHERE rk <= 5
""")
top.write.mode("overwrite").parquet("/tmp/top-branches")
print("rows", top.count())
spark.stop()

Timestamps stay on the GPU only in UTC. A Python UDF will not. Keep the job in SQL.

Submit on OpenShift

The Stackable operator starts the driver, and the driver starts executors. Confirm nvidia.com/gpu: 1 on the executor pod. Spark's resource config is what requests the card.

oc new-project rapids
oc new-build --binary --strategy=docker --name=spark-cudf -n rapids
oc start-build spark-cudf --from-dir=. --follow -n rapids
oc create sa spark -n rapids
oc adm policy add-role-to-user edit -z spark -n rapids
# Lab shortcut: the CUDA entrypoint and Spark's Kubernetes launcher expect to run as a chosen uid.
oc adm policy add-scc-to-user anyuid -z spark -n rapids

deploy/branch-spend.yaml:

apiVersion: spark.stackable.tech/v1alpha1
kind: SparkApplication
metadata:
  name: branch-spend
  namespace: rapids
spec:
  sparkImage:
    custom: image-registry.openshift-image-registry.svc:5000/rapids/spark-cudf:latest
    productVersion: "3.5.5"
  mode: cluster
  mainApplicationFile: local:///opt/spark/work-dir/pipeline.py
  sparkConf:
    spark.plugins: com.nvidia.spark.SQLPlugin
    spark.executor.instances: "1"
    spark.executor.cores: "4"
    spark.executor.memory: 4g
    spark.executor.memoryOverhead: 2g
    spark.executor.resource.gpu.amount: "1"
    spark.task.resource.gpu.amount: "0.25"
    spark.executor.resource.gpu.vendor: nvidia.com
    spark.executor.resource.gpu.discoveryScript: /opt/sparkRapidsPlugin/getGpusResources.sh
    spark.executor.extraClassPath: /opt/sparkRapidsPlugin/rapids-4-spark.jar
    spark.driver.extraClassPath: /opt/sparkRapidsPlugin/rapids-4-spark.jar
    spark.rapids.sql.explain: NOT_ON_GPU
    spark.rapids.sql.concurrentGpuTasks: "2"
    spark.rapids.memory.pinnedPool.size: 1g
    spark.sql.files.maxPartitionBytes: 512m
    spark.sql.session.timeZone: UTC
    spark.eventLog.enabled: "true"
    spark.eventLog.dir: file:/tmp/events
  driver:
    config:
      resources:
        cpu: { min: "1", max: "1" }
        memory: { limit: 2Gi }
  executor:
    replicas: 1
    config:
      resources:
        cpu: { min: "4", max: "4" }
        memory: { limit: 8Gi }

spark.task.resource.gpu.amount is 1 / spark.executor.cores, so four tasks share the card. concurrentGpuTasks is how many of those tasks run CUDA at once. Two is a start on a 16 GB A2. More than the card can hold will spill or fail.

oc apply -f deploy/branch-spend.yaml
oc get pods -n rapids -w
oc logs -n rapids -l spark-role=driver --tail=50

If the executor has no nvidia.com/gpu: 1, the plugin has no device and every operator stays on the CPU while the job still succeeds.

The same keys work as --conf on spark-submit without the operator. Put them in gpu.conf (one key value per line) and submit from a machine logged in with oc:

spark-submit --master k8s://$(oc whoami --show-server) --deploy-mode cluster \
  --name branch-spend --properties-file gpu.conf \
  --conf spark.kubernetes.namespace=rapids \
  --conf spark.kubernetes.authenticate.driver.serviceAccountName=spark \
  --conf spark.kubernetes.authenticate.submission.oauthToken=$(oc whoami -t) \
  --conf spark.kubernetes.container.image=image-registry.openshift-image-registry.svc:5000/rapids/spark-cudf:latest \
  local:///opt/spark/work-dir/pipeline.py

Read NOT_ON_GPU, then time it

spark.rapids.sql.explain=NOT_ON_GPU logs only operators that stayed on the CPU. A timestamp outside UTC, a Python UDF (ArrowEvalPython), or an expression the plugin has not implemented will show up. Rewrite the UDF as SQL, or leave the expression and accept a copy back to the CPU.

Remove the plugin (spark.plugins unset, no GPU limit) and run the same SparkApplication once. That is the CPU time. Put the plugin back, run twice, and keep the second duration. The first GPU launch pays for kernel compilation, the same effect the overview measured. The Spark UI's job duration, or the driver line for the action (count), is the number to write down.

Parquet on S3 is Hadoop configuration on the same sparkConf: spark.hadoop.fs.s3a.endpoint, spark.hadoop.fs.s3a.path.style.access=true, and the access key and secret. Use http for an in-cluster RGW and https for the external route. Take the keys from the lakehouse secret in the lakehouse lab. spark.sql.files.maxPartitionBytes=512m keeps splits large enough that the GPU is not starved.

The qualification tool reads CPU event logs and estimates which applications are worth moving. It does not need a GPU. Copy the event directory off the driver, then:

pip install spark-rapids-user-tools
spark_rapids qualification --eventlogs file:///tmp/cpu-events

Read qualification_summary.csv in the run directory it prints. The estimate is a planning input. The number you keep is still the timed pair from the cluster. After a GPU run, spark_rapids profiling --eventlogs file:///tmp/gpu-events explains that run. The Apache Spark lab is the CPU job this plugin attaches to. Streaming stays on Kafka. cuML trains on the account features. Drop anyuid for a real cluster and use an SCC that only allows the uid the image runs as.