Lecture 05-3 · Quantization III
المحاضرة 05-3 · التكميم الثالث
PTQ summary, its limits, and QAT setup
ملخص PTQ وحدوده، وإعداد QAT
P2 SHOULDP2 مهمSlide 36 collects every Post-Training Quantization knob into one map. The INT8 results on slides 37 and 38 show that PTQ is almost free for big CNNs but hurts small mobile models. The fix is Quantization-Aware Training: fine-tune the network while it "sees" its own quantization error.
تجمع الشريحة 36 كل مقابض Post-Training Quantization في خريطة واحدة. وتُظهر نتائج INT8 في الشريحتين 37 و38 أن PTQ شبه مجاني للشبكات الالتفافية الكبيرة لكنه يضر نماذج الجوال الصغيرة. والحل هو Quantization-Aware Training: ضبط الشبكة بينما "ترى" خطأ تكميمها بنفسها.
The PTQ map (slide 36)
خريطة PTQ (الشريحة 36)
The left half of slide 36 repeats the storage and compute table: floating-point weights use FP storage and FP arithmetic; K-means stores integer indices plus an FP codebook but still computes in FP; linear quantization stores integer weights and computes with integer arithmetic. The right half lists the four decisions you make when you linearly quantize after training:
النصف الأيسر من الشريحة 36 يكرر جدول التخزين والحساب: الأوزان العائمة تستخدم تخزيناً وحساباً FP؛ وK-means يخزّن فهارس صحيحة مع codebook عائم لكنه يحسب بـ FP؛ أما linear quantization فيخزّن أوزاناً صحيحة ويحسب بحساب الأعداد الصحيحة. والنصف الأيمن يسرد القرارات الأربعة عند التكميم الخطي بعد التدريب:
Zero point
نقطة الصفر (zero point)
Asymmetric (Z ≠ 0, covers [rmin, rmax] exactly) or symmetric (Z = 0, range ±|r|max).
غير متماثل (Z ≠ 0، يغطي [r_min, r_max] بدقة) أو متماثل (Z = 0، المدى ±|r|_max).
Scaling granularity
دقة التحجيم (scaling granularity)
Per-tensor (one S), per-channel (one S per output channel), group quantization (one S per small group, VS-Quant, MX).
لكل tensor (مقياس S واحد)، لكل قناة (S لكل قناة خرج)، أو group quantization (S لكل مجموعة صغيرة، VS-Quant وMX).
Range clipping
قص المدى
Exponential Moving Average of min/max, minimising KL divergence, or minimising mean-square error.
المتوسط المتحرك الأسي (EMA) لـ min/max، أو تقليل KL divergence، أو تقليل mean-square error.
Rounding
التقريب
Round-to-nearest, or AdaRound (learn up or down per weight to reconstruct the output).
التقريب إلى الأقرب، أو AdaRound (تعلّم للأعلى أو للأسفل لكل وزن لإعادة بناء المخرج).
Post-training INT8 results (slides 37 and 38)
نتائج INT8 بعد التدريب (الشريحتان 37 و38)
The table compares two PTQ recipes. Column 1: activations symmetric, per-tensor, range by minimising KL divergence; weights symmetric, per-tensor. Column 2: activations asymmetric, per-tensor, range by EMA; weights symmetric, per-channel. Each entry is the top-1 accuracy change versus the FP model.
يقارن الجدول وصفتين لـ PTQ. العمود 1: التفعيلات متماثلة، لكل tensor، والمدى بتقليل KL divergence؛ والأوزان متماثلة لكل tensor. العمود 2: التفعيلات غير متماثلة، لكل tensor، والمدى بـ EMA؛ والأوزان متماثلة، لكل قناة. كل خانة هي التغير في دقة top-1 مقارنة بنموذج FP.
| Networkالشبكة | Act: sym, per-tensor, KL Weight: sym, per-tensorالتفعيل: متماثل، per-tensor، KL الوزن: متماثل، per-tensor | Act: asym, per-tensor, EMA Weight: sym, per-channelالتفعيل: غير متماثل، per-tensor، EMA الوزن: متماثل، per-channel |
|---|---|---|
| GoogleNet | −0.45% | 0% |
| ResNet-50 | −0.13% | −0.6% |
| ResNet-152 | −0.08% | −1.8% |
| MobileNetV1 | − | −11.8% |
| MobileNetV2 | − | −2.1% |
الشبكات الكبيرة (GoogleNet وResNet-50 وResNet-152) تفقد أقل من 2% في كلا العمودين. أما MobileNetV1 فيفقد 11.8% وMobileNetV2 يفقد 2.1%، والعمود الأول لا يذكر قيمة لهما (−).
Slide 38's verdict: smaller models seem to not respond as well to post-training quantization, presumably due to their smaller representational capacity. MobileNets are already compressed by design (depthwise separable convolutions, few parameters), so there is little redundancy left to absorb rounding noise. The slide then asks: how should we improve performance of quantized models? Slide 39 answers with Quantization-Aware Training.
حكم الشريحة 38: النماذج الأصغر لا تستجيب جيداً لـ post-training quantization، على الأرجح بسبب سعتها التمثيلية الأصغر (representational capacity). نماذج MobileNet مضغوطة بالتصميم أصلاً (depthwise separable convolutions ومعاملات قليلة)، فلا يبقى فيها فائض يمتص ضجيج التقريب. ثم تسأل الشريحة: كيف نحسّن أداء النماذج المكمّاة؟ وتجيب الشريحة 39 بـ Quantization-Aware Training.
QAT: train with quantization in the loop
QAT: التدريب والتكميم داخل الحلقة
Slide 40: to minimise the loss of accuracy, especially for aggressive quantization with 4 bits and lower, the network is trained or fine-tuned with quantized weights and activations. Usually fine-tuning a pre-trained floating-point model gives better accuracy than training from scratch. The slide's example is the K-means fine-tuning you already saw in Deep Compression: gradients are grouped by cluster index, summed per cluster, scaled by the learning rate and subtracted from the centroids.
الشريحة 40: لتقليل فقدان الدقة، خصوصاً مع التكميم العنيف بـ 4 بتات أو أقل، تُدرَّب الشبكة أو تُضبط بأوزان وتفعيلات مكمّاة. وعادةً ضبط نموذج FP مدرَّب مسبقاً يعطي دقة أفضل من التدريب من الصفر. مثال الشريحة هو ضبط K-means الذي رأيته في Deep Compression: تُجمَّع التدرجات حسب فهرس العنقود، وتُجمع لكل عنقود، وتُضرب في معدل التعلم وتُطرح من المراكز (centroids).
blue cluster (index 3): gradients −0.03, 0.12, 0.02, −0.07 reduce: sum = 0.04 centroid 3: 2.00 − 0.04 = 1.96 (slide uses lr = 1) centroid 2: 1.50 − 0.02 = 1.48 centroid 1: 0.00 − 0.04 = −0.04 centroid 0: −1.00 − (−0.03) = −0.97
هذه أرقام الشريحة 40: مجموع تدرجات العنقود الأزرق 0.04، فيصبح المركز 2.00 − 0.04 = 1.96، وبالمثل 1.48 و−0.04 و−0.97.
For linear quantization, slides 41 to 44 build the training graph. Three bullets repeat on every slide and are the core of QAT:
في linear quantization تبني الشرائح من 41 إلى 44 مخطط التدريب. ثلاث نقاط تتكرر في كل شريحة وهي جوهر QAT:
- A full-precision copy of the weights W is maintained throughout training.
تُحفظ نسخة عالية الدقة من الأوزان W طوال التدريب.
- The small gradients are accumulated without loss of precision.
تتراكم التدرجات الصغيرة دون فقدان للدقة.
- Once the model is trained, only the quantized weights are used for inference.
بعد انتهاء التدريب، تُستخدم الأوزان المكمّمة فقط للاستدلال (inference).
This is simulated (fake) quantization. The weight path is W → SWqW = Q(W): the FP weights are quantized and immediately dequantized, so Q(W) is a float tensor that only takes values on the quantization grid. The activation path is Y → SY(qY − ZY) = Q(Y). Conv, BatchNorm and ReLU still run in full precision; the network simply experiences the same rounding and clipping it will meet at INT8 inference time and learns to tolerate it.
هذا هو التكميم المحاكى (simulated / fake quantization). مسار الأوزان هو W → S_W q_W = Q(W): تُكمَّم الأوزان العائمة ثم يُفك تكميمها فوراً، فـ Q(W) tensor عائم لا يأخذ إلا قيماً على شبكة التكميم. ومسار التفعيلات هو Y → S_Y(q_Y − Z_Y) = Q(Y). أما Conv وBatchNorm وReLU فما زالت تعمل بدقة كاملة؛ الشبكة فقط تختبر نفس التقريب والقص الذي ستواجهه عند الاستدلال بـ INT8 وتتعلم تحمّله.
Use slide 43's fake quantizer: 2-bit signed q ∈ {−2, −1, 0, 1}, Z = −1, S = 1.07, so Q(r) = S(q − Z) with q = clamp(round(r / S + Z), −2, 1).
استخدم المكمِّم المحاكى في الشريحة 43: q ∈ {−2, −1, 0, 1} بـ 2 بت بإشارة، Z = −1، S = 1.07، أي Q(r) = S(q − Z) حيث q = clamp(round(r / S + Z), −2, 1).
r = 2.09: 2.09/1.07 − 1 = 0.953 → q = 1 Q = 1.07(1 + 1) = 2.14 r = −0.98: −0.98/1.07 − 1 = −1.916 → q = −2 Q = 1.07(−2 + 1) = −1.07 r = 0.09: 0.09/1.07 − 1 = −0.916 → q = −1 Q = 1.07(−1 + 1) = 0 Why keep the FP copy? Let w = 0.09 and each step add 0.01 (−lr·g = +0.01). Q(w) moves from 0 to 1.07 only when r/S − 1 ≥ −0.5, i.e. r ≥ 0.535. with the FP copy: 0.09 + 45 × 0.01 = 0.54 ≥ 0.535 → flips after 45 steps storing only Q(w): 0 + 0.01 → rounds back to 0 every step → never moves
That is bullet 2 in numbers: each update is far smaller than one grid step (1.07), so it would be rounded away if only the quantized value were stored. The FP master copy lets tiny updates add up until they cross a rounding threshold.
هذه هي النقطة 2 بالأرقام: كل تحديث أصغر بكثير من خطوة شبكة واحدة (1.07)، فسيضيع بالتقريب لو خُزّنت القيمة المكمّاة فقط. النسخة الرئيسية العائمة تسمح للتحديثات الصغيرة بالتراكم حتى تعبر عتبة التقريب.
The open question (slide 44)
السؤال المفتوح (الشريحة 44)
Slide 44 ends with a red box: how should gradients back-propagate through the (simulated) quantization? Q() is a staircase. Its derivative is 0 on every flat step and undefined at the jumps, so by the chain rule dL/dW = dL/dQ(W) × dQ(W)/dW = 0 and the FP weights would never change. The next page answers this with the Straight-Through Estimator.
تنتهي الشريحة 44 بصندوق أحمر: كيف تنتشر التدرجات عكسياً عبر التكميم (المحاكى)؟ Q() دالة سُلّمية. مشتقتها 0 على كل درجة مستوية وغير معرّفة عند القفزات، فبقاعدة السلسلة dL/dW = dL/dQ(W) × dQ(W)/dW = 0 ولن تتغير الأوزان العائمة أبداً. تجيب الصفحة التالية عن هذا بـ Straight-Through Estimator.
"Train in float, look like int." Fake quantization keeps every tensor in FP32 but forces the values at layer boundaries onto the INT grid. After training, throw away the FP copy and ship qW.
"درّب بالعائم، وابدُ كأنك صحيح". التكميم المحاكى يُبقي كل tensor بـ FP32 لكنه يجبر القيم عند حدود الطبقات على شبكة INT. بعد التدريب ارمِ النسخة العائمة وانشر q_W.
Fake quantization does not speed up training. All arithmetic still runs in floating point (plus the extra quantize/dequantize ops). The integer speedup only appears at inference.
التكميم المحاكى لا يسرّع التدريب. كل العمليات الحسابية ما زالت عائمة (مع عمليات quantize/dequantize إضافية). تسريع الأعداد الصحيحة يظهر فقط عند الاستدلال.
Gradients cannot pass through Q() as is. dQ/dW = 0 almost everywhere, so plain backprop gives zero gradient. Do not answer "QAT uses the true derivative of round()".
لا يمكن للتدرجات المرور عبر Q() كما هي. dQ/dW = 0 تقريباً في كل مكان، فالانتشار العكسي العادي يعطي تدرجاً صفرياً. لا تجب بأن QAT يستخدم المشتقة الحقيقية لـ round().
Read the PTQ table as drops, not accuracies. "−11.8%" for MobileNetV1 means 11.8 points lost versus FP, not a final accuracy of 11.8%. The "−" in the first column means not reported, not zero loss.
اقرأ جدول PTQ كانخفاضات لا كدقة. "−11.8%" لـ MobileNetV1 تعني فقدان 11.8 نقطة مقارنة بـ FP، وليست دقة نهائية 11.8%. والرمز "−" في العمود الأول يعني غير مذكور، وليس فقداناً صفرياً.
Small models suffer more. The slide's reason is smaller representational capacity, not "fewer layers" or "more channels".
النماذج الصغيرة تتضرر أكثر. سبب الشريحة هو السعة التمثيلية الأصغر، وليس "طبقات أقل" ولا "قنوات أكثر".
Check yourself
اختبر نفسك
On slide 37, which network loses the most accuracy with asymmetric per-tensor EMA activations and symmetric per-channel weights?
في الشريحة 37، أي شبكة تفقد أكبر قدر من الدقة مع تفعيلات غير متماثلة لكل tensor بـ EMA وأوزان متماثلة لكل قناة؟
B. −11.8% is by far the largest drop. A and C are real entries but smaller. D's −0.45% belongs to the other column (symmetric per-tensor KL); GoogleNet loses 0% in this column.
B. −11.8% هو أكبر انخفاض بفارق كبير. الخياران A وC قيمتان حقيقيتان في الجدول لكنهما أصغر. وقيمة −0.45% في الخيار D تخص العمود الآخر (KL متماثل per-tensor)؛ أما GoogleNet فيخسر 0% في هذا العمود.
Why do the slides say small models respond poorly to PTQ?
لماذا تقول الشرائح إن النماذج الصغيرة تستجيب بشكل سيئ لـ PTQ؟
C. Slide 38: "presumably due to their smaller representational capacity". A is just a recipe choice used for every network. B is not claimed. D is false: the −11.8% entry is already per-channel.
C. الشريحة 38: "presumably due to their smaller representational capacity"، أي يُفترض أن السبب سعتها التمثيلية الأصغر. الخيار A مجرد اختيار في الوصفة يُستخدم لكل الشبكات. والخيار B لم يُذكر. والخيار D خاطئ: قيمة −11.8% هي أصلاً بتكميم per-channel.
During QAT, why is a full-precision copy of W kept?
أثناء QAT، لماذا تُحفظ نسخة عالية الدقة من W؟
A. Each update is much smaller than one quantization step; on the quantized value alone it would vanish. B is wrong: only the quantized weights are used for inference. C and D are invented.
A. كل تحديث أصغر بكثير من خطوة تكميم واحدة؛ ولو طُبِّق على القيمة المكمّمة وحدها لتلاشى. الخيار B خاطئ: الأوزان المكمّمة وحدها تُستخدم في الاستدلال. والخياران C وD مختلَقان.
In simulated (fake) quantization, which statement is true?
في التكميم المحاكى (fake quantization)، أي عبارة صحيحة؟
D. Slide 42: Q(W) and Q(Y) ensure discrete-valued weights and activations at the boundaries; "these operations still run in full precision". A is the opposite. B drops the weight quantizer. C reverses the roles: Q() is applied in the forward pass.
D. الشريحة 42: Q(W) وQ(Y) يضمنان أوزاناً وتفعيلات منفصلة القيم عند الحدود؛ و"these operations still run in full precision"، أي أن هذه العمليات لا تزال تعمل بالدقة الكاملة. الخيار A هو العكس. والخيار B يُسقط مكمِّم الأوزان. والخيار C يعكس الأدوار: Q() يُطبَّق في المرور الأمامي.
With slide 43's quantizer (S = 1.07, Z = −1, q ∈ {−2, −1, 0, 1}), what is Q(1.48)?
مع مكمِّم الشريحة 43 (S = 1.07، Z = −1، q ∈ {−2, −1, 0, 1})، ما قيمة Q(1.48)؟
B. 1.48 / 1.07 − 1 = 0.383, round to q = 0, Q = 1.07 × (0 + 1) = 1.07, matching slide 43. A skips quantization. C uses q = 1. D forgets to subtract Z.
B. 1.48 / 1.07 − 1 = 0.383، تُقرَّب إلى q = 0، وQ = 1.07 × (0 + 1) = 1.07، مطابقاً للشريحة 43. الخيار A يتخطى التكميم. والخيار C يستخدم q = 1. والخيار D ينسى طرح Z.