Inferensys

Glossary

Graph Fusion

Graph fusion is a compiler optimization technique that automatically identifies and merges subgraphs of operators within a computational graph to create efficient, fused kernels, reducing inference latency and memory overhead.
Performance engineer optimizing AI latency on laptop, latency charts visible, technical optimization session.
COMPILER OPTIMIZATION

What is Graph Fusion?

Graph fusion is a core compiler optimization for accelerating neural network inference and training by restructuring computational graphs.

Graph fusion is a compiler optimization technique that automatically identifies and merges sequences or clusters of operators within a neural network's computational graph into single, compound operations. This process, guided by pattern matching and hardware-aware cost models, creates custom fused kernels that execute the combined logic. The primary goal is to minimize kernel launch overhead and reduce costly intermediate memory transfers between global GPU memory and on-chip registers or caches, thereby improving computational efficiency and reducing latency.

The optimization is performed by fusion compilers like XLA, TVM, and MLIR, which analyze data dependencies and operator properties to form fusion groups. Key strategies include vertical fusion of dependent operations and horizontal fusion of parallel operations. Successful fusion, as seen in patterns like Conv-BN-ReLU or FlashAttention, transforms memory-bound operations into compute-bound ones, maximizing hardware utilization. The profitability of fusion is evaluated against potential downsides like increased register pressure.

COMPILER OPTIMIZATION

Core Mechanisms of Graph Fusion

Graph fusion is a compiler-driven optimization that transforms a computational graph by merging multiple primitive operations into efficient, compound kernels. Its core mechanisms are designed to minimize overhead and maximize hardware utilization.

01

Pattern Matching & Subgraph Identification

The compiler first traverses the computational graph to identify subgraphs of operators that are profitable to fuse. This is often done via pattern matching against known, high-benefit sequences (e.g., Conv-BatchNorm-ReLU). The process analyzes data dependencies and tensor shapes to find fusible groups, known as fusion groups.

02

Cost Model & Profitability Analysis

A cost model evaluates whether fusing a candidate group will yield a net performance gain. It estimates:

  • Reduction in kernel launch overhead and global memory traffic.
  • Increase in arithmetic intensity and data locality.
  • Potential downsides like increased register pressure or reduced parallelism. The fusion planner uses this model to construct an optimal fusion plan, prioritizing memory-bound operations where fusion benefits are highest.
03

Kernel Generation & Code Emission

Once a fusion group is finalized, the compiler generates a single fused kernel. This involves:

  • Lowering the fused operator subgraph to a unified intermediate representation.
  • Applying loop fusion and scheduling transformations to optimize for cache hierarchy (e.g., L1, shared memory).
  • Emitting target-specific code (e.g., CUDA, Metal) for the new compound operation. Compilers like XLA, TVM, and MLIR specialize in this automated kernel generation.
04

Vertical vs. Horizontal Fusion

Fusion strategies are categorized by the dataflow relationship between operators:

  • Vertical Fusion: Merges a producer operator with its consumer (e.g., a matrix multiplication followed by a bias add). This reduces intermediate results written to slow memory.
  • Horizontal Fusion: Merges independent operators that consume the same input or operate in parallel. This amortizes kernel launch overhead and can improve memory coalescing. Compilers use heuristics to apply the most beneficial strategy per subgraph.
05

Ahead-of-Time vs. Just-in-Time Fusion

Fusion can occur at different stages in the compilation pipeline:

  • Ahead-of-Time (AOT) Fusion: The graph is analyzed, fused, and compiled to a static executable before deployment. This minimizes runtime overhead and is used for fixed graph deployments.
  • Just-in-Time (JIT) Fusion: Performed dynamically at runtime (e.g., via torch.compile). This allows fusion decisions to adapt to dynamic input shapes and specific hardware contexts, offering flexibility for dynamic graphs.
06

Canonical Fused Operators

Certain fused operator patterns are so ubiquitous they have dedicated, hand-tuned implementations:

  • Fused Conv-BN-ReLU: The standard building block of CNNs, combining convolution, batch normalization, and activation.
  • Fused Multi-Head Attention: A key optimization in transformers, famously implemented by FlashAttention. It fuses the entire attention mechanism (QKV projection, scoring, softmax, aggregation) into one IO-aware kernel to minimize HBM accesses.
  • Elementwise Fusion: Chains pointwise ops (e.g., Add, Sigmoid, Mul) that operate independently on each tensor element.
COMPILER OPTIMIZATION

How Graph Fusion Works: A Compiler's Process

Graph fusion is a compiler-driven optimization that merges multiple computational nodes in a neural network's dataflow graph into single, efficient kernels.

The process begins with graph analysis, where the compiler traverses the computational graph to identify candidate subgraphs for fusion. It uses pattern matching to find known, profitable operator sequences (like Conv-BN-ReLU) and applies fusion heuristics or a cost model to evaluate the performance impact of merging them. The goal is to create fusion groups that minimize intermediate memory transfers and kernel launch overhead.

Once groups are identified, the compiler performs kernel generation, creating a single fused kernel that implements the combined operations. This involves fusion-aware scheduling to optimize memory access patterns and register usage for the target hardware. The final fused graph is then executed, with the fused kernel reducing data movement and improving arithmetic intensity, leading to lower latency and higher throughput during inference.

IMPLEMENTATION LANDSCAPE

Graph Fusion in Major Frameworks & Compilers

Graph fusion is not a monolithic technique but is implemented through distinct compiler architectures and optimization philosophies across the machine learning ecosystem. This section details how major frameworks execute this critical optimization.

FUSION STRATEGIES

Types of Fusion: Vertical vs. Horizontal

A comparison of the two primary strategies for combining operators within a computational graph, based on the data dependency relationship between the operations.

CharacteristicVertical FusionHorizontal Fusion

Primary Objective

Reduce intermediate memory traffic between sequentially dependent operations

Amortize kernel launch overhead and improve parallelism for independent operations

Dataflow Relationship

Fuses a producer operator with its direct consumer operator

Fuses multiple sibling operators that consume the same input or operate in parallel

Graph Pattern

Linear chain (e.g., Op A -> Op B)

Fan-out or parallel branch (e.g., Input -> Op A, Op B, Op C)

Typical Operations Fused

Elementwise ops (ReLU, Sigmoid) following heavy ops (MatMul, Conv), Convolution-BatchNorm-ReLU

Independent pointwise operations applied to the same tensor (e.g., multiple Sigmoid branches)

Primary Performance Gain

Eliminates write/read of intermediate tensor to slow global memory (DRAM)

Reduces total number of kernel launches, consolidating parallel work

Compiler Complexity

Moderate; requires dependency analysis and profitability checks for memory-bound sequences

High; must handle potential resource conflicts (e.g., register pressure) and may reduce parallelism if done naively

Impact on Kernel Characteristics

Increases arithmetic intensity of the fused kernel; can become compute-bound

Increases the workload and potential parallelism of a single kernel; remains memory-bound if fusing only light ops

Example in Frameworks

XLA fusing a MatMul with a following bias add and ReLU activation

TVM fusing three independent Sigmoid operations applied to the same input tensor into one kernel

GRAPH FUSION

Frequently Asked Questions

Graph fusion is a critical compiler optimization for high-performance machine learning. This FAQ addresses common technical questions about how it works, its benefits, and its implementation across modern frameworks.

Graph fusion is a compiler optimization that automatically identifies and merges multiple, adjacent computational operators within a neural network's dataflow graph into a single, compound operation executed by a fused kernel. It works by analyzing the computational graph, applying pattern matching and fusion heuristics to find profitable subgraphs (called fusion groups), and then generating or selecting a unified kernel that performs the combined computation, thereby minimizing intermediate memory transfers and kernel launch overhead.

The Fusion Process

  1. Graph Analysis: The compiler (e.g., XLA, TVM, torch.compile) parses the model's computational graph.
  2. Pattern Matching: It identifies known, efficient patterns for fusion, such as Conv -> BatchNorm -> ReLU.
  3. Profitability Analysis: A cost model for fusion estimates the performance impact of merging candidate operators.
  4. Kernel Generation: For the selected fusion groups, the compiler generates a fused kernel (e.g., using CUDA or MLIR) or selects a pre-optimized one (like FlashAttention).
  5. Graph Rewriting: The original graph is rewritten, replacing the subgraph with the new fused operator.
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.