What is Operator fusion?

An optimization that combines several operations into one kernel, avoiding writing intermediate results to memory and reading them back.

Field: AI Hardware & Software Stack

Japanese name: 演算の融合

Why fusion helps

In neural-network computation, a matrix multiplication is often followed by elementwise operations such as adding a bias, an activation function and normalization. Running these as separate kernels means each kernel writes its intermediate result to memory such as HBM and the next kernel reads it back. Elementwise operations do little computation per element and have low arithmetic intensity, so most of their time goes to memory reads and writes. Operator fusion is the optimization that combines them into one kernel and keeps intermediate results in registers or on-chip memory while computation continues. The amount of computation is unchanged, but memory traffic and the number of kernel launches drop.

Common fusion patterns

The most common pattern fuses chains of elementwise operations, processing a run of additions, multiplications and activation functions with a single read and write. Another typical pattern, epilogue fusion, applies the bias addition and activation function to the output of a matrix multiplication or convolution before it is written out. Operations that include reductions, computing sums or maxima, such as softmax and layer normalization, can also be packed into one kernel. FlashAttention, which fuses a matrix multiplication, softmax and a second matrix multiplication tile by tile in the attention computation so that the large attention matrix is never written to memory, is an example where fusion pays off strongly.

Automatic fusion by compilers

Besides people writing dedicated kernels by hand, fusion is done automatically by AI compilers and inference runtimes. XLA, TVM, PyTorch's compilation feature and TensorRT analyze the model as a computation graph, find groups of operations that can be combined, and generate kernels suited to the target GPU or NPU. Inference does not need to keep intermediate results for backpropagation as training does, so it can fuse more aggressively. How far to fuse depends on tensor shapes, memory layout and the registers and on-chip memory of the target hardware, and the compiler estimates these to choose the scope of fusion.

Limits and caveats

Fusion has its limits. Packing many operations into one kernel increases its use of registers and on-chip memory, which can reduce the number of threads that run concurrently and actually slow it down. Operations with different data layouts or parallelization schemes can be hard to combine efficiently. In training, intermediate results are needed for backpropagation, so one must choose between keeping them in memory and recomputing them during the backward pass. And when fusion changes the order of computation or the number of roundings, floating-point results can differ slightly from the unfused version, which needs care when validating results against each other.

Try it: questions about Operator fusion

What is the main purpose of operator fusion in AI computation?

  1. ATo combine several operations into one kernel, avoiding writing intermediate results to memory and reading them back
  2. BTo merge two AI models into one for higher accuracy
  3. CTo physically fuse arithmetic units together
Show answer and explanation

Answer: A. To combine several operations into one kernel, avoiding writing intermediate results to memory and reading them back

A matrix product is often followed by element-wise operations such as adding a bias, an activation function and normalization. Doing each in a separate kernel means writing intermediate results to memory and reading them back every time, which takes longer than the computation. Combining them into one kernel lets the intermediate values stay in registers or on-chip memory throughout.

FAQ

What is operator fusion?

It is an optimization that combines several operations into one kernel, avoiding writing intermediate results to memory and reading them back. For example, doing a matrix multiplication, bias addition and ReLU in one kernel means only the final result has to be written to memory.

What does operator fusion reduce?

Mainly the amount of memory traffic and the number of kernel launches. The amount of computation, such as multiplications, is unchanged. Elementwise operations are often limited by memory bandwidth, so cutting reads and writes greatly shortens execution time.

Who performs operator fusion?

Typically AI compilers and inference runtimes such as XLA, TVM, PyTorch's compilation feature and TensorRT do it automatically by analyzing the computation graph. For performance-critical parts, people also write fused kernels by hand, as with FlashAttention.

Questions that mention Operator fusion (1)

Operator fusion appears in 2 practice questions across 2 topics, including AI Hardware & Software Stack, Performance Analysis.

  1. When the Roofline model shows a workload in the memory-bandwidth-limited region, which direction improves performance?

Books on AI Hardware & Software Stack Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary