Majid Al-RaimiSymmetric Quantization and Bias Handlingالتكميم المتماثل ومعالجة الانحياز (Bias)

COE 592Lecture 5.2المحاضرة 5.2Part 04الجزء 04

Symmetric Quantization and Bias Handlingالتكميم المتماثل ومعالجة الانحياز (Bias)

Symmetric quantization fixes Z = 0 and uses the range [-|r|max, |r|max], which makes the S formula simpler and removes the runtime Z_W q_X term. Bias then enters the fully-connected layer and is quantized with Z_b = 0 and S_b = S_W S_X, so that it shares the accumulator's scale.التكميم المتماثل يثبّت Z = 0 ويستخدم المدى [-|r|max, |r|max]، مما يبسّط صيغة S ويزيل الحد Z_W q_X وقت التشغيل. ثم يدخل الانحياز (bias) في الطبقة المتصلة كليًا ويُكمَّم بـZ_b = 0 وS_b = S_W S_X، ليشارك المُجمِّع (accumulator) مقياسه نفسه.

Conceptsالمفاهيم
4
Slidesالشرائح
15-19
Readingمدة القراءة
24 min24 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

The previous part ended with a question: the matrix multiply has a term, Z_W q_X, that depends on the input and so must be computed at every inference. Can we make it vanish? This part answers yes, by choosing a weight zero point of exactly 0. That one choice is called symmetric quantization, and it changes the scale formula, the integer pipeline, and how the bias gets folded in.

These slides are dense with exam material. Expect to compute a symmetric scale for a given bit width, to defend why weights get Z_W = 0 while activations do not, and to explain why the bias is stored as a 32-bit integer with scale S_W S_X. The running 4x4 weight matrix from slide 8, with values from -1.08 to 2.12, carries every worked number.

انتهى الجزء السابق بسؤال: في ضرب المصفوفات حدّ هو Z_W q_X يعتمد على المُدخل، ولذلك يجب حسابه في كل عملية استدلال. هل يمكن أن نجعله يختفي؟ يجيب هذا الجزء بنعم، وذلك باختيار نقطة صفر للأوزان تساوي 0 تمامًا. هذا الاختيار الواحد يسمى التكميم المتماثل (symmetric quantization)، وهو يغيّر صيغة المقياس، ومسار الحساب بالأعداد الصحيحة، وطريقة دمج الانحياز (bias).

هذه الشرائح مليئة بمادة الامتحان. توقّع أن يُطلب منك حساب مقياس متماثل لعرض بتات معيّن، وأن تدافع عن سبب حصول الأوزان على Z_W = 0 بينما لا تحصل عليه مخرجات التنشيط، وأن تشرح لماذا يُخزَّن الانحياز كعدد صحيح بـ32 بت بمقياس S_W S_X. مصفوفة الأوزان المرافقة 4x4 من الشريحة 8، بقيمها من -1.08 إلى 2.12، تحمل كل الأرقام المحلولة.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. State what symmetric quantization fixes (Z = 0, range [-|r|max, |r|max]) and when it fits the data.أن تذكر ما الذي يثبّته التكميم المتماثل (Z = 0، والمدى [-|r|max, |r|max]) ومتى يناسب البيانات.
  2. Compute the full range symmetric scale S = |r|max / 2^(N-1) for any bit width, and explain why +|r|max gets clipped.أن تحسب مقياس المدى الكامل المتماثل S = |r|max / 2^(N-1) لأي عرض بتات، وأن تشرح لماذا تُقصّ القيمة +|r|max.
  3. Contrast full range mode with restricted range mode and with asymmetric quantization on the same tensor.أن تقارن نمط المدى الكامل بنمط المدى المقيّد وبالتكميم غير المتماثل على نفس الموتر.
  4. Explain why weights use Z_W = 0 while ReLU activations keep Z_X different from 0.أن تشرح لماذا تستخدم الأوزان Z_W = 0 بينما تحتفظ مخرجات ReLU بقيمة Z_X لا تساوي 0.
  5. Derive the fully-connected layer with bias and justify Z_b = 0, S_b = S_W S_X and a 32-bit bias.أن تشتق الطبقة كاملة الاتصال مع الانحياز (bias) وتبرر اختيار Z_b = 0 وS_b = S_W S_X وتخزين الانحياز بـ32 بت.

Take two tensors. One is a layer of trained weights, with values scattered from about -2 to about +2 and most of them near 0. The other is the output of a ReLU, every value between 0 and 6. Both need to become 8-bit codes. Should both get the same kind of integer grid?

خذ موترين. الأول طبقة من الأوزان المدرّبة، قيمها منتشرة من حوالي -2 إلى حوالي +2 ومعظمها قريب من 0. والثاني مخرجات ReLU، كل قيمها بين 0 و6. كلاهما يجب أن يتحول إلى رموز بـ8 بت. فهل يجب أن يحصل الاثنان على نفس النوع من شبكة الأعداد الصحيحة؟

So far we have used asymmetric quantization: the float range [r_min, r_max] is stretched onto the code range [q_min, q_max], and the zero point Z lands wherever real 0 happens to fall. In the left half of slide 15 that is somewhere in the middle of the dots, and for the running matrix at 2 bits it was Z = -1. Symmetric quantization removes that freedom. It declares Z = 0 up front and, to make that consistent, quantizes a range centred on zero: [-|r|max, |r|max], where |r|max is the largest absolute value in the tensor.

حتى الآن استخدمنا asymmetric quantization (التكميم غير المتماثل): يُمدّ مدى القيم العشرية [r_min, r_max] على مدى الرموز [q_min, q_max]، وتقع zero point (نقطة الصفر) Z حيثما صادف أن وقع الصفر الحقيقي 0. في النصف الأيسر من الشريحة 15 يقع ذلك في مكان ما وسط النقاط، وفي المصفوفة المرافقة عند 2 بت كانت Z = -1. Symmetric quantization (التكميم المتماثل) يلغي هذه الحرية. فهو يعلن Z = 0 مسبقًا، ولكي يكون ذلك متسقًا يكمّم مدى متمركزًا حول الصفر: [-|r|max, |r|max]، حيث |r|max هي أكبر قيمة مطلقة في الموتر.

r=S (q−Z)→  Z=0  r=S qr = S\,(q - Z) \quad\xrightarrow{\;Z = 0\;}\quad r = S\,q
Symmetric quantization is pure scalingالتكميم المتماثل تحجيم خالص

The integer side does not change. The codes are still signed N-bit integers with q_min and q_max taken from the table on the slide:

جانب الأعداد الصحيحة لا يتغير. الرموز ما زالت أعدادًا صحيحة بإشارة من N-bit، وقيمتا q_min and q_max مأخوذتان من الجدول الموجود في الشريحة:

Bit width Nعرض البتات Nq_min = -2^(N-1)q_min = -2^(N-1)q_max = 2^(N-1) - 1q_max = 2^(N-1) - 1Levelsعدد المستويات
22-2-21144
33-4-43388
44-8-8771616
88-128-128127127256256
Signed N-bit code ranges (slide 15)مدى الرموز ذات الإشارة بعرض N بت (الشريحة 15)

Notice the range is already lopsided: there is one more negative code than positive. Keep that in mind; it is the root of the clipping problem in the next concept.

لاحظ أن المدى غير متوازن أصلًا: فيه رمز سالب واحد زيادة على عدد الرموز الموجبة. تذكّر هذا، فهو أصل مشكلة القصّ (clipping) في المفهوم التالي.

Who should be symmetric?من الذي يجب أن يكون متماثلًا؟

The answer depends on the shape of the data. Trained weights have a bell-shaped distribution centred near 0, so a range centred on 0 fits them well and almost no codes go to waste. ReLU outputs are the opposite: every value is 0 or positive. A symmetric grid would reserve the entire negative half of its codes for values that never occur.

الإجابة تعتمد على شكل البيانات. الأوزان المدرّبة لها bell-shaped distribution (توزيع على شكل جرس) متمركز قرب 0، لذلك يناسبها مدى متمركز حول 0، ولا يكاد يضيع أي رمز. مخرجات ReLU عكس ذلك: كل قيمة إما 0 أو موجبة. الشبكة المتماثلة ستحجز النصف السالب كله من رموزها لقيم لا تظهر أبدًا.

Weights spread evenly around 0, so Z = 0 fits. ReLU activations sit on one side, so their zero point moves to q_min.الأوزان تنتشر بالتساوي حول 0، لذلك يناسبها Z = 0. مخرجات ReLU تقع في جهة واحدة، لذلك تنتقل نقطة الصفر الخاصة بها إلى q_min.

Worked exampleمثال محلول

ReLU6 activations in [0, 6] as INT8مخرجات ReLU6 في المدى [0, 6] بصيغة INT8

  1. Symmetric, full rangeمتماثل، بالمدى الكامل

    |r|max = 6, so S = 6 / 128 = 0.0469. Only codes 0 to 127 are ever used; the 128 negative codes are dead.
    |r|max = 6، إذن S = 6 / 128 = 0.0469. الرموز من 0 إلى 127 فقط هي التي تُستخدم فعليًا، أما الرموز السالبة وعددها 128 فميتة.
  2. Asymmetricغير متماثل

    S = (6 - 0) / (127 - (-128)) = 6 / 255 = 0.0235 and Z = round(q_min - r_min / S) = round(-128 - 0) = -128. All 256 codes cover real data.
    S = (6 - 0) / (127 - (-128)) = 6 / 255 = 0.0235 و Z = round(q_min - r_min / S) = round(-128 - 0) = -128. كل الرموز وعددها 256 تغطي بيانات حقيقية.
  3. Resultالنتيجة

    Asymmetric gives a step about half as large (0.0235 versus 0.0469), so the worst-case rounding error S / 2 is halved. That is why activations keep a zero point.
    غير المتماثل يعطي خطوة بحجم النصف تقريبًا (0.0235 مقابل 0.0469)، لذلك ينخفض أسوأ خطأ تقريب S / 2 إلى النصف. ولهذا تحتفظ مخرجات التنشيط بنقطة صفر.

Recall

Why is Z_W = 0 a good choice for weights but Z_X = 0 a poor choice for ReLU activations?لماذا يكون Z_W = 0 اختيارًا جيدًا للأوزان بينما يكون Z_X = 0 اختيارًا سيئًا لمخرجات ReLU؟

Weights are bell-shaped around 0, so a range symmetric about 0 wastes almost no codes. ReLU outputs are all non-negative, so a symmetric range would leave every negative code unused and double the step size. Asymmetric quantization puts Z_X at q_min and uses all codes on real data.
الأوزان على شكل جرس حول 0، لذلك المدى المتماثل حول 0 لا يكاد يضيّع أي رمز. مخرجات ReLU كلها غير سالبة، لذلك المدى المتماثل سيترك كل الرموز السالبة دون استخدام ويضاعف حجم الخطوة. التكميم غير المتماثل يضع Z_X عند q_min ويستخدم كل الرموز لبيانات حقيقية.

Run the running matrix through a symmetric quantizer. Its largest magnitude is 2.12, so the range is [-2.12, 2.12]. How big is one step?

مرّر المصفوفة المرافقة عبر مكمِّم متماثل. أكبر قيمة مطلقة فيها هي 2.12، لذلك المدى هو [-2.12, 2.12]. فكم حجم الخطوة الواحدة؟

Slide 16 puts the two scale formulas side by side. The asymmetric one is the familiar endpoint match from part two:

الشريحة 16 تضع صيغتي المقياس جنبًا إلى جنب. صيغة غير المتماثل هي مطابقة الطرفين المألوفة من الجزء الثاني:

S=rmax⁡−rmin⁡qmax⁡−qmin⁡S = \frac{r_{\max} - r_{\min}}{q_{\max} - q_{\min}}
Asymmetric scaleالمقياس غير المتماثل

The symmetric one comes from matching only the negative end. With Z = 0, the endpoint equation r_min = S(q_min - Z) becomes -|r|max = S q_min, and q_min = -2^(N-1):

أما صيغة المتماثل فتأتي من مطابقة الطرف السالب فقط. مع Z = 0 تصبح معادلة الطرف r_min = S(q_min - Z) هي -|r|max = S q_min، وq_min = -2^(N-1):

S=rmin⁡qmin⁡−Z=−∣r∣max⁡qmin⁡=∣r∣max⁡2N−1S = \frac{r_{\min}}{q_{\min} - Z} = \frac{-|r|_{\max}}{q_{\min}} = \frac{|r|_{\max}}{2^{N-1}}
Symmetric scale, full range mode (slide 16)المقياس المتماثل، نمط المدى الكامل (الشريحة 16)

This is full range mode: it uses every code from -2^(N-1) up to 2^(N-1) - 1. For the running matrix in INT8, S = 2.12 / 128 = 0.01656.

هذا هو full range mode (نمط المدى الكامل): يستخدم كل الرموز من -2^(N-1) حتى 2^(N-1) - 1. للمصفوفة المرافقة بصيغة INT8 يكون S = 2.12 / 128 = 0.01656.

The value at the top does not fitالقيمة في الأعلى لا تتسع

The scale was fixed by the negative end. Now quantize the positive end: q = round(2.12 / 0.01656) = 128. But q_max = 127. Code 128 does not exist, so the value is clamped to 127 and comes back as 127 x 0.01656 = 2.103. In general, +|r|max maps to 2^(N-1), one step past q_max, because the signed range has one more negative code than positive.

المقياس حُدّد بواسطة الطرف السالب. الآن كمّم الطرف الموجب: q = round(2.12 / 0.01656) = 128. لكن q_max = 127. الرمز 128 غير موجود، لذلك تُحصر القيمة (clamp) إلى 127 وتعود بقيمة 127 x 0.01656 = 2.103. وبشكل عام، تُحوَّل +|r|max إلى 2^(N-1)، أي خطوة واحدة بعد q_max، لأن المدى ذا الإشارة فيه رمز سالب واحد زيادة على الرموز الموجبة.

Full range mode at 3 bits: -|r|max lands on q_min = -4, but +|r|max lands on code 4, which does not exist, so it is clipped back to q_max = 3.نمط المدى الكامل عند 3 بت: تقع ‎-|r|max على q_min = -4، لكن ‎+|r|max تقع على الرمز 4 وهو غير موجود، لذلك تُقصّ وتعود إلى q_max = 3.

Restricted range modeنمط المدى المقيّد

Many deployed toolchains give up the lonely -128 code instead. In restricted range mode the codes run from -(2^(N-1) - 1) to 2^(N-1) - 1, which is [-127, 127] for INT8, and the scale divides by q_max instead:

كثير من سلاسل الأدوات المستخدمة فعليًا تتخلى بدلًا من ذلك عن الرمز الوحيد -128. في restricted range mode (نمط المدى المقيّد) تمتد الرموز من -(2^(N-1) - 1) إلى 2^(N-1) - 1، أي [-127, 127] في INT8، ويُقسم المقياس على q_max بدلًا من ذلك:

Srestricted=∣r∣max⁡2N−1−1S_{\text{restricted}} = \frac{|r|_{\max}}{2^{N-1} - 1}
Restricted range scale (not on the slides)مقياس المدى المقيد (ليس في الشرائح)

Now both +|r|max and -|r|max land exactly on a code, and nothing clips. The cost is one code out of 2^N. TensorFlow Lite stores int8 weights in [-127, 127] with zero point 0, and NVIDIA's int8 workflow (Wu et al.) makes the same choice, noting that losing one value of 256 is insignificant at 8 bits but must be reconsidered at lower bit widths.

الآن تقع كل من +|r|max و-|r|max على رمز بالضبط، ولا يُقصّ شيء. الثمن رمز واحد من أصل 2^N. يخزّن TensorFlow Lite أوزان int8 في [-127, 127] مع نقطة صفر 0، وسير عمل int8 من NVIDIA (Wu et al.) يتخذ الاختيار نفسه، مع ملاحظة أن خسارة قيمة واحدة من 256 لا تُذكر عند 8 بت، لكن يجب إعادة النظر فيها عند عروض بتات أقل.

Worked exampleمثال محلول

Three symmetric grids for the running matrix (|r|max = 2.12)ثلاث شبكات متماثلة للمصفوفة المرافقة (‎|r|max = 2.12)

  1. INT8, full rangeINT8، بالمدى الكامل

    S = 2.12 / 128 = 0.01656. 2.12 clips to 2.103.
    S = 2.12 / 128 = 0.01656. تُقصّ 2.12 إلى 2.103.
  2. INT8, restricted rangeINT8، بالمدى المقيّد

    S = 2.12 / 127 = 0.01669. Slightly coarser, but 2.12 is code 127 exactly.
    S = 2.12 / 127 = 0.01669. أخشن قليلًا، لكن 2.12 هي الرمز 127 بالضبط.
  3. 2-bit, full range2 بت، بالمدى الكامل

    S = 2.12 / 2^1 = 1.06. Codes -2, -1, 0, 1 give the levels {-2.12, -1.06, 0, 1.06}. Quantize the top-left weight 2.09: 2.09 / 1.06 = 1.97, which rounds to 2, clips to q_max = 1, and comes back as 1.06.
    S = 2.12 / 2^1 = 1.06. الرموز -2, -1, 0, 1 تعطي المستويات {-2.12, -1.06, 0, 1.06}. كمّم الوزن في أعلى اليسار 2.09: 2.09 / 1.06 = 1.97، وهي تُقرّب إلى 2، ثم تُقصّ إلى q_max = 1، وتعود بقيمة 1.06.
  4. Resultالنتيجة

    At 8 bits the two modes differ by less than one percent in S. At 2 bits the clipped weight is off by 2.09 - 1.06 = 1.03, about half its value. Compare the asymmetric 2-bit grid from part two (S = 1.07, Z = -1), which brings 2.09 back as 1.07 x (1 - (-1)) = 2.14, an error of only 0.05.
    عند 8 بت يختلف النمطان بأقل من واحد بالمئة في S. وعند 2 بت يبتعد الوزن المقصوص بمقدار 2.09 - 1.06 = 1.03، أي حوالي نصف قيمته. قارن ذلك بالشبكة غير المتماثلة بـ2 بت من الجزء الثاني (S = 1.07، Z = -1)، التي تعيد 2.09 بقيمة 1.07 x (1 - (-1)) = 2.14، بخطأ قدره 0.05 فقط.

The trade-off, on the same tensorالمقايضة على نفس الموتر

The running matrix is skewed: its values reach +2.12 but only -1.08. Asymmetric INT8 fits that exactly with S = 3.20 / 255 = 0.01255. Symmetric must cover [-2.12, 2.12] and uses S = 0.01656, about 32% coarser, and the codes below round(-1.08 / 0.01656) = -65 are never used: 63 of 256 codes, roughly a quarter, are wasted. Drag the slider below from a balanced range to a fully one-sided one and watch the symmetric grid stay frozen while the asymmetric one follows the data.

المصفوفة المرافقة منحرفة: قيمها تصل إلى +2.12 لكنها لا تنزل إلا إلى -1.08. التكميم غير المتماثل بـINT8 يطابق ذلك تمامًا بـS = 3.20 / 255 = 0.01255. أما المتماثل فيجب أن يغطي [-2.12, 2.12] ويستخدم S = 0.01656، أي أخشن بحوالي 32%، والرموز الأقل من round(-1.08 / 0.01656) = -65 لا تُستخدم أبدًا: 63 رمزًا من 256، أي ربع الرموز تقريبًا، تضيع. حرّك المنزلق أدناه من مدى متوازن إلى مدى في جهة واحدة بالكامل، وراقب كيف تبقى الشبكة المتماثلة ثابتة بينما تتبع الشبكة غير المتماثلة البيانات.

ExplorerمستكشفSkew the range, compare the gridsأمِل المدى، وقارن الشبكات
-1.08
asymmetric: S = (r_max - r_min) / (2^N - 1)symmetric full range: S = |r|max / 2^(N-1)-2-1012
Asym S1.067Z = -1
Sym S1.060Z = 0
Levels in data4 vs 3of 4
Error at 2.120.01 vs 1.06asym vs sym
Asymmetricغير متماثلSymmetric (full range)متماثل (بالمدى الكامل)
Zero pointنقطة الصفرZ = round(q_min - r_min / S)Z = round(q_min - r_min / S)Z = 0Z = 0
Range quantizedالمدى المكمَّم[r_min, r_max][r_min, r_max][-|r|max, |r|max][-|r|max, |r|max]
Scaleالمقياس(r_max - r_min) / (q_max - q_min)(r_max - r_min) / (q_max - q_min)|r|max / 2^(N-1)|r|max / 2^(N-1)
Skewed dataالبيانات المنحرفةUses every codeيستخدم كل الرموزWastes the empty sideيضيّع الجهة الفارغة
Runtime cost in matmulكلفة وقت التشغيل في ضرب المصفوفاتExtra input-dependent termحدّ إضافي يعتمد على المُدخلNoneلا شيء
Typical useالاستخدام المعتادReLU activationsمخرجات ReLUWeightsالأوزان
Asymmetric versus symmetric quantizationالتكميم غير المتماثل مقابل التكميم المتماثل

Quick checkاختبار سريع

With N = 4 bits and |r|max = 2.4, what is the full range symmetric scale S?إذا كان N = 4 بت و |r|max = 2.4، فما قيمة المقياس المتماثل بالمدى الكامل S؟

Recall

In full range mode, what code does +|r|max map to, and what happens to it?في نمط المدى الكامل، إلى أي رمز تتحول ‎+|r|max، وماذا يحدث لها؟

It maps to |r|max / S = 2^(N-1), which is one more than q_max = 2^(N-1) - 1. The clamp sends it to q_max, so the largest positive value is reconstructed one step S too small. Restricted range mode avoids this by dividing by 2^(N-1) - 1.
تتحول إلى |r|max / S = 2^(N-1)، وهو أكبر بواحد من q_max = 2^(N-1) - 1. الحصر (clamp) يرسلها إلى q_max، لذلك يُعاد بناء أكبر قيمة موجبة أصغر من حقيقتها بخطوة واحدة S. نمط المدى المقيّد يتجنب ذلك بالقسمة على 2^(N-1) - 1.

Here is the payoff. Recall the full expansion of a quantized matrix multiplication from part three, with its four terms inside the bracket:

هنا تأتي الفائدة. تذكّر المفكوك الكامل لـquantized matrix multiplication (ضرب المصفوفات المكمّم) من الجزء الثالث، بحدوده الأربعة داخل القوس:

qY=SWSXSY(qWqX−ZWqX−ZXqW+ZWZX)+ZYq_Y = \frac{S_W S_X}{S_Y}\big(q_W q_X - Z_W q_X - Z_X q_W + Z_W Z_X\big) + Z_Y
General quantized Y = WXالصيغة العامة المكمَّمة لـ Y = WX

Sort the terms by what they depend on. q_W q_X is the real work: integer multiply-accumulates. Z_X q_W and Z_W Z_X involve only weights and fixed parameters, so they are precomputed offline. That leaves Z_W q_X: it contains the input, so it has to be recomputed for every new input, as an extra sum over the activations. Set Z_W = 0 and both Z_W q_X and Z_W Z_X disappear:

رتّب الحدود بحسب ما تعتمد عليه. q_W q_X هو العمل الحقيقي: عمليات ضرب وتجميع بالأعداد الصحيحة. Z_X q_W وZ_W Z_X لا تتضمنان إلا الأوزان ومعاملات ثابتة، لذلك تُحسبان مسبقًا (precomputed) خارج وقت التشغيل. يبقى Z_W q_X: فهو يحتوي على المُدخل، لذلك يجب إعادة حسابه لكل مُدخل جديد، كمجموع إضافي على قيم التنشيط. اجعل Z_W = 0 فيختفي كل من Z_W q_X وZ_W Z_X:

qY=SWSXSY(qWqX−ZXqW)+ZYq_Y = \frac{S_W S_X}{S_Y}\big(q_W q_X - Z_X q_W\big) + Z_Y
With Z_W = 0 (slide 17)مع Z_W = 0 (الشريحة 17)

At inference time the only work inside the bracket is the N-bit integer product q_W q_X, summed in a 32-bit accumulator. The term Z_X q_W is a constant per output, added once. Outside the bracket, the rescale by S_W S_X / S_Y and the addition of Z_Y are unchanged from part three.

وقت الاستدلال، العمل الوحيد داخل القوس هو حاصل الضرب الصحيح بـN بت q_W q_X، الذي يُجمع في 32-bit accumulator (مجمّع بـ32 بت). الحد Z_X q_W ثابت لكل مخرج، ويُضاف مرة واحدة. وخارج القوس، تبقى rescale (إعادة القياس) بـS_W S_X / S_Y وإضافة Z_Y دون تغيير عن الجزء الثالث.

Worked exampleمثال محلول

One output of a two-term dot productمخرج واحد لضرب نقطي من حدّين

  1. Set upالإعداد

    Weights q_W = [3, -2] (symmetric, Z_W = 0), activations q_X = [10, 4] with Z_X = 2.
    الأوزان q_W = [3, -2] (متماثلة، Z_W = 0)، ومخرجات التنشيط q_X = [10, 4] مع Z_X = 2.
  2. Runtimeوقت التشغيل

    q_W q_X = 3 x 10 + (-2) x 4 = 22.
    q_W q_X = 3 x 10 + (-2) x 4 = 22.
  3. Offlineمسبقًا خارج وقت التشغيل

    Z_X q_W = 2 x (3 + (-2)) = 2, known before any input arrives.
    Z_X q_W = 2 x (3 + (-2)) = 2، وهو معروف قبل وصول أي مُدخل.
  4. Resultالنتيجة

    Accumulator = 22 - 2 = 20. Check directly: 3 x (10 - 2) + (-2) x (4 - 2) = 24 - 4 = 20. Had the weights used Z_W = 1, the term Z_W (10 + 4) = 14 would also be needed, and its 10 + 4 changes with every input.
    المجمّع = 22 - 2 = 20. تحقّق مباشرة: 3 x (10 - 2) + (-2) x (4 - 2) = 24 - 4 = 20. لو استخدمت الأوزان Z_W = 1 لاحتجنا أيضًا إلى الحد Z_W (10 + 4) = 14، والجزء 10 + 4 فيه يتغير مع كل مُدخل.

Recall

Why ask for Z_W = 0 but not Z_X = 0?لماذا نطلب Z_W = 0 ولا نطلب Z_X = 0؟

Z_W multiplies q_X, the input, so the term Z_W q_X must be computed at every inference. Z_X multiplies q_W, a constant, so Z_X q_W is precomputed for free. Weights are centred anyway, so Z_W = 0 costs nothing in accuracy, while one-sided activations need their zero point.
Z_W يُضرب في q_X، وهو المُدخل، لذلك يجب حساب الحد Z_W q_X في كل عملية استدلال. أما Z_X فيُضرب في q_W، وهو ثابت، لذلك يُحسب Z_X q_W مسبقًا مجانًا. والأوزان متمركزة أصلًا، لذلك Z_W = 0 لا يكلّف شيئًا في الدقة، بينما تحتاج مخرجات التنشيط ذات الجهة الواحدة إلى نقطة الصفر الخاصة بها.

A real fully-connected layer computes Y = WX + b. Say one output has accumulator scale S_W S_X = 0.0005 and bias b = 0.37. The accumulator holds an integer that means 0.0005 per unit. How do we add 0.37 to it without leaving integer arithmetic?

الطبقة كاملة الاتصال الحقيقية تحسب Y = WX + b. لنفترض أن لأحد المخرجات مقياس مجمّع S_W S_X = 0.0005 وانحيازًا b = 0.37. المجمّع يحمل عددًا صحيحًا كل وحدة فيه تعني 0.0005. فكيف نضيف 0.37 إليه دون أن نغادر الحساب بالأعداد الصحيحة؟

Quantize every quantity, bias included, with its own scale and zero point, then substitute r = S(q - Z) into each one:

كمّم كل كمية، بما فيها الانحياز، بـscale (مقياس) ونقطة صفر خاصين بها، ثم عوّض r = S(q - Z) في كل واحدة منها:

SY(qY−ZY)=SW(qW−ZW)⋅SX(qX−ZX)+Sb(qb−Zb)S_Y(q_Y - Z_Y) = S_W(q_W - Z_W)\cdot S_X(q_X - Z_X) + S_b(q_b - Z_b)
Y = WX + b in quantized form (slide 18)Y = WX + b بالصيغة المكمَّمة (الشريحة 18)

Apply Z_W = 0 from the previous concept:

طبّق Z_W = 0 من المفهوم السابق:

SY(qY−ZY)=SWSX(qWqX−ZXqW)+Sb(qb−Zb)S_Y(q_Y - Z_Y) = S_W S_X(q_W q_X - Z_X q_W) + S_b(q_b - Z_b)
After Z_W = 0بعد Z_W = 0

The two halves still speak different units. The first is in units of S_W S_X; the second is in units of S_b and carries its own offset Z_b. Slide 18 boxes exactly these two mismatches. Slide 19 removes both by choice:

النصفان ما زالا يتكلمان بوحدات مختلفة. الأول بوحدات S_W S_X، والثاني بوحدات S_b ويحمل إزاحته الخاصة Z_b. الشريحة 18 تضع إطارًا حول هذين الاختلافين بالضبط، والشريحة 19 تزيلهما كليهما بالاختيار:

Zb=0,Sb=SWSX  ⟹  SY(qY−ZY)=SWSX(qWqX−ZXqW+qb)Z_b = 0,\quad S_b = S_W S_X \;\Longrightarrow\; S_Y(q_Y - Z_Y) = S_W S_X\big(q_W q_X - Z_X q_W + q_b\big)
Bias quantization (slide 19)تكميم الانحياز (الشريحة 19)

This is bias quantization in one line. Because S_b equals the accumulator's scale, q_b is already in accumulator units and is simply added to the running sum. Because Z_b = 0, there is no extra offset to subtract. The result is still one rescale and one addition of Z_Y, exactly as before. Any other S_b would force a second rescale, (S_b / S_W S_X) q_b, a fixed-point multiply per bias, just to line the units up.

هذا هو bias quantization (تكميم الانحياز) في سطر واحد. لأن S_b يساوي مقياس المجمّع، فإن q_b يكون أصلًا بوحدات المجمّع ويُضاف ببساطة إلى المجموع الجاري. ولأن Z_b = 0، لا توجد إزاحة إضافية يجب طرحها. النتيجة ما زالت إعادة قياس واحدة وإضافة واحدة لـZ_Y، تمامًا كما كان من قبل. أي قيمة أخرى لـS_b كانت ستفرض إعادة قياس ثانية، (S_b / S_W S_X) q_b، أي ضربًا بالفاصلة الثابتة لكل انحياز، فقط لمواءمة الوحدات.

int8 products sum into an int32 accumulator at scale S_W S_X. The int32 bias, quantized at that same scale with Z_b = 0, joins the sum before one rescale to int8.نواتج ضرب int8 تُجمع في مجمّع int32 بمقياس S_W S_X. والانحياز int32، المكمَّم بنفس المقياس مع Z_b = 0، ينضم إلى المجموع قبل إعادة قياس واحدة إلى int8.

Why the bias needs 32 bitsلماذا يحتاج الانحياز إلى 32 بت

Worked exampleمثال محلول

Quantizing b = 0.37تكميم b = 0.37

  1. Scaleالمقياس

    S_W = 0.01, S_X = 0.05, so S_b = S_W S_X = 0.0005 and Z_b = 0.
    S_W = 0.01 وS_X = 0.05، إذن S_b = S_W S_X = 0.0005 وZ_b = 0.
  2. Codeالرمز

    q_b = round(0.37 / 0.0005) = 740.
    q_b = round(0.37 / 0.0005) = 740.
  3. Resultالنتيجة

    740 is far outside the int8 range [-128, 127]. At this scale an int8 bias could only reach 127 x 0.0005 = 0.0635. The bias is stored as int32, the same width as the accumulator it joins.
    740 بعيدة جدًا خارج مدى int8 [-128, 127]. بهذا المقياس لا يستطيع انحياز int8 أن يصل إلا إلى 127 x 0.0005 = 0.0635. لذلك يُخزَّن الانحياز بصيغة int32، بنفس عرض المجمّع الذي ينضم إليه.

The pattern is general. S_W S_X is the product of two small numbers, so it is very small, so even a modest bias becomes a large integer. Storing it as int32 costs almost nothing: there is one bias per output neuron or channel, a tiny fraction of the parameters. Jacob et al. add a second reason: each bias entry is added to many outputs, so any rounding error in it acts as a systematic offset, which hurts accuracy. A fine scale and a wide integer keep that error below S_b / 2 = 0.00025.

هذا النمط عام. S_W S_X هو حاصل ضرب عددين صغيرين، لذلك هو صغير جدًا، ولذلك حتى الانحياز المتواضع يصبح عددًا صحيحًا كبيرًا. تخزينه بصيغة int32 لا يكلّف تقريبًا شيئًا: هناك انحياز واحد لكل خلية عصبية مخرجة أو قناة، وهذا جزء ضئيل من المعاملات. ويضيف Jacob et al. سببًا ثانيًا: كل عنصر انحياز يُضاف إلى مخرجات كثيرة، لذلك أي خطأ تقريب فيه يعمل كإزاحة منهجية، وهذا يضرّ بالدقة. المقياس الدقيق والعدد الصحيح العريض يبقيان هذا الخطأ تحت S_b / 2 = 0.00025.

Quick checkاختبار سريع

Why is the bias quantized with S_b = S_W S_X and Z_b = 0?لماذا يُكمَّم الانحياز بـ S_b = S_W S_X و Z_b = 0؟

Quick checkاختبار سريع

Which tensor usually keeps a nonzero zero point in INT8 inference?أي موتر يحتفظ عادةً بنقطة صفر لا تساوي الصفر في الاستدلال بصيغة INT8؟

Recall

With S_W = 0.02 and S_X = 0.1, what is q_b for b = -0.5, and what type stores it?إذا كان S_W = 0.02 و S_X = 0.1، فما قيمة q_b عندما b = -0.5، وما النوع الذي يخزّنه؟

S_b = 0.02 x 0.1 = 0.002, Z_b = 0, so q_b = round(-0.5 / 0.002) = -250. That is outside int8 [-128, 127], and it is stored as int32 to match the accumulator.
S_b = 0.02 x 0.1 = 0.002 وZ_b = 0، إذن q_b = round(-0.5 / 0.002) = -250. هذا خارج مدى int8 [-128, 127]، ويُخزَّن بصيغة int32 ليطابق المجمّع.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • Symmetric quantization fixes Z = 0 and quantizes the range [-|r|max, |r|max]; asymmetric keeps a free Z and fits [r_min, r_max].التكميم المتماثل يثبّت Z = 0 ويكمّم المدى [-|r|max, |r|max]، أما غير المتماثل فيترك Z حرًّا ويطابق المدى [r_min, r_max].
  • Asymmetric: S = (r_max - r_min) / (q_max - q_min). Symmetric full range: S = r_min / (q_min - Z) = -|r|max / q_min = |r|max / 2^(N-1).غير المتماثل: S = (r_max - r_min) / (q_max - q_min). المتماثل بالمدى الكامل: S = r_min / (q_min - Z) = -|r|max / q_min = |r|max / 2^(N-1).
  • Full range maps +|r|max to code 2^(N-1), one step past q_max = 2^(N-1) - 1, so the largest positive value is clipped.المدى الكامل يحوّل +|r|max إلى الرمز 2^(N-1)، أي خطوة واحدة بعد q_max = 2^(N-1) - 1، لذلك تُقصّ أكبر قيمة موجبة.
  • Restricted range (not on the slides) uses S = |r|max / (2^(N-1) - 1) and codes [-127, 127] for INT8, giving up one code to stay symmetric.المدى المقيّد (غير موجود في الشرائح) يستخدم S = |r|max / (2^(N-1) - 1) والرموز [-127, 127] في INT8، فيتنازل عن رمز واحد ليبقى متماثلًا.
  • Running matrix (|r|max = 2.12): INT8 full range S = 0.01656, restricted S = 0.01669, 2-bit S = 1.06 with levels {-2.12, -1.06, 0, 1.06}.المصفوفة المرافقة (|r|max = 2.12): INT8 بالمدى الكامل S = 0.01656، وبالمدى المقيّد S = 0.01669، وبـ2 بت S = 1.06 مع المستويات {-2.12, -1.06, 0, 1.06}.
  • At 2 bits, 2.09 / 1.06 = 1.97 rounds to 2, clips to 1 and comes back as 1.06: an error of 1.03.عند 2 بت: 2.09 / 1.06 = 1.97 تُقرّب إلى 2، ثم تُقصّ إلى 1، وتعود بقيمة 1.06، أي بخطأ قدره 1.03.
  • Weights are bell-shaped around 0, so they go symmetric. ReLU activations are one-sided, so they stay asymmetric with Z_X not 0.الأوزان على شكل جرس حول 0، لذلك تُكمَّم تكميمًا متماثلًا. مخرجات ReLU في جهة واحدة، لذلك تبقى غير متماثلة مع Z_X لا يساوي 0.
  • With Z_W = 0: q_Y = (S_W S_X / S_Y)(q_W q_X - Z_X q_W) + Z_Y. Only q_W q_X runs at inference; Z_X q_W is precomputed.مع Z_W = 0: q_Y = (S_W S_X / S_Y)(q_W q_X - Z_X q_W) + Z_Y. وحده q_W q_X يُحسب أثناء الاستدلال، أما Z_X q_W فيُحسب مسبقًا.
  • Bias uses Z_b = 0 and S_b = S_W S_X so q_b adds straight into the int32 accumulator: S_Y(q_Y - Z_Y) = S_W S_X(q_W q_X - Z_X q_W + q_b).الانحياز يستخدم Z_b = 0 وS_b = S_W S_X حتى يُضاف q_b مباشرة إلى المجمّع int32: S_Y(q_Y - Z_Y) = S_W S_X(q_W q_X - Z_X q_W + q_b).
  • S_b is tiny, so q_b is large: S_W = 0.01, S_X = 0.05, b = 0.37 gives q_b = 740, which needs 32 bits.S_b صغير جدًا، لذلك يكون q_b كبيرًا: S_W = 0.01 وS_X = 0.05 وb = 0.37 تعطي q_b = 740، وهذا يحتاج 32 بت.

Sourcesالمصادر