- 3.7× faster inference on the Galerkin Transformer, from fusion alone
- 1.71× on a Transformer with MXINT8 weights, on an RTX 3070
- 1.14× on F-FNO, end to end
Many operations in a neural network are limited by memory bandwidth rather than arithmetic: each one reads its inputs from GPU memory and writes its result back, only for the next operation to read it again. Fusing a chain of operations into one kernel keeps the intermediate values on chip. AutoFuser does this automatically for a PyTorch model: it finds fusable chains in the model’s graph, generates a Triton kernel for each, tunes it for the actual input shapes, and rewrites the model to call the fused kernels.
One of its eight blocks
27 operations. Eager PyTorch runs them one at a time, so every intermediate result makes a round trip through GPU memory.
AutoFuser finds 2 chains it can fuse, and skips 3 more because its inputs include tensor sizes read at run time, which the code generator has no metadata for.
2 generated Triton kernels replace 6 operations, so 27 become 23. Select a fused kernel to see its code. Across the whole model, 17 kernels replace 51 operations. The fused model ran 3.72× faster end to end.
add → dropout
import triton
import triton.language as tl
@triton.jit
def fused_fused_add_4_to_dropout_1(in_ptr0, in_ptr1, out_ptr0, n_elements, BLOCK_SIZE: tl.constexpr):
pid = tl.program_id(0)
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
inp0 = tl.load(in_ptr0 + offsets, mask=mask)
inp1 = tl.load(in_ptr1 + offsets, mask=mask)
t0 = inp1 + inp0
# dropout 'dropout_1' (eval → identity)
t0 = t0
tl.store(out_ptr0 + offsets, t0, mask=mask) layer_norm → linear → gelu → linear
import triton
import triton.language as tl
@triton.jit
def _fused_chain_aac86fab(
x_ptr,
x_stride,
lnw_layer_norm_5_ptr,
lnb_layer_norm_5_ptr,
lnw_linear_12_ptr,
lnb_linear_12_ptr,
lnw_linear_13_ptr,
lnb_linear_13_ptr,
eps_layer_norm_5,
out_ptr,
out_stride,
n_rows,
d_in,
d_out,
BLOCK_M: tl.constexpr,
BLOCK_D_0: tl.constexpr,
BLOCK_N_linear_12: tl.constexpr,
BLOCK_N_linear_13: tl.constexpr
):
pid = tl.program_id(0)
pid_n = tl.program_id(1) # ← new
row_offs = pid * BLOCK_M + tl.arange(0, BLOCK_M)
row_mask = row_offs < n_rows
x = tl.load(
x_ptr + row_offs[:, None] * x_stride + tl.arange(0, BLOCK_D_0)[None, :],
mask=row_mask[:, None] & (tl.arange(0, BLOCK_D_0) < d_in)[None, :],
other=0.0,
).to(tl.float32)
# layer_norm 'layer_norm_5' d=128 BLOCK=BLOCK_D_0
_mean_t0 = tl.sum(x, axis=1)[:, None] / 128
_xc_t0 = x - _mean_t0
_var_t0 = tl.sum(_xc_t0 * _xc_t0, axis=1)[:, None] / 128
_inv_t0 = tl.rsqrt(_var_t0 + eps_layer_norm_5)
_lnw_t0 = tl.load(lnw_layer_norm_5_ptr + tl.arange(0, BLOCK_D_0))
_lnb_t0 = tl.load(lnb_layer_norm_5_ptr + tl.arange(0, BLOCK_D_0))
t0 = _xc_t0 * _inv_t0 * _lnw_t0[None, :] + _lnb_t0[None, :]
# linear 'linear_12' [128 -> 128]
_n_offs_t1 = tl.arange(0, BLOCK_N_linear_12)
_n_mask_t1 = _n_offs_t1 < 128
_k_offs_t1 = tl.arange(0, BLOCK_D_0)
_k_mask_t1 = _k_offs_t1 < 128
_w_t1 = tl.load(lnw_linear_12_ptr + _k_offs_t1[:, None] * 128 + _n_offs_t1[None, :], mask=_k_mask_t1[:, None] & _n_mask_t1[None, :], other=0.0, eviction_policy='evict_last')
_acc_t1 = tl.zeros([BLOCK_M, BLOCK_N_linear_12], dtype=tl.float32)
_acc_t1 += tl.dot(t0, _w_t1, out_dtype=tl.float32, input_precision="tf32")
t1 = _acc_t1 # [BLOCK_M, BLOCK_N_linear_12]
t1 = t1 + tl.load(lnb_linear_12_ptr + _n_offs_t1, mask=_n_mask_t1, other=0.0)[None, :]
t2 = t1 * 0.5 * (1.0 + tl.math.erf(t1 * 0.7071067811865476))
# linear 'linear_13' [128 -> 128]
_n_offs_t3 = pid_n * BLOCK_N_linear_13 + tl.arange(0, BLOCK_N_linear_13)
_n_mask_t3 = _n_offs_t3 < 128
_k_offs_t3 = tl.arange(0, BLOCK_N_linear_12)
_k_mask_t3 = _k_offs_t3 < 128
_w_t3 = tl.load(lnw_linear_13_ptr + _k_offs_t3[:, None] * 128 + _n_offs_t3[None, :], mask=_k_mask_t3[:, None] & _n_mask_t3[None, :], other=0.0, eviction_policy='evict_last')
_acc_t3 = tl.zeros([BLOCK_M, BLOCK_N_linear_13], dtype=tl.float32)
_acc_t3 += tl.dot(t2, _w_t3, out_dtype=tl.float32, input_precision="tf32")
t3 = _acc_t3 # [BLOCK_M, BLOCK_N_linear_13]
t3 = t3 + tl.load(lnb_linear_13_ptr + _n_offs_t3, mask=_n_mask_t3, other=0.0)[None, :]
_col_offs_out = pid_n * BLOCK_N_linear_13 + tl.arange(0, BLOCK_N_linear_13)
tl.store(
out_ptr + row_offs[:, None] * out_stride + _col_offs_out[None, :],
t3,
mask=row_mask[:, None] & (_col_offs_out < d_out)[None, :],
) One of its eight blocks
17 operations. Eager PyTorch runs them one at a time, so every intermediate result makes a round trip through GPU memory.
AutoFuser finds 2 chains it can fuse.
2 generated Triton kernels replace 6 operations, so 17 become 13. Select a fused kernel to see its code. Across the whole model, 17 kernels replace 51 operations.
linear → dropout
import triton
import triton.language as tl
@triton.jit
def _fused_chain_4fa14f78(
x_ptr,
x_stride,
lnw_linear_11_ptr,
lnb_linear_11_ptr,
out_ptr,
out_stride,
n_rows,
d_in,
d_out,
BLOCK_M: tl.constexpr,
BLOCK_D_0: tl.constexpr,
BLOCK_K_linear_11: tl.constexpr,
BLOCK_N_linear_11: tl.constexpr
):
pid = tl.program_id(0)
pid_n = tl.program_id(1) # ← new
row_offs = pid * BLOCK_M + tl.arange(0, BLOCK_M)
row_mask = row_offs < n_rows
x = tl.load(
x_ptr + row_offs[:, None] * x_stride + tl.arange(0, BLOCK_D_0)[None, :],
mask=row_mask[:, None] & (tl.arange(0, BLOCK_D_0) < d_in)[None, :],
other=0.0,
).to(tl.float32)
# linear 'linear_11' [128 -> 128]
_n_offs_t0 = pid_n * BLOCK_N_linear_11 + tl.arange(0, BLOCK_N_linear_11)
_n_mask_t0 = _n_offs_t0 < 128
_acc_t0 = tl.zeros([BLOCK_M, BLOCK_N_linear_11], dtype=tl.float32)
for _ks_t0 in range(0, 128, BLOCK_K_linear_11):
_k_offs_t0 = _ks_t0 + tl.arange(0, BLOCK_K_linear_11)
_k_mask_t0 = _k_offs_t0 < 128
_xt_t0 = tl.load(x_ptr + row_offs[:, None] * x_stride + _k_offs_t0[None, :], mask=row_mask[:, None] & _k_mask_t0[None, :], other=0.0).to(tl.float32)
_wt_t0 = tl.load(lnw_linear_11_ptr + _k_offs_t0[:, None] * 128 + _n_offs_t0[None, :], mask=_k_mask_t0[:, None] & _n_mask_t0[None, :], other=0.0, eviction_policy='evict_last')
_acc_t0 += tl.dot(_xt_t0, _wt_t0, out_dtype=tl.float32, input_precision="tf32")
t0 = _acc_t0
t0 = t0 + tl.load(lnb_linear_11_ptr + _n_offs_t0, mask=_n_mask_t0, other=0.0)[None, :]
# dropout 'dropout_1' (eval → identity)
t1 = t0
_col_offs_out = pid_n * BLOCK_N_linear_11 + tl.arange(0, BLOCK_N_linear_11)
tl.store(
out_ptr + row_offs[:, None] * out_stride + _col_offs_out[None, :],
t1,
mask=row_mask[:, None] & (_col_offs_out < d_out)[None, :],
) layer_norm → linear → gelu → linear
import triton
import triton.language as tl
@triton.jit
def _fused_chain_5f387e2d(
x_ptr,
x_stride,
lnw_layer_norm_3_ptr,
lnb_layer_norm_3_ptr,
lnw_linear_12_ptr,
lnb_linear_12_ptr,
lnw_linear_13_ptr,
lnb_linear_13_ptr,
eps_layer_norm_3,
out_ptr,
out_stride,
n_rows,
d_in,
d_out,
BLOCK_M: tl.constexpr,
BLOCK_D_0: tl.constexpr,
BLOCK_N_linear_12: tl.constexpr,
BLOCK_N_linear_13: tl.constexpr
):
pid = tl.program_id(0)
pid_n = tl.program_id(1) # ← new
row_offs = pid * BLOCK_M + tl.arange(0, BLOCK_M)
row_mask = row_offs < n_rows
x = tl.load(
x_ptr + row_offs[:, None] * x_stride + tl.arange(0, BLOCK_D_0)[None, :],
mask=row_mask[:, None] & (tl.arange(0, BLOCK_D_0) < d_in)[None, :],
other=0.0,
).to(tl.float32)
# layer_norm 'layer_norm_3' d=128 BLOCK=BLOCK_D_0
_mean_t0 = tl.sum(x, axis=1)[:, None] / 128
_xc_t0 = x - _mean_t0
_var_t0 = tl.sum(_xc_t0 * _xc_t0, axis=1)[:, None] / 128
_inv_t0 = tl.rsqrt(_var_t0 + eps_layer_norm_3)
_lnw_t0 = tl.load(lnw_layer_norm_3_ptr + tl.arange(0, BLOCK_D_0))
_lnb_t0 = tl.load(lnb_layer_norm_3_ptr + tl.arange(0, BLOCK_D_0))
t0 = _xc_t0 * _inv_t0 * _lnw_t0[None, :] + _lnb_t0[None, :]
# linear 'linear_12' [128 -> 128]
_n_offs_t1 = tl.arange(0, BLOCK_N_linear_12)
_n_mask_t1 = _n_offs_t1 < 128
_k_offs_t1 = tl.arange(0, BLOCK_D_0)
_k_mask_t1 = _k_offs_t1 < 128
_w_t1 = tl.load(lnw_linear_12_ptr + _k_offs_t1[:, None] * 128 + _n_offs_t1[None, :], mask=_k_mask_t1[:, None] & _n_mask_t1[None, :], other=0.0, eviction_policy='evict_last')
_acc_t1 = tl.zeros([BLOCK_M, BLOCK_N_linear_12], dtype=tl.float32)
_acc_t1 += tl.dot(t0, _w_t1, out_dtype=tl.float32, input_precision="tf32")
t1 = _acc_t1 # [BLOCK_M, BLOCK_N_linear_12]
t1 = t1 + tl.load(lnb_linear_12_ptr + _n_offs_t1, mask=_n_mask_t1, other=0.0)[None, :]
t2 = t1 * 0.5 * (1.0 + tl.math.erf(t1 * 0.7071067811865476))
# linear 'linear_13' [128 -> 128]
_n_offs_t3 = pid_n * BLOCK_N_linear_13 + tl.arange(0, BLOCK_N_linear_13)
_n_mask_t3 = _n_offs_t3 < 128
_k_offs_t3 = tl.arange(0, BLOCK_N_linear_12)
_k_mask_t3 = _k_offs_t3 < 128
_w_t3 = tl.load(lnw_linear_13_ptr + _k_offs_t3[:, None] * 128 + _n_offs_t3[None, :], mask=_k_mask_t3[:, None] & _n_mask_t3[None, :], other=0.0, eviction_policy='evict_last')
_acc_t3 = tl.zeros([BLOCK_M, BLOCK_N_linear_13], dtype=tl.float32)
_acc_t3 += tl.dot(t2, _w_t3, out_dtype=tl.float32, input_precision="tf32")
t3 = _acc_t3 # [BLOCK_M, BLOCK_N_linear_13]
t3 = t3 + tl.load(lnb_linear_13_ptr + _n_offs_t3, mask=_n_mask_t3, other=0.0)[None, :]
_col_offs_out = pid_n * BLOCK_N_linear_13 + tl.arange(0, BLOCK_N_linear_13)
tl.store(
out_ptr + row_offs[:, None] * out_stride + _col_offs_out[None, :],
t3,
mask=row_mask[:, None] & (_col_offs_out < d_out)[None, :],
) The whole model
29 operations. Eager PyTorch runs them one at a time, so every intermediate result makes a round trip through GPU memory.
AutoFuser finds 2 chains it can fuse.
2 generated Triton kernels replace 6 operations, so 29 become 25. Select a fused kernel to see its code. Across the whole model, 2 kernels replace 6 operations. The fused model ran 1.14× faster end to end.
linear → gelu → linear
import triton
import triton.language as tl
@triton.jit
def _fused_chain_20315cf5(
x_ptr,
x_stride,
lnw_linear_ptr,
lnb_linear_ptr,
lnw_linear_1_ptr,
lnb_linear_1_ptr,
out_ptr,
out_stride,
n_rows,
d_in,
d_out,
BLOCK_M: tl.constexpr,
BLOCK_D_0: tl.constexpr,
BLOCK_K_linear: tl.constexpr,
BLOCK_N_linear: tl.constexpr,
BLOCK_N_linear_1: tl.constexpr
):
pid = tl.program_id(0)
pid_n = tl.program_id(1) # ← new
row_offs = pid * BLOCK_M + tl.arange(0, BLOCK_M)
row_mask = row_offs < n_rows
x = tl.load(
x_ptr + row_offs[:, None] * x_stride + tl.arange(0, BLOCK_D_0)[None, :],
mask=row_mask[:, None] & (tl.arange(0, BLOCK_D_0) < d_in)[None, :],
other=0.0,
).to(tl.float32)
# linear 'linear' [2 -> 64]
_n_offs_t0 = tl.arange(0, BLOCK_N_linear)
_n_mask_t0 = _n_offs_t0 < 64
_acc_t0 = tl.zeros([BLOCK_M, BLOCK_N_linear], dtype=tl.float32)
for _ks_t0 in range(0, 2, BLOCK_K_linear):
_k_offs_t0 = _ks_t0 + tl.arange(0, BLOCK_K_linear)
_k_mask_t0 = _k_offs_t0 < 2
_xt_t0 = tl.load(x_ptr + row_offs[:, None] * x_stride + _k_offs_t0[None, :], mask=row_mask[:, None] & _k_mask_t0[None, :], other=0.0).to(tl.float32)
_wt_t0 = tl.load(lnw_linear_ptr + _k_offs_t0[:, None] * 64 + _n_offs_t0[None, :], mask=_k_mask_t0[:, None] & _n_mask_t0[None, :], other=0.0, eviction_policy='evict_last')
_acc_t0 += tl.dot(_xt_t0, _wt_t0, out_dtype=tl.float32, input_precision="tf32")
t0 = _acc_t0
t0 = t0 + tl.load(lnb_linear_ptr + _n_offs_t0, mask=_n_mask_t0, other=0.0)[None, :]
t1 = t0 * 0.5 * (1.0 + tl.math.erf(t0 * 0.7071067811865476))
# linear 'linear_1' [64 -> 32]
_n_offs_t2 = pid_n * BLOCK_N_linear_1 + tl.arange(0, BLOCK_N_linear_1)
_n_mask_t2 = _n_offs_t2 < 32
_k_offs_t2 = tl.arange(0, BLOCK_N_linear)
_k_mask_t2 = _k_offs_t2 < 64
_w_t2 = tl.load(lnw_linear_1_ptr + _k_offs_t2[:, None] * 32 + _n_offs_t2[None, :], mask=_k_mask_t2[:, None] & _n_mask_t2[None, :], other=0.0, eviction_policy='evict_last')
_acc_t2 = tl.zeros([BLOCK_M, BLOCK_N_linear_1], dtype=tl.float32)
_acc_t2 += tl.dot(t1, _w_t2, out_dtype=tl.float32, input_precision="tf32")
t2 = _acc_t2 # [BLOCK_M, BLOCK_N_linear_1]
t2 = t2 + tl.load(lnb_linear_1_ptr + _n_offs_t2, mask=_n_mask_t2, other=0.0)[None, :]
_col_offs_out = pid_n * BLOCK_N_linear_1 + tl.arange(0, BLOCK_N_linear_1)
tl.store(
out_ptr + row_offs[:, None] * out_stride + _col_offs_out[None, :],
t2,
mask=row_mask[:, None] & (_col_offs_out < d_out)[None, :],
) linear → gelu → linear
import triton
import triton.language as tl
@triton.jit
def _fused_chain_ca7b8bfe(
x_ptr,
x_stride,
lnw_linear_2_ptr,
lnb_linear_2_ptr,
lnw_linear_3_ptr,
lnb_linear_3_ptr,
out_ptr,
out_stride,
n_rows,
d_in,
d_out,
BLOCK_M: tl.constexpr,
BLOCK_D_0: tl.constexpr,
BLOCK_K_linear_2: tl.constexpr,
BLOCK_N_linear_2: tl.constexpr,
BLOCK_N_linear_3: tl.constexpr
):
pid = tl.program_id(0)
pid_n = tl.program_id(1) # ← new
row_offs = pid * BLOCK_M + tl.arange(0, BLOCK_M)
row_mask = row_offs < n_rows
x = tl.load(
x_ptr + row_offs[:, None] * x_stride + tl.arange(0, BLOCK_D_0)[None, :],
mask=row_mask[:, None] & (tl.arange(0, BLOCK_D_0) < d_in)[None, :],
other=0.0,
).to(tl.float32)
# linear 'linear_2' [32 -> 32]
_n_offs_t0 = tl.arange(0, BLOCK_N_linear_2)
_n_mask_t0 = _n_offs_t0 < 32
_acc_t0 = tl.zeros([BLOCK_M, BLOCK_N_linear_2], dtype=tl.float32)
for _ks_t0 in range(0, 32, BLOCK_K_linear_2):
_k_offs_t0 = _ks_t0 + tl.arange(0, BLOCK_K_linear_2)
_k_mask_t0 = _k_offs_t0 < 32
_xt_t0 = tl.load(x_ptr + row_offs[:, None] * x_stride + _k_offs_t0[None, :], mask=row_mask[:, None] & _k_mask_t0[None, :], other=0.0).to(tl.float32)
_wt_t0 = tl.load(lnw_linear_2_ptr + _k_offs_t0[:, None] * 32 + _n_offs_t0[None, :], mask=_k_mask_t0[:, None] & _n_mask_t0[None, :], other=0.0, eviction_policy='evict_last')
_acc_t0 += tl.dot(_xt_t0, _wt_t0, out_dtype=tl.float32, input_precision="tf32")
t0 = _acc_t0
t0 = t0 + tl.load(lnb_linear_2_ptr + _n_offs_t0, mask=_n_mask_t0, other=0.0)[None, :]
t1 = t0 * 0.5 * (1.0 + tl.math.erf(t0 * 0.7071067811865476))
# linear 'linear_3' [32 -> 1]
_n_offs_t2 = pid_n * BLOCK_N_linear_3 + tl.arange(0, BLOCK_N_linear_3)
_n_mask_t2 = _n_offs_t2 < 1
_k_offs_t2 = tl.arange(0, BLOCK_N_linear_2)
_k_mask_t2 = _k_offs_t2 < 32
_w_t2 = tl.load(lnw_linear_3_ptr + _k_offs_t2[:, None] * 1 + _n_offs_t2[None, :], mask=_k_mask_t2[:, None] & _n_mask_t2[None, :], other=0.0, eviction_policy='evict_last')
_acc_t2 = tl.zeros([BLOCK_M, BLOCK_N_linear_3], dtype=tl.float32)
_acc_t2 += tl.dot(t1, _w_t2, out_dtype=tl.float32, input_precision="tf32")
t2 = _acc_t2 # [BLOCK_M, BLOCK_N_linear_3]
t2 = t2 + tl.load(lnb_linear_3_ptr + _n_offs_t2, mask=_n_mask_t2, other=0.0)[None, :]
_col_offs_out = pid_n * BLOCK_N_linear_3 + tl.arange(0, BLOCK_N_linear_3)
tl.store(
out_ptr + row_offs[:, None] * out_stride + _col_offs_out[None, :],
t2,
mask=row_mask[:, None] & (_col_offs_out < d_out)[None, :],
) I proposed the project and its pipeline for the Advanced Deep Learning Systems course at Imperial, and built it with Bon Tang, Bruno Duaso and Oliver Hannibal. We used Imperial’s MASE framework for the graph representation and evaluated on scientific machine learning models such as neural operators and transformer-based PDE solvers.
Exposing the whole graph
Models are built from nested modules, and a fusion opportunity often spans a module boundary. I wrote the pass that recursively traces each submodule and splices it into the parent graph, so the fusion analysis sees individual operations instead of opaque module calls. Submodules that cannot be traced (FFT-based layers, for example) are left intact. I also patched einops’ rearrange, repeat and reduce so that models using them could be traced at all.
Generating kernels
Before the pipeline could generate anything, I wrote fused Triton kernels by hand for the operations we cared about (an MLP, layer normalisation, a convolution and an upsampling block) to give the generated code a target. I then wrote the code generator for chains that contain reductions and matrix multiplications: it lowers a chain of linear layers, layer norms, softmax and MLP blocks into a single row-tiled Triton kernel, with its own block sizes for each operation. On the transformer model, the automatically fused kernels matched the performance of my hand-written ones. I also wrote the dispatch layer and most of the per-operation lowering handlers that the code generators share.
Tiling and autotuning
Each kind of chain gets a tiling strategy that defines its tunable block sizes, its launch grid and which configurations are valid: elementwise, row-wise reductions, layer norm, matrix multiply, linear plus activation, layer norm plus linear, and full MLP blocks. The autotuner takes the generated kernel source as a string, loads it into a temporary module, generates candidate configurations from the strategy, and uses Triton’s own benchmarking to pick the fastest. Results are cached on disk, so a model only pays for tuning once.
Putting it together
I wrote the graph rewrite that replaces each fused chain with a single call, checking that fusion groups do not overlap, handling kernels with several outputs, and removing the nodes that are no longer used. The orchestrator ties the stages together behind one call: the model is compiled lazily on its first forward pass, so tuning happens with real input shapes, and it reports which candidates were fused and which were skipped. Finally, I wrote the benchmark harness that times each model in eager mode and with AutoFuser, and checks the fused model’s outputs against the original.
Bruno built the loading of the scientific models into MASE and many of the operation handlers, Bon Tang the original fusion specification and a separate 3D-tiling code generator, and Oliver the MXINT8 weight quantisation and parts of the elementwise code generation.
Results
We benchmarked eight models from the Neural-Solver-Library on its Airfoil dataset. AutoFuser made seven of them faster, and the change in test loss stayed small throughout. The largest gain was on the Galerkin Transformer, from 33.9 ms to 9.1 ms per inference, a 3.72× speedup. F-FNO ran 1.14× faster, and with MXINT8 weights, a Transformer on an RTX 3070 ran 1.71× faster.