Lecture 05-2 · Quantization II

المحاضرة 05-2 · التكميم (Quantization II)

Computing scale and zero point

حساب الـ scale والـ zero point

Slides 7–10الشرائح 7–10 P1 MUST

Pin the two ends of the real range to the two ends of the integer range, solve two equations, and you have S and Z. This is the most calculable thing in the quantization lectures: expect it on the exam with numbers.

ثبّت طرفي المدى الحقيقي على طرفي المدى الصحيح، وحُلّ معادلتين، فتحصل على S وZ. هذا أكثر شيء قابل للحساب في محاضرات الـ quantization: توقّعه في الامتحان بالأرقام.

Deriving S (slide 7)

اشتقاق S (الشريحة 7)

Require the extreme reals to map onto the extreme integer codes. Both ends obey the same affine map:

نشترط أن تقع القيم الحقيقية القصوى على الرموز الصحيحة القصوى. الطرفان يخضعان للتحويل نفسه:

rmax = S ( qmax − Z )     rmin = S ( qmin − Z )

Subtract the second from the first. Z cancels, leaving one equation in S:

اطرح الثانية من الأولى. يُحذف Z، وتبقى معادلة واحدة في S:

rmax − rmin = S ( qmax − qmin )  ⟹  S = rmax − rminqmax − qmin

Read it as "real width divided by integer width": the real range is cut into (qmax − qmin) equal steps, which is 2N − 1 steps for N bits.

اقرأها هكذا: "العرض الحقيقي مقسوماً على العرض الصحيح": المدى الحقيقي يُقسَّم إلى (qmax − qmin) خطوة متساوية، أي 2N − 1 خطوة لعرض N-bit.

Deriving Z (slide 9)

اشتقاق Z (الشريحة 9)

Now that S is known, rearrange the rmin equation for Z and round, because Z must be an integer:

بعد معرفة S، أعِد ترتيب معادلة rmin لإيجاد Z ثم قرّب، لأن Z يجب أن يكون عدداً صحيحاً:

rmin = S ( qmin − Z )  ⟹  Z = qmin − rminS  ⟹  Z = round ( qmin − rminS )

SLIDE 8 TO 10 · 2-BIT · RANGE OF THE 4 x 4 WEIGHT MATRIX r r min = -1.08 r max = 2.12 0 real width = 2.12 - (-1.08) = 3.20 q q min = -2 Z = -1 0 q max = 1 integer width = 1 - (-2) = 3 steps · S = 3.20 / 3 = 1.07 Z = round(-2 - (-1.08 / 1.07)) = round(-0.99) = -1
S is the ratio of the two widths. Z is the integer that falls under real 0 once the real range is laid over the integer range.
الـ S هو النسبة بين العرضين. والـ Z هو العدد الصحيح الذي يقع تحت الصفر الحقيقي عندما نضع المدى الحقيقي فوق المدى الصحيح.
Worked example · slides 8 and 10مثال محلول · الشريحتان 8 و10

The 4 by 4 weight matrix has rmin = −1.08 and rmax = 2.12. Quantize to 2-bit signed, so qmin = −2 and qmax = 1.

مصفوفة الأوزان 4 × 4 فيها rmin = −1.08 وrmax = 2.12. كمّمها إلى 2-bit signed، أي qmin = −2 وqmax = 1.

Scale
  S = (r_max - r_min) / (q_max - q_min)
    = (2.12 - (-1.08)) / (1 - (-2))
    = 3.20 / 3
    = 1.0667  ~ 1.07

Zero point
  r_min / S = -1.08 / 1.07 = -1.0093
  Z = round(q_min - r_min / S)
    = round(-2 - (-1.0093))
    = round(-2 + 1.0093)
    = round(-0.9907)
    = -1

Check with the other end (should agree after rounding)
  q_max - r_max / S = 1 - 2.12 / 1.07 = 1 - 1.981 = -0.981  -> -1

الخطوات: S = 3.20 / 3 ≈ 1.07، ثم Z = round(−2 + 1.0093) = round(−0.9907) = −1. والتحقق من الطرف الآخر يعطي −0.981 الذي يُقرَّب أيضاً إلى −1.

Quantizing and dequantizing a value

تكميم قيمة وإعادتها (dequantize)

Inverting r = S(q − Z) gives q = r/S + Z. The result must be an integer inside the representable range, so round and clamp:

عكس r = S(q − Z) يعطي q = r/S + Z. يجب أن تكون النتيجة عدداً صحيحاً داخل المدى القابل للتمثيل، لذلك نقرّب ثم نقصّ (clamp):

q = clamp( round( r / S ) + Z,  qmin,  qmax )     r̂ = S ( q − Z )

  1. Divide by the scale: r / S puts the value in integer units.
  2. اقسم على الـ scale: r / S يضع القيمة بوحدات صحيحة.
  3. Round to the nearest integer.
  4. قرّب إلى أقرب عدد صحيح.
  5. Add the zero point Z (already an integer, so the sum stays an integer).
  6. أضف الـ zero point Z (وهو عدد صحيح أصلاً، فيبقى المجموع عدداً صحيحاً).
  7. Clamp to [qmin, qmax] so outliers saturate instead of wrapping around.
  8. قصّ (clamp) النتيجة إلى [qmin, qmax] حتى تتشبّع القيم الشاذة بدلاً من أن تلتف.
  9. To check, dequantize: r̂ = S(q − Z). The error r − r̂ is at most S/2 for in-range values.
  10. للتحقق، أعِد التحويل (dequantize): r̂ = S(q − Z). والخطأ r − r̂ لا يتجاوز S/2 للقيم داخل المدى.

الخطوات: اقسم على S لتصبح القيمة بوحدات صحيحة؛ قرّب لأقرب عدد صحيح؛ أضف Z؛ قصّ النتيجة إلى [qmin, qmax] حتى تتشبّع القيم الشاذة بدل أن تلتف؛ وللتحقق أعِد التحويل r̂ = S(q − Z)، والخطأ لا يتجاوز S/2 للقيم داخل المدى.

Worked exampleمثال محلول

Same S = 1.07, Z = −1. Quantize three values: two weights from the matrix and an out-of-range value 3.00.

نفس S = 1.07 وZ = −1. كمّم ثلاث قيم: وزنين من المصفوفة وقيمة خارج المدى 3.00.

r = 1.48 : 1.48 / 1.07 = 1.383 -> round 1 -> 1 + (-1) = 0
           r_hat = 1.07 x (0 + 1)  = 1.07     error 0.41

r = -0.98: -0.98 / 1.07 = -0.916 -> round -1 -> -1 + (-1) = -2
           r_hat = 1.07 x (-2 + 1) = -1.07   error 0.09

r = 3.00 : 3.00 / 1.07 = 2.804 -> round 3 -> 3 + (-1) = 2
           2 > q_max = 1  -> clamp to 1
           r_hat = 1.07 x (1 + 1)  = 2.14     error 0.86 (clipping error)

Both matrix entries reproduce the slide 5 codes (0 and −2). The value 3.00 is clipped, and its error exceeds S/2 because it lies outside [rmin, rmax].

الوزنان من المصفوفة يعيدان رموز الشريحة 5 نفسها (0 و−2). أما 3.00 فيُقصّ، وخطؤه يتجاوز S/2 لأنه خارج [rmin, rmax].

Worked example · fresh numbers, 3-bitمثال محلول · أرقام جديدة، 3-bit

An activation tensor ranges over [−0.4, 2.4]. Quantize to 3-bit signed (qmin = −4, qmax = 3), then quantize r = 1.3.

tensor للـ activations مداه [−0.4, 2.4]. كمّمه إلى 3-bit signed (qmin = −4، qmax = 3)، ثم كمّم r = 1.3.

S = (2.4 - (-0.4)) / (3 - (-4)) = 2.8 / 7 = 0.4
Z = round(-4 - (-0.4 / 0.4)) = round(-4 + 1) = -3

check ends:  r_min -> -0.4/0.4 = -1 -> -1 + (-3) = -4 = q_min
             r_max ->  2.4/0.4 =  6 ->  6 + (-3) =  3 = q_max
             r = 0 ->   0 + (-3) = -3 = Z

r = 1.3 : 1.3 / 0.4 = 3.25 -> round 3 -> 3 + (-3) = 0
          r_hat = 0.4 x (0 - (-3)) = 1.2     error 0.1

النتائج: S = 0.4، Z = −3، والطرفان يقعان تماماً على qmin وqmax، وr = 1.3 يصبح q = 0 ويُستعاد كـ 1.2 بخطأ 0.1.

Worked example · fresh numbers, 8-bitمثال محلول · أرقام جديدة، 8-bit

Range [−0.5, 2.0], 8-bit signed (qmin = −128, qmax = 127). Find S and Z, then quantize r = 0.73.

المدى [−0.5, 2.0]، 8-bit signed (qmin = −128، qmax = 127). أوجد S وZ، ثم كمّم r = 0.73.

S = (2.0 - (-0.5)) / (127 - (-128)) = 2.5 / 255 = 0.009804
r_min / S = -0.5 / 0.009804 = -51.0
Z = round(-128 + 51.0) = -77

r = 0.73 : 0.73 / 0.009804 = 74.46 -> round 74 -> 74 + (-77) = -3
           r_hat = 0.009804 x (-3 + 77) = 0.009804 x 74 = 0.7255
           error = 0.73 - 0.7255 = 0.0045  (below S/2 = 0.0049)

النتائج: S = 2.5 / 255 ≈ 0.0098، Z = −77، وr = 0.73 يصبح q = −3 ويُستعاد كـ 0.7255. مع 8-bit الخطوة أصغر بكثير، فالخطأ صغير جداً.

Exam trapفخ في الامتحان

Sign errors with a negative rmin. Z = qmin − rmin/S, and rmin/S is negative, so you add: −2 − (−1.0093) = −0.99. Writing −2 − 1.0093 = −3.01 gives Z = −3, which is not even a 2-bit code.

أخطاء الإشارة مع rmin سالب. Z = qmin − rmin/S، وrmin/S سالب، لذلك أنت تجمع: −2 − (−1.0093) = −0.99. كتابة −2 − 1.0093 = −3.01 تعطي Z = −3، وهذا ليس رمزاً صالحاً في 2-bit أصلاً.

Pair rmin with qmin. Do not use 0 for qmin (that is the unsigned habit) and do not mix rmin with qmax. For the 2-bit example the denominator of S is 1 − (−2) = 3, not 1 or 4.

اقرن rmin مع qmin. لا تستخدم 0 بدل qmin (هذه عادة unsigned)، ولا تخلط rmin مع qmax. في مثال 2-bit مقام S هو 1 − (−2) = 3، وليس 1 ولا 4.

Round Z, keep S as a float. S = 1.0667 stays fractional. Z = −0.99 must become −1.

قرّب Z، وأبقِ S عشرياً. S = 1.0667 يبقى كسرياً. أما Z = −0.99 فيجب أن يصبح −1.

Clamp after adding Z. Forgetting the clamp lets 3.00 become q = 2, an illegal 2-bit code (in hardware it would wrap to −2).

قصّ بعد إضافة Z. نسيان الـ clamp يجعل 3.00 يصبح q = 2، وهو رمز غير صالح في 2-bit (في العتاد سيلتف إلى −2).

Memory trickحيلة للحفظ

"S is width over width; Z is where zero falls." S = real width / integer width. Z = round(qmin − rmin/S): start at qmin and walk |rmin|/S steps up toward real 0.

"S عرض على عرض؛ وZ حيث يقع الصفر." S = العرض الحقيقي ÷ العرض الصحيح. وZ = round(qmin − rmin/S): ابدأ من qmin وتقدّم |rmin|/S خطوة نحو الصفر الحقيقي.

Check yourself

اختبر نفسك

A tensor ranges over [−1.08, 2.12] and is quantized to 2-bit signed integers. What is S?

tensor مداه [−1.08, 2.12] ويُكمَّم إلى أعداد صحيحة بإشارة بعرض 2-bit. كم يساوي S؟

B. S = (2.12 − (−1.08)) / (1 − (−2)) = 3.20 / 3 ≈ 1.07. A divides by 4 (the number of codes, not the number of steps). C forgets the denominator. D uses 1.04 / 3 by dropping the sign of rmin.

B. S = (2.12 − (−1.08)) / (1 − (−2)) = 3.20 / 3 ≈ 1.07. الخيار A يقسم على 4 (عدد الرموز، لا عدد الخطوات). والخيار C ينسى المقام. والخيار D يستخدم 1.04 / 3 بإسقاط إشارة rmin.

Continuing with S = 1.07, rmin = −1.08 and qmin = −2, what is Z?

بالاستمرار مع S = 1.07 وrmin = −1.08 وqmin = −2، كم يساوي Z؟

C. Z = round(−2 − (−1.08 / 1.07)) = round(−2 + 1.009) = round(−0.99) = −1. A is the sign error (−2 − 1.009). B forgets to round, but Z must be an integer.

C. Z = round(−2 − (−1.08 / 1.07)) = round(−2 + 1.009) = round(−0.99) = −1. الخيار A هو خطأ الإشارة (−2 − 1.009). والخيار B ينسى التقريب، مع أن Z يجب أن يكون عدداً صحيحاً.

A tensor ranges over [−0.4, 2.4] and is quantized to 3-bit signed integers. What are S and Z?

tensor مداه [−0.4, 2.4] ويُكمَّم إلى أعداد صحيحة بإشارة بعرض 3-bit. ما قيمتا S وZ؟

A. q range is −4 to 3, so S = 2.8 / 7 = 0.4. Z = round(−4 − (−0.4 / 0.4)) = round(−4 + 1) = −3. B divides by 8 codes. C is the sign error. D uses qmax with rmin.

A. مدى q من −4 إلى 3، لذلك S = 2.8 / 7 = 0.4. وZ = round(−4 − (−0.4 / 0.4)) = round(−4 + 1) = −3. الخيار B يقسم على 8 رموز. والخيار C هو خطأ الإشارة. والخيار D يستخدم qmax مع rmin.

With S = 1.07 and Z = −1 (2-bit), what integer code does the weight r = 1.48 get?

مع S = 1.07 وZ = −1 (2-bit)، ما الرمز الصحيح الذي يأخذه الوزن r = 1.48؟

D. round(1.48 / 1.07) = round(1.383) = 1, then 1 + (−1) = 0, which is in range. It decodes to 1.07 × (0 + 1) = 1.07. A forgets to add Z. B subtracts Z instead of adding it.

D. round(1.48 / 1.07) = round(1.383) = 1، ثم 1 + (−1) = 0، وهو داخل المدى. ويُفك إلى 1.07 × (0 + 1) = 1.07. الخيار A ينسى إضافة Z. والخيار B يطرح Z بدلاً من إضافته.

During quantization, why is the clamp to [qmin, qmax] needed?

أثناء الـ quantization، لماذا نحتاج القصّ إلى [qmin, qmax]؟

B. An outlier such as 3.00 gives round(2.80) + (−1) = 2, outside the 2-bit range; clamping saturates it to 1. Clipped values have error larger than S/2, so C is false. Rounding already yields integers (D), and Z is made an integer by rounding, not clamping (A).

B. القيمة الشاذة مثل 3.00 تعطي round(2.80) + (−1) = 2، وهي خارج مدى 2-bit، فيشبّعها القص (clamp) إلى 1. والقيم المقصوصة خطؤها أكبر من S/2، لذا فالخيار C خطأ. والتقريب يعطي أعداداً صحيحة أصلاً (D)، وZ يصبح صحيحاً بالتقريب لا بالقص (A).