llm-decode-kernels

Fused SwiGLU

The MLP activation is silu(gate) * up. With a merged gate/up projection the kernel reads both halves of one tensor in place and writes the product in one pass: 3 bytes moved per 2 read, the minimum possible.

Kernel

kernels/swiglu.py: _swiglu_kernel (15 lines, click to show)
@triton.jit
def _swiglu_kernel(
    g_ptr, u_ptr, out_ptr,
    stride_g, stride_u, stride_out,
    I,
    BLOCK: tl.constexpr,
):
    row = tl.program_id(0)
    cols = tl.program_id(1) * BLOCK + tl.arange(0, BLOCK)
    mask = cols < I
    g = tl.load(g_ptr + row * stride_g + cols, mask=mask, other=0.0)
    u = tl.load(u_ptr + row * stride_u + cols, mask=mask, other=0.0)
    gf = g.to(tl.float32)
    act = (gf * tl.sigmoid(gf)).to(g.dtype)  # silu in fp32, rounded like F.silu
    tl.store(out_ptr + row * stride_out + cols, act * u, mask=mask)

L4

swiglu speedup
shapetriton (us)eager (us)compile (us)speedup vs eagertriton % peak bw
M=1, I=89601.12.71.02.46x16%
M=16, I=89604.57.14.41.58x64%
M=2048, I=8960-783.6440.1--
M=128, I=896027.734.627.41.25x83%
M=1024, I=8960233.0302.1221.71.30x79%
M=4096, I=8960932.41559.0879.31.67x79%
M=1, I=189441.63.21.51.98x24%
M=16, I=189448.311.18.11.34x73%
M=128, I=1894460.666.759.81.10x80%
M=1024, I=18944486.2830.1492.71.71x80%
M=4096, I=189441942.33293.31840.51.70x80%

Profile on L4

launchtime (ns)DRAM % peakSM % peakoccupancy %regs/thread
swiglu_M409681068893.425.9388.3229

H100

swiglu speedup
shapetriton (us)eager (us)compile (us)speedup vs eagertriton % peak bw
M=1, I=89601.53.71.32.43x1%
M=16, I=89602.17.52.13.54x12%
M=128, I=89604.212.14.12.90x49%
M=1024, I=896020.956.520.42.70x79%
M=4096, I=896077.5209.674.72.70x85%
M=1, I=189441.94.01.92.16x2%
M=16, I=189442.47.92.43.29x23%
M=128, I=189446.818.16.72.65x64%
M=1024, I=1894442.2113.740.82.69x82%
M=4096, I=18944160.7433.7154.22.70x86%

Profile on H100

launchtime (ns)DRAM % peakSM % peakoccupancy %regs/thread
swiglu_M40967212884.7230.5784.9531

Times are CUDA-graph medians over 3 sweeps with rotating buffers (working set at least 4x L2). * = more than 10% spread between sweeps.