Lecture 05-3 · Quantization III

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

Activation ranges: EMA and calibration

مدى التفعيلات: EMA والمعايرة

Slides 22–26الشرائح 22–26 P2 SHOULDP2 مهم

Weights are fixed after training, so their rmin and rmax are known. Activations change with every input. To fix S and Z before deployment you must estimate the activation range, either while training (EMA) or afterwards with calibration data.

الأوزان ثابتة بعد التدريب، فقيم rmin وrmax لها معروفة. أما activations فتتغير مع كل دخل. ولتثبيت S وZ قبل النشر يجب تقدير مدى activations، إما أثناء التدريب (EMA) أو بعده باستخدام بيانات calibration.

The question this section answers

السؤال الذي يجيب عنه هذا القسم

Slide 22 opens Post-Training Quantization with: how should we get the optimal linear quantization parameters (S, Z)? Topic I was granularity, this is Topic II, dynamic range clipping, and Topic III will be rounding. Recall S = (rmax − rmin) / (qmax − qmin) and Z = round(qmin − rmin/S). Everything depends on which rmin, rmax you pick.

الشريحة 22 تبدأ Post-Training Quantization بسؤال: كيف نحصل على أفضل معاملات linear quantization أي (S, Z)؟ الموضوع الأول كان granularity، وهذا هو الموضوع الثاني dynamic range clipping، والثالث سيكون rounding. وتذكّر أن S = (rmax − rmin) / (qmax − qmin) وZ = round(qmin − rmin/S). فكل شيء يعتمد على اختيار rmin وrmax.

Slide 23: unlike weights, the activation range varies across inputs, so the statistics are gathered before deploying the model. There are two ways to gather them.

الشريحة 23: بخلاف الأوزان، مدى activations يتغير من دخل لآخر، لذلك تُجمع الإحصاءات قبل نشر النموذج. وهناك طريقتان لجمعها.

Type 1: during training, exponential moving average

النوع الأول: أثناء التدريب، المتوسط المتحرك الأسي (EMA)

Slide 24 (Jacob et al., CVPR 2018). Each training step t sees a batch and measures its own rmax(t) and rmin(t). The running estimate is updated as:

الشريحة 24 (Jacob et al., CVPR 2018). في كل خطوة تدريب t تُرى دفعة بيانات ويُقاس لها rmax(t) وrmin(t). ويُحدَّث التقدير الجاري هكذا:

r̂(t)max, min = α · r(t)max, min + (1 − α) · r̂(t−1)max, min

تقول المعادلة: التقدير الجديد يساوي α مضروبة في القيمة المقاسة الآن، زائد (1 − α) مضروبة في التقدير السابق.

α is the weight on the new observation. The observed ranges are smoothed across thousands of training steps, so one unusual batch cannot yank the range around. A small α means heavy smoothing and slow reaction; a large α trusts the latest batch.

α هي وزن القياس الجديد. والمدى المقاس يُنعَّم عبر آلاف خطوات التدريب، فلا تستطيع دفعة واحدة غير عادية أن تحرّك المدى كثيراً. وα صغيرة تعني تنعيماً قوياً واستجابة بطيئة، وα كبيرة تعني الثقة بآخر دفعة.

Workedمثال محلول
alpha = 0.9, previous estimate [-1.0, 6.0],
this batch measures [-2.0, 8.0]

r_max = 0.9 x 8.0 + 0.1 x 6.0  = 7.2 + 0.6  =  7.8
r_min = 0.9 x (-2.0) + 0.1 x (-1.0) = -1.8 - 0.1 = -1.9

then UINT8 parameters, q in [0, 255]:
S = (7.8 - (-1.9)) / 255 = 9.7 / 255 = 0.03804
Z = round(0 - (-1.9) / 0.03804) = round(49.95) = 50

same batch with alpha = 0.1 instead:
r_max = 0.1 x 8.0 + 0.9 x 6.0 = 6.2   (barely moves)

المثال المحلول: α = 0.9، والتقدير السابق [−1.0, 6.0]، والدفعة الحالية تقيس [−2.0, 8.0]. فيكون rmax = 0.9 × 8.0 + 0.1 × 6.0 = 7.8 وrmin = 0.9 × (−2.0) + 0.1 × (−1.0) = −1.9. ثم بدقة UINT8 حيث q من 0 إلى 255: S = 9.7 / 255 = 0.03804 وZ = round(49.95) = 50. ولو كانت α = 0.1 لكان rmax = 6.2 فقط، أي أنه بالكاد يتحرك.

Type 2: calibration batches after training

النوع الثاني: دفعات معايرة بعد التدريب

Slide 25: run a few "calibration" batches of samples through the trained FP32 model and record the activations. Taking the absolute max over everything is a bad idea: spending dynamic range on the outliers hurts the representation ability. A rare huge value stretches S, and the dense bulk near zero lands on only a few levels. The slide's simple fix: use the mean of the min/max of each sample in the batches.

الشريحة 25: مرّر بضع دفعات calibration عبر نموذج FP32 المدرَّب وسجّل activations. أخذ القيمة القصوى المطلقة لكل شيء فكرة سيئة: صرف المدى على القيم الشاذة outliers يضر بقدرة التمثيل. فقيمة نادرة ضخمة تمدّ S، وتقع الكتلة الكثيفة قرب الصفر على مستويات قليلة. والحل البسيط في الشريحة: استخدم متوسط min/max لكل عينة في الدفعات.

Workedمثال محلول
four calibration samples, per-sample max of a ReLU output:
  3.1   2.9   3.0   12.0   (the last one is an outlier)

global max          = 12.0
mean of sample max  = (3.1 + 2.9 + 3.0 + 12.0) / 4 = 21 / 4 = 5.25

UINT8 step size (r_min = 0):
  global max: S = 12.0 / 255 = 0.0471
  mean max:   S = 5.25 / 255 = 0.0206   (2.29x finer)

المثال المحلول: أربع عينات calibration، والقيمة القصوى لكل عينة 3.1 و2.9 و3.0 و12.0 (الأخيرة شاذة). القيمة القصوى الكلية 12.0، ومتوسط القيم القصوى 21 / 4 = 5.25. وبدقة UINT8: الخطوة مع القصوى الكلية 12.0 / 255 = 0.0471، ومع المتوسط 5.25 / 255 = 0.0206، أي أدق بـ 2.29 مرة. والقيمة الشاذة نفسها تُقصّ إلى 5.25.

Type 2, analytical: minimize the MSE

النوع الثاني تحليلياً: تقليل MSE

Slide 26 (Banner et al., NeurIPS 2019) picks the clip value |r|max that minimizes the mean-square error between the inputs X and their quantized reconstruction Q(X):

الشريحة 26 (Banner et al., NeurIPS 2019) تختار قيمة القص |r|max التي تقلّل mean-square error بين الدخل X ونسخته المعاد بناؤها بعد quantization أي Q(X):

min|r|max  E[ (X − Q(X))2 ]

تقول المعادلة: اختر |r|max التي تجعل متوسط مربع الفرق بين X وQ(X) أصغر ما يمكن.

Assume the inputs follow a Gaussian or Laplace distribution. For Laplace(0, b), the optimal clipping values were solved numerically:

نفترض أن الدخل يتبع توزيع Gaussian أو Laplace. ولتوزيع Laplace(0, b) حُسبت قيم القص المثلى عددياً:

BitsالبتاتOptimal |r|max|r|max الأمثلb = 0.5b = 1
22.83 b1.4152.83
33.89 b1.9453.89
45.03 b2.5155.03

الجدول: عند 2 بت القص الأمثل 2.83b، وعند 3 بت 3.89b، وعند 4 بت 5.03b. ومع b = 0.5 تصبح القيم 1.415 و1.945 و2.515.

The Laplace parameter b is estimated from the calibration input distribution. For a zero-mean Laplace, the maximum-likelihood estimate of b is simply the mean of |x| over the calibration data.

ومعامل Laplace أي b يُقدَّر من توزيع دخل calibration. ولتوزيع Laplace متوسطه صفر، فإن تقدير maximum-likelihood لـ b هو ببساطة متوسط |x| على بيانات calibration.

x −α +α Δ 2M steps of width Δ = 2α / 2M: rounding error grows with α clipping error clipping error α = |r|max: shrink it and tails clip, grow it and steps coarsen
The slide-26 picture: an M-bit grid of 2M steps across [−α, α]. Everything beyond ±α is clipped to the edge. The MSE-optimal α balances the two error sources.صورة الشريحة 26: شبكة بـ M بت فيها 2M خطوة عبر [−α, α]. كل ما يتجاوز ±α يُقص إلى الحافة. وقيمة α المثلى بمعيار MSE توازن بين مصدري الخطأ.

الرسم يمثل صورة الشريحة 26: توزيع Laplace مدبّب حول الصفر، وخطا قص عند −α و+α. داخل المدى توجد 2M خطوة عرض كل منها Δ = 2α / 2M. والذيلان خارج ±α مظللان لأنهما clipping error. إذا صغّرت α زاد القص، وإذا كبّرتها صارت الخطوات أخشن وزاد rounding error. والقيمة المثلى توازن بين المصدرين.

Workedمثال محلول
calibration gives b = 0.5, target 3 bits (M = 3)

|r|max = 3.89 x 0.5 = 1.945
grid: 2^3 = 8 steps across [-1.945, 1.945]
step  Delta = 2 x 1.945 / 8 = 0.486
an activation of 2.7 is clipped to the edge, 1.945

same data at 4 bits: 5.03 x 0.5 = 2.515
more bits -> finer steps -> afford a wider clip

المثال المحلول: calibration تعطي b = 0.5، والهدف 3 بتات. إذن |r|max = 3.89 × 0.5 = 1.945، والشبكة فيها 23 = 8 خطوات على [−1.945, 1.945]، فتكون الخطوة Δ = 2 × 1.945 / 8 = 0.486. وقيمة 2.7 تُقصّ إلى الحافة 1.945. ونفس البيانات عند 4 بتات: 5.03 × 0.5 = 2.515. فكلما زادت البتات صارت الخطوات أدق، فنتحمل مدى قص أوسع.

Trapفخ

α multiplies the new sample, not the old estimate. In this slide's EMA, r̂(t) = α · r(t) + (1 − α) · r̂(t−1). Plugging α onto the previous value gives the alpha = 0.1 answer (6.2) when the question meant 7.8.

α تضرب القياس الجديد، لا التقدير القديم. في معادلة EMA في هذه الشريحة r̂(t) = α · r(t) + (1 − α) · r̂(t−1). ووضع α على القيمة السابقة يعطي جواب 6.2 بينما المطلوب 7.8.

A larger range is not safer. Clipping trades clipping error (tails) against rounding error (step size). The optimal clip grows with the bit width: 2.83b, 3.89b, 5.03b for 2, 3, 4 bits. And those multipliers are for Laplace; a Gaussian has different constants.

المدى الأكبر ليس أكثر أماناً. فالقص يوازن بين clipping error (الذيول) وrounding error (حجم الخطوة). وقيمة القص المثلى تكبر مع عدد البتات: 2.83b و3.89b و5.03b لـ 2 و3 و4 بتات. وهذه المضاعفات خاصة بـ Laplace، أما Gaussian فلها ثوابت مختلفة.

Memory hookخطّاف للذاكرة

"Three, four, five" for 2, 3, 4 bits. 2.83 ≈ 3, 3.89 ≈ 4, 5.03 ≈ 5: roughly one extra b of clip per extra bit.

"ثلاثة، أربعة، خمسة" لـ 2 و3 و4 بتات. 2.83 ≈ 3 و3.89 ≈ 4 و5.03 ≈ 5: تقريباً b إضافية من القص لكل بت إضافي.

Check yourself

اختبر نفسك

EMA with α = 0.2. The previous estimate of rmax is 5 and the current batch measures 10. What is the new estimate?

EMA مع α = 0.2. التقدير السابق لـ rmax هو 5 والدفعة الحالية تقيس 10. ما التقدير الجديد؟

0.2 × 10 + 0.8 × 5 = 2 + 4 = 6. Option A puts α on the old value (0.2 × 5 + 0.8 × 10). Option C is a plain average and D ignores history.

0.2 × 10 + 0.8 × 5 = 2 + 4 = 6. الخيار A يضع α على القيمة القديمة. والخيار C متوسط عادي، والخيار D يتجاهل التاريخ.

Why does activation quantization need range statistics collected before deployment, while weight quantization does not?

لماذا تحتاج activation quantization إلى إحصاءات المدى قبل النشر، بينما لا تحتاجها weight quantization؟

Slide 23: unlike weights, the activation range varies across inputs. The weight range can be read directly from the trained tensor.

الشريحة 23: بخلاف الأوزان، مدى activations يتغير بين المدخلات. أما مدى الأوزان فيُقرأ مباشرة من الـ tensor المدرَّب.

During calibration, why use the mean of each sample's min/max rather than the global min/max over all samples?

أثناء calibration، لماذا نستخدم متوسط min/max لكل عينة بدلاً من min/max الكلي على كل العينات؟

Spending range on outliers hurts representation ability. The mean deliberately clips rare extremes; option C is the opposite of what happens.

صرف المدى على القيم الشاذة يضر بقدرة التمثيل. والمتوسط يقصّ القيم النادرة عمداً، فالخيار C عكس ما يحدث.

Calibration estimates a Laplace b = 1. What is the MSE-optimal clip |r|max for 4-bit quantization?

تقدّر calibration أن b = 1 لتوزيع Laplace. ما قيمة القص المثلى |r|max لـ 4-bit quantization؟

5.03b for 4 bits. 2.83 and 3.89 are the 2-bit and 3-bit values.

5.03b لـ 4 بتات. و2.83 و3.89 هما قيمتا 2 و3 بتات.

Why does the optimal Laplace clip value grow as the bit width increases?

لماذا تكبر قيمة القص المثلى لـ Laplace كلما زاد عدد البتات؟

The MSE balances clipping error against rounding error. Extra bits shrink the rounding term, so the optimum shifts toward a wider clip.

MSE يوازن بين clipping error وrounding error. والبتات الإضافية تقلّل حد rounding، فينتقل الأمثل نحو قص أوسع.