There are three things involved when running algorithms on hardware:
- How fast can it do math (OPs/second)
- How fast can we move the data around ie bandwidth (bytes/second)
- How much much data can we store ie memory (bytes) Using a roofline model can help us upper and lower bound the time of a given computation with the three factors in mind.
For computation, deep learning is just matmuls, and each matmul is just floating-point multiplication and addition operations (FLOPs). The accelerator speed determines how long this takes.
For communication within a chip, tensors need to be transferred between accelerator memory or HBM and the compute cores. On an h100 its about 3.35TB/s and a TPU v6e about 1.6TB/s.
Then to communicate between chips we can use a few hardware options (ICI, DCN, PCIe), each of them have different bandwidths. This happens when we want to distribute a model across multiple accelerators.
Between and within chips, we measure speed of communication as bytes/s and estimate the total communication time as
We can usually overlap the computation within a chip and communication within the chip. This means that we can lower-bound the training and inference time by using the maximum of the two times. Then the upper bound can be the sum of the two.
If we assume a perfect overlap in the communication steps and the computation then we can differentiate the bounds. if time to compute is longer than comm then we are compute-bound and when comm is longer, communication-bound. One way to look at which type we fall into is using arithmetic intensity and operational intensity.
The arithmetic intensity of an algorithm is given by the ratio of the total FLOPs it performs to the number of bytes it needs to communicate.
When arithmetic intensity is high, the FLOPs per byte is high, meaning that time to compute is large compared to time to communicate, and we use more of the available FLOPs. When the opposite is true, we spend more time communicating and end up with idle FLOPs. The point where this crosses over is the peak arithmetic intensity of our hardware. The ratio of peak accelerator FLOPs/s to accelerator bandwidth.
The intensity of acceleration is the arithmetic intensity at which the accelerator achieves the peak FLOPs/s.
[!example] The dot product: if we want to do where the function is
bf16[N], bf16[N] -> bf16[1]
So two N dim vectors of floating point 16, to 1 floating point 16 value. The steps are, load x and y into memory, each of which have 2N bytes (bf16 is 2 bytes large). Then we perform N multiplications and N-1 additions and then write 2 bytes back into HBM
Recall intensity is so we get
Note: theres a hidden limit as N -> Inf So the dot product has an arithmetic intensity of 1/2 ie the dot product does 0.5 floating point ops per byte loaded. So the arithmetic intensity is lower than the hardware and so the dot product is thus, communication-bound.
[!example] Matrix multiplication: Matmuls are X * Y -> Z where X, Y has shape
bf16[B,D], bf16[D,F]respectively and Z has shapebf[B,F]. So X has BD bf16s so 2BD bytes and Y has DF bf16s and 2DF bytes. Doing some breakdown we get for every entry in Z, we do 2D FLOPs, and since Z is BF in size, we do 2DBF FLOPs. So now wan can try and calculate intensity
Now we can do a nice simplification if we assume the batch size B is small relative to D and F
This is actually a reasonable assumption since batch size B is typically less that 1024 and D and F and usually much larger. So, we generally become compute bound when batch size B is greater than 240 tokens.
We can also use rooflines for communication. This is because in most instances of large training or inference, we distribute matrix multiplications across multiple TPUs.
[!question 1 int8 matmuls]
- How many bytes need to be loaded from memory? How many need to be written back to memory? ans: X and Y have BD and DF int8s respectively, since each int8 is 1 byte, we have BD and DF total bytes needed to be loaded. Since Z has BF int8s we then need BF bytes written back into HBM.
- How many total OPs are performed? ans: We need 2DBF FLOPs
- What is the arithmetic intensity? ans: the arithmetic intensity is and then if we assume B is small, then we get
- What is a roofline estimate for Tmath and Tcomms? What are reasonable upper and lower bounds for the runtime of the whole operation? ans: and the lower bound is and upper bound is
Question 2
In practice we often do different weight vs. activation quantization, so we might store our weights in very low precision but keep activations (and compute) in a higher precision. Say we want to quantize our weights in int8 but keep activations (and compute) in bfloat16. At what batch size do we become compute bound? Assume 1.97e14 bfloat16 FLOPs/s.
ans: Total bytes = and FLOPs = the same as before, using the same small B assumption we get arithmetic intensity of > 240 so B > 120 is when we become compute bound