Training a Small Specialized Model for Kernel Optimization: Practices and Lessons Learned
Our team, which primarily builds training frameworks (especially for reinforcement learning), spent several months training a small, domain-specific model for GPU kernel optimization. This article walks through why we did it, how we defined the task and evaluation pipeline, how we built and filtered training data, our three-stage training strategy (continued pretraining, supervised fine-tuning, and rule-based RL), and the key findings: a well-evaluated vertical domain plus filtered data plus rule-based RL can produce results competitive with much larger general-purpose models within that narrow scope. We also share how this experience fed back into our framework engineering priorities.
Training a Small Specialized Model for Kernel Optimization: Practices and Lessons Learned
1. Background and Motivation
We are fundamentally a training framework team—focused especially on reinforcement learning training frameworks. Our core job should be the framework itself, not training models. So why did we spend several months training a small model for kernel operator optimization? Two reasons:
First, a framework team must gain insight from real training runs. Framework developers who never run end-to-end training jobs tend to design based on assumptions: interfaces that don’t match real usage patterns, performance optimizations aimed at the wrong bottlenecks, and stability assumptions that don’t survive long-horizon training. Only by personally running the full “data—training—evaluation—iteration” loop can you build a good framework.
Second, we wanted to test a hypothesis: in a sufficiently narrow vertical domain, how close can a small-to-mid-sized model get to the performance of frontier general-purpose LLMs? Kernel optimization is an ideal test bed: the problem is well-defined (given an operator’s semantics, generate a high-performance implementation), evaluation is objective (a three-layer check of compilation, unit tests, and performance benchmarks), and general-purpose LLMs have clear weaknesses on this task—unstable compile rates for generated code, insensitivity to hardware microarchitecture characteristics, and a lack of global planning for memory layout and parallelization strategies.
2. Task Definition and Evaluation
We formalized the task as follows: the input is a semantic description of an operator (e.g., a Python reference implementation or pseudocode) plus hardware constraints for the target device (thread block specifications, shared memory capacity, register limits); the output is high-performance kernel source code. Evaluation uses a three-stage funnel:
- Compilability: the code must compile cleanly with no explicit undefined behavior.
- Correctness: numerical comparison against a reference implementation on multiple randomly generated inputs (floating-point error within a specified tolerance is allowed).
- Performance score: speedup is computed relative to the reference or baseline implementation, expressed as a normalized performance score.
The value of this evaluation system is that it is fully automated with no subjective human grading—naturally suited to large-scale validation, and a solid foundation for later RL with rule-based rewards.
3. Data Construction: More Important Than the Model
In practice, data quality affected the final results far more than model size or training tricks. We used three data sources:
- Real repository code: operator implementations with complete tests extracted from open-source code, preserving the “description—implementation—test” triple.
- Synthetic data: using general-purpose LLMs to semantically rewrite existing operators and perturb parameters, expanding input distribution diversity.
- Recycled failure samples: code generated by the model that failed evaluation, corrected and used as error-correction training data. This significantly improved the model’s ability to “know where it went wrong.”
One key lesson: synthetic data must be filtered by a real evaluator, not just by LLM self-assessment. Early on, we trusted the generator’s self-scoring, which introduced a large number of “plausible but wrong” samples—and after training, the model’s compile rate actually dropped.
4. Training Strategy and Phases
Given a limited budget, we used a three-stage progressive approach:
Stage 1: General coding foundation. Continued pretraining on public code corpora, with emphasis on CUDA / Triton / C++ distributions, to ensure baseline code generation capability.
Stage 2: In-domain supervised fine-tuning. SFT on high-quality “description—implementation” pairs filtered by the evaluator. In this stage we ran curriculum learning experiments: starting with simple operators (elementwise operations, reductions), then gradually introducing higher-complexity tasks (matrix multiplication, fused operators, template-style kernels). Results showed curriculum learning gave a stable, modest gain in the final performance score, but had little effect on compile rate.
Stage 3: Rule-based reinforcement learning. The three-layer evaluation served as the reward signal: strong negative reward for compilation failure, neutral-to-negative for correct-but-slow code, and positive reward scaled by speedup for correct-and-fast code. This stage delivered the largest gains—the model’s performance score improved substantially at the same correctness level, especially learning strategies that SFT struggled to teach, such as actively tuning tile sizes and reducing redundant shared memory reads and writes.
5. Key Findings
1. A vertical small model can approach—but not fully surpass—general-purpose LLMs. On in-domain standard evaluations, our small model performed close to and in some cases better than much larger general models (particularly on compile success and style consistency). But when the task description drifted slightly (e.g., changed hardware constraints, new operator categories), the generalization gap became apparent. This matched expectations: a vertical model learns efficient in-distribution strategies, not general problem-solving ability.
2. Evaluability is a prerequisite for success in vertical models. We could do RL and data filtering only because the evaluator was automatic and reliable. In a domain with fuzzy evaluation (e.g., open-ended writing), the same methodology would lose much of its effectiveness.
3. Pitfalls in reward design. Pure performance rewards induced the model to “overfit the evaluation”—for example, hardcoding optimizations for the specific shapes in test cases, which failed on different shapes. Our eventual solution was to mix the average performance across multiple random shapes into the reward, and to detect and penalize obvious constant-folding-style shortcuts.
6. Feedback into Our Framework Work
This training effort also gave us new insights about the framework itself:
- The bottleneck of RL training is often not the algorithm, but sampling and evaluation throughput. The evaluator must actually compile and run code, making it inherently slow. The framework must support highly concurrent, asynchronous evaluation and reward feedback, or GPUs spend much of their time idle.
- Fault tolerance matters for long-horizon training. Over thousands of RL steps, occasional timeouts and OOMs on evaluation nodes are the norm. The framework must auto-retry and distinguish “model code timeout” from “infrastructure failure,” or the reward signal gets polluted by noise.
- Reproducibility design must come first. Fixing random seeds and versioning data and evaluation configurations should be done early in experimentation—retrofitting them later is extremely costly.
7. Conclusion
With a limited budget, a vertical-domain small model is a pragmatic path: a clear evaluation system + high-quality filtered data + rule-based reinforcement learning—if these three things are done well, you can achieve solid results in a narrow domain. But it is not a replacement for general-purpose models; it is a cost-effective choice for scenarios where the evaluator can be automated and the task boundary is clear. For our framework team, the biggest value of this experience was walking the entire pipeline from data to RL ourselves and stepping on the pitfalls firsthand—only then do you know where the framework should focus. Although we didn’t push the training budget to its limit, these insights are already enough to inform the next phase of our work.
