Lecture 05-1 · Quantization Iالمحاضرة 05-1 · التكميم I

Why low bits: energy, integers, fixed point

لماذا البتات القليلة: الطاقة، والأعداد الصحيحة، والفاصلة الثابتة

Slides 1–6الشرائح 1–6 P1 MUST

Fewer bits per number means cheaper arithmetic and less data to move. Before we can shrink numbers we need to know exactly how integers and fixed-point numbers are stored, and what range each encoding covers.

عدد أقل من البتات لكل رقم يعني عمليات حسابية أرخص وبيانات أقل تُنقل. وقبل أن نصغّر الأرقام يجب أن نعرف بالضبط كيف تُخزَّن الأعداد الصحيحة (integers) وأعداد النقطة الثابتة (fixed-point)، وما المدى (range) الذي يغطيه كل ترميز.

Less bit-width, less energy

عرض بتات أقل، طاقة أقل

Slide 3 reuses Horowitz's 45 nm, 0.9 V energy table (ISSCC 2014). Every row is one arithmetic operation, and the cost grows with both bit-width and with "float vs int". The headline is the bottom line of the slide: one DRAM access costs 200x+ more than these operations, so shrinking the bits also shrinks the far larger memory bill.

تعيد الشريحة 3 استخدام جدول الطاقة لـ Horowitz عند 45 nm و0.9 V (ISSCC 2014). كل سطر عملية حسابية واحدة، والتكلفة تزيد مع عرض البتات (bit-width) ومع كون العدد float بدل int. والرسالة الأهم في أسفل الشريحة: وصول واحد إلى DRAM يكلّف 200x+ أكثر من هذه العمليات، فتصغير البتات يصغّر أيضاً فاتورة الذاكرة الأكبر بكثير.

OperationالعمليةEnergy (pJ)الطاقة (pJ)OperationالعمليةEnergy (pJ)الطاقة (pJ)
8-bit int ADDجمع 8-bit int0.038-bit int MULTضرب 8-bit int0.2
32-bit int ADDجمع 32-bit int0.132-bit int MULTضرب 32-bit int3.1
16-bit float ADDجمع 16-bit float0.416-bit float MULTضرب 16-bit float1.1
32-bit float ADDجمع 32-bit float0.932-bit float MULTضرب 32-bit float3.7

الجدول يضع الجمع (ADD) على اليسار والضرب (MULT) على اليمين. لاحظ أن الضرب أغلى من الجمع دائماً، وأن 8-bit int هو الأرخص في العمودين، وأن 32-bit float MULT بقيمة 3.7 pJ هو الأغلى.

0.01 0.1 1 10 pJ 8-bit int ADD 32-bit int ADD 16-bit float ADD 32-bit float ADD 8-bit int MULT 32-bit int MULT 16-bit float MULT 32-bit float MULT 0.03 0.1 0.4 0.9 0.2 3.1 1.1 3.7 30x 16x
Slide 3 on a log axis. 30x = 0.9 / 0.03 (fp32 add vs int8 add). 16x ≈ 3.1 / 0.2 = 15.5 (int32 mult vs int8 mult).الشريحة 3 على محور لوغاريتمي. 30x = 0.9 / 0.03 (جمع fp32 مقابل جمع int8). 16x ≈ 3.1 / 0.2 = 15.5 (ضرب int32 مقابل ضرب int8).

The two arrows on the slide are ratios you should be able to recompute. Going from a 32-bit float add (0.9 pJ) to an 8-bit int add (0.03 pJ) is 0.9 / 0.03 = 30x. Going from a 32-bit int multiply (3.1 pJ) to an 8-bit int multiply (0.2 pJ) is 3.1 / 0.2 = 15.5 ≈ 16x. A neural network is dominated by multiply-accumulate, so both the adder and the multiplier get cheaper when weights and activations move to 8-bit integers.

السهمان في الشريحة نسبتان يجب أن تعرف حسابهما. الانتقال من جمع 32-bit float (0.9 pJ) إلى جمع 8-bit int (0.03 pJ) يعطي 0.9 / 0.03 = 30x. والانتقال من ضرب 32-bit int (3.1 pJ) إلى ضرب 8-bit int (0.2 pJ) يعطي 3.1 / 0.2 = 15.5 ≈ 16x. الشبكة العصبية يغلب عليها multiply-accumulate، لذلك يرخص الجامع والضارب معاً عندما تنتقل الأوزان والتفعيلات إلى أعداد صحيحة 8-bit.

Integers: three encodings of the same 8 bits

الأعداد الصحيحة: ثلاثة ترميزات للبتات الثمانية نفسها

Slide 5 shows three ways to read an n-bit word. The bits are the same; only the meaning of the top bit changes.

تعرض الشريحة 5 ثلاث طرق لقراءة كلمة من n بت. البتات نفسها، ويتغير فقط معنى البت الأعلى.

Unsigned

بلا إشارة (Unsigned)

Every bit has weight 2i. Range [0, 2n − 1]. For n = 8: 0 to 255.

لكل بت وزن 2^i. المدى [0, 2^n − 1]. عند n = 8: من 0 إلى 255.

Sign-magnitude

الإشارة والمقدار (Sign-magnitude)

Top bit is a sign flag, the other n − 1 bits are the magnitude. Range [−(2n−1 − 1), 2n−1 − 1]. For n = 8: −127 to 127. Both 000...0 and 100...0 mean 0.

البت الأعلى علامة الإشارة، والبتات الباقية n − 1 هي المقدار. المدى [−(2^(n−1) − 1), 2^(n−1) − 1]. عند n = 8: من −127 إلى 127. كل من 000...0 و100...0 يعني صفراً.

Two's complement

المتمم الثنائي (Two's complement)

Top bit has weight −2n−1, the rest are positive. Range [−2n−1, 2n−1 − 1]. For n = 8: −128 to 127. One zero; 100...0 = −2n−1.

البت الأعلى وزنه −2^(n−1) والباقي موجبة. المدى [−2^(n−1), 2^(n−1) − 1]. عند n = 8: من −128 إلى 127. صفر واحد فقط، و100...0 = −2^(n−1).

b7b6b5b4 b3b2b1b0 Unsigned Sign-magnitude Two's complement 0011 0001 1011 0001 1100 1111 32 + 16 + 1 = 49 sign = −, magnitude 32 + 16 + 1 → −49 −128 + 64 + 8 + 4 + 2 + 1 = −49 top bit: +128 top bit: sign flag top bit: −128
Slide 5's three words. Only the role of b7 changes between encodings.الكلمات الثلاث في الشريحة 5. الذي يتغير بين الترميزات هو دور b7 فقط.
Worked exampleمثال محلول

Encode and decode the slide's ±49 in all three schemes.

رمّز وفك رموز ±49 من الشريحة في الأنظمة الثلاثة.

49 = 32 + 16 + 1 = 0011 0001   (unsigned)

Sign-magnitude −49: flip only the sign bit
  1011 0001 = −(32 + 16 + 1) = −49

Two's complement −49: invert all bits, add 1
  0011 0001 → 1100 1110 → 1100 1111
  check: −128 + 64 + 8 + 4 + 2 + 1 = −128 + 79 = −49

Ranges for n = 8
  unsigned          0 .. 255        (256 values)
  sign-magnitude −127 .. 127        (255 values, two zeros)
  two's complement −128 .. 127      (256 values, one zero)

Two's complement wins in hardware: one zero, all 256 patterns used, and the same adder works for signed and unsigned numbers. This is the INT8 that quantized networks use.

يفوز two's complement في العتاد: صفر واحد، وكل الأنماط الـ 256 مستخدمة، ونفس الجامع (adder) يعمل للأعداد ذات الإشارة وبدونها. وهذا هو INT8 الذي تستخدمه الشبكات المكمّاة.

Fixed point: an integer with an implied scale

الفاصلة الثابتة: عدد صحيح بمقياس ضمني

Slide 6 splits 8 bits into a sign bit, 3 integer bits and 4 fraction bits, with an imaginary binary point between them. In two's complement the weights are −23, 22, 21, 20, 2−1, 2−2, 2−3, 2−4.

تقسم الشريحة 6 البتات الثماني إلى بت إشارة و3 بتات للجزء الصحيح و4 بتات للكسر، مع نقطة ثنائية وهمية بينهما. في two's complement تكون الأوزان −2^3, 2^2, 2^1, 2^0, 2^−1, 2^−2, 2^−3, 2^−4.

The slide's second line is the key idea for the rest of the lecture: the same bits read as a plain integer give 49, and the fixed-point value is just that integer times a fixed scale, 49 × 2−4 = 3.0625. Fixed point is "integer arithmetic plus a constant scale factor". Linear quantization later generalises this scale from a power of two to any real number S.

السطر الثاني في الشريحة هو الفكرة المفتاحية لبقية المحاضرة: نفس البتات إذا قُرئت كعدد صحيح عادي تعطي 49، وقيمة fixed-point هي هذا العدد مضروباً في مقياس ثابت، 49 × 2^−4 = 3.0625. أي أن fixed point هو "حساب أعداد صحيحة مع معامل مقياس ثابت". وسيعمّم linear quantization لاحقاً هذا المقياس من قوة للعدد 2 إلى أي عدد حقيقي S.

0011.00012 = 2 + 1 + 2−4 = 3.0625 = 49 × 2−4

Memory trickحيلة للحفظ

"Fixed point = int × 2−f". Read the bits as an integer, then shift the point left by the number of fraction bits f.

"Fixed point = int × 2^−f". اقرأ البتات كعدد صحيح، ثم أزح النقطة يساراً بعدد بتات الكسر f.

With 4 fraction bits the step is 2−4 = 0.0625 and the range of this 8-bit format is −8 to 7.9375 (that is −128 × 0.0625 to 127 × 0.0625).

مع 4 بتات كسر تكون الخطوة 2^−4 = 0.0625، ويكون مدى هذا الشكل ذي 8 بتات من −8 إلى 7.9375 (أي من −128 × 0.0625 إلى 127 × 0.0625).

Exam trapفخ في الاختبار

The 16x is int32 mult vs int8 mult, not fp32 vs int8. 3.1 / 0.2 = 15.5 ≈ 16. If you divide fp32 mult by int8 mult you get 3.7 / 0.2 = 18.5, which is not on the slide. The 30x is the add pair: 0.9 / 0.03.

الرقم 16x هو ضرب int32 مقابل ضرب int8، وليس fp32 مقابل int8. 3.1 / 0.2 = 15.5 ≈ 16. إذا قسمت ضرب fp32 على ضرب int8 تحصل على 3.7 / 0.2 = 18.5، وهذا غير موجود في الشريحة. أما 30x فهو زوج الجمع: 0.9 / 0.03.

Two's complement is asymmetric. 8-bit covers −128 to 127, not −127 to 127 and not −128 to 128. The extra negative value is 1000 0000 = −128. Sign-magnitude is the symmetric one, paid for with a wasted second zero.

Two's complement غير متماثل. الـ 8-bit يغطي من −128 إلى 127، وليس من −127 إلى 127 ولا من −128 إلى 128. القيمة السالبة الإضافية هي 1000 0000 = −128. أما sign-magnitude فهو المتماثل، وثمنه صفر ثانٍ مُهدَر.

Same bits, different value. 1011 0001 is −49 in sign-magnitude but −79 in two's complement (−128 + 32 + 16 + 1). Always check which encoding a question states.

نفس البتات، قيمة مختلفة. 1011 0001 يساوي −49 في sign-magnitude لكنه −79 في two's complement (−128 + 32 + 16 + 1). تحقق دائماً من الترميز المذكور في السؤال.

Check yourself

اختبر نفسك

Using slide 3's table, how many times cheaper is an 8-bit int ADD than a 32-bit float ADD?

باستخدام جدول الشريحة 3، كم مرة يكون جمع 8-bit int أرخص من جمع 32-bit float؟

B. 0.9 / 0.03 = 30. A is 0.1 / 0.03 (int32 add vs int8 add). C is the multiply arrow, 3.1 / 0.2. D is 3.7 / 0.03, which mixes a multiply with an add.

الجواب B. 0.9 / 0.03 = 30. والخيار A هو 0.1 / 0.03 (جمع int32 مقابل جمع int8). وC هو سهم الضرب، 3.1 / 0.2. وD هو 3.7 / 0.03، وهو يخلط عملية ضرب مع عملية جمع.

What is the range of an 8-bit two's complement integer?

ما مدى عدد صحيح 8-bit بتمثيل two's complement؟

C. [−2n−1, 2n−1 − 1] = [−128, 127]. A is sign-magnitude. B would need 257 patterns. D is unsigned.

الجواب C. [−2n−1, 2n−1 − 1] = [−128, 127]. والخيار A هو مدى sign-magnitude. وB يحتاج 257 نمطاً. وD هو مدى unsigned.

What decimal value does 1100 1111 represent in 8-bit two's complement?

ما القيمة العشرية التي يمثلها 1100 1111 في two's complement ذي 8 بتات؟

A. −128 + 64 + 8 + 4 + 2 + 1 = −49. B reads the word as unsigned. C reads it as sign-magnitude (−(64 + 15)). D has no meaning in any of the three schemes.

الجواب A. −128 + 64 + 8 + 4 + 2 + 1 = −49. والخيار B يقرأ الكلمة كعدد unsigned. وC يقرؤها كـ sign-magnitude (−(64 + 15)). وD لا معنى له في أي من الأنظمة الثلاثة.

An 8-bit fixed-point number uses slide 6's layout (sign, 3 integer bits, 4 fraction bits, two's complement). What is 0101 1000?

عدد fixed-point ذو 8 بتات يستخدم تقسيم الشريحة 6 (إشارة، 3 بتات صحيحة، 4 بتات كسر، two's complement). ما قيمة 0101 1000؟

D. As an integer the word is 64 + 16 + 8 = 88, and 88 × 2−4 = 5.5. Equivalently 4 + 1 + 2−1 = 5.5. A forgets the scale. B reads the fraction nibble as a decimal digit. C shifts by 3 instead of 4.

الجواب D. الكلمة كعدد صحيح هي 64 + 16 + 8 = 88، و88 × 2−4 = 5.5. وبطريقة مكافئة 4 + 1 + 2−1 = 5.5. والخيار A ينسى المقياس. وB يقرأ نصف بايت الكسر (nibble) كرقم عشري. وC يزيح بـ 3 بدلاً من 4.

Which integer encoding has two bit patterns for zero?

أي ترميز للأعداد الصحيحة لديه نمطان من البتات للصفر؟

B. 000...0 is +0 and 100...0 is −0. In two's complement 100...0 is −2n−1, so there is only one zero, and fixed point built on two's complement inherits that. Unsigned has no sign at all.

الجواب B. 000...0 هو +0 و100...0 هو −0. أما في two's complement فالنمط 100...0 هو −2n−1، فلا يوجد إلا صفر واحد، والفاصلة الثابتة المبنية على two's complement ترث ذلك. وunsigned لا إشارة فيه أصلاً.