0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Can a 24 GB card fine-tune a 7B model?
Dev does a quick calculation: seven billion parameters, two bytes each, fourteen gigabytes. A twenty-four-gigabyte graphics card should be plenty for fine-tuning, right? Not even close. Running the model needs about fourteen gigabytes for its weights. But fully fine-tuning it with the usual optimiser needs over a hundred gigabytes, before counting anything else. And in the other direction, the same model can be squeezed to about three and a half gigabytes, small enough to run on a laptop. Today: the memory maths every AI engineer needs.
Number formats, why training needs so much more memory than running, and quantisation, the art of storing numbers with fewer bits.
0:57Where this fits in the course
Season Five, Module Nineteen, stop two: memory maths, precision and quantisation.
1:04Number formats: FP32, FP16, BF16, INT8, INT4
Every parameter is a number, and how much memory it takes depends on its format. Standard thirty-two-bit floating point, F-P thirty-two, uses four bytes. Floating-point numbers have an exponent, which sets how big or tiny a number can be, its range, and a mantissa, which sets how many significant digits it keeps, its precision. Sixteen-bit formats halve the memory. F-P sixteen keeps decent precision but a small range: numbers above about sixty-five thousand overflow, which can break training. B-F sixteen, brain float, makes a different trade: it keeps F-P thirty-two's full range, with fewer digits of precision.
That makes it much safer for training, and it's now standard for large models. Integer formats go further: eight-bit integers, one byte, with two hundred and fifty-six possible values, and four-bit, half a byte, with just sixteen. For a seven-billion-parameter model, the weights alone take twenty-eight gigabytes in F-P thirty-two, fourteen in B-F sixteen, seven in eight-bit, and three and a half in four-bit.
2:17GPU memory for running a model
Running a model, inference, needs room for three things: the weights, fourteen gigabytes in B-F sixteen; the K-V cache from episode forty-four, which grows with the length of the conversation and the number of users, about two gigabytes for one four-thousand-token conversation in our example design; and a little working memory. When many customers are served at once, the K-V cache can grow to rival the weights themselves, a key challenge for the inference module coming up.
2:51GPU memory for fine-tuning: the 16-bytes rule
Fine-tuning needs much more, and here's exactly why. The weights, in B-F sixteen: two bytes per parameter. A gradient for every weight, from backpropagation: another two bytes. A precise thirty-two-bit master copy of the weights, so that tiny updates aren't lost to rounding: four bytes. And the Adam optimiser from episode twenty-seven keeps two running averages per parameter, momentum and squared gradients, in thirty-two-bit: eight more bytes. That's about sixteen bytes per parameter. For seven billion parameters: a hundred and twelve gigabytes, eight times the memory needed to run the model, and that's before activations, the intermediate values stored during the forward pass for use in backpropagation.
Those grow with batch size and text length. A technique called gradient checkpointing saves memory by recomputing some activations during the backward pass instead of storing them, trading extra computation for memory. So Dev's single twenty-four-gigabyte card can't do full fine-tuning. Next episode, we'll see the clever methods that make it possible anyway.
4:05Quantisation by hand: scale, round, dequantise
Quantisation stores numbers with fewer bits. The idea: choose a scale, divide every weight by it, round to the nearest integer, and store those integers. When you need the weights, multiply back by the scale. Let's do it by hand with five weights. For eight-bit quantisation, the weight furthest from zero, minus one point two seven four, should map to minus one hundred and twenty-seven, so the scale is one point two seven four over a hundred and twenty-seven, about zero point zero one. Divide and round: eighty-two, minus forty-two, five, minus one hundred and twenty-seven, thirty-four.
Multiply back: zero point eight two two six, minus zero point four two one three, and so on. The biggest error is about four thousandths. Very close. Now four-bit. We use fifteen of its sixteen levels, from minus seven to seven, so it stays symmetric around zero. The scale becomes zero point one eight two. The weights become five, minus two, zero, minus seven, two. Multiply back, and the errors grow to almost nine hundredths, and the small weight, zero point zero five two, has rounded all the way down to zero.
5:29Doing quantisation well: outliers, PTQ and QAT
Practical quantisation adds a few refinements. Instead of one scale for a whole matrix, use a separate scale for each small block of weights, so one unusually large weight doesn't force a coarse grid on all the others. Watch for outliers: a handful of very large values in weights or activations can ruin the scale, so they're often kept in higher precision. There are two main approaches. Post-training quantisation takes a finished model and quantises it, using a small sample of data to choose good scales. It's quick.
Quantisation-aware training simulates the rounding during training, so the model learns to cope with it, giving better quality at very low bit widths, at the cost of extra training. Fewer bits mean smaller and often faster models, but quality can drop, sometimes more on reasoning, maths or less common languages than on everyday chat. As always, check on your own scorecard.
6:29Try it yourself: a 13-billion-parameter model
Let's size a thirteen-billion-parameter model. How much memory do its weights need at each precision? Pause. At thirty-two bits, fifty-two gigabytes. At sixteen bits, twenty-six, which already doesn't fit on a twenty-four-gigabyte card. At eight bits, thirteen, which fits with room for the KV cache. At four bits, six and a half. And fully fine-tuning it, at about sixteen bytes per parameter, would need over two hundred gigabytes. A few multiplications tell you what hardware you need before you spend a penny.
7:11Why training keeps a 32-bit copy of the weights
Here's why mixed-precision training keeps a thirty-two-bit copy of the weights. In BF16, the next number after one is about one point zero zero seven eight: there's nothing in between. Now suppose a weight of exactly one receives a tiny update of one thousandth. One point zero zero one is closer to one than to the next BF16 value, so it rounds straight back to one. The update simply vanishes. Repeat that across thousands of steps, and learning stalls. So the updates are applied to a full-precision master copy, while the heavy maths runs in fast sixteen-bit numbers.
7:53Range vs precision: FP16 overflow
Number formats trade range against precision. FP16 is fairly precise, but its largest value is only about sixty-five thousand. If an activation reaches seventy thousand, it overflows to infinity, and training collapses into "not a number". BF16 makes the opposite trade: less precision, but the same enormous range as thirty-two-bit numbers, so overflow is rare. That's why BF16 has become so popular for training. FP16 can still work, with a technique called loss scaling to keep tiny gradients from disappearing.
8:33Try it yourself: one outlier spoils the scale
Remember the four-bit example? One large weight, minus one point two seven four, set the scale for everyone, so the small weights were rounded coarsely, with errors up to zero point zero eight seven. What if we split the weights into two groups, each with its own scale? Pause and predict whether the error shrinks.
It does. The first group gets a finer scale, because its largest weight is only zero point eight two three, and the worst error drops to about zero point zero five three. The price is storing one extra scale. Real quantisers use small groups, often a few dozen weights each, for exactly this reason: outliers stay contained.
9:22Quantising the KV cache too
Weights aren't the only thing that can be compressed. Remember the KV cache, forty gibibytes for twenty conversations in sixteen-bit numbers? Store it in eight bits, and it halves to twenty. And if the model was also built with grouped-query attention, a design choice made when it was trained, it falls to five: room for up to eight times as many conversations. But compressing the cache can affect accuracy on long conversations, so test those specifically before switching it on.
9:57Did quantisation hurt? How to measure it
Never assume quantisation is free. Compare the quantised model with the original on the golden set and every gate, on each language, on long conversations, on arithmetic and dates, and on JSON validity. Perplexity on Brightcart's own text is a quick early warning too. If the differences sit within the error bars and every gate still passes, ship the smaller, cheaper, faster model. If a particular slice suffers, try a gentler setting, like eight bits for sensitive layers.
10:32Pause & Predict: will it fit on a 24 GB card?
Brightcart has a twenty-four-gigabyte card. Will each plan fit? A seven-billion-parameter model in sixteen bits, serving ten conversations. A thirteen-billion model in sixteen bits. The thirteen-billion model in four bits. Pause.
The first fits: fourteen gigabytes of weights, about five for the KV cache, plus some overhead. The second doesn't: its weights alone are twenty-six gigabytes. The third fits comfortably, at six and a half gigabytes of weights. The rule is always the same: weights, plus KV cache, plus overhead, must fit in memory.
11:21Where the bytes go during fine-tuning
Let's revisit where the hundred and twelve gigabytes go when fully fine-tuning a seven-billion-parameter model. Fourteen for the weights, fourteen for the gradients, twenty-eight for the full-precision master copy, and fifty-six for Adam's two running averages, before counting activations. Each piece can be attacked. Freeze most of the weights, and most gradients and optimiser states disappear. Compress the frozen weights to four bits. Recompute activations instead of storing them. Next episode, we'll combine these tricks to fine-tune on a single twenty-four-gigabyte card.
12:00Quantise after training, or train for it?
There are two broad ways to quantise. Post-training quantisation takes a finished model, works out the scales, often by running a small sample of typical text through it, and converts it. It's quick and cheap, and usually works well at eight bits, though it gets riskier at four or fewer. Quantisation-aware training simulates the rounding during training, so the model learns weights that survive it, which takes more effort but holds up better at very low precision. And many setups keep the most sensitive parts, like the embedding and output layers, at higher precision.
12:38Myth vs Fact: does it fit, so can I fine-tune it?
Myth: if a model fits on your graphics card to run, you can fine-tune it on the same card. Fact: running needs about two bytes per parameter, while full fine-tuning with Adam needs about sixteen, plus activations. For a seven-billion-parameter model, that's fourteen gigabytes versus well over a hundred.
13:01One-minute recap
Let's recap. Each parameter costs four bytes in F-P thirty-two, two in B-F sixteen, one in eight-bit and half a byte in four-bit. B-F sixteen keeps F-P thirty-two's range with less precision, which suits training. Running a model needs its weights plus the K-V cache. Full fine-tuning with Adam needs about sixteen bytes per parameter, a hundred and twelve gigabytes for seven billion, plus activations. And quantisation shrinks models by storing scaled, rounded integers, with errors that grow as the bits shrink. One sentence to remember: running a model costs its weights; training it costs its weights, gradients and the optimiser's memory too.
13:47Next: LoRA and QLoRA
Mission complete. Dev now knows that fully fine-tuning a seven-billion-parameter model needs over a hundred gigabytes, far beyond one card, while a four-bit version could run in about three and a half. So can we fine-tune it on a single twenty-four-gigabyte card anyway? Yes, by training only a tiny number of new parameters, and squeezing the rest down to four bits. LoRA and QLoRA, next. See you on the map.