Mastering Sparse CUDA Generation through Pretrained Models and Deep Reinforcement Learning

Yaoyu Wang, Hankun Dai, Zhidong Yang, Junmin Xiao, Guangming TanICLR 2026

TL;DR

SparseRL is a novel framework that combines Large Language Models (LLMs) with Deep Reinforcement Learning (DRL) to generate hyper-optimized CUDA code for sparse matrix operations. By treating the LLM as a policy and using actual GPU execution time as a reward signal, the system produces code that is not only functionally correct but often executes faster than industry-standard libraries like NVIDIA’s cuSPARSE.

The Challenge of Sparse Data

In High-Performance Computing (HPC), "sparse" data—matrices filled predominantly with zeros—is ubiquitous, powering everything from Graph Neural Networks (GNNs) to large-scale scientific simulations. Unlike dense computations, sparse operations are notoriously difficult to optimize. The "shape" of the data is irregular, highly variable, and often only known at runtime.

While LLMs have become surprisingly adept at writing general-purpose Python or C++, they frequently stumble when faced with the "black magic" of GPU optimization. Writing high-performance CUDA code requires deep architectural knowledge, including memory coalescing, thread synchronization, and cache management. Existing code-generation models typically focus on functional correctness (whether the code compiles and runs) rather than execution efficiency (how fast it runs). SparseRL bridges this gap by training the model to act as a performance engineer.

The SparseRL Approach

The authors propose SparseRL, which frames code generation as a reinforcement learning problem.

SparseRL Training Pipeline

Here is a breakdown of their novel methodology:

  • LLM as a Policy: The framework utilizes a pre-trained model (such as CodeT5 or Qwen) as a stochastic policy. The model "acts" by generating tokens of CUDA code step-by-step.
  • Sinusoidal Sparse Embeddings: To help the model understand the structural "shape" of the input matrix, SparseRL employs a 2D sinusoidal embedding technique—similar to positional encodings in Transformers—to represent the row and column indices of non-zero elements.
  • Hierarchical Reward Function: The core innovation lies in how the model is rewarded across three stages:
  • Compilation: Did the generated code successfully build?
  • Correctness: Does it produce the mathematically accurate result?
  • Efficiency: How does its execution time compare to a baseline like cuSPARSE? Faster execution yields a higher reward.
  • PPO Training: Proximal Policy Optimization (PPO) is used to fine-tune the model. This allows the LLM to explore various optimization strategies (e.g., loop unrolling, shared memory usage) and reinforce the patterns that minimize latency.

Datasets and Evaluation

The researchers evaluated SparseRL using two primary benchmarks:

  1. SuiteSparse Matrix Collection: An industry-standard collection of 1,100 matrices spanning various scientific domains, including fluid dynamics and social networks.
  2. Deep Learning Matrix Collection (DLMC): 500 matrices specifically derived from deep learning workloads, such as pruned neural network layers.

Key Insights

  • Hardware in the Loop: A major takeaway is that HPC code cannot be optimized in a vacuum. By integrating a real GPU compiler and executor into the training loop, the model discovers non-intuitive tweaks—like specific warp-padding strategies—that human engineers might take years to perfect.
  • Modality Alignment: Interestingly, natural language prompts actually hindered performance in the final stages. By removing text prompts and feeding the model raw structural embeddings of the matrix, the model focused entirely on the relationship between data patterns and hardware performance.
  • Beyond "One-Size-Fits-All": SparseRL does not generate a single, generic SpMV (Sparse Matrix-Vector multiplication) kernel. Instead, it generates customized kernels tailored to the specific sparsity pattern of the input matrix at runtime.

Key Results

SparseRL achieves state-of-the-art results across multiple tasks, proving the viability of RL-driven code optimization.

Performance Comparison (GFLOPS)
  • SpMV Performance: SparseRL improved the compilation rate by 20% over existing methods and produced code that ran 30% faster on average.
  • SpMM Performance: In Sparse Matrix-Matrix Multiplication tasks, SparseRL achieved up to a 6.8x speedup over CodeRL, significantly outperforming both NVIDIA’s cuSPARSE and the TVM-S compiler in terms of GFLOPS.
  • Human-Level Quality: Professional GPU developers reviewed the generated code, noting that it was clear, well-commented, and effectively utilized advanced CUDA features like shared memory and atomic operations.

Limitations and Future Work

Despite its success, SparseRL faces a few practical hurdles:

  • Training Cost: Fine-tuning the model requires substantial compute—approximately 5 days on 8 Tesla V100/A100 GPUs. This positions it as an "offline" optimization tool rather than a real-time, millisecond-latency generator.
  • Search Overhead: The time required to generate and verify multiple code candidates is currently high. The authors suggest future work could involve "proxy models" (e.g., GNNs that predict execution time without running the code) to accelerate this search.
  • Scope: While currently focused on CUDA for NVIDIA GPUs, the framework is theoretically backend-agnostic and could be extended to OpenCL or AMD’s ROCm in the future.

Summary updated

BibTeX
@inproceedings{wang2026mastering,
  author = {Yaoyu Wang and Hankun Dai and Zhidong Yang and Junmin Xiao and Guangming Tan},
  title = {{Mastering Sparse CUDA Generation through Pretrained Models and Deep Reinforcement Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=VdLEaGPYWT},
}
Figure