Lecture 05-2 · Quantization II
المحاضرة 05-2 · التكميم (Quantization II)
Symmetric quantization and ZW = 0
الـ symmetric quantization وZW = 0
P1 MUSTPin the zero point at 0, make the floating-point range symmetric around 0, and the scale becomes one division. For weights this kills two of the four terms in the integer matrix multiply.
ثبّت الـ zero point عند 0، واجعل المدى العشري متماثلاً حول الصفر، فيصبح حساب الـ scale قسمة واحدة. وبالنسبة للأوزان، يحذف هذا حدّين من الحدود الأربعة في ضرب المصفوفات الصحيح.
Asymmetric vs symmetric
غير المتماثل (asymmetric) مقابل المتماثل (symmetric)
The general linear map is r = S(q − Z). Asymmetric quantization fits [rmin, rmax] exactly and needs a zero point Z (usually not 0). Symmetric quantization forces Z = 0 and uses the symmetric floating range [−|r|max, |r|max], where |r|max = max |r| over the tensor. Real 0 then maps to integer 0, and r = S·q.
الدالة الخطية العامة هي r = S(q − Z). الـ asymmetric يطابق المدى [rmin, rmax] بالضبط ويحتاج zero point Z (عادةً ليس صفراً). أما الـ symmetric فيفرض Z = 0 ويستخدم المدى المتماثل [−|r|max, |r|max]، حيث |r|max هو أكبر قيمة مطلقة في الـ tensor. عندها يُقابَل الصفر الحقيقي بالعدد الصحيح 0، ويصبح r = S·q.
| Bit width Nعرض البتات N | qmin | qmax |
|---|---|---|
| 2 | −2 | 1 |
| 3 | −4 | 3 |
| 4 | −8 | 7 |
| 8 | −128 | 127 |
| N | −2N−1 | 2N−1 − 1 |
جدول الشريحة 15: لعدد صحيح بإشارة من N بت، qmin = −2N−1 وqmax = 2N−1 − 1. لاحظ عدم التماثل: يوجد رمز سالب واحد زائد عن الموجب.
Two ways to compute S
طريقتان لحساب S
Full-range mode (slide 16). Start from the asymmetric endpoint equation rmin = S(qmin − Z), put Z = 0 and rmin = −|r|max:
وضع المدى الكامل (full range mode)، الشريحة 16. نبدأ من معادلة الطرف rmin = S(qmin − Z)، ونضع Z = 0 وrmin = −|r|max:
S = rminqmin − Z = −|r|maxqmin = |r|max2N−1
Restricted-range mode. Map +|r|max to qmax instead and use only the codes −qmax … qmax (−127 … 127 for INT8; the code −128 is never produced). This is the form 05-3 uses in its per-channel example on slides 10–13:
وضع المدى المقيّد (restricted range). نقابل +|r|max بـ qmax بدلاً من ذلك ونستخدم الرموز −qmax … qmax فقط (−127 … 127 في INT8، والرمز −128 لا يُستعمل). هذا هو الشكل المستخدم في مثال per-channel في محاضرة 05-3 (الشرائح 10–13):
S = |r|maxqmax = |r|max2N−1 − 1
Either way the quantize step is q = clamp(round(r / S), qmin, qmax) and dequantize is r ≈ S·q. No Z appears anywhere.
في الحالتين، خطوة التكميم هي q = clamp(round(r / S), qmin, qmax) والاسترجاع r ≈ S·q. لا يظهر Z إطلاقاً.
The 4×4 running weight matrix from the lecture has |r|max = 2.12. Compute S both ways at 2 bits and at 8 bits, then see what happens to the largest weight +2.12.
مصفوفة الأوزان 4×4 المتكررة في المحاضرة لها |r|max = 2.12. احسب S بالطريقتين عند 2 بت و8 بت، ثم انظر ماذا يحدث لأكبر وزن +2.12.
N = 2 (q_min = -2, q_max = 1)
full range: S = 2.12 / 2^1 = 2.12 / 2 = 1.06
+2.12 / 1.06 = 2 -> clamp to q_max = 1 -> 1.06 (error 1.06)
-2.12 / 1.06 = -2 -> q_min, exact
restricted: S = 2.12 / (2^1 - 1) = 2.12 / 1 = 2.12
+2.12 / 2.12 = 1 -> 2.12 exact; codes used: -1, 0, 1
N = 8 (q_min = -128, q_max = 127)
full range: S = 2.12 / 128 = 0.016563
+2.12 / S = 128 -> clamp to 127 -> 2.1034 (error 0.0166)
restricted: S = 2.12 / 127 = 0.016693
+2.12 / S = 127 -> 2.12 exact
The two conventions give different S. At 8 bits the difference is under 1%, so it barely matters. At 2 bits full range halves S (finer steps) but clips the positive extreme; restricted range keeps both extremes but leaves only three usable levels.
الاصطلاحان يعطيان S مختلفاً. عند 8 بت الفرق أقل من 1% فلا يكاد يهم. أما عند 2 بت فالمدى الكامل يقسم S على اثنين (خطوات أدق) لكنه يقص الطرف الموجب، بينما المدى المقيّد يحافظ على الطرفين لكنه لا يترك إلا ثلاثة مستويات مستخدمة.
Why ZW = 0 simplifies the matmul (slide 17)
لماذا يبسّط ZW = 0 ضرب المصفوفات (الشريحة 17)
From the previous section, the integer-only form of Y = WX is
من القسم السابق، الصيغة الصحيحة بالكامل لـ Y = WX هي
qY = SWSXSY ( qWqX − ZWqX − ZXqW + ZWZX ) + ZY
Weights are roughly symmetric around zero, so quantize them symmetrically: ZW = 0. The terms −ZWqX and +ZWZX vanish:
الأوزان متماثلة تقريباً حول الصفر، لذا نكمّمها تكميماً متماثلاً: ZW = 0. فيختفي الحدّان −ZWqX و+ZWZX:
qY = SWSXSY ( qWqX − ZXqW ) + ZY
Weights: symmetric is natural
الأوزان: المتماثل هو الخيار الطبيعي
Trained weight distributions are centred near 0, so forcing ZW = 0 costs almost nothing in range and buys a simpler kernel.
توزيعات الأوزان المدرّبة متمركزة قرب الصفر، لذا فرض ZW = 0 لا يكلّف شيئاً تقريباً من المدى ويعطي نواة حسابية أبسط.
Activations: often keep ZX
الـ Activations: غالباً نُبقي ZX
After ReLU every activation is ≥ 0. A symmetric range [−|r|max, |r|max] then spends the whole negative half of the codes on values that never occur: one bit of resolution wasted. That is why the slide keeps ZX and only zeroes ZW.
بعد ReLU تكون كل الـ activations أكبر من أو تساوي الصفر. المدى المتماثل عندها يهدر النصف السالب كاملاً من الرموز على قيم لا تظهر أبداً: ضياع بت كامل من الدقة. لهذا تُبقي الشريحة ZX وتصفّر ZW فقط.
Two symmetric scale formulas. |r|max/2N−1 (full range, slide 16) and |r|max/(2N−1 − 1) (restricted, the 05-3 example) are both "symmetric". Read which denominator the question uses. For the 05-3 matrix at 2 bits the answer is 2.12, not 1.06.
صيغتان للـ scale المتماثل. |r|max/2N−1 (المدى الكامل، الشريحة 16) و|r|max/(2N−1 − 1) (المقيّد، مثال 05-3) كلاهما "متماثل". انتبه لأي مقام يستخدمه السؤال. لمصفوفة 05-3 عند 2 بت الجواب 2.12 وليس 1.06.
Z = 0 is the integer zero point, not "rmin = 0". Symmetric means real 0 maps to integer 0; the real range still includes negative numbers.
Z = 0 هو الـ zero point الصحيح، وليس "rmin = 0". المتماثل يعني أن الصفر الحقيقي يُقابَل بالعدد الصحيح صفر؛ والمدى الحقيقي ما زال يحتوي قيماً سالبة.
Which terms vanish. With ZW = 0 you lose −ZWqX and +ZWZX. The term −ZXqW stays (it needs ZX = 0 to vanish).
أي الحدود تختفي. مع ZW = 0 يختفي −ZWqX و+ZWZX. أما −ZXqW فيبقى (يحتاج ZX = 0 ليختفي).
"Full range divides by the negative end, restricted by the positive end." |qmin| = 2N−1, qmax = 2N−1 − 1.
"المدى الكامل يقسم على الطرف السالب، والمقيّد على الطرف الموجب." |qmin| = 2N−1، وqmax = 2N−1 − 1.
Check yourself
اختبر نفسك
A weight tensor has |r|max = 2.12. Using slide 16's full-range symmetric formula with N = 2, what is S?
لدى tensor أوزان |r|max = 2.12. باستخدام صيغة المدى الكامل المتماثل في الشريحة 16 مع N = 2، ما قيمة S؟
B. S = |r|max/2N−1 = 2.12/2 = 1.06. A is the restricted-range answer 2.12/(2 − 1). C divides by 3 (qmax − qmin, the asymmetric denominator, with a symmetric numerator). D multiplies instead of dividing.
B. S = |r|max/2N−1 = 2.12/2 = 1.06. الخيار A هو إجابة المدى المقيّد 2.12/(2 − 1). والخيار C يقسم على 3 (qmax − qmin، وهو مقام الحالة غير المتماثلة، مع بسط متماثل). والخيار D يضرب بدلاً من أن يقسم.
After setting ZW = 0 in qY = (SWSX/SY)(qWqX − ZWqX − ZXqW + ZWZX) + ZY, what remains inside the parentheses?
بعد وضع ZW = 0 في المعادلة، ماذا يبقى داخل القوسين؟
C. Every term carrying ZW drops; −ZXqW has no ZW, so it stays and is precomputed. A would also need ZX = 0. B and D keep terms that contain the zeroed ZW.
C. كل حد يحمل ZW يسقط؛ و−ZXqW لا يحتوي على ZW، فيبقى ويُحسب مسبقاً. الخيار A يحتاج أيضاً ZX = 0. والخياران B وD يُبقيان حدوداً تحتوي على ZW الذي صار صفراً.
Why is the dropped term ZWqX the most valuable one to eliminate?
لماذا يُعدّ الحد المحذوف ZWqX أكثر حدّ يستحق الإزالة؟
A. qX changes with every input, so ZWqX would be recomputed at runtime. ZXqW and ZWZX use only offline-known quantities. All terms are integers (C false) and none changes the rescale factor (D false).
A. qX يتغير مع كل مدخل، لذلك سيُعاد حساب ZWqX وقت التشغيل. أما ZXqW وZWZX فيستخدمان كميات معروفة مسبقاً فقط. وكل الحدود أعداد صحيحة (C خطأ)، ولا يغيّر أي منها معامل إعادة التحجيم (D خطأ).
A post-ReLU activation tensor (all values ≥ 0) is quantized to INT8 with a symmetric range. What is the main cost?
يُكمَّم tensor من الـ activations بعد ReLU (كل القيم ≥ 0) إلى INT8 بمدى متماثل. ما التكلفة الرئيسية؟
D. The symmetric range covers [−|r|max, |r|max] but no value is negative, so codes −128 … −1 are dead. A is wrong: symmetric maps 0 to 0 exactly. B and C do not follow from the choice of range.
D. المدى المتماثل يغطي [−|r|max, |r|max] لكن لا توجد قيمة سالبة، فتكون الرموز من −128 إلى −1 ميتة. الخيار A خطأ: المتماثل يقابل 0 بـ 0 بالضبط. والخياران B وC لا ينتجان عن اختيار المدى.