Hardware

Dive into Deep Learning · §13.2

Hardware Anatomy: CPUs, GPUs, Tensor Cores, and Memory Hierarchy
memory hierarchy · wire count & shoreline · sector access · SMs & Tensor Cores · PCIe & NVLink · decode sweep time

Why a 165-TFLOP/s GPU Decodes Only 450 Tokens/s

Motivation

  • Arithmetic time (12\,\mu\text{s}): Executing 2N = 2 \times 10^9\text{ FLOPs} per token on a 1B GPT takes 12\,\mu\text{s} at 165\text{ TFLOP/s}.
  • Memory transfer time (2.2\text{ ms}): Streaming all 2\text{ GB} of bf16 weights from GDDR6X at 0.9\text{ TB/s} takes 2.2\text{ ms}—nearly 200\times longer.
  • Throughput ceiling (\sim 450\text{ tok/s}): Single-stream decoding leaves Tensor Cores idle over 99\% of the time.

The Memory Hierarchy: Trading Capacity for Bandwidth

Memory Hierarchy

  • On-die registers & SRAM (L_1/shared, L_2): Hold \sim 0.1\text{ GB} millimeters from the ALUs and deliver tens of \text{TB/s}.
  • Off-die GPU DRAM (GDDR6X / HBM3e) & beyond: Capacity grows by orders of magnitude (24\text{--}180\text{ GB} DRAM \to hundreds of \text{GB} host RAM \to \text{TB} NVMe), while bandwidth drops by orders of magnitude at every boundary.

Bandwidth Equals Wire Count Times Bit Rate per Wire

Physical Bandwidth

  • The wire law: \text{BW} = N_{\text{wires}} \times r_{\text{wire}} (number of data wires times bit rate per wire).
  • RTX 4090 GDDR6X vs. PCIe 4.0 \times 16: Memory uses 384\text{ wires} \times 21\text{ Gb/s} \approx 1.0\text{ TB/s}; PCIe uses 16\text{ lanes} \times 16\text{ Gb/s} = 32\text{ GB/s} per direction—similar per-wire speed, but 24\times fewer wires.
  • Distance sets wire count: Every step away from the silicon die (HBM interposer \to PCB traces \to PCIe slot/cables) cuts wire density and bandwidth by roughly 10\times.

The Latency Ladder: From Register Cycles to Python

Access Latency

  • Bandwidth vs. latency: Bandwidth governs steady-state flow; latency \alpha is the delay before the first byte arrives.
  • On-chip vs. off-chip: Register/SRAM reads take nanoseconds (10\text{--}30\text{ ns}); off-chip GPU DRAM takes \sim 250\text{ ns}.
  • Python dispatch (10\text{--}100\,\mu\text{s}): Launching one eager operation from Python takes 40\text{--}400\times longer than a DRAM read—enough time for the RTX 4090 to stream 10\text{ MB} of data.

Sequential Streaming vs. Random 32-Byte Sector Reads

Access Patterns

GPU DRAM achieves its rated bandwidth \beta only on sequential, coalesced addresses. Below, we gather a 1\text{ GiB} float32 array x[idx] using an identity permutation versus a random permutation:

identity:   4.8 ms, 0.90 TB/s at 16 B/element
random  :  37.4 ms, 0.32 TB/s at 44 B/element
random / identity: 7.8x

Why random gathers lose 8\text{--}10\times in speed: Memory transactions fetch 32-byte sectors (wasting 28 of 32\text{ B} per random 4-byte read), and random addresses repeatedly miss open kilobyte-scale DRAM rows and address-translation caches.

Block Transfers and Half-Bandwidth Size n_1/2

Hockney Transfer Model

t(n) \;=\; \alpha + \frac{n}{\beta}, \qquad \beta_{\textrm{eff}}(n) \;=\; \frac{\beta}{1 + \alpha\beta / n}

  • Half-performance size (n_{1/2} = \alpha\beta): The payload size where transfer time n/\beta equals start-up latency \alpha, achieving \frac{1}{2}\beta.
  • NVMe disk shuffling (\alpha \approx 70\,\mu\text{s}, \beta = 14\text{ GB/s}): n_{1/2} \approx \mathbf{1\text{ MB}}; random 4\text{ KB} reads waste >99\% of drive bandwidth, so datasets must be stored in megabyte shards.

The Shoreline Bottleneck: Compute Is Area, I/O Is Edge

Silicon Geometry

  • Area vs. perimeter scaling: Logic transistors fill the die interior (O(L^2)), whereas off-chip I/O pads line the die perimeter or shoreline (O(L)).
  • HBM on a silicon interposer: Stacking DRAM dies vertically next to the GPU over thousands of micron-scale interposer wires gives a B200 8\text{ TB/s} (vs. \sim 0.5\text{ TB/s} for CPU DDR5 channels).
  • Multi-die packages: Joining two reticle-limit dies (\sim 858\text{ mm}^2 each) doubles the shoreline to fit eight HBM stacks.

Generational Scaling: Compute Outgrows Bandwidth

Hardware Trends

  • Disproportionate scaling (2017–2024): Across V100 \to A100 \to H100 \to B200, 16-bit Tensor Core peak P grew 18\times while HBM bandwidth \beta grew 8.9\times.
  • Doubled ridge point (I^* = P/\beta): The threshold intensity to saturate compute rose from 139\text{ FLOP/B} (V100) to 281\text{--}295\text{ FLOP/B} (B200/H100).
  • Systems consequence: An operation at I = 200\text{ FLOP/B} that was compute-bound on V100 is bandwidth-bound on H100 and B200.

The Energy Cost of Moving Bytes vs. Arithmetic

Power & Energy

  • Off-chip memory dominates power: In 45\text{ nm} CMOS (Horowitz, 2014), an fp32 multiply costs 3.7\text{ pJ}, whereas reading 64\text{ bits} from off-chip DRAM costs 1.3\text{--}2.6\text{ nJ}—roughly 500 fp32 multiplications.
  • On-chip SRAM is 20\text{--}100\times cheaper (10\text{--}100\text{ pJ}).
  • Power caps bandwidth: Every technique that saves memory traffic (operator fusion, narrower formats, on-chip tiling) saves watts for the exact same physical reason it saves milliseconds.

Streaming Multiprocessors and Latency Hiding

GPU Execution Model

\textrm{Bytes in flight} \;=\; \beta \times \textrm{latency} \;\approx\; 250\textrm{ KB}

  • Zero-overhead warp switching: Each of the 128 SMs executes threads in 32-thread warps, switching resident warps every cycle whenever one stalls on a 250\text{ ns} DRAM load.
  • Little’s Law occupancy floor: Sustaining 1.0\text{ TB/s} requires \ge 250\text{ KB} in flight (\sim 16 active warps/SM issuing 4-byte loads); tiny grids cannot hide DRAM latency.

Deriving Peak FLOP/s from SMs, Clock, and Tensor Cores

Tensor Cores

P_{\textrm{fp32}} \;=\; 128 \times 128 \times 2 \times 2.52\textrm{ GHz} \;\approx\; 82.6\textrm{ TFLOP/s}

  • Systolic tile multipliers: Tensor Cores amortize instruction fetch and register access across matrix tiles in a single instruction with fp32 accumulation.
  • Throughput scaling by precision: tf32 reaches 82.6\text{ TFLOP/s} (1\times), bf16/fp16 doubles to 165.2\text{ TFLOP/s} (2\times), and fp8 quadruples to 330.4\text{ TFLOP/s} (4\times).

Floating-Point Formats: Exponent Range and Precision

Number Formats

  • Why bfloat16 replaced float16 for training: fp32, tf32, and bf16 all share an 8-bit exponent (dynamic range \sim 10^{\pm 38}), so bf16 requires no loss scaling.
  • Twofold roofline benefit: Stepping down in bit width (except tf32, which is stored as 32-bit fp32 in DRAM) both halves memory bytes moved and doubles Tensor Core P. Sub-8-bit formats (fp8, fp4) require per-tensor or per-block (16–32 element) microscaling.

Data-Center vs. Consumer GPU Specifications

Accelerator Comparison

Comparing two flagship data-center GPUs against two consumer cards highlights where consumer cards trade off capacity, bandwidth, and multi-GPU interconnects:

Specification H100 SXM B200 RTX 4090 (Ours) RTX 5090
Memory Capacity (C) 80\text{ GB HBM3} 180\text{ GB HBM3e} 24\text{ GB GDDR6X} 32\text{ GB GDDR7}
Memory Bandwidth (\beta) 3.35\text{ TB/s} 8.0\text{ TB/s} 1.01\text{ TB/s} 1.79\text{ TB/s}
bfloat16 Tensor Core (P) 989\text{ TFLOP/s} 2{,}250\text{ TFLOP/s} 165.2\text{ TFLOP/s} 209.5\text{ TFLOP/s}
fp8 / fp4 Tensor Core 1{,}979 / — 4{,}500 / 9{,}000 330.4 / — 419 / 1{,}676
Ridge Point (I^*, bf16) 295\text{ FLOP/B} 281\text{ FLOP/B} 164\text{ FLOP/B} 117\text{ FLOP/B}
Inter-GPU Link (1-way) NVLink 4 (450\text{ GB/s}) NVLink 5 (900\text{ GB/s}) PCIe 4.0 (\le 32\text{ GB/s}) PCIe 5.0 (\le 64\text{ GB/s})

Host-to-Device Transfers: Pageable vs. Pinned Memory

Host Data Paths

The GPU’s DMA copy engines can stream minibatches over PCIe concurrently with SM execution only if the source buffer resides in page-locked (pinned) host RAM:

pageable: 14.2 GB/s
pinned: 23.1 GB/s
  • Why pageable memory is slower: The OS can swap pageable pages at any time, forcing the driver to copy pageable arrays on the CPU into a private pinned staging buffer before DMA begins.
  • Systems rule: Keep small datasets resident in GPU DRAM; for streamed datasets, allocate host batches in pinned memory (pin_memory=True / 'pinned_host') and copy asynchronously (non_blocking=True).

LLM Inference on the Roofline: Prefill vs. Decode

Workload Analysis

Autoregressive language model serving splits into two distinct roofline regimes for a model of N parameters stored in bfloat16 (2N bytes):

\textrm{Prefill ($n$ prompt tokens): } I \approx n \;\textrm{(compute-bound)}, \qquad \textrm{Decode ($1$ token): } I \approx 1 \;\textrm{(bandwidth-bound)}

  • Single-stream decode bound: Because generating one token reads all 2N weight bytes plus the KV cache at intensity I \approx 1 \ll I^*, token generation speed is governed strictly by memory bandwidth \beta: \textrm{tokens/s} \;\lesssim\; \frac{\beta}{\textrm{bytes moved per token}} \;=\; \frac{\beta}{2N + \textrm{KV bytes}}
  • 1B GPT on RTX 4090 (\beta \approx 0.9\text{ TB/s}, 2N = 2\text{ GB}): At short context lengths, \textrm{tokens/s} \lesssim \frac{900\text{ GB/s}}{2\text{ GB}} \approx \mathbf{450\text{ tokens/s}}.

The 20 ms Memory Sweep Time and Batched Decoding

Capacity & Sweep Time

  • Universal \sim 20\text{ ms} sweep invariant (C/\beta): Reading a full GPU’s DRAM once takes 18\text{--}24\text{ ms} across RTX 4090, RTX 5090, H100, and B200 alike.
  • Full-card 1-stream ceiling: Any model whose weights fill its card decodes a single sequence at \beta/C \approx \mathbf{42\text{--}56\text{ tokens/s}}.
  • Batched decoding (B streams): Shares each weight read across B tokens (I \approx B), multiplying total throughput linearly until B \approx I^*.

Concept Check: Hardware Bandwidth, Formats, and Decode

Check Your Understanding

Work out the following hardware estimates on the RTX 4090 (\beta_{\textrm{achieved}} \approx 0.9\text{ TB/s}, bf16 peak P = 165\text{ TFLOP/s}, fp8 peak 330\text{ TFLOP/s}, ridge I^* = 164\text{ FLOP/byte}):

  1. We switch from bfloat16 to fp8 for (a) an elementwise kernel at I = 0.25\text{ FLOP/B} and (b) a large matmul at I = 1{,}000\text{ FLOP/B}. What speedup does each get, and why does neither get 4\times?
  2. An 8B-parameter model in bfloat16 (16\text{ GB} of weights, KV cache neglected) runs on the RTX 4090. What is the maximum token throughput for 1 stream (B = 1) and across 64 concurrent streams (B = 64)?

Answers:

  1. (a) 2\times faster (bandwidth-bound: halves bytes moved); (b) 2\times faster (compute-bound: doubles Tensor Core P from 165 \to 330\text{ TFLOP/s}). Neither gains 4\times because only one roof binds at a time.
  2. 1 stream (B = 1): Streaming 16\text{ GB} at 0.9\text{ TB/s} takes 17.8\text{ ms/step} \to \approx 56\text{ tokens/s}. Batch B = 64: Still bandwidth-bound (I \approx 64 < 164), so each step still takes \sim 17.8\text{ ms} \to 64 \times 56 \approx 3{,}600\text{ tokens/s} total.

Summary: Hardware Laws and Characteristic Magnitudes

Key Takeaways

Hardware Principle / Metric Rule / Formula RTX 4090 / Workstation Practical Systems Consequence
Wire Bandwidth Law \beta = \textrm{wires} \times \textrm{rate/wire} 1.0\text{ TB/s} DRAM vs. 23\text{ GB/s} PCIe Each hop off-die cuts wire count and bandwidth by \sim 10\text{--}40\times
Shoreline & Energy Compute \propto L^2, I/O \propto L 1\text{ DRAM read} \approx 500\text{ FLOPs} Ridge points (117\text{--}295\text{ FLOP/B}) rise across generations
Sector & Row Access t(n) = \alpha + n/\beta, 32-B sectors Random gather \sim 10\times slower Coalesce memory reads; shuffle disk data in \ge 1\text{ MB} blocks
Little’s Law (SMs) \textrm{In-flight} = \beta \times \textrm{latency} \approx 250\text{ KB} (128 SMs) Kernels must expose enough warps to hide 250\text{ ns} DRAM latency
Precision Narrowing \textrm{Bytes} \div 2, Tensor Core P \times 2 tf32 83\text{ TF/s} \to bf16 165\text{ TF/s} Speeds up both bandwidth-bound and compute-bound kernels
Decode & Sweep Time \textrm{tok/s} \lesssim \beta / (2N + \textrm{KV}) C/\beta \approx 24\text{ ms} (\sim 42\text{ tok/s} full) Single-stream decode is memory-bound; batching raises I \approx B