Megakernels Explained: Fusing GPU Passes for Frame-Locked AR Overlays

Megakernels fuse a whole GPU pass into one persistent kernel. How they differ from fusion and CUDA graphs, what they cost, and how to profile the payoff.

Megakernels Explained: Fusing GPU Passes for Frame-Locked AR Overlays
Written by TechnoLynx Published on 11 Jul 2026

A megakernel is a single persistent GPU kernel that runs a whole pass, such as an LLM forward step, instead of dozens or hundreds of separate kernel launches. Each streaming multiprocessor (SM) gets one resident thread block. That block works through a task list, and data dependencies are tracked by counters in global memory instead of being implied by kernel boundaries. The problem megakernels solve is the dead time between kernels: launch overhead, per-kernel setup and teardown, and the full-grid barrier that makes every SM wait for the slowest one before the next kernel can start. Whether fusing everything pays off depends on whether the overhead it removes exceeds its fixed costs. You find that out on a profiler timeline, not by assuming it.

Take a frame-locked overlay at broadcast cadence, the kind of real-time AR overlay work used in sports broadcast. There, a chain of GPU passes (pose transform, warp, composite, colour) has to lock to camera and player pose inside one frame. A study of streaming text-to-speech inference, “One Megakernel is Not Enough”, concludes that fusion is a question of where to cut, not whether to fuse, and its best design sometimes used no megakernel.

How a Megakernel Differs from Kernel Fusion and a Chain of Separate Kernel Launches

The conventional approach splits a model forward pass into around a hundred kernels, each covering a few operations such as RMS norm, attention or an MLP layer. GPU kernels launch in strict order. No thread block of the next kernel can start until every block of the previous one has finished. Hazy Research gives a concrete case: a 512-block down projection on a 148-SM B200 leaves 80 SMs empty at the end of the kernel.

Plain kernel fusion merges a few adjacent operators into one kernel, so their intermediate results never round-trip through memory. Within one Chiplet-task of its persistent kernel, the Fleet paper measures the effect of fusing one operator into another. Fusing the SiLU activation into the gate-and-up GEMM removed an intermediate buffer and raised the L2 hit rate from 9.4% to 17.4% at batch size 1. In plain fusion, the kernel boundaries between fused groups remain.

A megakernel removes the boundaries altogether. The unit of scheduling shrinks from a whole operation to one tile of one operation, and the unit of synchronisation shrinks from the whole GPU to the specific producers a task depends on. That opens up overlaps a kernel boundary forbids. Hazy Research’s interpreter starts loading the weights for the next instruction while the previous one is still storing its results. Cohere’s decode engine streams weight tiles into shared memory before the activation they will be multiplied with even exists.

The Bubbles Megakernels Remove in LLM Decode

Batch-1 Llama-1B decode on one GPU is entirely memory-bound, so the focus is keeping weight loads flowing without stalls. A standard serving system runs almost 250 kernel launches per decode token across a 36-layer model. Between launches the L2 cache contents are not kept, so each kernel reloads its weights and activations from HBM. A survey of megakernel work summarises Ada-MK’s profile of TensorRT-LLM serving Qwen2.5-1.5B, in which kernel launch overhead alone took 14.6% of end-to-end inference time.

Those gaps are the “bubbles”. Fusing the whole Llama-1B forward pass into one kernel let Hazy Research use 78% of H100 memory bandwidth and run almost 2.5x faster than vLLM and over 1.5x faster than SGLang. Their H100 megakernel ran the forward pass in under one millisecond, and the B200 version in under 680 microseconds. Cohere reports 292 tokens per second at batch size 1, 1.58x faster than vLLM. In full serving, where prefill runs as ordinary PyTorch kernels and routing and batch size vary, Cohere’s gain shrinks to 1.25x to 1.41x in average decode throughput.

CUDA Graphs and Programmatic Dependent Launch Narrow the Gap Without Closing It

CUDA graphs capture many operations so the CPU can launch them with a single call. In NVIDIA’s own test on a Tesla V100, a 2.9 µs kernel cost 9.6 µs per launch with per-kernel synchronisation. Overlapping the launches brought that down to 3.8 µs, and a captured CUDA graph brought it to 3.4 µs. That still leaves launch overhead on top of the kernel’s own 2.9 µs. Fleet’s authors point out what graphs leave untouched: dependent nodes still run one after another with gaps between kernels, and kernel boundaries still flush L2. Graphs also only pay off when the same graph is reused often enough to cover the cost of instantiating it.

With programmatic dependent launch (PDL), a secondary kernel can start the part of its work that does not depend on the primary kernel, then wait in cudaGridDependencySynchronize() until the primary signals that its results are ready. A megakernel allows that kind of fine-grained overlap across operator boundaries throughout the pass.

Execution style Launches per pass Intermediates between stages Barrier between stages Main cost
Chain of separate kernels One per operator Written to and reloaded from memory Full grid Launch gaps and straggler tails
Operator fusion Fewer, one per fused group Kept on-chip inside a group Full grid between groups Boundaries remain between groups
CUDA graph One CPU call, kernels still separate Through memory, L2 flushed at boundaries Full grid Residual per-kernel launch overhead
Programmatic dependent launch Separate kernels Through memory Partial: independent prologue overlaps Overlap between a primary kernel and its dependent kernel
Megakernel One per step Pipelined across tasks Per-dependency counters Register pressure, shared launch shape, in-kernel synchronisation

How Persistent Kernels Relate to Megakernels, and What Mirage Does to Mega-Kernelize a Program

A persistent kernel is launched once and stays resident for the lifetime of the computation, instead of being relaunched for each operator. The LLM megakernels described here are all persistent, but not every persistent kernel is a megakernel. Concurrent Real-Time’s persistent-threads work for hard real-time CUDA launches a kernel at application start and then commands it to perform different behaviours, without fusing a model’s operators into a scheduled task graph. Our way of putting it: persistence is the execution mechanism, and a megakernel is persistence plus a scheduler that runs a whole pass inside that resident kernel.

Hazy Research builds that scheduler as an on-GPU interpreter. Each SM receives a sequence of instructions scheduled ahead of time in Python, and the same schedule is reused for hundreds of forward passes. Each instruction has separate load, compute and store sections, which is what lets the interpreter pipeline across instruction boundaries. Dependencies are handled by counters in global memory: an instruction increments a counter when it completes, and a dependent instruction waits until the counter reaches its target. Spin-waiting on a counter only works if the producer block is actually resident and running. That is why persistent designs size the grid to what the GPU can hold at once. CUDA formalises the guarantee for grid-wide synchronisation: cudaLaunchCooperativeKernel either launches all the requested blocks or fails.

Mirage Persistent Kernel (MPK) automates building the megakernel. Its compiler lowers a tensor program into an SM-level task graph, generates a CUDA implementation for each task, and runs the tasks inside one persistent kernel with decentralised scheduling across SMs. Capturing dependencies per SM instead of per operator is what makes cross-operator software pipelining and fine-grained compute and communication overlap possible. The MPK arXiv paper reports up to 1.7x lower end-to-end latency than kernel-per-operator serving systems. The project’s repository advertises 1.2x to 6.7x. We would plan around the paper’s figure, because it is stated as an end-to-end result against named serving systems. On AMD hardware, Fleet found the chiplet-unaware Mirage baseline already 1.34x faster than vLLM at batch size 1 on an MI350X (7.83 ms against 10.51 ms), and chiplet-aware scheduling cut that to 6.73 ms.

A compiler is not mandatory. Cohere built its megakernel by hand, wrapping ordinary GEMM and attention kernels in one shared calling convention for warp roles, task format and barriers.

Can You Build a Megakernel in Triton, or Do You Need Hand-Written CUDA?

PyTorch has run whole LLMs on Triton kernels alone. Whether Triton gets you the last margin is a separate question. Cohere’s megakernel leans on low-level Hopper features: a fixed 12-warp shape with dedicated producer warps issuing TMA loads and storer warps issuing TMA stores, plus wgmma tensor-core instructions even at batch size 1. Hazy Research’s instructions are built from a CUDA template, and MPK generates CUDA for each task.

The Triton-against-CUDA evidence here comes from ordinary kernels, not megakernels. The PyTorch team ran Llama3-8B and Granite-8B with 100% Triton kernels and reached 0.76x to 0.78x of the CUDA-kernel performance on H100 and 0.62x to 0.82x on A100. They attributed the gap mainly to matmul and flash attention kernel latency. Our judgement: Triton is a good way to prototype a fused region and find out whether the bubbles matter for your workload. If the measured headroom is thin, plan for CUDA on the hot tasks, because warp specialisation and the asynchronous copy engines are where the published wins come from.

The Costs of Fusing Many Stages: Register Pressure, Occupancy and Maintainability

Every operator inside a megakernel shares one launch configuration, without per-operator specialisation, and register pressure goes up, which can lead to spilling. Laine, Karras and Aila of NVIDIA explained why this hurts on a GPU. A single register-hungry hot spot drives up the register usage of the whole kernel, fewer threads stay resident, and latency hiding gets worse. Fleet’s authors report that the combined register footprint limits occupancy to a single wave per SIMD, which removes latency hiding through wave switching. Compiling each task separately would fix that but give up the single launch, a trade-off they consider built into the approach.

For Llama-1B, a combined prefill-and-decode megakernel spills registers to local memory, and running prefill and decode as two separate megakernels beats it even on first-token latency. A megakernel launch also holds its thread-block allocation until all its tasks finish, including time spent waiting on dependencies.

Divergence is the other tax. Under SIMT execution, threads in a warp that take different branches are masked off and do no useful work. Large kernels can also overrun the GPU’s small instruction caches.

On maintainability, the sources point both ways. Cohere runs every opcode, attention and RMSNorm included, under the same threadblock shape and resolves warp roles at compile time. Once that framework existed, adding a new GEMM task became a small edit rather than a new kernel. Persistent designs that host heterogeneous functions need glue code to translate the one fixed grid configuration into the indexing each function expects. NVIDIA’s Cooperative Groups documentation notes that ad hoc primitives for synchronising across thread blocks have produced brittle code that is expensive to tune and maintain across GPU generations. Concurrent Real-Time notes that its busy-waiting persistent-threads design is likely to consume more power than letting the GPU go idle.

Where Do Megakernels Pay off Beyond LLMs, in Real-Time Pipelines Like Frame-Locked AR Compositing?

Judging from persistent-thread timing on small workloads, persistent execution pays off where a fixed frame deadline makes launch jitter the binding constraint. Concurrent Real-Time frames hard real-time systems as running at 1 kHz to 10 kHz, with frame durations from 1000 µs down to 100 µs, and states that CUDA launch and synchronise calls are not deterministic enough for many such applications. On a Jetson TX2, one test workload took a mean of 14.6 µs and a maximum of 18.7 µs with persistent threads, a spread of 4.1 µs. With launch-and-synchronise over zero-copy memory, the same work took 34.6 µs mean and 78.8 µs maximum, a 44.2 µs spread. For a frame-locked overlay, the worst-case figure decides whether you miss cadence, and that is where persistence helps most.

Graphics also supplies counter-evidence. In path tracing, Laine and colleagues split a megakernel into specialised wavefront kernels and measured speedups of 36% to 221% across three scenes. In the Conference scene, where material divergence hurt the megakernel badly, its thread utilisation was 23% against 53% for the wavefront version. A Vulkan study measured a wavefront path tracer at 73.6 FPS (13.58 ms per frame) against 64.7 FPS (15.47 ms) for the megakernel, about 16% faster. The authors attributed the gain to better memory behaviour and cache locality rather than more raw compute. The same authors still describe megakernels as attractive for latency-sensitive, lower-sample-count real-time work, because they minimise per-frame orchestration overhead.

What follows on broadcast AR compositing is our reading, not a measured result. An overlay compositing chain does broadly the same per-pixel work across the frame, so it has far less of the branch divergence that sank the path-tracing megakernels. In a chain like that, we expect a fused kernel to help most on the hot path after camera and player pose arrive. That stretch is where several short passes would each pay a launch and a full-grid barrier, and where keeping warped intermediates on-chip saves a memory round-trip per stage. Fusion also turns a variable stack of launches into one cost you can hold against the frame budget. The risk is the familiar one: a heavy colour or effects stage with high register use can drag down occupancy for the whole kernel.

How Do You Measure Whether a Megakernel Reclaims Latency Against a Fixed Frame or Token Budget?

Profile the conventional pipeline before you fuse anything. Context from profiling, not a best-practice rule, decides whether an optimisation is worth its cost. NVIDIA defines kernel launch latency in Nsight Systems as the time from the start of the launch API call to the start of kernel execution. A rising launch latency in a queue of asynchronous kernels is not necessarily a problem: it can simply mean the CPU is running ahead of the GPU. NVIDIA’s advice is to first get the GPU to be the bottleneck in Nsight Systems, then move to Nsight Compute for individual kernels.

A Measurement Procedure Against a Fixed Budget

  1. Fix the budget and the statistic: per-frame or per-token worst case and spread, not the mean, since frame-locked work fails on its tail.
  2. Capture the conventional graph in Nsight Systems and add up the gaps between kernels, the launch latency and the straggler tails at each boundary.
  3. Apply CUDA graphs and programmatic dependent launch, and measure again.
  4. Estimate the trade: a persistent region adds a fixed cost F per step and saves S(b) at batch size (or workload size) b. Launch overhead a graph has not removed stays constant with b, while saved intermediate traffic grows with it.
  5. Prototype the candidate region and measure the same worst-case statistic over a long run of frames or tokens.
  6. Re-check at every operating point you ship, because the crossover moves.

The prediction step can be done without building every candidate. The authors of “One Megakernel is Not Enough” selected megakernel regions from conventional-graph profiles and standalone kernel benchmarks, and their planner picked the measured-fastest design in 13 of 13 held-out cases on H100 NVL.

When Separate Kernels Are the Better Call

Separate kernels win once the megakernel’s lead falls below its fixed costs. On H100 NVL, Qwen3-TTS’s four-region megakernel lead fell from 15.9 ms at batch 16 to 0.65 ms at batch 64, while fixed costs stayed at 0.7 to 1.1 ms, so conventional execution won at batches 48 and 64. Regions that are already compute-bound, such as large-model prefill, tend to gain little. Across 13 model and batch combinations, the best design used zero to four megakernels; for NH-TTS and Qwen3-TTS the next-best layout was 3% to 29% slower.

Condition in your profile Leaning
Launch overhead and kernel barriers dominate the profile Megakernel or fused region
Memory-bound stages with intermediates round-tripping to memory Fused region keeps them on-chip
Heavy branch divergence across threads Separate specialised kernels
One stage with very high register use Cut it out into its own kernel
Compute-bound regions, or large batches where overhead is amortised Conventional kernels
Sub-millisecond hard deadline where launch jitter breaks the budget Persistent execution

TechnoLynx’s GPU Audit measures this deterministic compositing budget directly, testing kernel fusion as one lever when per-launch and memory-traffic overhead threatens broadcast cadence; the GPU Performance Engineering page describes how that work is scoped.

What Matters Most About a Mega Kernel in Practice?

Where you cut matters most. Each cut decides which stages share a launch configuration, how long thread blocks are held, and which fixed costs you pay. In that study, a split design beat the monolith for NH-TTS and Llama-1B. The three-megakernel text-to-speech pipeline published its first audio 21.4% sooner than a single megakernel and 4.05x sooner than the conventional TensorRT pipeline at batch 32. For a frame-locked overlay, we would treat “one kernel for the whole compositing chain” as a hypothesis, and expect the profiled answer to be a fused hot path with any divergent or register-heavy stage left as its own kernel.

A cheaper algorithm can remove a stage outright, which is why it is worth weighing algorithmic restructuring against kernel tuning before committing to a fused design. For broadcast AR, the first measurement on your own overlay pipeline is what settles how much of the frame budget goes to launch gaps.