We address a fundamental question: How far should kernel fusion extend and which boundaries are worth preserving? Unlike the general convention of using one monolith megakernel at lower batch sizes or supporting only a selective model component as megakernel and rest of the model as conventional graph, we show that the best configuration is a composition of multiple megakernels with no overlapping conventional graph component, or a fully conventional graph. We present a framework that selects megakernel regions in a streaming inference model from conventional-graph profiles and standalone kernel benchmarks, without running any candidate design end to end. It simulates each candidate's tile and event graphs to predict time to first output and selects the measured best design in all 13 combinations of model and batch size we tested. Across NH-TTS, Qwen3-TTS and Llama-1B, the best composition ranges from two to four megakernels to fully conventional execution and none is a single megakernel. We introduce four model categories (megakernel-stable/ receding/ advancing/ averse) that capture how a model's best megakernel composition changes with batch size. Using NH-TTS model on H100 NVL GPU, we demonstrate that preserving three specialized megakernels can improve both latency and throughput despite introducing additional kernel boundaries. At batch size 32, this design delivers 21.4% lower mean batch-p95 time to first audio (TTFA) and 28% higher throughput than the evaluated monolith megakernel. Against conventional TensorRT graph execution, it reduces engine-level mean batch TTFA p95 by 4.05x and increases throughput by 19.7%.
Figures & tables
Fig. 1: Six representative NH-TTS layouts from the 81-candidate design space. The three-megakernel design with the lowest measured first-publication latency at batch 32 on H100 NVL is highlighted.
Fig. 3: Best megakernel composition for NH-TTS at batch size 32
Feature
Modeled in
Algorithmic change
Cross-kernel readiness
Tile graph
Edges: drop the barrier edges between consecutive kernels; keep the tile-to-tile tensor edges
Weight prefetch
Tile graph
Scheduling rule: one staging slot per worker. A staged tile’s weight load is queued while its predecessors still run; its compute still waits for them
On-chip forwarding
Tile graph
Node weights: remove the forwarded tensor’s store and reload from the memory queue
Producer–consumer overlap
Event graph
Edges, capacity: the first chunk depends on step r instead of the preceding task, and runs in its own CTA allocation
Fewer launches, internal synchronization
Event graph
Nodes: a dispatch node before each launch, weighted by the per-launch cost; synchronization adds to tile weights
Smaller CTA budget
Service table
Tile scheduling uses W workers. For an opaque operator with a grid of g CTAs, compute time is approximated by scaling with ⌈g/W⌉/⌈g/132⌉ , assuming equal-cost CTA waves.
TABLE I: Feature modeling and algorithmic changes.
Stage 1 a
Megakernel
TensorRT
Megakernel
0.991ms
1.290ms
Stage 0 a
TensorRT
1.216ms
9.760ms
a Refer Figure 3
TABLE II: Megakernel vs TensorRT Latencies
Model
Conventional baseline
First publication
Batch sizes
NH-TTS
TensorRT-LLM with CUDA graphs, plus TensorRT engines
Time to First Audio (TTFA) at engine publication
1, 32, 64
Qwen3-TTS
PyTorch with vLLM FlashAttention-3, torch.compile and CUDA graphs
Time to First Audio (TTFA) on the host
1, 16, 32, 48, 64
a Llama 3.2 1B
PyTorch SDPA (FlashAttention) in one CUDA graph
Time to First Token (TTFT)
1, 2, 16, 32, 48
a 496-token prompt
TABLE III: The three models in our evaluation.
Fig. 4: Predicted selections within the implemented validation pools. Predicted and measured margins compare the winner with the nearest alternative in the same pool.
Large
Batch
Pays
Doesn’t
Pays
Megakernel-stable (NH-TTS; Llama-1B for first-token latency)
Megakernel-receding (Qwen3-TTS; Llama-1B for inter-token latency)
Small
Doesn’t
Megakernel-advancing a
Megakernel-averse a
a Predicted, not observed.
TABLE IV: Four model categories by where megakernels pay
When large language models (LLMs) serve real-time inference in commercial online advertising systems, end-to-end latency must be strictly bounded to the millisecond range. Yet every token generated during the decode phase triggers thousands of kernel launches, and kernel launch overhead alone can account for 14.6% of end-to-end inference time. MegaKernel eliminates launch overhead and inter-operator HBM round-trips by fusing multiple operators into a single persistent kernel. However, existing MegaKernel implementations face a fundamental tension between portability and efficiency on resource-constrained GPUs such as NVIDIA Ada: hand-tuned solutions are tightly coupled to specific architectures and lack portability, while auto-compiled approaches introduce runtime dynamic scheduling whose branch penalties are unacceptable in latency-critical settings. We observe that under a fixed deployment configuration, the optimal execution path of a MegaKernel is uniquely determined, and runtime dynamic decision-making can be entirely hoisted to compile time. Building on this insight, we propose Ada-MK: (1) a three-dimensional shared-memory constraint model combined with K-dimension splitting that reduces peak shared memory usage by 50%; (2) MLIR-based fine-grained DAG offline search that solidifies the optimal execution path, completely eliminating runtime branching; and (3) a heterogeneous hybrid inference engine that embeds MegaKernel as a plugin into TensorRT-LLM, combining high-throughput Prefill with low-latency Decode. On an NVIDIA L20, Ada-MK improves single-batch throughput by up to 23.6% over vanilla TensorRT-LLM and 50.2% over vLLM, achieving positive gains across all tested scenarios--the first industrial deployment of MegaKernel in a commercial online advertising system.
AutoMegaKernel (AMK) compiles a HuggingFace Llama-family model into a single persistent cooperative CUDA kernel that runs the whole forward pass in one launch, with no per-model hand-written CUDA. The contribution is the system, not raw speed. A frozen schedule-IR validator statically certifies deadlock-freedom and race-freedom via static graph checks (not a mechanized proof), so an unsafe agent-proposed schedule is rejected before launch: across 7,160 adversarial schedules (6,091 unsafe) it had zero false-accepts and accepted all 360 real lowerings. The same source retargets sm_80/sm_90/sm_120 from one codebase, auto-generates correct megakernels for 10 of 10 supported models, and on a real SmolLM2-135M checkpoint reproduces HuggingFace greedy decode token-for-token (perplexity match 2.5e-7). An unattended, agent-drivable autoresearch loop self-improves the megakernel over its own baseline (1.25-1.72x). A search-found int8 (W8A16) megakernel beats CUDA-graphed cuBLAS bf16 at batch-1 decode across NVIDIA's datacenter inference fleet: L4 up to 1.33x, the current-gen L40S 1.25-1.27x, A10G up to 1.08x at scale, and the consumer RTX 5090 1.19-1.23x. The ordering is not a clean function of bandwidth (the 864 GB/s L40S beats the 600 GB/s A10G); the divide is inference-class vs training-class. AMK trails cuBLAS on the high-bandwidth training-class A100/H100, where the harness localizes the cross-SM-sync bottleneck; we report the gap plainly. This is a precision-asymmetric (W8A16 vs bf16) comparison at decode position 0; the largest real checkpoint is TinyLlama-1.1B. Code and the harness: https://github.com/RightNow-AI/AutoMegaKernel
Mixed batching (MB)--interleaving prefill and decode in a single batch--has become the standard scheduling strategy for large language model (LLM) inference due to its efficiency in maximizing compute and memory utilization. However, through controlled experiments, we find that prefill-decode interference inflates MB's per-step marginal cost above that of pure decode. On the high-bandwidth H200 (4.8 TB/s), this occurs only when decode tokens exceed 80% of the batch; however, on the bandwidth-constrained RTX PRO 6000 (1.792 TB/s), this threshold plummets to just 20%. Consequently, the optimal choice between MB and exclusive batching (EB) fundamentally depends on GPU memory bandwidth, model size, and workload composition. We derive a closed-form condition for this EB-MB performance crossover, along with asymptotically optimal phase-switching thresholds and memory-safe batch sizing for EB. Optimized EB achieves up to 41.9% higher throughput on bandwidth-constrained GPUs, while MB retains its advantage on high-bandwidth hardware with larger models. Our hybrid scheduler EB+ applies this condition online to dynamically switch between EB and MB without manual intervention. Under non-stationary traffic with distribution or concurrency shifts, EB+ attains the highest or near-highest throughput in every setting, outperforming MB by up to 36.4%.
Weifang Zhang, Yuzhou Nie, Bowen Pang +2
The Hong Kong Polytechnic University · UC Santa Barbara · Technology and Engineering Center for Space Utilization, Chinese Academy of Sciences +1