COE 592Lecture 5.3المحاضرة 5.3Part 03الجزء 03
Dynamic range clipping for activations: EMA, calibration, MSE and KLقص المدى الديناميكي للتنشيطات: EMA والمعايرة وMSE وKL
Activations change with every input, so their range must be estimated before deployment, either with an exponential moving average during training or by running calibration batches. Clipping outliers trades clipping error for finer resolution, and the clip point is chosen by minimizing MSE (analytically for Laplace, or with OCTAV's Newton-Raphson) or KL divergence.تتغير التنشيطات مع كل مدخل، لذا يجب تقدير مداها قبل النشر، إما بمتوسط متحرك أسي (EMA) أثناء التدريب أو بتشغيل دفعات معايرة (calibration). قص القيم الشاذة يقايض خطأ القص بدقة تمثيل أنعم، وتُختار نقطة القص بتقليل MSE (تحليليًا لتوزيع Laplace، أو بطريقة Newton-Raphson في OCTAV) أو تباعد KL.
- Conceptsالمفاهيم
- 6
- Slidesالشرائح
- 22-31
- Readingمدة القراءة
- 36 min36 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Linear quantization needs a range before it can do anything. Parts one and two chose how many scales a tensor gets. This part asks a different question: for one scale, which range should it cover? For weights the answer is easy because they sit still. Activations do not, so you have to measure them, and then decide how much of what you measured to throw away.
The thread runs in one direction. First, why activations need data at all. Second and third, the two ways to collect that data: a moving average during training, or calibration batches after it. Then the central trade: cutting off rare outliers costs a little clipping error but buys finer steps for everything else. The last three concepts are three ways to choose the cut: a closed-form MSE table for Laplace data, KL divergence as TensorRT does it, and OCTAV's Newton-Raphson search. For the midterm, expect an EMA update by hand, the Laplace table, and a "why does clipping help" explanation.
يحتاج التكميم الخطي إلى مدى قبل أن يفعل أي شيء. اختار الجزءان الأول والثاني عدد المقاييس (scales) التي يحصل عليها الموتر. أما هذا الجزء فيطرح سؤالًا مختلفًا: لمقياس واحد، ما المدى الذي يجب أن يغطيه؟ في الأوزان الجواب سهل لأنها ثابتة لا تتحرك. التنشيطات ليست كذلك، لذلك عليك أن تقيسها، ثم تقرر كم مما قسته ستتخلص منه.
يسير الخيط في اتجاه واحد. أولًا: لماذا تحتاج التنشيطات إلى بيانات أصلًا. ثانيًا وثالثًا: الطريقتان لجمع تلك البيانات، إما متوسط متحرك أثناء التدريب، أو دفعات معايرة (calibration batches) بعده. ثم المقايضة المحورية: قص القيم الشاذة النادرة يكلّف قليلًا من خطأ القص (clipping error) لكنه يشتري خطوات أدق لكل القيم الأخرى. المفاهيم الثلاثة الأخيرة هي ثلاث طرق لاختيار حد القص: جدول MSE بصيغة مغلقة لبيانات لابلاس، وتباعد KL كما يفعله TensorRT، وبحث نيوتن-رافسون في OCTAV. في اختبار منتصف الفصل توقّع تحديث EMA يدويًا، وجدول لابلاس، وشرحًا لسؤال «لماذا يفيد القص».
By the end you canفي نهاية هذا الجزء ستستطيع
- Explain why weight ranges can be read offline but activation ranges must be estimated from data before deployment.أن تشرح لماذا يمكن قراءة مدى الأوزان دون اتصال (offline)، بينما يجب تقدير مدى التنشيطات من البيانات قبل النشر.
- Update an exponential moving average of r_max and r_min by hand, and state which way the slide's α points.أن تحدّث متوسطًا متحركًا أُسّيًا (EMA) لـ r_max وr_min يدويًا، وأن تحدد على أي حدّ تقع α في صيغة الشريحة: على القيمة الجديدة أم على التقدير القديم.
- Explain why clipping outliers can lower the total quantization error, in terms of clipping error and rounding error.أن تشرح لماذا قد يقلل قص القيم الشاذة الخطأ الكلي للتكميم، من خلال خطأ القص وخطأ التقريب.
- Read the Laplace table (2.83b, 3.89b, 5.03b) and turn a measured b into a clip value.أن تقرأ جدول لابلاس (2.83b و3.89b و5.03b) وتحوّل قيمة b المقيسة إلى قيمة قص.
- Contrast the MSE and KL calibration objectives, and describe how TensorRT's entropy method picks a threshold.أن تقارن بين هدفي المعايرة MSE وKL، وتصف كيف تختار طريقة الإنتروبيا في TensorRT حد العتبة.
- Describe what OCTAV computes with Newton-Raphson and what its int4 results show.أن تصف ما يحسبه OCTAV باستخدام نيوتن-رافسون وما تُظهره نتائجه عند int4.
Take a trained image classifier and look at the output of its third convolution layer. Feed it a dark night photo and the largest activation might be 3.1. Feed it a bright beach photo and the largest might be 11.4. The weights of that layer were identical both times. Only the input changed.
خذ مصنّف صور مدرَّبًا وانظر إلى مخرج طبقة الالتفاف الثالثة فيه. أدخل صورة ليلية مظلمة فقد يكون أكبر تنشيط 3.1. أدخل صورة شاطئ مشرقة فقد يكون الأكبر 11.4. أوزان تلك الطبقة كانت متطابقة في المرتين، والذي تغيّر هو المدخل فقط.
The range is the scaleالمدى هو المقياس
This matters because the range is not a side detail of linear quantization. It is the scale itself. With , the scale is fixed by the range you choose to cover:
هذا مهم لأن المدى ليس تفصيلًا جانبيًا في التكميم الخطي، بل هو المقياس نفسه. مع ، يتحدد المقياس بالمدى الذي تختار أن تغطيه:
So the question on slide 22, how to get the optimal (S, Z) in Post-training quantization (PTQ), is the same question as how to pick the range. The lecture splits it into three topics: granularity (how many scales, the previous two parts), Dynamic range clipping (this part), and rounding (the next part).
إذن سؤال الشريحة 22، أي كيف نحصل على (S, Z) الأمثل في post-training quantization، هو نفسه سؤال كيف نختار المدى. تقسّمه المحاضرة إلى ثلاثة موضوعات: الدقة الحُبيبية (granularity)، أي عدد المقاييس، وهو موضوع الجزأين السابقين، وdynamic range clipping (هذا الجزء)، والتقريب (rounding) في الجزء التالي.
| Weightsالأوزان | Activationsالتنشيطات | |
|---|---|---|
| When the values existمتى توجد القيم | After training, frozenبعد التدريب، وتكون مجمّدة | Only when an input flows through the networkفقط عندما يمر مدخل عبر الشبكة |
| Does the range changeهل يتغير المدى | Neverأبدًا | With every input image, sentence or sensor frameمع كل صورة أو جملة أو إطار حسّاس يدخل |
| How the range is foundكيف يُحدَّد المدى | Read min and max (or a clip) directly from the tensorيُقرأ الحد الأدنى والأعلى (أو حد قص) مباشرة من الموتر | Estimated from data: an EMA during training or calibration batchesيُقدَّر من البيانات: EMA أثناء التدريب أو دفعات معايرة |
| Where the work happensأين يتم العمل | Offline, onceدون اتصال (offline)، مرة واحدة | Offline, before deployment, so the device can use fixed scalesدون اتصال، قبل النشر، حتى يستخدم الجهاز مقاييس ثابتة |
Jacob et al. state the rule plainly: for weights, set the range to the tensor's own min and max. For activations, the range depends on the inputs, so it has to be estimated. Why not just measure each activation's range live, on the device, for every input? You can, and some runtimes do, but it costs an extra pass over every tensor at inference time, and it stops the integer pipeline from folding the scales into fixed constants ahead of time. Edge deployment usually wants fixed scales, so the statistics are gathered before deployment, which is exactly what slide 23 stresses.
يذكر Jacob et al. القاعدة بوضوح: في الأوزان، اجعل المدى هو الحد الأدنى والأعلى للموتر نفسه. أما في التنشيطات فالمدى يعتمد على المدخلات، لذلك يجب تقديره. لماذا لا نقيس مدى كل تنشيط مباشرة على الجهاز لكل مدخل؟ يمكنك ذلك، وبعض بيئات التشغيل تفعله، لكنه يكلّف مرورًا إضافيًا على كل موتر وقت الاستدلال، ويمنع خط المعالجة بالأعداد الصحيحة من دمج المقاييس في ثوابت محددة مسبقًا. النشر على الأجهزة الطرفية يريد عادة مقاييس ثابتة، لذلك تُجمع الإحصاءات قبل النشر، وهذا بالضبط ما تؤكده الشريحة 23.
Recall
Why can a weight tensor be quantized with no data at all, while an activation tensor cannot?لماذا يمكن تكميم موتر الأوزان دون أي بيانات، بينما لا يمكن ذلك لموتر التنشيطات؟
Suppose you are already training with quantization in the loop. Every training step already computes the activations, so you see their min and max for free. One batch says the max is 6.0, the next says 8.0, the next 5.0. Which one do you believe?
افترض أنك تدرّب النموذج والتكميم داخل الحلقة أصلًا. كل خطوة تدريب تحسب التنشيطات بالفعل، فترى حدها الأدنى والأعلى مجانًا. دفعة تقول إن الحد الأعلى 6.0، والتالية تقول 8.0، والتي بعدها 5.0. أيها تصدّق؟
Smooth the range instead of trusting one batchنعّم المدى بدل أن تثق بدفعة واحدة
None of them alone. Type 1 on slide 24, from Jacob et al., keeps an Exponential moving average (EMA) of the observed extremes. Each step blends the newest observation with the running estimate:
لا واحدة منها وحدها. النوع الأول في الشريحة 24، من Jacob et al.، يحتفظ بـ exponential moving average للقيم القصوى المرصودة. كل خطوة تمزج أحدث رصد مع التقدير الجاري:
Unroll it and the weight on an observation from k steps ago is . Old batches never vanish, they fade geometrically. That is why the paper says the ranges are smoothed across thousands of training steps: one freak batch moves the estimate by only a fraction α of its surprise.
افرد الصيغة فيصبح وزن رصدٍ جاء قبل k خطوة هو . الدفعات القديمة لا تختفي أبدًا، بل يتلاشى أثرها هندسيًا. لهذا تقول الورقة إن المديات تُنعَّم عبر آلاف خطوات التدريب: دفعة شاذة واحدة تحرّك التقدير بجزء α فقط من مقدار مفاجأتها.
Worked exampleمثال محلول
Two EMA steps by hand, α = 0.1خطوتا EMA يدويًا، α = 0.1
Initialize from the first batchابدأ من الدفعة الأولى
The first batch gives r_min = -2.0 and r_max = 6.0 (a tensor before its ReLU, so it has negative values). Start the estimate there: r̂_min = -2.0, r̂_max = 6.0.تعطي الدفعة الأولى r_min = -2.0 وr_max = 6.0 (موتر قبل دالة ReLU الخاصة به، لذلك فيه قيم سالبة). ابدأ التقدير من هناك: r̂_min = -2.0 وr̂_max = 6.0.Step 1: observe -3.0 and 8.0الخطوة 1: نرصد -3.0 و8.0
r̂_max = 0.1 × 8.0 + 0.9 × 6.0 = 0.8 + 5.4 = 6.2. r̂_min = 0.1 × (-3.0) + 0.9 × (-2.0) = -0.3 - 1.8 = -2.1.r̂_max = 0.1 × 8.0 + 0.9 × 6.0 = 0.8 + 5.4 = 6.2. r̂_min = 0.1 × (-3.0) + 0.9 × (-2.0) = -0.3 - 1.8 = -2.1.Step 2: observe -1.0 and 5.0الخطوة 2: نرصد -1.0 و5.0
r̂_max = 0.1 × 5.0 + 0.9 × 6.2 = 0.5 + 5.58 = 6.08. r̂_min = 0.1 × (-1.0) + 0.9 × (-2.1) = -0.1 - 1.89 = -1.99.r̂_max = 0.1 × 5.0 + 0.9 × 6.2 = 0.5 + 5.58 = 6.08. r̂_min = 0.1 × (-1.0) + 0.9 × (-2.1) = -0.1 - 1.89 = -1.99.Turn the range into an 8-bit scaleحوّل المدى إلى مقياس 8 بت
S = (6.08 - (-1.99)) / 255 = 8.07 / 255 ≈ 0.0316.S = (6.08 - (-1.99)) / 255 = 8.07 / 255 ≈ 0.0316.Resultالنتيجة
The observed max jumped to 8.0 and then fell to 5.0, but the estimate only moved 6.0 → 6.2 → 6.08. That damping is the point of the EMA.قفز الحد الأعلى المرصود إلى 8.0 ثم هبط إلى 5.0، لكن التقدير تحرّك فقط 6.0 → 6.2 → 6.08. هذا التخميد هو الغاية من EMA.
| Step tالخطوة t | Sourceالمصدر | Observed r_min / r_maxالمرصود r_min / r_max | EMA r̂_min / r̂_maxتقدير EMA r̂_min / r̂_max |
|---|---|---|---|
| 00 | first batchالدفعة الأولى | -2.0 / 6.0-2.0 / 6.0 | -2.00 / 6.00-2.00 / 6.00 |
| 11 | next batchالدفعة التالية | -3.0 / 8.0-3.0 / 8.0 | -2.10 / 6.20-2.10 / 6.20 |
| 22 | next batchالدفعة التالية | -1.0 / 5.0-1.0 / 5.0 | -1.99 / 6.08-1.99 / 6.08 |
Quick checkاختبار سريع
Using the slide's formula with α = 0.1, the running r̂_max is 6.0 and the next batch shows 8.0. What is the new r̂_max?باستخدام صيغة الشريحة مع α = 0.1، التقدير الجاري r̂_max يساوي 6.0 والدفعة التالية تُظهر 8.0. ما قيمة r̂_max الجديدة؟
Now suppose the model is already trained in FP32 and you will not train again. You still need ranges. Type 2 on slide 25 runs a few Calibration batches of ordinary samples through the trained model and records the activations. No labels, no gradients, just forward passes.
افترض الآن أن النموذج مدرَّب مسبقًا بدقة FP32 ولن تدرّبه من جديد. ما زلت تحتاج إلى المديات. النوع الثاني في الشريحة 25 يمرّر بضع calibration batches من العينات العادية عبر النموذج المدرَّب ويسجّل التنشيطات. لا تسميات، ولا تدرجات، مجرد تمريرات أمامية.
One outlier can waste most of the gridقيمة شاذة واحدة قد تهدر معظم الشبكة
The naive choice is the global min and max seen during calibration. Picture four calibration images whose maximum activations in one layer are 5.8, 6.1, 6.0 and 25.0. The last one is a glare spot. With unsigned 8-bit codes and the global max, the step is 25.0 / 255 ≈ 0.098. Nearly all real values live below 6.1, so they get only about 62 of the 255 steps. Three quarters of the codes are reserved for values that almost never occur.
الخيار الساذج هو الحد الأدنى والأعلى الكليّان المرصودان أثناء المعايرة. تخيّل أربع صور معايرة أكبر تنشيطاتها في طبقة ما هي 5.8 و6.1 و6.0 و25.0. الأخيرة بقعة وهج. مع رموز 8 بت بلا إشارة والحد الأعلى الكلي، تكون الخطوة 25.0 / 255 ≈ 0.098. تقريبًا كل القيم الحقيقية تقع تحت 6.1، فلا تحصل إلا على نحو 62 من الخطوات الـ255. ثلاثة أرباع الرموز محجوزة لقيم لا تكاد تظهر أبدًا.
The slide's first fix is to average the per-sample extremes instead of taking the global one: (5.8 + 6.1 + 6.0 + 25.0) / 4 = 10.725. The step becomes 10.725 / 255 ≈ 0.042, more than twice as fine. The glare value now lands beyond the range and is clipped to 10.725. The mean softens the outlier, though it does not remove its pull, which is why the slide also points to analytical methods.
الحل الأول في الشريحة هو أخذ متوسط القيم القصوى لكل عينة بدل القيمة القصوى الكلية: (5.8 + 6.1 + 6.0 + 25.0) / 4 = 10.725. تصبح الخطوة 10.725 / 255 ≈ 0.042، أي أدق بأكثر من الضعف. قيمة الوهج تقع الآن خارج المدى وتُقص إلى 10.725. المتوسط يخفف أثر القيمة الشاذة لكنه لا يزيل جذبها، ولهذا تشير الشريحة أيضًا إلى الطرق التحليلية.
Two errors, one dialخطآن ومقبض واحد
Every clipping decision trades two kinds of error. Values inside the range suffer rounding error, at most half a step and on average in squared terms. Values outside suffer clipping error: they are forced to the edge, and the error grows with how far out they were. Widen the range and the clipping error falls but every step grows. Narrow it and the steps shrink but more values are cut. The total has a minimum somewhere in between, and that is what the next three concepts hunt for.
كل قرار قص يقايض بين نوعين من الخطأ. القيم داخل المدى تعاني خطأ التقريب (rounding error)، وهو نصف خطوة على الأكثر، ومتوسطه بالقيم المربعة. أما القيم خارج المدى فتعاني خطأ القص (clipping error): تُجبر على الحافة، ويكبر الخطأ كلما كانت أبعد. وسّع المدى فينخفض خطأ القص لكن كل خطوة تكبر. ضيّقه فتصغر الخطوات لكن تُقص قيم أكثر. للمجموع قيمة دنيا في مكان ما بينهما، وهذا ما تبحث عنه المفاهيم الثلاثة التالية.
Recall
Name the two error sources in a clipped quantizer and say which way each moves when you widen the range.سمِّ مصدري الخطأ في مكمِّم مقصوص، وقل في أي اتجاه يتحرك كل منهما عندما توسّع المدى.
A calibration run tells you that one layer's activations look like a sharp peak at zero with long, thin tails, and that the mean absolute value is 0.2. You will quantize to 4 bits. Where do you clip?
تخبرك جولة معايرة أن تنشيطات إحدى الطبقات تبدو كقمة حادة عند الصفر مع ذيول طويلة رفيعة، وأن متوسط القيمة المطلقة 0.2. ستكمّم إلى 4 بت. أين تقص؟
Write the error down and minimize itاكتب الخطأ صراحة ثم قلّله
Slide 26 answers with the most direct objective: pick the clip that minimizes the mean squared error between the input X and its reconstruction Q(X).
تجيب الشريحة 26 بالهدف الأكثر مباشرة: اختر حد القص الذي يقلل متوسط مربع الخطأ (MSE) بين المدخل X وإعادة بنائه Q(X).
To solve it you need the distribution of X. Banner et al. observe that activations are often close to Gaussian or Laplace. For a Laplace(0, b) input quantized with M bits over [-α, α] (their notation, α is the clip), the two error terms have a closed form:
لحلها تحتاج إلى توزيع X. يلاحظ Banner et al. أن التنشيطات كثيرًا ما تكون قريبة من التوزيع الغاوسي أو توزيع لابلاس. لمدخل Laplace(0, b) مكمَّم بـM بت على [-α, α] (برموزهم، α هي حد القص)، يكون لحدّي الخطأ صيغة مغلقة:
Set the derivative to zero, , and solve numerically. Because α only ever appears as α/b, the answer is a pure multiple of b. That multiple is the Laplace optimal clipping table:
اجعل المشتقة صفرًا، ، وحلّ عدديًا. وبما أن α لا تظهر إلا في الصورة α/b، فالجواب مضاعف خالص لـb. هذا المضاعف هو جدول Laplace clipping:
The scale parameter is estimated from the calibration data as the mean absolute deviation, . For our layer that is the 0.2 we measured.
يُقدَّر معامل المقياس من بيانات المعايرة بوصفه متوسط الانحراف المطلق، . في طبقتنا هذا هو الـ0.2 الذي قسناه.
Worked exampleمثال محلول
Laplace clip for b = 0.2حد قص لابلاس عند b = 0.2
4 bits4 بت
|r|max = 5.03 × 0.2 = 1.006. Step Δ = 2 × 1.006 / 16 ≈ 0.126.|r|max = 5.03 × 0.2 = 1.006. الخطوة Δ = 2 × 1.006 / 16 ≈ 0.126.Price the two errors at that clipاحسب كلفة الخطأين عند حد القص هذا
Clipping: 2 × 0.04 × e^(-5.03) ≈ 0.08 × 0.00654 ≈ 0.00052. Rounding: 1.006² / 768 ≈ 0.00132. Total ≈ 0.00184.القص: 2 × 0.04 × e^(-5.03) ≈ 0.08 × 0.00654 ≈ 0.00052. التقريب: 1.006² / 768 ≈ 0.00132. المجموع ≈ 0.00184.Compare with a wide range of 2.0 (10b)قارن بمدى واسع قدره 2.0 (10b)
Clipping: 0.08 × e^(-10) ≈ 0.0000036. Rounding: 4 / 768 ≈ 0.00521. Total ≈ 0.00521, about 2.8× worse, even though almost nothing was clipped.القص: 0.08 × e^(-10) ≈ 0.0000036. التقريب: 4 / 768 ≈ 0.00521. المجموع ≈ 0.00521، أي أسوأ بنحو 2.8×، مع أنه لم يُقص شيء تقريبًا.2 bits2 بت
|r|max = 2.83 × 0.2 = 0.566, so Δ = 2 × 0.566 / 4 ≈ 0.283.|r|max = 2.83 × 0.2 = 0.566، إذن Δ = 2 × 0.566 / 4 ≈ 0.283.Resultالنتيجة
At 4 bits clip at 1.006; at 2 bits clip at 0.566. Fewer bits, tighter clip.عند 4 بت قص عند 1.006؛ وعند 2 بت قص عند 0.566. بتات أقل، قص أضيق.
| Bits Mعدد البتات M | Optimal clipحد القص الأمثل | Clip at b = 0.2حد القص عند b = 0.2 | Step Δالخطوة Δ | Fraction of values clippedنسبة القيم المقصوصة |
|---|---|---|---|---|
| 22 | 2.83b2.83b | 0.5660.566 | 0.2830.283 | 5.9%5.9% |
| 33 | 3.89b3.89b | 0.7780.778 | 0.1940.194 | 2.0%2.0% |
| 44 | 5.03b5.03b | 1.0061.006 | 0.1260.126 | 0.65%0.65% |
Uses the Banner et al. model: MSE ≈ clipping + |r|max² / (3 · 4^M), with the clipping term 2b² e^(-|r|max / b) for Laplace and a numerical integral for Gaussian. Values are in units of the scale parameter, so multiply the best clip by your measured b or σ. The rounding model assumes the density is flat inside each bin, so it is rough at 2 bits.نستخدم نموذج Banner et al.: MSE ≈ clipping + |r|max² / (3 · 4^M)، حيث حد القص يساوي 2b² e^(-|r|max / b) لتوزيع Laplace، وتكاملًا عدديًا لتوزيعGaussian. القيم معطاة بوحدات معامل المقياس للتوزيع، لذا اضرب أفضل قيمة قص في قيمة b أو σ التي قستها. يفترض نموذج التقريب أن الكثافة ثابتة داخل كل خانة، لذلك يكون تقريبيًا عند 2 bits.
Quick checkاختبار سريع
A layer's calibration data gives b = 0.3. What clip does the Laplace table give at 3 bits?تعطي بيانات المعايرة لإحدى الطبقات b = 0.3. ما حد القص الذي يعطيه جدول لابلاس عند 3 بت؟
Quick checkاختبار سريع
Clipping a Laplace tensor at 5.03b instead of 10b at 4 bits lowers the total MSE. Why?قص موتر لابلاسي عند 5.03b بدل 10b عند 4 بت يخفض MSE الكلي. لماذا؟
Recall
For b = 0.5, give the 2-bit and 4-bit clips and the fraction of values clipped in each case.عند b = 0.5، أعطِ حد القص عند 2 بت و4 بت ونسبة القيم المقصوصة في كل حالة.
Look at slide 27's histogram for VGG-19 conv3_4. The counts fall over seven orders of magnitude between 0 and about 25,000. No neat Laplace formula is going to describe that tail, so a clip read from an assumed shape is on shaky ground. NVIDIA's TensorRT took a different route in 2017: compare distributions instead of values.
انظر إلى المدرّج التكراري في الشريحة 27 لطبقة VGG-19 conv3_4. تنخفض الأعداد عبر سبع مراتب عشرية بين 0 ونحو 25,000. لن تصف أي صيغة لابلاس أنيقة ذلك الذيل، لذلك فحد القص المقروء من شكل مفترض يقف على أرض هشة. سلكت TensorRT من NVIDIA طريقًا مختلفًا في 2017: قارن بين التوزيعات بدل القيم.
KL divergence: how much information the quantized histogram losesتباعد KL: كم من المعلومات يفقد المدرّج التكراري المكمَّم
The idea on slide 27 is that the INT8 model should encode the same information as the FP32 model. Information loss between two distributions is measured by KL divergence:
الفكرة في الشريحة 27 أن نموذج INT8 يجب أن يرمّز المعلومات نفسها التي يرمّزها نموذج FP32. فقدان المعلومات بين توزيعين يُقاس بـKL divergence:
Worked exampleمثال محلول
KL on three binsتباعد KL على ثلاث خانات
Set upالإعداد
Let P = [0.5, 0.3, 0.2] and Q = [0.4, 0.4, 0.2], using the natural log.لتكن P = [0.5, 0.3, 0.2] وQ = [0.4, 0.4, 0.2]، مع استخدام اللوغاريتم الطبيعي.Sum the termsاجمع الحدود
0.5 ln(0.5/0.4) + 0.3 ln(0.3/0.4) + 0.2 ln(1) = 0.5 × 0.2231 + 0.3 × (-0.2877) + 0 = 0.1116 - 0.0863.0.5 ln(0.5/0.4) + 0.3 ln(0.3/0.4) + 0.2 ln(1) = 0.5 × 0.2231 + 0.3 × (-0.2877) + 0 = 0.1116 - 0.0863.Resultالنتيجة
D_KL(P || Q) ≈ 0.0253 nats. Swapping the arguments gives ≈ 0.0258: KL is not symmetric, so the order matters. TensorRT uses the FP32 histogram as P.D_KL(P || Q) ≈ 0.0253 نات (nats). تبديل الوسيطين يعطي ≈ 0.0258: تباعد KL ليس متماثلًا، لذلك الترتيب مهم. تستخدم TensorRT المدرّج التكراري لـFP32 بوصفه P.
How the threshold is chosenكيف تُختار العتبة
- Run the calibration batches and build a fine histogram of each layer's activation values.شغّل دفعات المعايرة وابنِ مدرّجًا تكراريًا دقيقًا لقيم التنشيط في كل طبقة.
- Try many candidate thresholds. For each, form the reference P: the histogram up to the threshold, with every value beyond it counted at the threshold, because that is where clipping will put it. This is the single detached point in the right panel of slide 28.جرّب عتبات مرشحة كثيرة. لكل واحدة كوّن المرجع P: المدرّج التكراري حتى العتبة، مع احتساب كل قيمة تتجاوزها عند العتبة نفسها، لأن القص سيضعها هناك. هذه هي النقطة المنفصلة الوحيدة في اللوحة اليمنى من الشريحة 28.
- Quantize that histogram into the 8-bit levels to get Q, and compute D_KL(P || Q).كمّم ذلك المدرّج التكراري إلى مستويات 8 بت لتحصل على Q، واحسب D_KL(P || Q).
- Keep the threshold with the smallest divergence. Repeat for every layer.احتفظ بالعتبة ذات أصغر تباعد. كرّر ذلك لكل طبقة.
Slide 28 shows the trade on ResNet-152 res4b30. The entropy method sets the threshold near 45 although values reach about 470. The slide's question, what happens with a higher r_max, has the answer it gives: a larger dynamic range means larger steps, so a larger quantization (rounding) loss on the dense part of the histogram, where almost all values live.
تُظهر الشريحة 28 المقايضة على طبقة ResNet-152 res4b30. تضع طريقة الإنتروبيا العتبة قرب 45 مع أن القيم تصل إلى نحو 470. وسؤال الشريحة، ماذا يحدث مع r_max أعلى، جوابه كما تعطيه: مدى ديناميكي أكبر يعني خطوات أكبر، أي خسارة تكميم (تقريب) أكبر في الجزء الكثيف من المدرّج التكراري، حيث تعيش تقريبًا كل القيم.
Every layer gets its own answerلكل طبقة جوابها الخاص
Slide 29 is the best evidence that no fixed rule (say, clip at the 99th percentile) would work. The thresholds read from its four panels:
الشريحة 29 هي أفضل دليل على أنه لا توجد قاعدة ثابتة (مثل القص عند المئين 99) تنجح. العتبات المقروءة من لوحاتها الأربع:
| Layerالطبقة | Largest valueأكبر قيمة | KL thresholdعتبة KL | How much is clippedمقدار ما يُقص |
|---|---|---|---|
| AlexNet pool2AlexNet pool2 | about 140نحو 140 | about 140نحو 140 | None: a spike of real values sits at the top of the rangeلا شيء: قمة من القيم الحقيقية تقع عند أعلى المدى |
| GoogLeNet inception_3a/poolGoogLeNet inception_3a/pool | about 140نحو 140 | about 140نحو 140 | None, for the same reasonلا شيء، للسبب نفسه |
| GoogLeNet inception_5a/5x5GoogLeNet inception_5a/5x5 | about 175نحو 175 | about 137نحو 137 | Mild: only the sparse far tail is cutخفيف: لا يُقص إلا الذيل البعيد المتناثر |
| ResNet-152 res4b8_branch2aResNet-152 res4b8_branch2a | about 210نحو 210 | about 15نحو 15 | Heavy: almost all mass sits near zeroشديد: تقريبًا كل الكتلة الاحتمالية تقع قرب الصفر |
The two pooling layers keep their full range because their histograms rise into a spike at the top: many real values sit there, so cutting them would distort the distribution badly. ResNet's branch layer is the opposite: a tall peak near zero and a tail of rare values, so the method clips about 93% of the range away and loses almost no probability mass.
طبقتا التجميع (pooling) تحتفظان بمداهما الكامل لأن مدرّجهما التكراري يرتفع إلى قمة عند الطرف الأعلى: قيم حقيقية كثيرة تقع هناك، فقصها سيشوّه التوزيع بشدة. طبقة الفرع في ResNet هي العكس: قمة عالية قرب الصفر وذيل من القيم النادرة، لذلك تقص الطريقة نحو 93% من المدى ولا تكاد تفقد شيئًا من الكتلة الاحتمالية.
| Methodالطريقة | What it minimizes or computesما الذي تقلله أو تحسبه | Characterطبيعتها |
|---|---|---|
| Mean of per-sample min/maxمتوسط الحد الأدنى والأعلى لكل عينة | Average the extremes of each calibration sampleنأخذ متوسط القيم القصوى لكل عينة معايرة | Cheap, softens one-off outliers, ignores the shape of the bulkرخيصة، تخفف أثر القيم الشاذة العارضة، وتتجاهل شكل الكتلة الكثيفة |
| Minimize MSEتقليل MSE | E[(X - Q(X))²]E[(X - Q(X))²] | Penalizes large value errors; analytical model for Laplace and Gaussian, or OCTAV for any dataتعاقب أخطاء القيم الكبيرة؛ نموذج تحليلي لتوزيعي لابلاس والغاوسي، أو OCTAV لأي بيانات |
| Minimize KL divergenceتقليل تباعد KL | D_KL(P || Q)D_KL(P || Q) | Compares histogram shapes; TensorRT's default entropy calibration for INT8تقارن أشكال المدرّجات التكرارية؛ وهي معايرة الإنتروبيا الافتراضية في TensorRT لـINT8 |
Quick checkاختبار سريع
Which statement best separates the MSE and KL calibration objectives?أي العبارات تفرّق بين هدفي المعايرة MSE وKL على أفضل وجه؟
Recall
Why do AlexNet pool2 and GoogLeNet inception_3a/pool get no clipping from the entropy method?لماذا لا تقص طريقة الإنتروبيا شيئًا من AlexNet pool2 وGoogLeNet inception_3a/pool؟
Slide 30 puts two int4 grids over the same tensor. On the left, 16 levels are spread from -0.8 to 0.8, about 0.1 apart. The tensor's density is a narrow peak within roughly ±0.15, so only about three levels touch it: large quantization noise exactly where the data is, and most levels spent on low-density regions. On the right, the grid is clipped to about ±0.2. The same 16 levels are now about 0.025 apart and almost all of them cover the peak.
تضع الشريحة 30 شبكتي int4 فوق الموتر نفسه. على اليسار، 16 مستوى موزعة من -0.8 إلى 0.8، المسافة بينها نحو 0.1. كثافة الموتر قمة ضيقة ضمن نحو ±0.15، فلا يلامسها إلا نحو ثلاثة مستويات: ضجيج تكميم كبير تمامًا حيث توجد البيانات، ومعظم المستويات مهدورة على مناطق منخفضة الكثافة. على اليمين، الشبكة مقصوصة إلى نحو ±0.2. المستويات الـ16 نفسها صارت المسافة بينها نحو 0.025، وتقريبًا كلها تغطي القمة.
Find the MSE minimum without assuming a shapeأوجد أصغر MSE دون افتراض شكل للتوزيع
The Laplace table needs the data to be Laplace. OCTAV (Sakr et al., NVIDIA, ICML 2022) drops that assumption. It writes the clipped quantization MSE as a function of the clip scalar s, using the data itself:
يحتاج جدول لابلاس إلى أن تكون البيانات لابلاسية. أما OCTAV (Sakr et al.، NVIDIA، ICML 2022) فيتخلى عن هذا الافتراض. يكتب MSE التكميم المقصوص دالةً في قيمة القص s، مستخدمًا البيانات نفسها:
Newton-Raphson finds a minimum by repeating . Working out the derivatives turns that into one compact update that needs only a few sums over the tensor:
تجد طريقة نيوتن-رافسون القيمة الدنيا بتكرار . وبحساب المشتقات يتحول ذلك إلى تحديث واحد مختصر لا يحتاج إلا إلى بضعة مجاميع على الموتر:
Worked exampleمثال محلول
OCTAV on Laplace data, 4 bitsOCTAV على بيانات لابلاس، 4 بت
Plug in the Laplace expectationsعوّض توقعات لابلاس
For Laplace(0, 1): E[|X| 1(|X| > s)] = (s + 1)e^(-s) and P(|X| > s) = e^(-s). With B = 4, 4^(-4)/3 = 1/768.لتوزيع Laplace(0, 1): E[|X| 1(|X| > s)] = (s + 1)e^(-s) وP(|X| > s) = e^(-s). ومع B = 4 يكون 4^(-4)/3 = 1/768.Iterate from a poor guess, s = 1كرّر بدءًا من تخمين سيئ، s = 1
s₂ = 2 × 0.368 / (0.632/768 + 0.368) ≈ 2.00, then the values below.s₂ = 2 × 0.368 / (0.632/768 + 0.368) ≈ 2.00، ثم القيم في الجدول أدناه.Resultالنتيجة
It settles at s ≈ 5.03 in about six steps: the same 5.03b as Banner's table, reached without solving anything by hand.يستقر عند s ≈ 5.03 في نحو ست خطوات: القيمة 5.03b نفسها في جدول Banner، دون حل أي شيء يدويًا.
| Iteration nالتكرار n | Clip s_nحد القص s_n |
|---|---|
| 11 | 1.001.00 |
| 22 | 2.002.00 |
| 33 | 2.972.97 |
| 44 | 3.883.88 |
| 55 | 4.594.59 |
| 66 | 4.964.96 |
| 77 | 5.035.03 |
Because each step is a handful of reductions over the tensor, OCTAV is cheap enough to rerun every training iteration: it "finds optimal scalars on the fly", as slide 31 puts it. The paper uses only 10 iterations everywhere and reports that the starting guess does not matter.
لأن كل خطوة ليست سوى بضع عمليات اختزال على الموتر، فإن OCTAV رخيص بما يكفي لإعادة تشغيله في كل تكرار تدريب: إنه «يجد القيم المثلى أثناء التشغيل»، كما تقول الشريحة 31. تستخدم الورقة 10 تكرارات فقط في كل مكان، وتذكر أن التخمين الابتدائي لا يهم.
| Networkالشبكة | FP32FP32 | OCTAV int4OCTAV int4 | Changeالتغيّر |
|---|---|---|---|
| ResNet-50ResNet-50 | 76.0776.07 | 75.8475.84 | -0.23-0.23 |
| MobileNet-V2MobileNet-V2 | 71.7171.71 | 70.8870.88 | -0.83-0.83 |
| BERT-LargeBERT-Large | 91.0091.00 | 87.0987.09 | -3.91-3.91 |
The CNNs lose under one point at 4 bits. BERT-Large loses almost four, a reminder that transformers have harder activation outliers, the same networks Wu et al. found hardest for max calibration.
تخسر شبكات CNN أقل من نقطة واحدة عند 4 بت. أما BERT-Large فيخسر نحو أربع نقاط، وهذا تذكير بأن المحوّلات (transformers) فيها قيم شاذة أصعب في التنشيطات، وهي الشبكات نفسها التي وجدها Wu et al. الأصعب مع معايرة max.
Recall
On slide 30, why does max-scaled int4 quantization give large noise even though no value is clipped?في الشريحة 30، لماذا يعطي تكميم int4 المضبوط على القيمة القصوى ضجيجًا كبيرًا مع أنه لا تُقص أي قيمة؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Weights are fixed after training, so their range is read directly. Activations change with every input, so their range is estimated from data before deployment.الأوزان ثابتة بعد التدريب، لذلك يُقرأ مداها مباشرة. أما التنشيطات فتتغير مع كل مدخل، لذلك يُقدَّر مداها من البيانات قبل النشر.
- Choosing r_max and r_min is choosing the scale S, and the scale sets the step size of every quantized value in that tensor.اختيار r_max وr_min هو نفسه اختيار المقياس S، والمقياس يحدد حجم الخطوة لكل قيمة مكمّمة في ذلك الموتر.
- Type 1 (Jacob et al.): during training, smooth the observed min and max with an EMA, r̂(t) = α r(t) + (1 - α) r̂(t-1). With α on the new value, α must be small (PyTorch uses 0.01).النوع الأول (Jacob et al.): أثناء التدريب نُنعّم الحد الأدنى والأعلى المرصودين بمتوسط متحرك أُسّي EMA، أي r̂(t) = α r(t) + (1 - α) r̂(t-1). وبما أن α تضرب القيمة الجديدة، يجب أن تكون α صغيرة (PyTorch تستخدم 0.01).
- Type 2: run a few calibration batches through the trained FP32 model. Options: mean of per-sample min/max, minimize MSE, minimize KL divergence.النوع الثاني: نمرّر بضع دفعات معايرة عبر نموذج FP32 المدرَّب. الخيارات: متوسط الحد الأدنى والأعلى لكل عينة، أو تقليل MSE، أو تقليل تباعد KL.
- Total error = clipping error (values beyond the clip) + rounding error (Δ²/12 with Δ = 2|r|max / 2^M). Widening the range lowers the first and raises the second.الخطأ الكلي = خطأ القص (للقيم التي تتجاوز حد القص) + خطأ التقريب (Δ²/12 حيث Δ = 2|r|max / 2^M). توسيع المدى يقلل الأول ويزيد الثاني.
- For Laplace(0, b) inputs the MSE-optimal clip is 2.83b, 3.89b, 5.03b at 2, 3, 4 bits. Example: b = 0.2 at 4 bits gives 1.006.لمدخلات Laplace(0, b) يكون حد القص الأمثل وفق MSE هو 2.83b و3.89b و5.03b عند 2 و3 و4 بت. مثال: b = 0.2 عند 4 بت يعطي 1.006.
- KL calibration (TensorRT entropy method) picks the threshold whose quantized histogram loses the least information, D_KL(P || Q) = Σ P log(P/Q). It chooses a different clip for every layer.معايرة KL (طريقة الإنتروبيا في TensorRT) تختار العتبة التي يفقد عندها المدرّج التكراري المكمَّم أقل قدر من المعلومات، D_KL(P || Q) = Σ P log(P/Q). وهي تختار حد قص مختلفًا لكل طبقة.
- OCTAV solves the MSE problem with Newton-Raphson in a few iterations, on the fly. Int4 results: ResNet-50 76.07 to 75.84, MobileNet-V2 71.71 to 70.88, BERT-Large 91.00 to 87.09.يحل OCTAV مسألة MSE بطريقة نيوتن-رافسون في بضع تكرارات وأثناء التشغيل. نتائج int4: ResNet-50 من 76.07 إلى 75.84، وMobileNet-V2 من 71.71 إلى 70.88، وBERT-Large من 91.00 إلى 87.09.
Sourcesالمصادر
- Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference (Jacob et al.)PaperCVPR 2018, arXiv:1712.05877Weights use their own min and max; activation ranges are aggregated with an EMA whose smoothing parameter is close to 1, and activation quantization is disabled for the first 50 thousand to 2 million steps.تستخدم الأوزان حدها الأدنى والأعلى الخاصين بها؛ وتُجمع مديات التنشيطات بمتوسط EMA معامل تنعيمه قريب من 1، ويُعطَّل تكميم التنشيطات في أول 50 ألف إلى 2 مليون خطوة.(opens in a new tab)(يفتح في تبويب جديد)
- Post training 4-bit quantization of convolutional networks for rapid-deployment (Banner, Nahshan, Soudry)PaperNeurIPS 2019Eq. 5 and 6: the Laplace MSE model, optimal clips 2.83b, 3.89b, 5.03b for 2, 3, 4 bits, and b estimated as E|X - E(X)|.المعادلتان 5 و6: نموذج MSE لتوزيع لابلاس، وحدود القص المثلى 2.83b و3.89b و5.03b عند 2 و3 و4 بت، وتقدير b بالصيغة E|X - E(X)|.(opens in a new tab)(يفتح في تبويب جديد)
- Optimal Clipping and Magnitude-aware Differentiation for Improved Quantization-aware Training (Sakr et al.)PaperICML 2022, arXiv:2206.06501OCTAV: Theorem 3.1 gives the Newton-Raphson recursion for the MSE-optimal clip; Section 3 discusses convexity and 10-iteration convergence; the experiments note rare non-convex layers.OCTAV: تعطي المبرهنة 3.1 علاقة نيوتن-رافسون التكرارية لحد القص الأمثل وفق MSE؛ ويناقش القسم 3 التحدّب والتقارب في 10 تكرارات؛ وتشير التجارب إلى طبقات نادرة غير محدّبة.(opens in a new tab)(يفتح في تبويب جديد)
- Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation (Wu et al.)PaperNVIDIA, arXiv:2004.09602Table 5 and the calibration section: max, entropy (KL, TensorRT's default) and percentile calibration compared; max suffers on networks with outliers, 99.9% clips too aggressively.الجدول 5 وقسم المعايرة: مقارنة بين معايرة max والإنتروبيا (KL، الافتراضية في TensorRT) والمئين؛ معايرة max تتعثر في الشبكات ذات القيم الشاذة، والمئين 99.9% يقص بشدة زائدة.(opens in a new tab)(يفتح في تبويب جديد)
- MovingAverageMinMaxObserverDocsPyTorch documentationEMA of min and max with the constant on the new observation, averaging_constant = 0.01 by default.متوسط EMA للحد الأدنى والأعلى يضع الثابت على الرصد الجديد، مع averaging_constant = 0.01 افتراضيًا.(opens in a new tab)(يفتح في تبويب جديد)
- TensorRT Python API: Int8 CalibratorDocsNVIDIA TensorRT documentationLists the ENTROPY_CALIBRATION, ENTROPY_CALIBRATION_2 and MINMAX_CALIBRATION algorithms; the implicit calibration API is deprecated since TensorRT 10.1 in favor of explicit quantization.تسرد خوارزميات ENTROPY_CALIBRATION وENTROPY_CALIBRATION_2 وMINMAX_CALIBRATION؛ وواجهة المعايرة الضمنية مُهملة (deprecated) منذ TensorRT 10.1 لصالح التكميم الصريح.(opens in a new tab)(يفتح في تبويب جديد)