Lecture 05-3 · Quantization III

المحاضرة 05-3 · التكميم الثالث

AdaRound: rounding is not optimal

AdaRound: التقريب للأقرب ليس الأمثل

Slides 32–35الشرائح 32–35 P2 SHOULDP2 مهم

After choosing the granularity (Topic I) and the clipping range (Topic II), Post-Training Quantization has one knob left: Topic III, rounding. Every weight sits between two grid points, and rounding each one to the nearest point is not the best choice for the layer as a whole. AdaRound learns, per weight, whether to round down or up.

بعد اختيار الدقة (granularity، الموضوع الأول) ومدى القص (clipping range، الموضوع الثاني)، يبقى لـ Post-Training Quantization مقبض أخير: الموضوع الثالث، التقريب (rounding). كل وزن يقع بين نقطتين من الشبكة، وتقريب كل وزن إلى أقرب نقطة ليس الخيار الأفضل للطبقة ككل. يتعلّم AdaRound لكل وزن هل يُقرَّب للأسفل أم للأعلى.

The philosophy: weights are correlated

الفلسفة: الأوزان مترابطة

Slide 33 starts from the claim in its title: rounding-to-nearest is not optimal. Rounding each weight on its own minimises each weight's own error, but the layer never uses one weight alone. It uses all of them together in a dot product with the input, so the errors add up. The best rounding for each weight is not the best rounding for the whole tensor.

تبدأ الشريحة 33 من الادعاء الذي في عنوانها: التقريب إلى الأقرب (rounding-to-nearest) ليس الأمثل. تقريب كل وزن وحده يقلّل خطأ ذلك الوزن وحده، لكن الطبقة لا تستخدم وزناً واحداً منفرداً. إنها تستخدمها كلها معاً في حاصل ضرب نقطي (dot product) مع المدخل، فتتجمع الأخطاء. أفضل تقريب لكل وزن ليس أفضل تقريب للـ tensor كله.

rounding-to-nearest AdaRound (one potential result) 01 01 w1 = 0.3 w2 = 0.5 w3 = 0.7 w4 = 0.2 w2 flips: up for nearest, down for AdaRound nearest: [0, 1, 1, 0] AdaRound: [0, 0, 1, 0]
Slide 33's example. Each weight (in units of the quantization step) can only land on ⌊w⌋ = 0 or ⌈w⌉ = 1. Nearest rounding picks per weight; AdaRound picks jointly, so 0.5 may go down.مثال الشريحة 33. كل وزن (بوحدات خطوة التكميم) لا يمكن أن يقع إلا على ⌊w⌋ = 0 أو ⌈w⌉ = 1. التقريب للأقرب يختار لكل وزن على حدة؛ أما AdaRound فيختار للأوزان معاً، لذا قد تنزل 0.5 إلى الأسفل.

So what is optimal? Slide 33 answers: the rounding that reconstructs the original activation the best, which may be very different from nearest rounding. The target is the layer's output Wx, not the weights W. Two scope notes from the slide are exam material: AdaRound is for weight quantization only, and it uses short-term tuning, so it is "(almost) post-training quantization": no labels and no full retraining, only a quick per-layer optimisation on a small batch of calibration inputs.

إذن ما الأمثل؟ تجيب الشريحة 33: التقريب الذي يعيد بناء التفعيل (activation) الأصلي بأفضل شكل، وقد يختلف كثيراً عن التقريب إلى الأقرب. الهدف هو مخرج الطبقة Wx وليس الأوزان W. ملاحظتان من الشريحة مهمتان للامتحان: AdaRound لتكميم الأوزان فقط، ويستخدم ضبطاً قصير المدى (short-term tuning)، لذلك هو "تقريباً post-training quantization": بلا تسميات (labels) وبلا إعادة تدريب كاملة، فقط تحسين سريع لكل طبقة على دفعة صغيرة من مدخلات المعايرة (calibration).

The method: pick floor or ceil, learned

الطريقة: اختيار floor أو ceil بالتعلّم

Slide 34 turns the idea into a search space. Instead of always taking the nearest value ⌊w⌉, choose from the two neighbours {⌊w⌋, ⌈w⌉} to get the best reconstruction. To make the choice learnable, add a continuous offset δ to the floor and then round:

تحوّل الشريحة 34 الفكرة إلى فضاء بحث. بدلاً من أخذ القيمة الأقرب ⌊w⌉ دائماً، نختار من الجارين {⌊w⌋, ⌈w⌉} للحصول على أفضل إعادة بناء. ولجعل الاختيار قابلاً للتعلّم، نضيف إزاحة مستمرة δ إلى الـ floor ثم نقرّب:

w̃ = ⌊ ⌊w⌋ + δ ⌉,    δ ∈ [0, 1]

The inner bracket is floor, the outer one is round-to-nearest. If δ ends near 0 the result is ⌊w⌋ (round down); if δ ends near 1 the result is ⌊w⌋ + 1 = ⌈w⌉ (round up). The weight can never move further than one grid step, so AdaRound only ever decides up or down, which is exactly the title of the paper (Nagel et al., "Up or Down? Adaptive Rounding for Post-Training Quantization", PMLR 2020).

القوس الداخلي هو floor، والخارجي هو التقريب إلى الأقرب. إذا انتهت δ قرب 0 فالنتيجة ⌊w⌋ (تقريب للأسفل)، وإذا انتهت قرب 1 فالنتيجة ⌊w⌋ + 1 = ⌈w⌉ (تقريب للأعلى). لا يمكن للوزن أن يتحرك أكثر من خطوة شبكة واحدة، فـ AdaRound يقرّر دائماً للأعلى أم للأسفل فقط، وهذا بالضبط عنوان الورقة (Nagel et al., "Up or Down? Adaptive Rounding for Post-Training Quantization", PMLR 2020).

The objective on slide 35

الهدف في الشريحة 35

The slide writes the optimisation in two steps (it omits the derivation). First, the quantized weights W̃ should make the layer output match the full-precision output; then W̃ is replaced by the learnable rounding:

تكتب الشريحة التحسين على خطوتين (وتحذف الاشتقاق). أولاً، يجب أن تجعل الأوزان المكمّاة W̃ مخرج الطبقة مطابقاً للمخرج عالي الدقة، ثم تُستبدل W̃ بالتقريب القابل للتعلّم:

argminV ‖ Wx − W̃x ‖F2 + λ freg(V)
→ argminV ‖ Wx − ⌊ ⌊W⌋ + h(V) ⌉x ‖F2 + λ freg(V)

x, V and the loss

x وV ودالة الخسارة

x is the input to the layer (calibration activations). V is a variable with the same shape as W, one entry per weight; the slide calls it a random variable, and it is what gets optimised. The first term is the squared Frobenius norm of the output error, so the loss lives in activation space.

x هو مدخل الطبقة (تفعيلات المعايرة). V متغير بنفس شكل W، عنصر لكل وزن؛ تسميه الشريحة متغيراً عشوائياً، وهو ما يتم تحسينه. الحد الأول هو مربع معيار Frobenius لخطأ المخرج، فالخسارة تعيش في فضاء التفعيلات.

h(V) and freg(V)

h(V) وfreg(V)

h() maps any real V into (0, 1), for example a rectified sigmoid (the slide's plot is flat at 0, rises, then flat at 1). h(V) plays the role of δ. freg(V) is a regulariser that pushes h(V) to be binary, so at the end every weight is cleanly rounded down (0) or up (1). λ balances the two terms.

h() تحوّل أي V حقيقي إلى (0, 1)، مثل rectified sigmoid (منحنى الشريحة ثابت عند 0 ثم يرتفع ثم ثابت عند 1). تقوم h(V) بدور δ. وf_reg(V) منظِّم (regularizer) يدفع h(V) لتكون ثنائية، فينتهي كل وزن مقرَّباً بوضوح للأسفل (0) أو للأعلى (1). وλ توازن بين الحدّين.

0 1 −6 0 6 V h(V) = rectified sigmoid ∈ [0, 1] ⌊W⌋ + h(V) W̃ x ‖Wx − W̃x‖² + λ f_reg loss is measured on the layer output, gradient flows back into V only
Slide 35's ingredients. h(V) is a soft version of δ; the regulariser drives it to exactly 0 or 1 so the final rounding is hard.مكوّنات الشريحة 35. h(V) نسخة ناعمة من δ؛ والحد التنظيمي (regulariser) يدفعها إلى 0 أو 1 تماماً كي يكون التقريب النهائي حاداً.
Worked exampleمثال محلول

Take slide 33's weights (already divided by the scale, so the grid is the integers) and one calibration input x = [1, 1, 2, 1]. Compare weight error and output error for the two roundings.

خذ أوزان الشريحة 33 (مقسومة مسبقاً على المقياس، فالشبكة هي الأعداد الصحيحة) ومدخل معايرة واحداً x = [1, 1, 2, 1]. قارن خطأ الأوزان وخطأ المخرج للتقريبين.

W  = [0.3, 0.5, 0.7, 0.2]        x = [1, 1, 2, 1]
Wx = 0.3 + 0.5 + 1.4 + 0.2 = 2.4

nearest   W~ = [0, 1, 1, 0]
  weight error  (0.3, −0.5, −0.3, 0.2)  sum of squares = 0.09+0.25+0.09+0.04 = 0.47
  W~x = 0 + 1 + 2 + 0 = 3      output error = 3 − 2.4 = 0.6   squared 0.36

AdaRound  W~ = [0, 0, 1, 0]
  weight error  (0.3,  0.5, −0.3, 0.2)  sum of squares = 0.47   (same!)
  W~x = 0 + 0 + 2 + 0 = 2      output error = 2 − 2.4 = −0.4  squared 0.16

Both roundings have the same weight error (0.5 is equally far from 0 and 1), but rounding w2 down cuts the squared output error from 0.36 to 0.16. Nearest rounding cannot see this, because it never looks at x. AdaRound's loss is built on Wx, so it can.

للتقريبين نفس خطأ الأوزان (0.5 على نفس المسافة من 0 و1)، لكن تقريب w2 للأسفل يخفّض مربع خطأ المخرج من 0.36 إلى 0.16. التقريب إلى الأقرب لا يرى ذلك لأنه لا ينظر إلى x أبداً. أما خسارة AdaRound فمبنية على Wx، فيستطيع رؤيته.

Memory trickحيلة للحفظ

"Ada asks: up or down, judged by the output." Three words to remember: floor + h(V) (the choice), ‖Wx − W̃x‖ (the judge), freg (forces a hard 0/1 decision).

"Ada يسأل: للأعلى أم للأسفل، والحَكَم هو المخرج". ثلاث كلمات للحفظ: floor + h(V) (الاختيار)، ‖Wx − W̃x‖ (الحَكَم)، f_reg (يفرض قراراً حاسماً 0/1).

Exam trapفخ امتحاني

AdaRound minimises output (activation) error, not weight error. If an answer says "AdaRound minimises ‖W − W̃‖", it is describing rounding-to-nearest, which already does that perfectly per weight.

AdaRound يقلّل خطأ المخرج (activation) وليس خطأ الأوزان. إذا قالت إجابة إن AdaRound يقلّل ‖W − W̃‖ فهي تصف التقريب إلى الأقرب، الذي يفعل ذلك بشكل مثالي لكل وزن أصلاً.

Weights only. AdaRound does not choose roundings for activations; activations change with every input, so there is no fixed set of values to tune.

الأوزان فقط. لا يختار AdaRound التقريب للتفعيلات؛ فالتفعيلات تتغير مع كل مدخل، فلا توجد مجموعة قيم ثابتة لضبطها.

Only one step of freedom. w̃ is either ⌊w⌋ or ⌈w⌉. AdaRound never moves a weight two grid steps, and it does not change the scale S or zero point Z.

حرية خطوة واحدة فقط. w̃ إما ⌊w⌋ أو ⌈w⌉. لا يحرّك AdaRound وزناً خطوتين أبداً، ولا يغيّر المقياس S ولا نقطة الصفر Z.

"Almost" PTQ, not QAT. It does a short optimisation of V per layer, but it does not train the network's weights end to end with a task loss.

"تقريباً" PTQ، وليس QAT. يقوم بتحسين قصير لـ V لكل طبقة، لكنه لا يدرّب أوزان الشبكة من طرف إلى طرف بخسارة المهمة.

Check yourself

اختبر نفسك

Why is rounding-to-nearest not optimal according to slide 33?

لماذا لا يكون التقريب إلى الأقرب أمثل بحسب الشريحة 33؟

C. The layer output mixes all weights, so their rounding errors interact. A is false: nearest rounding gives the smallest weight error by definition. B and D have nothing to do with rounding.

C. خرج الطبقة يمزج كل الأوزان، فتتفاعل أخطاء تقريبها. الخيار A خاطئ: التقريب للأقرب يعطي أصغر خطأ في الأوزان بحكم التعريف. والخياران B وD لا علاقة لهما بالتقريب.

In AdaRound, a weight w = 3.4 (in grid units) can end up as which values?

في AdaRound، الوزن w = 3.4 (بوحدات الشبكة) يمكن أن ينتهي إلى أي قيم؟

B. w̃ = ⌊⌊w⌋ + δ⌉ with δ ∈ [0, 1] gives ⌊3 + δ⌉ ∈ {3, 4}, that is {⌊w⌋, ⌈w⌉}. A is nearest rounding. C allows moves of more than one step. D forgets the outer rounding; the result must be on the integer grid.

B. w̃ = ⌊⌊w⌋ + δ⌉ مع δ ∈ [0, 1] يعطي ⌊3 + δ⌉ ∈ {3, 4}، أي {⌊w⌋, ⌈w⌉}. الخيار A هو التقريب للأقرب. والخيار C يسمح بالتحرك أكثر من خطوة واحدة. والخيار D ينسى التقريب الخارجي؛ فالنتيجة يجب أن تقع على الشبكة الصحيحة.

What quantity does the first term of AdaRound's objective measure?

ما الكمية التي يقيسها الحد الأول في هدف AdaRound؟

A. AdaRound reconstructs the activation. B is what nearest rounding already minimises. C would be full training (no labels are needed). D is the KL clipping method from Topic II.

A. يعيد AdaRound بناء التفعيل. الخيار B هو ما يقلّله التقريب للأقرب أصلاً. والخيار C سيكون تدريباً كاملاً (ولا حاجة إلى تسميات). والخيار D هو طريقة القص بـ KL من الموضوع II.

What is the role of freg(V) in AdaRound?

ما دور f_reg(V) في AdaRound؟

D. Without it, h(V) could settle at 0.5 and the rounding would be ambiguous. B is the job of h() (the rectified sigmoid). A and C are not part of AdaRound.

D. بدونه قد تستقر h(V) عند 0.5 ويصبح التقريب غامضاً. الخيار B هو وظيفة h() (الـ rectified sigmoid). والخياران A وC ليسا جزءاً من AdaRound.

Which statement about AdaRound's scope is correct?

أي عبارة عن نطاق AdaRound صحيحة؟

C. Slide 33: "For weight quantization only" and "With short-term tuning, (almost) post-training quantization". A adds activations, B confuses it with QAT, D invents a per-weight scale.

C. الشريحة 33: "For weight quantization only" و"With short-term tuning, (almost) post-training quantization"، أي للأوزان فقط، ومع ضبط قصير المدى يكون (تقريباً) تكميماً بعد التدريب. الخيار A يضيف التفعيلات، والخيار B يخلطه مع QAT، والخيار D يخترع scale لكل وزن.