Chapter 2: How to Think About TPUs

Scaling Book Exercises — Chapter 2

Chapter: 2 — How to Think About TPUs
Source: handwritten Onyx Boox A4 PDF
Note: Transcribed from handwritten solutions; book markdown used only for exercise statements and notation.

This document contains only the handwritten solution content from the scan, with exercise statements copied from the Chapter 2 book markdown and kept separate from the solutions.

Exercise 1 — Bounding LLM latency

Exercise statement

Say you want to sample from a 200B parameter model in bf16 that’s split across 32 TPU v4p. How long would it take to load all the parameters from HBM into the systolic array? Hint: use the numbers above.

Solution

Scan pages: 1-2

v4p ⇒

  • HBM BW = 1.2𝑒12
  • FLOPs/s bf16 = 2.75𝑒14
  • HBM capacity = 32GB

200Billion=200⋅109 (bf16). Split across 32 TPUs v4p ⇒ Bytes per TPU:

200 ⋅ 10 9 ⋅ 2 32 = 400 ⋅ 10 9 32 = 12.5 ⋅ 10 9 bytes = 12.5 GB

12.5GB≤32GB. So it does fit.

Each TPU HBM has 12.5GB of weights to send to the Tensor Cores:

𝑇 comms = 12.5 ⋅ 10 9 bytes 1.2 𝑒 12 bytes/s ≈ 10 ⋅ 10 − 3 s = 10 ms .

Exercise 2 — TPU details

Exercise statement

Consider a full TPU v5e pod. How many total CPU hosts are there? How many TPU TensorCores? What is the total FLOPs/s for the whole pod? What is the total HBM? Do the same exercise for TPU v5p pod.

Solution

Scan pages: 2-5

TPU v5e pod

  • Pod size: 16×16
  • Host size: 4×2
  • HBM per chip: 16GB
  • FLOPs bf16 per chip = 1.97𝑒14
  • Host size: 2×4=8

16×16: how many blocks of 2×4 fit:

One host (schematic):
+----+----+----+----+
|    |    |    |    |
+----+----+----+----+
|    |    |    |    |
+----+----+----+----+

4 hosts to cover one “row”, 8 hosts to cover one “column” ⇒32 hosts. I could also just flatten: 16×16=256

[ ][ ][ ][ ][ ][ ][ ][ ] ...
<---------- 8 ---------> is a host

and then divide i.e 2568=32 hosts.

TPU v5e has only one Tensor Core per chip ⇒256Tensor Cores. Also you can derive by cores per host and from the book you see using previous result: 32×8=256Tensor Cores where 32 is hosts in the pod and 8 is cores per host.

Total FLOPs/s = 256×1.97𝑒14≈500𝑒14

Total HBM = 256×16⋅109=4096𝑒9=4096 GB.

TPU v5p pod

  • Pod Size: 16×20×28
  • Host Size: 2×2×1

Flatten ⇒ 16×20×28=8960 chips.

Total Hosts = 89604=2240

Tensor Cores = 8960×2=17920

Total HBM:

8960 × 96 ⋅ 10 9 = 860160 ⋅ 10 9 ≈ 860 ⋅ 10 3 ⋅ 10 9 = 860 TB .

Total FLOPs/s:

8960 × 4.59 𝑒 14 ≈ 41000 × 10 14 ≈ 4.1 Exa FLOPs/s .

Exercise 3 — PCIe operational intensity

Exercise statement

Imagine we’re forced to store a big weight matrix 𝐴 of type bf16[D, F], and a batch of activations 𝑥 of type bf16[B, D] in host DRAM and want to do a matrix multiplication on them. This is running on a single host, and we’re using a single TPU v6e chip attached to it. You can assume 𝐵≪𝐷, and 𝐹=4𝐷. What is the smallest batch size 𝐵 we need to remain FLOPs bound over PCIe? Assume PCIe bandwidth of 1.6e10 bytes / second.

Solution

Scan pages: 6-7

[Host] -- PCIe --> [TPU]

Assume:

  • TPU v6e.
  • bf16[𝐵,𝐷]⋅𝐷bf16[𝐷,𝐹]→bf16[𝐵,𝐹]
  • 𝐵≪𝐷, 𝐹=4𝐷.
  • PCIe BW = 1.6𝑒10 bytes/s.
  • FLOPs/s = 9.20𝑒14.

Assuming I can overlap communication with compute:

AI ≔ 2 ⋅ 𝐵 𝐷 𝐹 2 𝐵 𝐷 + 2 𝐷 𝐹 + 2 𝐵 𝐹 ≥ 9.20 ⋅ 10 14 1.6 ⋅ 10 10

Using 𝐹=4𝐷 and 𝐵≪𝐷:

AI ≈ 2 ⋅ 𝐵 ⋅ 𝐷 ⋅ 4 𝐷 2 ⋅ 𝐷 ⋅ 4 𝐷 = 𝐵 ⋅ 𝐷 2 𝐷 2

⇒𝐵≥5.75⋅104=57.5⋅103

Like 57 sequences of 1024 seq len.

Exercise 4 — General matmul latency

Exercise statement

Let’s say we want to multiply a weight matrix int8[16384, 4096] by an activation matrix of size int8[B, 4096] where 𝐵 is some unknown batch size. Let’s say we’re on 1 TPU v5e to start.

  1. How long will this multiplication take as a function of 𝐵? Hint: it may help to calculate how long it will take to load the arrays from HBM and how long the multiplication will actually take. Which is bottlenecking you?
  2. What if we wanted to run this operation out of VMEM? How long would it take as a function of 𝐵?

Solution

Scan pages: 7-13

v5e:

  • HBM BW = 8.2𝑒11
  • FLOPs/s int8 = 3.94𝑒14

VMEM BW ≈ HBM BW ×22 ≈8.2⋅1011×22=180.4×1011 bytes/s.

int8[𝐵,4096]⋅int8[4096,16384]→int8[𝐵,16384]

𝑇=max(𝑇comms,𝑇math)

Note: 16384=4⋅4096, so let 𝐷=4096 and 𝐹=4096⋅4⇒𝐹=4⋅𝐷 as before.

[𝐵,𝐷]⋅[𝐷,4𝐷]→[𝐵,4𝐷]

𝑇 comms = 𝐵 ⋅ 𝐷 + 4 ⋅ 𝐷 2 + 𝐵 ⋅ 4 ⋅ 𝐷 8.2 𝑒 11 = 𝐵 ⋅ ( 𝐷 + 4 ⋅ 𝐷 ) + 4 ⋅ 𝐷 2 8.2 𝑒 11 = 𝐵 ⋅ ( 5 𝐷 ) + 4 ⋅ 𝐷 2 8.2 𝑒 11 𝑇 math = 2 ⋅ 𝐵 ⋅ 𝐷 ⋅ 𝐹 3.94 𝑒 14 = 8 ⋅ 𝐵 ⋅ 𝐷 2 3.94 𝑒 14 𝑇 operation = max ( 𝑇 comms ( 𝐵 ) , 𝑇 math ( 𝐵 ) ) = max ( 𝐵 ⋅ 5 ⋅ 𝐷 + 4 ⋅ 𝐷 2 8.2 𝑒 11 , 8 ⋅ 𝐵 ⋅ 𝐷 2 3.94 𝑒 14 ) ⇒ 𝑇 operation ( 𝐵 , BW ) = max ( 𝐵 ⋅ 5 ⋅ 𝐷 + 4 ⋅ 𝐷 2 BW , 8 ⋅ 𝐵 ⋅ 𝐷 2 3.94 𝑒 14 )

Since: VMEM = 128 MiB=128⋅106.

4096×(4⋅4096)≈68⋅106. At least the weights do fit in VMEM.

Bottleneck: compute bound when?

𝑇 comms ( 𝐵 , BW ) ≤ 𝑇 math ( 𝐵 ) ⇔ 𝐵 ⋅ 5 ⋅ 𝐷 + 4 ⋅ 𝐷 2 BW ≤ 8 ⋅ 𝐵 ⋅ 𝐷 2 3.94 𝑒 14 FLOPs/s ⇔ 𝐵 ⋅ 5 ⋅ 𝐷 + 4 ⋅ 𝐷 2 8 ⋅ 𝐵 ⋅ 𝐷 2 ≤ BW 3.94 𝑒 14 = AI − 1 ⇔ 𝐵 ⋅ 5 ⋅ 𝐷 8 ⋅ 𝐵 ⋅ 𝐷 2 + 4 ⋅ 𝐷 2 8 ⋅ 𝐵 ⋅ 𝐷 2 ≤ AI − 1 ⇔ 0 ≤ 5 8 ⋅ 𝐷 + 4 8 ⋅ 𝐵 ≤ AI − 1 ⇔ 4 8 ⋅ 𝐵 ≤ AI − 1 − 5 8 ⋅ 𝐷 ⇔ 1 2 ⋅ 𝐵 ≤ AI − 1 − 5 8 ⋅ 𝐷 ⇔ 1 𝐵 ≤ 2 ⋅ AI − 1 − 10 8 ⋅ 𝐷 ⇔ 𝐵 ≥ 1 2 ⋅ ( 1 AI ) − 10 8 ⋅ 𝐷 ⇔ 𝐵 ≥ 1 2 ⋅ ( BW 3.94 𝑒 14 ) − 3 ⋅ 10 − 4

BWHBM=8.2𝑒11

𝐵 ≥ 1 2 ⋅ 8.2 𝑒 11 3.94 𝑒 14 − 3 ⋅ 10 − 4 ≈ 1 4 ⋅ 10 − 3 = ( 4 ⋅ 10 − 3 ) − 1 = 1 4 ⋅ 10 3 = 250

BWVMEM=180.4𝑒11

𝐵 ≥ 1 2 ⋅ 180.4 𝑒 11 3.94 𝑒 14 − 3 ⋅ 10 − 4 ≈ 11 .

Exercise 5 — ICI bandwidth

Exercise statement

Let’s say we have a TPU v5e 4x4 slice. Let’s say we want to send an array of type bf16[8, 128, 8192] from TPU{0,0} to TPU{3, 3}. Let’s say the per-hop latency for TPU v5e is 1𝜇𝑠.

  1. How soon will the first byte arrive at its destination?
  2. How long will the total transfer take?

Solution

Scan pages: 14-17

4x4 slice, no wraparound.
Source = [00] = TPU{0,0}; receiver = [33] = TPU{3,3}.

P1: across the top row, then down the right column.
P2: down the left column, then across the bottom row.

[00]--P1-->[01]--P1-->[02]--P1-->[03]
  |                                |
  P2                               P1
  v                                v
[10]       [11]       [12]       [13]
  |                                |
  P2                               P1
  v                                v
[20]       [21]       [22]       [23]
  |                                |
  P2                               P1
  v                                v
[30]--P2-->[31]--P2-->[32]--P2-->[33]

v5e ⇒(4,4) slice has no wraparound.

Assume 𝑇comms, hop=𝛼+𝐷BW for one hop. Then, assuming pipelining:

𝑇 comms = #Hops ⋅ 𝛼 + 𝐷 BW where 𝛼 = 1 𝜇 𝑠

#Hops=6⇒ first byte arrives after 6𝜇𝑠.

Different algos, but a lower bound on time is:

𝑇 ≥ 𝐷 2 ⋅ BW

since it needs to receive 𝐷 bytes at 2∗BW:

           |
           |
           v
----> [TPU[3, 3]]

Algo 1

Data Split = 2×8×128×81922

path1 = P1 arrows: [00] -> [01] -> [02] -> [03] -> [13] -> [23] -> [33]
path2 = P2 arrows: [00] -> [10] -> [20] -> [30] -> [31] -> [32] -> [33]

⇒ max ( 𝐷 (bytes) 2 ⋅ BW path1 , 𝐷 (bytes) 2 ⋅ BW path2 ) = 𝐷 2 ⋅ BW ≥ lower bound

Algo 2

Everything through path 1:

max ( 𝐷 BW path1 , 0 ) = 𝐷 BW path1 > lower bound

BW = 4.5𝑒10. Therefore:

𝐷 2 = 2 × 8 × 128 × 8192 2 = 2 3 ⋅ 2 7 ⋅ 2 13 = 2 23 ⇒ 2 23 4.5 ⋅ 10 10 ≈ 0.00018 = 0.18 ⋅ 10 − 3 s

𝑇comms=6𝜇𝑠+0.18 ms.

Exercise 6 — Pulling it all together

Exercise statement

Imagine you have a big matrix 𝐴: int8[128 * 1024, 128 * 1024] sharded evenly across a TPU v5e 4x4 slice but offloaded to host DRAM on each chip. Let’s say you want to copy the entire array to TPU{0, 0} and multiply it by a vector bf16[8, 128 * 1024]. How long will this take? Hint: use the numbers above.

Solution

Scan pages: 18-20

Question 6 setup, schematic:

Host 0 (4x2)             Host 1 (4x2)
+----+----+              +----+----+
|    |----|--------------|    |    |
+----+----+              +----+----+
|    |    |              |    |    |
+----+----+              +----+----+
|    |    |              |    |    |
+----+----+              +----+----+
|    |    |              |    |    |
+----+----+              +----+----+

No wraparound links.

int8𝐴[128⋅1024,128⋅1024]. Assume each host DRAM has an even part of the array 𝐴⇒1282⋅102422≈8.6GB per host, so it fits in v5e HBM, which is 16GB.

Path

[Host_B] --PCIe--> [TPU] --ICI--> [TPU[0,0]]

[Host_A] --PCIe--> [TPU[0,0]]

Assuming pipelining:

𝑇 comms ( 𝐴 ) ≔ 8.6 GB BW PCIe = 8.6 ⋅ 10 9 1.6 ⋅ 10 10 ≈ 5.3 ⋅ 10 − 1 s = 0.53 s

Bytes inbf16[8,128×1024]=8×128×1024×2≈2GB. So let’s assume it’s in HBM already. Assuming intended operation is:

[ 128 × 1024 , 128 × 1024 ] @ [ 128 × 1024 , 8 ] ⇒ 𝑇 = max ( 𝑇 comms , 𝑇 math ) = max ( 0.53 s , ( 128 ⋅ 1024 ) 2 ⋅ 8 ⋅ 2 1.97 𝑒 14 ) ≈ max ( 0.53 s , 0.001 s ) = 0.53 s .