Majid Al-RaimiWhat quantization is and the three main approachesما التكميم والمناهج الثلاثة الرئيسية

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

What quantization is and the three main approachesما التكميم والمناهج الثلاثة الرئيسية

Quantization maps a continuous or large set of values onto a small discrete set, leaving a quantization error. Introduces the three neural network approaches (K-means, linear, binary/ternary) and the storage versus computation table, using the 4x4 running example matrix.التكميم يربط مجموعة قيم متصلة أو كبيرة بمجموعة منفصلة صغيرة، تاركًا خطأ تكميم. يقدم المناهج الثلاثة في الشبكات العصبية (K-means والخطي والثنائي/الثلاثي) وجدول التخزين مقابل الحساب، باستخدام مصفوفة المثال الجاري 4x4.

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

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

The first three parts of this lecture were about number formats: how many bits a value takes and what those bits can express. This part turns that knowledge into a technique. Quantization is the act of deliberately throwing away precision so that each number fits in fewer bits, and it is the single most widely deployed compression method for neural networks on embedded hardware.

Three ideas build on each other. First, what quantization means in general, and the error it always leaves behind. Second, the three ways the course quantizes a neural network, compared through one table of storage and computation that you will keep filling in until part 8. Third, the 4x4 weight matrix that the rest of the lecture reuses, and the idea of weight sharing it is built to show. For the midterm, expect the definition, the error, the table and the question of why K-means saves memory but not compute.

الأجزاء الثلاثة الأولى من هذه المحاضرة كانت عن صيغ الأعداد: كم بتًا تأخذ القيمة، وماذا تستطيع هذه البتات أن تعبّر عنه. هذا الجزء يحوّل تلك المعرفة إلى تقنية. التكميم (quantization) هو التخلّي المتعمَّد عن جزء من الدقة كي يتسع كل عدد في بتات أقل، وهو أكثر طرق ضغط الشبكات العصبية انتشارًا على العتاد المدمج (embedded hardware).

هناك ثلاث أفكار تُبنى كل واحدة منها على السابقة. أولًا، ما معنى التكميم بشكل عام، والخطأ الذي يتركه دائمًا خلفه. ثانيًا، الطرق الثلاث التي يكمّم بها المقرر الشبكة العصبية، مقارنةً من خلال جدول واحد للتخزين والحساب ستستمر في ملئه حتى الجزء 8. ثالثًا، مصفوفة الأوزان 4x4 التي تعيد بقية المحاضرة استخدامها، وفكرة مشاركة الأوزان (weight sharing) التي صُمّمت لتوضيحها. في الاختبار النصفي، توقّع أسئلة عن التعريف، والخطأ، والجدول، وسؤال لماذا يوفّر K-means في الذاكرة لا في الحساب.

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

  1. State the definition of quantization word for word and define quantization error.تذكر تعريف التكميم (quantization) حرفيًا كما في الشريحة، وتعرّف خطأ التكميم (quantization error).
  2. Compute the quantization error of a value and say why rounding bounds it by half a step.تحسب خطأ التكميم لقيمة ما، وتشرح لماذا يجعل التقريب إلى أقرب مستوى هذا الخطأ لا يتجاوز نصف خطوة.
  3. Explain palettization as K-means quantization of colours, with the palette as the codebook.تشرح الـ palettization على أنها تكميم K-means للألوان، حيث تكون لوحة الألوان (palette) هي الـ codebook.
  4. Name the three neural network quantization approaches and fill the storage and computation table for each.تسمّي الطرق الثلاث لتكميم الشبكات العصبية، وتملأ جدول التخزين والحساب لكل منها.
  5. Explain why K-means quantization saves memory but leaves the arithmetic in floating point.تشرح لماذا يوفّر تكميم K-means في الذاكرة لكنه يُبقي العمليات الحسابية بالفاصلة العائمة (floating point).
  6. Apply weight sharing to the 4x4 running example and compute the error of each shared weight.تطبّق مشاركة الأوزان (weight sharing) على المثال المستمر 4x4، وتحسب خطأ كل وزن مُشارَك.

Take a thermometer that reads 23.4718 °C and a display that can only show whole degrees. It shows 23. Anything from 22.5 to just under 23.5 also shows 23. The display has replaced an infinite set of possible temperatures with a short list of integers, and the 0.4718 it dropped is gone for good.

خذ ميزان حرارة يقرأ 23.4718 °C وشاشة لا تعرض إلا الدرجات الكاملة. ستعرض 23. وأي قيمة من 22.5 حتى ما دون 23.5 بقليل ستعرض أيضًا 23. لقد استبدلت الشاشة مجموعة لا نهائية من درجات الحرارة الممكنة بقائمة قصيرة من الأعداد الصحيحة، والجزء 0.4718 الذي حذفته ضاع إلى الأبد.

The definitionالتعريف

That is Quantization, and the lecture defines it in one sentence worth memorizing exactly:

هذا هو quantization (التكميم)، والمحاضرة تعرّفه في جملة واحدة تستحق أن تحفظها بنصّها:

Two phrases carry the meaning. “Continuous or otherwise large” covers both a truly continuous input, such as an analogue voltage, and an input that is already digital but has too many possible values, such as an FP32 weight with about four billion bit patterns. “A discrete set” is the small menu of values the output may take. For neural networks the input is almost always the second kind: weights are already numbers in a computer, just in a format far richer than the network needs.

عبارتان تحملان المعنى. عبارة "متصلة أو كبيرة جدًا" (continuous or otherwise large) تشمل المُدخل المتصل فعلًا، مثل جهد كهربائي تناظري، وتشمل أيضًا المُدخل الرقمي أصلًا لكن له عدد هائل من القيم الممكنة، مثل وزن بصيغة FP32 له قرابة أربعة مليارات نمط بتات. أما "مجموعة منفصلة" (a discrete set) فهي القائمة الصغيرة من القيم التي يُسمح للمُخرج أن يأخذها. في الشبكات العصبية يكون المُدخل دائمًا تقريبًا من النوع الثاني: الأوزان أعداد مخزّنة في الحاسوب أصلًا، لكن بصيغة أغنى بكثير مما تحتاجه الشبكة.

Quantization errorخطأ التكميم

Every quantizer loses something. The lecture names it: the difference between an input value and its quantized value is the Quantization error. For the thermometer it is 23.4718 - 23 = 0.4718. Written as a formula, with Q(x)Q(x) the quantized value of xx:

كل مُكمِّم يفقد شيئًا. والمحاضرة تسمّيه: الفرق بين قيمة المُدخل وقيمته بعد التكميم هو quantization error (خطأ التكميم). في مثال ميزان الحرارة يساوي 23.4718 - 23 = 0.4718. وبصيغة رياضية، حيث Q(x)Q(x) هي القيمة المكمَّمة لـ xx:

e=x−Q(x)e = x - Q(x)
Quantization error: input minus quantized valueخطأ التكميم: المدخل ناقص القيمة المكمَّمة

When the allowed values are evenly spaced with step Δ\Delta and you round to the nearest one, an input inside the range can never be more than half a step away from its output, so ∣e∣≤Δ/2|e| \le \Delta/2. Halve the step and the worst-case error halves. A smaller step needs more levels, and more levels need more bits: that trade between bits and error is the whole subject of this lecture.

عندما تكون القيم المسموح بها متباعدة بانتظام بخطوة Δ\Delta وتقرّب إلى أقربها، فإن أي مُدخل داخل المدى لا يمكن أن يبعد عن مُخرجه أكثر من نصف خطوة، لذلك ∣e∣≤Δ/2|e| \le \Delta/2. إذا نصّفت الخطوة تنصّف أسوأ خطأ ممكن. لكن الخطوة الأصغر تحتاج مستويات أكثر، والمستويات الأكثر تحتاج بتات أكثر: هذه المقايضة بين عدد البتات والخطأ هي موضوع هذه المحاضرة كله.

A continuous sine is sampled and each sample is rounded to the nearest of five levels; the teal bars are the quantization errorsموجة جيبية متصلة تُؤخذ منها عيّنات، وتُقرَّب كل عيّنة إلى أقرب مستوى من خمسة مستويات؛ والأعمدة الخضراء المزرقّة هي أخطاء التكميم

The figure on slide 21 shows this on a signal. The continuous curve can take any height. The quantized signal is drawn as stems that may only stop on the dotted levels. Where the curve passes exactly through a level the error is zero; between levels the stem stops short or overshoots, and that gap is the error. Notice that the error is not random noise added on top: it is fully determined by the input and the grid.

الشكل في الشريحة 21 يوضّح هذا على إشارة. المنحنى المتصل يمكن أن يأخذ أي ارتفاع. أما الإشارة المكمَّمة فمرسومة كسيقان (stems) لا يُسمح لها أن تتوقف إلا على المستويات المنقّطة. حيث يمرّ المنحنى تمامًا عبر مستوى يكون الخطأ صفرًا؛ وبين المستويات تتوقف الساق قبل المنحنى أو تتجاوزه، وتلك الفجوة هي الخطأ. لاحظ أن الخطأ ليس ضجيجًا عشوائيًا يُضاف فوق الإشارة: إنه محدَّد بالكامل بالمُدخل وبالشبكة (grid).

Palettization: the same idea for coloursالـ Palettization: الفكرة نفسها مطبَّقة على الألوان

The right half of slide 21 applies quantization to an image. A normal photo stores each pixel as 24 bits of colour, about 16.7 million possible values. The quantized version may use only 16 colours. Those 16 colours are the palette, and with Palettization each pixel stores only which palette entry it uses: a 4-bit index, since 2⁴ = 16.

النصف الأيمن من الشريحة 21 يطبّق التكميم على صورة. الصورة العادية تخزّن كل بكسل في 24 بتًا من اللون، أي قرابة 16.7 million قيمة ممكنة. أما النسخة المكمَّمة فقد تستخدم 16 لونًا فقط. هذه الألوان الـ 16 هي لوحة الألوان (palette)، ومع palettization يخزّن كل بكسل فقط رقم الخانة التي يستخدمها من اللوحة: فهرس (index) من 4-bit، لأن 2⁴ = 16.

Worked exampleمثال محلول

What palettization saves on a 640 x 480 imageماذا توفّر الـ palettization في صورة بأبعاد 640 x 480

  1. Originalالأصل

    640 × 480 = 307,200 pixels at 24 bits each is 7,372,800 bits, about 921.6 kB.

    640 × 480 = 307,200 بكسل، كل منها 24 بتًا، يساوي 7,372,800 بت، أي قرابة 921.6 kB.

  2. Indexesالفهارس

    Each pixel stores a 4-bit index: 307,200 × 4 = 1,228,800 bits, about 153.6 kB.

    كل بكسل يخزّن فهرسًا من 4-bit: 307,200 × 4 = 1,228,800 بت، أي قرابة 153.6 kB.

  3. Paletteلوحة الألوان

    16 colours at 24 bits each is 384 bits, just 48 B.

    16 لونًا، كل منها 24 بتًا، يساوي 384 بتًا، أي 48 B فقط.

  4. Resultالنتيجة

    About 6× smaller (24 / 4), and the palette is almost free. The cost is that every pixel now carries a colour quantization error, which is why the zoomed fur on the slide looks blocky.
    أصغر بقرابة 6× (24 / 4)، ولوحة الألوان تكاد تكون بلا تكلفة. الثمن هو أن كل بكسل يحمل الآن خطأ تكميم في لونه، ولهذا يبدو الفرو المكبَّر في الشريحة متكتّلًا (blocky).

This analogy is not loose. The standard way to pick a good 16-colour palette is to run K-means clustering on all the pixel colours and keep the 16 cluster centres. Replace “pixel colours” with “weights” and you have exactly the K-means weight quantization of the next part. Apple’s Core ML tools even call weight clustering “palettization”.

هذا التشبيه ليس تقريبيًا. الطريقة المعتادة لاختيار لوحة جيدة من 16 لونًا هي تشغيل التجميع بـ K-means (K-means clustering) على ألوان كل البكسلات والاحتفاظ بمراكز العناقيد الـ 16. استبدل "ألوان البكسلات" بـ "الأوزان" وستحصل تمامًا على تكميم الأوزان بـ K-means في الجزء التالي. بل إن أدوات Core ML من Apple تسمّي تجميع الأوزان (weight clustering) باسم "palettization".

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

A weight of 1.53 is quantized to the shared value 1.50. What is its quantization error?وزن قيمته 1.53 كُمِّم إلى القيمة المُشارَكة 1.50. ما خطأ التكميم له؟

Recall

Without looking: give the slide's definition of quantization, then define quantization error.دون أن تنظر: اذكر تعريف التكميم كما في الشريحة، ثم عرّف خطأ التكميم.

Quantization is the process of constraining an input from a continuous or otherwise large set of values to a discrete set. Quantization error is the difference between an input value and its quantized value.

التكميم هو عملية تقييد مُدخل من مجموعة قيم متصلة أو كبيرة جدًا بطريقة أخرى، إلى مجموعة منفصلة (Quantization is the process of constraining an input from a continuous or otherwise large set of values to a discrete set). وخطأ التكميم هو الفرق بين قيمة المُدخل وقيمته بعد التكميم.

Hand three engineers the same sixteen FP32 weights and ask each to store them in fewer bits. The first lists the four typical values once and stores, for every weight, which of the four it is closest to. The second lays an evenly spaced integer ruler over the range and stores each weight’s position on that ruler. The third keeps only the sign. Those are the three approaches on slide 22.

أعطِ ثلاثة مهندسين الأوزان الستة عشر نفسها بصيغة FP32، واطلب من كل واحد أن يخزّنها في بتات أقل. الأول يكتب القيم النموذجية الأربع مرة واحدة، ثم يخزّن لكل وزن أيّ هذه القيم الأربع هي الأقرب إليه. الثاني يضع مسطرة أعداد صحيحة متساوية التباعد فوق المدى، ويخزّن موقع كل وزن على تلك المسطرة. الثالث يحتفظ بالإشارة فقط. هذه هي الطرق الثلاث في الشريحة 22.

The 4x4 FP32 matrix, then its K-means indexes with a four-entry codebook, its 2-bit linear codes and its 1-bit signsمصفوفة 4x4 بصيغة FP32، ثم فهارس K-means مع codebook من أربع خانات، ثم رموزها الخطية من 2 بت، ثم إشاراتها من بت واحد
  1. K-means-based quantization. Group the weights into clusters, keep one floating-point value per cluster in a Codebook, and store each weight as a small integer index into it. On the slide, four clusters give the codebook {3: 2.00, 2: 1.50, 1: 0.00, 0: -1.00} and 2-bit indexes. This is K-means-based weight quantization, and it is the rest of this lecture.التكميم المعتمد على K-means. اجمع الأوزان في عناقيد (clusters)، واحتفظ بقيمة واحدة بالفاصلة العائمة لكل عنقود في codebook، وخزّن كل وزن كفهرس صحيح صغير يشير إليه. في الشريحة، أربعة عناقيد تعطي الـ codebook {3: 2.00, 2: 1.50, 1: 0.00, 0: -1.00} وفهارس من 2-bit. هذا هو K-means quantization، وهو موضوع بقية هذه المحاضرة.
  2. Linear quantization. Store an integer qq per weight and recover the real value with one affine formula shared by the whole tensor. The slide writes (q - (-1)) × 1.07, which is the general form r=S(q−Z)r = S(q - Z) with scale S = 1.07 and zero point Z = -1. This is Linear quantization, the topic of lecture 05-2.التكميم الخطي (Linear quantization). خزّن عددًا صحيحًا qq لكل وزن، واسترجع القيمة الحقيقية بصيغة تآلفية (affine) واحدة يشترك فيها الـ tensor كله. الشريحة تكتب (q - (-1)) × 1.07، وهي الصيغة العامة r=S(q−Z)r = S(q - Z) مع scale قيمته S = 1.07 وzero point قيمته Z = -1. هذا هو linear quantization، موضوع المحاضرة 05-2.
  3. Binary or ternary quantization. Push to the extreme: one bit per weight (two values) or three values with zero allowed. This is Binary and ternary quantization, covered in lecture 05-3.التكميم الثنائي أو الثلاثي (Binary or ternary quantization). اذهب إلى أقصى حد: بت واحد لكل وزن (قيمتان)، أو ثلاث قيم مع السماح بالصفر. هذا هو binary/ternary quantization، ويُشرح في المحاضرة 05-3.
r=S (q−Z),S=1.07, Z=−1r = S\,(q - Z),\qquad S = 1.07,\ Z = -1
Linear quantization preview from slide 22: real value from integer codeلمحة عن التكميم الخطي من الشريحة 22: القيمة الحقيقية من الرمز الصحيح

A quick check makes the linear formula concrete. The top-left weight 2.09 has code q = 1, so it reconstructs as (1 + 1) × 1.07 = 2.14, an error of 2.09 - 2.14 = -0.05. Its codes run from -2 to 1, exactly the range of a 2-bit two’s complement integer from part 1. Leave the derivation of 1.07 and -1 for lecture 05-2; here you only need to recognize the shape.

تحقّق سريع يجعل الصيغة الخطية ملموسة. الوزن في أعلى اليسار 2.09 رمزه q = 1، فيُعاد بناؤه كـ (1 + 1) × 1.07 = 2.14، بخطأ قدره 2.09 - 2.14 = -0.05. رموزه تمتد من -2 إلى 1، وهو تمامًا مدى عدد صحيح من 2-bit بتمثيل المتمم الثنائي (two’s complement) من الجزء 1. اترك اشتقاق 1.07 و-1 للمحاضرة 05-2؛ هنا يكفي أن تتعرّف على شكل الصيغة.

The table that organizes the whole topicالجدول الذي ينظّم الموضوع كله

Slides 22 and 23 do not compare the approaches by accuracy. They ask two engineering questions instead: what is kept in memory (storage), and what kind of arithmetic runs at inference (computation). The baseline network answers “floating-point” to both. Slide 23 fills in the K-means column, and the linear column is filled on slide 43.

الشريحتان 22 و23 لا تقارنان الطرق من حيث الدقة. بل تطرحان سؤالين هندسيين: ماذا يُحفظ في الذاكرة (التخزين، storage)، وما نوع العمليات الحسابية التي تجري أثناء الاستدلال (الحساب، computation). الشبكة الأساسية (baseline) تجيب "فاصلة عائمة" (floating-point) على السؤالين. الشريحة 23 تملأ عمود K-means، والعمود الخطي يُملأ في الشريحة 43.

Baseline (no quantization)الأساسي (بدون تكميم)K-means-basedالمعتمد على K-meansLinearالخطيBinary / ternaryالثنائي / الثلاثي
StorageالتخزينFloating-point weightsأوزان بالفاصلة العائمةInteger weights; floating-point codebookأوزان صحيحة؛ codebook بالفاصلة العائمةInteger weights (slide 43, part 8)أوزان صحيحة (الشريحة 43، الجزء 8)Lecture 05-3 (greyed out here)المحاضرة 05-3 (باهت هنا)
ComputationالحسابFloating-point arithmeticحساب بالفاصلة العائمةFloating-point arithmeticحساب بالفاصلة العائمةInteger arithmetic (slide 43, part 8)حساب بأعداد صحيحة (الشريحة 43، الجزء 8)Lecture 05-3 (greyed out here)المحاضرة 05-3 (باهت هنا)
Storage and computation per approach. Baseline and K-means are slides 22 and 23; linear is slide 43 (part 8); binary/ternary stays greyed out in this lecture.التخزين والحساب لكل طريقة. الأساسي وK-means من الشريحتين 22 و23؛ والخطي من الشريحة 43 (الجزء 8)؛ أما الثنائي/الثلاثي فيبقى باهتًا في هذه المحاضرة.

The K-means column is the surprising one. Its storage really is integer: 2 bits per weight instead of 32. But before a multiply can happen, each index has to be looked up in the codebook, and what comes out is a floating-point number. The multiply-accumulate units therefore still run in floating point. The saving is in memory size and in the bytes moved from DRAM, not in the arithmetic itself. Part 8 shows the lookup step explicitly (slide 42). Linear quantization is the approach that finally changes the second row, because its integers can be multiplied as integers.

عمود K-means هو المفاجئ. تخزينه فعلًا بأعداد صحيحة: 2 بت لكل وزن بدلًا من 32. لكن قبل أن يحدث أي ضرب، يجب البحث عن كل فهرس في الـ codebook، وما يخرج من البحث عدد بالفاصلة العائمة. لذلك تظل وحدات الضرب والجمع التراكمي (multiply-accumulate) تعمل بالفاصلة العائمة. التوفير يكون في حجم الذاكرة وفي عدد البايتات المنقولة من DRAM، لا في العمليات الحسابية نفسها. الجزء 8 يُظهر خطوة البحث صراحةً (الشريحة 42). والتكميم الخطي هو الطريقة التي تغيّر أخيرًا الصف الثاني، لأن أعداده الصحيحة يمكن ضربها كأعداد صحيحة.

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

Which entry of the storage and computation table describes K-means-based quantization?أيّ خانة في جدول التخزين والحساب تصف التكميم المعتمد على K-means؟

Recall

Linear quantization on slide 22 uses S = 1.07 and Z = -1. What real value does the code q = -2 represent?التكميم الخطي في الشريحة 22 يستخدم S = 1.07 وZ = -1. ما القيمة الحقيقية التي يمثّلها الرمز q = -2؟

r = 1.07 × (-2 - (-1)) = 1.07 × (-1) = -1.07. That is the reconstruction of -0.98, -1.08, -0.91 and -1.03 in the running example.

r = 1.07 × (-2 - (-1)) = 1.07 × (-1) = -1.07. هذه هي القيمة المُعاد بناؤها للأوزان -0.98 و-1.08 و-0.91 و-1.03 في المثال المستمر.

Here is the matrix the rest of the lecture lives on: sixteen weights of one layer, each stored as a 32-bit float. Look at it for a moment before reading on and ask which numbers are almost the same.

هذه هي المصفوفة التي تدور حولها بقية المحاضرة: ستة عشر وزنًا من طبقة واحدة، كل منها مخزّن كعدد بالفاصلة العائمة من 32-bit. تأمّلها لحظة قبل أن تكمل القراءة، واسأل نفسك: أيّ هذه الأعداد متقاربة جدًا؟

W=[2.09−0.981.480.090.05−0.14−1.082.12−0.911.920−1.031.8701.531.49]W = \begin{bmatrix} 2.09 & -0.98 & 1.48 & 0.09 \\ 0.05 & -0.14 & -1.08 & 2.12 \\ -0.91 & 1.92 & 0 & -1.03 \\ 1.87 & 0 & 1.53 & 1.49 \end{bmatrix}
The running example: a 4x4 FP32 weight matrix (slide 24)المثال المستمر: مصفوفة أوزان 4x4 بصيغة FP32 (الشريحة 24)

Sixteen FP32 values cost 16 × 32 = 512 bits, or 64 B. Yet the matrix clearly does not use sixteen independent values. Four of them sit near 2, three near 1.5, four near -1 and five near 0. Slide 25 takes the first group, 2.09, 2.12, 1.92 and 1.87, crosses them out and writes one number in their place: 2.0.

ست عشرة قيمة بصيغة FP32 تكلّف 16 × 32 = 512 بتًا، أي 64 B. ومع ذلك من الواضح أن المصفوفة لا تستخدم ست عشرة قيمة مستقلة. أربع منها قريبة من 2، وثلاث قريبة من 1.5، وأربع قريبة من -1، وخمس قريبة من 0. الشريحة 25 تأخذ المجموعة الأولى، 2.09 و2.12 و1.92 و1.87، وتشطبها وتكتب مكانها عددًا واحدًا: 2.0.

The four weights near 2 light up in the running matrix and collapse into one shared value, 2.0الأوزان الأربعة القريبة من 2 تُضاء في المصفوفة المستمرة ثم تنضمّ في قيمة مُشارَكة واحدة، 2.0

That move is Weight sharing: several weights that are close in value are replaced by one shared value. It is quantization by the definition above, because the four inputs are constrained to a discrete set (here a single value), and each now carries a quantization error.

هذه الخطوة هي weight sharing (مشاركة الأوزان): عدة أوزان متقاربة في القيمة تُستبدل بقيمة واحدة مُشارَكة. إنها تكميم وفق التعريف السابق، لأن المُدخلات الأربعة قُيّدت بمجموعة منفصلة (هنا قيمة واحدة)، وكل منها يحمل الآن خطأ تكميم.

WeightالوزنShared valueالقيمة المُشارَكةErrorالخطأ
2.092.092.002.00+0.09+0.09
2.122.122.002.00+0.12+0.12
1.921.922.002.00-0.08-0.08
1.871.872.002.00-0.13-0.13
Quantization error of each shared weight (input minus quantized value)خطأ التكميم لكل وزن مُشارَك (المُدخل ناقص القيمة المكمَّمة)

Two facts in that table matter later. First, every error is small compared with the weights themselves, at most 0.13 against values near 2. Second, the errors add up to zero: 0.09 + 0.12 - 0.08 - 0.13 = 0. That is no accident. 2.0 is exactly the mean of the four weights, (2.09 + 2.12 + 1.92 + 1.87) / 4 = 8.00 / 4 = 2.00, and the mean is the single value that minimizes the squared error of a group. In part 5 that mean gets its proper name, the Centroid of a K-means cluster.

حقيقتان في هذا الجدول ستهمّان لاحقًا. أولًا، كل خطأ صغير مقارنة بالأوزان نفسها، فهو لا يتجاوز 0.13 أمام قيم قريبة من 2. ثانيًا، مجموع الأخطاء يساوي صفرًا: 0.09 + 0.12 - 0.08 - 0.13 = 0. وهذا ليس صدفة. القيمة 2.0 هي تمامًا متوسط الأوزان الأربعة، (2.09 + 2.12 + 1.92 + 1.87) / 4 = 8.00 / 4 = 2.00، والمتوسط هو القيمة الوحيدة التي تجعل مجموع مربعات الأخطاء في المجموعة أصغر ما يمكن. في الجزء 5 يأخذ هذا المتوسط اسمه الصحيح: centroid (مركز) عنقود K-means.

Do the same for every group and the matrix needs only four distinct values. Four values can be told apart with 2 bits, since 2² = 4, so each weight can be stored as a 2-bit label pointing at its shared value instead of a 32-bit float. Part 5 turns this into the exact storage bill (64 B down to 20 B), and shows how the network is retrained around the shared values.

افعل الشيء نفسه مع كل مجموعة، فلن تحتاج المصفوفة إلا إلى أربع قيم مختلفة. أربع قيم يمكن التمييز بينها بـ 2 بت، لأن 2² = 4، لذلك يمكن تخزين كل وزن كتسمية (label) من 2-bit تشير إلى قيمته المُشارَكة بدلًا من عدد بالفاصلة العائمة من 32-bit. الجزء 5 يحوّل هذا إلى حساب التخزين الدقيق (من 64 B إلى 20 B)، ويوضّح كيف يُعاد تدريب الشبكة حول القيم المُشارَكة.

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

Why does K-means weight sharing on this 4x4 matrix not reduce the cost of each multiply at inference?لماذا لا تقلّل مشاركة الأوزان بـ K-means على هذه المصفوفة 4x4 تكلفة كل عملية ضرب أثناء الاستدلال؟

Recall

Group the remaining weights of the running example. Which value would each group share, and what is the error of -1.08?جمّع بقية أوزان المثال المستمر. ما القيمة التي ستتشاركها كل مجموعة، وما خطأ الوزن -1.08؟

Near 1.5: 1.48, 1.53, 1.49, mean 4.50 / 3 = 1.50. Near 0: 0.09, 0.05, -0.14, 0, 0, mean 0.00. Near -1: -0.98, -1.08, -0.91, -1.03, mean -4.00 / 4 = -1.00. The error of -1.08 is -1.08 - (-1.00) = -0.08. These are exactly the codebook values on slide 22.

قرب 1.5: 1.48 و1.53 و1.49، والمتوسط 4.50 / 3 = 1.50. قرب 0: 0.09 و0.05 و-0.14 و0 و0، والمتوسط 0.00. قرب -1: -0.98 و-1.08 و-0.91 و-1.03، والمتوسط -4.00 / 4 = -1.00. خطأ -1.08 هو -1.08 - (-1.00) = -0.08. وهذه بالضبط قيم الـ codebook في الشريحة 22.

Recall

How many bits does the original 4x4 matrix take, and how many bits per weight are needed once only four shared values remain?كم بتًا تأخذ المصفوفة 4x4 الأصلية، وكم بتًا يحتاج كل وزن بعد أن تبقى أربع قيم مُشارَكة فقط؟

16 × 32 = 512 bits (64 B). With four shared values, 2 bits per weight identify which one, plus a small table of the four values themselves.

16 × 32 = 512 بتًا (64 B). مع أربع قيم مُشارَكة، يكفي 2 بت لكل وزن لتحديد أيّها يستخدم، إضافة إلى جدول صغير يحوي القيم الأربع نفسها.

Recapالخلاصة

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

  • Quantization is the process of constraining an input from a continuous or otherwise large set of values to a discrete set.التكميم هو عملية تقييد مُدخل من مجموعة قيم متصلة أو كبيرة جدًا إلى مجموعة منفصلة (discrete).
  • Quantization error is the difference between an input value and its quantized value. With round-to-nearest on a grid of step Δ it is at most Δ/2.خطأ التكميم هو الفرق بين قيمة المُدخل وقيمته بعد التكميم. مع التقريب إلى الأقرب على شبكة خطوتها Δ، لا يتجاوز الخطأ Δ/2.
  • Palettization (a 16-colour image) is K-means quantization of colours: the palette is the codebook and each pixel stores a 4-bit index.الـ palettization (صورة بـ 16 لونًا) هي تكميم K-means للألوان: لوحة الألوان هي الـ codebook، وكل بكسل يخزّن فهرسًا (index) من 4 بتات.
  • Three approaches: K-means-based, linear, binary/ternary. Baseline stores FP weights and computes in FP arithmetic.ثلاث طرق: المعتمدة على K-means، والخطية (linear)، والثنائية/الثلاثية (binary/ternary). النموذج الأساسي (baseline) يخزّن أوزانًا بالفاصلة العائمة ويحسب بحساب الفاصلة العائمة.
  • K-means stores integer indexes plus an FP codebook, but computes in FP arithmetic: it saves storage, not compute.طريقة K-means تخزّن فهارس صحيحة إضافة إلى codebook بالفاصلة العائمة، لكنها تحسب بحساب الفاصلة العائمة: توفّر في التخزين لا في الحساب.
  • Linear quantization stores integers and can compute in integers, r = S(q - Z); the slide preview uses S = 1.07, Z = -1. Binary/ternary comes later.التكميم الخطي يخزّن أعدادًا صحيحة ويمكنه الحساب بأعداد صحيحة، r = S(q - Z)؛ ومعاينة الشريحة تستخدم S = 1.07 وZ = -1. أما الثنائي/الثلاثي فيأتي لاحقًا.
  • Running example: a 4x4 FP32 matrix (64 B). Weight sharing stores 2.09, 2.12, 1.92 and 1.87 all as 2.0, with errors 0.09, 0.12, -0.08 and -0.13.المثال المستمر: مصفوفة 4x4 بصيغة FP32 (64 B). مشاركة الأوزان تخزّن 2.09 و2.12 و1.92 و1.87 كلها بالقيمة 2.0، بأخطاء 0.09 و0.12 و-0.08 و-0.13.

Sourcesالمصادر