Hardware and Kernels
Memory, processors, kernels and compilers, with numbers for estimates.
How CPUs and GPUs run neural networks, how kernels and compilers make code fast on them, and the numbers for quick estimates.
Memory and compute
Memory hierarchy – Each level is larger and slower than the one above it, so fast code reuses data while it is in a fast level. Rough sizes:
| Level | CPU (per core unless noted) | GPU (H100 SXM) |
|---|---|---|
| Registers | 16–32 vector registers | 256 KB per SM (streaming multiprocessor) |
| L1 / shared memory | 32–48 KB | 256 KB per SM, up to 228 KB as shared |
| L2 | 1–2 MB | 50 MB, shared |
| L3 | tens to hundreds of MB, shared | none |
| Main memory | DDR5: 80–100 GB/s desktop, 300–600 GB/s server | 80 GB HBM at 3.35 TB/s |
A main-memory access costs about 100 ns, a few hundred cycles. Data moves in cache lines of 64 bytes, so reading one float costs as much as reading 16 neighbouring ones.
Roofline model – A kernel's attainable speed is
where the arithmetic intensity is FLOPs per byte moved. Below the ridge point a kernel is memory-bound; above it, compute-bound.
Example: a 32-core server CPU with AVX-512 does about 64 fp32 FLOPs per cycle per core, so at 2.5 GHz it peaks near 5 TFLOP/s. With 300 GB/s of memory its ridge is about 17 FLOPs per byte, against a few hundred for a GPU.
Intensity of common operations – In bf16, an matrix multiply does FLOPs on bytes, so at best (with tiling good enough to read each matrix once). That is compute-bound from on the CPU above and from on an H100. A matrix–vector product does about 1 FLOP per byte and an elementwise operation less, so both are memory-bound on any hardware.
Processors
GPU execution – Threads are grouped into warps of 32 that are issued one instruction at a time together, and warps into blocks that each run on one SM. When threads in a warp take different branches, the branches run one after the other. Memory latency is hidden by switching between many resident warps, so a kernel needs enough parallel work to keep each SM busy (occupancy).
Coalescing and bank conflicts – When the 32 threads of a warp read consecutive addresses, the reads combine into a few wide transactions; strided reads waste most of each transaction. Shared memory has 32 banks, and threads hitting the same bank at different addresses are serialized.
Tensor Cores – Units that do a small matrix multiply–accumulate per instruction. On an H100 they give 989 TFLOP/s in dense bf16 against 67 TFLOP/s for ordinary fp32 arithmetic. They work best when the innermost dimensions are multiples of 8 for bf16 (16 for int8), and multiples of 64 or 128 avoid partly empty tiles, which is why vocabulary and hidden sizes are padded to such multiples.
SIMD on CPUs – One instruction operates on a whole vector register: 8 fp32 values with AVX2 (256-bit), 16 with AVX-512, 4 with Arm NEON (128-bit). A fused multiply–add (FMA) counts as 2 FLOPs, and server cores have up to two FMA units.
Low-precision instructions matter most for inference. AVX-512 VNNI (unsigned × signed) and Arm's SDOT (signed × signed) multiply 8-bit values and add groups of 4 into int32 in one instruction. Intel AMX adds 8 tile registers of 1 KB each and a matrix unit, giving roughly an order of magnitude more bf16 and int8 throughput per core than AVX-512.
Threads and NUMA – Dense math uses one thread per physical core, since two hyperthreads share the same FMA units. On a multi-socket server each socket has its own memory, and reading the other socket's memory is slower (NUMA, non-uniform memory access). Pin threads to cores and allocate memory on the same node. Threads that write to the same cache line slow each other down even when they touch different variables (false sharing).
Kernels
Loop order – For row-major , the loop order i, j, k reads down a column, touching a new cache line on every step. The order i, k, j streams rows of and contiguously and vectorizes, which is often several times faster with no other change.
Tiling a matrix multiply – Split the matrices into blocks that fit in a fast level of memory, so each loaded element is reused about tile-size times. CPU libraries (BLIS, oneDNN) tile once per cache level. They copy tiles into a contiguous packed layout, and an inner microkernel keeps a small block of , such as fp32, in registers. GPU kernels tile into shared memory per block and into registers per warp, feeding Tensor Cores.
Quantized kernels – Weights are stored in blocks with a scale per block. For example, 32 4-bit weights with one fp16 scale use 4.5 bits per weight. The kernel unpacks and rescales the weights in registers just before the FMA, so memory traffic stays at the compressed size. With int8 activations it can use the integer dot-product instructions directly and apply the scales afterwards.
Compilers
Kernel compilers – Triton lets you write a GPU kernel as a program that computes one tile of the output, using block-level loads and tl.dot. The compiler maps the tile to threads and handles shared memory, coalescing and Tensor Cores. It lowers through MLIR and LLVM to NVIDIA and AMD machine code, and an experimental CPU backend exists.
Graph compilers – torch.compile captures a PyTorch program as a graph and generates fused kernels: Triton on GPUs, C++ with OpenMP on CPUs. XLA does the same for JAX and TensorFlow, and is the only route onto TPUs. TVM separates what to compute from the schedule (tiling, loop order, vectorization, threading) and searches over schedules with a learned cost model.
Autotuning – The best tile sizes, number of warps, pipeline stages and unrolling depend on the chip and the matrix shapes. Benchmark the candidate configurations once per shape and cache the fastest.
A kernel compiler for CPUs – A CPU version of Triton has to:
- Map tiles to cores.
- Vectorize the inner loops to the SIMD width.
- Choose tile sizes for each cache level and pack operands into the right layout.
- Emit the matrix and dot-product instructions (AMX, VNNI, SDOT).
- Fuse elementwise operations into the end of the matrix multiply.
- Autotune all of this per processor.
A CPU has tens of cores rather than thousands of threads to hide memory latency, so cache blocking and data layout matter more than on a GPU.
Estimates
Decoding on a CPU – For a dense model at batch size 1, each generated token reads every weight once (plus the KV cache, which grows with context), so
Example: a 7B model at 4.5 bits per weight is about 3.9 GB. A desktop with 90 GB/s gives at most about 23 tokens per second, and a 300 GB/s server about 75. Real systems reach roughly 60–80% of this bound.
Prompt processing on a CPU – Prefill is compute-bound at about FLOPs per token for parameters. A 7B model needs 14 GFLOPs per token, so a CPU sustaining 1 TFLOP/s processes about 70 prompt tokens per second, and a 2,000-token prompt waits about 30 seconds before the first output token. Long prompts, not generation speed, are usually the bottleneck for CPU inference.
Measuring – Report prefill and decode speed separately. Compare achieved bandwidth (bytes read per second) with the machine's measured peak, for example from the STREAM benchmark, to see how close to the bound a kernel runs. Profile with perf or VTune on CPUs and Nsight Compute on GPUs.