NN Quantization
1. Why fewer bits?
Pruning made the network smaller by deleting weights. Quantization keeps every weight but stores each one in fewer bits. The two stack: a pruned network can also be quantized.
Why it pays off:
Memory. A weight in 32-bit float takes 4 bytes. The same weight in 8-bit integer takes 1 byte: the model is 4× smaller and moves 4× less data from memory, which in Why Is My Neural Network Slow? we saw is where most of the time and energy go.
Energy. An 8-bit integer multiply costs roughly 20× less energy than a 32-bit float multiply. Adds are cheaper still.
Speed. Integer units are smaller and faster than float units, so a chip fits more of them.
The price is precision: fewer bits means fewer distinct values a weight can take. The whole art is losing bits without losing accuracy.
![[visual: one weight as a 32-bit bar shrinking to 16, 8, 4 bits, with bytes and energy under each]](https://static.wixstatic.com/media/8f17d0_b3b614984de5452699cc1f26c8f8c817~mv2.png/v1/fill/w_980,h_344,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/8f17d0_b3b614984de5452699cc1f26c8f8c817~mv2.png)
التشذيب (pruning) صغّر الشبكة بحذف أوزان، أما التكميم (Quantization) فيبقي كل وزن لكن يخزنه بعدد بتات أقل (مثلا استبدال Float - 4 bytes ب integer -1 byte ) . أقل بتات = ذاكرة أصغر ونقل بيانات أقل وطاقة أقل بكثير. الثمن: دقة أقل في تمثيل كل رقم، والفن كله في خسارة البتات دون خسارة دقة النموذج. |
2. How a computer stores a number
To understand what "fewer bits" costs, we need to know what the bits do. Three ways to write a number in binary:
Integer. Every bit is a power of two; the first bit is the sign. 8 bits cover −128 to 127, whole numbers only.
00110001 = 32 + 16 + 1 = 49
Fixed point. The same integer with an imaginary point at a fixed place, here after 4 bits. Some bits now mean halves, quarters, eighths. Cheap, but the point never moves, so very big and very small numbers cannot share a format.
0011.0001 = 2 + 1 + 1/16 = 3.0625 (the same 49, divided by 16)
Floating point. The bits are split into two jobs: an exponent that says where the point sits (how big the number is) and a fraction that holds the digits. The point floats, so one format can hold 0.0001 and 10000.
Take 48 = 1.5 × 2⁵: the fraction is 1.5, the exponent is 5. Keep the fraction and change the exponent to 1 and you get 1.5 × 2¹ = 3; to −1 and you get 1.5 × 2⁻¹ = 0.75. Same digits, different size. In 8 bits (1 sign, 4 exponent, 3 fraction) it looks like this:
sign exponent fraction
0 1100 100 = 1.5 × 2⁵ = 48
0 1000 100 = 1.5 × 2¹ = 3
0 0110 100 = 1.5 × 2⁻¹ = 0.75Remind you : In an integer, the bits count 4, 2, 1 from the left, so 100 = 4. In the fraction field, the bits count ½, ¼, ⅛ from the left, as if there were a point before them: .100 = ½ and 1.5= 1.100 (1 + ½).
Every float starts with “1.”, so that leading 1 is never stored. 1.5 in binary is 1.100; drop the 1 and the fraction field holds 100, which therefore means 1.5.
The exponent is stored with a fixed offset of 7, so 1100 = 12 − 7 = 5, 1000 = 8 − 7 = 1, and 0110 = 6 − 7 = −1. The offset is what lets the exponent be negative for small numbers.
The split is the whole story of the float formats you see in model cards:
Format | Exponent bits | Fraction bits | Total | Meaning |
FP32 | 8 | 23 | 32 | the default; wide range, fine precision |
FP16 | 5 | 10 | 16 | half the size, but a narrow range: numbers above 65 504 overflow |
BF16 | 8 | 7 | 16 | same range as FP32, coarser digits; what training usually uses |
FP8 | 4 or 5 | 3 or 2 | 8 | the two 8-bit variants trade range for digits |
INT8 | – | – | 8 | whole numbers −128 to 127 |
INT4 | – | – | 4 | sixteen values, −8 to 7 |
The one rule to remember:
more exponent bits = wider range; more fraction bits = finer precision.
With a fixed budget of bits you choose one or the other. Integers have no exponent at all, so they are the cheapest to compute with and the first choice for quantization.
.
العدد الصحيح في الحاسوب كل بتاته قوى العدد 2 ولا يحمل كسرًا. أما العدد العائم فيكتب العدد على صورة أرقام × قوة للعدد 2، ويقسم بتاته بين كسر يحمل الأرقام وأُس يحدد موضع الفاصلة:
الأرقام (1.5) ثابتة في الكسر، والأس وحده يحرّك الفاصلة. في 8 بتات (1 إشارة، 4 أس، 3 كسر): كسر أس إشارة
0 1100 100 = 1.5 × 2⁵ = 48
0 1000 100 = 1.5 × 2¹ = 3
0 0110 100 = 1.5 × 2⁻¹ = 0.75كل عدد عائم float يبدأ بـ «.1»، فلا تُخزن هذه الواحد: 1.5 بالثنائي هي 1.100، نحذف الواحد فيبقى في خانة الكسر 100، وهو يعني إذن 1.5. والأس يُخزن مع إزاحة ثابتة قدرها 7: 1100 = 12 − 7 = 5، و1000 = 8 − 7 = 1، و0110 = 6 − 7 = −1؛ الإزاحة هي ما يسمح للأس أن يكون سالبًا للأعداد الصغيرة. لذلك بتات أكثر للأس = مدى أوسع (أعداد أكبر وأصغر)، وبتات أكثر للكسر = دقة أعلى (أرقام أكثر)، وبميزانية بتات ثابتة تختار أحدهما. الأعداد الصحيحة بلا أس أصلًا، لذلك هي الأرخص حسابيًا والخيار الأول للتكميم. |
3. What is quantization?
Quantization means taking values from a large set and forcing them onto a small set of allowed values.
The everyday example is a picture. A photo may use millions of colours; save it with a 16-colour palette and every pixel snaps to the nearest of the 16. The picture is much smaller and still recognisable, but every pixel moved a little. That small move is the quantization error.

In a neural network the "pixels" are the weights. A trained weight might be 2.0913…; after quantization it becomes the nearest allowed value, say 2.0. Millions of weights each move a little, and the question is whether the network still gives the same answers. Usually it does, because the weights were never that exact to begin with.
The two methods below differ in how the allowed values are chosen:
K-means: let the data choose. Pick a few values that sit where the weights actually cluster, and keep a small table of them.
Linear: use evenly spaced values, like marks on a ruler, so that integer arithmetic works directly.
التكميم هو إجبار قيم كثيرة على مجموعة صغيرة من القيم المسموحة، كحفظ صورة بـ 6 ألوان فقط: كل بكسل ينتقل إلى أقرب لون، والفرق الصغير هو خطأ التكميم. في الشبكة الشيء نفسه مع الأوزان: عندي آلاف القيم المختلفة، وأريد حصرها في عدد قليل من القيم المسموحة، مثلًا 4 قيم إن كان لديّ بتّان فقط لكل وزن. السؤال الوحيد هو: ما هذه القيم الأربع؟ هنا تختلف الآليتان:
|
4. Method 1: K-means, a codebook of shared values
The idea. Look at all the weights in a layer and group them into a few clusters. Replace every weight with the centre of its cluster. Store the centres once in a small table (the codebook) and, for each weight, store only which centre it uses (its index).
A tiny example. A layer has 16 weights such as 2.09, −0.98, 1.48, 0.09, … They fall into four groups, with centres 2.0, 1.5, 0.0 and −1.0. Each weight now needs only 2 bits to say "I am centre number 3".
Before | After | |
Per weight | 32 bits | 2 bits (an index) |
Shared | – | 4 centres × 32 bits |
16 weights | 64 bytes | 4 + 16 = 20 bytes |
For a real layer with millions of weights the codebook is negligible, so the saving is simply 32 / bits-per-index: 16× at 2 bits, 8× at 4 bits.
Getting accuracy back. The centres can be fine-tuned: during training, the gradients of all weights in a cluster are added up and nudge that cluster's centre. The indexes stay fixed; only the 4 (or 16, or 256) shared values move.
How many bits? Convolution layers survive down to about 4 bits per weight; fully connected layers down to about 2. Beyond that accuracy drops.
The catch. This saves storage only. At run time each index is looked up in the codebook and turned back into a 32-bit float, and all the arithmetic is still float. The model is smaller on disk and in memory, not faster to compute.

Who decides what? You pick the bit width, and that fixes the number of centres: 2 bits give 4, 4 bits give 16. K-means then finds the centres itself by looking at where the weights cluster; nobody sets a minimum or maximum. The centres stay real values (floats) in the codebook; only the indexes are small integers, which is why this saves storage but not arithmetic.
نجمع أوزان الطبقة في بضع مجموعات، ونستبدل كل وزن بمركز مجموعته. نخزن المراكز مرة واحدة في جدول صغير (codebook)، ولكل وزن نخزن رقم مركزه فقط ببتات قليلة. المراكز نفسها يمكن ضبطها بالتدريب لاستعادة الدقة. أنت تختار عدد البتات فقط، وهذا يحدد عدد المراكز (بتّان = 4 مراكز). K-means يجد المراكز بنفسه من تجمعات الأوزان، بلا حد أدنى أو أقصى محدد العيب: يوفر التخزين فقط؛ عند التشغيل تُسترجع الأوزان من الجدول ويبقى الحساب كله بالأعداد العائمة. |
5. Method 2: linear quantization, integers all the way
The idea. Lay a ruler over the weights. The ruler has evenly spaced marks, one per integer the bits can hold (for 8 bits, −128 to 127). Every weight snaps to the nearest mark and is stored as that integer. Two numbers describe the ruler, and they are all you need to convert back:
the scale: how far apart the marks are, in real units;
the zero point: which integer mark sits on the real value 0.

r is the real weight, q the stored integer, S the scale, Z the zero point.
A tiny example, 2 bits. With 2 bits the integers are −2, −1, 0, 1. The weights run from −1.08 to 2.12, a span of 3.2, over 3 steps between the marks, so the scale is about 1.07. The zero point works out to −1: the integer −1 stands for the real 0. So the weight 2.09 becomes integer 1 (real 2.14), −0.98 becomes −2 (real −1.07), 0.09 becomes −1 (real 0). The errors are bigger than with K-means, because the marks are evenly spaced instead of placed where the weights cluster.
تخيّل مسطرة علاماتها متساوية المسافات، علامة لكل عدد صحيح تتسع له البتات (في 8 بتات: من −128 إلى 127). نضع المسطرة فوق الأوزان، فينتقل كل وزن إلى أقرب علامة ويُخزن كعدد صحيح. رقمان فقط يصفان المسطرة ويكفيان للرجوع إلى القيمة الحقيقية: المقياس (S) وهو المسافة بين العلامتين بالوحدات الحقيقية، ونقطة الصفر (Z) وهي العلامة الصحيحة التي تقابل الصفر الحقيقي. القيمة الحقيقية = المقياس × (العدد المخزن − نقطة الصفر). مثال ببتّين: الأعداد الصحيحة المتاحة −2، −1، 0، 1. الأوزان تمتد من −1.08 إلى 2.12، أي مدى 3.2 على ثلاث خطوات، فالمقياس ≈ 1.07، ونقطة الصفر = −1 (العدد −1 يمثل الصفر الحقيقي). فالوزن 2.09 يصبح 1 (يُقرأ 2.14)، و−0.98 يصبح −2 (يُقرأ −1.07)، و0.09 يصبح −1 (يُقرأ 0). الخطأ هنا أكبر من K-means لأن العلامات متساوية المسافات بدل أن تقع حيث تتجمع الأوزان |
Why bother, then? Because the ruler lets the whole layer run on integers. Weight × input becomes integer × integer; the sums pile up in a 32-bit integer; and only at the very end is the result scaled back to 8 bits by a fixed multiplier. No float arithmetic anywhere in the layer, which is exactly what cheap chips are good at. This is why INT8 is the format phones and edge devices run on.
الميزة أن الطبقة كلها تُحسب بالأعداد الصحيحة: ( Weight × input) يصبح (integer × integer)، ونتائج الجمع تتراكم في 32 بت، وفي النهاية فقط تُعاد النتيجة إلى 8 بتات بمضاعِف ثابت. لا حساب عائم في أي مكان، وهذا ما تجيده الشرائح الرخيصة، ولهذا INT8 هو ما تعمل به الجوالات والأجهزة الصغيرة. |
Symmetric vs asymmetric. If we force the zero point to 0, the ruler is centred on zero and a few terms in the arithmetic vanish, which makes the layer simpler and faster. The price is wasted marks when the weights are lopsided (say all positive). Weights are usually symmetric around 0, so for weights we take Z = 0; for activations after ReLU, which are all positive, the zero point earns its keep.
إن ثبّتنا نقطة الصفر عند 0 صارت المسطرة متمركزة حول الصفر وسقطت بعض حدود الحساب، فتصبح الطبقة أبسط وأسرع. الثمن علامات مهدورة إن كانت الأوزان مائلة إلى جهة واحدة. الأوزان عادة متماثلة حول الصفر فنأخذ لها Z = 0، أما المخرجات بعد ReLU فكلها موجبة، وهنا تستحق نقطة الصفر مكانها. |
The catch. The errors are a little larger than K-means for the same bits, and choosing the range (the ends of the ruler) matters: one giant weight stretches the ruler and makes all the other marks coarse. How to choose the range, and how to quantize activations, is the next post.
الخطأ أكبر قليلًا من K-means عند عدد البتات نفسه، واختيار المدى (طرفي المسطرة) مهم: وزن واحد ضخم يمدّ المسطرة فتصبح كل العلامات الأخرى خشنة. كيف نختار المدى وكيف نكمّم المخرجات، هذا موضوع المقال القادم. |
.
6. The two side by side
K-means | Linear | |
Allowed values | wherever the weights cluster | evenly spaced marks |
What is stored | small integer index + a codebook | small integer + scale and zero point |
Error for the same bits | smaller | a little larger |
Arithmetic at run time | float (after a lookup) | integer, end to end |
What it saves | storage | storage, compute and energy |
Typical use | compressing a model on disk | running a model on a phone or edge chip |
Pruning asked which weights can go; quantization asks how many bits each weight deserves. K-means squeezes the file; linear quantization makes the maths cheap. The two can be combined with pruning: deleting weights, then shrinking the ones left, is how a model dozens of times smaller keeps the same accuracy.
Next in the series: how to choose the range for linear quantization, how to quantize activations, and the two ways of training with quantization in mind.
الـK-means يختار القيم حيث تتجمع الأوزان فيقل الخطأ لكنه يوفر التخزين فقط؛ الخطي يستخدم علامات متساوية فيزيد الخطأ قليلًا لكنه يجعل الحساب كله بالأعداد الصحيحة، فيوفر التخزين والحساب والطاقة معًا. التشذيب سأل أي الأوزان تُحذف، والتكميم يسأل كم بتًا يستحق كل وزن، والجمع بينهما يعطي نموذجًا أصغر بعشرات المرات بالدقة نفسها.
Sources
MIT 6.5940 Lecture 5: Quantization, Part I
Han et al., Deep Compression, ICLR 2016 (K-means quantization)
Jacob et al., Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference, CVPR 2018 (linear quantization)
Horowitz, Computing's Energy Problem, ISSCC 2014 (energy per operation)




Comments