Lecture 05-2 · Quantization II
المحاضرة 05-2 · التكميم (Quantization II)
Integer-only matrix multiplication
ضرب المصفوفات بالأعداد الصحيحة فقط
P1 MUSTSubstitute r = S(q − Z) for W, X and Y in Y = WX, solve for the output integer qY, and every operation turns into an integer multiply, an integer add, a fixed-point multiply or a bit shift. No floating-point unit is needed at run time.
عوّض r = S(q − Z) لكل من W وX وY في Y = WX، وحُلّ لإيجاد العدد الصحيح الناتج qY، فتتحول كل عملية إلى ضرب صحيح أو جمع صحيح أو ضرب fixed-point أو إزاحة بتات. لا حاجة لوحدة floating-point وقت التشغيل.
The derivation (slide 11)
الاشتقاق (الشريحة 11)
Each of the three tensors has its own scale and zero point. Write the real product in terms of integers:
لكل من الـ tensors الثلاثة scale وzero point خاص به. اكتب الضرب الحقيقي بدلالة الأعداد الصحيحة:
SY ( qY − ZY ) = SW ( qW − ZW ) · SX ( qX − ZX )
Divide both sides by SY and move ZY across:
اقسم الطرفين على SY وانقل ZY إلى الطرف الآخر:
qY = SW SXSY ( qW − ZW ) ( qX − ZX ) + ZY
Expand the bracket into four terms:
افتح القوس إلى أربعة حدود:
qY = SW SXSY ( qWqX − ZWqX − ZXqW + ZWZX ) + ZY
In a real matrix product each term is summed over the inner dimension K, so qWqX means Σk qW[i,k] qX[k,j], ZWqX means ZW Σk qX[k,j], ZXqW means ZX Σk qW[i,k], and ZWZX is added K times (K ZWZX). The slide writes them compactly.
في ضرب المصفوفات الفعلي يُجمع كل حد على البعد الداخلي K: الحد qWqX يعني Σk qW[i,k] qX[k,j]، وZWqX يعني ZW Σk qX[k,j]، وZXqW يعني ZX Σk qW[i,k]، وZWZX يُضاف K مرة. الشريحة تكتبها بشكل مختصر.
What each box costs (slides 12 to 14)
كلفة كل صندوق (الشرائح من 12 إلى 14)
| Pieceالجزء | Depends on input?يعتمد على المدخل؟ | How it runsكيف يُنفَّذ |
|---|---|---|
| qWqX | yesنعم | N-bit integer multiplies, 32-bit integer accumulateضرب صحيح بعرض N-bit، وتجميع صحيح بعرض 32-bit |
| − ZWqX | yes (qX)نعم (qX) | run time, 32-bit integer subtract; vanishes if ZW = 0وقت التشغيل، طرح صحيح بعرض 32-bit؛ ويختفي إذا كان ZW = 0 |
| − ZXqW + ZWZX | noلا | precomputed once (weights are static, ZX fixed after calibration)يُحسب مسبقاً مرة واحدة (الأوزان ثابتة، وZX ثابت بعد المعايرة) |
| SWSX / SY | noلا | rescale to N-bit integer: fixed-point multiply by M0 plus a right shift by nإعادة التحجيم إلى عدد صحيح بعرض N-bit: ضرب fixed-point في M0 مع إزاحة إلى اليمين بمقدار n |
| + ZY | noلا | N-bit integer additionجمع صحيح بعرض N-bit |
الجدول يلخّص كل جزء: qWqX ضرب صحيح بعرض N-bit مع تجميع 32-bit؛ وZWqX يعتمد على المدخل فيُحسب وقت التشغيل، ويختفي إذا كان ZW = 0؛ و−ZXqW + ZWZX لا يعتمد على المدخل فيُحسب مسبقاً مرة واحدة؛ ومعامل إعادة القياس يُنفَّذ كضرب fixed-point وإزاحة؛ وZY جمع صحيح بعرض N-bit.
Why can −ZXqW + ZWZX be precomputed? It contains only weight codes and zero points. The weights do not change after training, and ZX is fixed once the activation range is calibrated, so this is a constant per output, folded in like a bias. The term ZWqX contains qX, which changes with every input, so it must run each time.
لماذا يمكن حساب −ZXqW + ZWZX مسبقاً؟ لأنه يحتوي فقط على رموز الأوزان والـ zero points. الأوزان لا تتغير بعد التدريب، وZX ثابت بعد معايرة مدى الـ activations، فهذا ثابت لكل مخرج يُدمج مثل الـ bias. أما ZWqX فيحتوي على qX الذي يتغير مع كل مدخل، فيجب حسابه في كل مرة.
Why a 32-bit accumulator? Two 8-bit integers multiply to a 16-bit product, and summing K such products grows the result by about log2K more bits. A 16-bit register would overflow after a handful of terms; 32 bits leaves room for thousands.
لماذا مُراكِم 32-bit؟ ضرب عددين 8-bit يعطي ناتجاً 16-bit، وجمع K نواتج يزيد العرض بحوالي log2K بت. سجل 16-bit سيفيض بعد عدد قليل من الحدود؛ و32 bit تترك مجالاً لآلاف الحدود.
Turning the float rescale into integers (slide 13)
تحويل إعادة التحجيم العشرية إلى أعداد صحيحة (الشريحة 13)
Empirically, M = SWSX/SY always lies in (0, 1). Any such number can be written as
تجريبياً، M = SWSX/SY يقع دائماً في (0, 1). وأي عدد كهذا يمكن كتابته بالشكل
SW SXSY = 2−n M0, M0 ∈ [0.5, 1)
M0 is stored as a fixed-point integer (for example M0 × 231 in an int32), so multiplying by it is an integer multiply. The factor 2−n is a right shift by n bits. To find n, keep doubling M until it lands in [0.5, 1); the number of doublings is n.
يُخزَّن M0 كعدد صحيح fixed-point (مثلاً M0 × 231 في int32)، فالضرب فيه ضرب صحيح. والمعامل 2−n إزاحة لليمين بـ n بت. لإيجاد n، ضاعف M حتى يقع في [0.5, 1)؛ وعدد مرات المضاعفة هو n.
M = 0.3 0.3 x 2 = 0.6 in [0.5, 1) -> n = 1 0.3 = 2^-1 x 0.6 M = 0.0075 0.0075 x 2^7 = 0.0075 x 128 = 0.96 in [0.5, 1) -> n = 7 (x 2^6 = 0.48 is still below 0.5) 0.0075 = 2^-7 x 0.96 M = 0.3125 0.3125 x 2 = 0.625 -> n = 1, M_0 = 0.625
التفكيك: 0.3 = 2−1 × 0.6، و0.0075 = 2−7 × 0.96 (لأن 0.0075 × 128 = 0.96، و× 64 يعطي 0.48 وهو أقل من 0.5)، و0.3125 = 2−1 × 0.625.
A length-2 dot product (K = 2). Weights qW = [3, −2] with ZW = 1; inputs qX = [6, 1] with ZX = 2. Scales SW = 0.02, SX = 0.05, SY = 0.0032, and ZY = −10.
ضرب نقطي بطول 2 (K = 2). الأوزان qW = [3, −2] مع ZW = 1؛ المدخلات qX = [6, 1] مع ZX = 2. المقاييس SW = 0.02، SX = 0.05، SY = 0.0032، وZY = −10.
Direct: Σ (q_W - Z_W)(q_X - Z_X) = (3-1)(6-2) + (-2-1)(1-2)
= 2x4 + (-3)(-1) = 8 + 3 = 11
Four terms
Σ q_W q_X = 3x6 + (-2)x1 = 18 - 2 = 16 runtime
Z_W Σ q_X = 1 x (6 + 1) = 7 runtime
Z_X Σ q_W = 2 x (3 - 2) = 2 precomputed
K Z_W Z_X = 2 x 1 x 2 = 4 precomputed
16 - 7 - 2 + 4 = 11 (matches)
Rescale
M = S_W S_X / S_Y = 0.02 x 0.05 / 0.0032 = 0.001 / 0.0032 = 0.3125
= 2^-1 x 0.625
11 x 0.625 = 6.875, then >> 1 -> 3.4375 -> round 3
Output
q_Y = 3 + Z_Y = 3 + (-10) = -7
Check in reals
true Y = S_W S_X x 11 = 0.001 x 11 = 0.011
decoded = S_Y (q_Y - Z_Y) = 0.0032 x 3 = 0.0096 (rounding error 0.0014)
The precomputed part (−2 + 4 = +2) is a single constant stored with the weights. Only ΣqWqX and ZWΣqX touch the input.
الجزء المحسوب مسبقاً (−2 + 4 = +2) ثابت واحد يُخزَّن مع الأوزان. فقط ΣqWqX وZWΣqX يتعاملان مع المدخل.
Why ZW = 0 (slide 14)
لماذا ZW = 0 (الشريحة 14)
Slide 14 shows a trained weight histogram: a narrow bell centred at 0, roughly symmetric (about −0.1 to 0.1). With a range symmetric around 0, the zero point naturally comes out as ZW = 0. Then both −ZWqX (the only run-time correction term) and ZWZX vanish:
تعرض الشريحة 14 مدرّج أوزان مدرَّبة: جرس ضيّق متمركز عند 0 ومتماثل تقريباً (من حوالي −0.1 إلى 0.1). مع مدى متماثل حول الصفر، يخرج الـ zero point طبيعياً ZW = 0. عندها يختفي −ZWqX (حد التصحيح الوحيد وقت التشغيل) وZWZX:
qY = SW SXSY ( qWqX − ZXqW ) + ZY
That saves a run-time sum over the input for every output. This is the symmetric weight quantization of the next page.
هذا يوفّر مجموعاً على المدخل وقت التشغيل لكل مخرج. وهذا هو الـ symmetric quantization للأوزان في الصفحة التالية.
ZWqX is not precomputable. It has ZW (a constant) in front, but qX is the live input. Only terms with no qX in them, −ZXqW and ZWZX, are precomputed. Slide 12 shades exactly those two.
ZWqX لا يُحسب مسبقاً. صحيح أن أمامه ZW وهو ثابت، لكن qX هو المدخل الحي. فقط الحدود الخالية من qX، أي −ZXqW وZWZX، تُحسب مسبقاً. والشريحة 12 تظلّل هذين الحدين بالضبط.
Multiplies are N-bit, the accumulator is 32-bit. Do not say "8-bit addition" for the bracket; the sum of many 16-bit products needs 32 bits. Only the final + ZY is an N-bit add, after rescaling.
الضرب بعرض N-bit، والمُراكِم 32-bit. لا تقل "جمع 8-bit" لما داخل القوس؛ مجموع كثير من نواتج 16-bit يحتاج 32 bit. فقط + ZY الأخير جمع بعرض N-bit، بعد إعادة القياس.
M0 is in [0.5, 1), not [0, 1). Writing M0 = 0.3 with n = 0 is wrong: 0.3 must be written 2−1 × 0.6. Keeping M0 at least 0.5 preserves the maximum precision in the fixed-point multiplier.
M0 في [0.5, 1)، وليس [0, 1). وضع M0 = 0.3 مع n = 0 خطأ: يجب كتابة 0.3 كـ 2−1 × 0.6. إبقاء M0 لا يقل عن 0.5 يحفظ أعلى دقة في المضاعِف fixed-point.
"If it has qX, it runs every time." Of the four bracket terms, the two with qX run live; the two without are a bias you store. And ZW = 0 kills one live term for free.
"إذا فيه qX فهو يُحسب كل مرة." من الحدود الأربعة داخل القوس، الحدّان اللذان فيهما qX يُحسبان وقت التشغيل؛ والآخران bias تخزّنه. وZW = 0 يحذف حداً حياً مجاناً.
Check yourself
اختبر نفسك
In qY = (SWSX/SY)(qWqX − ZWqX − ZXqW + ZWZX) + ZY, which term cannot be precomputed even though it contains a zero point?
في المعادلة أعلاه، أي حد لا يمكن حسابه مسبقاً رغم أنه يحتوي على zero point؟
B. ZWqX depends on the input qX, which changes every inference. ZXqW and ZWZX use only static weights and fixed zero points, so slide 12 marks them "Precompute". ZY is a constant add.
B. الحد ZWqX يعتمد على المدخل qX الذي يتغير مع كل استدلال. أما ZXqW وZWZX فيستخدمان الأوزان الثابتة والـ zero points الثابتة فقط، لذلك تسمّيهما الشريحة 12 "Precompute". وZY جمع ثابت.
The rescale factor is M = 0.0075. Written as 2−nM0 with M0 ∈ [0.5, 1), what are n and M0?
معامل إعادة القياس M = 0.0075. عند كتابته بالشكل 2−nM0 مع M0 ∈ [0.5, 1)، ما قيمتا n وM0؟
C. 0.0075 × 27 = 0.0075 × 128 = 0.96, which is in [0.5, 1). A gives 0.48 < 0.5. B gives 1.92 ≥ 1. D is off by powers of ten (0.75 × 2−2 = 0.1875).
C. 0.0075 × 27 = 0.0075 × 128 = 0.96، وهو داخل [0.5, 1). الخيار A يعطي 0.48 < 0.5. والخيار B يعطي 1.92 ≥ 1. والخيار D خاطئ بقوى العشرة (0.75 × 2−2 = 0.1875).
Why does slide 12 label the bracket "32-bit Integer Addition/Subtraction" when weights and activations are only 8-bit?
لماذا تسمّي الشريحة 12 ما داخل القوس "32-bit Integer Addition/Subtraction" رغم أن الأوزان والـ activations بعرض 8-bit فقط؟
A. Products grow to 16 bits and the K-term sum adds about log2K more bits, so the accumulator is 32-bit. The scales are handled separately by M0 and a shift. qY is rescaled back to N bits.
A. تكبر النواتج إلى 16 بت، ومجموع K حداً يضيف نحو log2K بت أخرى، لذلك يكون المُراكِم 32-bit. والـ scales تُعالج بشكل منفصل عبر M0 وإزاحة. وqY يُعاد تحجيمه إلى N بت.
SW = 0.02, SX = 0.05, SY = 0.0032. What is the rescale factor and its 2−nM0 form?
SW = 0.02، SX = 0.05، SY = 0.0032. ما معامل إعادة القياس وما شكله 2−nM0؟
D. M = (0.02 × 0.05) / 0.0032 = 0.001 / 0.0032 = 0.3125; doubling once gives 0.625 ∈ [0.5, 1), so n = 1. A inverts the fraction. B forgets to divide by SY (and 1.024 is outside the range). C leaves M0 below 0.5.
D. M = (0.02 × 0.05) / 0.0032 = 0.001 / 0.0032 = 0.3125؛ ومضاعفته مرة واحدة تعطي 0.625 ∈ [0.5, 1)، إذن n = 1. الخيار A يقلب الكسر. والخيار B ينسى القسمة على SY (و1.024 خارج المدى أيضاً). والخيار C يترك M0 أقل من 0.5.
Slide 14 asks "ZW = 0?". What is the main benefit of a zero weight zero point?
الشريحة 14 تسأل "ZW = 0?". ما الفائدة الأساسية من جعل الـ zero point للأوزان صفراً؟
B. The weight histogram is a bell centred at 0, so a symmetric range gives ZW = 0 at little cost, and the input-dependent ZWqX term (plus ZWZX) disappears. ZXqW still exists and is still precomputed; the scales are unchanged.
B. مدرّج الأوزان جرس متمركز عند 0، فالمدى المتماثل يعطي ZW = 0 بكلفة قليلة، ويختفي الحد المعتمد على المدخل ZWqX (ومعه ZWZX). أما ZXqW فما زال موجوداً ويُحسب مسبقاً، والـ scales لا تتغير.