Majid Al-RaimiWhy fewer bits, integers and fixed pointلماذا بتات أقل، والأعداد الصحيحة والفاصلة الثابتة

COE 592Lecture 5.1المحاضرة 5.1Part 01الجزء 01

Why fewer bits, integers and fixed pointلماذا بتات أقل، والأعداد الصحيحة والفاصلة الثابتة

Lower bit-width means cheaper arithmetic and memory traffic. Covers unsigned, sign-magnitude and two's complement integers, then fixed-point numbers as an integer times a power-of-two scale.عرض بت أقل يعني حسابًا وحركة ذاكرة أرخص. يغطي الأعداد الصحيحة بلا إشارة، وبالإشارة والمقدار (sign-magnitude)، والمتمم الثنائي (two's complement)، ثم أعداد الفاصلة الثابتة بوصفها عددًا صحيحًا مضروبًا في مقياس هو قوة للعدد 2.

Conceptsالمفاهيم
3
Slidesالشرائح
1-6
Readingمدة القراءة
18 min18 دقيقة
Understoodما فهمته
0/3 conceptsمفاهيم

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

Lecture 05-1 opens with a plan in three steps. First, review how computers store numbers: integers, fixed point and floating point (FP32, FP16, INT4 and friends). Second, learn what neural network Quantization is. Third, meet the common ways to do it: K-means-based, linear, and binary or ternary quantization. The outline slide makes the point with a picture: a smooth sine wave sampled at points that snap to a few dashed levels. Snapping to a small set of levels is the whole subject.

This part covers the first step and the reason behind it. We start with the energy bill, because it explains why anyone would give up precision on purpose. Then we ask the slide's simple question, how is numeric data represented, and answer it for integers. Finally, we read fixed point as an integer with a hidden scale. That reading is the bridge to linear quantization, r=S(q−Z)r = S(q - Z), in lecture 05-2. For the midterm, expect range formulas for n bits, hand encodings such as -49, a fixed-point decode, and ratios read off the energy table.

تبدأ المحاضرة 05-1 بخطة من ثلاث خطوات. أولاً، مراجعة طريقة تخزين الحاسوب للأعداد: الأعداد الصحيحة (integers) والـ fixed point والـ floating point (FP32 وFP16 وINT4 وأخواتها). ثانياً، فهم ما هو quantization في الشبكات العصبية. ثالثاً، التعرف على الطرق الشائعة لتنفيذه: الطريقة القائمة على K-means، والطريقة الخطية (linear)، والـ quantization الثنائي أو الثلاثي (binary أو ternary). شريحة المخطط العام توضح الفكرة بصورة: موجة جيبية ناعمة نأخذ منها عينات عند نقاط تُجبر على الالتصاق بعدد قليل من المستويات المتقطعة. هذا الالتصاق بمجموعة صغيرة من المستويات هو الموضوع كله.

يغطي هذا الجزء الخطوة الأولى والسبب وراءها. نبدأ بفاتورة الطاقة، لأنها تشرح لماذا قد يتنازل أحد عن الدقة عن قصد. ثم نطرح سؤال الشريحة البسيط: كيف تُمثَّل البيانات العددية؟ ونجيب عنه للأعداد الصحيحة. وأخيراً نقرأ الـ fixed point على أنه عدد صحيح بمقياس (scale) مخفي. هذه القراءة هي الجسر إلى الـ linear quantization، أي r=S(q−Z)r = S(q - Z)، في المحاضرة 05-2. في الاختبار النصفي توقع صيغ النطاق لعدد n من البتات، وترميزات يدوية مثل -49، وفك ترميز عدد fixed-point، ونسباً تُقرأ من جدول الطاقة.

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

  1. Quote the 45 nm energy table and compute the 30x add ratio and the roughly 16x multiply ratio between 32-bit and 8-bit operations.تستشهد بجدول الطاقة لتقنية 45 nm وتحسب نسبة 30x للجمع ونسبة 16x تقريباً للضرب بين عمليات 32-bit وعمليات 8-bit.
  2. Explain why multiplier energy grows roughly with the square of the bit-width, adder energy roughly linearly, and why a DRAM access dwarfs both.تشرح لماذا تنمو طاقة الضارب (multiplier) تقريباً مع مربع عدد البتات (bit-width)، وطاقة الجامع (adder) خطياً تقريباً، ولماذا تطغى قراءة واحدة من DRAM على الاثنين.
  3. Write the n-bit range of unsigned, sign-magnitude and two's complement integers, and evaluate it for n = 8 and n = 4.تكتب نطاق الأعداد الصحيحة ذات n بت بصيغ unsigned و sign-magnitude و two's complement، وتحسبه عند n = 8 و n = 4.
  4. Encode and decode any 8-bit value in all three integer formats, including -49 by hand.ترمّز وتفك ترميز أي قيمة من 8 بتات بصيغ الأعداد الصحيحة الثلاث، بما في ذلك -49 يدوياً.
  5. Read a fixed-point number both as a sum of fractional powers of two and as an integer times 2^-4, and state its resolution and range.تقرأ عدد fixed-point بطريقتين: كمجموع لقوى اثنين الكسرية، وكعدد صحيح مضروب في 2^-4، وتذكر دقته (resolution) ونطاقه (range).

Take one multiply-accumulate, the operation a neural network repeats billions of times. In 32-bit floating point it costs a multiply (3.7 pJ) plus an add (0.9 pJ), so 4.6 pJ. In 8-bit integers it costs 0.2 pJ plus 0.03 pJ, so 0.23 pJ. The same arithmetic job, done with fewer bits, is about 20x cheaper. That one number is why this lecture exists.

خذ عملية multiply-accumulate واحدة، وهي العملية التي تكررها الشبكة العصبية مليارات المرات. في الـ floating point بحجم 32-bit تكلف عملية ضرب (3.7 pJ) زائد عملية جمع (0.9 pJ)، أي 4.6 pJ. وفي الأعداد الصحيحة بحجم 8-bit تكلف 0.2 pJ زائد 0.03 pJ، أي 0.23 pJ. المهمة الحسابية نفسها، حين تُنفذ ببتات أقل، أرخص بنحو 20x. هذا الرقم وحده هو سبب وجود هذه المحاضرة.

The table every efficiency lecture quotesالجدول الذي تستشهد به كل محاضرة عن الكفاءة

The figures come from Mark Horowitz's ISSCC 2014 keynote, measured for a 45 nm process at 0.9 V. They are rough by design: the exact numbers change with the process node, but the ratios between them have held for a decade. The slide lists four precisions for each of two operations.

تأتي هذه الأرقام من الكلمة الرئيسية لـ Mark Horowitz في مؤتمر ISSCC 2014، وقد قيست لتقنية تصنيع 45 nm عند جهد 0.9 V. وهي تقريبية عن قصد: الأرقام الدقيقة تتغير مع عقدة التصنيع (process node)، لكن النسب بينها ظلت ثابتة لعقد كامل. تعرض الشريحة أربع دقّات (precisions) لكل عملية من العمليتين.

Operationالعملية8b int8b int32b int32b int16b float16b float32b float32b floatfp32 / int8fp32 / int8
ADDجمع (ADD)0.030.030.10.10.40.40.90.90.9 / 0.03 = 30x0.9 / 0.03 = 30x
MULTضرب (MULT)0.20.23.13.11.11.13.73.73.7 / 0.2 = 18.5x3.7 / 0.2 = 18.5x
Energy per operation in pJ, 45 nm, 0.9 V (Horowitz, ISSCC 2014). Last column: 32-bit float over 8-bit int.الطاقة لكل عملية بوحدة pJ، بتقنية 45 nm وجهد 0.9 V (Horowitz، ISSCC 2014). العمود الأخير: 32-bit float مقسوماً على 8-bit int.
The eight operations plus one DRAM read on a log axis: 8-bit integer rows in teal, with the 30x add ratio and the 16x multiply ratio drawn as arrowsالعمليات الثماني مع قراءة واحدة من DRAM على محور لوغاريتمي: صفوف الأعداد الصحيحة 8-bit باللون الأزرق المخضر، مع نسبة 30x للجمع ونسبة 16x للضرب مرسومتين كسهمين

Reading the ratiosقراءة النسب

Worked exampleمثال محلول

The two arrows on slide 3السهمان في الشريحة 3

  1. Adds: 32-bit float against 8-bit intالجمع: 32-bit float مقابل 8-bit int

    0.9 pJ0.03 pJ=30\dfrac{0.9\ \text{pJ}}{0.03\ \text{pJ}} = 30
    This is the 30x arrow on the slide.
    0.9 pJ0.03 pJ=30\dfrac{0.9\ \text{pJ}}{0.03\ \text{pJ}} = 30
    هذا هو سهم 30x في الشريحة.
  2. Multiplies: 32-bit int against 8-bit intالضرب: 32-bit int مقابل 8-bit int

    3.1 pJ0.2 pJ=15.5≈16\dfrac{3.1\ \text{pJ}}{0.2\ \text{pJ}} = 15.5 \approx 16
    This matches the 16x label.
    3.1 pJ0.2 pJ=15.5≈16\dfrac{3.1\ \text{pJ}}{0.2\ \text{pJ}} = 15.5 \approx 16
    هذا يطابق التسمية 16x.
  3. Multiplies: 32-bit float against 8-bit intالضرب: 32-bit float مقابل 8-bit int

    3.7 pJ0.2 pJ=18.5\dfrac{3.7\ \text{pJ}}{0.2\ \text{pJ}} = 18.5
    This is the ratio you get if you read the arrow from the longest bar, which is where the slide's dotted line sits.
    3.7 pJ0.2 pJ=18.5\dfrac{3.7\ \text{pJ}}{0.2\ \text{pJ}} = 18.5
    هذه هي النسبة التي تحصل عليها إذا قرأت السهم من أطول عمود، وهو المكان الذي يقع فيه الخط المنقط في الشريحة.
  4. Resultالنتيجة

    Moving from 32 to 8 bits makes an add about 30x cheaper (float to int) and a multiply about 16x to 18.5x cheaper, depending on whether you start from int32 or fp32.
    الانتقال من 32 إلى 8 بتات يجعل الجمع أرخص بنحو 30x (من float إلى int)، ويجعل الضرب أرخص بما بين 16x و18.5x تقريباً، بحسب ما إذا كانت نقطة البداية int32 أو fp32.

Why multiplies punish bits more than addsلماذا يعاقب الضرب على البتات أكثر من الجمع

Look at integers only, so the format does not change. Going from 8 to 32 bits is 4x the Bit-width. The add grows from 0.03 to 0.1 pJ, about 3.3x: roughly linear. The multiply grows from 0.2 to 3.1 pJ, about 15.5x, which is close to 4² = 16: roughly quadratic.

انظر إلى الأعداد الصحيحة فقط حتى لا تتغير الصيغة. الانتقال من 8 إلى 32 بتاً يعني 4x في bit-width. يزداد الجمع من 0.03 إلى 0.1 pJ، أي نحو 3.3x: نمو خطي تقريباً. ويزداد الضرب من 0.2 إلى 3.1 pJ، أي نحو 15.5x، وهذا قريب من 4² = 16: نمو تربيعي تقريباً.

The hardware explains the pattern. An n-bit ripple adder is a chain of n full adders, one per bit. An n-bit array multiplier forms n partial products of n bits each and adds them, so it needs on the order of n² cells. Real designs are cleverer, but the growth stays close to these orders.

العتاد يفسر هذا النمط. الجامع المتتابع (ripple adder) بعدد n من البتات هو سلسلة من n جامعات كاملة (full adders)، واحد لكل بت. أما الضارب المصفوفي (array multiplier) بعدد n من البتات فيكوّن n من النواتج الجزئية (partial products) طول كل منها n بت ثم يجمعها، لذلك يحتاج إلى عدد من الخلايا من رتبة n². التصاميم الحقيقية أذكى من ذلك، لكن النمو يبقى قريباً من هذه الرتب.

Eadd(n)∝n,Emult(n)∝n2E_{\text{add}}(n) \propto n, \qquad E_{\text{mult}}(n) \propto n^{2}
Rule of thumb for integer arithmetic energyقاعدة تقريبية لطاقة الحساب بالأعداد الصحيحة

Floating point adds overhead on top: aligning exponents, normalizing and rounding. That is why a 32-bit float ADD (0.9 pJ) costs 9x a 32-bit int ADD (0.1 pJ) even though both handle 32 bits. A 16-bit float multiply (1.1 pJ) is still cheaper than a 32-bit integer one (3.1 pJ), because its significand is only 11 bits wide. Parts 2 and 3 unpack these float formats.

الـ floating point يضيف عبئاً إضافياً فوق ذلك: محاذاة الأُسس (exponents)، والتطبيع (normalizing)، والتقريب (rounding). لهذا يكلف 32-bit float ADD (0.9 pJ) تسعة أضعاف (9x) 32-bit int ADD (0.1 pJ) رغم أن كليهما يتعامل مع 32 بتاً. ومع ذلك فإن ضرب 16-bit float (1.1 pJ) أرخص من ضرب عدد صحيح 32-bit (3.1 pJ)، لأن الجزء المعنوي (significand) فيه عرضه 11 بتاً فقط. الجزءان 2 و3 يشرحان صيغ الـ float هذه بالتفصيل.

Memory dominates bothالذاكرة تطغى على الاثنين

The bottom line of slide 3 says one DRAM access costs 200x or more than an arithmetic operation. Han et al. quote the same Horowitz table: about 640 pJ for a 32-bit DRAM read and about 5 pJ for a 32-bit on-chip SRAM read. Then 640 / 3.1 ≈ 206, so one fetch from DRAM costs as much as about 200 32-bit integer multiplies, and over 20,000 8-bit integer adds. Horowitz's own paper puts a DRAM access at 1 to 2 nJ, a couple of orders of magnitude above a functional operation.

يقول السطر الأخير في الشريحة 3 إن قراءة واحدة من DRAM تكلف 200x أو أكثر من عملية حسابية. ويستشهد Han et al. بجدول Horowitz نفسه: نحو 640 pJ لقراءة 32-bit من DRAM، ونحو 5 pJ لقراءة 32-bit من SRAM على الشريحة (on-chip). إذن 640 / 3.1 ≈ 206، أي أن جلبة واحدة من DRAM تكلف ما يعادل نحو 200 عملية ضرب لأعداد صحيحة 32-bit، وأكثر من 20,000 عملية جمع لأعداد صحيحة 8-bit. وورقة Horowitz نفسها تقدّر الوصول إلى DRAM بما بين 1 و2 nJ، أي أعلى من العملية الوظيفية بمرتبتين عشريتين تقريباً.

This is why fewer bits help twice. Each operation gets cheaper, and each weight or activation is smaller, so fewer bytes cross the expensive path from DRAM. An 8-bit weight is a quarter of the traffic of a 32-bit one, and a model that shrinks enough may fit in on-chip SRAM, which avoids DRAM almost completely. You saw the memory side of this argument in lecture 03 and in Pruning (lecture 04-1). Quantization attacks the same bill from the other direction: instead of fewer weights, fewer bits per weight.

لهذا تفيد البتات الأقل مرتين. كل عملية تصبح أرخص، وكل وزن (weight) أو تنشيط (activation) يصبح أصغر، فتعبر بايتات أقل المسار المكلف من DRAM. الوزن بحجم 8-bit يمثل ربع حركة البيانات التي يحتاجها وزن بحجم 32-bit، والنموذج الذي يصغر بما يكفي قد يتسع في SRAM على الشريحة، فيتجنب DRAM بالكامل تقريباً. رأيت جانب الذاكرة من هذه الحجة في المحاضرة 03 وفي pruning (المحاضرة 04-1). الـ quantization يهاجم الفاتورة نفسها من الاتجاه الآخر: بدلاً من أوزان أقل، بتات أقل لكل وزن.

Recall

Without looking: what are the 8-bit int and 32-bit float energies for ADD and MULT, and which two ratios does slide 3 highlight?دون أن تنظر: ما طاقة ADD و MULT لـ 8-bit int و 32-bit float، وما النسبتان اللتان تبرزهما الشريحة 3؟

ADD: 0.03 pJ (int8) and 0.9 pJ (fp32), ratio 30x. MULT: 0.2 pJ (int8) and 3.7 pJ (fp32), ratio 18.5x; the slide labels its multiply arrow 16x, which matches int32 at 3.1 pJ (15.5x).

ADD: 0.03 pJ (int8) و0.9 pJ (fp32)، والنسبة 30x. MULT: 0.2 pJ (int8) و3.7 pJ (fp32)، والنسبة 18.5x؛ والشريحة تسمي سهم الضرب 16x، وهذا يطابق int32 عند 3.1 pJ (15.5x).

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

Going from 8-bit to 32-bit integers, how does multiply energy grow in the 45 nm table?عند الانتقال من أعداد صحيحة 8-bit إلى 32-bit، كيف تنمو طاقة الضرب في جدول 45 nm؟

Write 49 in binary: 32 + 16 + 1, so 00110001 in eight bits. Now try to store -49. There is no minus sign in a register, only bits, so some bit pattern has to mean "negative". The three classic answers to that question are the three integer formats on slide 5, and each one gives the same pattern a different meaning.

اكتب 49 بالنظام الثنائي: 32 + 16 + 1، أي 00110001 في ثمانية بتات. الآن حاول تخزين -49. لا توجد إشارة سالب داخل المسجل (register)، بل بتات فقط، لذلك لا بد أن يعني نمطٌ ما من البتات "سالب". الإجابات الكلاسيكية الثلاث عن هذا السؤال هي صيغ الأعداد الصحيحة الثلاث في الشريحة 5، وكل منها يعطي النمط نفسه معنى مختلفاً.

Unsigned: every bit is a positive power of twoصيغة unsigned: كل بت قوة موجبة للعدد اثنين

In an Unsigned integer, bit i has weight 2i2^{i}, and the value is the sum of the weights of the bits that are set. All zeros gives 0; all ones gives 2n−12^{n}-1. There is no way to write a negative number.

في unsigned integer يكون وزن البت i هو 2i2^{i}، والقيمة هي مجموع أوزان البتات التي قيمتها 1. كل البتات أصفار تعطي 0، وكل البتات آحاد تعطي 2n−12^{n}-1. لا توجد طريقة لكتابة عدد سالب.

vunsigned=∑i=0n−1bi 2i,range [0, 2n−1]v_{\text{unsigned}} = \sum_{i=0}^{n-1} b_i\,2^{i}, \qquad \text{range } [0,\ 2^{n}-1]

Sign-magnitude: a sign flag in front of an unsigned numberصيغة sign-magnitude: علامة إشارة أمام عدد unsigned

Sign-magnitude spends the top bit on the sign and keeps the other n - 1 bits as an unsigned magnitude. So -49 is the sign 1 followed by 0110001: 10110001. It is how people write numbers on paper, which makes it easy to read. It has two flaws. Both 00000000 and 10000000 mean zero (plus zero and minus zero), which wastes a code and complicates equality tests. And adding a positive and a negative number needs a different circuit from adding two positives, because the hardware must compare magnitudes and subtract.

صيغة sign-magnitude تخصص البت الأعلى للإشارة وتُبقي البتات الأخرى وعددها n - 1 كمقدار (magnitude) بلا إشارة. إذن -49 هو بت الإشارة 1 يليه 0110001: 10110001. هكذا يكتب الناس الأعداد على الورق، ولذلك يسهل قراءتها. لكن لها عيبين. النمطان 00000000 و10000000 كلاهما يعني الصفر (صفر موجب وصفر سالب)، وهذا يهدر رمزاً ويعقّد اختبارات التساوي. كما أن جمع عدد موجب وعدد سالب يحتاج إلى دائرة مختلفة عن جمع عددين موجبين، لأن العتاد يجب أن يقارن المقدارين ثم يطرح.

vSM=(−1)bn−1∑i=0n−2bi 2i,range [−(2n−1−1), 2n−1−1]v_{\text{SM}} = (-1)^{b_{n-1}} \sum_{i=0}^{n-2} b_i\,2^{i}, \qquad \text{range } [-(2^{n-1}-1),\ 2^{n-1}-1]

Two's complement: the top bit has a negative weightصيغة two's complement: للبت الأعلى وزن سالب

Two's complement changes one thing: the top bit is no longer a flag but a weight, and that weight is negative, −2n−1-2^{n-1}. All other bits keep their positive weights. For eight bits the weights are -128, 64, 32, 16, 8, 4, 2, 1.

صيغة two's complement تغيّر شيئاً واحداً: البت الأعلى لم يعد علامة بل وزناً، وهذا الوزن سالب، −2n−1-2^{n-1}. وتحتفظ بقية البتات بأوزانها الموجبة. في حالة ثمانية بتات تكون الأوزان -128, 64, 32, 16, 8, 4, 2, 1.

vTC=−bn−1 2n−1+∑i=0n−2bi 2i,range [−2n−1, 2n−1−1]v_{\text{TC}} = -b_{n-1}\,2^{n-1} + \sum_{i=0}^{n-2} b_i\,2^{i}, \qquad \text{range } [-2^{n-1},\ 2^{n-1}-1]

Worked exampleمثال محلول

Encode -49 in 8-bit two's complementرمّز -49 بصيغة two's complement ذات 8 بتات

  1. Write +49اكتب +49

    49 = 32 + 16 + 1, so 00110001.
    49 = 32 + 16 + 1، إذن 00110001.
  2. Invert every bitاقلب كل بت

    11001110
    11001110
  3. Add 1أضف 1

    11001110 + 1 = 11001111
    11001110 + 1 = 11001111
  4. Check with the weightsتحقق باستخدام الأوزان

    -128 + 64 + 8 + 4 + 2 + 1 = -49. A second check: read 11001111 as unsigned, 207, and note 256 - 49 = 207.
    -128 + 64 + 8 + 4 + 2 + 1 = -49. تحقق ثانٍ: اقرأ 11001111 كعدد unsigned فتحصل على 207، ولاحظ أن 256 - 49 = 207.
  5. Resultالنتيجة

    -49 = 11001111 in two's complement, against 10110001 in sign-magnitude. These are the two lower patterns on slide 5.
    -49 = 11001111 بصيغة two's complement، مقابل 10110001 بصيغة sign-magnitude. هذان هما النمطان السفليان في الشريحة 5.

The second check is the deep reason the format works. A negative value -x is stored as the unsigned pattern of 2n−x2^{n}-x, which is arithmetic modulo 2n2^{n}. An ordinary unsigned adder already works modulo 2n2^{n}, since the carry out of the top bit is simply dropped. So the same adder handles signed and unsigned numbers, subtraction is "add the negation", and there is a single zero. Practically every CPU, DSP and neural accelerator uses two's complement for signed integers for this reason.

التحقق الثاني هو السبب العميق لنجاح هذه الصيغة. القيمة السالبة -x تُخزَّن على شكل النمط unsigned للعدد 2n−x2^{n}-x، وهذا حساب بباقي القسمة على 2n2^{n} (modulo). والجامع العادي للأعداد unsigned يعمل أصلاً بباقي القسمة على 2n2^{n}، لأن المحمول (carry) الخارج من البت الأعلى يُهمل ببساطة. لذلك يتعامل الجامع نفسه مع الأعداد ذات الإشارة والأعداد بلا إشارة، ويصبح الطرح "أضف السالب"، ويوجد صفر واحد فقط. لهذا السبب يستخدم عملياً كل CPU وDSP ومسرّع للشبكات العصبية صيغة two's complement للأعداد الصحيحة ذات الإشارة.

The 16 patterns of a 4-bit integer on a wheel: counting up from 0000 reaches 7 at 0111, and one more step wraps to -8 at 1000الأنماط الستة عشر لعدد صحيح من 4 بتات على عجلة: العد تصاعدياً من 0000 يصل إلى 7 عند 0111، وخطوة واحدة إضافية تلتف إلى -8 عند 1000

Ranges for 8 and 4 bitsالنطاقات لـ 8 و4 بتات

Formatالصيغةn-bit rangeالنطاق لعدد n من البتات8 bits8 بتات4 bits4 بتاتZerosالأصفار-49-49
UnsignedUnsigned[0, 2^n - 1][0, 2^n - 1][0, 255][0, 255][0, 15][0, 15]Oneصفر واحدNot representableغير قابل للتمثيل
Sign-magnitudeSign-magnitude[-(2^(n-1) - 1), 2^(n-1) - 1][-(2^(n-1) - 1), 2^(n-1) - 1][-127, 127][-127, 127][-7, 7][-7, 7]Two (000...0 and 100...0)صفران (000...0 و100...0)1011000110110001
Two's complementTwo's complement[-2^(n-1), 2^(n-1) - 1][-2^(n-1), 2^(n-1) - 1][-128, 127][-128, 127][-8, 7][-8, 7]Oneصفر واحد1100111111001111
Integer formats side by side. The last column is how -49 is stored in 8 bits.صيغ الأعداد الصحيحة جنباً إلى جنب. العمود الأخير يبيّن كيف يُخزَّن -49 في 8 بتات.

Count the distinct values. Unsigned and two's complement both use all 2n2^{n} patterns: 256 values for eight bits, 16 for four. Sign-magnitude gets only 2n−12^{n}-1 distinct values, 255 and 15, because two patterns share zero. The pattern 100...0 shows the difference most sharply: minus zero in sign-magnitude, but −2n−1-2^{n-1} in two's complement, which is -128 for 8 bits and -8 for 4.

عُدّ القيم المختلفة. صيغتا unsigned وtwo's complement تستخدمان كل الأنماط وعددها 2n2^{n}: 256 قيمة لثمانية بتات، و16 لأربعة. أما sign-magnitude فلا تحصل إلا على 2n−12^{n}-1 من القيم المختلفة، أي 255 و15، لأن نمطين يتشاركان الصفر. والنمط 100...0 يُظهر الفرق بأوضح صورة: هو صفر سالب في sign-magnitude، لكنه −2n−1-2^{n-1} في two's complement، أي -128 لـ 8 بتات و-8 لـ 4 بتات.

ExplorerمستكشفOne bit pattern, four readingsنمط بتات واحد، وأربع قراءات
weights shown are two's complement; in Q3.4 divide each by 16, so the four bits on the right are the fractionالأوزان المعروضة بصيغة two's complement، وفي Q3.4 تُقسم كل منها على 16، فتكون البتات الأربعة على اليمين هي الكسر
Unsigned49range [0, 255]المدى [0, 255]
Sign-magnitude49range [-127, 127], two zerosالمدى [-127, 127]، وفيه صفران
Two's complement49range [-128, 127]المدى [-128, 127]
Fixed point Q3.43.0625two's complement x 2^-4

Pattern 00110001. The bits never change between the four readings; only the rule for turning them into a number does. Try 10000000: unsigned 128, sign-magnitude -0, two's complement -128, fixed point -8.النمط 00110001. البتات لا تتغير بين القراءات الأربع، وإنما تتغير القاعدة التي تحولها إلى عدد. جرّب 10000000: بلا إشارة 128، وبالإشارة والمقدار -0، وبـ two's complement -128، وبالنقطة الثابتة -8.

Recall

Give the ranges of 4-bit unsigned, sign-magnitude and two's complement integers, and say what 1000 means in each.اذكر نطاقات الأعداد الصحيحة ذات 4 بتات بصيغ unsigned و sign-magnitude و two's complement، وقل ماذا يعني النمط 1000 في كل منها.

Unsigned [0, 15], where 1000 is 8. Sign-magnitude [-7, 7], where 1000 is minus zero. Two's complement [-8, 7], where 1000 is -8.

unsigned: [0, 15]، والنمط 1000 فيها يساوي 8. sign-magnitude: [-7, 7]، والنمط 1000 فيها صفر سالب. two's complement: [-8, 7]، والنمط 1000 فيها يساوي -8.

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

In 8-bit two's complement, which bit pattern encodes -49?في صيغة two's complement ذات 8 بتات، أي نمط بتات يرمّز -49؟

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

What is the range of a 4-bit two's complement integer?ما نطاق عدد صحيح بصيغة two's complement ذات 4 بتات؟

Take the same bits as before, 00110001, and draw a dot between the fourth and fifth bit: 0011.0001. The left part reads 3 and the right part 0001 is one sixteenth, so the number is 3.0625. Nothing about the bits changed. Only our agreement about where the point sits did.

خذ البتات نفسها كما في السابق، 00110001، وارسم نقطة بين البت الرابع والبت الخامس: 0011.0001. الجزء الأيسر يُقرأ 3، والجزء الأيمن 0001 يساوي جزءاً من ستة عشر، إذن العدد هو 3.0625. لم يتغير شيء في البتات. الذي تغير فقط هو اتفاقنا على مكان النقطة.

Reading one: fractional powers of twoالقراءة الأولى: قوى اثنين الكسرية

A Fixed-point number format fixes, once and for all, how many bits sit after the binary point. The slide's format uses eight bits in two's complement: a sign bit, three integer bits and four fraction bits. The weights continue past the point into negative powers of two.

صيغة fixed-point تحدد مرة واحدة وإلى الأبد عدد البتات التي تقع بعد النقطة الثنائية (binary point). الصيغة في الشريحة تستخدم ثمانية بتات بنظام two's complement: بت إشارة، وثلاثة بتات للجزء الصحيح، وأربعة بتات للكسر. وتستمر الأوزان بعد النقطة إلى قوى اثنين السالبة.

0011.0001=−0⋅23+0⋅22+1⋅21+1⋅20+0⋅2−1+0⋅2−2+0⋅2−3+1⋅2−4=3.06250011.0001 = -0\cdot 2^{3} + 0\cdot 2^{2} + 1\cdot 2^{1} + 1\cdot 2^{0} + 0\cdot 2^{-1} + 0\cdot 2^{-2} + 0\cdot 2^{-3} + 1\cdot 2^{-4} = 3.0625

Reading two: an integer times a fixed scaleالقراءة الثانية: عدد صحيح مضروب في مقياس ثابت

Now ignore the point and read the eight bits as the two's complement integer 49. Every weight in reading one is exactly the reading-two weight times 2−42^{-4}: −27⋅2−4=−23-2^{7}\cdot 2^{-4} = -2^{3}, 20⋅2−4=2−42^{0}\cdot 2^{-4} = 2^{-4}, and so on. So the whole value is the integer times one shared scale.

الآن تجاهل النقطة واقرأ البتات الثمانية كعدد صحيح بصيغة two's complement، فتحصل على 49. كل وزن في القراءة الأولى يساوي بالضبط الوزن المقابل في القراءة الثانية مضروباً في 2−42^{-4}: −27⋅2−4=−23-2^{7}\cdot 2^{-4} = -2^{3}، و20⋅2−4=2−42^{0}\cdot 2^{-4} = 2^{-4}، وهكذا. إذن القيمة كلها هي العدد الصحيح مضروباً في مقياس (scale) واحد مشترك.

v=q⋅2−f⇒0011.0001=49×2−4=49×0.0625=3.0625v = q \cdot 2^{-f} \quad\Rightarrow\quad 0011.0001 = 49 \times 2^{-4} = 49 \times 0.0625 = 3.0625
f is the number of fraction bits; q is the stored two's complement integerf عدد بتات الكسر؛ وq العدد الصحيح المخزن بصيغة المتمم الثنائي (two's complement)
The integer 49 and the fixed-point number 3.0625 are the same eight bits; the binary point, and with it the factor 2^-4, lives only in the formatالعدد الصحيح 49 والعدد fixed-point الذي قيمته 3.0625 هما البتات الثمانية نفسها؛ النقطة الثنائية، ومعها العامل 2^-4، موجودة في الصيغة فقط

This second reading is the one to remember. The binary point is never stored. It is a property of the format, which means fixed point is integer arithmetic plus a fixed scale. Adding two numbers in the same format is a plain integer add, because the scale factors out: q12−4+q22−4=(q1+q2) 2−4q_1 2^{-4} + q_2 2^{-4} = (q_1 + q_2)\,2^{-4}. Multiplying is a plain integer multiply whose result has scale 2−82^{-8}; shift right by four bits to return to 2−42^{-4}. A fixed-point unit is just an integer ALU and a shifter, which is why it is cheap.

هذه القراءة الثانية هي التي يجب أن تتذكرها. النقطة الثنائية لا تُخزَّن أبداً. إنها خاصية للصيغة، وهذا يعني أن الـ fixed point هو حساب أعداد صحيحة زائد مقياس ثابت. جمع عددين من الصيغة نفسها هو جمع أعداد صحيحة عادي، لأن المقياس يخرج عاملاً مشتركاً: q12−4+q22−4=(q1+q2) 2−4q_1 2^{-4} + q_2 2^{-4} = (q_1 + q_2)\,2^{-4}. والضرب هو ضرب أعداد صحيحة عادي يكون مقياس ناتجه 2−82^{-8}؛ أزِح الناتج إلى اليمين أربعة بتات لتعود إلى 2−42^{-4}. وحدة الـ fixed-point ليست سوى ALU للأعداد الصحيحة مع مُزيح (shifter)، ولهذا هي رخيصة.

Resolution and rangeالدقة والنطاق

With four fraction bits the gap between neighbouring values, the resolution, is 2^-4 = 0.0625. The range is the two's complement range [-128, 127] times that scale: from -128 / 16 = -8 (pattern 1000.0000) to 127 / 16 = 7.9375 (pattern 0111.1111). Moving the point one place to the left halves both the step and the range; moving it right doubles both. Fixed point can never have fine steps and a wide range at the same time, which is the gap floating point (part 2) was invented to close.

مع أربعة بتات كسرية تكون المسافة بين قيمتين متجاورتين، أي الدقة (resolution)، هي 2^-4 = 0.0625. والنطاق هو نطاق two's complement أي [-128, 127] مضروباً في هذا المقياس: من -128 / 16 = -8 (النمط 1000.0000) إلى 127 / 16 = 7.9375 (النمط 0111.1111). تحريك النقطة خانة واحدة إلى اليسار يُنصّف الخطوة والنطاق معاً، وتحريكها إلى اليمين يضاعفهما معاً. لا يمكن للـ fixed point أبداً أن يجمع بين خطوات دقيقة ونطاق واسع في الوقت نفسه، وهذه هي الفجوة التي اختُرع الـ floating point (الجزء 2) لسدّها.

Worked exampleمثال محلول

Store 2.7 in the same 8-bit formatخزّن 2.7 بالصيغة نفسها ذات 8 بتات

  1. Divide by the scaleاقسم على المقياس

    2.7 / 0.0625 = 43.2
    2.7 / 0.0625 = 43.2
  2. Round to the nearest integerقرّب إلى أقرب عدد صحيح

    q = 43 = 00101011, which reads as 0010.1011.
    q = 43 = 00101011، ويُقرأ على أنه 0010.1011.
  3. Decodeفك الترميز

    43 x 0.0625 = 2.6875
    43 x 0.0625 = 2.6875
  4. Resultالنتيجة

    The stored value is 2.6875, off by 0.0125. The error can never exceed half a step, 0.03125. That round-to-the-grid error is your first taste of Quantization error.
    القيمة المخزنة هي 2.6875، بفارق 0.0125. لا يمكن أن يتجاوز الخطأ نصف خطوة، أي 0.03125. خطأ التقريب إلى الشبكة هذا هو أول لمحة لك عن quantization error.

Hold on to the equation v=q⋅2−4v = q \cdot 2^{-4}. It is linear quantization in disguise: Linear quantization writes r=S(q−Z)r = S(q - Z), a real value rr recovered from a stored integer qq with a scale SS and a zero point ZZ. Fixed point is the special case S=2−4, Z=0S = 2^{-4},\ Z = 0. Lecture 05-2 lets SS be any real number chosen from the data and lets ZZ shift the grid, so the 256 levels of an 8-bit integer land where a tensor's values actually are.

احتفظ بالمعادلة v=q⋅2−4v = q \cdot 2^{-4}. إنها linear quantization متنكّر: linear quantization يكتب r=S(q−Z)r = S(q - Z)، أي قيمة حقيقية rr نستعيدها من عدد صحيح مخزن qq بمقياس SS ونقطة صفر (zero point) ZZ. الـ fixed point هو الحالة الخاصة S=2−4, Z=0S = 2^{-4},\ Z = 0. المحاضرة 05-2 تسمح لـ SS أن يكون أي عدد حقيقي يُختار من البيانات، وتسمح لـ ZZ بإزاحة الشبكة، بحيث تقع المستويات الـ 256 لعدد صحيح 8-bit حيث توجد قيم الـ tensor فعلاً.

  • Resolution with four fraction bits: 0.0625.الدقة مع أربعة بتات كسرية: 0.0625.
  • Range of the 8-bit format: [-8, 7.9375].نطاق الصيغة ذات 8 بتات: [-8, 7.9375].
  • Conversion: q=round(v/2−4)q = \text{round}(v / 2^{-4}), then v≈q⋅2−4v \approx q \cdot 2^{-4}.التحويل: q=round(v/2−4)q = \text{round}(v / 2^{-4})، ثم v≈q⋅2−4v \approx q \cdot 2^{-4}.

Recall

Represent -3.0625 in the same 8-bit fixed-point format (sign, 3 integer bits, 4 fraction bits).مثّل -3.0625 بصيغة الـ fixed-point نفسها ذات 8 بتات (إشارة، و3 بتات للجزء الصحيح، و4 بتات للكسر).

-3.0625 / 0.0625 = -49, and -49 in two's complement is 11001111, so the answer is 1100.1111. Check: -8 + 4 + 0.5 + 0.25 + 0.125 + 0.0625 = -3.0625.

-3.0625 / 0.0625 = -49، و-49 بصيغة two's complement هو 11001111، إذن الجواب هو 1100.1111. التحقق: -8 + 4 + 0.5 + 0.25 + 0.125 + 0.0625 = -3.0625.

Recall

Why is fixed-point multiplication followed by a shift, and by how many bits in this format?لماذا يتبع ضربَ الـ fixed-point عمليةُ إزاحة، وبكم بت في هذه الصيغة؟

Each operand carries scale 2^-4, so the integer product carries 2^-8. Shifting the product right by 4 bits (with rounding) brings it back to scale 2^-4, the format of the inputs.

كل معامل يحمل المقياس 2^-4، لذلك يحمل ناتج الضرب الصحيح المقياس 2^-8. إزاحة الناتج إلى اليمين بمقدار 4 بتات (مع التقريب) تعيده إلى المقياس 2^-4، أي صيغة المدخلات.

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

In the sign + 3 integer + 4 fraction format, what does 1100.1111 represent?في صيغة إشارة + 3 بتات للجزء الصحيح + 4 بتات للكسر، ماذا يمثل النمط 1100.1111؟

Recapالخلاصة

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

  • Fewer bits means cheaper arithmetic and less data to move. At 45 nm: 8b int ADD 0.03 pJ against 32b float ADD 0.9 pJ (30x); 8b int MULT 0.2 pJ against 32b int MULT 3.1 pJ (15.5x, about 16x).بتات أقل تعني حساباً أرخص وبيانات أقل تحتاج إلى نقل. عند 45 nm: جمع 8b int يكلف 0.03 pJ مقابل 0.9 pJ لجمع 32b float (30x)، وضرب 8b int يكلف 0.2 pJ مقابل 3.1 pJ لضرب 32b int (15.5x، أي 16x تقريباً).
  • Multipliers scale roughly with the square of the bit-width (4x the bits, about 16x the energy); adders scale roughly linearly (4x the bits, about 3.3x the energy).تنمو طاقة الضوارب تقريباً مع مربع عدد البتات (4 أضعاف البتات تعطي قرابة 16 ضعف الطاقة)، وتنمو طاقة الجوامع خطياً تقريباً (4 أضعاف البتات تعطي قرابة 3.3 أضعاف الطاقة).
  • A 32-bit DRAM read costs about 640 pJ, about 200x a 32-bit multiply. Shrinking every weight from 32 to 8 bits cuts that traffic by 4x.قراءة 32-bit من DRAM تكلف نحو 640 pJ، أي نحو 200 ضعف عملية ضرب 32-bit. تقليص كل وزن من 32 إلى 8 بتات يخفض هذه الحركة 4 مرات.
  • Unsigned n bits: [0, 2^n - 1], so [0, 255] for 8 bits and [0, 15] for 4 bits.صيغة unsigned بعدد n من البتات: [0, 2^n - 1]، أي [0, 255] لـ 8 بتات و [0, 15] لـ 4 بتات.
  • Sign-magnitude: [-(2^(n-1) - 1), 2^(n-1) - 1], so [-127, 127] and [-7, 7], with two zeros (000...0 and 100...0).صيغة sign-magnitude: [-(2^(n-1) - 1), 2^(n-1) - 1]، أي [-127, 127] و [-7, 7]، مع صفرين (000...0 و 100...0).
  • Two's complement: [-2^(n-1), 2^(n-1) - 1], so [-128, 127] and INT4 [-8, 7]. One zero; the top bit has weight -2^(n-1). Negate by inverting and adding 1: -49 = 11001111.صيغة two's complement: [-2^(n-1), 2^(n-1) - 1]، أي [-128, 127] و INT4 [-8, 7]. صفر واحد فقط، والبت الأعلى وزنه -2^(n-1). نحصل على السالب بقلب البتات ثم إضافة 1: -49 = 11001111.
  • Fixed point is an integer plus a fixed power-of-two scale: 0011.0001 = 49 x 2^-4 = 3.0625. With 4 fraction bits the resolution is 0.0625 and the 8-bit range is [-8, 7.9375].الـ fixed point هو عدد صحيح مع مقياس (scale) ثابت من قوى اثنين: 0011.0001 = 49 x 2^-4 = 3.0625. مع 4 بتات كسرية تكون الدقة 0.0625 ويكون نطاق الصيغة ذات 8 بتات [-8, 7.9375].
  • Fixed point is linear quantization with S = 2^-4 and Z = 0. Lecture 05-2 frees S to be any real number and adds a zero point Z.الـ fixed point هو linear quantization بقيمتي S = 2^-4 و Z = 0. المحاضرة 05-2 تحرر S لتصبح أي عدد حقيقي وتضيف zero point هو Z.

Sourcesالمصادر