Majid Al-RaimiScale and Zero Point: Deriving S and Zالمقياس (Scale) ونقطة الصفر (Zero Point): اشتقاق S وZ

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

Scale and Zero Point: Deriving S and Zالمقياس (Scale) ونقطة الصفر (Zero Point): اشتقاق S وZ

Defines the affine map r = S(q - Z), where the scale S is a float and the zero point Z is an integer, along with the signed N-bit integer range. Both parameters are then derived by matching the endpoints of the float range to the endpoints of the integer range, and computed for the running example.يعرّف الربط الأفيني r = S(q - Z)، حيث المقياس S عدد عائم ونقطة الصفر Z عدد صحيح، مع مدى العدد الصحيح ذي الإشارة من N بت. ثم يُشتق المعاملان بمطابقة طرفي مدى الأعداد العائمة مع طرفي مدى الأعداد الصحيحة، ويُحسبان للمثال الجاري.

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

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

Part 1 gave the idea: store small integers and recover real numbers with a straight line. This part finds the two numbers that define that line. One is the scale S and the other is the zero point Z. Every later formula in this lecture assumes you can produce both from a range in under a minute. That covers the quantized matrix multiply, the symmetric shortcut and the integer-only layer.

The chain is short. First, what each symbol in r = S(q - Z) is and what type it has. Second, the integer range an N-bit code can hold. Third, the derivation of S from the two ends of the range. Fourth, the derivation of Z and the reason it must be an integer. For the COE 592 midterm on Wed 28 Oct 2026, expect three question shapes: derive S and Z, compute them for given numbers, and explain why zero must be exactly representable.

أعطانا الجزء 1 الفكرة: نخزّن أعدادًا صحيحة صغيرة ونستعيد الأعداد الحقيقية بخط مستقيم. هذا الجزء يجد العددين اللذين يحددان ذلك الخط. الأول هو المقياس S (scale) والثاني هو نقطة الصفر Z (zero point). كل صيغة لاحقة في هذه المحاضرة تفترض أنك تستطيع حسابهما من مدى معين في أقل من دقيقة. ويشمل ذلك ضرب المصفوفات المكمّمة، والاختصار المتماثل (symmetric)، والطبقة التي تعمل بالأعداد الصحيحة فقط.

السلسلة قصيرة. أولًا: ما معنى كل رمز في r = S(q - Z) وما نوعه. ثانيًا: مدى الأعداد الصحيحة الذي يستوعبه رمز من N بت. ثالثًا: اشتقاق S من طرفي المدى. رابعًا: اشتقاق Z وسبب وجوب أن يكون عددًا صحيحًا. في اختبار منتصف الفصل لمقرر COE 592 يوم Wed 28 Oct 2026، توقّع ثلاثة أشكال من الأسئلة: اشتقاق S وZ، وحسابهما لأعداد معطاة، وشرح لماذا يجب أن يكون الصفر قابلًا للتمثيل بدقة تامة.

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

  1. State the affine map r = S(q - Z) and say which symbols are integers and which are floating point.تكتب الدالة التآلفية r = S(q - Z) وتحدد أي الرموز أعداد صحيحة وأيها أعداد عشرية (floating point).
  2. Write the signed N-bit range q_min = -2^(N-1), q_max = 2^(N-1) - 1 for any N without a table.تكتب مدى الأعداد الصحيحة ذات الإشارة بعدد N من البتات، q_min = -2^(N-1) و q_max = 2^(N-1) - 1، لأي قيمة N دون الرجوع إلى جدول.
  3. Derive S = (r_max - r_min) / (q_max - q_min) by subtracting the two endpoint equations, and explain why Z cancels.تشتق S = (r_max - r_min) / (q_max - q_min) بطرح معادلتي الطرفين، وتشرح لماذا يختفي Z.
  4. Derive Z = round(q_min - r_min / S) and explain why Z must be an integer.تشتق Z = round(q_min - r_min / S) وتشرح لماذا يجب أن يكون Z عددًا صحيحًا.
  5. Compute S, Z, a quantized code and its reconstruction for any given range and bit width.تحسب S و Z والرمز المكمّم (quantized code) والقيمة المعاد بناؤها منه لأي مدى وأي عدد بتات.

Take the top-left weight of the slide's 4 x 4 matrix, 2.09. After quantization, the chip stores the 2-bit code 1. It also keeps two numbers for the whole matrix: Z = -1 and S = 1.07. To get a real number back it computes (1 - (-1)) x 1.07 = 2.14. That is close to 2.09 but not equal to it.

خذ الوزن في الزاوية العليا اليسرى من مصفوفة الشريحة ذات الحجم 4 x 4، وقيمته 2.09. بعد التكميم تخزّن الشريحة الإلكترونية الرمز ذا البتين 1. وتحتفظ أيضًا بعددين للمصفوفة كلها: Z = -1 وS = 1.07. ولكي تستعيد عددًا حقيقيًا تحسب (1 - (-1)) x 1.07 = 2.14. هذه القيمة قريبة من 2.09 لكنها لا تساويها.

The map and the types of its symbolsالدالة وأنواع رموزها

That recovery rule is Linear quantization written as an Affine mapping: a scale times a shifted integer.

قاعدة الاستعادة هذه هي linear quantization (التكميم الخطي) مكتوبًا على شكل affine mapping (دالة تآلفية): مقياس مضروب في عدد صحيح مُزاح.

r=S (q−Z)r = S\,(q - Z)
Linear quantization maps an integer code q to a real value rالتكميم الخطي يحوّل رمزًا صحيحًا q إلى قيمة حقيقية r

Read the map from right to left. Start from the stored integer Quantized value (q), subtract the Zero point (Z), then multiply by the Scale (S). The types matter as much as the formula, and they are a favourite exam detail:

اقرأ الدالة من اليمين إلى اليسار. ابدأ من العدد الصحيح المخزّن quantized value (q)، واطرح منه zero point (Z)، ثم اضرب الناتج في scale (S). الأنواع مهمة بقدر أهمية الصيغة نفسها، وهي تفصيل يحب الامتحان أن يسأل عنه:

SymbolالرمزMeaningالمعنىTypeالنوعHow manyالعدد
rrReal value (weight or activation)القيمة الحقيقية (وزن أو تفعيل)Floating point (FP32)عدد عشري (FP32)Given by the modelيعطيه النموذج
qqStored codeالرمز المخزّنInteger (N-bit signed)عدد صحيح (بإشارة من N بت)Computed per valueيُحسب لكل قيمة
ZZZero point: the code for real 0نقطة الصفر: الرمز الذي يمثل الصفر الحقيقي 0Integer (same type as q)عدد صحيح (من نفس نوع q)One per tensor, derivedواحدة لكل موتر، مشتقة
SSScale: real step between neighbouring codesالمقياس: الخطوة الحقيقية بين رمزين متجاورينFloating point (FP32)عدد عشري (FP32)One per tensor, derivedواحد لكل موتر، مشتق
The four symbols in r = S(q - Z)الرموز الأربعة في r = S(q - Z)

The two quantities that live with each value (r and q) are a float and an integer. The two quantization parameters (S and Z) are also a float and an integer. Z has the same type as q, so in the slide example it is a 2-bit signed integer. That is no accident: the paper the slides come from, Jacob et al. (CVPR 2018), defines Z as the quantized value that corresponds to real 0. A value can only be a code if it is an integer in the code's type.

الكميتان المرتبطتان بكل قيمة (r وq) هما عدد عشري وعدد صحيح. ومعاملا التكميم (S وZ) هما أيضًا عدد عشري وعدد صحيح. نوع Z هو نفس نوع q، لذلك في مثال الشرائح هو عدد صحيح بإشارة من بتين. وهذا ليس صدفة: الورقة التي أُخذت منها الشرائح، Jacob et al. (CVPR 2018)، تعرّف Z بأنه القيمة المكمّمة التي تقابل الصفر الحقيقي 0. ولا يمكن لقيمة أن تكون رمزًا إلا إذا كانت عددًا صحيحًا من نوع الرموز.

Codes -2, -1, 0, 1 shift right by 1 (subtract Z = -1), then stretch by S = 1.07. Code Z lands on real 0الرموز -2 و -1 و 0 و 1 تُزاح إلى اليمين بمقدار 1 (نطرح Z = -1)، ثم تُمدّ بالمقياس S = 1.07. الرمز Z يقع على الصفر الحقيقي 0

The picture shows the two operations in order. Subtracting Z slides the integer grid so the code Z sits on zero. Multiplying by S stretches the spacing from 1 to S. The Reconstructed weights therefore always lie on a uniform grid of spacing S. The gap between an original value and its grid point is the Quantization error, at most S/2 for in-range values.

يعرض الشكل العمليتين بالترتيب. طرح Z يزيح شبكة الأعداد الصحيحة بحيث يقع الرمز Z على الصفر. والضرب في S يمدّ المسافة بين النقاط من 1 إلى S. لذلك تقع reconstructed weights (الأوزان المعاد بناؤها) دائمًا على شبكة منتظمة المسافة بين نقاطها S. والفرق بين القيمة الأصلية ونقطة الشبكة الخاصة بها هو quantization error (خطأ التكميم)، ولا يتجاوز S/2 للقيم الواقعة داخل المدى.

Worked exampleمثال محلول

Check two cells of slide 5 by hand (S = 1.07, Z = -1)تحقّق يدويًا من خليتين في الشريحة 5 (S = 1.07، Z = -1)

  1. Quantize r = 2.09كمّم r = 2.09

    q = round(2.09 / 1.07) + Z = round(1.953) - 1 = 2 - 1 = 1. The slide shows 1.
    q = round(2.09 / 1.07) + Z = round(1.953) - 1 = 2 - 1 = 1. والشريحة تعرض 1.
  2. Reconstructأعد البناء

    1.07 x (1 - (-1)) = 2.14. The error is 0.05, well under S/2 = 0.535.
    1.07 x (1 - (-1)) = 2.14. الخطأ يساوي 0.05، وهو أقل بكثير من S/2 = 0.535.
  3. Quantize r = 0كمّم r = 0

    q = round(0 / 1.07) + Z = -1. Real zero becomes exactly the code Z, and 1.07 x (-1 - (-1)) = 0 decodes back to exactly zero.
    q = round(0 / 1.07) + Z = -1. الصفر الحقيقي يصبح الرمز Z بالضبط، و1.07 x (-1 - (-1)) = 0 يُفك ترميزه ليعود صفرًا تمامًا.
  4. Resultالنتيجة

    Forward: q = round(r / S) + Z, clamped to the code range. Backward: r = S(q - Z). Zero goes in and comes out with no error.
    الاتجاه الأمامي: q = round(r / S) + Z، مع حصر الناتج (clamp) داخل مدى الرموز. الاتجاه العكسي: r = S(q - Z). الصفر يدخل ويخرج دون أي خطأ.

Recall

In r = S(q - Z), which two symbols are integers and which two are floating point?في r = S(q - Z)، أي رمزين عددان صحيحان وأي رمزين عددان عشريان؟

q and Z are integers (the same N-bit signed type). r and S are floating point. S and Z are the quantization parameters, one pair per tensor.
q وZ عددان صحيحان (من نفس النوع: عدد صحيح بإشارة من N بت). وr وS عددان عشريان. S وZ هما معاملا التكميم، وهناك زوج واحد منهما لكل موتر (tensor).

With 2 bits you can write exactly four patterns: 00, 01, 10, 11. Read as signed numbers they are 0, 1, -2 and -1. So a 2-bit code lives in [-2, 1], and quantization must squeeze the whole real range of the matrix onto those four points.

باستخدام بتين يمكنك كتابة أربعة أنماط فقط: 00 و01 و10 و11. وإذا قرأتها كأعداد بإشارة فهي 0 و1 و-2 و-1. إذن الرمز ذو البتين يقع في [-2, 1]، وعلى التكميم أن يضغط المدى الحقيقي الكامل للمصفوفة على هذه النقاط الأربع.

Two number linesخطّا أعداد

Picture two parallel lines. The top line is the real axis r. On it, the tensor occupies an interval from r_min and r_max (here -1.08 to 2.12). The bottom line holds the integer codes from q_min and q_max. Quantization ties the two lines at their ends: q_min maps to r_min and q_max maps to r_max. Somewhere between them, the code Z points straight up at real 0. Every derivation in this part is just algebra on that picture.

تخيّل خطين متوازيين. الخط العلوي هو المحور الحقيقي r. وعليه يشغل الموتر فترة تمتد بين r_min and r_max (هنا من -1.08 إلى 2.12). والخط السفلي يحمل الرموز الصحيحة بين q_min and q_max. التكميم يربط الخطين عند طرفيهما: q_min يقابل r_min وq_max يقابل r_max. وفي مكان ما بينهما يشير الرمز Z مباشرة إلى الصفر الحقيقي 0. كل اشتقاق في هذا الجزء هو مجرد جبر على هذه الصورة.

The signed N-bit rangeمدى الأعداد ذات الإشارة بعدد N من البتات

The codes use Two's complement, the standard signed encoding. With a Bit width (N) of N there are 2^N codes. Half of them are negative and the other half are zero plus the positives. That is why the range is lopsided by one:

تستخدم الرموز two's complement (المتمم الثنائي)، وهو الترميز القياسي للأعداد ذات الإشارة. ومع bit width (N) (عرض بتات) يساوي N يوجد 2^N رمزًا. نصفها سالب، والنصف الآخر هو الصفر مع الأعداد الموجبة. لهذا يكون المدى غير متوازن بفارق واحد:

qmin⁡=−2N−1,qmax⁡=2N−1−1q_{\min} = -2^{N-1}, \qquad q_{\max} = 2^{N-1} - 1
Signed N-bit integer rangeمدى العدد الصحيح ذي الإشارة بعدد N من البتات
Bit width Nعرض البتات NCodesعدد الرموزq_minq_minq_maxq_max
2244-2-211
3388-4-433
441616-8-877
88256256-128-128127127
NN2^N2^N-2^(N-1)-2^(N-1)2^(N-1) - 12^(N-1) - 1
Signed code ranges you should know without thinkingمديات الرموز ذات الإشارة التي يجب أن تعرفها دون تفكير
2, 3 and 4 bits: each extra bit doubles the codes, and the negative side always has one more code than the positive side2 و 3 و 4 بتات: كل بت إضافي يضاعف عدد الرموز، والجانب السالب فيه دائمًا رمز واحد زيادة على الجانب الموجب
BinaryثنائيDecimalعشريRole in the exampleدوره في المثال
010111q_maxq_max
000000code 0, which is not real 0 hereالرمز 0، وهو هنا لا يمثل الصفر الحقيقي
1111-1-1Z in the running exampleZ في المثال المستمر
1010-2-2q_minq_min
2-bit two's complement codes used in the running example (slides 8 and 10)رموز المتمم الثنائي (two's complement) ذات البتين المستخدمة في المثال المستمر (الشريحتان 8 و 10)

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

What is the signed 5-bit integer range [q_min, q_max]?ما مدى الأعداد الصحيحة ذات الإشارة من 5 بتات [q_min, q_max]؟

The slide matrix spans -1.08 to 2.12, a real width of 3.20. The 2-bit codes -2, -1, 0, 1 have 3 gaps between them. Stretch three equal gaps over a width of 3.20 and each gap is 3.20 / 3 = 1.0667 real units. That number is the scale.

مصفوفة الشريحة تمتد من -1.08 إلى 2.12، أي بعرض حقيقي قدره 3.20. والرموز ذات البتين -2, -1, 0, 1 بينها 3 فجوات. إذا مددت ثلاث فجوات متساوية على عرض 3.20 فإن كل فجوة تساوي 3.20 / 3 = 1.0667 وحدة حقيقية. هذا العدد هو المقياس.

Width of the real range divided by the number of integer gaps: 3.20 / 3 = 1.07 per stepعرض المدى الحقيقي مقسومًا على عدد الفجوات بين الأعداد الصحيحة: 3.20 / 3 = 1.07 لكل خطوة

The derivation (slide 7)الاشتقاق (الشريحة 7)

Now turn the intuition into algebra. Demand that the affine map send each end of the integer range to the matching end of the real range. That gives two equations, both containing the unknowns S and Z:

الآن حوّل هذا الحدس إلى جبر. اشترط أن ترسل الدالة التآلفية كل طرف من مدى الأعداد الصحيحة إلى الطرف المقابل من المدى الحقيقي. هذا يعطي معادلتين، كل منهما تحتوي المجهولين S وZ:

rmax⁡=S (qmax⁡−Z)r_{\max} = S\,(q_{\max} - Z)
Top endpointالطرف العلوي
rmin⁡=S (qmin⁡−Z)r_{\min} = S\,(q_{\min} - Z)
Bottom endpointالطرف السفلي

Subtract the second from the first. Expanding gives S q_max - S Z - S q_min + S Z. The two S Z terms cancel, so the zero point disappears:

اطرح الثانية من الأولى. بعد فك الأقواس نحصل على S q_max - S Z - S q_min + S Z. الحدّان S Z يلغي أحدهما الآخر، فتختفي نقطة الصفر:

rmax⁡−rmin⁡=S (qmax⁡−qmin⁡)r_{\max} - r_{\min} = S\,(q_{\max} - q_{\min})
Z cancels in the subtractionتُحذف Z عند الطرح
S=rmax⁡−rmin⁡qmax⁡−qmin⁡S = \dfrac{r_{\max} - r_{\min}}{q_{\max} - q_{\min}}
The scale: real width divided by integer widthالمقياس (scale): العرض الحقيقي مقسومًا على العرض الصحيح

Worked exampleمثال محلول

Slide 8: the scale of the 2-bit exampleالشريحة 8: مقياس المثال ذي البتين

  1. Read the range off the matrixاقرأ المدى من المصفوفة

    The largest entry is r_max = 2.12 and the smallest is r_min = -1.08. These are the two cells highlighted on slide 8.
    أكبر عنصر هو r_max = 2.12 وأصغر عنصر هو r_min = -1.08. وهما الخليتان المظللتان في الشريحة 8.
  2. Read the integer rangeاقرأ مدى الأعداد الصحيحة

    For N = 2: q_max = 1 and q_min = -2.
    عندما N = 2: q_max = 1 وq_min = -2.
  3. Divideاقسم

    S = (2.12 - (-1.08)) / (1 - (-2)) = 3.20 / 3 = 1.0667.
    S = (2.12 - (-1.08)) / (1 - (-2)) = 3.20 / 3 = 1.0667.
  4. Resultالنتيجة

    S = 1.0667, which the slides round to 1.07. One integer step is worth about 1.07 in real units.
    S = 1.0667، وتقرّبه الشرائح إلى 1.07. الخطوة الصحيحة الواحدة تساوي تقريبًا 1.07 بالوحدات الحقيقية.

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

In the derivation on slide 7, why is S computed before Z?في الاشتقاق في الشريحة 7، لماذا يُحسب S قبل Z؟

Recall

Derive S from the two endpoint equations in two lines.اشتق S من معادلتي الطرفين في سطرين.

r_max - r_min = S(q_max - Z) - S(q_min - Z) = S(q_max - q_min), so S = (r_max - r_min) / (q_max - q_min). The -SZ terms cancel.
r_max - r_min = S(q_max - Z) - S(q_min - Z) = S(q_max - q_min)، إذن S = (r_max - r_min) / (q_max - q_min). الحدّان -SZ يلغي أحدهما الآخر.

You now know one integer step is worth 1.0667. The smallest real value -1.08 sits at code -2. Walk up from there: -1.08 is about one step below zero, so real zero must sit about one code above -2, at code -1. That code is the zero point.

أنت تعرف الآن أن الخطوة الصحيحة الواحدة تساوي 1.0667. وأصغر قيمة حقيقية -1.08 تقع عند الرمز -2. اصعد من هناك: -1.08 تقع تحت الصفر بخطوة واحدة تقريبًا، إذن الصفر الحقيقي يجب أن يقع فوق -2 برمز واحد تقريبًا، أي عند الرمز -1. هذا الرمز هو نقطة الصفر.

The derivation (slide 9)الاشتقاق (الشريحة 9)

Take the bottom endpoint equation and solve it for Z, now that S is known:

خذ معادلة الطرف السفلي وحلّها لإيجاد Z، بعد أن أصبح S معروفًا:

rmin⁡=S (qmin⁡−Z)  ⇒  Z=qmin⁡−rmin⁡Sr_{\min} = S\,(q_{\min} - Z) \;\Rightarrow\; Z = q_{\min} - \dfrac{r_{\min}}{S}
Solve the bottom endpoint for Zحلّ معادلة الطرف السفلي لإيجاد Z

Nothing forces that expression to be a whole number, but Z must be a code. So it is rounded:

لا شيء يجبر هذا التعبير على أن يكون عددًا صحيحًا، لكن Z يجب أن يكون رمزًا. لذلك نقرّبه:

Z=round⁡ ⁣(qmin⁡−rmin⁡S)Z = \operatorname{round}\!\left(q_{\min} - \dfrac{r_{\min}}{S}\right)
The zero point, rounded so it is an integerنقطة الصفر (zero point)، مقرّبةً لتكون عددًا صحيحًا

Using the top equation instead gives Z = q_max - r_max / S. Before rounding it is the same number, because S was built to make both ends agree.

لو استخدمت المعادلة العليا بدلًا منها لحصلت على Z = q_max - r_max / S. وقبل التقريب هي العدد نفسه، لأن S بُني أصلًا ليجعل الطرفين متوافقين.

Worked exampleمثال محلول

Slide 10: the zero point of the 2-bit exampleالشريحة 10: نقطة الصفر في المثال ذي البتين

  1. Divide r_min by Sاقسم r_min على S

    r_min / S = -1.08 / 1.07 = -1.009.
    r_min / S = -1.08 / 1.07 = -1.009.
  2. Subtract from q_minاطرح من q_min

    q_min - r_min / S = -2 - (-1.009) = -2 + 1.009 = -0.991.
    q_min - r_min / S = -2 - (-1.009) = -2 + 1.009 = -0.991.
  3. Roundقرّب

    Z = round(-0.991) = -1. With the unrounded S = 1.0667 you get round(-0.9875) = -1, the same answer.
    Z = round(-0.991) = -1. ومع القيمة غير المقرّبة S = 1.0667 تحصل على round(-0.9875) = -1، أي الجواب نفسه.
  4. Resultالنتيجة

    Z = -1, stored as the 2-bit pattern 11. Every zero in the matrix is stored as -1, which matches the quantized matrix on slide 5.
    Z = -1، ويُخزَّن بالنمط الثنائي ذي البتين 11. كل صفر في المصفوفة يُخزَّن على شكل -1، وهذا يطابق المصفوفة المكمّمة في الشريحة 5.

Why Z must be an integer: exact zeroلماذا يجب أن يكون Z عددًا صحيحًا: الصفر الدقيق

Rounding Z costs something. The ends of the range no longer land exactly. With the unrounded Z = -0.9875, code 1 would decode to 1.0667 x 1.9875 = 2.12. With Z = -1 it decodes to 1.0667 x 2 = 2.13, and code -2 to -1.07 rather than -1.08. The whole grid slides by 0.0125S, never more than S/2. The design accepts that small shift to buy something more valuable, the Exact representation of zero. If Z is an integer, then the code q = Z exists, and r = S(Z - Z) = 0 exactly.

تقريب Z له ثمن. طرفا المدى لم يعودا يقعان في مكانهما بالضبط. مع القيمة غير المقرّبة Z = -0.9875 كان الرمز 1 سيُفك إلى 1.0667 x 1.9875 = 2.12. ومع Z = -1 يُفك إلى 1.0667 x 2 = 2.13، والرمز -2 يُفك إلى -1.07 بدلًا من -1.08. الشبكة كلها تنزاح بمقدار 0.0125S، ولا يتجاوز ذلك أبدًا S/2. يقبل التصميم هذه الإزاحة الصغيرة ليكسب شيئًا أثمن، وهو exact representation of zero (التمثيل الدقيق للصفر). إذا كان Z عددًا صحيحًا فإن الرمز q = Z موجود، وr = S(Z - Z) = 0 تمامًا.

Real zero is not a rare value in a neural network. It appears all the time:

الصفر الحقيقي ليس قيمة نادرة في الشبكة العصبية. إنه يظهر طوال الوقت:

  • Zero padding. Convolutions pad feature maps with zeros at the borders. Jacob et al. give this as the motivation for exact zero.الحشو بالأصفار (zero padding). الالتفافات (convolutions) تحشو خرائط الخصائص بأصفار عند الحواف. ويذكر Jacob et al. هذا بوصفه الدافع لتمثيل الصفر بدقة.
  • ReLU outputs. Every negative pre-activation becomes exactly zero, so a large share of the activations after a ReLU are zero.مخرجات ReLU. كل قيمة سالبة قبل التفعيل تصبح صفرًا تمامًا، لذلك تكون نسبة كبيرة من التفعيلات بعد ReLU أصفارًا.
  • Pruned weights. The pruning lectures (04-1 and 04-2) set many weights to exactly zero, and quantizing them must keep them zero.الأوزان المقلّمة (pruned weights). محاضرتا التقليم (04-1 و04-2) تجعلان كثيرًا من الأوزان صفرًا تمامًا، ويجب أن يحافظ تكميمها على كونها صفرًا.
With an integer Z, every padded border cell decodes to exactly 0. With Z = -0.6, the nearest code -1 decodes to -0.4S, a bias on every border cellمع Z صحيح، كل خلية حدّية محشوة تُفك إلى 0 تمامًا. ومع Z = -0.6 فإن أقرب رمز وهو -1 يُفك إلى -0.4S، وهذا انحياز على كل خلية حدّية

Now suppose Z were fractional, say -0.6. No stored integer decodes to zero. The best the hardware can do is the nearest code -1, which decodes to S(-1 - (-0.6)) = -0.4S. Every padded cell then carries the same -0.4S error. It is systematic rather than random, so it does not average out. It adds up in every convolution window that touches the border, which biases the layer output. A fractional zero point would also need a float subtraction inside the integer pipeline, which part 3 works hard to avoid.

افترض الآن أن Z كسري، مثلًا -0.6. لا يوجد عدد صحيح مخزّن يُفك إلى الصفر. أفضل ما يستطيعه العتاد هو أقرب رمز -1، الذي يُفك إلى S(-1 - (-0.6)) = -0.4S. عندها تحمل كل خلية محشوة الخطأ نفسه -0.4S. هذا الخطأ منهجي (systematic) وليس عشوائيًا، لذلك لا يُلغى بالمتوسط. بل يتراكم في كل نافذة التفاف تلامس الحافة، فينحاز خرج الطبقة. كما أن نقطة الصفر الكسرية تتطلب عملية طرح عشرية داخل خط المعالجة الصحيح (integer pipeline)، وهذا ما يجتهد الجزء 3 في تجنّبه.

Try the explorer below. Drag the ends of the shaded range and watch S and Z update, with the teal arrow always running from code Z to real 0. The small ticks above the real axis are the representable values S(q - Z). One of them always sits exactly on zero, while the range ends drift slightly once Z is rounded. The presets load the slide example and both drills below.

جرّب المستكشف أدناه. اسحب طرفي المدى المظلل وراقب S وZ وهما يتحدّثان، مع السهم الأخضر المزرق الذي يمتد دائمًا من الرمز Z إلى الصفر الحقيقي 0. العلامات الصغيرة فوق المحور الحقيقي هي القيم القابلة للتمثيل S(q - Z). إحداها تقع دائمًا على الصفر تمامًا، بينما ينزاح طرفا المدى قليلًا بعد تقريب Z. الإعدادات الجاهزة تحمّل مثال الشرائح والتمرينين أدناه.

ExplorerمستكشفDrag r_min and r_max, then read off S and Zاسحب r_min وr_max، ثم اقرأ S وZ
r-4-3-2-101234567-1.082.12-2-101Z = -1q
scale S1.0667(2.12 - (-1.08)) / (1 - (-2))
zero point Z-1round(-0.9875)
integer range[-2, 1]4 codes
representable reals[-1.067, 2.133]S(q_min - Z) to S(q_max - Z)
code q for r1clamp(round(2.09 / 1.0667) + -1)
reconstructed r2.1333error -0.0433, half step 0.5333

Exam drillsتمارين الامتحان

Worked exampleمثال محلول

Drill 1: ReLU6 activations in [0, 6], 8-bit asymmetricالتمرين 1: تفعيلات ReLU6 في [0, 6]، تكميم غير متماثل بـ 8 بتات

  1. Integer rangeمدى الأعداد الصحيحة

    N = 8: q_min = -128, q_max = 127, so q_max - q_min = 255.
    N = 8: q_min = -128 وq_max = 127، إذن q_max - q_min = 255.
  2. Scaleالمقياس

    S = (6 - 0) / 255 = 0.02353.
    S = (6 - 0) / 255 = 0.02353.
  3. Zero pointنقطة الصفر

    Z = round(-128 - 0 / 0.02353) = round(-128) = -128.
    Z = round(-128 - 0 / 0.02353) = round(-128) = -128.
  4. Resultالنتيجة

    S = 0.02353, Z = -128. Real 0 is code -128 and real 6 is code 127. No codes are wasted on negative values that ReLU6 can never produce.
    S = 0.02353 وZ = -128. الصفر الحقيقي 0 هو الرمز -128، والقيمة الحقيقية 6 هي الرمز 127. لا يُهدر أي رمز على قيم سالبة لا يمكن أن تنتجها ReLU6 أصلًا.

Worked exampleمثال محلول

Drill 2: r in [-1, 3], N = 4, then quantize r = 1.0التمرين 2: r في [-1, 3] و N = 4، ثم كمّم r = 1.0

  1. Integer rangeمدى الأعداد الصحيحة

    N = 4: q_min = -8, q_max = 7, so q_max - q_min = 15.
    N = 4: q_min = -8 وq_max = 7، إذن q_max - q_min = 15.
  2. Scaleالمقياس

    S = (3 - (-1)) / 15 = 4 / 15 = 0.2667.
    S = (3 - (-1)) / 15 = 4 / 15 = 0.2667.
  3. Zero pointنقطة الصفر

    r_min / S = -1 / 0.2667 = -3.75, so Z = round(-8 + 3.75) = round(-4.25) = -4.
    r_min / S = -1 / 0.2667 = -3.75، إذن Z = round(-8 + 3.75) = round(-4.25) = -4.
  4. Quantize r = 1.0كمّم r = 1.0

    q = round(1.0 / 0.2667) + Z = round(3.75) + (-4) = 4 - 4 = 0.
    q = round(1.0 / 0.2667) + Z = round(3.75) + (-4) = 4 - 4 = 0.
  5. Reconstructأعد البناء

    r = 0.2667 x (0 - (-4)) = 1.067. The error 0.067 is below S/2 = 0.133.
    r = 0.2667 x (0 - (-4)) = 1.067. الخطأ 0.067 أقل من S/2 = 0.133.
  6. Resultالنتيجة

    S = 0.2667, Z = -4, q = 0, r = 1.067. Note the trap: the code 0 here means real 1.067, not real zero. Real zero is code -4.
    S = 0.2667 وZ = -4 وq = 0 وr = 1.067. انتبه للفخ: الرمز 0 هنا يعني القيمة الحقيقية 1.067 وليس الصفر الحقيقي. الصفر الحقيقي هو الرمز -4.

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

For r in [-0.5, 1.5] with N = 2, what is the zero point Z?للمدى r في [-0.5, 1.5] مع N = 2، ما قيمة نقطة الصفر Z؟

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

A tool stores Z = -0.6 as a float. What breaks when the layer zero-pads?أداة تخزّن Z = -0.6 كعدد عشري. ما الذي يتعطل عندما تحشو الطبقة بالأصفار (zero padding)؟

Recall

Compute S and Z for r in [-1.08, 2.12] with 2 bits, showing each line.احسب S و Z للمدى r في [-1.08, 2.12] مع بتين، واكتب كل سطر.

S = (2.12 - (-1.08)) / (1 - (-2)) = 3.20 / 3 = 1.0667 (slides: 1.07). Z = round(-2 - (-1.08 / 1.07)) = round(-2 + 1.009) = round(-0.991) = -1.
S = (2.12 - (-1.08)) / (1 - (-2)) = 3.20 / 3 = 1.0667 (في الشرائح: 1.07). Z = round(-2 - (-1.08 / 1.07)) = round(-2 + 1.009) = round(-0.991) = -1.

Recall

Give the 8-bit asymmetric S and Z for ReLU6 activations in [0, 6].أعطِ قيمتي S و Z للتكميم غير المتماثل بـ 8 بتات لتفعيلات ReLU6 في [0, 6].

S = 6 / 255 = 0.02353 and Z = round(-128 - 0 / S) = -128.
S = 6 / 255 = 0.02353 وZ = round(-128 - 0 / S) = -128.

Recall

Why must Z be an integer? Name three places real zero shows up.لماذا يجب أن يكون Z عددًا صحيحًا؟ اذكر ثلاثة مواضع يظهر فيها الصفر الحقيقي.

So that r = 0 is exactly representable: q = Z decodes to S(Z - Z) = 0. Zero padding, ReLU outputs and pruned weights. A fractional Z would put the same error on every one of them, a bias rather than noise.
حتى يكون r = 0 قابلًا للتمثيل بدقة: q = Z يُفك إلى S(Z - Z) = 0. المواضع الثلاثة: الحشو بالأصفار، ومخرجات ReLU، والأوزان المقلّمة. وZ الكسري سيضع الخطأ نفسه على كل واحد منها، فيصبح انحيازًا وليس ضجيجًا.

Recapالخلاصة

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

  • Linear quantization maps an integer code back to a real number with r = S(q - Z).التكميم الخطي (linear quantization) يعيد الرمز الصحيح q إلى عدد حقيقي بالعلاقة r = S(q - Z).
  • r and S are floating point. q and Z are integers of the same type, for example 2-bit or 8-bit signed.r و S أعداد عشرية (floating point). أما q و Z فهما عددان صحيحان من النوع نفسه، مثل عدد صحيح بإشارة من 2 بت أو 8 بت.
  • Signed N-bit codes run from q_min = -2^(N-1) to q_max = 2^(N-1) - 1: [-2, 1], [-4, 3], [-8, 7], [-128, 127].الرموز ذات الإشارة بعدد N من البتات تمتد من q_min = -2^(N-1) إلى q_max = 2^(N-1) - 1: أي [-2, 1] و [-4, 3] و [-8, 7] و [-128, 127].
  • Pin the range ends: r_max = S(q_max - Z) and r_min = S(q_min - Z). Subtracting cancels Z and gives S = (r_max - r_min) / (q_max - q_min).ثبّت طرفي المدى: r_max = S(q_max - Z) و r_min = S(q_min - Z). الطرح يلغي Z ويعطي S = (r_max - r_min) / (q_max - q_min).
  • Substitute S back into the r_min equation: Z = q_min - r_min / S, then round so Z is an integer.عوّض S في معادلة r_min فتحصل على Z = q_min - r_min / S، ثم قرّب الناتج حتى يصبح Z عددًا صحيحًا.
  • Running example: S = 3.20 / 3 = 1.0667 (slides write 1.07) and Z = round(-0.991) = -1.المثال المستمر: S = 3.20 / 3 = 1.0667 (الشرائح تكتب 1.07) و Z = round(-0.991) = -1.
  • Z is the code for real 0, so 0 is stored exactly. Zero padding, ReLU zeros and pruned weights stay exact. A fractional Z would bias every padded cell.Z هو الرمز الذي يمثل الصفر الحقيقي، لذلك يُخزَّن الصفر بدقة تامة. الحشو بالأصفار (zero padding) وأصفار ReLU والأوزان المقلّمة (pruned) تبقى صفرًا تمامًا. لو كان Z كسريًا لانحازت كل خلية محشوة.

Sourcesالمصادر