Majid Al-RaimiQuantization-aware training: fake quantization and the straight-through estimatorالتدريب الواعي بالتكميم (QAT): التكميم الزائف والمقدّر المباشر (STE)

COE 592Lecture 5.3المحاضرة 5.3Part 05الجزء 05

Quantization-aware training: fake quantization and the straight-through estimatorالتدريب الواعي بالتكميم (QAT): التكميم الزائف والمقدّر المباشر (STE)

QAT fine-tunes the network with quantization simulated in the forward pass while a full-precision weight copy accumulates small gradient updates. Because rounding has zero gradient almost everywhere, the straight-through estimator passes gradients through as if quantization were the identity, and QAT recovers most of the INT8 accuracy that PTQ loses on MobileNets.يضبط QAT الشبكة ضبطًا دقيقًا مع محاكاة التكميم في التمرير الأمامي، بينما تراكم نسخة كاملة الدقة من الأوزان تحديثات تدرج صغيرة. ولأن للتقريب تدرجًا صفريًا في كل مكان تقريبًا، يمرر المقدّر المباشر (straight-through estimator) التدرجات كأن التكميم دالة هوية، ويستعيد QAT معظم دقة INT8 التي يفقدها PTQ على نماذج MobileNet.

Conceptsالمفاهيم
4
Slidesالشرائح
39-48
Readingمدة القراءة
24 min24 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

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

The last part ended on a bad number: INT8 post-training quantization drops MobileNetV1 to 0.1% top-1 with per-tensor scales. Clever choices of scale, clipping and rounding only go so far. The next lever is to let the network itself adapt to quantization by training with it in the loop.

The part follows one argument. First, why training helps and what it looks like for K-means quantization, where only the centroids move. Second, how linear quantization is put into the training graph as fake quantization while a full-precision weight copy absorbs the updates. Third, the obstacle: rounding has zero gradient, and the straight-through estimator that gets around it. Fourth, the payoff in measured accuracy. For the midterm, expect to compute a centroid update, explain the role of the FP copy, derive the zero gradient and write the STE rule, and read the PTQ versus QAT table.

انتهى الجزء السابق برقم سيئ: الـ INT8 post-training quantization يُسقط دقة MobileNetV1 إلى 0.1% في top-1 عند استخدام scale واحد لكل tensor (per-tensor). الاختيارات الذكية للـ scale والـ clipping والتقريب (rounding) لها حدود. الرافعة التالية هي أن نترك الشبكة نفسها تتكيّف مع الـ quantization بأن ندرّبها وهو داخل الحلقة.

يتبع هذا الجزء حجة واحدة. أولاً، لماذا يفيد التدريب، وكيف يبدو في حالة K-means quantization حيث تتحرك الـ centroids فقط. ثانياً، كيف يُدخَل الـ linear quantization في رسم التدريب (training graph) على شكل fake quantization، بينما تمتص نسخة الأوزان بالدقة الكاملة التحديثات. ثالثاً، العقبة: التقريب له gradient يساوي صفراً، والـ straight-through estimator هو ما يلتف عليها. رابعاً، المكسب في الدقة المقاسة. في الاختبار النصفي توقّع أن تحسب تحديث centroid، وأن تشرح دور نسخة الـ FP، وأن تشتق الـ gradient الصفري وتكتب قاعدة الـ STE، وأن تقرأ جدول المقارنة بين PTQ وQAT.

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

  1. Explain why aggressive quantization (4 bits and below) needs training with quantization in the loop, and why fine-tuning a pretrained FP model beats training from scratch.أن تشرح لماذا يحتاج الـ quantization الحاد (4 بتات فأقل) إلى تدريب يكون فيه الـ quantization داخل الحلقة، ولماذا يتفوق الـ fine-tuning لنموذج FP مدرَّب مسبقاً على التدريب من الصفر.
  2. Carry out one K-means QAT step: group gradients by cluster index, sum, scale by the learning rate and subtract from the centroids.أن تنفّذ خطوة واحدة من K-means QAT: تجميع الـ gradients حسب رقم الـ cluster، ثم جمعها، ثم ضربها في معدل التعلم وطرحها من الـ centroids.
  3. Describe fake (simulated) quantization: which tensors are quantized, which operations still run in full precision, and why a full-precision weight copy is kept.أن تصف الـ fake (simulated) quantization: أي الـ tensors تُكمَّم، وأي العمليات تبقى بالدقة الكاملة، ولماذا نحتفظ بنسخة من الأوزان بالدقة الكاملة.
  4. Show with the chain rule why the true gradient through a quantizer is zero, and write the straight-through estimator for weights and activations.أن تبيّن بقاعدة السلسلة (chain rule) لماذا يساوي الـ gradient الحقيقي عبر الـ quantizer صفراً، وأن تكتب الـ straight-through estimator للأوزان وللـ activations.
  5. Read the INT8 PTQ versus QAT table and explain why MobileNets gain the most.أن تقرأ جدول INT8 الذي يقارن PTQ مع QAT وتشرح لماذا تستفيد شبكات MobileNet أكثر من غيرها.

Take the 4×4 weight matrix you have quantized many times already and cluster it with K-means into four values: 2.00, 1.50, 0.00, -1.00. Every weight is now stored as a 2-bit cluster index, and the network only ever sees those four numbers. Its accuracy has dropped. Can a few more training steps win some of it back, even though the weights are no longer free to take any value?

خذ مصفوفة الأوزان 4×4 التي كمّمتها مرات عديدة من قبل، وجمّعها بالـ K-means في أربع قيم: 2.00, 1.50, 0.00, -1.00. أصبح كل وزن الآن مخزَّناً كرقم cluster من 2 بت، والشبكة لا ترى إلا هذه الأرقام الأربعة. لقد انخفضت دقتها. هل يمكن لبضع خطوات تدريب إضافية أن تستعيد جزءاً منها، رغم أن الأوزان لم تعد حرة في أخذ أي قيمة؟

Train with the quantizer, not after itدرّب مع الـ quantizer، لا بعده

Yes, and that is the whole idea of Quantization-aware training (QAT). In Post-training quantization (PTQ), the network was trained without ever seeing rounding, and we rounded it afterwards. The error that rounding adds is a surprise to every later layer. If the network is trained or fine-tuned while its weights and activations are already quantized, gradient descent can steer the remaining free parameters toward a solution that works well on the quantization grid. This matters most for aggressive settings, 4 bits and below, where the grid is so coarse that no choice of scale or rounding alone keeps accuracy.

نعم، وهذه هي فكرة Quantization-aware training (QAT) كلها. في Post-training quantization (PTQ) دُرّبت الشبكة دون أن ترى التقريب أبداً، ثم قرّبناها بعد ذلك. الخطأ الذي يضيفه التقريب يكون مفاجأة لكل طبقة لاحقة. أما إذا دُرّبت الشبكة أو أُجري لها fine-tuning وأوزانها وactivations مكمَّمة أصلاً، فإن الـ gradient descent يستطيع توجيه المعاملات الحرة المتبقية نحو حل يعمل جيداً على شبكة الـ quantization. هذا مهم أكثر ما يكون في الإعدادات الحادة، 4 بتات فأقل، حيث تكون الشبكة خشنة لدرجة أنه لا يوجد اختيار للـ scale أو للتقريب وحده يحافظ على الدقة.

The slides add a practical rule: fine-tune a pretrained floating-point model rather than training a quantized model from scratch. Krishnamoorthi reports the same finding in his INT8 study. The FP model has already found a good region of the loss surface with full freedom. QAT then only has to make small corrections so that the quantized version of that solution works, which is a much easier problem than searching the whole space through a coarse grid.

تضيف الشرائح قاعدة عملية: اعمل fine-tuning لنموذج floating-point مدرَّب مسبقاً بدلاً من تدريب نموذج مكمَّم من الصفر. ويذكر Krishnamoorthi النتيجة نفسها في دراسته عن INT8. نموذج الـ FP وجد أصلاً منطقة جيدة من سطح الخسارة (loss surface) وهو يتمتع بحرية كاملة. وبعدها لا يحتاج الـ QAT إلا إلى تصحيحات صغيرة كي تعمل النسخة المكمَّمة من ذلك الحل، وهذه مسألة أسهل بكثير من البحث في الفضاء كله عبر شبكة خشنة.

QAT for K-means quantization: only the centroids learnQAT في K-means quantization: الـ centroids وحدها تتعلم

With K-means quantization the cluster assignment is frozen. What can still change is the codebook, the four centroid values. Every weight in cluster kk equals the centroid ckc_k, so by the chain rule the gradient of the loss with respect to ckc_k is the sum of the gradients of all weights that point to it. That gives a four-step recipe from Deep Compression (Han et al., ICLR 2016): compute the usual weight gradients, group them by cluster index, reduce each group by summing, then multiply by the learning rate and subtract from the centroid.

في K-means quantization يكون توزيع الأوزان على الـ clusters مجمَّداً. ما يمكن أن يتغير هو الـ codebook، أي قيم الـ centroids الأربع. كل وزن في الـ cluster رقم kk يساوي الـ centroid ckc_k، لذلك بقاعدة السلسلة يكون gradient الخسارة بالنسبة إلى ckc_k هو مجموع gradients كل الأوزان التي تشير إليه. هذا يعطينا وصفة من أربع خطوات من Deep Compression (Han et al., ICLR 2016): احسب gradients الأوزان المعتادة، وجمّعها حسب رقم الـ cluster، واختزل كل مجموعة بالجمع، ثم اضرب في معدل التعلم واطرح من الـ centroid.

ck←ck−η∑i: Ii=k∂L∂Wic_k \leftarrow c_k - \eta \sum_{i:\,I_i = k} \frac{\partial L}{\partial W_i}
Centroid update: sum the gradients of every weight with index kتحديث المركز: اجمع تدرجات كل وزن فهرسه k

Worked exampleمثال محلول

One centroid update from slide 40 (lr = 1)تحديث centroid واحد من الشريحة 40 (lr = 1)

  1. Group by indexالتجميع حسب الرقم

    The cluster index matrix puts five weights in cluster 1. Their gradients are 0.02, -0.01, 0.01, 0.04, -0.02.
    مصفوفة أرقام الـ clusters تضع خمسة أوزان في الـ cluster رقم 1. وgradients هذه الأوزان هي 0.02, -0.01, 0.01, 0.04, -0.02.
  2. Reduceالاختزال

    Sum them: 0.02 - 0.01 + 0.01 + 0.04 - 0.02 = 0.04.
    اجمعها: 0.02 - 0.01 + 0.01 + 0.04 - 0.02 = 0.04.
  3. Scale and subtractالضرب ثم الطرح

    With lr = 1, the centroid moves from 0.00 to 0.00 - 0.04 = -0.04.
    مع lr = 1 ينتقل الـ centroid من 0.00 إلى 0.00 - 0.04 = -0.04.
  4. Resultالنتيجة

    Doing the same for every cluster turns [2.00, 1.50, 0.00, -1.00] into [1.96, 1.48, -0.04, -0.97]. Note the sign: cluster 0 has a negative sum -0.03, so its centroid rises.
    تطبيق الشيء نفسه على كل cluster يحوّل [2.00, 1.50, 0.00, -1.00] إلى [1.96, 1.48, -0.04, -0.97]. انتبه للإشارة: الـ cluster رقم 0 مجموعه سالب -0.03، لذلك يرتفع الـ centroid الخاص به.
IndexالرقمGrouped gradientsالـ gradients المجمَّعةSumالمجموعCentroid beforeالـ centroid قبلCentroid afterالـ centroid بعد
33-0.03, 0.12, 0.02, -0.07-0.03, 0.12, 0.02, -0.070.040.042.002.001.961.96
220.03, 0.01, -0.020.03, 0.01, -0.020.020.021.501.501.481.48
110.02, -0.01, 0.01, 0.04, -0.020.02, -0.01, 0.01, 0.04, -0.020.040.040.000.00-0.04-0.04
00-0.01, -0.02, -0.01, 0.01-0.01, -0.02, -0.01, 0.01-0.03-0.03-1.00-1.00-0.97-0.97
All four clusters of slide 40, with lr = 1الـ clusters الأربعة في الشريحة 40، مع lr = 1
Gradients grouped by cluster, reduced by summation, and subtracted from each centroidالـ gradients مجمَّعة حسب الـ cluster، ثم مختزلة بالجمع، ثم مطروحة من كل centroid

Recall

Why should QAT start from a pretrained FP model, and what changes during K-means QAT?لماذا يجب أن يبدأ الـ QAT من نموذج FP مدرَّب مسبقاً، وما الذي يتغير أثناء K-means QAT؟

The FP model already sits in a good region of the loss surface, so QAT only makes small corrections; training through the coarse grid from scratch reaches lower accuracy. In K-means QAT the cluster indices stay fixed and only the centroids change, each by lr times the sum of its members' gradients.

نموذج الـ FP موجود أصلاً في منطقة جيدة من سطح الخسارة، لذلك لا يجري الـ QAT إلا تصحيحات صغيرة؛ أما التدريب عبر الشبكة الخشنة من الصفر فيصل إلى دقة أقل. في K-means QAT تبقى أرقام الـ clusters ثابتة ولا تتغير إلا الـ centroids، كل منها بمقدار lr مضروباً في مجموع gradients أعضائه.

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

Cluster 0 has centroid -1.00 and grouped gradients -0.01, -0.02, -0.01 and 0.01. With lr = 1, what is the new centroid?الـ cluster رقم 0 قيمة الـ centroid فيه -1.00، والـ gradients المجمَّعة فيه هي -0.01 و-0.02 و-0.01 و0.01. مع lr = 1، ما قيمة الـ centroid الجديدة؟

Now switch to linear quantization. Suppose one weight is w = 1.48 on the 2-bit grid of slide 43, whose step is S = 1.07. It rounds to the level 1.07. A typical SGD update is tiny, say 0.005. If we stored only the quantized weight and applied the update to it, we would get 1.075, which rounds straight back to 1.07. Every update is erased, forever.

لننتقل الآن إلى الـ linear quantization. افترض أن وزناً واحداً قيمته w = 1.48 على شبكة الـ 2 بت في الشريحة 43، وخطوتها S = 1.07. هذا الوزن يُقرَّب إلى المستوى 1.07. تحديث SGD المعتاد صغير جداً، وليكن 0.005. لو خزّنّا الوزن المكمَّم فقط وطبقنا التحديث عليه لحصلنا على 1.075، وهذه تُقرَّب مباشرة إلى 1.07 من جديد. كل تحديث يُمحى، وإلى الأبد.

Keep a full-precision copy and quantize on the flyاحتفظ بنسخة بالدقة الكاملة وكمّم أثناء التشغيل

The fix is to keep a Full-precision weight copy of the weights WW throughout training. The optimizer updates this copy, so small steps accumulate without being rounded away. Every forward pass then quantizes the current copy into Q(W)Q(W) and uses that. In the example, w = 1.48 must reach the rounding boundary halfway between 1.07 and 2.14, at 1.5 × 1.07 = 1.605. That is 0.125 away, so after about 25 steps of 0.005 the quantized weight jumps to 2.14. The many small steps were not lost, they were stored up. Once training ends the copy is thrown away and only the quantized weights are used for inference.

الحل هو الاحتفاظ بـ Full-precision weight copy للأوزان WW طوال التدريب. الـ optimizer يحدّث هذه النسخة، فتتراكم الخطوات الصغيرة دون أن يمحوها التقريب. ثم في كل forward pass نكمّم النسخة الحالية إلى Q(W)Q(W) ونستخدمها. في المثال، يجب أن يصل w = 1.48 إلى حد التقريب الواقع في منتصف المسافة بين 1.07 و2.14، أي عند 1.5 × 1.07 = 1.605. المسافة 0.125، لذلك بعد نحو 25 خطوة كل منها 0.005 يقفز الوزن المكمَّم إلى 2.14. الخطوات الصغيرة الكثيرة لم تضِع، بل كانت تُدَّخر. وعند انتهاء التدريب تُرمى النسخة ولا تُستخدم في الـ inference إلا الأوزان المكمَّمة.

Fake quantization: discrete values, floating-point arithmeticالـ fake quantization: قيم متقطعة وحساب بالـ floating point

How is Q(W)Q(W) computed? It is linear quantization followed immediately by dequantization, all in floating point: W→SW qW=Q(W)W \to S_W\, q_W = Q(W). The result is still an FP tensor, but every entry lies exactly on the quantization grid. This is called Simulated (fake) quantization, or simulated quantization, because no integer hardware is involved. Slide 43 shows the arithmetic with the zero point included.

كيف تُحسب Q(W)Q(W)؟ هي linear quantization يتبعه مباشرة dequantization، وكل ذلك بالـ floating point: W→SW qW=Q(W)W \to S_W\, q_W = Q(W). الناتج ما زال tensor من نوع FP، لكن كل عنصر فيه يقع تماماً على شبكة الـ quantization. يسمى هذا Simulated (fake) quantization، أي quantization محاكى، لأنه لا يستخدم أي عتاد للأعداد الصحيحة. الشريحة 43 تعرض الحساب مع إدخال الـ zero point.

Q(W)=S (qW−Z),qW=clamp⁡ ⁣(round⁡(W/S)+Z, qmin⁡, qmax⁡)Q(W) = S\,(q_W - Z),\qquad q_W = \operatorname{clamp}\!\big(\operatorname{round}(W/S) + Z,\ q_{\min},\ q_{\max}\big)
Quantize, then dequantize, without leaving floating pointكمِّم ثم ألغِ التكميم، دون مغادرة الفاصلة العائمة

Worked exampleمثال محلول

Slide 43: the 2-bit Q(W) for the 4×4 matrixالشريحة 43: قيم Q(W) بـ 2 بت للمصفوفة 4×4

  1. Scale and zero pointالـ scale والـ zero point

    The weights span [-1.08, 2.12] and 2-bit signed codes span [-2, 1], so S = 3.20 / 3 ≈ 1.07 and Z = round(-2 + 1.08/1.07) = round(-0.99) = -1.
    تمتد الأوزان على [-1.08, 2.12]، وتمتد الرموز الموقّعة (signed codes) بـ 2 بت على [-2, 1]، لذلك S = 3.20 / 3 ≈ 1.07 وZ = round(-2 + 1.08/1.07) = round(-0.99) = -1.
  2. Codesالرموز

    The first row [2.09, -0.98, 1.48, 0.09] gives q_W = [1, -2, 0, -1]. For example 1.48 / 1.07 ≈ 1.38, which rounds to 1, and adding Z = -1 gives 0.
    الصف الأول [2.09, -0.98, 1.48, 0.09] يعطي q_W = [1, -2, 0, -1]. مثلاً 1.48 / 1.07 ≈ 1.38، وهذه تُقرَّب إلى 1، وبإضافة Z = -1 نحصل على 0.
  3. Dequantizeالـ dequantization

    (q_W - Z) × S = [2, -1, 1, 0] × 1.07 = [2.14, -1.07, 1.07, 0].
    (q_W - Z) × S = [2, -1, 1, 0] × 1.07 = [2.14, -1.07, 1.07, 0].
  4. Resultالنتيجة

    The full Q(W)Q(W) has rows [2.14, -1.07, 1.07, 0], [0, 0, -1.07, 2.14], [-1.07, 2.14, 0, -1.07] and [2.14, 0, 1.07, 1.07]. Only four values occur: -1.07, 0, 1.07, 2.14. This is what the layer computes with during QAT.
    صفوف Q(W)Q(W) الكاملة هي [2.14, -1.07, 1.07, 0] و[0, 0, -1.07, 2.14] و[-1.07, 2.14, 0, -1.07] و[2.14, 0, 1.07, 1.07]. لا تظهر إلا أربع قيم: -1.07, 0, 1.07, 2.14. وهذا ما تحسب به الطبقة أثناء الـ QAT.

Activations get the same treatment. The output YY of a layer is fake-quantized as Y→SY(qY−ZY)=Q(Y)Y \to S_Y (q_Y - Z_Y) = Q(Y) before it is passed on, so the next layer receives Q(X)Q(X) as its input. The scale of the activations is not known from the weights; it is tracked during training, typically with an Exponential moving average (EMA) of the observed range.

الـ activations تُعامَل بالطريقة نفسها. مخرج الطبقة YY يمر بـ fake quantization على الشكل Y→SY(qY−ZY)=Q(Y)Y \to S_Y (q_Y - Z_Y) = Q(Y) قبل أن يُمرَّر، فتستقبل الطبقة التالية Q(X)Q(X) كمدخل لها. الـ scale الخاص بالـ activations لا يُعرف من الأوزان؛ بل يُتتبَّع أثناء التدريب، عادةً باستخدام Exponential moving average (EMA) للمدى المرصود.

Inputالدخل
from layer N-1من الطبقة N-1

Arrives already fake-quantized as Q(X).

يصل وقد خضع مسبقًا للتكميم الزائف (fake quantization) بصورة Q(X).

Q(X)
Layer Nالطبقة N
full precisionبالدقة الكاملة

Conv, batch norm and ReLU run in FP, using Q(W) from the FP weight copy W.

تعمل Conv وbatch norm وReLU بالفاصلة العائمة (FP)، باستخدام Q(W) المأخوذة من نسخة الأوزان W بصيغة FP.

Y
Activation quantizationتكميم التنشيطات
fake quantتكميم زائف

Y becomes S_Y (q_Y - Z_Y) = Q(Y).

تصبح Y هي S_Y (q_Y - Z_Y) = Q(Y).

Q(Y)
Outputالخرج
to layer N+1إلى الطبقة N+1

Discrete values, exactly as integer inference will produce them.

قيم متقطعة، تمامًا كما سينتجها الاستدلال بالأعداد الصحيحة (integer inference).

Forward pass of one layer in QAT: quantize at the boundaries, compute in full precision insideالـ forward pass لطبقة واحدة في QAT: نكمّم عند الحدود، ونحسب بالدقة الكاملة في الداخل
Tensor or operationالـ tensor أو العمليةWhat happensما يحدثValue usedالقيمة المستخدمة
Weights Wالأوزان WFull precision copy, updated by the optimizerنسخة بالدقة الكاملة يحدّثها الـ optimizerQ(W) = S_W q_WQ(W) = S_W q_W
Layer input Xمدخل الطبقة XAlready quantized by the previous layerمكمَّم أصلاً بواسطة الطبقة السابقةQ(X)Q(X)
Conv, batch norm, ReLUconv و batch norm و ReLURun in full precision on the fake-quantized valuesتعمل بالدقة الكاملة على القيم المكمَّمة بالـ fake quantizationNot quantized inside the blockلا تُكمَّم داخل الكتلة
Layer output Yمخرج الطبقة YFull precision result of the blockنتيجة الكتلة بالدقة الكاملةQ(Y) = S_Y (q_Y - Z_Y)Q(Y) = S_Y (q_Y - Z_Y)
What is quantized where during the QAT forward passما الذي يُكمَّم وأين أثناء الـ forward pass في QAT

Recall

Why must QAT keep a full-precision copy of the weights?لماذا يجب أن يحتفظ الـ QAT بنسخة من الأوزان بالدقة الكاملة؟

Gradient updates are usually much smaller than half a quantization step. Applied to a quantized weight, each one rounds straight back to the same level and is lost. The FP copy accumulates them until the weight crosses a rounding boundary, and only then does Q(W) change.

تحديثات الـ gradient عادةً أصغر بكثير من نصف خطوة quantization. إذا طُبّق كل منها على وزن مكمَّم فإنه يُقرَّب مباشرة إلى المستوى نفسه ويضيع. نسخة الـ FP تراكمها حتى يعبر الوزن حدّ تقريب، وعندها فقط تتغير Q(W).

Recall

During fake quantization, which tensors are quantized and which operations stay in full precision?أثناء الـ fake quantization، أي الـ tensors تُكمَّم وأي العمليات تبقى بالدقة الكاملة؟

The weights (Q(W) = S_W q_W) and the layer outputs (Q(Y) = S_Y (q_Y - Z_Y)) are quantized, so inputs to the next layer are Q(X). Conv, batch norm and ReLU inside the layer still run in full precision.

تُكمَّم الأوزان (Q(W) = S_W q_W) ومخرجات الطبقات (Q(Y) = S_Y (q_Y - Z_Y))، فتكون مدخلات الطبقة التالية Q(X). أما conv وbatch norm وReLU داخل الطبقة فتبقى تعمل بالدقة الكاملة.

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

During quantization-aware training, which values are stored in full precision and updated by the optimizer?أثناء الـ quantization-aware training، ما القيم المخزَّنة بالدقة الكاملة والتي يحدّثها الـ optimizer؟

Plot Q(w)=round⁡(w)Q(w) = \operatorname{round}(w) for w from 0 to 5. You get a staircase. Pick any point, say w = 2.3, and nudge it slightly: Q stays at 2. The slope there is exactly zero. The same is true at every point except the jumps at 0.5, 1.5, 2.5, …, where the function is not differentiable at all.

ارسم Q(w)=round⁡(w)Q(w) = \operatorname{round}(w) لقيم w من 0 إلى 5. ستحصل على درَج. اختر أي نقطة، ولتكن w = 2.3، وحرّكها قليلاً: تبقى Q عند 2. الميل هناك يساوي صفراً تماماً. والأمر نفسه صحيح عند كل نقطة ما عدا القفزات عند 0.5, 1.5, 2.5, …، حيث الدالة غير قابلة للاشتقاق أصلاً.

The true gradient kills learningالـ gradient الحقيقي يقتل التعلم

Backpropagation reaches the weights through the quantizer, so the chain rule multiplies by that slope.

الـ backpropagation يصل إلى الأوزان عبر الـ quantizer، لذلك تضرب قاعدة السلسلة في ذلك الميل.

gW=∂L∂W=∂L∂Q(W)⋅∂Q(W)∂W=∂L∂Q(W)⋅0=0g_W = \frac{\partial L}{\partial W} = \frac{\partial L}{\partial Q(W)}\cdot\frac{\partial Q(W)}{\partial W} = \frac{\partial L}{\partial Q(W)}\cdot 0 = 0
Quantization is discrete-valued, so its derivative is zero almost everywhereالتكميم متقطع القيم، لذلك مشتقته صفر تقريبًا في كل مكان

The layer above still produces a useful signal ∂L/∂Q(W)\partial L/\partial Q(W): it knows whether the effective weight should be larger or smaller. But the zero factor wipes it out. The FP copy would receive a zero gradient on every step and never move. Slide 44 posed exactly this question: how should gradients back-propagate through the simulated quantization?

الطبقة التي فوقها ما زالت تنتج إشارة مفيدة ∂L/∂Q(W)\partial L/\partial Q(W): فهي تعرف هل يجب أن يكون الوزن الفعلي أكبر أم أصغر. لكن العامل الصفري يمحوها. ستستقبل نسخة الـ FP gradient صفرياً في كل خطوة ولن تتحرك أبداً. الشريحة 44 طرحت هذا السؤال بالضبط: كيف يجب أن تنتشر الـ gradients للخلف عبر الـ quantization المحاكى؟

Pretend the quantizer is the identity on the way backتظاهَر بأن الـ quantizer هو دالة الهوية في طريق العودة

The Straight-through estimator (STE) answers it with a deliberate approximation. In the forward pass, quantize as usual. In the backward pass, treat the quantizer as if it were the identity function, the dashed diagonal through the staircase, whose slope is 1. The gradient arriving at Q(W)Q(W) is passed straight through to WW. The idea goes back to Hinton's 2012 Coursera lectures and was analysed by Bengio, Léonard and Courville in 2013.

يجيب Straight-through estimator (STE) عن هذا السؤال بتقريب متعمَّد. في الـ forward pass نكمّم كالمعتاد. وفي الـ backward pass نعامل الـ quantizer كأنه دالة الهوية (identity)، أي القطر المتقطع المار عبر الدرَج، وميله 1. الـ gradient الواصل إلى Q(W)Q(W) يُمرَّر مباشرة إلى WW. تعود الفكرة إلى محاضرات Hinton على Coursera عام 2012، وحلّلها Bengio وLéonard وCourville عام 2013.

gW=∂L∂W≈∂L∂Q(W),gY←∂L∂Q(Y)g_W = \frac{\partial L}{\partial W} \approx \frac{\partial L}{\partial Q(W)}, \qquad g_Y \leftarrow \frac{\partial L}{\partial Q(Y)}
STE for weights and for activationsSTE للأوزان وللتنشيطات
Every tread of round(w) has slope 0; the STE uses the dashed identity line, slope 1, for the backward pass onlyكل درجة في round(w) ميلها 0؛ والـ STE يستخدم خط الهوية المتقطع، وميله 1، في الـ backward pass فقط

Why is this a reasonable lie? Over a range of many steps the staircase follows the identity line closely: raising ww by one step raises Q(w)Q(w) by one step on average. The STE uses that average slope instead of the local one. Combined with the FP copy, it gives exactly the behaviour you want: the copy drifts in the direction the loss prefers, and Q(W)Q(W) jumps to the next level once the drift is large enough.

لماذا هذه كذبة معقولة؟ على مدى خطوات كثيرة يتبع الدرَج خط الهوية عن قرب: رفع ww بمقدار خطوة واحدة يرفع Q(w)Q(w) بخطوة واحدة في المتوسط. الـ STE يستخدم ذلك الميل المتوسط بدلاً من الميل المحلي. ومع نسخة الـ FP يعطي بالضبط السلوك المطلوب: تنجرف النسخة في الاتجاه الذي تفضّله الخسارة، وتقفز Q(W)Q(W) إلى المستوى التالي عندما يكبر الانجراف بما يكفي.

Watch it work on a single weight. Loss L = ½ (Q(w) - 2.3)² wants the effective weight to be 2.3, but the grid only offers integers. Switch between the true gradient and the STE, and step the training loop.

شاهدها تعمل على وزن واحد. الخسارة L = ½ (Q(w) - 2.3)² تريد أن يكون الوزن الفعلي 2.3، لكن الشبكة لا تقدم إلا أعداداً صحيحة. بدّل بين الـ gradient الحقيقي والـ STE، وتقدّم في حلقة التدريب خطوة خطوة.

SimulatorمحاكيOne weight trained through a quantizerوزن واحد يُدرَّب عبر مكمِّم
Step0of 60Q(w) jumped 0 timesقفزت Q(w) 0 مرة
FP copy w0.300FP32the teal dot and tick trailالنقطة الخضراء المزرقة وأثر العلامات خلفها
Q(w)0levelwhat the forward pass seesما يراه المرور الأمامي (forward pass)
Gradient g_w-2.30= -2.30 x 1dL/dQ(w) times the backward slopedL/dQ(w) مضروبًا في ميل المرور الخلفي

Loss L = ½ (Q(w) - 2.3)², so dL/dQ(w) = Q(w) - 2.3. Update w ← w - 0.15 g_w starting from w = 0.3. With the true gradient the weight never moves. With STE it climbs, Q(w) jumps from level to level, and w ends up hovering at the rounding boundary 2.5 because neither level 2 nor 3 equals the target.الخسارة L = ½ (Q(w) - 2.3)²، لذلك dL/dQ(w) = Q(w) - 2.3. التحديث هو w ← w - 0.15 g_w بدءًا من w = 0.3. مع التدرج الحقيقي لا يتحرك الوزن أبدًا. أما مع STE فإنه يصعد، وتقفز Q(w) من مستوى إلى آخر، وينتهي الأمر بـ w وهو يحوم عند حد التقريب 2.5 لأن أيًّا من المستويين 2 و3 لا يساوي الهدف.

With the true gradient nothing ever happens. With the STE the FP copy climbs, Q(w)Q(w) jumps from 0 to 1 to 2, and then the copy settles at the boundary 2.5, flipping between levels 2 and 3. Krishnamoorthi observed the same effect in real networks: during QAT the FP weights converge to the quantization decision boundaries.

مع الـ gradient الحقيقي لا يحدث شيء أبداً. ومع الـ STE تصعد نسخة الـ FP، وتقفز Q(w)Q(w) من 0 إلى 1 إلى 2، ثم تستقر النسخة عند الحد 2.5 وتتأرجح بين المستويين 2 و3. لاحظ Krishnamoorthi الأثر نفسه في الشبكات الحقيقية: أثناء الـ QAT تتقارب أوزان الـ FP نحو حدود قرار الـ quantization.

Recall

Derive why the true gradient through a quantizer is zero, then write the STE.اشتق لماذا يساوي الـ gradient الحقيقي عبر الـ quantizer صفراً، ثم اكتب الـ STE.

g_W = ∂L/∂W = ∂L/∂Q(W) · ∂Q(W)/∂W. Rounding is flat between jumps, so ∂Q(W)/∂W = 0 almost everywhere, hence g_W = 0 and nothing learns. The STE treats Q as the identity in the backward pass: g_W = ∂L/∂Q(W), and for activations g_Y ← ∂L/∂Q(Y).

g_W = ∂L/∂W = ∂L/∂Q(W) · ∂Q(W)/∂W. التقريب مسطّح بين القفزات، لذلك ∂Q(W)/∂W = 0 تقريباً في كل مكان، ومن ثم g_W = 0 ولا يتعلم شيء. الـ STE يعامل Q كدالة الهوية في الـ backward pass: g_W = ∂L/∂Q(W)، وللـ activations g_Y ← ∂L/∂Q(Y).

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

Under the straight-through estimator, what is g_W for a weight inside the clipping range when dL/dQ(W) = 0.3?مع الـ straight-through estimator، ما قيمة g_W لوزن داخل مدى الـ clipping عندما تكون dL/dQ(W) = 0.3؟

Return to the worst number in this lecture. MobileNetV1 has 70.9% top-1 accuracy in floating point. INT8 PTQ with asymmetric Per-tensor quantization collapses it to 0.1%, which is chance level on ImageNet. Fine-tune the same model with QAT and the same per-tensor scheme reaches 70.0%.

لنعد إلى أسوأ رقم في هذه المحاضرة. دقة MobileNetV1 في top-1 هي 70.9% بالـ floating point. الـ INT8 PTQ مع Per-tensor quantization غير المتماثل (asymmetric) يهوي بها إلى 0.1%، وهو مستوى التخمين العشوائي على ImageNet. أجرِ fine-tuning للنموذج نفسه بالـ QAT فيصل المخطط نفسه (per-tensor) إلى 70.0%.

NetworkالشبكةFPFPPTQ asym per-tensorPTQ asym per-tensorPTQ sym per-channelPTQ sym per-channelQAT asym per-tensorQAT asym per-tensorQAT sym per-channelQAT sym per-channel
MobileNetV1MobileNetV170.970.90.10.159.159.170.070.070.770.7
MobileNetV2MobileNetV271.971.90.10.169.869.870.970.971.171.1
NASNet-MobileNASNet-Mobile74.9 (paper: 74.0)74.9 (في الورقة: 74.0)72.272.272.172.173.073.073.073.0
INT8 ImageNet top-1 (%), from Krishnamoorthi (2018) as shown on slide 47دقة top-1 على ImageNet عند INT8 (%)، من Krishnamoorthi (2018) كما تظهر في الشريحة 47
MobileNetV1 at INT8: PTQ collapses or loses 11.8 points, QAT lands within 0.2 to 0.9 points of FPMobileNetV1 عند INT8: الـ PTQ إما ينهار أو يخسر 11.8 نقطة، والـ QAT يصل إلى ما بين 0.2 و0.9 نقطة من الـ FP

Reading the tableقراءة الجدول

  • QAT recovers most of the loss. With Symmetric quantization and Per-channel quantization, MobileNetV1 goes from 59.1% to 70.7%, a gain of 11.6 points, ending only 0.2 below FP. MobileNetV2 goes from 69.8% to 71.1%.الـ QAT يستعيد معظم الخسارة. مع Symmetric quantization وPer-channel quantization ينتقل MobileNetV1 من 59.1% إلى 70.7%، أي مكسب قدره 11.6 نقطة، لينتهي أقل من الـ FP بـ 0.2 فقط. وينتقل MobileNetV2 من 69.8% إلى 71.1%.
  • Training makes simple schemes viable. Per-tensor asymmetric quantization is unusable after training (0.1%) but within 1 point of FP with QAT. The network learns to live with one shared scale.التدريب يجعل المخططات البسيطة صالحة للاستخدام. الـ quantization غير المتماثل لكل tensor غير قابل للاستخدام بعد التدريب (0.1%)، لكنه يصبح ضمن 1 نقطة من الـ FP مع الـ QAT. الشبكة تتعلم أن تتعايش مع scale واحد مشترك.
  • The models PTQ hurts most gain the most. NASNet-Mobile was already fine after PTQ (72.2%), so QAT adds less than 1 point.النماذج التي يضرّها الـ PTQ أكثر هي التي تكسب أكثر. كان NASNet-Mobile جيداً أصلاً بعد الـ PTQ (72.2%)، لذلك يضيف الـ QAT أقل من 1 نقطة.
  • Why MobileNets suffer, and so gain the most. Krishnamoorthi traces almost all of the loss to weight quantization. Batch norm is folded into the conv weights for inference, and its per-channel scaling makes the weight ranges of different kernels in one layer vary enormously. A single per-tensor scale is then sized for the widest kernel, so narrow kernels get only a handful of the 256 levels, or round to zero. Compact networks with few parameters, like MobileNets with their depthwise convolutions, have little redundancy to absorb that error, while larger networks such as ResNets are more robust. Per-channel scales sidestep the range problem, and QAT lets the weights adapt to whatever grid remains.لماذا تعاني شبكات MobileNet، ولذلك تكسب أكثر. يُرجع Krishnamoorthi معظم الخسارة تقريباً إلى quantization الأوزان. الـ batch norm يُدمج (folded) في أوزان الـ conv من أجل الـ inference، والتحجيم الخاص بكل قناة فيه يجعل مدى أوزان الـ kernels المختلفة في الطبقة الواحدة يتفاوت تفاوتاً هائلاً. عندها يُحدَّد الـ scale الواحد لكل tensor على مقاس أعرض kernel، فلا تحصل الـ kernels الضيقة إلا على عدد قليل من المستويات الـ 256، أو تُقرَّب إلى صفر. الشبكات المدمجة ذات المعاملات القليلة، مثل MobileNet بطبقات الـ depthwise convolutions، فيها تكرار (redundancy) قليل يمتص ذلك الخطأ، بينما الشبكات الأكبر مثل ResNet أكثر متانة. الـ scales لكل قناة (per-channel) تتجاوز مشكلة المدى، والـ QAT يسمح للأوزان بالتكيف مع أي شبكة متبقية.
  • Two variables change between columns. Each pair of columns changes both symmetry and granularity, so the table compares recipes, not single factors.متغيران يتغيران بين الأعمدة. كل زوج من الأعمدة يغيّر التماثل (symmetry) والدقة الحبيبية (granularity) معاً، لذلك يقارن الجدول وصفات كاملة، لا عوامل منفردة.

Where this leaves usأين يتركنا هذا

Slide 48 repeats the summary table of the two quantization families and points an arrow at a question mark. K-means quantization stores integer indices but still computes in floating point after a codebook lookup. Linear quantization stores integers and computes with integers. QAT works for both, with a different update rule for each. The question mark asks how far the bit width can go. The next part pushes it to the limit, a single bit, with Binarization, where the same STE idea is what makes training possible.

تكرر الشريحة 48 جدول الملخص لعائلتي الـ quantization وتوجّه سهماً نحو علامة استفهام. الـ K-means quantization يخزّن أرقاماً صحيحة (indices) لكنه ما زال يحسب بالـ floating point بعد البحث في الـ codebook. الـ linear quantization يخزّن أعداداً صحيحة ويحسب بأعداد صحيحة. والـ QAT يعمل مع كليهما، بقاعدة تحديث مختلفة لكل منهما. علامة الاستفهام تسأل: إلى أي حد يمكن أن ينخفض عدد البتات؟ الجزء التالي يدفعه إلى أقصى حد، بت واحد، مع binarization، حيث فكرة الـ STE نفسها هي ما يجعل التدريب ممكناً.

Floating pointFloating pointK-means quantizationK-means quantizationLinear quantizationLinear quantization
StorageالتخزينFP weightsأوزان FPInteger indices plus an FP codebookأرقام صحيحة (indices) مع codebook بالـ FPInteger weightsأوزان صحيحة
ComputationالحسابFP arithmeticحساب بالـ FPFP arithmetic (look up, then multiply)حساب بالـ FP (بحث في الـ codebook ثم ضرب)Integer arithmeticحساب بالأعداد الصحيحة
How QAT trains itكيف يدرّبه الـ QATNot applicableلا ينطبقSum gradients per cluster, update centroidsجمع الـ gradients لكل cluster ثم تحديث الـ centroidsFake quantization plus STE on an FP copyfake quantization مع STE على نسخة FP
The two families from slide 48, with how QAT trains eachالعائلتان من الشريحة 48، مع طريقة تدريب الـ QAT لكل منهما

Recall

Compare PTQ and QAT for MobileNetV1 at INT8 in both schemes on the slide.قارن بين PTQ و QAT لـ MobileNetV1 عند INT8 في المخططين الموجودين على الشريحة.

FP is 70.9%. Asymmetric per-tensor: PTQ 0.1%, QAT 70.0%. Symmetric per-channel: PTQ 59.1%, QAT 70.7%. QAT closes the gap to within 0.2 to 0.9 points and even makes per-tensor quantization usable.

الـ FP هو 70.9%. في asymmetric per-tensor: PTQ 0.1% وQAT 70.0%. في symmetric per-channel: PTQ 59.1% وQAT 70.7%. الـ QAT يضيّق الفجوة إلى ما بين 0.2 و0.9 نقطة، بل ويجعل الـ per-tensor quantization قابلاً للاستخدام.

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

For MobileNetV1 with symmetric per-channel INT8, how many top-1 points does QAT gain over PTQ?لـ MobileNetV1 مع INT8 بمخطط symmetric per-channel، كم نقطة في top-1 يكسبها الـ QAT مقارنة بالـ PTQ؟

Recapالخلاصة

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

  • PTQ is fine at 8 bits for big models, but small models and 4 bits or fewer need quantization-aware training. Start from a pretrained FP model, not from scratch.الـ PTQ مناسب عند 8 بتات للنماذج الكبيرة، لكن النماذج الصغيرة و4 بتات فأقل تحتاج إلى quantization-aware training. ابدأ من نموذج FP مدرَّب مسبقاً، لا من الصفر.
  • K-means QAT: group the weight gradients by cluster index, sum each group, multiply by lr and subtract from the centroid. With lr = 1, [2.00, 1.50, 0.00, -1.00] becomes [1.96, 1.48, -0.04, -0.97].في K-means QAT: نجمّع gradients الأوزان حسب رقم الـ cluster، ونجمع كل مجموعة، ثم نضرب في lr ونطرح من الـ centroid. مع lr = 1 تصبح [2.00, 1.50, 0.00, -1.00] هي [1.96, 1.48, -0.04, -0.97].
  • Linear QAT keeps a full-precision W. The forward pass uses Q(W) = S_W q_W and Q(Y) = S_Y (q_Y - Z_Y), so values crossing layer boundaries are discrete, while conv, batch norm and ReLU still run in FP.الـ linear QAT يحتفظ بنسخة W بالدقة الكاملة. الـ forward pass يستخدم Q(W) = S_W q_W و Q(Y) = S_Y (q_Y - Z_Y)، فتكون القيم التي تعبر حدود الطبقات متقطعة، بينما تبقى عمليات conv و batch norm و ReLU تعمل بالـ FP.
  • The FP copy lets updates much smaller than one quantization step accumulate until a weight crosses a rounding boundary. After training only the quantized weights ship.نسخة الـ FP تسمح للتحديثات الأصغر بكثير من خطوة quantization واحدة بأن تتراكم حتى يعبر الوزن حدّ تقريب. بعد التدريب تُشحن الأوزان المكمَّمة فقط.
  • dQ(W)/dW = 0 almost everywhere, so g_W = dL/dQ(W) · dQ(W)/dW = 0 and nothing learns. STE treats Q as the identity in the backward pass: g_W = dL/dQ(W), and likewise g_Y = dL/dQ(Y).dQ(W)/dW = 0 تقريباً في كل مكان، لذلك g_W = dL/dQ(W) · dQ(W)/dW = 0 ولا يتعلم شيء. الـ STE يعامل Q كدالة الهوية (identity) في الـ backward pass: g_W = dL/dQ(W)، وبالمثل g_Y = dL/dQ(Y).
  • INT8 on MobileNetV1: PTQ 0.1% (asymmetric per-tensor) and 59.1% (symmetric per-channel) become 70.0% and 70.7% with QAT, against 70.9% in FP.عند INT8 على MobileNetV1: نتائج PTQ وهي 0.1% (asymmetric per-tensor) و59.1% (symmetric per-channel) تصبح 70.0% و70.7% مع QAT، مقابل 70.9% بالـ FP.

Sourcesالمصادر