COE 592Lecture 5.3المحاضرة 5.3Part 04الجزء 04
AdaRound and what post-training INT8 achievesAdaRound وما يحققه INT8 بعد التدريب
Rounding each weight to nearest is not optimal for the layer output, so AdaRound learns whether each weight rounds up or down to reconstruct activations. The section closes the PTQ story with the summary of zero point, granularity, range and rounding choices and real INT8 PTQ results, where small models like MobileNet lose too much.تقريب كل وزن إلى الأقرب ليس الأمثل لمخرج الطبقة، لذا يتعلم AdaRound ما إذا كان كل وزن يُقرَّب للأعلى أو للأسفل لإعادة بناء التنشيطات. ويختتم القسم قصة PTQ بملخص خيارات نقطة الصفر والدقة (granularity) والمدى والتقريب، ونتائج INT8 PTQ حقيقية، حيث تفقد النماذج الصغيرة مثل MobileNet الكثير من الدقة.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 32-38
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
The last two parts chose the grid: how fine the scales are and where the range is clipped. One decision is left before a weight becomes an integer. It sits between two grid points, and something has to decide which one it lands on. Everyone rounds to nearest without thinking. This part shows why that habit is not optimal, how AdaRound learns a better choice, and then closes the post-training story with real INT8 numbers.
For the COE 592 midterm, expect three kinds of questions: explain why nearest rounding can be beaten, write and label the AdaRound objective, and read the INT8 results table to say which models suffer and what to do next. For edge deployment, this is the part that tells you when a push-button INT8 conversion is enough and when it is not.
اختار الجزءان السابقان الشبكة (grid): مدى دقة المقاييس، وأين يُقصّ المدى. بقي قرار واحد قبل أن يصبح الوزن عددًا صحيحًا. فالوزن يقع بين نقطتين من الشبكة، ولا بد من شيء يقرر على أيهما يستقر. الجميع يقرّب إلى الأقرب دون تفكير. يبيّن هذا الجزء لماذا لا تكون هذه العادة هي الأمثل، وكيف يتعلم AdaRound اختيارًا أفضل، ثم يختم قصة التكميم بعد التدريب بأرقام INT8 حقيقية.
في اختبار منتصف الفصل لمقرر COE 592، توقّع ثلاثة أنواع من الأسئلة: اشرح لماذا يمكن التفوق على التقريب إلى الأقرب، واكتب هدف AdaRound وسمِّ حدوده، واقرأ جدول نتائج INT8 لتقول أي النماذج تتضرر وما الخطوة التالية. أما في النشر على الأجهزة الطرفية (edge)، فهذا هو الجزء الذي يخبرك متى يكفي تحويل INT8 بضغطة زر ومتى لا يكفي.
By the end you canفي نهاية هذا الجزء ستستطيع
- Explain why rounding every weight to its nearest grid value is not the best rounding for the layer output.اشرح لماذا لا يكون تقريب كل وزن إلى أقرب قيمة في الشبكة هو أفضل تقريب لمخرج الطبقة.
- Write the AdaRound objective from memory and name every term: W, x, W̃, V, h(), λ and f_reg.اكتب هدف AdaRound من الذاكرة وسمِّ كل حد فيه: W وx وW̃ وV وh() وλ وf_reg.
- Show on a two-weight example how choosing floor or ceil per weight lowers the output error.بيّن على مثال بوزنين كيف يقلل اختيار الأرضية (floor) أو السقف (ceil) لكل وزن خطأ المخرج.
- Say what the rectified sigmoid and the regularizer f_reg do, and why the result is still post-training quantization.اذكر ما تفعله دالة rectified sigmoid والمنظِّم f_reg، ولماذا تظل النتيجة تكميمًا بعد التدريب.
- Fill the storage and computation table for FP, K-means and linear quantization, and list the four PTQ knobs.املأ جدول التخزين والحساب لكل من FP وK-means والتكميم الخطي، واذكر مفاتيح PTQ الأربعة.
- Read the INT8 PTQ table, name the models that lose most, explain why, and say what fixes it.اقرأ جدول نتائج INT8 PTQ، وسمِّ النماذج التي تخسر أكثر، واشرح السبب، واذكر ما يعالج ذلك.
Why rounding each weight to nearest is not optimalلماذا لا يكون تقريب كل وزن إلى أقرب قيمة هو الأمثل
Take a neuron with two weights, both exactly 0.5, on a grid whose only levels are 0 and 1. Feed it the input x = [1, 1]. The true output is 0.5 + 0.5 = 1. Round each weight on its own and both go the same way, up to 1, so the quantized output is 2. Each weight made the smallest possible mistake, yet the output is off by a full unit. Round one up and one down and the output is exactly 1 again.
خذ عصبونًا له وزنان، كل منهما يساوي 0.5 تمامًا، على شبكة ليس فيها إلا المستويان 0 و1. أدخل إليه x = [1, 1]. المخرج الحقيقي هو 0.5 + 0.5 = 1. قرّب كل وزن بمفرده فيذهب الاثنان في الاتجاه نفسه، إلى أعلى نحو 1، فيصبح المخرج المكمَّم 2. ارتكب كل وزن أصغر خطأ ممكن، ومع ذلك ينحرف المخرج بوحدة كاملة. قرّب أحدهما إلى أعلى والآخر إلى أسفل فيعود المخرج 1 تمامًا.
The third PTQ question: which way to roundسؤال PTQ الثالث: في أي اتجاه نقرّب
Post-training quantization (PTQ) asks how to get the best (S, Z) for a trained model, and the lecture splits that into three topics: granularity, dynamic range clipping, and rounding. Once S and Z are fixed, every weight w (measured in grid steps) lies between its floor ⌊w⌋ and its ceil ⌈w⌉. Rounding to nearest picks whichever is closer. That is the right choice if you only care about each weight on its own: it gives the smallest error per weight, at most half a step.
يسأل post-training quantization كيف نحصل على أفضل (S, Z) لنموذج مدرَّب، وتقسم المحاضرة ذلك إلى ثلاثة مواضيع: الدقة الحبيبية (granularity)، وقص المدى الديناميكي (dynamic range clipping)، والتقريب (rounding). بعد تثبيت S وZ، يقع كل وزن w (مقيسًا بعدد خطوات الشبكة) بين أرضيته ⌊w⌋ وسقفه ⌈w⌉. يختار rounding to nearest أيهما أقرب. وهذا هو الاختيار الصحيح إذا كان كل ما يهمك هو كل وزن بمفرده: فهو يعطي أصغر خطأ لكل وزن، نصف خطوة على الأكثر.
But the network never uses a weight on its own. A layer computes Wx, and every output is a sum of many weights times many inputs. The errors Δw = w̃ − w of the weights in one row add together inside that sum. If they all lean the same way, they pile up. If some lean up and others down, they cancel. Nearest rounding decides each weight blind to its neighbours, so it cannot arrange the cancellation. The slide states it in one line: weights are correlated with each other, so the best rounding for each weight is not the best rounding for the whole tensor.
لكن الشبكة لا تستخدم وزنًا بمفرده أبدًا. الطبقة تحسب Wx، وكل مخرج هو مجموع لأوزان كثيرة مضروبة في مُدخلات كثيرة. أخطاء الأوزان Δw = w̃ − w في صف واحد تُجمع معًا داخل هذا المجموع. إذا مالت كلها في الاتجاه نفسه تراكمت، وإذا مال بعضها إلى أعلى وبعضها إلى أسفل ألغى بعضها بعضًا. التقريب إلى الأقرب يقرر كل وزن دون أن يرى جيرانه، لذلك لا يستطيع ترتيب هذا الإلغاء. والشريحة تقولها في سطر واحد: الأوزان مترابطة فيما بينها، لذلك فإن أفضل تقريب لكل وزن ليس أفضل تقريب للموتر كله.
Why the loss sees pairs of weightsلماذا ترى دالة الخسارة الأوزان أزواجًا
Nagel et al. make this precise with a second-order Taylor expansion of the task loss around the trained weights. For a small weight perturbation Δw:
يجعل Nagel et al. هذا دقيقًا باستخدام مفكوك تايلور (Taylor expansion) من الدرجة الثانية لخسارة المهمة حول الأوزان المدرَّبة. لاضطراب صغير في الأوزان Δw:
A trained network sits near a minimum, so g ≈ 0 and the Hessian term dominates. If H were diagonal, the cost would be a sum of independent terms H_ii Δw_i², and making each |Δw_i| as small as possible (nearest rounding) would be optimal. The off-diagonal entries H_ij Δw_i Δw_j are exactly the interactions between weights, and nearest rounding ignores them. The paper then shows that, layer by layer and under mild assumptions, minimizing this cost becomes minimizing the mean squared error of the layer's pre-activations, E[(ΔW x)²]. That is why the slide says the optimal rounding is the one that reconstructs the original activation best.
الشبكة المدرَّبة تقع قرب نقطة صغرى، لذلك g ≈ 0 ويهيمن حد مصفوفة هيسه (Hessian). لو كانت H قطرية، لكانت الكلفة مجموع حدود مستقلة H_ii Δw_i²، ولكان جعل كل |Δw_i| أصغر ما يمكن (أي التقريب إلى الأقرب) هو الأمثل. أما العناصر خارج القطر H_ij Δw_i Δw_j فهي بالضبط التفاعلات بين الأوزان، والتقريب إلى الأقرب يتجاهلها. ثم تبيّن الورقة أنه، طبقة بعد طبقة وتحت افتراضات معتدلة، يصبح تقليل هذه الكلفة هو تقليل متوسط مربع الخطأ لقيم ما قبل التنشيط (pre-activations) في الطبقة، أي E[(ΔW x)²]. لهذا تقول الشريحة إن التقريب الأمثل هو الذي يعيد بناء التنشيط الأصلي بأفضل شكل.
Back to the slide's tensorعودة إلى موتر الشريحة
Slide 33 rounds [0.3, 0.5, 0.7, 0.2] to nearest and gets [0, 1, 1, 0]. AdaRound might instead return [0, 0, 1, 0]: the 0.5 goes down. Whether that is better depends on the inputs the layer actually sees, which is the whole point. In the explorer below the layer sees two inputs, x1 = [1, 0, 1, 1] and x2 = [−1, 1, 1, −1]. Both roundings have the same weight error (0.47), but nearest gives an output error of 1.73 and the AdaRound result gives 0.13, the best of all 16 possible roundings.
تقرّب الشريحة 33 المتجه [0.3, 0.5, 0.7, 0.2] إلى الأقرب فتحصل على [0, 1, 1, 0]. أما AdaRound فقد يعيد بدلًا من ذلك [0, 0, 1, 0]: القيمة 0.5 تنزل إلى أسفل. وكون ذلك أفضل أم لا يعتمد على المُدخلات التي تراها الطبقة فعلًا، وهذا هو بيت القصيد. في المستكشف أدناه ترى الطبقة مُدخلين، x1 = [1, 0, 1, 1] وx2 = [−1, 1, 1, −1]. للتقريبين خطأ الأوزان نفسه (0.47)، لكن التقريب إلى الأقرب يعطي خطأ مخرج قدره 1.73 بينما تعطي نتيجة AdaRound القيمة 0.13، وهي الأفضل بين كل التقريبات الممكنة وعددها 16.
Recall
Two weights are both 0.4 on an integer grid and the input is x = [1, 1]. Compare nearest rounding with the best rounding.وزنان قيمة كل منهما 0.4 على شبكة أعداد صحيحة والمُدخل x = [1, 1]. قارن التقريب إلى الأقرب بأفضل تقريب.
Quick checkاختبار سريع
Why can rounding every weight to its nearest grid value be suboptimal?لماذا قد لا يكون تقريب كل وزن إلى أقرب قيمة في الشبكة هو الأمثل؟
Rounding is a yes or no question per weight: up or down. With millions of weights there are 2^n answers, far too many to try. AdaRound turns that discrete search into a smooth optimization that gradient descent can solve in minutes.
التقريب سؤال إجابته نعم أو لا لكل وزن: إلى أعلى أم إلى أسفل. ومع ملايين الأوزان توجد 2^n إجابة، وهذا أكثر بكثير مما يمكن تجربته. يحوّل AdaRound هذا البحث المتقطع إلى تحسين سلس يستطيع الانحدار التدريجي (gradient descent) حله في دقائق.
Step 1: restrict the choice to floor or ceilالخطوة 1: حصر الاختيار في الأرضية أو السقف
Instead of ⌊w⌉ (round to nearest), AdaRound chooses from {⌊w⌋, ⌈w⌉}, the two grid points around the weight. It writes the choice with an offset δ:
بدلًا من ⌊w⌉ (التقريب إلى الأقرب)، يختار AdaRound من {⌊w⌋, ⌈w⌉}، أي نقطتي الشبكة المحيطتين بالوزن. ويكتب هذا الاختيار بإزاحة δ:
Take w = 2.3. The floor is 2. With δ = 0.2, ⌊2.2⌉ = 2; with δ = 0.9, ⌊2.9⌉ = 3. So δ is a dial that selects floor or ceil, and it never moves the weight more than one step. Nearest rounding is the special case δ = w − ⌊w⌋, the weight's own fractional part (here 0.3, giving ⌊2.3⌉ = 2); AdaRound is free to pick any δ instead.
خذ w = 2.3. الأرضية هي 2. مع δ = 0.2 نحصل على ⌊2.2⌉ = 2، ومع δ = 0.9 نحصل على ⌊2.9⌉ = 3. إذن δ مقبض يختار الأرضية أو السقف، ولا يحرّك الوزن أبدًا أكثر من خطوة واحدة. التقريب إلى الأقرب هو الحالة الخاصة δ = w − ⌊w⌋، أي الجزء الكسري من الوزن نفسه (هنا 0.3، مما يعطي ⌊2.3⌉ = 2)، أما AdaRound فحرّ في اختيار أي δ بدلًا من ذلك.
Step 2: pick the δ values that reconstruct the outputالخطوة 2: اختيار قيم δ التي تعيد بناء المخرج
The dials are learned. The loss is the reconstruction error of the layer output, plus a regularizer:
هذه المقابض تُتعلَّم. دالة الخسارة هي خطأ إعادة بناء مخرج الطبقة، مضافًا إليه منظِّم (regularizer):
Every term of the AdaRound objectiveكل حد في هدف AdaRound
- Wx
- The original layer output (pre-activation) with full-precision weights. This is the target.مخرج الطبقة الأصلي (قبل التنشيط، pre-activation) بأوزان كاملة الدقة. وهذا هو الهدف المطلوب.
- W̃x
- The same layer with quantized weights. Each entry of W̃ is either the floor or the ceil of the matching weight.الطبقة نفسها بأوزان مكمَّمة. كل عنصر في W̃ إما أرضية الوزن المقابل له أو سقفه.
- x
- The input to this layer, collected by running a few unlabeled calibration samples through the network.مُدخل هذه الطبقة، ويُجمع بتمرير عدد قليل من عينات المعايرة غير المعنونة عبر الشبكة.
- V
- A continuous, learnable tensor with the same shape as W. One entry per weight decides that weight's direction.موتر مستمر قابل للتعلّم له شكل W نفسه. لكل وزن عنصر واحد يقرر اتجاه تقريب ذلك الوزن.
- h(V)
- Maps each V entry into [0, 1] (a rectified sigmoid). It replaces the hard δ so gradients can flow.تنقل كل عنصر من V إلى [0, 1] (دالة rectified sigmoid). وتحل محل الإزاحة الحادة δ حتى تستطيع التدرجات أن تمر.
- ||·||²_F
- Squared Frobenius norm: the sum of squared output errors over every output unit and every calibration sample.مربع معيار فروبينيوس (Frobenius): مجموع مربعات أخطاء المخرج على كل وحدة مخرج وكل عينة معايرة.
- λ f_reg(V)
- The regularizer, weighted by λ. It is zero only when each h(V) is exactly 0 or 1, so it forces a real floor or ceil decision.المنظِّم (regularizer) مضروبًا في الوزن λ. قيمته صفر فقط عندما تكون كل h(V) مساوية 0 أو 1 تمامًا، لذلك يفرض قرارًا حقيقيًا بالأرضية أو السقف.
Step 3: make the offset smooth, then force it binaryالخطوة 3: اجعل الإزاحة سلسة، ثم أجبرها على أن تكون ثنائية
The hard round inside W̃ has zero gradient almost everywhere, so AdaRound replaces δ by h(V), a smooth function of a free variable V. The paper uses the Rectified sigmoid h(V) from Louizos et al.:
التقريب الحاد داخل W̃ تدرّجه صفر في كل مكان تقريبًا، لذلك يستبدل AdaRound الإزاحة δ بالدالة h(V)، وهي دالة سلسة لمتغير حر V. تستخدم الورقة دالة rectified sigmoid من Louizos et al.:
Stretching then clipping matters. A plain sigmoid only approaches 0 and 1, and its gradient vanishes on the way. The rectified version hits 0 and 1 exactly with a gradient that is still healthy just before it gets there. During training, though, nothing stops h(V) from settling at 0.5, a half-step weight that no integer grid can store. The regularizer fixes that:
المد ثم القص أمر مهم. دالة sigmoid العادية تقترب فقط من 0 و1، ويتلاشى تدرّجها في الطريق. أما النسخة المقوَّمة (rectified) فتبلغ 0 و1 تمامًا بتدرّج لا يزال سليمًا قبل وصولها مباشرة. لكن أثناء التدريب لا شيء يمنع h(V) من الاستقرار عند 0.5، أي وزن عند نصف خطوة لا تستطيع أي شبكة أعداد صحيحة تخزينه. والمنظِّم يعالج ذلك:
Worked exampleمثال محلول
How f_reg scores one weightكيف يقيّم f_reg وزنًا واحدًا
Undecided weightوزن غير محسوم
With h = 0.5, |2(0.5) − 1| = 0, so the penalty is 1 − 0 = 1, the maximum, whatever β is.مع h = 0.5 يكون |2(0.5) − 1| = 0، لذلك تكون العقوبة 1 − 0 = 1، وهي القيمة القصوى، أيًّا كانت β.Nearly decided, early in trainingشبه محسوم، في بداية التدريب
With h = 0.9 and β = 20: |0.8|^20 ≈ 0.012, penalty ≈ 0.99. Almost the same as undecided, so the regularizer barely acts and the reconstruction term steers.مع h = 0.9 وβ = 20: |0.8|^20 ≈ 0.012، والعقوبة ≈ 0.99. أي تقريبًا مثل الوزن غير المحسوم، لذلك لا يكاد المنظِّم يعمل، ويتولى حد إعادة البناء التوجيه.Nearly decided, late in trainingشبه محسوم، في نهاية التدريب
With h = 0.9 and β = 2: 0.8² = 0.64, penalty 0.36. Now there is a clear slope toward h = 1.مع h = 0.9 وβ = 2: 0.8² = 0.64، والعقوبة 0.36. الآن يوجد ميل واضح نحو h = 1.Resultالنتيجة
At h = 1 or h = 0 the penalty is 0. Annealing β lets the weights explore first and then commits each one to exactly floor or ceil, after which the outer round has nothing left to do.عند h = 1 أو h = 0 تكون العقوبة 0. تخفيض β تدريجيًا (annealing) يسمح للأوزان بالاستكشاف أولًا، ثم يُلزم كل وزن بالأرضية أو السقف تمامًا، وبعدها لا يبقى للتقريب الخارجي ما يفعله.
The two-weight exercise, solved the AdaRound wayتمرين الوزنين، محلولًا بطريقة AdaRound
Worked exampleمثال محلول
Weights [0.5, 0.5], input x = [1, 1], grid {0, 1}الأوزان [0.5, 0.5]، والمُدخل x = [1, 1]، والشبكة {0, 1}
Targetالهدف
Wx = 0.5 + 0.5 = 1.Wx = 0.5 + 0.5 = 1.Nearest roundingالتقريب إلى الأقرب
Rounding half up, as slide 33 does with 0.5, gives [1, 1], so W̃x = 2 and the squared error is (1 − 2)² = 1. Rounding half to even would give [0, 0] and the same error, 1.التقريب بجعل النصف إلى أعلى، كما تفعل الشريحة 33 مع 0.5، يعطي [1, 1]، لذلك W̃x = 2 ومربع الخطأ (1 − 2)² = 1. أما التقريب بجعل النصف إلى العدد الزوجي فيعطي [0, 0] والخطأ نفسه، 1.All four candidatesالمرشحون الأربعة كلهم
The floor of each weight is 0, so W̃ = h(V) once h is binary. The options are [0, 0], [1, 0], [0, 1], [1, 1] with outputs 0, 1, 1, 2 and squared errors 1, 0, 0, 1.أرضية كل وزن هي 0، لذلك W̃ = h(V) بمجرد أن تصبح h ثنائية. الخيارات هي [0, 0] و[1, 0] و[0, 1] و[1, 1]، ومخرجاتها 0, 1, 1, 2، ومربعات أخطائها 1, 0, 0, 1.What AdaRound learnsما يتعلمه AdaRound
Gradient descent on the reconstruction term pulls h(V1) + h(V2) toward 1, and f_reg then pushes the two values apart to 1 and 0.الانحدار التدريجي على حد إعادة البناء يسحب h(V1) + h(V2) نحو 1، ثم يدفع f_reg القيمتين بعيدًا عن بعضهما إلى 1 و0.Resultالنتيجة
AdaRound picks [1, 0] (or [0, 1]), output 1, error 0, against error 1 for nearest.يختار AdaRound المتجه [1, 0] (أو [0, 1])، فيكون المخرج 1 والخطأ 0، مقابل خطأ 1 للتقريب إلى الأقرب.
Why it still counts as post-trainingلماذا يظل يُعدّ تكميمًا بعد التدريب
- Weights only. Activations are quantized with the usual range methods; AdaRound only decides the rounding of weights, and it does not change the scale S, which is fixed beforehand.الأوزان فقط. تُكمَّم التنشيطات بطرق المدى المعتادة، وAdaRound يقرر فقط تقريب الأوزان، ولا يغيّر المقياس S الذي يُثبَّت مسبقًا.
- Layer by layer. Each layer is optimized on its own local reconstruction loss, so there is no end-to-end training and no task loss.طبقة بعد طبقة. كل طبقة تُحسَّن على خسارة إعادة البناء المحلية الخاصة بها، لذلك لا يوجد تدريب من الطرف إلى الطرف (end-to-end) ولا خسارة مهمة.
- Little data, no labels. The paper uses 1024 unlabeled ImageNet images and 10k Adam iterations per layer; ResNet-18 takes about 10 minutes. That is the slide's "short-term tuning, (almost) post-training quantization".بيانات قليلة بلا تسميات. تستخدم الورقة 1024 صورة غير معنونة من ImageNet و10k تكرار من Adam لكل طبقة، ويستغرق ResNet-18 نحو 10 دقائق. وهذا ما تسميه الشريحة "short-term tuning, (almost) post-training quantization"، أي ضبط قصير المدى، وتكميم بعد التدريب (تقريبًا).
- It pays off at low bit widths. With 4-bit weights, ResNet-18 goes from 23.99% with nearest rounding to 68.71% with AdaRound (FP32 69.68%), and ResNet-50 from 35.60% to 75.23% (FP32 76.07%).يؤتي ثماره عند عروض البتات المنخفضة. مع أوزان 4 بت، يرتفع ResNet-18 من 23.99% بالتقريب إلى الأقرب إلى 68.71% مع AdaRound (ودقة FP32 هي 69.68%)، ويرتفع ResNet-50 من 35.60% إلى 75.23% (ودقة FP32 هي 76.07%).
Recall
Name every term in argmin_V ||Wx − ⌊⌊W⌋ + h(V)⌉x||²_F + λ f_reg(V).سمِّ كل حد في argmin_V ||Wx − ⌊⌊W⌋ + h(V)⌉x||²_F + λ f_reg(V).
Recall
Why does AdaRound need f_reg at all, if the reconstruction loss already prefers good roundings?لماذا يحتاج AdaRound إلى f_reg أصلًا، إذا كانت خسارة إعادة البناء تفضّل التقريبات الجيدة بالفعل؟
Quick checkاختبار سريع
What does the regularizer f_reg(V) do in AdaRound?ما الذي يفعله المنظِّم f_reg(V) في AdaRound؟
Take the running 4×4 matrix from Lecture 5.1, with entries such as 2.09, −0.98 and 1.48. K-means quantization stores 2.09 as index 3 and looks up 2.00 in a floating-point codebook. Linear quantization stores it as the integer q = 1 and recovers (1 − (−1)) × 1.07 = 2.14. Both store small integers. Only one of them can also compute with them.
خذ المصفوفة 4×4 المرافقة من المحاضرة 5.1، بعناصر مثل 2.09 و−0.98 و1.48. تكميم K-means يخزّن 2.09 على أنه الفهرس 3 ويبحث عن 2.00 في دفتر رموز (codebook) بالفاصلة العائمة. أما التكميم الخطي فيخزّنه كعدد صحيح q = 1 ويستعيد (1 − (−1)) × 1.07 = 2.14. كلاهما يخزّن أعدادًا صحيحة صغيرة، لكن واحدًا منهما فقط يستطيع أيضًا أن يحسب بها.
| Approachالطريقة | Storageالتخزين | Computationالحساب | What it savesما الذي توفره |
|---|---|---|---|
| Floating-point baselineخط الأساس بالفاصلة العائمة | Floating-point weightsأوزان بالفاصلة العائمة | Floating-point arithmeticحساب بالفاصلة العائمة | Nothing savedلا توفير |
| K-means-basedK-means | Integer indexes plus a floating-point codebookفهارس صحيحة مع دفتر رموز (codebook) بالفاصلة العائمة | Floating-point arithmeticحساب بالفاصلة العائمة | Storage onlyالتخزين فقط |
| Linearخطي | Integer weights (plus S and Z)أوزان صحيحة (مع S وZ) | Integer arithmeticحساب بالأعداد الصحيحة | Storage and computeالتخزين والحساب |
K-means indexes are only pointers. Before any multiply-accumulate the hardware must look up the real floating-point value, so the arithmetic stays floating point and the saving is memory and bandwidth. Linear quantization has an affine map r = S(q − Z), which lets a whole matrix product run on integers with the scale applied once at the end. That is why the rest of this lecture tunes linear quantization.
فهارس K-means مجرد مؤشرات. قبل أي عملية ضرب وتجميع (multiply-accumulate) يجب على العتاد أن يبحث عن القيمة الحقيقية بالفاصلة العائمة، لذلك يبقى الحساب بالفاصلة العائمة، ويكون التوفير في الذاكرة وعرض النطاق (bandwidth). أما التكميم الخطي فله تحويل أفيني (affine) r = S(q − Z)، يسمح بإجراء ضرب مصفوفة كامل بالأعداد الصحيحة مع تطبيق المقياس مرة واحدة في النهاية. ولهذا تضبط بقية هذه المحاضرة التكميم الخطي.
Every post-training choice in one listكل اختيارات ما بعد التدريب في قائمة واحدة
With this part, all four knobs of linear post-training quantization are on the table. Each answers a different question about the same map r = S(q − Z):
مع هذا الجزء، أصبحت مفاتيح التكميم الخطي بعد التدريب الأربعة كلها مطروحة. كل منها يجيب عن سؤال مختلف حول التحويل نفسه r = S(q − Z):
| Knobالمفتاح | Optionsالخيارات | Question it answersالسؤال الذي يجيب عنه | Covered inموضع الشرح |
|---|---|---|---|
| Zero pointنقطة الصفر | Asymmetric, symmetricغير متماثل، متماثل | Where real 0 sits on the integer gridأين يقع الصفر الحقيقي على شبكة الأعداد الصحيحة | Lecture 5.2المحاضرة 5.2 |
| Scaling granularityالدقة الحبيبية للمقياس (granularity) | Per-tensor, per-channel, groupلكل موتر، لكل قناة، لكل مجموعة | How many values share one scale Sكم قيمة تتشارك مقياسًا واحدًا S | Parts 1 and 2الجزءان 1 و2 |
| Range clippingقص المدى | EMA, minimize KL, minimize MSEEMA، تقليل KL، تقليل MSE | Which r_min and r_max set Sأي r_min وr_max يحددان S | Part 3الجزء 3 |
| Roundingالتقريب | Nearest, AdaRoundإلى الأقرب، AdaRound | Which grid point each weight lands onأي نقطة في الشبكة يستقر عليها كل وزن | This partهذا الجزء |
The first three shape the grid: Symmetric quantization or asymmetric for Z, Quantization granularity for how many values share S, and Dynamic range clipping for what range S must cover. Rounding is the last step: it places each weight on the grid the first three built.
المفاتيح الثلاثة الأولى تشكّل الشبكة: symmetric quantization أو غير المتماثل لاختيار Z، وquantization granularity لعدد القيم التي تتشارك S، وdynamic range clipping للمدى الذي يجب أن يغطيه S. والتقريب هو الخطوة الأخيرة: فهو يضع كل وزن على الشبكة التي بنتها المفاتيح الثلاثة الأولى.
Recall
Fill the table: storage and computation for FP, K-means and linear quantization.املأ الجدول: التخزين والحساب لكل من FP وK-means والتكميم الخطي.
Convert ResNet-50 to INT8 with no retraining, symmetric per-channel weights and EMA-calibrated activations, and it loses 0.6% of top-1 accuracy. Apply the exact same recipe to MobileNetV1 and it loses 11.8%. Same bit width, same recipe, wildly different results. This concept is about why.
حوّل ResNet-50 إلى INT8 دون إعادة تدريب، بأوزان متماثلة لكل قناة وتنشيطات معايرة بـEMA، فيخسر 0.6% من دقة top-1. طبّق الوصفة نفسها تمامًا على MobileNetV1 فيخسر 11.8%. عرض البتات نفسه، والوصفة نفسها، ونتائج مختلفة اختلافًا هائلًا. هذا المفهوم يدور حول السبب.
Reading the tableقراءة الجدول
Slide 37 compares two post-training INT8 recipes. In the first, activations are symmetric per-tensor with the range chosen by minimizing KL divergence (the TensorRT entropy calibration from part 3), and weights are symmetric Per-tensor quantization. In the second, activations are asymmetric per-tensor with the range tracked by an Exponential moving average (EMA), and weights are symmetric Per-channel quantization. The numbers are the change in top-1 accuracy against the floating-point model.
تقارن الشريحة 37 بين وصفتين لتكميم INT8 بعد التدريب. في الأولى، التنشيطات متماثلة لكل موتر (per-tensor) مع اختيار المدى بتقليل KL divergence (معايرة الإنتروبيا في TensorRT من الجزء 3)، والأوزان متماثلة بأسلوب per-tensor quantization. في الثانية، التنشيطات غير متماثلة لكل موتر مع تتبع المدى بـexponential moving average، والأوزان متماثلة بأسلوب per-channel quantization. الأرقام هي التغير في دقة top-1 مقارنة بنموذج الفاصلة العائمة.
| Networkالشبكة | Act: sym per-tensor, KL. W: sym per-tensorالتنشيطات: متماثلة لكل موتر، KL. الأوزان: متماثلة لكل موتر | Act: asym per-tensor, EMA. W: sym per-channelالتنشيطات: غير متماثلة لكل موتر، EMA. الأوزان: متماثلة لكل قناة |
|---|---|---|
| GoogleNetGoogleNet | −0.45%−0.45% | 0%0% |
| ResNet-50ResNet-50 | −0.13%−0.13% | −0.6%−0.6% |
| ResNet-152ResNet-152 | −0.08%−0.08% | −1.8%−1.8% |
| MobileNetV1MobileNetV1 | not reportedلم يُذكر | −11.8%−11.8% |
| MobileNetV2MobileNetV2 | not reportedلم يُذكر | −2.1%−2.1% |
The boxed rows on slide 37 are the good news: for GoogleNet and the ResNets, INT8 post-training quantization costs at most 1.8% and often almost nothing. Krishnamoorthi's whitepaper reports the same overall picture: per-channel weights with per-layer activations stay within about 2% of floating point across many networks. For these models, PTQ is a push-button win: 4× smaller weights and integer arithmetic for almost free.
الصفوف المؤطَّرة في الشريحة 37 هي الخبر السار: بالنسبة لـGoogleNet ونماذج ResNet، يكلّف تكميم INT8 بعد التدريب 1.8% على الأكثر، وكثيرًا ما يكلّف شبه لا شيء. وتورد الورقة البيضاء (whitepaper) لـKrishnamoorthi الصورة العامة نفسها: الأوزان لكل قناة مع التنشيطات لكل طبقة تبقى في حدود نحو 2% من الفاصلة العائمة عبر شبكات كثيرة. بالنسبة لهذه النماذج، PTQ مكسب بضغطة زر: أوزان أصغر بمقدار 4× وحساب بالأعداد الصحيحة مقابل شبه لا شيء.
Why small models breakلماذا تنهار النماذج الصغيرة
Slide 38 boxes MobileNetV1 at −11.8% and MobileNetV2 at −2.1%, and gives the explanation: smaller models seem to not respond as well to post-training quantization, presumably due to their smaller representational capacity. A big ResNet has many redundant weights, so the small random errors that rounding adds average out and other weights carry the same information. A compact model like MobileNet has been squeezed to use nearly every parameter, so there is little slack to absorb the noise. Its depthwise separable convolutions make it worse: each depthwise filter has only 9 weights, so per-filter ranges vary a lot. Per-channel weight scales absorb that for the weights, but the activations in this recipe still share one scale per tensor, which wastes most of the grid on the narrow channels.
تؤطّر الشريحة 38 نموذج MobileNetV1 عند −11.8% وMobileNetV2 عند −2.1%، وتعطي التفسير: يبدو أن النماذج الأصغر لا تستجيب جيدًا للتكميم بعد التدريب، ويُفترض أن ذلك بسبب سعتها التمثيلية الأصغر. نموذج ResNet الكبير فيه أوزان زائدة كثيرة، لذلك تتلاشى بالمتوسط الأخطاء العشوائية الصغيرة التي يضيفها التقريب، وتحمل أوزان أخرى المعلومات نفسها. أما النموذج المضغوط مثل MobileNet فقد عُصر ليستخدم كل معامل تقريبًا، لذلك لا يبقى هامش يُذكر لامتصاص الضوضاء. والالتفافات القابلة للفصل عمقيًا (depthwise separable convolutions) تزيد الأمر سوءًا: كل مرشح عمقي فيه 9 أوزان فقط، لذلك تختلف المديات كثيرًا من مرشح لآخر. المقاييس لكل قناة تمتص ذلك في الأوزان، لكن التنشيطات في هذه الوصفة لا تزال تتشارك مقياسًا واحدًا لكل موتر، مما يهدر معظم الشبكة على القنوات الضيقة.
How to get the accuracy backكيف نستعيد الدقة
- Better PTQ. Per-channel scales, cross-layer equalization and bias correction (Nagel et al., ICCV 2019) bring MobileNetV2 at 8/8 bits to 71.2% against 71.72% in floating point, with no data at all. AdaRound pushes PTQ down to 4-bit weights.PTQ أفضل. المقاييس لكل قناة، والمعادلة عبر الطبقات (cross-layer equalization)، وتصحيح الانحياز (bias correction) من Nagel et al., ICCV 2019 ترفع MobileNetV2 عند 8/8 بت إلى 71.2% مقابل 71.72% بالفاصلة العائمة، دون أي بيانات على الإطلاق. ويدفع AdaRound تكميم PTQ نزولًا إلى أوزان 4 بت.
- Quantization-aware training (QAT). When PTQ is still not enough, let the network learn with quantization in the loop. In the whitepaper, QAT lifts MobileNetV1 from 59.1% to 70.7%, against 70.9% in floating point. That is the answer to the slide's question "How should we improve performance of quantized models?" and the subject of the next part.quantization-aware training. عندما لا يكفي PTQ، دع الشبكة تتعلم والتكميم داخل الحلقة. في الورقة البيضاء، يرفع QAT نموذج MobileNetV1 من 59.1% إلى 70.7%، مقابل 70.9% بالفاصلة العائمة. هذه هي الإجابة عن سؤال الشريحة "How should we improve performance of quantized models?" (كيف نحسّن أداء النماذج المكمَّمة؟)، وهي موضوع الجزء التالي.
Recall
Why does a large model like ResNet-152 tolerate post-training INT8 better than MobileNetV1?لماذا يتحمل نموذج كبير مثل ResNet-152 تكميم INT8 بعد التدريب أفضل من MobileNetV1؟
Quick checkاختبار سريع
In the slide 37 table, which network loses the most top-1 accuracy under post-training INT8, and why?في جدول الشريحة 37، أي شبكة تخسر أكبر قدر من دقة top-1 تحت تكميم INT8 بعد التدريب، ولماذا؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Rounding to nearest minimizes each weight's own error, but weights are correlated in the output Wx, so their errors can add up instead of cancel.التقريب إلى الأقرب يقلل خطأ كل وزن بمفرده، لكن الأوزان مترابطة في المخرج Wx، لذلك قد تتراكم أخطاؤها بدل أن يلغي بعضها بعضًا.
- Slide 33: [0.3, 0.5, 0.7, 0.2] rounds to [0, 1, 1, 0] by nearest; AdaRound may return [0, 0, 1, 0].الشريحة 33: المتجه [0.3, 0.5, 0.7, 0.2] يُقرَّب إلى [0, 1, 1, 0] بالتقريب إلى الأقرب، بينما قد يعيد AdaRound المتجه [0, 0, 1, 0].
- Optimal rounding is the one that best reconstructs the layer output (the activation), not the weights.التقريب الأمثل هو الذي يعيد بناء مخرج الطبقة (التنشيط) بأفضل شكل، وليس الأوزان.
- AdaRound picks floor or ceil per weight: w̃ = ⌊⌊w⌋ + δ⌉, δ ∈ [0, 1].يختار AdaRound الأرضية أو السقف لكل وزن: w̃ = ⌊⌊w⌋ + δ⌉، حيث δ ∈ [0, 1].
- Objective: argmin_V ||Wx − ⌊⌊W⌋ + h(V)⌉x||²_F + λ f_reg(V). x is the layer input, V a learnable tensor shaped like W, h a rectified sigmoid into [0, 1], f_reg pushes h(V) to 0 or 1.الهدف: argmin_V ||Wx − ⌊⌊W⌋ + h(V)⌉x||²_F + λ f_reg(V). هنا x مُدخل الطبقة، وV موتر قابل للتعلّم بشكل W نفسه، وh دالة rectified sigmoid تنقل القيم إلى [0, 1]، وf_reg يدفع h(V) إلى 0 أو 1.
- AdaRound is weight-only, layer by layer, with a short optimization on a little unlabeled data: almost post-training. 4-bit ResNet-18 goes from 23.99% (nearest) to 68.71% (FP 69.68%).AdaRound يعمل على الأوزان فقط، طبقة بعد طبقة، مع تحسين قصير على قليل من البيانات غير المعنونة: إنه تكميم بعد التدريب تقريبًا. ResNet-18 بأوزان 4 بت ترتفع من 23.99% (بالتقريب إلى الأقرب) إلى 68.71% (ودقة FP هي 69.68%).
- PTQ knobs: zero point (asymmetric, symmetric), granularity (per-tensor, per-channel, group), range clipping (EMA, KL, MSE), rounding (nearest, AdaRound).مفاتيح PTQ: نقطة الصفر (غير متماثل، متماثل)، والدقة الحبيبية granularity (لكل موتر، لكل قناة، لكل مجموعة)، وقص المدى (EMA، KL، MSE)، والتقريب (إلى الأقرب، AdaRound).
- K-means stores integer indexes plus an FP codebook and computes in FP; linear stores integers and computes in integers.K-means يخزّن فهارس صحيحة مع دفتر رموز codebook بالفاصلة العائمة ويحسب بالفاصلة العائمة، أما التكميم الخطي فيخزّن أعدادًا صحيحة ويحسب بالأعداد الصحيحة.
- INT8 PTQ costs large models under 2%, but MobileNetV1 loses 11.8% with symmetric per-channel weights. Small models have less spare capacity, which motivates QAT.تكميم INT8 بعد التدريب يكلّف النماذج الكبيرة أقل من 2%، لكن MobileNetV1 يخسر 11.8% مع أوزان متماثلة لكل قناة. النماذج الصغيرة لديها سعة احتياطية أقل، وهذا ما يدفع إلى QAT.
Sourcesالمصادر
- Up or Down? Adaptive Rounding for Post-Training QuantizationPaperNagel, Amjad, van Baalen, Louizos, Blankevoort. ICML 2020, PMLR 119The AdaRound paper behind slides 33 to 35.ورقة AdaRound التي تستند إليها الشرائح من 33 إلى 35.(opens in a new tab)(يفتح في تبويب جديد)
- Up or Down? Adaptive Rounding for Post-Training Quantization (arXiv full text)PaperarXiv 2004.10568Taylor expansion argument, the ResNet-18 rounding experiment, rectified sigmoid, f_reg with annealed β, and 4-bit results.حجة مفكوك تايلور، وتجربة التقريب على ResNet-18، ودالة rectified sigmoid، والمنظِّم f_reg مع تخفيض β تدريجيًا، ونتائج 4 بت.(opens in a new tab)(يفتح في تبويب جديد)
- Quantizing Deep Convolutional Networks for Efficient Inference: A WhitepaperPaperRaghuraman Krishnamoorthi, arXiv 2018Source of the per-channel column on slides 37 and 38, and the QAT recovery numbers for MobileNet.مصدر عمود لكل قناة في الشريحتين 37 و38، وأرقام استعادة الدقة بـQAT لنماذج MobileNet.(opens in a new tab)(يفتح في تبويب جديد)
- Data-Free Quantization Through Weight Equalization and Bias CorrectionPaperNagel, van Baalen, Blankevoort, Welling. ICCV 2019Cross-layer equalization and bias correction for MobileNet-style models.المعادلة عبر الطبقات (cross-layer equalization) وتصحيح الانحياز (bias correction) للنماذج من نمط MobileNet.(opens in a new tab)(يفتح في تبويب جديد)
- A White Paper on Neural Network QuantizationPaperNagel et al., arXiv 2021Readable overview of the whole PTQ pipeline, including AdaRound, and when to switch to QAT.عرض عام سهل القراءة لمسار PTQ كله، بما في ذلك AdaRound، ومتى يجب الانتقال إلى QAT.(opens in a new tab)(يفتح في تبويب جديد)