Author: Dev Desai
KernelPilot is an autonomous CUDA kernel optimization system built to answer a hard systems question:
Can an LLM-driven search stack take a real reference kernel, reason about GPU bottlenecks, synthesize better CUDA, validate correctness, and beat a production baseline without human-written kernel-specific optimization plans?
This repository is my answer to that question.
It combines multi-agent planning, profiler-guided refinement, retrieval-augmented CUDA knowledge, benchmark discipline, and correctness gating into a single end-to-end optimization pipeline for Blackwell-class NVFP4 workloads.
KernelPilot is not a prompt wrapper around nvcc. It is a research-style optimization engine with five tightly integrated pieces:
- a planner that forms optimization hypotheses from source code, runtime signals, and retrieved CUDA patterns
- a coder that turns those hypotheses into compilable CUDA kernels
- a search loop that explores, ranks, prunes, and refines branches
- a measurement layer that checks correctness and times candidates against a strong baseline
- a feedback loop that uses profiler evidence instead of generic “optimize this kernel” advice
The target workloads are:
- fused
add_rmsnormwith NVFP4 quantization nvfp4_quantizeblock quantization- fused
silu_mulwith NVFP4 quantization
The reported repository results show up to 1.845x speedup over the production baseline on NVIDIA B200, with an approximate geometric mean of 1.61x across the benchmarked kernels.
My working hypothesis for this project is:
An autonomous optimization loop can outperform strong baseline CUDA kernels if it is given four things at once:
- real runtime evidence instead of static code-only reasoning
- search diversity instead of single-shot generation
- correctness and anti-hack filters as hard gates
- external retrieval over high-quality CUDA sources so the model is not reasoning in a vacuum
This repository operationalizes that hypothesis. Every major subsystem exists to make one part of that claim testable.
Autonomous kernel optimization is difficult for reasons that compound:
- CUDA kernels are tightly coupled to hardware behavior, occupancy, register pressure, memory traffic, and launch geometry
- small source changes can produce large performance cliffs
- generated kernels often compile but are numerically wrong
- benchmark numbers are easy to inflate with unfair baselines or mismatched timing paths
- LLMs are prone to generic advice, hallucinated intrinsics, and unstable code revisions if the loop is under-constrained
KernelPilot addresses those issues with disciplined structure:
- official-baseline-first evaluation
- branch-based search instead of one-shot sampling
- seeded correctness checks
- runtime and profiler feedback
- RAG over real CUDA sources
- explicit retirement of stagnant candidate families
flowchart TD
A["Reference Kernel + Problem Shape"] --> B["run.py"]
B --> C["Baseline Measurement"]
C --> D["RLMEnvironment"]
D --> E["BeamSearch"]
E --> F["Planner"]
F --> G["RAG Retriever + CUDA Docs"]
F --> H["Coder"]
H --> I["nvcc Compile"]
I --> J["Benchmark"]
J --> K["Correctness Check"]
K --> L["Hybrid Profiler"]
L --> M["Reflector / Feedback"]
M --> E
E --> N["Best Verified Candidate"]
N --> O["Submission Formatter"]
flowchart LR
A["Planner Hypothesis"] --> B["Candidate CUDA"]
B --> C["Compile"]
C --> D["Time vs Baseline"]
D --> E["Correctness Gate"]
E --> F["Profiler Signals"]
F --> G["Refinement Feedback"]
G --> H["Next Branch / Retry / Retire"]
H --> A
The architecture is deliberately modular:
rlm/owns reasoning, prompts, environment state, planning, and feedback synthesissearch/owns branching, diversity, ranking, and search progressionprofiler/owns bottleneck evidence and hybrid performance analysiseval/owns fairness, timing, correctness, and submission formattingkernels/provides the baseline kernels and shared CUDA support headers
That separation matters because it makes the system inspectable. A hiring manager or reviewer can look at each layer independently and understand how decisions are made.
The repository’s benchmark history reports the following results on NVIDIA B200 against FlashInfer production baselines:
| Kernel | Shape | Speedup |
|---|---|---|
add_rmsnorm_fp4quant |
128 x 2048 |
1.691x |
add_rmsnorm_fp4quant |
128 x 4096 |
1.650x |
add_rmsnorm_fp4quant |
128 x 8192 |
1.420x |
nvfp4_quantize |
128 x 14336 |
1.845x |
silu_mul_fp4quant |
8 x 256 x 7168 |
1.450x |
Approximate geometric mean: 1.61x
These numbers matter for two reasons:
- the comparison target is not a toy baseline; it is a production-oriented FlashInfer reference path
- the system is solving multiple kernel families rather than one hand-tuned benchmark
KernelPilot is intended to discover patterns such as:
- vectorized loads and stores
- hardware FP4 intrinsic usage on Blackwell
- better thread utilization when work mapping leaves idle lanes
- register caching to avoid repeated global reads
- launch-bound and occupancy-aware refinements
- dataflow simplifications that reduce unnecessary passes
The point is not that every candidate will discover every pattern. The point is that the system is structured so those patterns can emerge from runtime-grounded search instead of being hardcoded into a kernel-specific script.
Performance claims are only interesting if the measurement path is credible.
KernelPilot takes that seriously:
- it attempts to use an official FlashInfer baseline first
- it marks fallback reference baselines as unofficial
- it reuses a consistent timing path between baseline and candidate
- it validates correctness before accepting a kernel as a win
- it records profiler and compiler evidence for follow-up reasoning
Important configured evaluation thresholds:
- absolute tolerance:
1e-2 - relative tolerance:
1e-3 - correctness seeds:
42,123,999 - benchmark warmup iterations:
500 - benchmark iterations:
100
This is one of the strongest parts of the project from a systems-engineering perspective: the code does not treat “compiled once” as success.
At the top level, run.py defines six task variants:
add_rmsnorm_fp4quant_b128xh2048add_rmsnorm_fp4quant_b128xh4096add_rmsnorm_fp4quant_b128xh8192nvfp4_quantize_m128xk14336silu_mul_fp4quant_b8xm256xk7168silu_mul_fp4quant_b8xm256xk14336
For each task, the runtime flow is:
- load configuration and environment variables
- resolve the kernel source and shape
- measure a baseline
- create an
RLMEnvironment - run
BeamSearch - validate the best candidate with correctness checks
- benchmark the final candidate
- format and save the output submission
This is the reasoning core of the project.
- rlm/engine.py: orchestration, decomposition, prompt building, multi-turn refinement, and tool routing
- rlm/planner.py: branch planning and direction setting
- rlm/planner_spec.py: structured planning schema and instruction shaping
- rlm/coder.py: code-generation prompt construction
- rlm/fixer.py: repair prompts for failed candidates
- rlm/reflector.py: converts measured outcomes into refinement guidance
- rlm/feedback.py: runtime-grounded targeted feedback
- rlm/environment.py: per-run state, counters, metrics, branch memory, and config handling
This is the search-control layer.
- search/beam_search.py: branch exploration, candidate timing, pruning, early stopping, and search progression
- search/diversity_selector.py: preserves strategy diversity across candidates
- search/combiner.py: combines strong candidates into merged variants
- search/strategy_bank.py: strategy catalog for CUDA optimization ideas
This is the evidence layer.
- profiler/kernel_profiler.py: compile-and-profile orchestration
- profiler/hybrid_profiler.py: hybrid bottleneck analysis
- profiler/roofline.py: roofline model support for B200
- profiler/metrics.py: metric schema
This is the fairness and safety layer.
- eval/benchmark.py: timing harness and summary metrics
- eval/correctness.py: seeded correctness validation
- eval/flashinfer_ref.py: official baseline path
- eval/runtime_checks.py: runtime validation
- eval/hack_detector.py: anti-cheat / anti-trivial-solution filtering
- eval/waferbench_format.py: output formatting
These files provide the actual optimization targets and shared CUDA utilities:
- kernels/reference/add_rmsnorm.cu
- kernels/reference/nvfp4_quantize.cu
- kernels/reference/silu_mul.cu
- kernels/common/nvfp4_utils.cuh
- kernels/common/b200_intrinsics.cuh
KernelPilot uses Pinecone-backed retrieval so the planner is not relying only on latent model memory.
Current configuration in config/search_config.yaml:
- provider:
pinecone - index:
cuda-kernels-v2 - top-k:
4 - rerank pool:
25 - text field:
source_code - source cap:
1
Supporting files:
The intent is simple: when the model proposes a change, it should do so with relevant examples and hardware-aware context, not vague CUDA folklore.
The active default settings are intentionally conservative:
- beam width:
1 - refinement rounds:
0 - max concurrent API calls:
4 - combine top-k:
2 - diversity mode:
family_diverse - hybrid profiler enabled
- target minimum speedup:
1.5x
That means the codebase currently contains more search sophistication than the default runtime path exercises.
This is actually a useful signal for a reviewer:
- the architecture is capable of deeper search
- the current defaults are set for controlled, cost-aware iteration
- the repository is structured so scaling experimentation is a config problem, not a rewrite
From pyproject.toml, the project expects Python >=3.10 and depends on:
anthropicpyyamlnumpypineconesentence-transformerstorchflashinfer
The lighter requirements.txt captures the non-heavy subset.
Install with:
pip install -r requirements.txt
pip install torch flashinfer
pip install -e .Environment variables:
ANTHROPIC_API_KEY=...
PINECONE_API_KEY=...
PINECONE_INDEX_NAME=cuda-kernels-v2
PINECONE_INDEX_HOST=...Additional supported environment overrides:
PINECONE_NAMESPACEPINECONE_EMBED_PROVIDERPINECONE_EMBED_MODELPINECONE_RERANK_MODEL
Optimize all kernels:
python run.pyOptimize a single kernel:
python run.py --kernel add_rmsnorm_fp4quant_b128xh2048Dry run without model calls:
python run.py --dry-runOverride search depth:
python run.py --beam-width 2 --rounds 2Allow unofficial local fallback baseline:
python run.py --allow-reference-baselineThe runtime emits submission artifacts and logs, including metadata such as:
- selected strategy
- bottleneck classification
- correctness pass/fail state
- max absolute error
- official-baseline flag
- search rounds completed
- cumulative API cost
- elapsed runtime
- rejection and attempt counters
This makes the output useful for both benchmarking and postmortem analysis.
If I were evaluating this project as a hiring manager, the impressive parts are not just the speedups.
The stronger signal is the engineering judgment behind the system:
- it treats measurement rigor as part of the product
- it separates reasoning, search, profiling, and evaluation concerns cleanly
- it builds an autonomous loop that still remains inspectable
- it uses retrieval and profiler evidence to make LLM output less hand-wavy
- it targets difficult, hardware-sensitive optimization problems rather than easy prompt demos
This repository is meant to show taste in systems design as much as raw implementation ability.
KernelPilot is ambitious, but it is not pretending to be effortless.
- it requires meaningful NVIDIA GPU access
- many optimizations are Blackwell and NVFP4 specific
- reproducibility depends on driver, toolchain, FlashInfer, and hardware setup
- external services are part of the current architecture
- benchmark claims should still be reproduced in the target environment
If you are reviewing the repository quickly, read these first:
- run.py
- config/search_config.yaml
- search/beam_search.py
- rlm/engine.py
- profiler/hybrid_profiler.py
- eval/correctness.py
That path gives the clearest story:
- what problem the system solves
- how it searches
- how it measures success
- how it avoids fooling itself