{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"memory-arithmetic","title":"Bytes per parameter, and the arithmetic of what fits on a card","url":"https://addaly.com/learn/maths-you-need/memory-arithmetic","position":61,"of":76,"minutes":9,"answer":"Memory is parameters times bytes per parameter, so a seven-billion model is 14 GB in fp16 and 3.5 GB in int4.","keyIdea":"Memory is parameters times bytes per parameter, so a seven-billion model is 14 GB in fp16 and 3.5 GB in int4, while training the same model holds about 16 bytes per parameter plus activations that scale with batch and context, which is why fine-tuning needs adapters to fit where inference already did."},"body":{"format":"markdown","text":"## A model is a number of bytes\n\nBefore a model can do anything it has to fit in memory, and the arithmetic for that is a multiplication: parameters times bytes per parameter. The bytes per parameter depend on the number format, which module 8 opens up; for now the four sizes are enough.\n\n```\nfloat32   4 bytes\nbf16/fp16 2 bytes\nint8      1 byte\nint4      0.5 byte\n```\n\nA seven-billion-parameter model:\n\n```\nfloat32  28 GB\nfp16     14 GB\nint8      7 GB\nint4    3.5 GB\n```\n\nSet those against the memory you have. A consumer graphics card has 8, 12, 16 or 24 GB. A laptop with a shared memory pool has 8 to 32 GB, of which the model can use perhaps three quarters. A phone has 6 to 12 GB shared with everything else. So the seven-billion model runs on an 8 GB card in int4, on a 16 GB card in fp16, and on a phone only in int4 with the operating system complaining. A seventy-billion model at int4 is 35 GB and needs either two cards or a machine built for it. None of this requires a benchmark; it is the multiplication.\n\n## Room to think: the KV cache\n\nAt inference a transformer keeps, for every token in the context, the key and value vectors of every layer, so that it need not recompute them for each new token. Per token that is\n\n```\n2 (key and value) × layers × width × bytes\n= 2 × 32 × 4096 × 2 = 524,288 bytes ≈ 0.5 MB\n```\n\nfor a typical seven-billion model in fp16. A 4,096-token context is therefore 2.1 GB on top of the weights, and a 32,000-token context is 17 GB, more than the weights themselves. Serving eight such conversations at once needs eight caches. When a hosted model charges more for long contexts, this is what it is charging for. The course `how-llms-work` covers the engineering around the cache; the size is this one product.\n\n## Training is far larger than the model\n\nTo train, you hold more than the weights. In the usual mixed-precision recipe with Adam, per parameter:\n\n```\nweights in fp16              2 bytes\nmaster copy of weights fp32  4 bytes\ngradient fp16                2 bytes\nAdam first moment fp32       4 bytes\nAdam second moment fp32      4 bytes\n                            16 bytes per parameter\n```\n\nSeven billion parameters times sixteen is 112 GB, before a single activation is stored. That is why full fine-tuning of a seven-billion model needs a data-centre card or several consumer ones, though inference needs one.\n\n> Figure: A seven-billion-parameter model, four ways. Bars (GB of memory): Inference, int4 3.5; Inference, fp16 14; Inference, float32 28; Training: 16 bytes a parameter, before any activations 112. Inference fits on a consumer card and training the same model does not, and almost all of the difference is optimiser state. Freeze the base and train adapters, and the sixteen bytes apply only to the half per cent of parameters you are training.\n\nThen the activations. Backpropagation, from module 3, must keep every layer's inputs until the backward pass. For a transformer block the saved tensors come to about 34 bytes per hidden unit per token per layer in mixed precision, once the attention scores are handled in blocks. For a 2,048-token sequence, width 4,096, 32 layers:\n\n```\n2048 × 4096 × 32 × 34 ≈ 9.1 GB per sequence\n```\n\nA batch of eight is 73 GB. Activation memory scales with batch and sequence length, weight memory does not, and this is why the batch that fits is decided by activations. Gradient checkpointing throws most of them away and recomputes them during the backward pass, trading about a third more compute for a fivefold memory reduction; it exists because of this arithmetic.\n\n## Why adapters made fine-tuning affordable\n\nFreeze the base model and train small added matrices, the LoRA recipe: the 16 bytes per parameter apply only to the adapter's parameters, perhaps 0.5 per cent of the total. The base sits at 2 bytes per parameter, read-only:\n\n```\nbase 7B × 2 bytes         = 14 GB\nadapters 35M × 16 bytes   = 0.56 GB\nactivations, batch 1      ≈ 9 GB (less with checkpointing)\n```\n\nAbout 24 GB, which is one high-end consumer card, against 120 GB or more for full fine-tuning. Quantise the frozen base to int4 and it is under 15 GB. The technique has a name and a paper, but the reason it works on a laptop is that most of the sixteen bytes were optimiser state, and optimiser state exists only for what you train.\n\n## The arithmetic of the data\n\nThe same habit applies to datasets. A text corpus of a billion tokens at two bytes per token id is 2 GB; as raw UTF-8 text at four characters a token, about 4 GB. An image dataset of a million 224 × 224 colour images as raw uint8 pixels is `10^6 × 224 × 224 × 3 = 150 GB`, which will not fit in RAM and must stream; as JPEGs, about a tenth of that on disk, decoded on the fly. Embeddings for ten million documents at 768 float32 dimensions are 30 GB, which is the reason the search lesson exists.\n\n## Three cautions\n\nPeak memory is not average memory: a temporary buffer during a large matrix multiply, or the moment when an optimiser step holds old and new weights together, can add half again. Frameworks reserve memory in blocks, so the number in the monitoring tool is above what the arithmetic says. And a model that fits with 200 MB to spare will fail at the first long input. Leave a fifth free, and when a thing does not fit, do the multiplication again before buying anything."},"check":{"question":"A seven-billion-parameter model runs comfortably for inference on a 24 GB card in fp16. Full fine-tuning with Adam in mixed precision fails to fit. What is the main reason?","options":[{"text":"The training data must be loaded into GPU memory alongside the model.","correct":false,"tempting":"Data streams through in batches; the footprint that breaks the card is per-parameter state and activations, not the dataset."},{"text":"The backward pass doubles the size of the weights, so 14 GB becomes 28 GB and overflows the card.","correct":false,"tempting":"Gradients add the size of the weights, but the master copy and the two Adam moments are the larger part; the total is eight times the fp16 weights, not two."},{"text":"fp16 cannot represent gradients, so training must convert the whole model to float32, doubling it.","correct":false,"tempting":"Mixed precision keeps an fp32 master copy alongside fp16 weights rather than replacing them, and that copy is only a quarter of the training footprint."},{"text":"Training holds about 16 bytes per parameter of state, over 110 GB.","correct":true}]},"prev":{"slug":"quadratic-costs-and-pairwise-work","title":"Anything pairwise is quadratic: distances, attention and deduplication","url":"https://addaly.com/learn/maths-you-need/quadratic-costs-and-pairwise-work"},"next":{"slug":"arithmetic-intensity-and-the-roofline","title":"Why a GPU idles: memory-bound versus compute-bound","url":"https://addaly.com/learn/maths-you-need/arithmetic-intensity-and-the-roofline"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Bytes per parameter, and the arithmetic of what fits on a card', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/memory-arithmetic (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Bytes per parameter, and the arithmetic of what fits on a card","courseTitle":"The Maths You Actually Need","module":{"index":7,"slug":"cost-and-scale","title":"Counting the cost: complexity and the arithmetic of scale"},"answer":null,"keyIdea":"Memory is parameters times bytes per parameter, so a seven-billion model is 14 GB in fp16 and 3.5 GB in int4, while training the same model holds about 16 bytes per parameter plus activations that scale with batch and context, which is why fine-tuning needs adapters to fit where inference already did.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"Inference fits on a consumer card and training the same model does not, and almost all of the difference is optimiser state. Freeze the base and train adapters, and the sixteen bytes apply only to the half per cent of parameters you are training.","series":[{"label":"Inference, int4","value":3.5},{"label":"Inference, fp16","value":14},{"label":"Inference, float32","value":28},{"highlight":true,"label":"Training: 16 bytes a parameter, before any activations","value":112}],"title":"A seven-billion-parameter model, four ways","type":"bars","unit":"GB of memory"},"alt":"Figure: A seven-billion-parameter model, four ways. Bars (GB of memory): Inference, int4 3.5; Inference, fp16 14; Inference, float32 28; Training: 16 bytes a parameter, before any activations 112. Inference fits on a consumer card and training the same model does not, and almost all of the difference is optimiser state. Freeze the base and train adapters, and the sixteen bytes apply only to the half per cent of parameters you are training."}},"blocks":[{"t":"h2","id":"a-model-is-a-number-of-bytes","runs":[{"text":"A model is a number of bytes"}]},{"t":"p","runs":[{"text":"Before a model can do anything it has to fit in memory, and the arithmetic for that is a multiplication: parameters times bytes per parameter. The bytes per parameter depend on the number format, which module 8 opens up; for now the four sizes are enough."}]},{"t":"code","lang":"","label":"Text","text":"float32   4 bytes\nbf16/fp16 2 bytes\nint8      1 byte\nint4      0.5 byte"},{"t":"p","runs":[{"text":"A seven-billion-parameter model:"}]},{"t":"code","lang":"","label":"Text","text":"float32  28 GB\nfp16     14 GB\nint8      7 GB\nint4    3.5 GB"},{"t":"p","runs":[{"text":"Set those against the memory you have. A consumer graphics card has 8, 12, 16 or 24 GB. A laptop with a shared memory pool has 8 to 32 GB, of which the model can use perhaps three quarters. A phone has 6 to 12 GB shared with everything else. So the seven-billion model runs on an 8 GB card in int4, on a 16 GB card in fp16, and on a phone only in int4 with the operating system complaining. A seventy-billion model at int4 is 35 GB and needs either two cards or a machine built for it. None of this requires a benchmark; it is the multiplication."}]},{"t":"h2","id":"room-to-think-the-kv-cache","runs":[{"text":"Room to think: the KV cache"}]},{"t":"p","runs":[{"text":"At inference a transformer keeps, for every token in the context, the key and value vectors of every layer, so that it need not recompute them for each new token. Per token that is"}]},{"t":"code","lang":"","label":"Text","text":"2 (key and value) × layers × width × bytes\n= 2 × 32 × 4096 × 2 = 524,288 bytes ≈ 0.5 MB"},{"t":"p","runs":[{"text":"for a typical seven-billion model in fp16. A 4,096-token context is therefore 2.1 GB on top of the weights, and a 32,000-token context is 17 GB, more than the weights themselves. Serving eight such conversations at once needs eight caches. When a hosted model charges more for long contexts, this is what it is charging for. The course "},{"text":"how-llms-work","code":true},{"text":" covers the engineering around the cache; the size is this one product."}]},{"t":"h2","id":"training-is-far-larger-than-the-model","runs":[{"text":"Training is far larger than the model"}]},{"t":"p","runs":[{"text":"To train, you hold more than the weights. In the usual mixed-precision recipe with Adam, per parameter:"}]},{"t":"code","lang":"","label":"Text","text":"weights in fp16              2 bytes\nmaster copy of weights fp32  4 bytes\ngradient fp16                2 bytes\nAdam first moment fp32       4 bytes\nAdam second moment fp32      4 bytes\n                            16 bytes per parameter"},{"t":"p","runs":[{"text":"Seven billion parameters times sixteen is 112 GB, before a single activation is stored. That is why full fine-tuning of a seven-billion model needs a data-centre card or several consumer ones, though inference needs one."}]},{"t":"p","runs":[{"text":"Then the activations. Backpropagation, from module 3, must keep every layer’s inputs until the backward pass. For a transformer block the saved tensors come to about 34 bytes per hidden unit per token per layer in mixed precision, once the attention scores are handled in blocks. For a 2,048-token sequence, width 4,096, 32 layers:"}]},{"t":"code","lang":"","label":"Text","text":"2048 × 4096 × 32 × 34 ≈ 9.1 GB per sequence"},{"t":"p","runs":[{"text":"A batch of eight is 73 GB. Activation memory scales with batch and sequence length, weight memory does not, and this is why the batch that fits is decided by activations. Gradient checkpointing throws most of them away and recomputes them during the backward pass, trading about a third more compute for a fivefold memory reduction; it exists because of this arithmetic."}]},{"t":"h2","id":"why-adapters-made-fine-tuning-affordable","runs":[{"text":"Why adapters made fine-tuning affordable"}]},{"t":"p","runs":[{"text":"Freeze the base model and train small added matrices, the LoRA recipe: the 16 bytes per parameter apply only to the adapter’s parameters, perhaps 0.5 per cent of the total. The base sits at 2 bytes per parameter, read-only:"}]},{"t":"code","lang":"","label":"Text","text":"base 7B × 2 bytes         = 14 GB\nadapters 35M × 16 bytes   = 0.56 GB\nactivations, batch 1      ≈ 9 GB (less with checkpointing)"},{"t":"p","runs":[{"text":"About 24 GB, which is one high-end consumer card, against 120 GB or more for full fine-tuning. Quantise the frozen base to int4 and it is under 15 GB. The technique has a name and a paper, but the reason it works on a laptop is that most of the sixteen bytes were optimiser state, and optimiser state exists only for what you train."}]},{"t":"h2","id":"the-arithmetic-of-the-data","runs":[{"text":"The arithmetic of the data"}]},{"t":"p","runs":[{"text":"The same habit applies to datasets. A text corpus of a billion tokens at two bytes per token id is 2 GB; as raw UTF-8 text at four characters a token, about 4 GB. An image dataset of a million 224 × 224 colour images as raw uint8 pixels is "},{"text":"10^6 × 224 × 224 × 3 = 150 GB","code":true},{"text":", which will not fit in RAM and must stream; as JPEGs, about a tenth of that on disk, decoded on the fly. Embeddings for ten million documents at 768 float32 dimensions are 30 GB, which is the reason the search lesson exists."}]},{"t":"h2","id":"three-cautions","runs":[{"text":"Three cautions"}]},{"t":"p","runs":[{"text":"Peak memory is not average memory: a temporary buffer during a large matrix multiply, or the moment when an optimiser step holds old and new weights together, can add half again. Frameworks reserve memory in blocks, so the number in the monitoring tool is above what the arithmetic says. And a model that fits with 200 MB to spare will fail at the first long input. Leave a fifth free, and when a thing does not fit, do the multiplication again before buying anything."}]}],"check":{"question":[{"text":"A seven-billion-parameter model runs comfortably for inference on a 24 GB card in fp16. Full fine-tuning with Adam in mixed precision fails to fit. What is the main reason?"}],"options":[{"text":[{"text":"The training data must be loaded into GPU memory alongside the model."}],"correct":false,"tempting":[{"text":"Data streams through in batches; the footprint that breaks the card is per-parameter state and activations, not the dataset."}]},{"text":[{"text":"The backward pass doubles the size of the weights, so 14 GB becomes 28 GB and overflows the card."}],"correct":false,"tempting":[{"text":"Gradients add the size of the weights, but the master copy and the two Adam moments are the larger part; the total is eight times the fp16 weights, not two."}]},{"text":[{"text":"fp16 cannot represent gradients, so training must convert the whole model to float32, doubling it."}],"correct":false,"tempting":[{"text":"Mixed precision keeps an fp32 master copy alongside fp16 weights rather than replacing them, and that copy is only a quarter of the training footprint."}]},{"text":[{"text":"Training holds about 16 bytes per parameter of state, over 110 GB."}],"correct":true,"tempting":null}],"order":[1,0,3,2],"why":null},"prev":{"slug":"quadratic-costs-and-pairwise-work","title":"Anything pairwise is quadratic: distances, attention and deduplication","minutes":9},"next":{"slug":"arithmetic-intensity-and-the-roofline","title":"Why a GPU idles: memory-bound versus compute-bound","minutes":10}}}