← Back to all blogs

CUDA Megakernel: Fusing 24 Layers into a Single Dispatch


Kernel fusion is the dark art of AI optimization. Every time the GPU has to stop, write data back to global memory, and wait for the CPU to dispatch the next kernel, you lose precious microseconds.

In a standard transformer architecture, a single forward pass might invoke thousands of separate CUDA kernels.

The Megakernel Approach

Instead of dispatching layer by layer, what if we wrote a single massive kernel that handled the entire network?

// A simplified example of the megakernel wrapper
extern "C" __global__ void megakernel_forward(
    float* __restrict__ q,
    float* __restrict__ k,
    float* __restrict__ v,
    float* __restrict__ out,
    const int seq_len,
    const int hidden_dim
) {
    // Thread block setup
    const int tid = threadIdx.x;
    const int bid = blockIdx.x;
    
    // Load weights into shared memory once
    __shared__ float smem_weights[MAX_DIM];
    
    // Execute all 24 layers without returning to global memory
    #pragma unroll
    for (int layer = 0; layer < 24; ++layer) {
        compute_attention(q, k, v, smem_weights, tid);
        __syncthreads();
        compute_ffn(out, smem_weights, tid);
        __syncthreads();
    }
}

The Results

By keeping the data in L1/Shared memory and executing the entire network in one dispatch, we increased throughput by 1.8x and dropped power consumption significantly, matching the token-per-joule efficiency of Apple’s M-series chips.