Lecture 05-3 · Quantization III

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

Straight-through estimator and QAT results

Straight-through estimator ونتائج QAT

Slides 45–48الشرائح 45–48 P1 MUSTP1 ضروري

Quantization is a staircase, and a staircase has zero slope almost everywhere, so ordinary backprop kills every gradient. The Straight-Through Estimator pretends Q() is the identity in the backward pass only. With it, INT8 QAT recovers almost all the accuracy that post-training quantization destroys on MobileNets.

التكميم دالة سُلّمية، والسُلّم ميله صفر تقريباً في كل مكان، فالانتشار العكسي العادي يقتل كل تدرج. يتظاهر Straight-Through Estimator بأن Q() هي دالة الهوية (identity) في المرور العكسي فقط. وبه يستعيد QAT بـ INT8 تقريباً كل الدقة التي يدمّرها post-training quantization في نماذج MobileNet.

The problem: dQ/dW = 0

المشكلة: dQ/dW = 0

Slide 45 plots Q(w) = round(w) for w from 0 to 5. Quantization is discrete-valued, so the derivative is 0 almost everywhere (on every flat step) and undefined at the jumps. Through the chain rule the weight gradient vanishes:

ترسم الشريحة 45 الدالة Q(w) = round(w) لقيم w من 0 إلى 5. التكميم منفصل القيم، لذلك المشتقة 0 تقريباً في كل مكان (على كل درجة مستوية) وغير معرّفة عند القفزات. وعبر قاعدة السلسلة يختفي تدرج الأوزان:

gW = ∂L∂W = ∂L∂Q(W) · ∂Q(W)∂W = ∂L∂Q(W) · 0 = 0

The network will learn nothing: gradients become 0 and the full-precision weights are never updated. This is the question slide 44 left open.

لن تتعلم الشبكة شيئاً: تصبح التدرجات 0 ولا تُحدَّث الأوزان عالية الدقة أبداً. وهذا هو السؤال الذي تركته الشريحة 44 مفتوحاً.

012 345 w 012 345 Q(w) = round(w) forward pass uses this staircase true slope: 0 on every step identity (dashed) STE uses slope 1 in the backward pass: ∂Q(W)/∂W ≈ 1 forward: quantize as usual backward: g_W = ∂L/∂Q(W)
Slide 45's plot. The blue staircase is what the forward pass computes; the dashed line is what the backward pass pretends Q() is.رسم الشريحة 45. الدرج الأزرق هو ما يحسبه المرور الأمامي؛ والخط المتقطع هو ما يتظاهر المرور الخلفي بأنه Q().

The fix: Straight-Through Estimator

الحل: Straight-Through Estimator

The STE (Hinton's 2012 Coursera lecture; Bengio et al., arXiv 2013) simply passes the gradient through the quantization as if it had been the identity function:

الـ STE (محاضرة Hinton على Coursera عام 2012؛ وBengio et al., arXiv 2013) يمرّر التدرج عبر التكميم ببساطة كأنه دالة الهوية:

gW = ∂L∂W = ∂L∂Q(W)

The gradient computed at the quantized weight Q(W) is copied unchanged onto the FP weight W, and W is updated. The forward pass is not changed at all: it still uses Q(W). STE is an approximation that lives only in the backward pass.

التدرج المحسوب عند الوزن المكمَّم Q(W) يُنسخ كما هو إلى الوزن العائم W، ثم يُحدَّث W. المرور الأمامي لا يتغير إطلاقاً: ما زال يستخدم Q(W). الـ STE تقريب يعيش في المرور العكسي فقط.

The full QAT picture (slide 46)

صورة QAT الكاملة (الشريحة 46)

Slide 46 adds the two gradients to the fake-quantization graph. On the weight side, gW ← ∂L/∂Q(W) flows from Layer N up through the weight quantizer to the FP weights. On the activation side, gY ← ∂L/∂Q(Y) flows from the next layer back through the activation quantizer to Y and into Layer N. Both quantizers are crossed with the STE.

تضيف الشريحة 46 التدرجين إلى مخطط التكميم المحاكى. في جانب الأوزان، g_W ← ∂L/∂Q(W) يتدفق من Layer N صعوداً عبر مكمِّم الأوزان إلى الأوزان العائمة. وفي جانب التفعيلات، g_Y ← ∂L/∂Q(Y) يتدفق من الطبقة التالية عائداً عبر مكمِّم التفعيلات إلى Y ثم إلى Layer N. ويُعبَر المكمِّمان كلاهما بـ STE.

weights W weight quantization Layer N activation quantization Layer N+1 Q(W) Y Q(Y) g_W ← ∂L/∂Q(W) ∂L/∂Q(W) g_Y ← ∂L/∂Q(Y) ∂L/∂Q(Y) STE: each quantizer passes its incoming gradient through unchanged
Slide 46. The violet gradient leaving each quantizer equals the one entering it.الشريحة 46. الـ gradient البنفسجي الخارج من كل مكمِّم يساوي الداخل إليه.
Worked exampleمثال محلول

One weight, Q(w) = round(w), learning rate 0.1. Start at w = 2.3, so Q(w) = 2. The loss gives ∂L/∂Q(W) = 0.5 at every step.

وزن واحد، Q(w) = round(w)، ومعدل تعلم 0.1. نبدأ من w = 2.3، أي Q(w) = 2. تعطي الخسارة ∂L/∂Q(W) = 0.5 في كل خطوة.

true chain rule:  g_W = 0.5 × 0 = 0      → w stays 2.3 forever
STE:              g_W = 0.5              → w ← w − 0.1 × 0.5 = w − 0.05

step  w (FP copy)   Q(w) used in forward
 0    2.30          2
 4    2.10          2
 8    1.90          2
16    1.50          2   (1.50 is the boundary)
17    1.45          1   ← first change in the quantized weight

With STE the FP copy drifts steadily, and the quantized value changes only when the drift crosses a rounding boundary. This is exactly why QAT needs both the FP master copy and the STE.

مع STE تنجرف النسخة العائمة بثبات، ولا تتغير القيمة المكمّاة إلا عندما يعبر الانجراف حد تقريب. ولهذا بالضبط يحتاج QAT إلى النسخة الرئيسية العائمة وإلى STE معاً.

INT8 QAT results (slide 47)

نتائج QAT بدقة INT8 (الشريحة 47)

Slide 47 (Krishnamoorthi whitepaper, 2018) compares top-1 accuracy of the FP model, PTQ and QAT, each with two settings: asymmetric per-tensor and symmetric per-channel.

تقارن الشريحة 47 (ورقة Krishnamoorthi البيضاء، 2018) دقة top-1 لنموذج FP وPTQ وQAT، ولكل منهما إعدادان: غير متماثل لكل tensor ومتماثل لكل قناة.

NetworkالشبكةFPPost-Training Quantizationالتكميم بعد التدريب (PTQ)Quantization-Aware Trainingالتدريب الواعي بالتكميم (QAT)
Asym, per-tensorغير متماثل، per-tensorSym, per-channelمتماثل، per-channelAsym, per-tensorغير متماثل، per-tensorSym, per-channelمتماثل، per-channel
MobileNetV170.9%0.1%59.1%70.0%70.7%
MobileNetV271.9%0.1%69.8%70.9%71.1%
NASNet-Mobile74.9%72.2%72.1%73.0%73.0%

لاحظ أن PTQ غير المتماثل لكل tensor ينهار إلى 0.1% (تخمين عشوائي على 1000 فئة) في MobileNetV1 وMobileNetV2، بينما يعيد QAT الدقة إلى أقل من نقطة واحدة من FP.

Worked exampleمثال محلول

Compute every column's drop versus FP (column minus FP).

احسب انخفاض كل عمود مقارنة بـ FP (العمود ناقص FP).

                 PTQ asym/T   PTQ sym/C   QAT asym/T   QAT sym/C
MobileNetV1      −70.8        −11.8       −0.9         −0.2
  (70.9 − 0.1 = 70.8;  70.9 − 59.1 = 11.8;  70.9 − 70.0 = 0.9;  70.9 − 70.7 = 0.2)
MobileNetV2      −71.8        −2.1        −1.0         −0.8
  (71.9 − 0.1;  71.9 − 69.8;  71.9 − 70.9;  71.9 − 71.1)
NASNet-Mobile    −2.7         −2.8        −1.9         −1.9
  (74.9 − 72.2;  74.9 − 72.1;  74.9 − 73.0;  74.9 − 73.0)

Recovered by QAT (sym per-channel) over PTQ (sym per-channel):
  MobileNetV1  70.7 − 59.1 = 11.6 points
  MobileNetV2  71.1 − 69.8 =  1.3 points
  NASNet       73.0 − 72.1 =  0.9 points

The PTQ symmetric per-channel drops (−11.8, −2.1) are the same MobileNet numbers that slide 37 showed. Two lessons: per-channel beats per-tensor for PTQ on depthwise-heavy MobileNets, and QAT closes the gap for both settings, with symmetric per-channel QAT the best or tied-best in every row.

انخفاضات PTQ المتماثل لكل قناة (−11.8 و−2.1) هي نفس أرقام MobileNet في الشريحة 37. درسان: لكل قناة أفضل من لكل tensor في PTQ على شبكات MobileNet الغنية بـ depthwise، وQAT يسد الفجوة في الإعدادين، وQAT المتماثل لكل قناة هو الأفضل أو المتعادل في الأفضلية في كل صف.

Slide 48 closes the section by returning to the storage/compute table with an arrow and a question mark: linear quantization already gives integer weights and integer arithmetic, so what is beyond it? The answer is to push the bit width all the way down, to binary and ternary networks (next pages).

تختم الشريحة 48 القسم بالعودة إلى جدول التخزين والحساب مع سهم وعلامة استفهام: linear quantization يعطي أصلاً أوزاناً صحيحة وحساباً صحيحاً، فما الذي بعده؟ الجواب هو دفع عرض البتات إلى الحد الأدنى، إلى الشبكات الثنائية (binary) والثلاثية (ternary) (الصفحات التالية).

Memory trickحيلة للحفظ

"Forward: staircase. Backward: straight line." STE = pretend ∂Q/∂W = 1. Everything else in the chain rule stays exact.

"أماماً: سُلّم. خلفاً: خط مستقيم". STE = تظاهر بأن ∂Q/∂W = 1. وكل ما عدا ذلك في قاعدة السلسلة يبقى دقيقاً.

Exam trapفخ امتحاني

STE does not remove quantization from the forward pass. The forward pass still computes with Q(W) and Q(Y); only the backward pass treats Q() as the identity. An answer like "STE trains in full precision and quantizes at the end" describes PTQ, not QAT.

STE لا يزيل التكميم من المرور الأمامي. المرور الأمامي ما زال يحسب بـ Q(W) وQ(Y)؛ والمرور العكسي فقط يعامل Q() كدالة هوية. إجابة مثل "يدرّب STE بدقة كاملة ويكمّم في النهاية" تصف PTQ وليس QAT.

STE is an approximation, not the true derivative. The true derivative of round() is 0 almost everywhere. STE's gradient is biased on purpose so that learning can happen.

STE تقريب وليس المشتقة الحقيقية. المشتقة الحقيقية لـ round() هي 0 تقريباً في كل مكان. تدرج STE متحيز عمداً حتى يحدث التعلّم.

0.1% is not a typo for "−0.1%". In slide 47 the PTQ asymmetric per-tensor entries for MobileNets are accuracies of 0.1% (a collapse), unlike slide 37 where entries are drops.

0.1% ليست خطأ مطبعياً لـ "−0.1%". في الشريحة 47 خانات PTQ غير المتماثل لكل tensor لنماذج MobileNet هي دقة 0.1% (انهيار)، بخلاف الشريحة 37 حيث الخانات انخفاضات.

Check yourself

اختبر نفسك

Without STE, why does QAT fail to update the weights?

بدون STE، لماذا يفشل QAT في تحديث الأوزان؟

A. The staircase is flat between jumps, so the chain rule multiplies by 0. C is wrong: ∂L/∂Q(W) is a normal gradient. B contradicts QAT's FP master copy. D is not what the slide says; the problem is a zero gradient, not an exploding one.

A. الدرج مسطح بين القفزات، فتضرب قاعدة السلسلة في 0. الخيار C خاطئ: ∂L/∂Q(W) هو gradient عادي. والخيار B يناقض النسخة الرئيسية بدقة FP في QAT. والخيار D ليس ما تقوله الشريحة؛ المشكلة gradient صفري وليس منفجراً.

What does the Straight-Through Estimator set gW to?

إلى ماذا يضبط Straight-Through Estimator قيمة g_W؟

C. STE treats Q() as the identity, so gW = ∂L/∂W = ∂L/∂Q(W). B is the exact chain rule, which equals 0 (A). D quantizes the gradient, which is a different idea.

C. يعامل STE الدالة Q() كدالة الهوية، لذا gW = ∂L/∂W = ∂L/∂Q(W). الخيار B هو قاعدة السلسلة الدقيقة، التي تساوي 0 (الخيار A). والخيار D يكمّم الـ gradient، وهي فكرة مختلفة.

From slide 47, how many accuracy points does MobileNetV1 lose with INT8 QAT, symmetric per-channel, versus FP?

من الشريحة 47، كم نقطة دقة يفقد MobileNetV1 مع QAT بـ INT8 متماثل لكل قناة مقارنة بـ FP؟

B. 70.9 − 70.7 = 0.2. A is QAT asymmetric per-tensor (70.9 − 70.0). C is PTQ symmetric per-channel's drop. D is how much QAT recovers over that PTQ setting (70.7 − 59.1).

B. 70.9 − 70.7 = 0.2. الخيار A هو QAT غير المتماثل per-tensor (70.9 − 70.0). والخيار C هو انخفاض PTQ المتماثل per-channel. والخيار D هو مقدار ما يستعيده QAT فوق إعداد PTQ ذاك (70.7 − 59.1).

Which statement about STE in QAT is correct?

أي عبارة عن STE في QAT صحيحة؟

D. A would make training ignore quantization (no QAT at all). B is false: the exact derivative is 0. C is false: slide 46 shows gY ← ∂L/∂Q(Y) through the activation quantizer as well.

D. الخيار A سيجعل التدريب يتجاهل التكميم (فلا يبقى QAT أصلاً). والخيار B خاطئ: المشتقة الدقيقة تساوي 0. والخيار C خاطئ: الشريحة 46 تُظهر gY ← ∂L/∂Q(Y) عبر مكمِّم التفعيلات أيضاً.

In slide 47, what happens to MobileNetV2 under PTQ with asymmetric per-tensor quantization?

في الشريحة 47، ماذا يحدث لـ MobileNetV2 تحت PTQ بتكميم غير متماثل لكل tensor؟

A. The entry is 0.1% top-1, a near-total collapse. B misreads the entry as a drop. C is PTQ symmetric per-channel. D is QAT asymmetric per-tensor.

A. القيمة هي 0.1% بمقياس top-1، أي انهيار شبه كامل. الخيار B يقرأ القيمة خطأً على أنها انخفاض. والخيار C هو PTQ المتماثل per-channel. والخيار D هو QAT غير المتماثل per-tensor.