Inferensys

Glossary

Fused Kernel

A fused kernel is a single, optimized GPU or accelerator kernel that executes multiple primitive operations, eliminating intermediate memory transfers to reduce latency and improve throughput.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
INFERENCE OPTIMIZATION

What is a Fused Kernel?

A fused kernel is a core compiler optimization technique for accelerating neural network inference on GPUs and specialized accelerators.

A fused kernel is a single, hand-written or compiler-generated GPU kernel that executes the combined computational logic of multiple primitive neural network operators, eliminating the need to write intermediate results to slow global memory. This technique directly reduces kernel launch overhead and improves data locality by keeping temporary values in fast registers or shared memory, which is critical for reducing latency in model inference. It is a foundational optimization within compilers like XLA, TVM, and torch.compile.

Fusion is most profitable for sequences of memory-bound operations, such as elementwise functions (e.g., adding bias and applying ReLU), or canonical patterns like Conv-BN-ReLU. By minimizing data movement—often the primary bottleneck—fused kernels increase arithmetic intensity and better saturate hardware compute units. Advanced implementations, like FlashAttention for transformers, exemplify how sophisticated fused kernels can enable new model capabilities by dramatically optimizing memory access patterns.

FUSED KERNEL

Core Mechanisms and Objectives

A fused kernel is a single, hand-written or compiler-generated GPU or accelerator kernel that implements the combined functionality of multiple primitive operations, eliminating intermediate memory stores and loads. This section breaks down its core mechanisms and primary objectives.

01

Primary Objective: Eliminate Intermediate Memory Traffic

The fundamental goal of a fused kernel is to bypass the memory hierarchy bottleneck. In a standard, unfused execution pipeline, each primitive operator (e.g., a matrix multiplication followed by a bias add and ReLU) writes its full output tensor to global GPU memory (HBM) only for the next operator to immediately read it back. This store/load cycle consumes hundreds of clock cycles and saturates the memory bus.

A fused kernel keeps intermediate results in fast, on-chip memory:

  • Registers: For scalar values and small vectors.
  • Shared Memory: For tile-level intermediate tensors.
  • L1/L2 Cache: For reusable data blocks. By fusing operations, the intermediate tensor never materializes in slow global memory, reducing DRAM bandwidth pressure and hiding memory latency.
02

Mechanism: Amortizing Kernel Launch Overhead

Every GPU kernel launch incurs fixed launch latency and scheduling overhead from the host CPU and the GPU driver. For a model with hundreds of small, sequential operations, this overhead can constitute a significant portion of total runtime.

Fusion addresses this by:

  • Reducing Launch Count: Converting N kernel launches into 1.
  • Unified Resource Allocation: A single kernel manages its thread blocks, shared memory, and register file allocation once.
  • Continuous Execution: Threads remain active, moving from one fused operation to the next without being disbanded and re-launched. This is particularly critical for lightweight, elementwise operations (like activations) where the launch cost can rival or exceed the computation time itself.
03

Mechanism: Increasing Arithmetic Intensity

Arithmetic Intensity (AI) is a key hardware metric defined as the number of floating-point operations (FLOPs) performed per byte of data transferred from main memory (FLOPs/byte). Higher AI moves a workload from being memory-bound to compute-bound, better utilizing the GPU's massive parallel compute units.

Fusion directly increases AI by:

  • Reusing Loaded Data: A single value loaded from global memory can be used by multiple fused operations (e.g., an input value used in a convolution, then batch norm, then activation).
  • Fusing Light & Heavy Ops: Attaching low-FLOP elementwise ops (like GELU, SiLU) to heavy compute ops (like GEMM) "for free" on already-resident data. This transforms the kernel's profile, allowing it to approach the theoretical peak FLOPS of the hardware by keeping the compute units saturated with work.
04

Compiler-Driven vs. Hand-Written Fusion

Fused kernels are created through two primary methodologies:

Compiler-Driven Fusion (e.g., XLA, TVM, torch.compile):

  • Graph-Level Analysis: The compiler's fusion planner analyzes the computational graph, identifying subgraphs (fusion groups) that are profitable to fuse.
  • Pattern Matching: Recognizes common patterns like Conv -> BatchNorm -> ReLU.
  • JIT/AOT Codegen: Generates the fused kernel code Just-In-Time at runtime or Ahead-Of-Time during compilation.

Hand-Written Fusion (Library Kernels):

  • Manual Optimization: Experts write a single, highly-tuned CUDA/HIP/Metal kernel for a specific, performance-critical pattern.
  • Canonical Examples: NVIDIA's cuDNN fused convolution kernels, or the FlashAttention kernel for multi-head attention.
  • Extreme Optimization: Allows for deep, architecture-specific optimizations (e.g., warp-level operations, tensor core usage) that compilers may not yet automate.
05

Vertical vs. Horizontal Fusion Strategies

Fusion strategies are categorized by the dataflow relationship between the operators being combined.

Vertical Fusion (Producer-Consumer):

  • Fuses operators that are sequentially dependent in the graph.
  • Example: LayerNorm -> Linear -> GELU. The output of one is the direct input to the next.
  • Primary Benefit: Eliminates the intermediate tensor between the two stages.

Horizontal Fusion (Parallel/Sibling):

  • Fuses operators that consume the same input or operate in parallel.
  • Example: Two separate GELU operations applied to different branches of a model.
  • Primary Benefit: Amortizes the cost of loading the shared input tensor across multiple operations, improving data reuse.

Modern compilers employ both strategies, often within the same kernel, to maximize data locality.

06

Trade-offs and Fusion Profitability Analysis

Fusion is not always beneficial. A compiler's cost model must evaluate fusion profitability.

Potential Downsides:

  • Register Pressure: A fused kernel may require more live variables, exhausting the GPU's limited register file. This can force spilling to slower memory, reducing performance.
  • Decreased Parallelism: Fusing a very large operation with a small one can cause thread divergence or underutilization of GPU cores.
  • Compilation Complexity: The search space of possible fusions grows exponentially; the compiler must make near-optimal decisions quickly.

Profitability Heuristics consider:

  • Operation Types: Elementwise ops are almost always profitable to fuse. Reductions are more complex.
  • Data Size & Shape: Small, transient tensors are ideal candidates.
  • Hardware Targets: Fusion strategies differ for data center GPUs (e.g., H100) versus mobile NPUs. The goal is to fuse as much as possible without triggering these negative side effects.
FUSED KERNEL

Implementation in AI Compilers

A fused kernel is a single, optimized GPU or accelerator kernel that executes the combined computational logic of multiple primitive operations, eliminating the need for intermediate memory stores and loads.

In AI compilers like XLA, TVM, and MLIR, kernel fusion is a critical graph-level optimization. The compiler identifies fusion groups—adjacent operators like Conv-BN-ReLU—within the computational graph and generates a single, custom kernel for the entire sequence. This process, guided by fusion heuristics and a cost model, minimizes kernel launch overhead and maximizes data locality by keeping intermediate tensors in fast on-chip memory (e.g., GPU registers or shared memory), directly reducing latency.

The implementation strategy depends on whether operations are memory-bound or compute-bound. For memory-bound chains, fusion primarily reduces DRAM traffic. For compute-bound workloads, it increases arithmetic intensity. Compilers perform this via pattern matching for fusion and fusion-aware scheduling, deciding between vertical fusion (producer-consumer chains) and horizontal fusion (parallel operations). The result is a fused kernel that executes with fewer global memory accesses and higher hardware utilization, a cornerstone of efficient inference.

FUSION STRATEGIES

Types of Fusion: Vertical vs. Horizontal

A comparison of the two primary strategies for combining operators into a single kernel, distinguished by the dataflow relationship between the operations being fused.

CharacteristicVertical FusionHorizontal Fusion

Dataflow Relationship

Producer-consumer (sequential dependency)

Sibling operators (parallel independence)

Primary Optimization Goal

Eliminate intermediate memory stores/loads

Amortize kernel launch overhead across parallel work

Typical Operation Pattern

Chains like Convolution → BatchNorm → ReLU

Multiple independent pointwise ops on the same tensor

Impact on Memory Bandwidth

Significantly reduces global memory traffic

Moderate reduction; depends on input reuse

Impact on Arithmetic Intensity

Can increase by combining light & heavy ops

Usually neutral; ops often similar in compute cost

Compiler Search Complexity

Lower; constrained by direct data dependencies

Higher; must evaluate combinatorial choices

Common Example

Fused Conv-BN-ReLU block

Fused elementwise (e.g., Add, Mul, Sigmoid) block

Potential Drawback

May increase register pressure in a single kernel

May limit parallelism if fused ops have different resource needs

FUSED KERNEL

Frequently Asked Questions

A fused kernel is a core optimization in high-performance machine learning, combining multiple operations into a single GPU execution unit. This glossary answers key technical questions for compiler and performance engineers.

A fused kernel is a single, hand-written or compiler-generated GPU (or other accelerator) kernel that implements the combined computational logic of multiple primitive operations, eliminating the need to write intermediate results to slow global memory.

In a standard unfused execution, each operation (e.g., a matrix multiplication followed by a bias add and a ReLU activation) launches its own kernel. Each launch incurs overhead, and each operation must load its input from and store its output to the device's DRAM. A fused kernel performs this entire sequence in one launch, keeping intermediate values in fast registers or shared memory. This optimization directly targets the memory-bound nature of many ML workloads, where data movement is the primary bottleneck, not computation.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.