Democratizing Megakernels for Any Workload with VibeSys

Keisuke Kamahori, Garv Ghai, Xikai Meng, Landy Zhong, Yiqi Mei, Shihang Li, Baris Kasikci

New ResearchInference & ServingAgents

November 30, 2026

TL;DR: VibeSys can write megakernels for any model, hardware, and workload you give it. We designed a divide-and-conquer approach for writing megakernels, where a lead agent builds one working single-launch kernel, splits it into phases, and has several subagents optimize the phases in parallel. Across six diverse workloads (spanning text LLMs, quantization, and multimodal models) and three hardware platforms (NVIDIA B300, AMD MI350X, and AWS Trainium2), VibeSys built megakernels that outperform carefully optimized non-agent baselines by 1.69× on NVIDIA B300, 2.20× on AMD MI350X, and 1.65× on AWS Trainium2 (geometric means). We also show that the approach helps build real systems, using long-context speech recognition as an example, and we discuss where megakernels fall short.


Since we released our VibeServe paper earlier this year, we have been pushing the performance frontier of AI inference with agent-built bespoke systems, written by a fleet of AI agents that VibeSys orchestrates. In previous posts, we built serving systems customized to AMD and Trainium chips, along with the surrounding ecosystem, such as a simulator and a benchmark framework. Those systems are serving frameworks, i.e., Python-level applications that call a number of kernels.

To push performance further, megakernels are a promising direction. A megakernel fuses every layer of a model into one large kernel, removing the overhead at kernel boundaries. That includes launch latency, the pipeline bubble while one kernel drains and the next fills up, and the round trip of activations through global memory. Megakernels shine on small, latency-sensitive workloads, such as decoding at batch size 1, where these costs make up a large share of each step. But they are notoriously hard to write and optimize.

Two lines of work show both the promise and the cost. Hand-written megakernels, such as Hazy Research’s Llama-1B megakernel, beat popular serving engines at low latency, but each is tuned by hand for one model on one GPU. Compiler approaches, such as Mirage Persistent Kernel (MPK), automate the process but cover only the operators and models they ship with.

Both approaches run into the same wall of rigidity. The model architecture, the hardware, the input shapes, and optimization techniques such as quantization all change how a megakernel should be written, so the best performance needs a bespoke megakernel for each configuration. Writing each one by hand does not scale, and a compiler cannot anticipate them all. We made the same argument for serving systems in the VibeServe paper, and it holds one layer down. Bespoke megakernels written by AI agents are the way to cover this long tail.

In this post, we use VibeSys to write megakernels. VibeSys works as a plugin for existing agent harnesses such as Claude Code and Codex, and orchestrates agents in several roles, such as implementers, judges, and profilers. For megakernels, we added a divide-and-conquer mode. The lead agent first gets a correct single-launch kernel working, then profiles it into a phase map that shows how much of the step each phase takes. It then hands the most promising phases to parallel workers, each optimizing its own phase on its own branch.


Experiments

Speedup of the best non-agent baseline and the VibeSys megakernel over PyTorch eager, on six workloads and three accelerators.

Figure 1. Speedup over PyTorch eager (log scale) for six workloads on NVIDIA B300, AMD MI350X, and AWS Trainium2. Each red arrow and number is the megakernel’s speedup over the best non-agent baseline on the same chip.

Setups. Each task is one inference step of a model at batch size 1, and the agent must run the whole step in one kernel launch. For the LLMs, the step decodes one token with 128 tokens already in the KV cache. A kernel counts only if its outputs match the PyTorch reference on fresh random inputs, and the score is the median (p50) step latency over 20 runs. We ran every task on one NVIDIA B300 GPU, one AMD MI350X GPU, and one AWS Trainium2 chip, with Claude Code running Opus 5.5 as the VibeSys lead and a 5-hour budget per task. The six tasks span a range of architectures and techniques.

  • Dense is Qwen3-0.6B, a small dense LLM with 28 layers.
  • MoE is Qwen3-30B-A3B, a mixture-of-experts LLM that routes each token to 8 of its 128 experts.
  • W8 is Gemma 3 4B with its weights quantized to 8 bits (W8A16).
  • W4 is the same Gemma 3 4B with 4-bit weights (W4A16).
  • TTS is Sesame’s CSM-1B text-to-speech model. One step produces one audio frame, which takes a backbone step and then 31 small depth-decoder steps, one per audio codebook.
  • VLA is Physical Intelligence’s π0.5 vision-language-action model for robots. One step produces a chunk of 50 actions through 10 denoising steps of its action expert.

We compare against PyTorch eager and against the best non-agent baseline we could build on each chip. We put real effort into these baselines, with torch.compile, CUDA and HIP graphs, programmatic dependent launch (PDL), the best library kernel for each layer, and hand fixes for graph breaks, so that the comparison is fair.

Detailed configuration of each workload and baseline

Workloads. Each row is one timed step that the megakernel must compute in a single launch, with the model shapes it runs at. To check correctness, a trusted evaluator runs each kernel in three trials on fresh random inputs and compares every output with the PyTorch reference, requiring tokens to match exactly, other outputs to stay within a tolerance band set against an FP32 reference, and inputs to be left unmodified.

Workload Model Step Geometry
Dense Qwen3-0.6B One decode step (28 layers, LM head, and argmax) batch 1, context 128; hidden 1024, 16 Q / 8 KV heads, head dim 128, FFN 3072, vocab 152k
MoE Qwen3-30B-A3B One decode step (48 layers with routed experts, LM head, and argmax) batch 1, context 128; hidden 2048, 32 Q / 4 KV heads, 128 experts, top-8, expert FFN 768, vocab 152k
W8 Gemma 3 4B (W8A16) One decode step (34 layers with INT8 weight-only linears, LM head, and argmax) batch 1, context 128; hidden 2560, 8 Q / 4 KV heads, head dim 256, FFN 10240, vocab 262k, local window 1024
W4 Gemma 3 4B (W4A16) Same as W8, with INT4 weights same as W8
TTS CSM-1B One audio frame, made of one backbone decode step and then 31 sequential depth-decoder steps, one per codebook batch 1, context 128; backbone 16 layers, hidden 2048; depth decoder 4 layers, hidden 1024; 32 codebooks, audio vocab 2051
VLA π0.5 One action chunk of 10 flow-matching (Euler) steps of the Gemma-300M action expert over the cached vision-language prefix batch 1; 3 images (256 tokens each) + 200 text tokens in the prefix (computed outside the timed step); 18 expert layers, hidden 1024; horizon 50, action dim 32

What the baselines searched over.

  • On B300, we tried a few obvious kernel options for each kind of layer, picked the fastest per layer, and ran the result in every reasonable execution mode. We also tried Mirage MPK, but it builds only a subset of our workloads, and on each of them it was slower than the torch.compile baseline.
    • Attention used PyTorch SDPA (default dispatch and each of its backends), FlashInfer decode and prefill, FlashAttention-4 (CuTe DSL), or flex_attention.
    • Matrix-vector products used cuBLAS, Triton kernels generated by Inductor, or FlashInfer mm_bf16 (CuTe DSL), with QKV and gate/up weights optionally fused.
    • Quantized layers used a Triton W8A16 or W4 GEMV, torch._weight_int8pack_mm, torch._weight_int4pack_mm on prepacked weights, or Inductor’s fused dequantize-and-matmul.
    • MoE experts used a gather loop, torch._grouped_mm, or an in-place Triton expert GEMV.
    • Norms and RoPE ran eagerly, through FlashInfer’s fused RMSNorm, or fused by Inductor.
    • Execution modes were eager, a manually captured CUDA graph, every torch.compile mode (default, reduce-overhead, max-autotune, and max-autotune-no-cudagraphs, also inside a CUDA graph), each with PDL on and off.
  • On MI350X, we ported the B300 search to the ROCm libraries, 127 variants in all.
    • Attention used PyTorch SDPA (default dispatch or memory-efficient), AITER’s CK flash attention, or the reference’s own masked attention fused by Inductor.
    • Matrix-vector products used hipBLASLt or Triton kernels generated by Inductor, with QKV and gate/up weights optionally fused.
    • Quantized layers used dequantization followed by hipBLASLt, a Triton W8A16 or W4 GEMV, or torch._weight_int4pack_mm.
    • MoE experts used stock PyTorch ops, an in-place Triton expert GEMV, or AITER’s fused_moe (CK, two stages).
    • Norms and activations ran eagerly, through AITER’s fused RMSNorm and SiLU-and-mul kernels, or fused by Inductor, optionally with its coordinate-descent tuning.
    • Execution modes were eager, a manually captured HIP graph, and every torch.compile mode, also inside a HIP graph. The finalists of each task were then graded again one at a time, next to the agent’s megakernel, on the same GPU.
  • On Trainium2, we compiled with torch.compile(backend="neuron"), the only backend that runs on the device (Inductor’s own modes do not), and swept five neuronx-cc configurations, from -O1 to -O3, plus model-type and weight-layout options.
    • Attention used the reference’s own attention or SDPA, which lowers to the NKI flash-attention kernel.
    • Projections used neuronx-cc’s own code or the fused QKV, MLP, and output-projection kernels of nkilib. At these batch-1 shapes, nkilib was slower everywhere, so no winner uses it.
    • MoE experts used the reference’s host-side gather loop, nkilib’s moe_tkg, or all local experts computed on the device with unrouted ones masked out.
    • Graph breaks were fixed by hand where a host sync split the compiled graph: MoE routing and CSM’s depth decoder, which torch.compile recompiled for every codebook index until it hit its recompile limit and fell back to eager.

Best non-agent baselines. On both GPUs, the fastest mode for every task was torch.compile with max-autotune-no-cudagraphs, run inside a manually captured CUDA or HIP graph. It beat reduce-overhead, the other compile modes, and plain graphs. The winners differ in the kernels used for each layer and, on B300, in programmatic dependent launch (PDL). On Trainium2, the compiler level mattered little (within about 5% for most tasks), and the big wins came from removing graph breaks.

Workload B300 (PDL; attention; matmuls and other ops) MI350X (attention; matmuls and other ops) Trainium2 (neuronx-cc config and changes)
Dense PDL on; FlashInfer decode; FlashInfer mm_bf16 (CuTe DSL) GEMVs with QKV and gate/up weights fused, norms fused by Inductor SDPA; hipBLASLt GEMVs; Inductor with coordinate-descent tuning -O2 with weight-layout optimization
MoE PDL off; SDPA; stock PyTorch ops, with Inductor turning the expert gather into an in-place gather GEMV SDPA; hipBLASLt GEMVs and stock expert ops; Inductor with coordinate-descent tuning -O2; all local experts on the device with routing as a mask, which removes a host sync per layer (6.2× over plain compile)
W8 PDL on; FlashInfer decode; Triton W8A16 GEMV SDPA; dequantization and hipBLASLt, fused by Inductor -O2 with weight-layout optimization
W4 PDL on; FlashInfer decode; torch._weight_int4pack_mm (tinygemm) on prepacked INT4 weights SDPA; Triton W4 GEMV -O2 --model-type=generic
TTS PDL on; FlashInfer single decode; QKV and gate/up fused, kernels chosen by Inductor, norms, RoPE, and activations fused by Inductor SDPA; QKV and gate/up fused, kernels chosen by Inductor, elementwise ops fused by Inductor -O2; the depth decoder indexed with a tensor, so it compiles once (12.4× over plain compile)
VLA PDL on; FlashAttention-4 (CuTe DSL, variable length); QKV and gate/up fused, adaRMS modulations precomputed (hoisted), elementwise ops fused by Inductor Reference attention fused by Inductor; QKV and gate/up fused, adaRMS modulations hoisted -O2 --model-type=generic

Every baseline variant, its code, and its measurements are in the B300, MI350X, and Trainium2 baselines.

  • The launch budget is one GPU kernel launch per step, counted by the profiler. On Trainium2, it is one NKI kernel call per NeuronCore.
  • On Trainium2, dense, MoE, W8, and W4 run with TP4 over the four logical NeuronCores of a trn2.3xlarge. TTS and VLA run on one NeuronCore.
  • B300 used CUDA 13.1 and PyTorch 2.13. MI350X used ROCm 7.1.1 and PyTorch 2.10. Trainium2 used torch-neuronx 2.14.3 and the Neuron SDK.
  • The final megakernels for the six workloads and Waypoint on every chip, together with the baselines, are in vibesys-megakernels.

VibeSys megakernels beat every baseline by 1.69× on B300, 2.20× on MI350X, and 1.65× on Trainium2

VibeSys megakernels beat the best non-agent baseline on every workload and chip (Figure 1). On NVIDIA B300, they are 1.16–2.15× faster (1.69× geomean), with the biggest gains on the quantized Gemma 3 cases. On AMD MI350X, they are 1.03–4.20× faster (2.20× geomean), led by the dense case. On AWS Trainium2, they are 1.34–2.42× faster (1.65× geomean), even after we fixed by hand the graph breaks that had left torch.compile far behind on MoE and CSM.


How it works

VibeSys as a plugin

VibeSys is a set of plugins for the coding agent you already use, such as Claude Code or Codex. A core plugin holds the optimization loop, and a domain plugin for each backend (CUDA, ROCm, or Trainium) adds hardware-specific skills such as profiling and kernel-writing guides. One command turns the agent’s session into a lead that never writes code itself. The lead spawns subagents as planners, workers, profilers, judges, and combiners, each a separate agent with a fresh context, and promotes a candidate only after a judge passes it and it measures faster than the current best.

Divide-and-conquer for megakernels

The phases of a megakernel share one launch, so a phase can get faster at the expense of the others. VibeSys therefore divides the kernel at its phase boundaries but accepts a phase’s change only by its effect on the whole step. Figure 2 shows the resulting loop, loosely following the dense Qwen3-0.6B run on B300.

Timeline of the divide-and-conquer mode. A worker writes the first kernel, a profiler writes a phase map, workers optimize attention, the matrix-vector phases, and a cross-phase barrier in parallel, a combiner glues them, a judge reviews, and the lead promotes the result, then the cycle repeats.

Figure 2. The divide-and-conquer loop, loosely following the dense Qwen3-0.6B run on B300. The top bar is the execution breakdown of one decode step inside the megakernel. The lead hands each slow part to its own worker. With each promotion the breakdown shrinks, and the straight boundaries between phases become stepped, as one half of the SMs starts the next phase while the other is still finishing the previous one.

We kept this design deliberately simple, because fancier scaffolding did not help much. In earlier experiments, we gave the agents specialized profiling tools and a harness for microbenchmarks, but neither improved the final kernels, since the agents could rebuild such tools from scratch whenever they needed them. What mattered was parallelizing the work and making sure every role, from implementing to judging and profiling, runs in a fresh context, which keeps progress steady and the results trustworthy.

What the agents actually did

Each 5-hour run on B300 started 40 to 104 subagents. Figure 3 shows how they climbed. In four of the six runs, the first working kernel was slower than the best non-agent baseline, and the agents passed it within about two hours.

Speedup of the best megakernel so far over the best non-agent baseline, over each 5-hour run on B300, for six workloads, with every other measured candidate.

Figure 3. Hill climbing on NVIDIA B300, as speedup over the best non-agent baseline for the same workload (the dashed line at 1×). Each blue step is a promotion to the result branch, and gray dots are other measured candidates that did not win.


To megakernel, or not to megakernel?

Whether megakernels are the right approach has recently become a hot topic. Together AI, which helped release some of the first megakernels, recently wrote that they walked away from megakernels for two reasons: removing kernel launches and overlapping operations does not actually require a megakernel (CUDA graphs with PDL reached 98% of their hand-written Llama-1B megakernel), and megakernels are too rigid for varying hardware and model architectures.

With agents like VibeSys, we can remove the rigidity obstacle. A bespoke megakernel no longer takes months of work for one model on one GPU. In the evaluation above, VibeSys wrote megakernels for various hardware platforms and model architectures spanning dense and MoE LLMs, quantized weights, speech, and robotics. These megakernels beat carefully tuned baselines that already use CUDA graphs, PDL, and the best library kernels, showing the potential of having auto-generated bespoke megakernels tailored to your workload.

However, we do not think that megakernels are a silver bullet. We hit a few failure modes in our exploration. One is Waypoint-1.5, a world model that generates video frames (Figure 4a). Each frame runs four denoising passes over 512 tokens, so the step is compute-bound, dominated by large matrix multiplies and attention, and launch overhead is a small share of it once the baseline runs inside a CUDA graph. A megakernel has little to remove here, and to win it would have to match fully tuned libraries such as cuBLAS and FlashAttention-4 inside a single launch. The agents’ megakernels came close but never pulled clearly ahead, at 0.92×, 1.02×, and 0.77× of the best baseline on B300, MI350X, and Trainium2.

Another is a much bigger model. We asked VibeSys to turn batch-1 inference of DeepSeek-V4.1-Flash, served by SGLang with tensor parallelism on four GB300s, into one launch per GPU (Figure 4b). It wrote a correct megakernel from scratch, but reached only a quarter of the throughput of sgl-nvidia-mega, the hand-written megakernel for this model. Spanning multiple GPUs has been hard, since the kernel has to run the all-reduces across the four GPUs itself, and getting a first correct kernel, including fixing a deadlock in that cross-GPU synchronization, took about three quarters of the time budget, which left little room for more optimization. We are working on further optimizing these kinds of workloads.

Two panels of speedup over the best non-agent baseline. (a) The Waypoint-1.5 frame step on NVIDIA B300, AMD MI350X, and AWS Trainium2. (b) Batch-1 inference of DeepSeek-V4.1-Flash on four NVIDIA GB300s, where stock SGLang is also shown.

Figure 4. Two failure modes. (a) For the Waypoint-1.5-1B frame step, the megakernel is 0.92×, 1.02×, and 0.77× as fast as the best non-agent baseline on B300, MI350X, and Trainium2. (b) For batch-1 inference of DeepSeek-V4.1-Flash on 4× GB300 (tokens/s per user), it reaches 0.25× of sgl-nvidia-mega, the best non-agent baseline, and 0.34× of stock SGLang.

Still, we believe that many important workloads benefit from megakernels, especially small, latency-bound steps like the ones in Figure 1. Because VibeSys writes a bespoke kernel for each configuration automatically, exploring a megakernel approach is much cheaper, and that removes the rigidity that has kept megakernels from being democratized.


Vibe-megakernels in the wild

To see how this works inside a real system, we applied the same approach to Murmur, our system for long-context speech recognition with models such as VibeVoice-ASR, an 8.5B speech LLM. Before the LLM decodes, VibeVoice’s speech encoders turn the raw audio into features, and pyannote’s diarization pipeline (a segmentation network, a speaker-embedding model, and clustering) labels who speaks when. To speed them up for local inference on a MacBook (Apple M4 Max), we asked VibeSys to write Metal megakernels for the speech encoder and the pyannote diarization components. These are good examples of long-tail workloads that would benefit from megakernels but where engineering effort has been the bottleneck, since they are built from convolutions, LSTMs, and clustering rather than the transformer layers of mainstream LLMs, and they run on Apple silicon through MLX and Metal.

For the speech encoder, the agents fused each residual block into a single Metal dispatch, and the encoder’s time in the transcription pipeline dropped from 16.1 s to 10.1 s (1.59× faster). For diarization, they wrote Winograd convolution kernels, batched the LSTM across audio windows, and sped up clustering, making diarization 2.21× faster (Figure 5). Both kept the outputs exactly the same, and we are upstreaming both changes to Murmur (pull requests for the speech encoder and for diarization).

Latency of the VibeVoice speech encoders and of pyannote speaker diarization on one AMI meeting, before and after VibeSys, on an Apple M4 Max.

Figure 5. Latency of two Murmur components on the same input, AMI meeting ES2004a (headset audio), on an Apple M4 Max, in the original Murmur and after a two-hour VibeSys campaign each (1.59× and 2.21× faster).


Try it yourself

VibeSys is open source at github.com/uw-syfi/vibesys. Install it as a plugin for your coding agent, together with the kernel plugin for your hardware.

claude plugin marketplace add uw-syfi/vibesys
claude plugin install vibesys@vibesys
claude plugin install vibesys-kernels-cuda@vibesys   # or vibesys-kernels-rocm, vibesys-kernels-trainium

Then describe your workload as a task, with a PyTorch reference, a correctness check, and a benchmark (/vibesys:create-task helps you set one up), and start the megakernel mode.

/vibesys:optimize <task> --megakernel-divide-and-conquer --budget time 300m --fanout 3

The megakernels and baselines from this post are available at vibesys-playground/vibesys-megakernels.


Acknowledgements

We thank AMD, AWS, CoralBricks, Mirendil, and Verda for providing the compute and token credits that made these experiments possible.


Citation

If you find this work useful, please cite it.

@misc{kamahori2026megakernels,
  title        = {Democratizing Megakernels for Any Workload with VibeSys},
  author       = {Keisuke Kamahori and Garv Ghai and Xikai Meng and Landy Zhong and Yiqi Mei and Shihang Li and Baris Kasikci},
  year         = {2026},
  month        = nov,
  howpublished = {\url{https://syfi.cs.washington.edu/blog/2026-11-30-megakernels/}}
}