What is AI kernel fusion?
Kernel fusion is a compiler and software technique that merges consecutive GPU operations into one kernel, so intermediate results are not written to main memory and read back. When several operations apply to the same input, it is loaded once rather than once per operation.
Unlike quantisation, it leaves the model's numbers unchanged and performs the same computation with fewer memory accesses. FlashAttention, for example, fuses all attention operations into one GPU kernel and computes exact attention.
- Topic
- Infrastructure and runtime
- Also known as
- Operator fusion, operation fusion
- Origin
- GPU computing; 2013 study on fusing linear algebra kernels
How AI kernel fusion works
- A compiler or developer finds consecutive operations sharing data, such as addition, activation and normalisation.
- They are merged into one kernel.
- Intermediate results stay in registers or shared memory instead of high bandwidth memory (HBM).
- Kernel launches and memory traffic fall, speeding up memory-bound operations most.
In more detail
The gap between compute and memory
Modern GPUs can perform far more arithmetic operations in the time it takes to move a single word to or from main memory. Chains of small operations therefore leave compute units idle while they wait on memory; fusion shortens that wait. For compute-bound operations such as matrix multiplication with a large inner dimension, where arithmetic sets the time, gains are smaller.
Commonly confused terms
| Term | How it differs |
|---|---|
| Batching | Batching processes several requests in the same computation to use the GPU more efficiently. Kernel fusion merges consecutive operations within one computation to cut memory traffic; the two can be combined. |
Why AI kernel fusion matters for on-premises AI
In memory-bound operations, time depends on memory accesses rather than computation; activation, dropout, softmax and layer normalisation fall here. A 2013 study found many GPU kernels limited by memory bandwidth and made sequences of linear algebra routines up to 2.61 times faster through fusion. An open-source machine learning compiler's documentation calls fusion its most important optimisation. In training, intermediate values still go to memory for the backward pass, limiting naive fusion.
Profiling its in-house language model, an inference team sees layer normalisation and activation running as separate kernels that wait on GPU memory. It moves to an inference engine with fusion support, remeasures latency and checks on its own test set that outputs are unchanged.
Common questions
Does kernel fusion affect model accuracy?
As the computation stays the same, as a rule it does not. If the order of operations changes, tiny floating point rounding differences can appear.
Who performs fusion?
Compilers can fuse many elementwise operations automatically. For complex computations such as attention, hand-written fused kernels are used.
Related terms
- AI graphics processing unit (GPU)
- FlashAttention
- AI memory-bound workload
- AI inference engine
- AI graphics processing unit (GPU) memory
Sources
- Optimizing CUDA Code By Kernel Fusion: Application on BLAS, arXiv, 2013
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness, arXiv, 2022
- XLA:GPU Architecture Overview, OpenXLA Project
In the glossary: Glossary, E to L