Introduction
Writing high-performance GPU kernels is a specialized skill requiring mastery of complex domain-specific languages like CUDA or Metal. A kernel optimized for an NVIDIA H100 often requires a fundamental algorithmic rewrite to run efficiently on an Apple M4 or AMD MI300 due to differing memory architectures. While automated tools like torch.compile exist, they frequently struggle with complex, multi-step operations.
KForge is an agentic framework designed to bridge this gap by using Large Language Models (LLMs) to automate the creation and optimization of GPU kernels. By mirroring the human workflow of iterative coding and performance profiling, KForge can generate programs that match or outperform hand-optimized baselines across diverse hardware backends.
The KForge Framework
KForge employs a multi-stage autonomous architecture consisting of two collaborative LLM-based agents that operate in a continuous feedback loop.
1. The Generation Agent
This agent produces the initial code through a "functional pass." It writes a kernel, attempts to compile and execute it, and uses resulting error messages or numerical mismatches to iteratively fix the code until it is functionally correct.
2. The Performance Analysis Agent
Once the code is functional, the Performance Analysis Agent initiates an "optimization pass." It processes raw profiling data—ranging from textual logs (NVIDIA Nsight) to visual screenshots of GUI tools (Apple Xcode Instruments). By identifying bottlenecks like low warp occupancy or memory bubbles, it provides actionable optimization hints back to the Generation Agent.
Experimental Evaluation
The framework was evaluated using KernelBench, a benchmark of 250 PyTorch workloads categorized into three difficulty levels:
- Level 1: Single primitives (e.g., simple convolutions).
- Level 2: Sequences of operations with fusion potential.
- Level 3: Complete architectures (e.g., AlexNet or Transformer components).
Testing spanned eight state-of-the-art LLMs, including OpenAI’s o3 and gpt-5 (preview), Anthropic’s Claude 3.5 Sonnet, and DeepSeek-R1.
Key Insights and Results
Reasoning and Cross-Platform Transfer
The study found a massive performance gap between standard "chat" models and "reasoning" models. For complex Level 3 kernels, reasoning models were often the only ones capable of producing functionally correct code. Interestingly, providing a CUDA reference implementation significantly boosted the quality of Apple Metal kernels, suggesting that LLMs can perform effective cross-platform knowledge transfer of parallel logic.
Performance Gains from Profiling
On the Apple Metal backend, incorporating visual profiling information allowed models to significantly increase the number of high-speed kernels. For the most difficult Level 3 problems, the addition of profiling feedback resulted in a 30% increase in kernels meeting the high-speed threshold.
Beating the Compiler
KForge demonstrated the ability to outperform standard automation. For Level 3 problems, kernels generated by GPT-5 were 1.5x faster than torch.compile in 11% of cases. In some instances, the LLMs discovered algorithmic optimizations that human-written benchmarks had overlooked, such as simplifying complex operations into constant values or basic matrix-vector multiplications.
Limitations and Future Work
While effective, the current feedback loop can sometimes lead models into local optima, focusing on marginal improvements rather than fundamental algorithmic restructures. Additionally, raw profiling data can be overwhelming for current LLMs.
Future research will focus on expanding support to training kernels (forward and backward passes), teaching LLMs to work directly with compiler Intermediate Representations (IRs), and integrating formal verification to ensure correctness across all possible tensor shapes and data types.
Summary updated