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
| shape | triton (us) | eager (us) | compile (us) | speedup vs eager | triton % peak bw |
|---|---|---|---|---|---|
| M=1, I=8960 | 1.1 | 2.7 | 1.0 | 2.46x | 16% |
| M=16, I=8960 | 4.5 | 7.1 | 4.4 | 1.58x | 64% |
| M=2048, I=8960 | - | 783.6 | 440.1 | - | - |
| M=128, I=8960 | 27.7 | 34.6 | 27.4 | 1.25x | 83% |
| M=1024, I=8960 | 233.0 | 302.1 | 221.7 | 1.30x | 79% |
| M=4096, I=8960 | 932.4 | 1559.0 | 879.3 | 1.67x | 79% |
| M=1, I=18944 | 1.6 | 3.2 | 1.5 | 1.98x | 24% |
| M=16, I=18944 | 8.3 | 11.1 | 8.1 | 1.34x | 73% |
| M=128, I=18944 | 60.6 | 66.7 | 59.8 | 1.10x | 80% |
| M=1024, I=18944 | 486.2 | 830.1 | 492.7 | 1.71x | 80% |
| M=4096, I=18944 | 1942.3 | 3293.3 | 1840.5 | 1.70x | 80% |
Profile on L4
| launch | time (ns) | DRAM % peak | SM % peak | occupancy % | regs/thread |
|---|---|---|---|---|---|
| swiglu_M4096 | 810688 | 93.42 | 5.93 | 88.32 | 29 |
H100
| shape | triton (us) | eager (us) | compile (us) | speedup vs eager | triton % peak bw |
|---|---|---|---|---|---|
| M=1, I=8960 | 1.5 | 3.7 | 1.3 | 2.43x | 1% |
| M=16, I=8960 | 2.1 | 7.5 | 2.1 | 3.54x | 12% |
| M=128, I=8960 | 4.2 | 12.1 | 4.1 | 2.90x | 49% |
| M=1024, I=8960 | 20.9 | 56.5 | 20.4 | 2.70x | 79% |
| M=4096, I=8960 | 77.5 | 209.6 | 74.7 | 2.70x | 85% |
| M=1, I=18944 | 1.9 | 4.0 | 1.9 | 2.16x | 2% |
| M=16, I=18944 | 2.4 | 7.9 | 2.4 | 3.29x | 23% |
| M=128, I=18944 | 6.8 | 18.1 | 6.7 | 2.65x | 64% |
| M=1024, I=18944 | 42.2 | 113.7 | 40.8 | 2.69x | 82% |
| M=4096, I=18944 | 160.7 | 433.7 | 154.2 | 2.70x | 86% |
Profile on H100
| launch | time (ns) | DRAM % peak | SM % peak | occupancy % | regs/thread |
|---|---|---|---|---|---|
| swiglu_M4096 | 72128 | 84.72 | 30.57 | 84.95 | 31 |
Times are CUDA-graph medians over 3 sweeps with rotating buffers (working set at least 4x L2). * = more than 10% spread between sweeps.