Majid Al-RaimiAdaRound and what post-training INT8 achievesAdaRound وما يحققه INT8 بعد التدريب

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 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

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في نهاية هذا الجزء ستستطيع

  1. Explain why rounding every weight to its nearest grid value is not the best rounding for the layer output.اشرح لماذا لا يكون تقريب كل وزن إلى أقرب قيمة في الشبكة هو أفضل تقريب لمخرج الطبقة.
  2. 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.
  3. Show on a two-weight example how choosing floor or ceil per weight lowers the output error.بيّن على مثال بوزنين كيف يقلل اختيار الأرضية (floor) أو السقف (ceil) لكل وزن خطأ المخرج.
  4. Say what the rectified sigmoid and the regularizer f_reg do, and why the result is still post-training quantization.اذكر ما تفعله دالة rectified sigmoid والمنظِّم f_reg، ولماذا تظل النتيجة تكميمًا بعد التدريب.
  5. Fill the storage and computation table for FP, K-means and linear quantization, and list the four PTQ knobs.املأ جدول التخزين والحساب لكل من FP وK-means والتكميم الخطي، واذكر مفاتيح PTQ الأربعة.
  6. Read the INT8 PTQ table, name the models that lose most, explain why, and say what fixes it.اقرأ جدول نتائج INT8 PTQ، وسمِّ النماذج التي تخسر أكثر، واشرح السبب، واذكر ما يعالج ذلك.

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 تمامًا.

Two weights at 0.5 with x = [1, 1]: nearest sends both up and the output becomes 2; AdaRound sends one up and one down and the output stays 1وزنان قيمة كل منهما 0.5 مع x = [1, 1]: التقريب إلى الأقرب يرفع الاثنين فيصبح المخرج 2، بينما يرفع AdaRound أحدهما ويخفض الآخر فيبقى المخرج 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:

E[L(w+Δw)−L(w)]≈Δw⊤g+12 Δw⊤H Δw\mathbb{E}\big[\mathcal{L}(\mathbf{w}+\Delta\mathbf{w}) - \mathcal{L}(\mathbf{w})\big] \approx \Delta\mathbf{w}^{\top}\mathbf{g} + \tfrac{1}{2}\,\Delta\mathbf{w}^{\top}\mathbf{H}\,\Delta\mathbf{w}
g is the expected gradient and H the expected Hessian of the loss with respect to the weightsg التدرّج المتوقع وH مصفوفة Hessian المتوقعة للخسارة بالنسبة إلى الأوزان

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.

ExplorerمستكشفFlip each weight between floor and ceilبدّل كل وزن بين floor وceil
x1 = [1, 0, 1, 1]Wx = 1.20, W̃x = 1.00squared error 0.04
x2 = [-1, 1, 1, -1]Wx = 0.70, W̃x = 2.00squared error 1.69
Output error1.73sumnearest gives 1.73
Weight error0.47‖w − ŵ‖²what nearest minimizes
Best possible0.13of 16keep flipping

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]. قارن التقريب إلى الأقرب بأفضل تقريب.

The true output is 0.8. Nearest gives [0, 0], output 0, squared error 0.64. Rounding one weight up gives [1, 0], output 1, squared error 0.04. The weight error grows from 0.32 to 0.52, but the output error drops sixteenfold, and the output is what matters.
المخرج الحقيقي هو 0.8. التقريب إلى الأقرب يعطي [0, 0]، والمخرج 0، ومربع الخطأ 0.64. تقريب أحد الوزنين إلى أعلى يعطي [1, 0]، والمخرج 1، ومربع الخطأ 0.04. يزداد خطأ الأوزان من 0.32 إلى 0.52، لكن خطأ المخرج ينخفض ست عشرة مرة، والمخرج هو ما يهم.

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⌉}، أي نقطتي الشبكة المحيطتين بالوزن. ويكتب هذا الاختيار بإزاحة δ:

w~=⌊⌊w⌋+δ⌉,δ∈[0,1]\tilde{w} = \big\lfloor \lfloor w \rfloor + \delta \big\rceil,\qquad \delta \in [0, 1]
⌊·⌋ is floor and ⌊·⌉ is round to nearest; δ below 0.5 keeps the floor, δ of 0.5 or more gives the ceil⌊·⌋ تقريب للأسفل (floor) و⌊·⌉ تقريب لأقرب عدد؛ وδ أقل من 0.5 تُبقي floor، وδ تساوي 0.5 أو أكثر تعطي ceil

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):

arg⁡min⁡V  ∥Wx−W~x∥F2+λfreg(V)\arg\min_{\mathbf{V}} \;\big\lVert \mathbf{W}\mathbf{x} - \tilde{\mathbf{W}}\mathbf{x} \big\rVert_F^2 + \lambda f_{reg}(\mathbf{V})
AdaRound objective, general formهدف AdaRound، الصيغة العامة
→  arg⁡min⁡V  ∥Wx−⌊⌊W⌋+h(V)⌉ x∥F2+λfreg(V)\rightarrow\;\arg\min_{\mathbf{V}} \;\big\lVert \mathbf{W}\mathbf{x} - \big\lfloor \lfloor \mathbf{W} \rfloor + \mathbf{h}(\mathbf{V}) \big\rceil\,\mathbf{x} \big\rVert_F^2 + \lambda f_{reg}(\mathbf{V})
With W̃ written as floor plus a learned offset h(V)مع كتابة W̃ على أنها floor مضافًا إليها إزاحة متعلَّمة h(V)

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.:

h(V)=clip⁡(σ(V)(ζ−γ)+γ,  0,  1),ζ=1.1,  γ=−0.1h(V) = \operatorname{clip}\big(\sigma(V)(\zeta - \gamma) + \gamma,\; 0,\; 1\big),\qquad \zeta = 1.1,\; \gamma = -0.1
A sigmoid stretched slightly past 0 and 1, then clippedدالة sigmoid ممدودة قليلًا إلى ما بعد 0 و1، ثم مقصوصة
The rectified sigmoid (accent) is a sigmoid stretched to −0.1 and 1.1 and clipped, so it reaches exactly 0 near V = −2.4 and exactly 1 near V = 2.4; the plain sigmoid (dashed) never doesدالة rectified sigmoid (باللون المميَّز) هي sigmoid ممدودة إلى −0.1 و1.1 ثم مقصوصة، لذلك تبلغ 0 تمامًا قرب V = −2.4 و1 تمامًا قرب V = 2.4، أما sigmoid العادية (المتقطعة) فلا تبلغهما أبدًا

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، أي وزن عند نصف خطوة لا تستطيع أي شبكة أعداد صحيحة تخزينه. والمنظِّم يعالج ذلك:

freg(V)=∑i,j1−∣2h(Vi,j)−1∣βf_{reg}(\mathbf{V}) = \sum_{i,j} 1 - \lvert 2h(V_{i,j}) - 1 \rvert^{\beta}
Zero when every h is 0 or 1; largest when h = 0.5. β is annealed from high to lowصفر حين تكون كل h إما 0 أو 1؛ وأكبر ما تكون عند h = 0.5. وتُخفَّض β تدريجيًا من قيمة عالية إلى منخفضة
f_reg against h(V) for β = 20, 6 and 2: a high β is a flat plateau that lets h move freely early on; a low β is a peak that pushes every h to 0 or 1 by the endf_reg مقابل h(V) عند β = 20 و6 و2: قيمة β العالية هضبة مسطحة تسمح لـ h بالحركة بحرية في البداية، وقيمة β المنخفضة قمة تدفع كل h إلى 0 أو 1 في النهاية

Worked exampleمثال محلول

How f_reg scores one weightكيف يقيّم f_reg وزنًا واحدًا

  1. 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، وهي القيمة القصوى، أيًّا كانت β.
  2. 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. أي تقريبًا مثل الوزن غير المحسوم، لذلك لا يكاد المنظِّم يعمل، ويتولى حد إعادة البناء التوجيه.
  3. 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.
  4. 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}

  1. Targetالهدف

    Wx = 0.5 + 0.5 = 1.
    Wx = 0.5 + 0.5 = 1.
  2. 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.
  3. 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.
  4. 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.
  5. 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).

W: full-precision weights. x: the layer input from calibration data. ⌊W⌋: elementwise floor. V: learnable tensor, same shape as W. h(): rectified sigmoid mapping V into [0, 1], the soft rounding offset. ||·||²_F: squared Frobenius norm of the output error. λ: weight of the regularizer. f_reg: penalty that is zero only when each h(V) is 0 or 1.
W: الأوزان بالدقة الكاملة. x: مُدخل الطبقة من بيانات المعايرة. ⌊W⌋: الأرضية عنصرًا بعنصر. V: موتر قابل للتعلّم، بشكل W نفسه. h(): دالة rectified sigmoid تنقل V إلى [0, 1]، وهي إزاحة التقريب اللينة. ||·||²_F: مربع معيار فروبينيوس لخطأ المخرج. λ: وزن المنظِّم. f_reg: عقوبة تساوي صفرًا فقط عندما تكون كل h(V) مساوية 0 أو 1.

Recall

Why does AdaRound need f_reg at all, if the reconstruction loss already prefers good roundings?لماذا يحتاج AdaRound إلى f_reg أصلًا، إذا كانت خسارة إعادة البناء تفضّل التقريبات الجيدة بالفعل؟

Because h(V) is continuous. Left alone, the reconstruction loss is happiest with in-between values like 0.5, which are not on the grid. f_reg pushes every h(V) to exactly 0 (floor) or 1 (ceil), with β annealed so it is weak early and strong late.
لأن h(V) مستمرة. إذا تُركت وحدها، تكون خسارة إعادة البناء في أفضل حالاتها مع قيم وسطية مثل 0.5، وهي ليست على الشبكة. يدفع f_reg كل h(V) إلى 0 (الأرضية) أو 1 (السقف) تمامًا، مع تخفيض β تدريجيًا بحيث يكون ضعيفًا في البداية وقويًا في النهاية.

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-meansInteger 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التخزين والحساب
Storage and computation for the three approaches (slide 36)التخزين والحساب في الطرق الثلاث (الشريحة 36)

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كم قيمة تتشارك مقياسًا واحدًا SParts 1 and 2الجزءان 1 و2
Range clippingقص المدىEMA, minimize KL, minimize MSEEMA، تقليل KL، تقليل MSEWhich r_min and r_max set Sأي r_min وr_max يحددان SPart 3الجزء 3
RoundingالتقريبNearest, AdaRoundإلى الأقرب، AdaRoundWhich grid point each weight lands onأي نقطة في الشبكة يستقر عليها كل وزنThis partهذا الجزء
The four PTQ knobs and their options (slide 36)مفاتيح PTQ الأربعة وخياراتها (الشريحة 36)

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 والتكميم الخطي.

FP: floating-point weights, floating-point arithmetic. K-means: integer weights (indexes) plus a floating-point codebook, floating-point arithmetic. Linear: integer weights, integer arithmetic.
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%
MobileNetV1MobileNetV1not reportedلم يُذكر−11.8%−11.8%
MobileNetV2MobileNetV2not reportedلم يُذكر−2.1%−2.1%
Post-training INT8 accuracy change (slides 37 and 38)التغير في دقة INT8 بعد التدريب (الشريحتان 37 و38)
Accuracy drop under post-training INT8: large ImageNet models lose well under 2%, MobileNetV1 loses 11.8%انخفاض الدقة تحت تكميم INT8 بعد التدريب: نماذج ImageNet الكبيرة تخسر أقل من 2% بكثير، بينما يخسر MobileNetV1 نسبة 11.8%

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؟

It has far more parameters than it strictly needs, so rounding noise in individual weights averages out and is compensated by redundant weights. MobileNetV1 is compact by design, with less representational capacity to spare and depthwise filters whose ranges vary widely, so the same noise costs much more accuracy.
لديه معاملات أكثر بكثير مما يحتاجه فعلًا، لذلك تتلاشى بالمتوسط ضوضاء التقريب في الأوزان المفردة وتعوّضها الأوزان الزائدة. أما 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المصادر