Inferensys

Glossary

Horizontal Fusion

Horizontal fusion is a compiler optimization that merges multiple independent, parallel operators sharing the same input tensor into a single kernel to reduce launch overhead and improve memory locality.
Cinematic overhead of a WeWork creative suite room with multiple curved monitors showing AI decision dashboards, executives in casual attire reviewing data, dramatic pendant lighting.
INFERENCE OPTIMIZATION

What is Horizontal Fusion?

Horizontal fusion is a compiler-level optimization technique in deep learning that merges multiple independent, parallel operators that consume the same input tensor into a single, unified computational kernel.

Horizontal fusion combines operators that execute in parallel within a computational graph, sharing a common input but producing separate outputs. Unlike vertical fusion, which chains sequential producer-consumer operations, horizontal fusion targets independent branches. The primary goal is to reduce kernel launch overhead and improve hardware utilization by executing multiple parallel operations in a single GPU kernel launch, thereby amortizing scheduling costs and enhancing data locality.

This optimization is particularly effective for elementwise operations (e.g., multiple parallel activations or tensor manipulations) that are memory-bound. By fusing them, the compiler minimizes global memory accesses and better saturates memory bandwidth. Compilers like XLA, TVM, and MLIR use fusion heuristics and cost models to automatically identify profitable horizontal fusion opportunities, balancing reduced overhead against potential increases in register pressure or decreased parallelism.

COMPILER OPTIMIZATION

Key Characteristics of Horizontal Fusion

Horizontal fusion merges independent, parallel operators that share an input or operate concurrently. Its primary goal is to amortize kernel launch overhead and improve hardware utilization by executing multiple operations in a single GPU kernel launch.

01

Parallel Operator Merging

Horizontal fusion combines independent operators that execute in parallel within the computational graph. Unlike vertical fusion, which chains dependent producer-consumer pairs, horizontal fusion targets operations that have no data dependencies on each other but can be executed simultaneously. For example, applying separate activation functions (e.g., Sigmoid and Tanh) to different channels of the same input tensor can be fused into a single kernel that processes all channels in one pass.

02

Shared Input Consumption

A core enabling condition for horizontal fusion is that the operators being fused consume the same input tensor or slices of the same tensor. This shared data access pattern allows a fused kernel to load the input data once from global GPU memory, perform all parallel computations in-register or in shared memory, and then write the results. This drastically reduces the memory bandwidth pressure that would occur if each operator launched separately and redundantly loaded the same input data.

03

Kernel Launch Overhead Amortization

The primary performance benefit is the amortization of kernel launch overhead. Each GPU kernel launch incurs fixed latency for scheduling, argument passing, and setup. By fusing N parallel operations:

  • Launch overhead is reduced from N times to 1 time.
  • This is particularly impactful for lightweight, elementwise operations (e.g., pointwise activations, scaling) where the launch overhead can be a significant fraction of the total execution time.
  • The fused kernel presents a larger, more substantial workload to the GPU scheduler, improving hardware occupancy and utilization.
04

Improved Data Locality & Cache Utilization

Horizontal fusion enhances data locality by keeping intermediate results within the GPU's fast memory hierarchy. When operators are separate, the output of one must be written to global memory (DRAM) before the next can read it. A fused kernel can:

  • Keep intermediate tensors in registers or shared memory.
  • Promote cache reuse across the fused operations.
  • This reduces the total volume of data transferred through the memory subsystem, which is often the bottleneck for performance.
05

Compiler-Driven Pattern Matching

Horizontal fusion is typically performed automatically by deep learning compilers like XLA, TVM, and PyTorch's Inductor (via torch.compile). These systems use pattern matching and cost models to identify profitable fusion opportunities. The compiler:

  1. Analyzes the computational graph for independent operators with shared inputs.
  2. Estimates the performance benefit using a fusion profitability heuristic.
  3. Generates a single, optimized fused kernel (e.g., via CUDA or ROCm) that implements the combined logic.
06

Trade-offs and Limitations

Fusion is not always profitable. Key trade-offs include:

  • Register Pressure: A fused kernel may require more GPU registers to hold intermediate values, potentially reducing warp occupancy.
  • Control Flow Divergence: Combining unrelated operations can lead to divergent execution paths within a warp, harming performance.
  • Compiler Complexity: Identifying safe and profitable horizontal fusion groups is a complex optimization problem. Over-fusing can create monolithic kernels that are difficult to optimize and may exceed hardware resource limits (e.g., shared memory).
OPERATOR AND KERNEL FUSION

How Horizontal Fusion Works

Horizontal fusion is a compiler optimization that merges independent, parallel operators to enhance computational efficiency.

Horizontal fusion is a graph-level compiler optimization that merges multiple independent operators that consume the same input tensor or execute in parallel within a computational graph. Unlike vertical fusion, which chains dependent operations, horizontal fusion combines sibling nodes, enabling their execution by a single, unified fused kernel. This technique primarily reduces kernel launch overhead and can improve cache utilization by processing multiple data streams concurrently within the same loop structure.

The optimization is driven by a fusion planner using a cost model for fusion to assess fusion profitability. It is particularly effective for memory-bound operations, as it amortizes memory access costs. Common targets include parallel elementwise operations (e.g., multiple independent activations). Compilers like XLA, TVM, and MLIR implement this via pattern matching for fusion and fusion-aware scheduling to generate efficient code for accelerators.

HORIZONTAL FUSION

Implementation Examples

Horizontal fusion merges independent, parallel operators that share an input or execute concurrently. These examples illustrate its application across compilers and neural network architectures.

01

Elementwise Operator Fusion

This is the most common form of horizontal fusion, where multiple pointwise operations applied to the same tensor are combined. For example, a sequence like y = relu(x) + sigmoid(x) * tanh(x) involves three independent reads of tensor x. A fused kernel performs all three computations in a single pass:

  • Single memory load for the input tensor x.
  • Parallel execution of ReLU, Sigmoid, and Tanh functions on each element.
  • Single memory store for the final output y. This eliminates redundant data movement and kernel launch overhead, turning a memory-bound workload into a more compute-efficient one.
02

Parallel Linear Layer Fusion

In transformer blocks, the feed-forward network (FFN) often consists of two parallel linear projections (often an up-projection and a gate projection) whose outputs are combined (e.g., via SiLU activation). Naively, these are two separate matrix multiplications (GEMMs). Horizontal fusion combines them:

  • Single, batched GEMM kernel that computes W_up * x and W_gate * x simultaneously.
  • Shared input x is read once for both operations.
  • Fused activation like SiLU is applied elementwise to the combined results. This pattern is critical in models like LLaMA's SwiGLU FFN, significantly reducing the memory bandwidth required for the input activations.
06

Limitations and Trade-offs

Horizontal fusion is not always profitable. Compilers use cost models to decide when to fuse. Key trade-offs include:

  • Register Pressure: Fusing many operations can increase the number of live variables, exhausting GPU registers and causing spilling to slower memory.
  • Reduced Parallelism: Extremely large fused kernels may leave GPU streaming multiprocessors underutilized if they cannot hide memory latency effectively.
  • Kernel Complexity: Hand-tuning fused kernels (e.g., FlashAttention) is complex, requiring expert knowledge of hardware memory hierarchies.
  • Dynamic Shapes: Fusion decisions made at compile-time (AOT) may be suboptimal for runtime-varying input shapes, favoring Just-In-Time (JIT) fusion approaches.
COMPARISON

Horizontal Fusion vs. Vertical Fusion

A comparison of two primary graph-level optimization strategies for combining computational operators to reduce kernel launch overhead and improve memory locality.

Optimization CharacteristicHorizontal FusionVertical Fusion

Primary Relationship

Parallel / Sibling Operators

Sequential / Producer-Consumer

Data Dependency Pattern

Operators consume the same input tensor(s)

Output of one operator is the input to the next

Primary Performance Goal

Reduce total kernel launches for parallel work

Reduce intermediate memory reads/writes

Typical Fused Pattern

Multiple independent elementwise ops (e.g., Sigmoid, Tanh, ReLU) on the same tensor

Conv → BatchNorm → ReLU (a linear chain)

Impact on Kernel Launch Overhead

High reduction (N parallel ops → 1 launch)

Moderate reduction (N sequential ops → 1 launch)

Impact on Memory Bandwidth

Moderate (single read of shared input)

High (eliminates writes/reads of intermediate tensors)

Compiler Complexity

Lower (independent ops are easier to combine)

Higher (must respect data dependencies and potential side effects)

Potential for Increased Register Pressure

Low to Moderate

High (multiple sequential computations in one kernel)

Example in Frameworks

XLA fusing multiple activations on the same tensor

PyTorch's torch.compile fusing Conv-BN-ReLU blocks

HORIZONTAL FUSION

Frequently Asked Questions

Horizontal fusion is a compiler optimization technique for merging independent, parallel computational operations. This glossary answers common technical questions about its mechanisms, benefits, and implementation.

Horizontal fusion is a compiler optimization that merges multiple independent operators that consume the same input tensor or execute in parallel within a computational graph into a single, unified kernel. Unlike vertical fusion, which chains sequentially dependent producer-consumer operations, horizontal fusion combines operations that are siblings in the dataflow graph, operating on the same or similarly shaped data. The primary goal is to amortize kernel launch overhead, improve data locality by keeping intermediate results in fast cache or registers, and increase arithmetic intensity for better hardware utilization. This technique is a core component of modern deep learning compilers like XLA, TVM, and MLIR.

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.