Heterogeneous Inference Environment and Limitations of Megakernel
AI inference workloads have entered an extremely heterogeneous environment due to weekly releases of model architectures, dynamic changes in batch size and sequence length, and diversification of hardware platforms. Together AI showcased megakernel — executing the entire model's forward pass as a single fused GPU kernel — last year, achieving record speeds, but production application remained distant. After months of effort, they supported only one model, one batch size, and one customer. The development and maintenance costs were unsustainable.
"Last year we built megakernels. This year we deleted them."
This declaration adorns the introduction of Together AI's blog 'Last year we built megakernels. This year we deleted them: The road to Together Compiler'. The team experimented to see if they could break down the performance gains of megakernels into simpler primitives, and by layering CUDA Graphs and PDL (Pattern Description Language) on top of the basic Torch Inductor kernel, they restored 98% of megakernel performance without writing a single line of kernel code. Furthermore, by introducing 'multi-kernel' and agent loops, they recorded performance exceeding the previous megakernel by up to 1.7x, achieving over 1100 TPS based on Llama 1B on H100.
Part 1: Performance Gaps Left by Torch Compile Heuristics
PyTorch has enabled rapid experimentation with modular abstractions like `nn.Module` for over a decade, but sequential execution of individual operations leads to kernel launch overhead, loss of opportunities for operation and memory movement overlap, and underutilization of GPU resources. To address this, Torch Compile (PyTorch 2.0, Inductor backend) and CUDA Graphs were introduced, but significant performance gaps remained, prompting Together researchers to continuously develop specialized implementations like FlashAttention, ThunderKittens, and megakernels.
Inductor Internal Operations and Fusion Strategy Blind Spots
Torch Compile operates as a JIT pipeline.
1. Graph Capture: TorchDynamo tracks Python execution to generate an FX graph. In this process, fake tensors — holding only shape, data type, and stride metadata without actual data allocation or operations — are used to infer operations without GPU execution. 2. Operations and Kernels: The captured graph is lowered to Inductor's unique IR and classified into categories such as pointwise (activation), reduction (sum, softmax), scan (cumulative sum), and template (GEMM, etc.). cuBLAS or custom operations are handled opaquely as ExternKernel. 3. Fusion and Optimization: The decision to fuse is made using a greedy pairwise algorithm. Heuristics such as legality checks with `can_fuse()` and estimates of memory traffic reduction with `score_fusion()` rank the fusions. However, this relies on local heuristics rather than global search, missing cases where executing operations that do not share memory access could increase GPU utilization.
With the basic Inductor alone, 669 TPS is achieved on Llama 1B. As shown in Part 2, small modifications can reach megakernel-level performance, but heuristics struggle to keep up with new hardware and architectures.
Empirical Experiment: A Single Flag Caused a 49% Performance Drop
The Together team systematically explored the tuning flag space of Inductor and discovered that the compiler heuristics do not reliably select optimal implementations across workloads. As of Q3 2026, Inductor exposes 607 tuning flags, of which 198 were added in the last year. The effects between flags are not independent, making predictions difficult.
A representative case is the `freezing` flag. It assumes weight invariance during inference and aims for operational acceleration through pre-transformation (e.g., restoring original precision after FP32 conversion). Experimental results:
| Batch Size Type | Operation Pattern | Effect of Freezing Activation |
|---|---|---|
| Large Batch | Matrix-Matrix Multiplication (GEMM) | Throughput improvement of 10-26% |
| Batch Size 1 | Matrix-Vector Multiplication (GEMV) | Throughput drop of 49% |
Analysis revealed that the FP32 conversion was not restored in matrix-vector operations, causing the GPU to process larger 32-bit weights instead of low-precision weights. This is a typical case where the same compiler optimization yields opposite results depending on batch size.
From Megakernel to Multi-Kernel: Technical Grounds for Paradigm Shift
While megakernels delivered extreme performance, they were overfit to single model, batch, and hardware. The Together team decomposed the performance gains of megakernels to find essential primitives.
- CUDA Graphs: Eliminating kernel launch overhead
- PDL: Pattern-based kernel fusion and optimization representation
- Basic Inductor Kernel: Utilizing already sufficiently optimized templates (GEMM, etc.)
With just this combination, they successfully restored 98% performance without writing new kernels. Not stopping there, they introduced multi-kernel — a combination of multiple kernels tailored to workload characteristics instead of a single massive kernel — and agent loops — optimization agents that automatically explore the search space — achieving an additional 1.7x acceleration compared to megakernels. This signifies a fundamental shift from heuristic-based static decisions to data and search-based dynamic optimization.
Together Compiler: Evolution into a Sustainable Optimization Framework
Over the past two years, Together AI has explored 'next-generation inference optimization systems', continuously building GPU programming and optimization systems. The culmination of this is the Together Compiler. This framework aims for continual AI inference optimization. In an environment where model, hardware, and traffic patterns are constantly changing, it automatically finds and deploys optimal kernel configurations by combining compiler infrastructure, agent exploration, and production feedback loops instead of manual kernel engineering.
The structure of the blog's three parts summarizes this journey.
- Part 1 (Body): Empirical limitations of Torch Compile heuristics and benchmarking
- Part 2: Re-examination of megakernels, decomposition, and replacement with simple primitives
- Part 3: 'Bitter lessons' from building agent-driven optimization infrastructure and the final production system
From the perspective of developers and companies, the emergence of Together Compiler suggests a paradigm shift from 'heroic kernel engineering' to 'systematic compiler engineering'. To build a maintainable high-performance inference stack in heterogeneous hardware and model environments, one must acknowledge heuristic limitations and internalize a search-based automatic optimization pipeline.