Inferensys

Glossary

Vertical Fusion

Vertical fusion is a compiler optimization that merges sequentially dependent operators in a computational graph into a single kernel to minimize intermediate memory transfers and reduce launch overhead.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
INFERENCE OPTIMIZATION

What is Vertical Fusion?

Vertical fusion is a compiler optimization that merges sequentially dependent computational operators within a neural network's dataflow graph.

Vertical fusion is the merging of a producer operator with its immediate consumer operator within a computational graph. This optimization chains operations that have a direct data dependency, where the output of one operation is the input to the next. By combining them into a single fused kernel, the system eliminates the need to write the intermediate result to main memory and read it back, significantly reducing memory bandwidth pressure and kernel launch overhead. It is a foundational technique within compilers like XLA, TVM, and MLIR for accelerating model inference.

The primary benefit of vertical fusion is improved data locality and reduced memory-bound latency. The fused kernel keeps intermediate tensor values in fast on-chip registers or cache, rather than performing costly round-trips to GPU global memory. This is particularly profitable for sequences of elementwise operations (e.g., Add, ReLU, Sigmoid) and canonical patterns like Conv-BN-ReLU. The compiler's fusion planner uses a cost model to assess fusion profitability, ensuring the combined operation does not create excessive register pressure or hinder parallelism.

OPERATOR AND KERNEL FUSION

Core Mechanisms and Objectives

Vertical fusion is a compiler optimization that merges sequentially dependent operators within a computational graph to reduce memory traffic and kernel launch overhead.

01

Definition and Dataflow

Vertical fusion is the merging of a producer operator with its immediate consumer operator within a computational graph. This optimization chains operations that have a direct, sequential data dependency, where the output of one operator is the sole input to the next.

  • Objective: Eliminate the need to write the producer's result to slow, high-bandwidth memory (HBM) and immediately read it back for the consumer.
  • Graph Traversal: Compilers identify these patterns by analyzing the dataflow graph, looking for edges connecting two nodes where the consumer has no other data dependencies.
  • Contrast with Horizontal Fusion: Unlike horizontal fusion, which merges parallel operators, vertical fusion explicitly targets the critical path of sequential execution.
02

Primary Performance Benefits

The fusion of vertically dependent operators delivers performance gains through two primary mechanisms:

  • Reduced Global Memory Traffic: The most significant benefit. Intermediate tensors are kept in fast on-chip memory (GPU registers or shared memory) instead of being written to and read from DRAM. This is critical for memory-bound operations.
  • Lowered Kernel Launch Overhead: Executing one fused kernel instead of two separate kernels amortizes the fixed launch latency associated with each GPU kernel invocation. This overhead becomes substantial in models with many small, sequential operations.

Example Impact: Fusing a GeLU activation directly after a Linear layer can eliminate a full read/write cycle of a potentially large activation tensor, directly translating to higher throughput and lower latency.

03

Canonical Fused Patterns

Compilers use pattern matching to identify common, profitable sequences for vertical fusion. Well-known examples include:

  • Conv-BN-ReLU: The foundational block of CNNs, fusing Convolution, Batch Normalization, and ReLU activation.
  • Linear-GeLU / Linear-GELU-Dropout: Common in transformer feed-forward networks.
  • LayerNorm-RMSNorm-Silu: Common normalization and activation sequences in modern architectures.
  • Elementwise Chains: Sequences like Add -> ReLU -> Multiply where all ops are pointwise.

Frameworks like PyTorch's Inductor and XLA have built-in pattern matchers that automatically rewrite these subgraphs into calls to pre-written or generated fused kernels for optimal performance.

04

Compiler Implementation

Vertical fusion is implemented within graph-level compilers and just-in-time (JIT) compilation systems. The process involves:

  1. Graph Lowering: The high-level model graph is lowered to a mid-level IR (like MLIR's Linalg dialect or Torch IR) consisting of primitive operators.
  2. Pattern Matching & Planning: A fusion planner identifies producer-consumer pairs and groups them into fusion groups based on fusion heuristics and a cost model.
  3. Kernel Code Generation: The compiler generates a single fused kernel (e.g., CUDA or Metal code) that implements the combined logic of the group.
  4. Scheduling: The compiler performs fusion-aware scheduling, deciding on loop structures, tiling, and memory promotion (e.g., to shared memory) for the fused kernel.

Key compilers performing this optimization include XLA (for JAX/TensorFlow), Apache TVM, MLIR-based compilers, and PyTorch's torch.compile with its Inductor backend.

05

Profitability Analysis and Constraints

Not all vertical fusions are beneficial. A fusion planner must analyze fusion profitability based on:

  • Memory vs. Compute Bound: Fusing a memory-bound operator (e.g., activation) with a compute-bound one (e.g., matmul) is almost always profitable. Fusing two compute-heavy ops may offer less benefit.
  • On-Chip Memory Pressure: Fusing too many operations can exhaust GPU registers or shared memory, leading to register spilling to slower memory, which harms performance.
  • Parallelism Reduction: Fusing may limit independent scheduling opportunities for the original operators.
  • Kernel Complexity: Extremely large fused kernels can hinder GPU warp scheduler efficiency and increase compilation time.

Compilers use a cost model for fusion to estimate execution cycles with and without fusion, often deciding to fuse only when a significant reduction in DRAM traffic is predicted.

06

Related Optimization: CUDA Graphs

While not fusion in the traditional sense, CUDA Graphs address a similar high-level goal: reducing launch overhead for sequences of operations. A CUDA Graph captures a whole series of kernel launches and memory copies into a single, replayable unit.

  • Coarse-Grained Optimization: It reduces CPU overhead and GPU driver latency by submitting the entire graph once, rather than each kernel individually.
  • Complementary to Fusion: CUDA Graphs are often used after aggressive kernel fusion. The graph contains fewer, larger fused kernels, making graph capture and replay even more efficient.
  • Use Case: Essential for achieving peak throughput in inference servers using frameworks like NVIDIA Triton, where the same graph of fused kernels is executed repeatedly for different requests.
COMPILER OPTIMIZATION

How Vertical Fusion Works: A Compiler's Process

Vertical fusion is a compiler optimization that merges sequentially dependent operations into a single kernel to minimize memory traffic and kernel launch overhead.

Vertical fusion is a graph-level compiler optimization that merges a producer operator with its immediate consumer operator within a computational graph. This process chains operations that have a direct data dependency, creating a single, compound kernel. The primary goal is to eliminate the need to write the producer's output to slow global memory and read it back for the consumer, thereby reducing memory bandwidth pressure and kernel launch latency. It is a fundamental technique within compilers like XLA, TVM, and MLIR for accelerating neural network inference.

The compiler identifies fusion candidates by analyzing the dataflow graph, looking for patterns where an operator's output is used by only one subsequent operation. A cost model evaluates fusion profitability, balancing reduced memory traffic against potential downsides like increased register pressure or decreased parallelism. Successful fusion, as in a fused Conv-BN-ReLU block, keeps intermediate tensor values in fast on-chip memory (registers or shared memory) across the combined operations. This transforms a memory-bound sequence into a more compute-bound kernel, maximizing hardware utilization and directly lowering inference latency.

COMPILER OPTIMIZATIONS

Canonical Examples of Vertical Fusion

Vertical fusion merges sequentially dependent producer-consumer operations. These canonical patterns demonstrate where the optimization is most impactful, eliminating intermediate memory traffic and kernel launch overhead.

01

Fused Conv-BN-ReLU

This is the quintessential example of vertical fusion in convolutional neural networks (CNNs). It combines three sequentially dependent layers:

  • Convolution: Applies filters to input feature maps.
  • Batch Normalization: Normalizes the output activations.
  • ReLU Activation: Applies a non-linear rectification.

Fusion Benefit: The intermediate NCHW tensors between these layers never materialize in global GPU memory. The normalization and activation are computed on-the-fly as the convolution outputs are produced, drastically reducing memory bandwidth pressure. This pattern is so common it is often implemented as a single, hand-tuned kernel in deep learning libraries.

~2.5x
Typical Speedup
02

Fused Multi-Head Attention (e.g., FlashAttention)

The transformer attention mechanism is a deep sequence of operations: linear projections, scoring, masking, softmax, and aggregation. A naive implementation launches separate kernels for each, writing massive intermediate attention score matrices (O(N²)) to slow HBM.

Vertical Fusion in FlashAttention: The algorithm fuses the entire attention block into a single kernel. It performs the softmax normalization using a online, numerically stable method without materializing the full score matrix. The key innovation is IO-awareness: it tiles computations to keep working data in fast SRAM, recomputing scores as needed. This reduces HBM reads/writes from quadratic to linear in sequence length.

4-10x
HBM Traffic Reduction
03

Elementwise Operation Chains

Chains of pointwise operations are prime candidates for vertical fusion. Common patterns include:

  • Activation Sequences: GELUDropoutResidual Add
  • Normalization & Scaling: LayerNormScaleBias
  • Precision Conversion: DequantizeFP16 OperationQuantize

Fusion Mechanism: The compiler identifies that these operations are elementwise independent—each output element depends only on the corresponding input element(s). It then generates a single kernel that loops over tensor elements once, applying the entire sequence of transformations in-register. This eliminates multiple passes over the data and associated kernel launch latency and memory bandwidth consumption.

>10 ops
Can Be Fused
04

Fused Linear + Activation (GeLU/SiLU)

In transformer feed-forward networks and MLP blocks, a linear (fully-connected) layer is immediately followed by a Gaussian Error Linear Unit (GeLU) or Sigmoid Linear Unit (SiLU) activation.

Vertical Fusion Process: The fused kernel computes the matrix multiplication for a block of the output. Instead of writing the block result back to global memory, it immediately applies the non-linear GeLU function (x * Φ(x)) using efficient polynomial approximations. This keeps the intermediate results in registers or shared memory, avoiding a round-trip to main memory. This optimization is critical in memory-bound workloads where the linear layer's output size is large.

~1.7x
End-to-End Speedup
06

Fused Reduction Operations

Vertical fusion is applied to sequences where a reduction (e.g., sum, max) feeds directly into another operation. A classic example is the fusion of Layer Normalization components.

LayerNorm Fusion Breakdown:

  1. Mean Calculation: A reduction kernel sums elements along a dimension.
  2. Variance Calculation: Another reduction kernel sums squared differences.
  3. Normalize & Scale: Elementwise normalization using the computed mean/variance.

Fused Implementation: A single kernel performs both reductions in one pass over the data using efficient parallel reduction algorithms, computes the statistics, and immediately normalizes the values. This avoids three separate kernel launches and two passes over the input tensor, providing significant latency savings for this common operation.

COMPARISON

Vertical Fusion vs. Horizontal Fusion

A comparison of two fundamental compiler strategies for merging operators in a computational graph to optimize inference performance.

Feature / CharacteristicVertical FusionHorizontal Fusion

Primary Objective

Reduce intermediate memory transfers between sequentially dependent operations.

Reduce kernel launch overhead by batching parallel independent operations.

Dataflow Pattern

Producer-consumer chain (sequential dependency).

Multiple independent consumers of the same input (parallel branches).

Memory Optimization

High. Eliminates writes/reads of intermediate tensors to global memory.

Low to Moderate. Input is read once, but outputs are typically separate.

Compute Intensity

Can increase arithmetic intensity by chaining light ops (e.g., activations) with heavy ops.

Does not inherently change the arithmetic intensity of the individual operations.

Typical Fused Pattern

Convolution → BatchNorm → ReLU (Conv-BN-ReLU).

Multiple independent pointwise operations (e.g., parallel Sigmoid and Tanh gates in an LSTM).

Compiler Complexity

Moderate. Requires dependency analysis and pattern matching for profitable chains.

Lower. Often simpler to identify independent ops that can be co-launched.

Impact on Kernel Launch Overhead

Reduces launches by collapsing a chain into one kernel.

Reduces launches by executing multiple ops in a single, batched kernel.

Register Pressure / Resource Usage

Can be high, as a single kernel holds live values for the entire chain.

Can be high if many parallel operations compete for registers within one kernel.

Applicability in Transformers

Common in fused attention kernels (e.g., FlashAttention) and feed-forward blocks.

Less common; transformer graphs are largely sequential. Potential in parallel activation paths within MoE or MLP layers.

VERTICAL FUSION

Frequently Asked Questions

Vertical fusion is a foundational compiler optimization for machine learning workloads. These questions address its core mechanisms, benefits, and implementation details for engineers optimizing inference performance.

Vertical fusion is a compiler optimization that merges a producer operator with its consumer operator into a single, unified kernel. It works by identifying sequentially dependent operations within a computational graph—where one operator's output is the direct input to the next—and combining their logic. This eliminates the need to write the producer's result to global memory (e.g., GPU VRAM) and then immediately read it back for the consumer. Instead, the intermediate result is kept in fast on-chip memory (registers or shared memory), passing the value directly within the fused kernel's execution. The process is typically performed by a fusion compiler (like XLA, TVM, or MLIR) which analyzes the graph, applies fusion heuristics to determine profitability, and generates a fused kernel.

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.