Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Handbooks  /  GPU Fundamentals
AI Engineering~12 min readIntermediate
Deep Dive

GPUs: the bottleneck is never where you think.

Every AI engineer eventually has the conversation: "just get a bigger GPU." Sometimes right, often expensively wrong — because GPU performance isn't one number, it's a race between arithmetic and memory, and most workloads lose on the memory side. This is the mental model that tells you which wall you're hitting, why FlashAttention was inevitable, and when the bigger GPU genuinely helps.

01

Throughput machines, not fast machines

A CPU is a latency processor: a few heroic cores with deep caches and branch predictors, built to finish one thread as fast as possible. A GPU is a throughput processor: ~100+ streaming multiprocessors (SMs) hosting tens of thousands of lightweight threads, built to finish all the work per second — no single thread is fast, the swarm is.

The execution model is SIMT — single instruction, multiple threads: threads run in lockstep groups of 32 called warps, one instruction driving 32 data elements. This is why GPUs devour uniform work (every element gets the same treatment — exactly what a matmul is) and why branch divergence hurts: if half a warp takes the if and half takes the else, the hardware runs both paths serially. Deep learning happens to be the most uniform large-scale workload ever invented — the marriage wasn't luck, it was fit.

02

The memory hierarchy is the whole game

Fast is small, big is slow (H100-class numbers)
Registersper-thread, ~instant — where actual arithmetic operands live.
Shared memory / L1~200 KB per SM of on-chip SRAM, ~19 TB/s class — the programmable scratchpad tiles live in.
L2 cache~50 MB shared across the chip.
HBM ("VRAM")80 GB at ~3.35 TB/s — huge, and the slowest thing on the die by an order of magnitude.

The compute units can chew through data far faster than HBM can deliver it. So the central discipline of GPU performance is arithmetic intensity: FLOPs per byte moved. Each GPU has a break-even ratio (peak FLOPs ÷ bandwidth ≈ ~300 FLOPs/byte on H100 BF16). Above it, you're compute-bound — the cores are the wall. Below it, memory-bound — the cores idle while bytes crawl. That one ratio — the roofline — explains most "why is this slow" mysteries in AI.

03

Where LLMs land on the roofline

WorkloadBound bySo the fix is…
Training / prefillcompute — big matmuls, high intensitytensor cores, lower precision (BF16→FP8), better MFU
Decode (one token)bandwidth — every weight read for little mathquantize (fewer bytes), batch (share the read), speculative decoding
Attention (naive)bandwidth — the N×N matrix commutes to HBMfuse it: FlashAttention
Elementwise opsbandwidth — one FLOP per bytekernel fusion, so bytes are read once for many ops

Tensor cores are why the compute side got so tall: dedicated units performing small matrix multiply-accumulates in hardware, with throughput that roughly doubles each precision step down (BF16 → FP8 → FP4 on the newest parts) — the hardware reason low-precision training and serving keep winning. And MFU (model FLOPs utilization) is the honesty metric: real training jobs hitting 35-45% of peak is good; the marketing number assumes a workload nobody runs.

→ The FlashAttention lesson

Same math, tiled so the N×N scores live in on-chip SRAM and never touch HBM: the operation was bandwidth-bound, so moving fewer bytes made it directly faster. Memorize the shape of that win — the modern optimization is almost never "compute less," it's "move less."

The ridge point is one division

"Compute- or memory-bound?" isn't a vibe — it's arithmetic. The ridge point is where the two roofs cross: the arithmetic intensity (FLOP per byte moved) at which a kernel stops being starved for data and starts being limited by math. It's just peak compute divided by peak bandwidth:

ridge = peak_FLOPs ÷ bandwidth = 312 TFLOP/s ÷ 2.0 TB/s = 156 FLOP/byte  (A100)

Below 156 FLOP/byte you're wasting the A100's compute waiting on memory; above it, you're finally math-limited. Now place LLM decoding on that axis. Decoding one token reads every weight (2 bytes each) to do ~2 FLOPs per weight — so its intensity is a dismal 1 FLOP/byte. Batch B tokens and the same weight read serves all of them:

decode intensity = 2·N·B FLOPs ÷ 2·N bytes = B FLOP/byte  →  single token (B=1) reaches just 2 of 312 TFLOP/s (0.6%)

To cross the ridge on an A100 you need batch ≥ 156. That single number is why batched serving exists.

This is not a memorized fact — it's a calculation you can rerun for any chip and batch size. The roofline calculator below does exactly that: pick a device, sweep the batch size, and watch decode climb off the bandwidth roof toward the compute roof.

04

The kernel stack: who writes the fast code

A kernel is a function launched across a grid of thread-blocks — the unit of GPU work. Almost nobody hand-writes them anymore; the stack has layers:

From metal to model
CUDA C++raw kernels, full control — vendor libraries and kernel specialists live here.
cuBLAS / cuDNN / CUTLASSNVIDIA's tuned matmul & conv kernels — what your framework actually calls.
Tritonwrite fused custom kernels in Python; the compiler handles the thread-level pain. Where FlashAttention-style wins get democratized.
torch.compiletraces your model and auto-fuses/generates kernels — the free ~1.3-2× most people skip.

The practical division of labor: use the framework; take the torch.compile win; profile before believing anything; and reach for Triton only when the profiler shows a genuinely unfused hot spot. Writing CUDA is a specialization — reading a profile is table stakes.

05

Many GPUs: the interconnect becomes the memory wall

Models outgrew single GPUs, so the memory-hierarchy story repeats one level up: NVLink (~900 GB/s GPU-to-GPU inside a node) and InfiniBand (~400 Gb/s between nodes) are to the cluster what HBM is to the chip — the slow layer everything must be tiled around. The primitive is the collective: all-reduce (sum gradients everywhere), all-gather, reduce-scatter, moved by NCCL and overlapped with compute so communication hides behind math.

The three parallelism strategies in one line each: data parallel (copy the model, split the batch, all-reduce gradients), tensor parallel (split individual matrices across GPUs — needs NVLink-class bandwidth), pipeline parallel (split layers into stages — cheap on bandwidth, pays a bubble). Real frontier runs compose all three; that composition is its own handbook, coming next.

06

The working checklist

QuestionHow to answer it
Compute- or memory-bound?arithmetic intensity vs the GPU's ratio; profiler confirms
Is the GPU even busy?MFU / SM occupancy — data loading and CPU stalls fake "slow GPU" constantly
Decode too slow?bandwidth ÷ model bytes first; quantize or batch before shopping
Training too slow?step-time breakdown: compute vs communication vs dataloader
Custom kernel worth it?only if the profiler shows unfused memory-bound ops on the hot path
RUN IT YOURSELF

Compute the roofline yourself

"Memory-bound" stops being jargon the moment you divide two numbers. This is the roofline model in a dozen lines: two accelerators (A100, H100) with their real peak FLOP/s and HBM bandwidth, the ridge point that separates memory-bound from compute-bound, and LLM decode placed on that axis. Sweep the batch size and watch a single-token decode — stuck at 0.6% of the A100's compute — climb the bandwidth roof until, at batch ≥ 156, it finally saturates the machine. That's not a rule of thumb; it's the number you just computed.

CPython · WebAssembly
By the numbers

Worked example: how fast can one GPU decode a 7B model?

A 7B-parameter model in BF16 is 7B × 2 bytes = 14 GB of weights. Every decode step reads all of them. Use the page’s A100 and H100 numbers and its intensity rule (batch B gives B FLOP/byte) to find the ceiling, ignoring the KV cache for now.

StepQuestionArithmeticAnswer
1Time to read the weights once on an A10014 GB ÷ 2.0 TB/s7 ms per step
2Time for the math, batch 12 × 7B FLOPs ÷ 312 TFLOP/s0.045 ms: the cores idle 99% of the step
3Ceiling at batch 11 token per 7 ms~143 tokens/s
4Batch 32math 1.44 ms, still under the 7 ms read4,571 tokens/s, still memory-bound
5Batch 156 (the ridge)math 7.0 ms = read 7.0 ms22,286 tokens/s; compute-bound from here
6Same model on an H100, batch 114 GB ÷ 3.35 TB/s = 4.18 ms~239 tokens/s
7H100 vs A100 at batch 13.35 ÷ 2.0, not 990 ÷ 3121.68× faster, not 3.17×
8A100 with 4-bit weights, batch 13.5 GB ÷ 2.0 TB/s = 1.75 ms~571 tokens/s, 4× step 3

Steps 2 and 3 are the page’s 0.6% figure seen from the other side: at batch 1 the A100 spends 7 ms moving bytes and 0.045 ms doing math. Batching (step 4) multiplies throughput at no cost per step until the ridge. Quantizing (step 8) helps even a single user, because it shrinks the bytes every step must read. Step 7 is the answer to “just get a bigger GPU”: for single-stream decode you get the bandwidth ratio, not the FLOP ratio. Real serving lands below these ceilings, and the KV cache is one reason. Each sequence in the batch reads its own cached keys and values every step, so bytes grow with batch × context length, and the ridge moves further away.

Trade-offs

Cost and trade-offs of the standard fixes

Reusing the columns: Time is what the fix speeds up. Space is what it costs in memory, quality or hardware.

CaseTimeSpaceWhy
Batching decodeThroughput rises about linearly with B up to the ridge (B ≈ 156 on an A100)KV cache memory per sequence; each user waits for the batch stepShares one weight read across B tokens. Latency per token does not improve.
Quantization (BF16 → 8- or 4-bit)Fewer bytes per step: 2× or 4× on the weight readSome quality risk; must be measured on your taskHelps memory-bound decode directly, even at batch 1.
FlashAttentionFewer HBM round trips for attentionScore memory linear in sequence length instead of N×NAt N = 8,192 one head’s BF16 score matrix is 134 MB; tiling keeps it on-chip.
Kernel fusion / torch.compileElementwise chains read memory once instead of once per opCompile time, and occasional graph breaksThe page’s ~1.3–2× for most models, for one line of code.
Tensor parallelSplits each matmul, and each weight read, across GPUsAn all-reduce per layer; needs NVLink-class linksCuts per-token latency when one GPU’s bandwidth is the wall.
Pipeline parallelFits models too big for one nodeA pipeline bubble of idle stagesLight on bandwidth, so it can cross nodes.
A bigger GPUBandwidth ratio for decode; FLOP ratio for trainingPriceCheck which roof you are under before paying for the other one.

Every row except the last moves fewer bytes or reuses each byte more. That is the page’s FlashAttention lesson as a menu: the cheap wins are almost always on the memory side.

Watch for

Common mistakes

  • Comparing GPUs on peak TFLOP/s for inference

    Single-stream decode is bandwidth-bound, so the spec that matters is memory bandwidth. The H100 has 3.17× the A100’s BF16 compute but 1.68× its bandwidth, and batch-1 decode speeds up by the smaller number.

  • Reading nvidia-smi “GPU-Util” as how busy the GPU is

    That figure is the share of time at least one kernel was running. A memory-bound kernel using a few percent of the FLOPs, or a tiny kernel on one SM, still shows 100%. Use MFU, SM occupancy and achieved bandwidth from a profiler instead.

  • Timing GPU code without synchronizing

    Kernel launches are asynchronous: the Python call returns before the GPU finishes. Timing it with a wall clock measures the launch. Call torch.cuda.synchronize() before reading the clock, or use CUDA events, and discard the first iterations, which include warm-up and compilation.

  • Leaving the KV cache out of the batch math

    The B FLOP/byte rule counts only weights. Each sequence also reads its KV cache every step, and that grows with context length. With long contexts, the cache can be read in larger volume than the weights, so a big batch may stay memory-bound long past B = 156.

  • Mixing up Gb/s and GB/s

    InfiniBand’s ~400 Gb/s is gigabits: 50 GB/s. NVLink’s ~900 GB/s is gigabytes, 18× more. That gap is why tensor parallelism, with an all-reduce in every layer, stays inside a node, and only pipeline or data parallelism crosses nodes.

In the wild

Where this shows up in production and interviews

  • LLM serving engines

    Engines such as vLLM exist to push B up: continuous batching adds and removes sequences between decode steps, and paged KV-cache memory packs more sequences into the same HBM. Both are the batching row of the trade-off table, engineered.

  • Training at scale

    Large training runs report MFU as their efficiency figure, and the page’s 35–45% is the range worth aiming for. Sharding schemes such as ZeRO and FSDP split optimizer state, gradients and weights across data-parallel GPUs, trading extra communication for memory.

  • Profilers

    Nsight Systems shows the timeline: kernels, copies, gaps where the GPU waits on the CPU or data loader. Nsight Compute analyses one kernel and plots it on a roofline chart. The PyTorch profiler maps both back to your model’s code.

  • The interview version

    “Estimate tokens per second for a 70B model on 8 H100s.” 70B × 2 bytes = 140 GB. Split by tensor parallelism, each GPU reads 17.5 GB per step, and 8 × 3.35 TB/s gives about 5.2 ms per token, or ~191 tokens/s per stream as an upper bound. Then say what lowers it: all-reduce time, KV cache reads and kernel efficiency.

Self-check

Check yourself

A 13B model in BF16 runs at batch 1 on one H100. What is the best possible decode speed?

About 129 tokens/s. The weights are 13B × 2 = 26 GB; 26 GB ÷ 3.35 TB/s = 7.76 ms per token. The math takes a fraction of a millisecond, so bandwidth sets the ceiling.

A square 4096 × 4096 × 4096 BF16 matmul: compute-bound or memory-bound on an A100?

Compute-bound. It does 2 × 4096³ ≈ 137 GFLOPs and moves three 4096² matrices of 2 bytes, about 101 MB. That is ~1,365 FLOP/byte, far above the A100’s ridge of 156. Big matmuls are why training is compute-bound.

Three elementwise ops (scale, add bias, GELU) run as three kernels. Roughly what does fusing them into one buy?

About 3× for that chain. Each op does almost no math per byte, so its time is the read and write of the tensor. Three kernels make three round trips to HBM; one fused kernel makes one. The FLOPs are the same.

A model needs 16 GPUs: two nodes of 8, NVLink inside each node, InfiniBand between them. How do you split it?

Tensor parallel inside each node, pipeline parallel across the two. Tensor parallelism all-reduces in every layer and needs NVLink-class bandwidth; pipeline parallelism only passes activations between stages, so it tolerates InfiniBand’s 50 GB/s.

Frequently asked

Quick answers

Why are GPUs good at deep learning?

SIMT throughput machines + tensor cores meet the most uniform workload ever: giant matmuls. No single thread is fast; the swarm is.

What is arithmetic intensity?

FLOPs per byte moved. Above the GPU's ratio (~300 on H100 BF16) you're compute-bound; below it, memory-bound — and most inference is below it.

Why is FlashAttention faster?

Same math, tiled into on-chip SRAM so the N×N scores never touch HBM. Bandwidth-bound op + fewer bytes = directly faster.

Must I learn CUDA?

Learn the model and the profiler; use torch.compile; reach for Triton on proven hot spots. Hand-written CUDA is a specialization.

▶  Watch it explained

Ray tracing: how GPUs fake reality by chasing light

GPU Fundamentals · AI Engineering · Vibe Engines · 2026
Finished this one? 0 / 209 Handbooks done
NextGPU vs TPU Next on the “Serving models, and paying for them” ladder · rung 4 of 13

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Handbooks