AI Explainers Developer Tools

Spark GPU Acceleration with AI Agents: Rewriting Slow UDFs

Spark GPU acceleration with AI agents targets the user-defined functions that force data back to the CPU; NVIDIA reports one rewrite cut 274 hours to 13.

Illustration of a data pipeline moving through a GPU, representing Spark GPU acceleration with AI agents rewriting slow functions
AI-generated editorial illustration.

Why does a Spark job that runs on GPUs still crawl through certain steps? Often the cause is a user-defined function, or UDF: custom code that Spark cannot look inside. Spark GPU acceleration AI agents are NVIDIA’s proposed answer, and NVIDIA’s developer blog describes a set of agent-driven workflows that rewrite those functions into GPU-friendly equivalents.

What is a UDF, and why does it slow down a GPU job?

Apache Spark is a widely used platform for processing large datasets. Most users write queries through SQL or DataFrames, which describe what result they want and leave the how to Spark. NVIDIA explains that this separation is what lets its cuDF plugin for Spark spread row-by-row operations across a GPU’s many parallel units without any change to user code.

A UDF is a custom routine a team writes to hold its own business logic. NVIDIA’s blog notes that UDFs break the declarative approach because they contain arbitrary code the system cannot analyze or parallelize. The GPU accelerator must convert the data from a columnar layout to rows, fall back to the CPU, and run the function row by row. That conversion and fallback adds transfer overhead and cancels much of the GPU’s advantage.

NVIDIA’s documentation describes the same fallback behavior at the plugin level: operations the plugin supports run on the GPU, and anything unsupported runs on the regular Spark CPU path. The documentation also states that the plugin cannot accelerate operations that manipulate RDDs, Spark’s older low-level data structure, directly.

How does the cuDF plugin plug into Spark?

According to NVIDIA’s documentation, Apache Spark 3.3 and later lets users supply a plugin that replaces the backend for SQL and DataFrame operations, and this needs no API changes from the user. The plugin swaps in GPU versions of the operations it supports. Running it requires a cluster with GPUs that meet the RAPIDS system requirements, one GPU per executor, and the cuDF plugin itself, enabled by pointing Spark’s plugin setting at NVIDIA’s SQL plugin class.

NVIDIA’s documentation also covers GPU scheduling. Spark can request GPUs and assign fractions of one to individual tasks, so several tasks can share a GPU. The documented guidance is to match that fraction to the number of executor cores for best performance. An explain setting can log which parts of a query landed on the GPU and which did not, with the not-on-GPU view as the default.

What are the three ways to rewrite a UDF?

NVIDIA’s blog lists three routes, ordered here from simplest to most involved:

  • Spark SQL expressions. Express the logic with Spark’s built-in operations, which the plugin already knows how to accelerate.
  • The RapidsUDF interface. Implement the function through the Java API for cuDF, so it runs natively on the GPU.
  • Custom CUDA kernels via JNI. Write low-level GPU code and call it from Spark through the Java Native Interface, for logic that the first two routes cannot express.

NVIDIA says that eliminating the fallback this way can yield speedups of more than 20x. The blog also notes that rewriting UDFs has traditionally been laborious because every rewrite needs careful validation that it produces the same results as the original.

What do the agent workflows actually do?

NVIDIA introduces the cuDF skills for Apache Spark, a set of skills that guide an AI coding agent through testing, conversion, benchmarking, and optimization. A skill, in this context, is a packaged set of instructions and tooling an agent can follow for a specific job. NVIDIA describes the workflow as using verification-driven feedback loops: the agent checks its rewritten function against the original and measures performance before moving on.

NVIDIA’s blog says this approach can turn a UDF into a GPU-accelerated equivalent within minutes, with speedups above 22x in Apache Spark jobs. Those figures come from NVIDIA’s own post, and the post is written by NVIDIA about NVIDIA’s products. It also carries an AI-generated summary with a note advising readers to verify important information.

A worked example of the workflow

Consider a data team whose nightly job calls a custom function to clean and score product records. The explain log shows that step falling back to the CPU while everything around it runs on the GPU. Following the pattern NVIDIA describes, the team would point an agent equipped with the cuDF skills at that function.

  • The agent first builds tests that capture what the existing function returns for a range of inputs.
  • It then proposes a rewrite, starting with the simplest route, Spark SQL expressions, and moving to the RapidsUDF interface or a custom kernel only if needed.
  • It runs the tests to confirm the new version matches the old one, then benchmarks both.
  • It optimizes based on those measurements and feeds the results back into the next attempt.

NVIDIA reports that a major consumer technology company converted more than 30 production UDFs with this workflow. In one case, operator time on a workload of 600 billion rows fell from 274.55 hours to 12.78 hours. NVIDIA does not name the company, so that result cannot be traced beyond the blog’s own account.

Where does this sit among other approaches?

The general category is accelerating data engines by replacing slow custom code with natively parallel operations. The cuDF plugin handles the standard SQL and DataFrame path automatically, so the remaining work concentrates on code the engine cannot see into. Rewriting a UDF by hand with Spark’s built-in expressions is the long-standing manual version of this fix. The agent workflow automates the same rewrite and its verification rather than introducing a different technique, and the heavier routes, the Java cuDF API and CUDA kernels, still demand GPU programming knowledge that an agent has to supply on the user’s behalf.

Limitations in practice

The published speedups come from NVIDIA’s own workloads and one unnamed customer, and speedup depends heavily on what a given function does, so results on other pipelines may differ. An agent’s rewrite is only as trustworthy as the tests that check it, which means teams should review the generated test coverage and compare outputs on their own data before replacing production code. NVIDIA’s plugin also has hardware and software requirements, including supported Spark versions, specific drivers, and listed GPU models, so the current compatibility documentation is the place to confirm a cluster qualifies.

What does the plugin require before any of this applies?

NVIDIA’s compatibility summary for the 26.08 plugin release lists Linux with glibc 2.28 or newer and NVIDIA driver R525 or later. It supports Scala 2.12 with Spark 3.3.0 through 3.5.9, and Scala 2.13 with several Spark 3.5 and 4.x releases. Named platforms include specific Databricks ML runtimes and several Google Cloud Dataproc versions. The listed packages are built against CUDA 12.9 or 13.1 and tested on GPUs from V100 through GB100. Any environment outside that list needs a check against NVIDIA’s current documentation.

The open question is how well the agent’s verification holds up on functions with unusual edge cases, since NVIDIA’s blog describes the feedback loops but its reported results focus on speed rather than on how often a rewrite needed human correction.

Sources