Lecture 05-2 · Quantization II
المحاضرة 05-2 · التكميم (Quantization II)
Linear quantization: the affine map r = S(q - Z)
التكميم الخطي (linear quantization): التحويل r = S(q - Z)
P1 MUSTLinear quantization stores every weight as a small integer q and recovers the real value with one multiply and one subtract: r = S(q - Z). Two numbers per tensor, a float scale S and an integer zero point Z, define the whole map.
الـ linear quantization يخزّن كل وزن كعدد صحيح صغير q ويستعيد القيمة الحقيقية بعملية ضرب وطرح واحدة: r = S(q - Z). رقمان فقط لكل tensor، وهما scale S من نوع float وzero point Z من نوع integer، يحددان التحويل بالكامل.
Where it sits (slide 2)
أين يقع بين الطرق (الشريحة 2)
Slide 2 compares the three families. K-means quantization stores integer indices plus a floating-point codebook, but still computes in floating point. Linear quantization stores integer weights and computes with integer arithmetic. That second property is the whole point: the multiply-accumulate hardware becomes cheap integer units.
تقارن الشريحة 2 بين العائلات الثلاث. الـ K-means quantization يخزّن فهارس صحيحة مع codebook من نوع floating-point، لكنه يبقى يحسب بـ floating-point. أما الـ linear quantization فيخزّن أوزاناً صحيحة ويحسب بـ integer arithmetic. هذه الخاصية الثانية هي جوهر الفكرة: وحدات multiply-accumulate تصبح وحدات صحيحة رخيصة.
| No quantizationبلا تكميم | K-means | Linearالخطي (Linear) | |
|---|---|---|---|
| Storageالتخزين | FP weightsأوزان FP | INT indices + FP codebookفهارس INT مع codebook من نوع FP | INT weightsأوزان INT |
| Computationالحساب | FP arithmeticحساب FP | FP arithmeticحساب FP | INT arithmeticحساب INT |
يلخّص الجدول المقارنة: بدون quantization التخزين والحساب كلاهما FP؛ في K-means التخزين صحيح مع codebook عشري والحساب FP؛ في linear التخزين والحساب كلاهما INT.
The four symbols
الرموز الأربعة
r = S ( q − Z )
r · real value · floating-point
r · القيمة الحقيقية · عشري (floating-point)
The original 32-bit float weight (or activation) you want to approximate.
الوزن الأصلي (أو activation) بصيغة 32-bit float الذي نريد تقريبه.
q · quantized value · integer
q · القيمة المكمّمة · عدد صحيح (integer)
The stored N-bit signed integer. This is what lives in memory.
العدد الصحيح المخزَّن بإشارة وبعرض N-bit. هذا ما يوجد فعلياً في الذاكرة.
Z · zero point · integer
Z · نقطة الصفر (zero point) · عدد صحيح (integer)
A quantization parameter. It is the integer that represents real 0, so r = 0 is exactly representable: put q = Z and r = S(Z − Z) = 0 with no error.
معامل quantization. هو العدد الصحيح الذي يمثّل الصفر الحقيقي، لذلك يمكن تمثيل r = 0 بدقة تامة: ضع q = Z فيكون r = S(Z − Z) = 0 بلا أي خطأ.
S · scale · floating-point
S · المقياس (scale) · عشري (floating-point)
A quantization parameter. It is the real-valued step between two neighbouring integer codes.
معامل quantization. هو المسافة الحقيقية بين رمزين صحيحين متجاورين.
Exact zero matters because zeros are everywhere in a network: zero padding in convolutions, ReLU outputs, pruned weights. If 0 could only be approximated, every padded pixel would inject a small bias.
دقة تمثيل الصفر مهمة لأن الأصفار موجودة في كل مكان في الشبكة: الـ zero padding في الـ convolution، ومخرجات ReLU، والأوزان المحذوفة بالـ pruning. لو كان الصفر مقرَّباً فقط، لأضاف كل بكسل padding انحيازاً صغيراً.
The slide 4 to 5 example
مثال الشريحتين 4 و5
A 4 by 4 float weight matrix is quantized to 2-bit signed integers with Z = −1 and S = 1.07. The 2-bit codes use two's complement:
مصفوفة أوزان float بحجم 4 × 4 تُكمَّم إلى أعداد صحيحة بإشارة بعرض 2-bit مع Z = −1 وS = 1.07. الرموز تستخدم two's complement:
| Binaryالثنائي (Binary) | Decimalالعشري (Decimal) | Reconstructed r = 1.07(q + 1)القيمة المستعادة r = 1.07(q + 1) |
|---|---|---|
| 01 | 1 | 2.14 |
| 00 | 0 | 1.07 |
| 11 | −1 | 0 |
| 10 | −2 | −1.07 |
في two's complement البت الأعلى سالب الوزن: 10 = −2 و11 = −1. والرمز 11 هو Z، لذلك يعطي الصفر الحقيقي.
weights r (fp32) quantized q (2-bit) 2.09 -0.98 1.48 0.09 1 -2 0 -1 0.05 -0.14 -1.08 2.12 -1 -1 -2 1 -0.91 1.92 0 -1.03 -2 1 -1 -2 1.87 0 1.53 1.49 1 -1 0 0 reconstructed (q - (-1)) x 1.07 error r - reconstructed 2.14 -1.07 1.07 0 -0.05 0.09 0.41 0.09 0 0 -1.07 2.14 0.05 -0.14 -0.01 -0.02 -1.07 2.14 0 -1.07 0.16 -0.22 0 0.04 2.14 0 1.07 1.07 -0.27 0 0.46 0.42
المصفوفة الأولى هي الأوزان الأصلية، والثانية رموزها الصحيحة، والثالثة القيم المستعادة (q + 1) × 1.07، والرابعة الخطأ r − r̂. لاحظ أن الصفران الأصليان يُستعادان بدقة (خطأ 0)، وأن أكبر خطأ 0.46 أقل من نصف الخطوة S/2 = 0.535.
Two things to see in the error matrix: the two original zeros come back as exactly 0, and no error exceeds S/2 = 0.535 (the largest is 0.46), because every in-range value is rounded to its nearest grid point.
أمران يجب ملاحظتهما في مصفوفة الخطأ: الصفران الأصليان يعودان صفراً تماماً، ولا يتجاوز أي خطأ S/2 = 0.535 (الأكبر 0.46)، لأن كل قيمة داخل المدى تُقرَّب إلى أقرب نقطة في الشبكة.
Reconstruct two entries by hand from slide 5: the top-left (q = 1) and the third entry of row 2 (q = −2).
استعد عنصرين يدوياً من الشريحة 5: العنصر أعلى اليسار (q = 1) والعنصر الثالث في الصف الثاني (q = −2).
r = S (q - Z), S = 1.07, Z = -1 Top-left, q = 1 (binary 01) q - Z = 1 - (-1) = 2 r = 1.07 x 2 = 2.14 error = 2.09 - 2.14 = -0.05 Row 2, col 3, q = -2 (binary 10) q - Z = -2 - (-1) = -1 r = 1.07 x (-1) = -1.07 error = -1.08 - (-1.07) = -0.01
Always subtract Z first (inside the bracket), then multiply by S.
اطرح Z أولاً دائماً (داخل القوس)، ثم اضرب في S.
Integer range per bit width (slide 6)
مدى الأعداد الصحيحة لكل عرض بتات (الشريحة 6)
An N-bit signed (two's complement) integer covers 2N codes:
العدد الصحيح بإشارة (two's complement) بعرض N-bit يغطّي 2N رمزاً:
qmin = −2N−1 qmax = 2N−1 − 1
| Bit width Nعرض البتات N | qmin | qmax | Codesعدد الرموز |
|---|---|---|---|
| 2 | −2 | 1 | 4 |
| 3 | −4 | 3 | 8 |
| 4 | −8 | 7 | 16 |
| 8 | −128 | 127 | 256 |
الجدول يعطي المدى لكل عرض: 2-bit من −2 إلى 1، و3-bit من −4 إلى 3، و4-bit من −8 إلى 7، و8-bit من −128 إلى 127. المدى غير متماثل: الجانب السالب أطول برمز واحد.
The range is lopsided: one more negative code than positive. The real interval [rmin, rmax] is stretched over [qmin, qmax] by the factor S, and Z is the integer that lands on real 0. How to pick S and Z from data is the next page.
المدى غير متوازن: رمز سالب إضافي مقارنة بالموجب. المجال الحقيقي [rmin, rmax] يُمدّ فوق [qmin, qmax] بالمعامل S، وZ هو العدد الصحيح الذي يقع على الصفر الحقيقي. أما كيف نختار S وZ من البيانات فهو موضوع الصفحة التالية.
A 3-bit tensor uses S = 0.5 and Z = 2. What real values do qmin, Z and qmax represent?
tensor بعرض 3-bit يستخدم S = 0.5 وZ = 2. ما القيم الحقيقية التي يمثلها qmin وZ وqmax؟
3-bit: q_min = -4, q_max = 3 q = -4 : r = 0.5 x (-4 - 2) = 0.5 x (-6) = -3.0 q = 2 : r = 0.5 x ( 2 - 2) = 0 q = 3 : r = 0.5 x ( 3 - 2) = 0.5 representable range [-3.0, 0.5], 8 points, step 0.5
A positive Z pushes the grid toward negative reals; a negative Z (as in slide 5) pushes it toward positive reals.
Z الموجب يدفع الشبكة نحو القيم الحقيقية السالبة؛ وZ السالب (كما في الشريحة 5) يدفعها نحو القيم الموجبة.
Z is an integer, S is a float. Z has the same type and bit width as q (slide 5 labels it "2-bit signed int"). If you write Z = −0.99 you have skipped the rounding step and lost exact zero.
Z عدد صحيح، وS عدد عشري. لـ Z النوع والعرض نفسه مثل q (الشريحة 5 تسميه "2-bit signed int"). إذا كتبت Z = −0.99 فقد تخطّيت خطوة التقريب وفقدت تمثيل الصفر بدقة.
S(q − Z), not Sq − Z. For q = 1: the correct value is 1.07 × (1 + 1) = 2.14. The wrong form gives 1.07 × 1 + 1 = 2.07. The zero point lives in integer units and must be subtracted before scaling.
S(q − Z) وليس Sq − Z. لـ q = 1: القيمة الصحيحة 1.07 × (1 + 1) = 2.14. الصيغة الخاطئة تعطي 1.07 × 1 + 1 = 2.07. الـ zero point بوحدات صحيحة ويجب طرحه قبل الضرب في S.
Read 2-bit codes as two's complement. Binary 10 is −2, not 2; binary 11 is −1, not 3.
اقرأ رموز 2-bit بنظام two's complement. الثنائي 10 يساوي −2 وليس 2؛ والثنائي 11 يساوي −1 وليس 3.
"Shift, then stretch." Subtract Z to shift the integer so that zero lines up, then multiply by S to stretch integer steps into real units.
"أزِح ثم مُدّ." اطرح Z لإزاحة العدد الصحيح حتى يتطابق الصفر، ثم اضرب في S لتمديد الخطوات الصحيحة إلى وحدات حقيقية.
Check yourself
اختبر نفسك
In r = S(q − Z), which quantities are integers?
في r = S(q − Z)، أي الكميات أعداد صحيحة؟
C. Slide 5 labels q and Z as integers and r and S as floating-point. Z must be an integer so that q = Z decodes to exactly r = 0.
C. الشريحة 5 تصف q وZ بأنهما عددان صحيحان، وr وS بأنهما عشريان. ويجب أن يكون Z عدداً صحيحاً حتى يُفك q = Z إلى r = 0 بالضبط.
With S = 1.07 and Z = −1, the stored 2-bit code is binary 10. What real value is reconstructed?
مع S = 1.07 وZ = −1، الرمز المخزَّن بعرض 2-bit هو الثنائي 10. ما القيمة الحقيقية المستعادة؟
B. Binary 10 in two's complement is −2. r = 1.07 × (−2 − (−1)) = 1.07 × (−1) = −1.07. A reads 10 as unsigned 2 and uses q − Z = 3. C forgets Z (1.07 × −2). D uses the wrong form Sq − Z = −2.14 + 1.
B. الثنائي 10 في two's complement يساوي −2. r = 1.07 × (−2 − (−1)) = 1.07 × (−1) = −1.07. الخيار A يقرأ 10 كعدد بلا إشارة 2 ويستخدم q − Z = 3. والخيار C ينسى Z (1.07 × −2). والخيار D يستخدم الصيغة الخاطئة Sq − Z = −2.14 + 1.
What is the integer range of a 4-bit signed quantized value?
ما مدى القيمة الصحيحة المكمّمة بإشارة بعرض 4-bit؟
D. qmin = −23 = −8 and qmax = 23 − 1 = 7, 16 codes in total. A is unsigned. B wastes a code (sign-magnitude). C needs 17 codes.
D. qmin = −23 = −8 وqmax = 23 − 1 = 7، أي 16 رمزاً إجمالاً. الخيار A بلا إشارة. والخيار B يهدر رمزاً (sign-magnitude). والخيار C يحتاج 17 رمزاً.
Why does linear quantization use a zero point at all?
لماذا يستخدم الـ linear quantization الـ zero point أصلاً؟
A. Slide 5: Z allows real r = 0 to be exactly representable by a quantized integer Z. Zero padding, ReLU zeros and pruned weights then cost no error. S stays a float, and other weights still have rounding error up to S/2.
A. الشريحة 5: الـ Z يسمح بتمثيل r = 0 الحقيقي بالضبط بالعدد الصحيح المكمَّم Z. وبذلك لا تكلّف أصفار الـ padding وأصفار الـ ReLU والأوزان المقلَّمة أي خطأ. ويبقى S عشرياً، وتبقى للأوزان الأخرى أخطاء تقريب تصل إلى S/2.