Introduction
The rapid proliferation of Large Language Models (LLMs) has led to an unprecedented demand for GPU compute resources. In this context, even marginal improvements in kernel efficiency can translate into significant savings in energy and hardware costs. While high-level frameworks like OpenAI Triton have democratized the creation of custom GPU kernels, they often fall short of the performance achieved by expert "ninja" engineers who manually tune assembly code. This paper introduces CuAsmRL, a framework that leverages Deep Reinforcement Learning (DRL) to automatically optimize NVIDIA GPU SASS (Streaming Assembler) schedules, bridging the gap between automated compilation and manual expert tuning.
The Challenge of Low-Level Optimization
Standard GPU compilation pipelines typically translate high-level code into PTX (Parallel Thread Execution), an intermediate representation, which is then lowered to SASS by NVIDIA's proprietary ptxas compiler. While ptxas applies aggressive optimizations, it often fails to find the optimal instruction schedule for complex kernels. Expert engineers can outperform the compiler by manually reordering instructions to better hide memory latency and maximize throughput.
However, manual SASS optimization is fraught with difficulty. SASS is a closed-source, poorly documented language. Furthermore, any manual optimization is brittle; a schedule optimized for one tile size or register count may perform poorly or fail entirely if the kernel configuration changes. CuAsmRL addresses these issues by automating the scheduling process through a robust RL-based search strategy that operates directly on the final binary.
Methodology
CuAsmRL treats instruction scheduling as an "assembly game" where an RL agent learns to reorder instructions to minimize execution time. The framework consists of several key components:
1. Integration and Disassembly
CuAsmRL integrates directly with the OpenAI Triton pipeline. It intercepts the compiled cubin file, disassembles it into SASS, and identifies candidate instructions for reordering. Once the RL agent proposes a new schedule, the framework reassembles the code into a functional binary.
2. State and Action Space
The state is represented as a matrix embedding of the SASS instructions, capturing opcodes, operands, and control codes. The action space is defined by the ability to swap specific memory instructions (such as global loads/stores) within the schedule. To ensure the agent does not produce invalid or incorrect code, CuAsmRL employs an Action Masker. This component uses static analysis to map register dependencies and barrier requirements, masking out any moves that would violate program logic or hardware constraints.
3. Reinforcement Learning Strategy
The system utilizes Proximal Policy Optimization (PPO) with a Convolutional Neural Network (CNN) architecture. The agent receives a reward based on the actual execution time of the mutated kernel on physical hardware. This "in-the-loop" evaluation ensures that the agent optimizes for real-world performance rather than a potentially inaccurate simulator model.
Experimental Evaluation
The authors evaluated CuAsmRL on an NVIDIA A100 (Ampere) GPU using a suite of critical LLM kernels, including:
- Compute-bound: Fused feed-forward networks, batch matrix multiplication (BMM), and FlashAttention.
- Memory-bound: Softmax and Root-Mean-Square Layer Normalization (RMSNorm).
Performance was compared against default Triton -O3 outputs, PyTorch/CuBLAS implementations, and highly optimized reference libraries like FlashAttention2.
Key Insights and Results
Performance Gains
CuAsmRL achieved a peak speedup of 26% and an average speedup of 9% over Triton's default optimized SASS. Notably, for complex kernels like FlashAttention, the RL agent was able to close the performance gap between automated Triton code and hand-tuned reference implementations.
Discovery of "Ninja" Tactics
The RL agent successfully discovered non-obvious scheduling strategies previously known only to expert engineers. For example, in fused GEMM kernels, the agent learned to schedule Tensor Core math instructions (HMMA) before global-to-shared memory loads (LDGSTS). This specific interleaving maximizes the use of the .reuse operand cache flag, effectively balancing instruction-level parallelism (ILP) and thread-level parallelism (TLP).
Efficiency and Utilization
Profiling data indicated that while compute utilization remained stable, memory throughput increased significantly (e.g., from 157 GB/s to 175 GB/s in specific tests). This suggests that the agent's primary contribution is the more efficient feeding of the memory pipeline, reducing the time the SM (Streaming Multiprocessor) spends stalled on data.
Limitations and Future Work
Despite its success, CuAsmRL faces challenges regarding training time. Because the agent requires actual GPU execution for rewards—taking approximately 15,000 steps with 200 executions per step—training can take up to five hours per kernel. While this is a manageable offline cost for production kernels, future work could involve developing predictive cost models to accelerate the search. Additionally, the current reliance on hardcoded heuristics for undocumented hardware quirks suggests a need for more generalized models that can adapt to new GPU architectures without manual intervention.
Conclusion
CuAsmRL demonstrates that Deep Reinforcement Learning can effectively navigate the complexities of proprietary GPU assembly. By automating the discovery of optimal instruction schedules, it provides a path toward achieving "ninja-level" performance without the prohibitive cost of manual assembly hacking, offering a powerful tool for the continued optimization of LLM infrastructure.
Summary updated