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 =
- FLOPs/s bf16 =
- HBM capacity =
(bf16). Split across 32 TPUs v4p Bytes per TPU:
So it does fit.
Each TPU HBM has of weights to send to the Tensor Cores:
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:
- Host size:
- HBM per chip:
- FLOPs bf16 per chip =
- Host size:
: how many blocks of fit:
One host (schematic):
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
4 hosts to cover one “row”, 8 hosts to cover one “column” I could also just flatten:
[ ][ ][ ][ ][ ][ ][ ][ ] ...
<---------- 8 ---------> is a host
and then divide i.e hosts.
TPU v5e has only one Tensor Core per chip Also you can derive by cores per host and from the book you see using previous result: where 32 is hosts in the pod and 8 is cores per host.
Total FLOPs/s =
Total HBM =
TPU v5p pod
- Pod Size:
- Host Size:
Flatten
Total Hosts =
Tensor Cores =
Total HBM:
Total 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 . 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.
- , .
- PCIe BW = .
- FLOPs/s = .
Assuming I can overlap communication with compute:
Using and :
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.
- 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?
- 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 =
- FLOPs/s int8 =
VMEM BW HBM BW
Note: , so let and as before.
Since: VMEM = .
. At least the weights do fit in VMEM.
Bottleneck: compute bound when?
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 .
- How soon will the first byte arrive at its destination?
- 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 slice has no wraparound.
Assume for one hop. Then, assuming pipelining:
first byte arrives after .
Different algos, but a lower bound on time is:
since it needs to receive bytes at :
|
|
v
----> [TPU[3, 3]]
Algo 1
Data Split =
path1 = P1 arrows: [00] -> [01] -> [02] -> [03] -> [13] -> [23] -> [33]
path2 = P2 arrows: [00] -> [10] -> [20] -> [30] -> [31] -> [32] -> [33]
Algo 2
Everything through path 1:
BW = . Therefore:
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.
. Assume each host DRAM has an even part of the array per host, so it fits in v5e HBM, which is .
Path
[Host_B] --PCIe--> [TPU] --ICI--> [TPU[0,0]]
[Host_A] --PCIe--> [TPU[0,0]]
Assuming pipelining:
. So let’s assume it’s in HBM already. Assuming intended operation is: