Lecture 05-1 · Quantization I
المحاضرة 05-1 · التكميم (Quantization I)
What quantization is: three approaches
ما هو الـ quantization: ثلاث طرق
P2 SHOULDQuantization maps a large or continuous set of values onto a small discrete set. For neural networks there are three main ways to do it, and they differ in one exam-critical way: what is stored, and what arithmetic runs at inference.
الـ quantization يحوّل مجموعة كبيرة أو متصلة من القيم إلى مجموعة صغيرة منفصلة. وفي الشبكات العصبية توجد ثلاث طرق رئيسية لذلك، وتختلف في نقطة حاسمة للامتحان: ما الذي يُخزَّن، وأي نوع من الحساب يجري وقت الاستدلال (inference).
Be able to fill the storage and computation table from memory, and to say which approach saves only memory and which also gives integer arithmetic. The 4 x 4 weight matrix on this page comes back in every later quantization page, so learn its numbers once.
يجب أن تستطيع ملء جدول التخزين والحساب من الذاكرة، وأن تقول أي طريقة توفّر الذاكرة فقط وأيها تعطي أيضاً حساباً بالأعداد الصحيحة (integer arithmetic). مصفوفة الأوزان 4 x 4 في هذه الصفحة تعود في كل صفحات الـ quantization اللاحقة، فاحفظ أرقامها مرة واحدة.
The definition and quantization error
التعريف وخطأ التكميم (quantization error)
Slide 21 defines quantization as the process of constraining an input from a continuous or otherwise large set of values to a discrete set. The difference between an input value and its quantized value is the quantization error. In the signal plot, the smooth curve can take any height, but each sample is snapped to the nearest dotted level; the short gap between the curve and the dot is the error.
تعرّف الشريحة 21 الـ quantization بأنه عملية حصر الدخل من مجموعة قيم متصلة أو كبيرة جداً في مجموعة منفصلة (discrete set). والفرق بين قيمة الدخل وقيمتها بعد التكميم يسمى خطأ التكميم (quantization error). في رسم الإشارة يمكن للمنحنى أن يأخذ أي ارتفاع، لكن كل عيّنة تُلصق بأقرب مستوى منقّط، والفجوة القصيرة بين المنحنى والنقطة هي الخطأ.
The second picture on slide 21 is palettization: the original photo is redrawn with only 16 colours. Each pixel then stores a 4-bit index into a 16-entry palette instead of a full 24-bit RGB colour. Keep this picture in mind: it is exactly what K-means weight quantization does to a weight matrix, with the palette playing the role of the codebook.
الصورة الثانية في الشريحة 21 هي الـ palettization: تُعاد رسم الصورة الأصلية بـ 16 لوناً فقط. فيخزّن كل بكسل فهرساً من 4 bit يشير إلى لوحة من 16 لوناً، بدلاً من لون RGB كامل من 24 bit. احتفظ بهذه الصورة في ذهنك: هذا بالضبط ما يفعله K-means weight quantization بمصفوفة الأوزان، واللوحة تقوم مقام الـ codebook.
The running example: one 4 x 4 weight matrix
المثال المستمر: مصفوفة أوزان واحدة 4 x 4
Slides 22 to 25 use the same 16 weights, stored as 32-bit floats. That is 16 x 32 = 512 bits = 64 bytes. Slide 25 shows the core idea in one line: the weights 2.09, 2.12, 1.92 and 1.87 are all close to each other, so why not replace all four with a single value, 2.0?
تستخدم الشرائح من 22 إلى 25 نفس الأوزان الـ 16 المخزّنة كأعداد عشرية من 32 bit. أي 16 x 32 = 512 bit = 64 B. وتعرض الشريحة 25 الفكرة الأساسية في سطر واحد: الأوزان 2.09 و2.12 و1.92 و1.87 كلها متقاربة، فلماذا لا نستبدلها جميعاً بقيمة واحدة هي 2.0؟
Three approaches: storage vs computation
ثلاث طرق: التخزين مقابل الحساب
Slides 22, 23 and 43 fill in the table one column at a time. The key is to read each column twice: once for what sits in memory, once for what the arithmetic units do.
تملأ الشرائح 22 و23 و43 الجدول عموداً بعد عمود. المفتاح أن تقرأ كل عمود مرتين: مرة لما يُخزَّن في الذاكرة، ومرة لما تفعله وحدات الحساب.
| Approachالطريقة | Storageالتخزين | Computationالحساب | Savesما توفّره |
|---|---|---|---|
| No quantization (baseline)بلا تكميم (الخط الأساسي baseline) | Floating-point weightsأوزان عشرية (floating-point) | Floating-point arithmeticحساب عشري | Nothingلا شيء |
| K-means-basedالمبني على K-means | Integer weights; floating-point codebookأوزان صحيحة، وcodebook عشري | Floating-point arithmeticحساب عشري | Storage onlyالتخزين فقط |
| Linearالخطي (Linear) | Integer weightsأوزان صحيحة | Integer arithmeticحساب بالأعداد الصحيحة | Storage and computeالتخزين والحساب |
| Binary / ternaryBinary / ternary | Binary or ternary weightsأوزان ثنائية أو ثلاثية | Bit operationsعمليات على البتات (bit operations) | Storage and compute, most aggressiveالتخزين والحساب، وهي الأشد |
الخط الأساسي يخزّن أوزاناً عشرية ويحسب بأعداد عشرية. الـ K-means يخزّن أعداداً صحيحة مع codebook عشري، لكن الحساب يبقى عشرياً، فيوفّر التخزين فقط. الـ linear quantization يخزّن أعداداً صحيحة ويحسب بأعداد صحيحة. والـ binary/ternary يخزّن أوزاناً ثنائية أو ثلاثية ويستبدل الضرب بعمليات على البتات (bit operations).
In Lecture 05-1 the binary/ternary column stays greyed out; it is filled in and taught in Quantization III. Here it matters only as the far end of the spectrum.
في محاضرة 05-1 يبقى عمود binary/ternary باهتاً، ويُملأ ويُشرح في Quantization III. هنا يهمّ فقط بوصفه الطرف الأقصى من الطيف.
K-means: integer index, float value
K-means: فهرس صحيح، وقيمة عشرية
The stored integer is not a number you compute with. It is a pointer into the codebook. At runtime each index is looked up and replaced by its float centroid, then the layer runs in ordinary floating point.
العدد الصحيح المخزَّن ليس رقماً تحسب به، بل هو مؤشر إلى الـ codebook. وقت التشغيل يُستبدل كل فهرس بالـ centroid العشري الخاص به، ثم تعمل الطبقة بالحساب العشري العادي.
Linear: integer is the value (up to an affine map)
Linear: العدد الصحيح هو القيمة (حتى تحويل affine)
The stored integer q relates to the real value by r = S(q - Z). On slide 22, S = 1.07 and Z = -1. Because the map is affine, the matrix multiply itself can be done on the integers q, with S and Z handled outside the inner loop.
العدد الصحيح المخزَّن q يرتبط بالقيمة الحقيقية بالعلاقة r = S(q - Z). في الشريحة 22 S = 1.07 وZ = -1. ولأن العلاقة affine، يمكن تنفيذ ضرب المصفوفات نفسه على الأعداد الصحيحة q، ويُعالج S وZ خارج الحلقة الداخلية.
Decode the top-left weight w = 2.09 in each scheme
K-means index = 3, codebook[3] = 2.00 w' = 2.00 error = +0.09
Linear q = round(2.09 / 1.07) + Z
= round(1.953) + (-1) = 2 - 1 = 1
r = 1.07 x (1 - (-1)) = 2.14 w' = 2.14 error = -0.05
Binary bit = 1, meaning +1 w' = +1 (a scale factor is
added in Quantization III)
Check one more linear entry: w = -0.98
q = round(-0.98 / 1.07) - 1 = round(-0.916) - 1 = -1 - 1 = -2
r = 1.07 x (-2 + 1) = -1.07
The linear integers on the slide all lie in {-2, -1, 0, 1}: that is a signed 2-bit range, the same bit budget as the 2-bit K-means indices.
كل الأعداد الصحيحة في المثال الخطي تقع في {-2, -1, 0, 1}، وهذا مدى 2-bit بإشارة، أي نفس ميزانية البتات لفهارس K-means ذات 2 bit.
K-means does not give integer arithmetic. Its weights are stored as integers, so it is easy to tick "integer arithmetic". Wrong: the integers are codebook indices, the weights are decoded to float, and every MAC is still floating point. K-means saves storage only.
الـ K-means لا يعطي حساباً بالأعداد الصحيحة. أوزانه مخزّنة كأعداد صحيحة، فيسهل أن تختار "integer arithmetic". هذا خطأ: الأعداد الصحيحة فهارس في الـ codebook، والأوزان تُفك إلى أعداد عشرية، وكل MAC يبقى عشرياً. الـ K-means يوفّر التخزين فقط.
Only linear (among the first three) gives integer storage and integer compute. Binary/ternary goes further and replaces multiplies with bit operations.
من بين الطرق الثلاث الأولى، الـ linear وحده يعطي تخزيناً وحساباً بالأعداد الصحيحة. والـ binary/ternary يذهب أبعد ويستبدل الضرب بعمليات على البتات.
Quantization error is per value, signed. It is r - Q(r) for each element, not one number for the whole tensor.
خطأ التكميم لكل قيمة على حدة، وله إشارة. هو r - Q(r) لكل عنصر، وليس رقماً واحداً للمصفوفة كلها.
K-means is a palette, linear is a ruler, binary is a coin. A palette shrinks the file but you still paint with real colours; a ruler lets you measure with integers; a coin has only two faces.
K-means لوحة ألوان، وlinear مسطرة، وbinary عملة معدنية. اللوحة تصغّر الملف لكنك ما زلت ترسم بألوان حقيقية، والمسطرة تتيح لك القياس بأعداد صحيحة، والعملة لها وجهان فقط.
Check yourself
اختبر نفسك
Which row correctly describes K-means-based weight quantization?
أي صف يصف K-means-based weight quantization بشكل صحيح؟
B. The indices are integers and the codebook is float; weights are decoded back to float before any MAC. A is linear quantization. D is binary/ternary. C is not any of the approaches.
B. الفهارس أعداد صحيحة والـ codebook عشري، والأوزان تُفك إلى قيم عشرية قبل أي MAC. الخيار A هو الـ linear quantization. والخيار D هو binary/ternary. والخيار C لا يطابق أياً من الطرق.
Using slide 22's linear map r = 1.07 x (q - (-1)), what real value does the stored integer q = -2 represent?
باستخدام العلاقة الخطية في الشريحة 22 r = 1.07 x (q - (-1))، ما القيمة الحقيقية التي يمثلها العدد المخزَّن q = -2؟
C. q - Z = -2 - (-1) = -1, and 1.07 x -1 = -1.07. A forgets to subtract the zero point (1.07 x -2). B adds Z with the wrong sign (-2 - 1 = -3). D would be q = -1, the zero point itself.
C. q - Z = -2 - (-1) = -1، و1.07 x -1 = -1.07. الخيار A ينسى طرح الـ zero point (1.07 x -2). والخيار B يضيف Z بإشارة خاطئة (-2 - 1 = -3). والخيار D يقابل q = -1، أي الـ zero point نفسه.
A weight of 1.87 is replaced by 2.00 after quantization. What is the quantization error by the slide's definition?
وزن قيمته 1.87 استُبدل بـ 2.00 بعد التكميم. ما خطأ التكميم وفق تعريف الشريحة؟
A. Error = input - quantized = 1.87 - 2.00 = -0.13, which is exactly the bottom-left entry of slide 27's error matrix. Its magnitude is 0.13; it is an absolute difference, not a percentage. C is the quantized value itself and D adds instead of subtracting.
A. الخطأ = الدخل ناقص القيمة بعد التكميم = 1.87 - 2.00 = -0.13، وهو بالضبط العنصر في أسفل اليسار من مصفوفة الخطأ في الشريحة 27. مقداره 0.13، وهو فرق مطلق وليس نسبة مئوية. والخيار C هو القيمة بعد التكميم نفسها، والخيار D يجمع بدلاً من أن يطرح.
Which approach is the only one of the three main approaches whose inference runs on integer arithmetic while keeping multi-bit weights?
أي طريقة هي الوحيدة من الطرق الثلاث الرئيسية التي يجري استدلالها بحساب الأعداد الصحيحة مع الإبقاء على أوزان متعددة البتات؟
D. Linear quantization stores integers and computes with integers. K-means (A) and palettization (C, the same idea applied to colours) decode back to real values before use. The baseline (B) is float everywhere.
D. الـ linear quantization يخزّن أعداداً صحيحة ويحسب بأعداد صحيحة. أما K-means (A) والـ palettization (C، وهي الفكرة نفسها مطبّقة على الألوان) فيفكّان القيم إلى أعداد حقيقية قبل استخدامها. والخط الأساسي (B) عشري في كل شيء.