{"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":"quantisation-as-rounding","title":"Quantisation is rounding with a scale: int8, int4 and the outlier problem","url":"https://addaly.com/learn/maths-you-need/quantisation-as-rounding","position":71,"of":76,"minutes":10,"answer":"Quantisation stores each weight as round(w / scale) with the scale set by the largest value in its group.","keyIdea":"Quantisation stores each weight as round(w / scale) with the scale set by the largest value in its group, so the error is half a step for everyone and a single outlier can round an entire tensor to zero, which is why scales are kept per channel or per block of 128."},"body":{"format":"markdown","text":"## The whole idea in one line\n\nTo store a float weight as an 8-bit integer, pick a scale and round:\n\n```\nq = round(w / scale)          an integer from −127 to 127\nw ≈ q × scale                 to recover it\n```\n\nThe scale is chosen so that the largest weight in the tensor lands on 127: `scale = max|w| / 127`. That is symmetric int8 quantisation, and it is most of what the word means. The course `running-models-yourself` covers the tools; this lesson is the arithmetic those tools perform.\n\n## One weight, by hand\n\nA tensor whose largest weight is 0.02 has `scale = 0.02 / 127 = 0.000157`. A weight of 0.0034:\n\n```\nq = round(0.0034 / 0.000157) = round(21.6) = 22\nw ≈ 22 × 0.000157 = 0.003465\nerror = 0.000065, about 2 per cent of the weight\n```\n\nThe error of any weight is at most half a scale step, `0.0000787` here. For weights near the maximum that is 0.4 per cent; for weights a tenth the size it is 4 per cent; for a weight of 0.00005 it rounds to zero and the error is 100 per cent. Quantisation is exact for the large values and brutal for the small ones, because the step size is set by the largest.\n\n## The outlier problem\n\nNow suppose that same tensor contains one weight of 5.0. The scale becomes `5 / 127 = 0.039`. The weight of 0.0034 is `round(0.086) = 0`. So is every weight below 0.02: the whole tensor apart from the outlier rounds to zero or to one step. One number has destroyed a million.\n\nThis is not hypothetical. Large language models develop a few channels with activations tens or hundreds of times larger than the rest, and naive int8 quantisation of those activations wrecks the model while the weights alone quantise fine. Three responses, all arithmetic:\n\n> Figure: What one outlier does to a tensor of 4,096 weights. Bars (per cent of the weights that round to zero at int8): No outlier, one scale for the whole tensor 1; One weight of 5.0, one scale for the whole tensor 95; One weight of 5.0, a scale per block of 128 4. The step size is set by the largest value in the group, so one weight a hundred times the rest rounds almost everything else to zero. A scale per channel or per block of 128 confines the damage to the block the outlier sits in, for about an eighth of a bit per weight.\n\n1. **Per-channel scales.** One scale per row or column instead of per tensor, so the outlier only sets the step for its own row. Costs one float per row; nearly free.\n2. **Group-wise scales.** One scale per block of 64 or 128 weights. An int4 model with a 16-bit scale per 128 weights spends `4 + 16/128 = 4.125` bits per weight, and the outlier's damage is confined to its block of 128.\n3. **Move the outlier.** Multiply an activation channel by a constant and divide the matching weight row by the same constant; the product is unchanged, and the difficulty has been shifted from the activations, which are hard to quantise, to the weights, which are easy. This is what the smoothing methods do.\n\n## Asymmetric, and the zero-point\n\nIf the values are not centred, an activation after a ReLU is never negative, half the integer range is wasted. The asymmetric form adds an offset:\n\n```\nq = round(w / scale) + zero_point\n```\n\nso that the minimum maps to 0 and the maximum to 255 in uint8. The zero-point is chosen so that a real zero maps exactly onto an integer, because padding and ReLU produce exact zeros in quantity and rounding them to something else adds a bias everywhere.\n\n## Four bits\n\nInt4 has sixteen levels. With a per-tensor scale that is hopeless; with group-wise scales it works for weights, and int4 weights with fp16 activations is the standard recipe for running a large model on a small device. The arithmetic of why: weights are static, so they can be quantised carefully, once, with all the tricks above; activations change with every input and must be quantised on the fly, which is why they are usually left at higher precision.\n\nThe error introduced is measurable and it is not zero. A model at int4 typically loses a few tenths of a point of perplexity relative to fp16; at int3 it loses several points and at int2 it stops working. The loss is larger for smaller models, because they have fewer redundant weights to absorb the noise, and larger in the layers nearest the output. Anyone claiming int4 is free has not measured it on their own task; the honest statement is that it is cheap and the price is known.\n\n## Why it is faster, and when it is not\n\nModule 7's arithmetic-intensity lesson said single-token generation is bound by streaming the weights. Int4 weights are a quarter of the bytes, so generation is up to four times faster, before any change in the arithmetic. For large-batch throughput, which is compute-bound, the gain depends on whether the hardware has integer matrix units; if it must convert int4 back to fp16 before multiplying, there is no speed-up at all, only memory saved.\n\n## Doing it yourself\n\n```python\nimport numpy as np\nw = np.random.randn(4096).astype(np.float32) * 0.01\nw[17] = 5.0                                   # one outlier\nscale = np.abs(w).max() / 127\nq = np.round(w / scale).astype(np.int8)\nprint((q == 0).mean())                        # fraction of weights that became zero: nearly all\nrow_scale = np.abs(w[:2048]).max() / 127      # a group without the outlier\nq2 = np.round(w[:2048] / row_scale)\nprint((q2 == 0).mean())                       # a few per cent\n```\n\nRun it, then delete the outlier line and run it again. The difference between the two fractions is the whole reason quantisation papers exist."},"check":{"question":"A tensor of weights mostly between −0.02 and 0.02 also contains one weight of 5.0. It is quantised to int8 with a single scale for the whole tensor. What happens to the typical weight?","options":[{"text":"It is stored with about 2 per cent error, as the arithmetic for a 0.0034 weight showed.","correct":false,"tempting":"That figure assumed the scale was set by 0.02; with the outlier the scale is 0.039, two hundred times coarser."},{"text":"It rounds to zero: the step is now 0.039 and the weight is under half of that.","correct":true},{"text":"The outlier is clipped to 127 × scale and the rest are unaffected.","correct":false,"tempting":"Clipping happens only if the scale is chosen below the maximum; with the scale set from the maximum, the outlier is exact and everything else pays."},{"text":"The int8 range overflows and the values wrap to negative numbers.","correct":false,"tempting":"Rounding w / scale never exceeds 127 when the scale is max|w| / 127; the damage is loss of the small values, not overflow of the large one."}]},"prev":{"slug":"integers-overflow-and-the-size-of-things","title":"Integers, overflow, and the token id that would not fit","url":"https://addaly.com/learn/maths-you-need/integers-overflow-and-the-size-of-things"},"next":{"slug":"condition-numbers","title":"The condition number, and how many digits a problem throws away","url":"https://addaly.com/learn/maths-you-need/condition-numbers"},"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) 'Quantisation is rounding with a scale: int8, int4 and the outlier problem', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/quantisation-as-rounding (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":"Quantisation is rounding with a scale: int8, int4 and the outlier problem","courseTitle":"The Maths You Actually Need","module":{"index":8,"slug":"numbers-in-a-machine","title":"Numbers inside a machine"},"answer":null,"keyIdea":"Quantisation stores each weight as round(w / scale) with the scale set by the largest value in its group, so the error is half a step for everyone and a single outlier can round an entire tensor to zero, which is why scales are kept per channel or per block of 128.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"The step size is set by the largest value in the group, so one weight a hundred times the rest rounds almost everything else to zero. A scale per channel or per block of 128 confines the damage to the block the outlier sits in, for about an eighth of a bit per weight.","series":[{"label":"No outlier, one scale for the whole tensor","value":1},{"highlight":true,"label":"One weight of 5.0, one scale for the whole tensor","value":95},{"label":"One weight of 5.0, a scale per block of 128","value":4}],"title":"What one outlier does to a tensor of 4,096 weights","type":"bars","unit":"per cent of the weights that round to zero at int8"},"alt":"Figure: What one outlier does to a tensor of 4,096 weights. Bars (per cent of the weights that round to zero at int8): No outlier, one scale for the whole tensor 1; One weight of 5.0, one scale for the whole tensor 95; One weight of 5.0, a scale per block of 128 4. The step size is set by the largest value in the group, so one weight a hundred times the rest rounds almost everything else to zero. A scale per channel or per block of 128 confines the damage to the block the outlier sits in, for about an eighth of a bit per weight."}},"blocks":[{"t":"h2","id":"the-whole-idea-in-one-line","runs":[{"text":"The whole idea in one line"}]},{"t":"p","runs":[{"text":"To store a float weight as an 8-bit integer, pick a scale and round:"}]},{"t":"code","lang":"","label":"Text","text":"q = round(w / scale)          an integer from −127 to 127\nw ≈ q × scale                 to recover it"},{"t":"p","runs":[{"text":"The scale is chosen so that the largest weight in the tensor lands on 127: "},{"text":"scale = max|w| / 127","code":true},{"text":". That is symmetric int8 quantisation, and it is most of what the word means. The course "},{"text":"running-models-yourself","code":true},{"text":" covers the tools; this lesson is the arithmetic those tools perform."}]},{"t":"h2","id":"one-weight-by-hand","runs":[{"text":"One weight, by hand"}]},{"t":"p","runs":[{"text":"A tensor whose largest weight is 0.02 has "},{"text":"scale = 0.02 / 127 = 0.000157","code":true},{"text":". A weight of 0.0034:"}]},{"t":"code","lang":"","label":"Text","text":"q = round(0.0034 / 0.000157) = round(21.6) = 22\nw ≈ 22 × 0.000157 = 0.003465\nerror = 0.000065, about 2 per cent of the weight"},{"t":"p","runs":[{"text":"The error of any weight is at most half a scale step, "},{"text":"0.0000787","code":true},{"text":" here. For weights near the maximum that is 0.4 per cent; for weights a tenth the size it is 4 per cent; for a weight of 0.00005 it rounds to zero and the error is 100 per cent. Quantisation is exact for the large values and brutal for the small ones, because the step size is set by the largest."}]},{"t":"h2","id":"the-outlier-problem","runs":[{"text":"The outlier problem"}]},{"t":"p","runs":[{"text":"Now suppose that same tensor contains one weight of 5.0. The scale becomes "},{"text":"5 / 127 = 0.039","code":true},{"text":". The weight of 0.0034 is "},{"text":"round(0.086) = 0","code":true},{"text":". So is every weight below 0.02: the whole tensor apart from the outlier rounds to zero or to one step. One number has destroyed a million."}]},{"t":"p","runs":[{"text":"This is not hypothetical. Large language models develop a few channels with activations tens or hundreds of times larger than the rest, and naive int8 quantisation of those activations wrecks the model while the weights alone quantise fine. Three responses, all arithmetic:"}]},{"t":"ol","start":1,"items":[[{"text":"Per-channel scales.","b":true},{"text":" One scale per row or column instead of per tensor, so the outlier only sets the step for its own row. Costs one float per row; nearly free."}],[{"text":"Group-wise scales.","b":true},{"text":" One scale per block of 64 or 128 weights. An int4 model with a 16-bit scale per 128 weights spends "},{"text":"4 + 16/128 = 4.125","code":true},{"text":" bits per weight, and the outlier’s damage is confined to its block of 128."}],[{"text":"Move the outlier.","b":true},{"text":" Multiply an activation channel by a constant and divide the matching weight row by the same constant; the product is unchanged, and the difficulty has been shifted from the activations, which are hard to quantise, to the weights, which are easy. This is what the smoothing methods do."}]]},{"t":"h2","id":"asymmetric-and-the-zero-point","runs":[{"text":"Asymmetric, and the zero-point"}]},{"t":"p","runs":[{"text":"If the values are not centred, an activation after a ReLU is never negative, half the integer range is wasted. The asymmetric form adds an offset:"}]},{"t":"code","lang":"","label":"Text","text":"q = round(w / scale) + zero_point"},{"t":"p","runs":[{"text":"so that the minimum maps to 0 and the maximum to 255 in uint8. The zero-point is chosen so that a real zero maps exactly onto an integer, because padding and ReLU produce exact zeros in quantity and rounding them to something else adds a bias everywhere."}]},{"t":"h2","id":"four-bits","runs":[{"text":"Four bits"}]},{"t":"p","runs":[{"text":"Int4 has sixteen levels. With a per-tensor scale that is hopeless; with group-wise scales it works for weights, and int4 weights with fp16 activations is the standard recipe for running a large model on a small device. The arithmetic of why: weights are static, so they can be quantised carefully, once, with all the tricks above; activations change with every input and must be quantised on the fly, which is why they are usually left at higher precision."}]},{"t":"p","runs":[{"text":"The error introduced is measurable and it is not zero. A model at int4 typically loses a few tenths of a point of perplexity relative to fp16; at int3 it loses several points and at int2 it stops working. The loss is larger for smaller models, because they have fewer redundant weights to absorb the noise, and larger in the layers nearest the output. Anyone claiming int4 is free has not measured it on their own task; the honest statement is that it is cheap and the price is known."}]},{"t":"h2","id":"why-it-is-faster-and-when-it-is-not","runs":[{"text":"Why it is faster, and when it is not"}]},{"t":"p","runs":[{"text":"Module 7’s arithmetic-intensity lesson said single-token generation is bound by streaming the weights. Int4 weights are a quarter of the bytes, so generation is up to four times faster, before any change in the arithmetic. For large-batch throughput, which is compute-bound, the gain depends on whether the hardware has integer matrix units; if it must convert int4 back to fp16 before multiplying, there is no speed-up at all, only memory saved."}]},{"t":"h2","id":"doing-it-yourself","runs":[{"text":"Doing it yourself"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\nw = np.random.randn(4096).astype(np.float32) * 0.01\nw[17] = 5.0                                   # one outlier\nscale = np.abs(w).max() / 127\nq = np.round(w / scale).astype(np.int8)\nprint((q == 0).mean())                        # fraction of weights that became zero: nearly all\nrow_scale = np.abs(w[:2048]).max() / 127      # a group without the outlier\nq2 = np.round(w[:2048] / row_scale)\nprint((q2 == 0).mean())                       # a few per cent"},{"t":"p","runs":[{"text":"Run it, then delete the outlier line and run it again. The difference between the two fractions is the whole reason quantisation papers exist."}]}],"check":{"question":[{"text":"A tensor of weights mostly between −0.02 and 0.02 also contains one weight of 5.0. It is quantised to int8 with a single scale for the whole tensor. What happens to the typical weight?"}],"options":[{"text":[{"text":"It is stored with about 2 per cent error, as the arithmetic for a 0.0034 weight showed."}],"correct":false,"tempting":[{"text":"That figure assumed the scale was set by 0.02; with the outlier the scale is 0.039, two hundred times coarser."}]},{"text":[{"text":"It rounds to zero: the step is now 0.039 and the weight is under half of that."}],"correct":true,"tempting":null},{"text":[{"text":"The outlier is clipped to 127 × scale and the rest are unaffected."}],"correct":false,"tempting":[{"text":"Clipping happens only if the scale is chosen below the maximum; with the scale set from the maximum, the outlier is exact and everything else pays."}]},{"text":[{"text":"The int8 range overflows and the values wrap to negative numbers."}],"correct":false,"tempting":[{"text":"Rounding w / scale never exceeds 127 when the scale is max|w| / 127; the damage is loss of the small values, not overflow of the large one."}]}],"order":[2,0,3,1],"why":null},"prev":{"slug":"integers-overflow-and-the-size-of-things","title":"Integers, overflow, and the token id that would not fit","minutes":8},"next":{"slug":"condition-numbers","title":"The condition number, and how many digits a problem throws away","minutes":9}}}