# Kernel optimization

Kernel optimization and H100s in pictures. Matrix multiply and fused softmax.

By Suhaas. Published December 5, 2026. Source: https://www.cerebellumlabs.ai/blog/kernel-optimization/

import { Coalescing, FusedSoftmax, H100Map, MatmulTiling, MemoryLadder, NextToken, VectorAdd } from "deep-learning-visuals";

I'm post-training a custom model for kernel optimization. In one experiment I trained on test-time refinements. I tried checking the quality of refinements but realized I was completely ill-equipped to do so. I made some visualizations to help me wrap my mind around a lot of this stuff. I'll use as few words as possible.

## GPU (H100)

<H100Map />

All the parts in an H100 do one of two main things: (1) store memory (2) do arithmetic. Tensor cores and CUDA cores do arithmetic, everything else is memory. In order to compute large operations quickly (like computing the next token), memory needs to be moved from memory stores to the cores as efficiently as possible. That is what kernels are for. A summary of the parts:

- **High Bandwidth Memory (HBM3):** the GPU's main memory, 80 GB. Data moves in and out at up to 3.35 TB/s.
- **L2 cache:** 50 MB of the most recently used HBM data. Managed by hardware.
- **Streaming Multiprocessor (SM):** like a mini GPU. Contains CUDA cores, tensor cores, and shared memory. *132 total, each with 128 CUDA cores, 4 tensor cores, and up to 227 KB of shared memory.*
- **Shared memory:** the memory of an SM. Can be used by any thread in the same block.
- **Register:** values a thread is working on right now. Up to 255 per thread.
- **Tensor core:** multiplies small matrices and does nothing else. *528 total, 989 TFLOPS at 16-bit.*
- **CUDA core:** does one ordinary operation at a time (add, multiply, exp, compare), so it runs everything that is not a matrix multiply. *16,896 total, 67 TFLOPS at 32-bit.*
- **Warp:** the GPU runs threads in packs of 32 called warps. All 32 do the same instruction at the same moment.

<MemoryLadder caption="Levels of memory. Sizes are NVIDIA's for the H100 SXM." />

The smaller and closer to the cores a memory store is, the faster it is to move data to the cores.

- **HBM3 to an SM:** 3.35 TB/s (slow)
- **Shared memory to registers:** about 33 TB/s (fast)

## Kernel

A very simple kernel: adding two arrays, then multiplying by a number.

<VectorAdd
  notes={[
    "Your program wants c = (a + b) × k, for two arrays of 16 numbers that are already in the GPU's memory. The CPU does not do the arithmetic. It launches kernels, and for each one asks for 16 threads: 4 blocks of 4. (Real launches use far bigger numbers; these are small so they fit in the picture.)",
    "A kernel is the whole job written for one thread: work out which element is mine, do the arithmetic for that one element, store the result. All 16 threads run exactly these lines. The only thing that differs between them is their position.",
    "Take thread 1 of block 2 in the add kernel. Its position gives i = 2 × 4 + 1 = 9, so it loads a[9] and b[9] from HBM into its registers, a core adds them, and it writes the sum to t[9]. It never looks at any other element.",
    "The naive way is one kernel per operation. The add kernel writes every sum out to HBM as t. Then the mul kernel reads t back in, multiplies by k, and writes c. Each element makes 3 trips out of HBM and 2 trips back.",
    "One kernel does both. Each thread keeps its sum in a register and multiplies it right there, so t never exists in HBM. This results in 2 fewer trips to HBM. This is a pattern we will see repeatedly in kernel optimization. Reuse a piece of data as much as possible to reduce trips to HBM.",
  ]}
/>

Now the realer example of generating next token during inference. Every row is a kernel the CPU launches.

<NextToken />

A model launches hundreds of kernels for one token.

## Matrix Multiplication

These steps follow Simon Boehm's [walkthrough](https://siboehm.com/articles/22/CUDA-MMM). The first one changes no arithmetic at all, only which thread computes which element.

Recall that threads run in warps: packs of 32 that do the same instruction at the same moment.

<Coalescing
  notes={[
    "Neighbouring threads compute elements one below the other in C. At each step of the dot product they all want the same element of B, and eight elements of A that sit a whole row apart in memory. Every load is its own transaction.",
    "Change two lines so neighbouring threads compute elements side by side in C instead. Now they share one element of A and want eight elements of B that sit side by side, and the hardware fetches those in a single transaction.",
  ]}
/>

On his A6000, that change alone took memory throughput from 15 GB/s to 110 GB/s.

The rest is the same idea each time: fetch data from far away once, then reuse it as many times as possible from somewhere close.

<MatmulTiling
  notes={[
    "Every thread computes one element of C and loads every operand from HBM itself: two loads per multiply, K multiplies per element.",
    "The block copies a tile of A and a tile of B into shared memory once, and all its threads work from that copy. Trips to HBM drop 32 times; the loads move to shared memory, which is far faster.",
    "Each thread now computes a column of eight elements, reusing each value of B it loads eight times. Fewer loads per element, and the running sums stay in registers.",
    "Each thread computes an 8 × 8 patch. It loads eight values of A and eight of B into registers and gets 64 products from them. Most of the work now happens without touching memory at all.",
  ]}
/>

## Fused Softmax

Softmax does almost no arithmetic per element, so its speed is set by how many times the data crosses to HBM and back.

<FusedSoftmax
  notes={[
    "Softmax as five library calls. Each one is its own kernel: it reads its inputs from HBM, does one cheap operation, and writes the whole result back for the next to read again. For an M × N input that is 5MN + 2M elements read and 3MN + 2M written.",
    "The same arithmetic in one kernel. A row is read once, the max, the exponentials and the sum stay in registers, and only the answer is written back: MN elements read, MN written. A quarter of the memory traffic, for the same result.",
  ]}
/>

Nothing here made the arithmetic faster. It's all about moving the data better.

## Refs

- Simon Boehm, [How to Optimize a CUDA Matmul Kernel for cuBLAS-like Performance](https://siboehm.com/articles/22/CUDA-MMM)
- Horace He, [Making Deep Learning Go Brrrr From First Principles](https://horace.io/brrr_intro.html)
- Benjamin Spector and others, [GPUs Go Brrr](https://hazyresearch.stanford.edu/blog/2024-05-12-tk) (ThunderKittens)
- NVIDIA, [Hopper Architecture In-Depth](https://developer.nvidia.com/blog/nvidia-hopper-architecture-in-depth/) and the [CUDA compute capability tables](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/compute-capabilities.html)
- Triton, [Fused Softmax](https://triton-lang.org/main/getting-started/tutorials/02-fused-softmax.html)
- The diagrams are drawn with [deep-learning-visuals](https://github.com/Cerebellum-Labs-AI/deep-learning-visuals)
