AI Run Lab

All chapters

Precision, memory and computation

A billion model weights must be stored, moved and used in arithmetic. The chosen number format affects all three.

Open this chapter with experiments and audio

How many bytes does a weight need?

Eight bits make one byte. A 32-bit floating-point number occupies four bytes and a 16-bit number occupies two. Multiplying the number of weights by bytes per weight estimates raw storage. Use a decimal gigabyte of one billion bytes throughout this example. A gibibyte, often used by software, is 2³⁰ bytes and therefore gives a different displayed number.

Weights16-bit storage4-bit packed storage
1 million2 MB0.5 MB
1 billion2 GB0.5 GB
7 billion14 GB3.5 GB

The four-bit column assumes that two values share a byte. Actual formats usually need additional scales and metadata. Some layers can remain at higher precision. Those details explain why a downloaded file can be larger than the simple multiplication suggests.

What changes when you reduce precision?

Imagine representing values at increments of 0.1. The value 0.34 rounds to 0.3 and 0.36 rounds to 0.4. A downstream sum now acts on slightly different inputs. This is a toy uniform grid. Floating-point formats instead use an exponent and a significand, so attainable values are spaced more widely at larger magnitudes.

FP16 and BF16 each occupy sixteen bits. BF16 allocates more bits to the exponent than FP16 and covers a wider magnitude range, with fewer significant bits. Equal storage therefore does not imply equal numerical behaviour. Mixed-precision systems can keep selected reductions or updates at higher precision to limit accumulated error.

Consider summing one very large number, one small number and the negative of the large number. With finite precision, adding the small number to the large one can round away its entire contribution. Cancelling the large numbers first can preserve it. A parallel reduction may add in a different order than a simple loop. Two correct implementations can therefore differ slightly, which is why numerical tests commonly use tolerances.

PyTorch · Numerical accuracy

At 2²⁴, adjacent representable Float32 values above the base are two units apart. The integer 16,777,217 lies midway between them and rounds to 16,777,216. Move the base to 2²³. Every integer in the displayed range now has an exact Float32 representation.

Can the model fit in memory?

At seven billion parameters, moving from sixteen to four bits saves 10.5 GB of raw weight storage. Inference also requires temporary working memory and, for many language models, a cache of attention keys and values. Longer prompts and more simultaneous requests usually increase that cache. Training requires additional space for gradients, saved activations and often optimizer state.

A graphics processor can perform many arithmetic operations in parallel. It may still wait for values to arrive from memory. Smaller weights can reduce this traffic if efficient kernels support the format. A fourfold decrease in storage therefore does not predict a fourfold decrease in response time.

Check the arithmetic on your computer

count = 7_000_000_000
for bits in [32, 16, 8, 4]:
    size_bytes = count * bits / 8
    print(bits, round(size_bytes / 1e9, 2), "GB",
          round(size_bytes / (2 ** 30), 2), "GiB")
A machine has exactly 14 GB free. Does a 7-billion-parameter model at 16 bits fit for generation?

The arithmetic accounts for 14 GB of raw weights alone. Successful generation would depend on extra memory, representation and possibly offloading. The raw-weight figure alone cannot establish that it fits.

PyTorch · Tensor data types

Why can adding the same numbers give two results?

A processor has a limited number of bits for the significant digits of a floating-point value. With the usual binary32 format, around 100,000,000 the gap between adjacent representable values is 8. Adding 1 to that number produces a value which rounds right back to 100,000,000. The spacing is much smaller close to zero. The number 1 is perfectly representable by itself.

Now add 100,000,000, 1 and −100,000,000. Round to binary32 after each addition, as a sequence of single-precision additions would do. Add the first two terms first and the intermediate value loses the 1. Subtract 100,000,000 and the answer is 0. Add the large positive and negative terms together first, then add 1. The answer is 1. On paper, reordering a sum preserves its value. Here the storage format acts between the operations.

from struct import pack, unpack

def f32(x):
    return unpack("!f", pack("!f", x))[0]

a = f32(100_000_000)
print(f32(f32(a + 1) - a), f32(f32(a - a) + 1))
Why might a parallel reduction disagree slightly with a serial loop?

Parallel workers may add separate groups and then combine their subtotals, changing where rounding occurs. This example deliberately puts cancellation and very different magnitudes together to expose the effect. Its size does not predict the error in a particular network. You need its values, data types and order of operations for that.

Inspect the gaps between representable integers →

Further reading · PyTorch · Numerical accuracy